Procházet zdrojové kódy

update: netdrv,napt,优化返回值管理,抽取魔数为宏定义

Wendal Chen před 3 měsíci
rodič
revize
8cd5b18c4b

+ 8 - 0
components/network/netdrv/include/luat_netdrv_napt.h

@@ -3,6 +3,14 @@
 
 #include "lwip/pbuf.h"
 
+// 返回值定义
+#define NAPT_RET_OK           0    // 转发成功
+#define NAPT_RET_SKIP         1    // 跳过处理,让LWIP继续
+#define NAPT_RET_NO_MAPPING  -1    // 未找到映射关系
+#define NAPT_RET_NO_MEMORY   -2    // 内存不足
+#define NAPT_RET_LOCK_FAIL   -3    // 加锁失败
+#define NAPT_RET_INVALID_CTX -4    // NAPT上下文无效
+
 // #define IP_NAPT_TIMEOUT_MS_TCP (30*60*1000)
 #define IP_NAPT_TIMEOUT_MS_TCP_DISCON (20*1000)
 #ifndef NAPT_TCP_MAP_ITEM_MAX

+ 13 - 6
components/network/netdrv/src/luat_netdrv_napt.c

@@ -23,6 +23,12 @@
 #define LUAT_LOG_TAG "netdrv.napt"
 #include "luat_log.h"
 
+// 超时时间定义
+#define NAPT_TCP_TIMEOUT_MS        (20*60*1000)   // TCP连接超时: 20分钟
+#define NAPT_TCP_DISCON_TIMEOUT_MS (20*1000)      // TCP断开连接超时: 20秒
+#define NAPT_UDP_TIMEOUT_MS        (2*60*1000)    // UDP超时: 2分钟
+#define NAPT_CLEANUP_INTERVAL_SEC  (5)            // 清理周期: 5秒
+
 #define ICMP_MAP_SIZE (32)
 #define UDP_MAP_TIMEOUT (60 * 1000)
 #define NAPT_MAX_PACKET_SIZE (1520)
@@ -194,7 +200,8 @@ err_t netdrv_ip_input_cb(int id, struct pbuf *p, struct netif *inp) {
     pbuf_copy_partial(p, napt_buff, len, 0);
     int ret = luat_netdrv_napt_pkg_input(id, napt_buff, len);
     // LLOGD("napt_pkg_input ret %d", ret);
-    return ret == 0 ? 1 : 0;
+    // 返回: NAPT_RET_OK(0)表示已转发,其他值表示LWIP继续处理
+    return ret == NAPT_RET_OK ? NAPT_RET_OK : NAPT_RET_SKIP;
     // return 1;
 }
 
@@ -339,16 +346,16 @@ __NETDRV_CODE_IN_RAM__ static void mapping_cleanup(luat_netdrv_napt_ctx_t *napt_
         it = &napt_ctx->items[i];
         tdiff = tnow - it->tm_ms;
         if (napt_ctx->ip_tp == IP_PROTO_TCP) {
-            if (tdiff > 20*60*1000) { // TCP是20分钟
+            if (tdiff > NAPT_TCP_TIMEOUT_MS) { // TCP是20分钟
                 flag = 1;
             }
-            else if ((((it->finack1 && it->finack2) || !it->synack) && tdiff > IP_NAPT_TIMEOUT_MS_TCP_DISCON)) {
+            else if ((((it->finack1 && it->finack2) || !it->synack) && tdiff > NAPT_TCP_DISCON_TIMEOUT_MS)) {
                 // print_item("TCP链接已关闭,移除", it);
                 flag = 1;
             }
         }
         else if (napt_ctx->ip_tp == IP_PROTO_UDP) {
-            if (tdiff > 2*60*1000) { // UDP 是2分钟
+            if (tdiff > NAPT_UDP_TIMEOUT_MS) { // UDP 是2分钟
                 flag = 1;
             }
         }
@@ -435,7 +442,7 @@ __NETDRV_CODE_IN_RAM__ int luat_netdrv_napt_tcp_wan2lan(napt_ctx_t* ctx, luat_ne
 
     luat_rtos_mutex_lock(napt_ctx->lock, 5000);
     // 清理映射关系
-    if (tsec - napt_ctx->clean_tm > 5) {
+    if (tsec - napt_ctx->clean_tm > NAPT_CLEANUP_INTERVAL_SEC) {
         // LLOGD("执行映射关系清理 %ld %ld", tsec, napt_ctx->clean_tm);
         mapping_cleanup(napt_ctx);
         napt_ctx->clean_tm = tsec;
@@ -492,7 +499,7 @@ __NETDRV_CODE_IN_RAM__ int luat_netdrv_napt_tcp_lan2wan(napt_ctx_t* ctx, luat_ne
     ret = -1;
 
     // 清理映射关系
-    if (tsec - napt_ctx->clean_tm > 5) {
+    if (tsec - napt_ctx->clean_tm > NAPT_CLEANUP_INTERVAL_SEC) {
         mapping_cleanup(napt_ctx);
         napt_ctx->clean_tm = tsec;
     }

+ 16 - 9
components/network/netdrv/src/luat_netdrv_napt_tcp.c

@@ -37,8 +37,15 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
     // luat_netdrv_napt_tcpudp_t* it = NULL;
     luat_netdrv_napt_tcpudp_t* it_map = NULL;
     int ret = 0;
+    
+    // P1检查: 验证NAPT上下文初始化
+    if (g_napt_tcp_ctx == NULL) {
+        LLOGD("TCP NAPT context not initialized");
+        return NAPT_RET_SKIP;
+    }
+    
     if (gw == NULL || gw->netif == NULL) {
-        return 0;
+        return NAPT_RET_SKIP;
     }
     if (tcp_buff == NULL) {
         tcp_buff = luat_heap_opt_zalloc(LUAT_HEAP_AUTO, 1600);
@@ -49,7 +56,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
         // 这是从外网到内网的TCP包
         // LLOGD("wnet.search dst port %d", ntohs(tcp_hdr->dest));
         ret = luat_netdrv_napt_tcp_wan2lan(ctx, &mapping, g_napt_tcp_ctx);
-        if (ret == 0) {
+        if (ret == NAPT_RET_OK) {
             // 修改目标端口
             tcp_hdr->dest = mapping.inet_port;
 
@@ -76,7 +83,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
             luat_netdrv_t* dst = luat_netdrv_get(mapping.adapter_id);
             if (dst == NULL) {
                 LLOGE("能找到TCP映射关系, 但目标netdrv不存在, 这肯定是BUG啊!!");
-                return 1;
+                return NAPT_RET_OK;
             }
             if (dst->dataout) {
                 if (ctx->eth && dst->netif->flags & NETIF_FLAG_ETHARP) {
@@ -101,10 +108,10 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
             else {
                 LLOGE("能找到TCP映射关系, 但目标netdrv不支持dataout!!");
             }
-            return 1; // 全部修改完成
+            return NAPT_RET_OK; // 全部修改完成
         }
         // LLOGD("没有找到TCP映射关系, 放行给LWIP处理");
-        return 0;
+        return NAPT_RET_SKIP;
     }
     else {
         // 内网, 尝试对外网的请求吗?
@@ -114,8 +121,8 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
         // 第一轮循环, 是否有已知映射
         // LLOGD("inet.search src port %d -> %d", ntohs(tcp_hdr->src), ntohs(tcp_hdr->dest));
         ret = luat_netdrv_napt_tcp_lan2wan(ctx, &mapping, g_napt_tcp_ctx);
-        if (ret != 0) {
-            return 0;
+        if (ret != NAPT_RET_OK) {
+            return NAPT_RET_SKIP;
         }
         it_map = &mapping;
 
@@ -145,7 +152,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
                 }
                 else {
                     LLOGD("网关netdrv是ETH,源网卡不是ETH, 当前不支持");
-                    return 0;
+                    return NAPT_RET_SKIP;
                 }
             }
             else {
@@ -160,7 +167,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_tcp_handle(napt_ctx_t* ctx) {
         else {
             LLOGD("TCP改写完成, 但GW不支持dataout回调?!!");
         }
-        return 1;
+        return NAPT_RET_OK;
     }
     return 0;
 }

+ 17 - 10
components/network/netdrv/src/luat_netdrv_napt_udp.c

@@ -34,6 +34,13 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_udp_handle(napt_ctx_t *ctx)
     struct udp_hdr *udp_hdr = (struct udp_hdr *)(((uint8_t *)ctx->iphdr) + iphdr_len);
     luat_netdrv_t *gw = ctx->drv_gw;
     int ret = 0;
+    
+    // P1检查: 验证NAPT上下文初始化
+    if (g_napt_udp_ctx == NULL) {
+        LLOGD("UDP NAPT context not initialized");
+        return NAPT_RET_SKIP;
+    }
+    
     if (udp_buff == NULL)
     {
         udp_buff = luat_heap_opt_zalloc(LUAT_HEAP_AUTO, 1600);
@@ -44,7 +51,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_udp_handle(napt_ctx_t *ctx)
     {
         // 这是从外网到内网的UDP包
         ret = luat_netdrv_napt_tcp_wan2lan(ctx, &mapping, g_napt_udp_ctx);
-        if (ret == 0)
+        if (ret == NAPT_RET_OK)
         {
             // 找到映射关系了!!! 修改目标ID
             udp_hdr->dest = mapping.inet_port;
@@ -75,7 +82,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_udp_handle(napt_ctx_t *ctx)
             if (dst == NULL)
             {
                 LLOGE("能找到UDP映射关系, 但目标netdrv不存在, 这肯定是BUG啊!!");
-                return 1;
+                return NAPT_RET_OK;
             }
             if (dst->dataout)
             {
@@ -105,21 +112,21 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_udp_handle(napt_ctx_t *ctx)
             {
                 LLOGE("能找到UDP映射关系, 但目标netdrv不支持dataout!!");
             }
-            return 1; // 全部修改完成
+            return NAPT_RET_OK; // 全部修改完成
         }
         // LLOGD("没有找到UDP映射关系, 放行给LWIP处理");
-        return 0;
+        return NAPT_RET_SKIP;
     }
     else
     {
         // 内网, 尝试对外网的请求吗?
         if (ip_hdr->dest.addr == ip_addr_get_ip4_u32(&ctx->net->netif->ip_addr))
         {
-            return 0; // 对网关的UDP请求, 交给LWIP处理
+            return NAPT_RET_SKIP; // 对网关的UDP请求, 交给LWIP处理
         }
         ret = luat_netdrv_napt_tcp_lan2wan(ctx, &mapping, g_napt_udp_ctx);
-        if (ret != 0) {
-            return 0;
+        if (ret != NAPT_RET_OK) {
+            return NAPT_RET_SKIP;
         }
         // 2. 修改信息
         ip_hdr->src.addr = ip_addr_get_ip4_u32(&gw->netif->ip_addr);
@@ -153,7 +160,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_udp_handle(napt_ctx_t *ctx)
                 else
                 {
                     LLOGD("网关netdrv是ETH,源网卡不是ETH, 当前不支持");
-                    return 0;
+                    return NAPT_RET_SKIP;
                 }
             }
             else
@@ -172,7 +179,7 @@ __NETDRV_CODE_IN_RAM__ int luat_napt_udp_handle(napt_ctx_t *ctx)
         {
             LLOGD("UDP改写完成, 但GW不支持dataout回调?!!");
         }
-        return 1;
+        return NAPT_RET_OK;
     }
-    return 0;
+    return NAPT_RET_SKIP;
 }