Explorar o código

update: netdrv,napt,完善新的实现,跑2天没死机

Wendal Chen hai 9 meses
pai
achega
77b626c7c9

+ 1 - 1
components/network/adapter_lwip2/net_lwip2.c

@@ -765,7 +765,7 @@ static void net_lwip2_task(void *param)
 		}
 		else
 		{
-			NET_DBG("udp bind %d", prvlwip.socket[socket_id].local_port);
+			// NET_DBG("udp bind %d", prvlwip.socket[socket_id].local_port);
 			error = udp_bind(prvlwip.socket[socket_id].pcb.udp, local_ip, prvlwip.socket[socket_id].local_port);
 			if (error) {
 				NET_DBG("udp bind ret %d port %d", error, prvlwip.socket[socket_id].local_port);

+ 5 - 1
components/network/netdrv/src/ch390h_task.c

@@ -409,7 +409,11 @@ static int task_loop(ch390h_t *ch, luat_ch390h_cstring_t* cs) {
     }
     if (ret) {
         pkg_evt_t evt = {0};
-        luat_rtos_queue_send(qt, &evt, sizeof(pkg_evt_t), 0);
+        size_t t = 0;
+        luat_rtos_queue_get_cnt(qt, &t);
+        if (t < 4) {
+            luat_rtos_queue_send(qt, &evt, sizeof(pkg_evt_t), 0);
+        }
     }
     return ret;
 }

+ 22 - 4
components/network/netdrv/src/luat_netdrv.c

@@ -14,6 +14,10 @@
 #define LUAT_LOG_TAG "netdrv"
 #include "luat_log.h"
 
+#ifndef __USER_FUNC_IN_RAM__
+#define __USER_FUNC_IN_RAM__ 
+#endif
+
 static luat_netdrv_t* drvs[NW_ADAPTER_QTY];
 
 luat_netdrv_t* luat_netdrv_ch390h_setup(luat_netdrv_conf_t *conf);
@@ -126,7 +130,7 @@ void luat_netdrv_print_pkg(const char* tag, uint8_t* buff, size_t len) {
 
 #define NAPT_CHKSUM_16BIT_LEN        sizeof(uint16_t)
 
-uint32_t alg_hdr_16bitsum(const uint16_t *buff, uint16_t len)
+__USER_FUNC_IN_RAM__ uint32_t alg_hdr_16bitsum(const uint16_t *buff, uint16_t len)
 {
     uint32_t sum = 0;
 
@@ -147,7 +151,7 @@ uint32_t alg_hdr_16bitsum(const uint16_t *buff, uint16_t len)
     return sum;
 }
 
-uint16_t alg_iphdr_chksum(const uint16_t *buff, uint16_t len)
+__USER_FUNC_IN_RAM__ uint16_t alg_iphdr_chksum(const uint16_t *buff, uint16_t len)
 {
     uint32_t sum = alg_hdr_16bitsum(buff, len);
 
@@ -157,7 +161,7 @@ uint16_t alg_iphdr_chksum(const uint16_t *buff, uint16_t len)
     return (uint16_t)(~sum);
 }
 
-uint16_t alg_tcpudphdr_chksum(uint32_t src_addr, uint32_t dst_addr, uint8_t proto, const uint16_t *buff, uint16_t len)
+__USER_FUNC_IN_RAM__ uint16_t alg_tcpudphdr_chksum(uint32_t src_addr, uint32_t dst_addr, uint8_t proto, const uint16_t *buff, uint16_t len)
 {
     uint32_t sum = 0;
 
@@ -215,7 +219,13 @@ int luat_netdrv_netif_input_proxy(struct netif * netif, uint8_t* buff, uint16_t
     memcpy(ptr->buff, buff, len);
     ptr->netif = netif;
     ptr->len = len;
-    int ret = tcpip_callback(luat_netdrv_netif_input, ptr);
+    // uint64_t tbegin = luat_mcu_tick64();
+    int ret = tcpip_callback_with_block(luat_netdrv_netif_input, ptr, 0);
+    // uint64_t tend = luat_mcu_tick64();
+    // uint64_t tused = (tend - tbegin) / luat_mcu_us_period();
+    // if (tused > 50) {
+    //     LLOGD("tcpip_callback!! use %lld us", tused);
+    // }
     if (ret) {
         luat_heap_free(ptr);
         LLOGE("tcpip_callback 返回错误!!! ret %d", ret);
@@ -223,3 +233,11 @@ int luat_netdrv_netif_input_proxy(struct netif * netif, uint8_t* buff, uint16_t
     }
     return 0;
 }
+
+
+
+void luat_netdrv_print_tm(const char * tag) {
+    uint64_t tnow = luat_mcu_tick64();
+    uint64_t t_us = tnow / luat_mcu_us_period();
+    LLOGI("tag %s time %lld", tag, t_us);
+}

+ 123 - 14
components/network/netdrv/src/luat_netdrv_napt.c

@@ -18,6 +18,7 @@
 #include "lwip/udp.h"
 #include "lwip/prot/tcp.h"
 #include "lwip/prot/udp.h"
+#include "lwip/ip_addr.h"
 
 #define LUAT_LOG_TAG "netdrv.napt"
 #include "luat_log.h"
@@ -136,7 +137,7 @@ __USER_FUNC_IN_RAM__ int luat_netdrv_napt_pkg_input(int id, uint8_t* buff, size_
         return 0;
     }
     // LLOGD("按协议类型, 使用对应的NAPT修改器进行处理 id %d proto %d", id, IPH_PROTO(ctx.iphdr));
-    uint64_t tbegin = luat_mcu_tick64();
+    // uint64_t tbegin = luat_mcu_tick64();
     int ret = 0;
     switch (IPH_PROTO(ctx.iphdr))
     {
@@ -153,9 +154,11 @@ __USER_FUNC_IN_RAM__ int luat_netdrv_napt_pkg_input(int id, uint8_t* buff, size_
         LLOGD("不是tcp/udp/icmp包, 不需要执行napt");
         return 0;
     }
-    uint64_t tend = luat_mcu_tick64();
-    uint64_t tused_us = (tend - tbegin) / luat_mcu_us_period();
-    LLOGI("time used %lld us tp %d", tused_us, IPH_PROTO(ctx.iphdr));
+    // uint64_t tend = luat_mcu_tick64();
+    // uint64_t tused_us = (tend - tbegin) / luat_mcu_us_period();
+    // if (tused_us > 100) {
+    //     LLOGI("time used %4lld us tp %2d way %d", tused_us, IPH_PROTO(ctx.iphdr), luat_netdrv_gw_adapter_id == id);
+    // }
     return ret;
 }
 #endif
@@ -193,15 +196,17 @@ int luat_netdrv_napt_pkg_input_pbuf(int id, struct pbuf* p) {
 
 static luat_rtos_mutex_t tcp_mutex;
 static luat_netdrv_napt_llist_t node_head;
+static size_t clean_tm = 1;
 
 // 端口分配
 #define NAPT_TCP_RANGE_START     0x1BBC
 #define NAPT_TCP_RANGE_END       0x5AAA
-// static uint32_t port_used;
-static u16 luat_napt_tcp_port_alloc(void) {
+static uint32_t port_used[1024];
+__USER_FUNC_IN_RAM__ static size_t luat_napt_tcp_port_alloc(void) {
+    #if 0
     luat_netdrv_napt_llist_t* head = node_head.next;
     size_t used = 0;
-    for (size_t i = NAPT_TCP_RANGE_END; i <= NAPT_TCP_RANGE_START; i++)
+    for (size_t i = NAPT_TCP_RANGE_START; i <= NAPT_TCP_RANGE_END; i++)
     {
         head = node_head.next;
         used = 0;
@@ -213,19 +218,99 @@ static u16 luat_napt_tcp_port_alloc(void) {
             head = head->next;
         }
         if (used == 0) {
+            LLOGD("分配新的TCP本地端口 %d", i);
             return i;
         }
     }
+    #else
+    size_t offset;
+    size_t soffset;
+    for (size_t i = 0; i <= NAPT_TCP_RANGE_END - NAPT_TCP_RANGE_START; i++) {
+        offset = i / ( 4 * 8);
+        soffset = i % ( 4 * 8);
+        if ((port_used[offset] & (1 << soffset)) == 0) {
+            port_used[offset] |= (1 << soffset);
+            return i + NAPT_TCP_RANGE_START;
+        }
+    }
+    #endif
     return 0;
 }
 
+__USER_FUNC_IN_RAM__ static void mapping_cleanup(void) {
+    luat_netdrv_napt_llist_t* head = node_head.next;
+    luat_netdrv_napt_llist_t* prev = &node_head;
+    luat_netdrv_napt_llist_t* tmp = NULL;
+    uint64_t tnow = luat_mcu_tick64_ms();
+    uint64_t tdiff = 0;
+    size_t offset;
+    size_t soffset;
+    size_t port;
+    int flag = 0;
+    luat_netdrv_napt_tcpudp_t* it = NULL;
+    while (head != NULL) {
+        it = &head->item;
+        // 远程ip(4 byte), 远程端口(2 byte), 本地映射端口(2 byte)
+        tdiff = tnow - it->tm_ms;
+        if (tdiff > 20*60*1000) {
+            flag = 1;
+            LLOGD("映射关系超时 %lldms port %ld", tdiff, it->wnet_local_port);
+        }
+        else if ((((it->finack1 && it->finack2) || !it->synack) &&
+                  tnow - it->tm_ms > IP_NAPT_TIMEOUT_MS_TCP_DISCON)) {
+            LLOGD("映射的TCP链路已经断开%lldms, 超过 %ld ms, 设置为无效", tnow - it->tm_ms, IP_NAPT_TIMEOUT_MS_TCP_DISCON);
+            flag = 1;
+        }
+        if (flag) {
+            tmp = head;
+            port = it->wnet_local_port - NAPT_TCP_RANGE_START;
+            LLOGD("释放端口号 %d", it->wnet_local_port);
+            offset = port / ( 4 * 8);
+            soffset = port % ( 4 * 8);
+            port_used[offset] &= (~(1 << soffset));
+            prev->next = head->next;
+            head = head->next;
+            // 注意, prev不变
+            luat_heap_opt_free(LUAT_HEAP_PSRAM, tmp);
+        }
+        else {
+            prev = head;
+            head = head->next;
+        }
+    }
+}
+
+
+__USER_FUNC_IN_RAM__ static void update_tcp_stat_wnet(struct tcp_hdr *tcphdr, luat_netdrv_napt_tcpudp_t* t) {
+    if ((TCPH_FLAGS(tcphdr) & (TCP_SYN|TCP_ACK)) == (TCP_SYN|TCP_ACK))
+      t->synack = 1;
+    if ((TCPH_FLAGS(tcphdr) & TCP_FIN))
+      t->fin1 = 1;
+    if (t->fin2 && (TCPH_FLAGS(tcphdr) & TCP_ACK))
+      t->finack2 = 1; /* FIXME: Currently ignoring ACK seq... */
+    if (TCPH_FLAGS(tcphdr) & TCP_RST)
+      t->rst = 1;
+  // LLOGD("TCP链路状态 synack %d fin1 %d finack2 %d rst %d", t->synack, t->fin1, t->finack2, t->rst);
+}
+
+__USER_FUNC_IN_RAM__ static void update_tcp_stat_inet(struct tcp_hdr *tcphdr, luat_netdrv_napt_tcpudp_t* t) {
+    if ((TCPH_FLAGS(tcphdr) & TCP_FIN))
+        t->fin2 = 1;
+    if (t->fin1 && (TCPH_FLAGS(tcphdr) & TCP_ACK))
+        t->finack1 = 1; /* FIXME: Currently ignoring ACK seq... */
+    if (TCPH_FLAGS(tcphdr) & TCP_RST)
+        t->rst = 1;
+    // LLOGD("TCP链路状态 synack %d fin1 %d finack2 %d rst %d", t->synack, t->fin1, t->finack2, t->rst);
+}
+
 // 外网到内网
-int luat_netdrv_napt_tcp_wan2lan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* mapping) {
+__USER_FUNC_IN_RAM__ int luat_netdrv_napt_tcp_wan2lan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* mapping) {
     int ret = -1;
     luat_netdrv_napt_llist_t* head = node_head.next;
     uint16_t iphdr_len = (ctx->iphdr->_v_hl & 0x0F) * 4;
     struct ip_hdr* ip_hdr = ctx->iphdr;
     uint64_t tnow = luat_mcu_tick64_ms();
+    uint32_t tsec = (uint32_t)(tnow / 1000);
     luat_netdrv_napt_tcpudp_t tmp = {0};
     struct tcp_hdr *tcp_hdr = (struct tcp_hdr*)(((uint8_t*)ctx->iphdr) + iphdr_len);
 
@@ -238,11 +323,18 @@ int luat_netdrv_napt_tcp_wan2lan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* map
         luat_rtos_mutex_create(&tcp_mutex);
     }
     luat_rtos_mutex_lock(tcp_mutex, 5000);
+    // 清理映射关系
+    if (tsec - clean_tm > 5) {
+        mapping_cleanup();
+        clean_tm = tsec;
+    }
     while (head != NULL) {
+        // 远程ip(4 byte), 远程端口(2 byte), 本地映射端口(2 byte)
         if (memcmp(&tmp.wnet_ip, &head->item.wnet_ip, 8) == 0) {
             memcpy(mapping, &head->item, sizeof(luat_netdrv_napt_tcpudp_t));
             head->item.tm_ms = tnow;
             ret = 0;
+            update_tcp_stat_wnet(tcp_hdr, &head->item);
             break;
         }
         head = head->next;
@@ -253,12 +345,13 @@ int luat_netdrv_napt_tcp_wan2lan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* map
 }
 
 // 内网到外网
-int luat_netdrv_napt_tcp_lan2wan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* mapping) {
+__USER_FUNC_IN_RAM__ int luat_netdrv_napt_tcp_lan2wan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* mapping) {
     int ret = -1;
     luat_netdrv_napt_llist_t* head = node_head.next;
     uint16_t iphdr_len = (ctx->iphdr->_v_hl & 0x0F) * 4;
     struct ip_hdr* ip_hdr = ctx->iphdr;
     uint64_t tnow = luat_mcu_tick64_ms();
+    uint32_t tsec = (uint32_t)(tnow / 1000);
     luat_netdrv_napt_tcpudp_t tmp = {0};
     struct tcp_hdr *tcp_hdr = (struct tcp_hdr*)(((uint8_t*)ctx->iphdr) + iphdr_len);
 
@@ -270,13 +363,23 @@ int luat_netdrv_napt_tcp_lan2wan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* map
     if (tcp_mutex == NULL) {
         luat_rtos_mutex_create(&tcp_mutex);
     }
+    
     luat_rtos_mutex_lock(tcp_mutex, 5000);
 
+    // 清理映射关系
+    if (tsec - clean_tm > 5) {
+        mapping_cleanup();
+        clean_tm = tsec;
+    }
+
     while (head != NULL) {
-        if (memcmp(&tmp.inet_ip, &head->item.inet_ip, 8 + 8) == 0) {
-            memcpy(mapping, &head->item, sizeof(luat_netdrv_napt_tcpudp_t));
+        // 本地ip(4 byte), 本地端口(2 byte), 远程ip(4 byte), 远程端口(2 byte)
+        if (memcmp(&tmp.inet_ip, &head->item.inet_ip, 6 + 6) == 0) {
             head->item.tm_ms = tnow;
             ret = 0;
+            // 映射关系找到了,那就关联一下情况
+            update_tcp_stat_inet(tcp_hdr, &head->item);
+            memcpy(mapping, &head->item, sizeof(luat_netdrv_napt_tcpudp_t));
             break;
         }
         head = head->next;
@@ -296,11 +399,17 @@ int luat_netdrv_napt_tcp_lan2wan(napt_ctx_t* ctx, luat_netdrv_napt_tcpudp_t* map
             break;
         }
         else {
-            node->item.wnet_local_port = tcp_hdr->dest;
-            memcpy(&node->item, mapping, sizeof(luat_netdrv_napt_tcpudp_t));
-            node->item.tm_ms = luat_mcu_tick64_ms();
+            memcpy(&node->item, &tmp, sizeof(luat_netdrv_napt_tcpudp_t));
+            node->item.tm_ms = tnow;
+            if (ctx->eth) {
+                memcpy(node->item.inet_mac, ctx->eth->src.addr, 6);
+            }
+            
+            node->item.adapter_id = ctx->net->id;
             node->next = node_head.next;
             node_head.next = node;
+            memcpy(mapping, &node->item, sizeof(luat_netdrv_napt_tcpudp_t));
+            LLOGD("分配出去的端口号 %d", node->item.wnet_local_port);
             ret = 0;
         }
         break;

+ 15 - 189
components/network/netdrv/src/luat_netdrv_napt_tcp.c

@@ -14,88 +14,16 @@
 #define LUAT_LOG_TAG "netdrv.napt.tcp"
 #include "luat_log.h"
 
-#define TCP_MAP_SIZE (2048)
-#define TCP_MAP_TIMEOUT (15*60*1000)
-
-/* napt tcp port range: 7100-23210 */
-#define NAPT_TCP_RANGE_START     0x1BBC
-#define NAPT_TCP_RANGE_END       0x5AAA
-
 extern int luat_netdrv_gw_adapter_id;
-static uint16_t napt_curr_id = NAPT_TCP_RANGE_START;
 static luat_netdrv_napt_tcpudp_t* tcps;
+static uint8_t *tcp_buff;
 
 #define u32 uint32_t
 #define u16 uint16_t
 #define u8 uint8_t
 #define NAPT_ETH_HDR_LEN             sizeof(struct ethhdr)
 
-static u16 luat_napt_tcp_port_alloc(void)
-{
-    u16 cnt = 0;
-
-again:
-    if (napt_curr_id++ == NAPT_TCP_RANGE_END)
-    {
-        napt_curr_id = NAPT_TCP_RANGE_START;
-    }
-
-    for (size_t i = 0; i < TCP_MAP_SIZE; i++)
-    {
-        if (napt_curr_id == tcps[i].wnet_local_port)
-        {
-            if (++cnt > (NAPT_TCP_RANGE_END - NAPT_TCP_RANGE_START))
-            {
-                return 0;
-            }
-
-	       goto again;
-        }
-    }
-
-    return napt_curr_id;
-}
 
-static void print_item(const char* tag, luat_netdrv_napt_tcpudp_t* it) {
-    char buff[16] = {0};
-    char buff2[16] = {0};
-    ip_addr_t ip;
-    
-    ip_addr_set_ip4_u32(&ip, it->inet_ip);
-    ipaddr_ntoa_r(&ip, buff, 16);
-
-    ip_addr_set_ip4_u32(&ip, it->wnet_ip);
-    ipaddr_ntoa_r(&ip, buff2, 16);
-
-    LLOGD("%s (%5d) 内网 %s:%d <-> 外网 %s:%d", tag, it->wnet_local_port,
-        buff, ntohs(it->inet_port),
-        buff2, ntohs(it->wnet_port)
-    );
-}
-
-static void update_tcp_stat_wnet(struct tcp_hdr *tcphdr, luat_netdrv_napt_tcpudp_t* t) {
-      if ((TCPH_FLAGS(tcphdr) & (TCP_SYN|TCP_ACK)) == (TCP_SYN|TCP_ACK))
-        t->synack = 1;
-      if ((TCPH_FLAGS(tcphdr) & TCP_FIN))
-        t->fin1 = 1;
-      if (t->fin2 && (TCPH_FLAGS(tcphdr) & TCP_ACK))
-        t->finack2 = 1; /* FIXME: Currently ignoring ACK seq... */
-      if (TCPH_FLAGS(tcphdr) & TCP_RST)
-        t->rst = 1;
-    // LLOGD("TCP链路状态 synack %d fin1 %d finack2 %d rst %d", t->synack, t->fin1, t->finack2, t->rst);
-}
-
-static void update_tcp_stat_inet(struct tcp_hdr *tcphdr, luat_netdrv_napt_tcpudp_t* t) {
-    if ((TCPH_FLAGS(tcphdr) & TCP_FIN))
-        t->fin2 = 1;
-    if (t->fin1 && (TCPH_FLAGS(tcphdr) & TCP_ACK))
-        t->finack1 = 1; /* FIXME: Currently ignoring ACK seq... */
-    if (TCPH_FLAGS(tcphdr) & TCP_RST)
-        t->rst = 1;
-    // LLOGD("TCP链路状态 synack %d fin1 %d finack2 %d rst %d", t->synack, t->fin1, t->finack2, t->rst);
-}
-
-static uint8_t *tcp_buff;
 int luat_napt_tcp_handle(napt_ctx_t* ctx) {
     uint16_t iphdr_len = (ctx->iphdr->_v_hl & 0x0F) * 4;
     struct ip_hdr* ip_hdr = ctx->iphdr;
@@ -103,50 +31,25 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
     luat_netdrv_t* gw = luat_netdrv_get(luat_netdrv_gw_adapter_id);
     luat_netdrv_napt_tcpudp_t* it = NULL;
     luat_netdrv_napt_tcpudp_t* it_map = NULL;
+    int ret = 0;
     if (gw == NULL || gw->netif == NULL) {
         return 0;
     }
-    if (tcps == NULL) {
-        tcps = luat_heap_opt_zalloc(LUAT_HEAP_PSRAM, sizeof(luat_netdrv_napt_tcpudp_t) * TCP_MAP_SIZE);
-    }
     if (tcp_buff == NULL) {
         tcp_buff = luat_heap_opt_zalloc(LUAT_HEAP_SRAM, 1600);
     }
     uint64_t tnow = luat_mcu_tick64_ms();
+    luat_netdrv_napt_tcpudp_t mapping = {0};
     if (ctx->is_wnet) {
         // 这是从外网到内网的TCP包
         // LLOGD("wnet.search dst port %d", ntohs(tcp_hdr->dest));
-        for (size_t i = 0; i < TCP_MAP_SIZE; i++)
-        {
-            it = &tcps[i];
-            if (it->is_vaild == 0) {
-                continue;
-            }
-            tnow = luat_mcu_tick64_ms();
-            if (it->is_vaild && tnow > it->tm_ms &&  (tnow - it->tm_ms) > TCP_MAP_TIMEOUT) {
-                // LLOGD("映射关系超时了!!设置为无效 %lld %lld %lld", tnow, it->tm_ms, tnow - it->tm_ms);
-                it->is_vaild = 0;
-                continue;
-            }
-            // print_item("wnet.search item", it);
-            // 校验远程IP与预期IP是否相同
-            if (ip_hdr->src.addr != it->wnet_ip) {
-                // LLOGD("IP地址不匹配,下一条");
-                continue;
-            }
-            // 下行的目标端口, 与本地端口, 是否一直
-            if (tcp_hdr->dest != tcps[i].wnet_local_port) {
-                // LLOGD("port不匹配,下一条");
-                continue;
-            }
-            // 找到映射关系了!!!
-            // LLOGD("TCP port %u -> %d", ntohs(tcp_hdr->dest), ntohs(tcps[i].inet_port));
-            tcps[i].tm_ms = tnow;
+        ret = luat_netdrv_napt_tcp_wan2lan(ctx, &mapping);
+        if (ret == 0) {
             // 修改目标端口
-            tcp_hdr->dest = tcps[i].inet_port;
+            tcp_hdr->dest = mapping.inet_port;
 
             // 修改目标地址到内网地址,并重新计算ip的checksu
-            ip_hdr->dest.addr = tcps[i].inet_ip;
+            ip_hdr->dest.addr = mapping.inet_ip;
             ip_hdr->_chksum = 0;
             ip_hdr->_chksum = alg_iphdr_chksum((u16 *)ip_hdr, iphdr_len);
 
@@ -163,10 +66,9 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
             // 如果是ETH包, 那还需要修改源MAC和目标MAC
             if (ctx->eth) {
                 memcpy(ctx->eth->src.addr, ctx->net->netif->hwaddr, 6);
-                memcpy(ctx->eth->dest.addr, tcps[i].inet_mac, 6);
+                memcpy(ctx->eth->dest.addr, mapping.inet_mac, 6);
             }
-            update_tcp_stat_wnet(tcp_hdr, &tcps[i]);
-            luat_netdrv_t* dst = luat_netdrv_get(tcps[i].adapter_id);
+            luat_netdrv_t* dst = luat_netdrv_get(mapping.adapter_id);
             if (dst == NULL) {
                 LLOGE("能找到TCP映射关系, 但目标netdrv不存在, 这肯定是BUG啊!!");
                 return 1;
@@ -178,7 +80,7 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
                 }
                 else if (!ctx->eth && dst->netif->flags & NETIF_FLAG_ETHARP) {
                     // 需要补全一个ETH头部
-                    memcpy(tcp_buff, tcps[i].inet_mac, 6);
+                    memcpy(tcp_buff, mapping.inet_mac, 6);
                     memcpy(tcp_buff + 6, dst->netif->hwaddr, 6);
                     memcpy(tcp_buff + 12, "\x08\x00", 2);
                     memcpy(tcp_buff + 14, ip_hdr, ctx->len);
@@ -206,88 +108,12 @@ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
         }
         // 第一轮循环, 是否有已知映射
         // LLOGD("inet.search src port %d -> %d", ntohs(tcp_hdr->src), ntohs(tcp_hdr->dest));
-        for (size_t i = 0; i < TCP_MAP_SIZE; i++)
-        {
-            it = &tcps[i];
-            if (it->is_vaild == 0) {
-                continue;
-            }
-            tnow = luat_mcu_tick64_ms();
-            if (tnow > it->tm_ms && (tnow - it->tm_ms) > TCP_MAP_TIMEOUT) {
-                // LLOGD("映射关系超时了!!设置为无效 %lld %lld %lld", tnow, it->tm_ms, tnow - it->tm_ms);
-                it->is_vaild = 0;
-                it->tm_ms = 0;
-                continue;
-            }
-
-            // 判断TCP链路状态
-            if ((((it->finack1 && it->finack2) || !it->synack) &&
-                  tnow - it->tm_ms > IP_NAPT_TIMEOUT_MS_TCP_DISCON)) {
-                //LLOGD("映射的TCP链路已经断开%lldms, 超过 %ld ms, 设置为无效", tnow - it->tm_ms, IP_NAPT_TIMEOUT_MS_TCP_DISCON);
-                it->is_vaild = 0;
-                it->tm_ms = 0;
-                continue;
-            }
-            
-            // print_item("inet.search", it);
-            // 几个要素都要相同 源IP/源端口/目标IP/目标端口, 如果是MAC包, 源MAC也要相同
-            if (it->inet_ip != ip_hdr->src.addr || it->inet_port != tcp_hdr->src) {
-                // LLOGD("源ip/port不匹配, 继续下一条");
-                continue;
-            }
-            if (it->wnet_ip != ip_hdr->dest.addr || it->wnet_port != tcp_hdr->dest) {
-                // LLOGD("目标ip/port不匹配, 继续下一条");
-                continue;
-            }
-            if (ctx->eth && memcmp(ctx->eth->src.addr, it->inet_mac, 6)) {
-                // LLOGD("源MAC不匹配, 继续下一条");
-                continue;
-            }
-            // 都相同, 那就是同一个映射了, 可以复用
-            it->tm_ms = tnow;
-            it_map = it;
-            break;
-        }
-        // 寻找一个空位
-        if (it_map == NULL) {
-            if ((TCPH_FLAGS(tcp_hdr) & (TCP_SYN|TCP_ACK)) == TCP_SYN && PP_NTOHS(tcp_hdr->src) >= 1024) {
-                // 允许新增映射
-            }
-            else {
-                // LLOGI("非SYN包/源端口小于1024,且没有已知映射,不允许新增映射 %02X %d", TCPH_FLAGS(tcp_hdr), PP_NTOHS(tcp_hdr->src));
-                // TODO 应该返回RST?
-                return 0;
-            }
-            for (size_t i = 0; i < TCP_MAP_SIZE; i++) {
-                it = &tcps[i];
-                if (it->is_vaild) {
-                    continue;
-                }
-                // 有空位了, 马上分配
-                it_map = it;
-                break;
-            }
-            if (it_map == NULL) {
-                LLOGE("没有空闲的TCP映射了!!!!");
-                return 0;
-            }
-            memset(it_map, 0, sizeof(luat_netdrv_napt_tcpudp_t));
-            it->adapter_id = ctx->net->id;
-            it->inet_port = tcp_hdr->src;
-            it->wnet_port = tcp_hdr->dest;
-            it->inet_ip = ip_hdr->src.addr;
-            it->wnet_ip = ip_hdr->dest.addr;
-            it->wnet_local_port = luat_napt_tcp_port_alloc();
-            it->tm_ms = tnow;
-            if (ctx->eth) {
-                memcpy(it->inet_mac, ctx->eth->src.addr, 6);
-            }
-            it->is_vaild = 1;
-            //LLOGD("分配新的TCP映射 inet %d wnet %d", it->inet_port, it->wnet_local_port);
-        }
-        else {
-            update_tcp_stat_inet(tcp_hdr, it_map);
+        ret = luat_netdrv_napt_tcp_lan2wan(ctx, &mapping);
+        if (ret != 0) {
+            return 0;
         }
+        it_map = &mapping;
+
         // 2. 修改信息
         ip_hdr->src.addr = ip_addr_get_ip4_u32(&gw->netif->ip_addr);
         tcp_hdr->src = it_map->wnet_local_port;

+ 1 - 1
components/network/netdrv/src/luat_netdrv_napt_udp.c

@@ -13,7 +13,7 @@
 #include "luat_log.h"
 
 #ifdef LUAT_USE_PSRAM
-#define UDP_MAP_SIZE (8*1024)
+#define UDP_MAP_SIZE (256)
 #else
 #define UDP_MAP_SIZE (1024)
 #endif