luat_lib_napt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. @module natp
  3. @summary 网络地址端口转换(开发中)
  4. @version 1.0
  5. @date 2024.4.22
  6. @auther wendal
  7. @tag LUAT_USE_NAPT
  8. @usage
  9. -- 开发中, 请关注 https://github.com/wendal/xt804-spinet
  10. */
  11. #include "luat_base.h"
  12. #include "luat_ulwip.h"
  13. #include "luat_napt.h"
  14. #include "luat_crypto.h"
  15. #include "luat_zbuff.h"
  16. #include "luat_network_adapter.h"
  17. #define LUAT_LOG_TAG "napt"
  18. #include "luat_log.h"
  19. extern uint8_t napt_target_adapter;
  20. /*
  21. 初始化NAPT
  22. @api napt.init(adapter)
  23. @int adapter 目标网卡索引, 默认是socket.LWIP_AP, 这里指内网
  24. @return nil 无返回值
  25. */
  26. static int l_napt_init(lua_State *L) {
  27. if (lua_isinteger(L, 1)) {
  28. napt_target_adapter = lua_tointeger(L, 1);
  29. }
  30. else {
  31. napt_target_adapter = NW_ADAPTER_INDEX_LWIP_WIFI_AP;
  32. }
  33. return 0;
  34. }
  35. // EC618/EC7XX的特殊逻辑
  36. #if ENABLE_PSIF
  37. #ifndef UINT8
  38. #define UINT8 uint8_t
  39. #endif
  40. #ifndef UINT16
  41. #define UINT16 uint16_t
  42. #endif
  43. #ifndef UINT32
  44. #define UINT32 uint32_t
  45. #endif
  46. #ifndef BOOL
  47. #define BOOL uint8_t
  48. #endif
  49. static void toPs(void *ctx)
  50. {
  51. extern BOOL PsifRawUlOutput(UINT8, UINT8 *, UINT16);
  52. uint8_t *pkt = (uint8_t *)((void **)ctx)[0];
  53. uint32_t len = (uint32_t)((void **)ctx)[1];
  54. uint8_t ipv = pkt[0];
  55. ipv = ((ipv >> 4) & 0x0F);
  56. UINT8 cid = ipv == 4 ? 1 : 2;
  57. int rc = PsifRawUlOutput(cid, pkt, len);
  58. if (rc != 1) {
  59. LLOGE("PsifRawUlOutput rc %d", rc);
  60. }
  61. luat_heap_free(ctx);
  62. luat_heap_free(pkt);
  63. }
  64. static void sendIp2Ps(uint8_t *pkt, uint32_t len, struct netif* netif)
  65. {
  66. void **param = (void **)luat_heap_malloc(sizeof(void *) * 2);
  67. if (param == NULL) {
  68. luat_heap_free(pkt);
  69. LLOGE("no mem for sendIp2Ps");
  70. return;
  71. }
  72. param[0] = (void *)pkt;
  73. param[1] = (void *)len;
  74. int rc = tcpip_callback(toPs, param);
  75. if (rc) {
  76. luat_heap_free(param);
  77. luat_heap_free(pkt);
  78. }
  79. }
  80. #endif
  81. /*
  82. 重建MAC包
  83. @api napt.rebuild(buff, is_inet, adapter)
  84. @userdata 待处理的MAC包,必须是zbuff对象
  85. @bool 来源是不是内网
  86. @int 目标网络适配器的索引, 例如socket.LWIP_GP
  87. @return bool 成功返回true,失败返回false
  88. */
  89. static int l_napt_rebuild(lua_State *L) {
  90. luat_zbuff_t* zbuff = tozbuff(L);
  91. u8 is_inet = lua_toboolean(L, 2);
  92. ip_addr_t gw_ip = {0};
  93. struct netif* netif = NULL;
  94. int ip = -1;
  95. ip = luaL_checkinteger(L, 3);
  96. if (ip < 0) {
  97. return 0;
  98. }
  99. if (ip >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
  100. return 0;
  101. }
  102. netif = ulwip_find_netif(ip);
  103. if (netif == NULL) {
  104. return 0;
  105. }
  106. ip_addr_set_ip4_u32(&gw_ip, ip_addr_get_ip4_u32(&netif->ip_addr));
  107. int dlen = zbuff->len - zbuff->used;
  108. uint8_t* data = (uint8_t*)zbuff->addr + zbuff->used;
  109. int rc = luat_napt_input(is_inet, (u8*)data, dlen, &gw_ip);
  110. if (!rc) {
  111. if (is_inet)
  112. memcpy(data, netif->hwaddr, 6);
  113. else {
  114. // LLOGD("赋值mac %02x:%02x:%02x:%02x:%02x:%02x", netif->hwaddr[0], netif->hwaddr[1], netif->hwaddr[2],
  115. // netif->hwaddr[3], netif->hwaddr[4], netif->hwaddr[5]);
  116. memcpy(data + 6, netif->hwaddr, 6);
  117. }
  118. #if ENABLE_PSIF
  119. if (ip == NW_ADAPTER_INDEX_LWIP_GPRS) {
  120. char* tmp = luat_heap_malloc(dlen - 14);
  121. if (!tmp) {
  122. LLOGE("no mem for sendIp2Ps");
  123. return 0;
  124. }
  125. memcpy(tmp, data + 14, dlen - 14);
  126. sendIp2Ps((uint8_t*)tmp, dlen - 14, netif);
  127. lua_pushboolean(L, 1);
  128. return 1;
  129. }
  130. #endif
  131. lua_pushboolean(L, 1);
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. /*
  137. 检查和清理NAT表
  138. @api napt.check()
  139. @return nil 无返回值
  140. @usage
  141. -- 两次check之间没有数据包的映射记录,会被清理
  142. */
  143. static int l_napt_check(lua_State *L) {
  144. luat_napt_table_check(NULL);
  145. return 0;
  146. }
  147. #include "rotable2.h"
  148. static const rotable_Reg_t reg_napt[] =
  149. {
  150. { "init" , ROREG_FUNC(l_napt_init)},
  151. { "rebuild", ROREG_FUNC(l_napt_rebuild)},
  152. { "check", ROREG_FUNC(l_napt_check)},
  153. { NULL, ROREG_INT(0)}
  154. };
  155. LUAMOD_API int luaopen_napt( lua_State *L ) {
  156. luat_napt_init();
  157. luat_newlib2(L, reg_napt);
  158. return 1;
  159. }