Просмотр исходного кода

fix: icmp,应该放到lwip线程里调用

Wendal Chen 8 месяцев назад
Родитель
Сommit
8f4d1c2ea4

+ 7 - 4
components/network/icmp/binding/luat_lib_icmp.c

@@ -22,6 +22,7 @@ sys.waitUnitl("PING_RESULT", 3000)
 #include "luat_msgbus.h"
 
 #include "lwip/ip_addr.h"
+#include "lwip/tcpip.h"
 
 #include "rotable2.h"
 
@@ -120,13 +121,15 @@ static int l_icmp_ping(lua_State *L) {
         }
     }
     const char* ip = luaL_checkstring(L, 2);
-    size_t len = luaL_optinteger(L, 3, 128);
-    ip_addr_t addr = {0};
-    if (0 == ipaddr_aton(ip, &addr)) {
+    ctx->len = luaL_optinteger(L, 3, 128);
+    if (0 == ipaddr_aton(ip, &ctx->tmpdst)) {
         LLOGW("目标地址非法 %s", ip);
         return 0;
     };
-    int result = luat_icmp_ping(ctx, &addr, len);
+    int result = tcpip_callback_with_block(luat_icmp_ping, ctx, 1);
+    if (result) {
+        LLOGW("luat_icmp_ping/tcpip_callback_with_block result %d", result);
+    }
     lua_pushinteger(L, result == 0);
     return 1;
 }

+ 3 - 1
components/network/icmp/include/luat_icmp.h

@@ -18,6 +18,8 @@ typedef struct luat_icmp_ctx
     struct netif *netif;
     struct raw_pcb *pcb;
     ip_addr_t dst;
+    ip_addr_t tmpdst;
+    size_t len;
     uint16_t id;
     uint16_t seqno;
     uint64_t send_time;
@@ -28,6 +30,6 @@ typedef struct luat_icmp_ctx
 
 luat_icmp_ctx_t* luat_icmp_init(uint8_t adapter_id);
 luat_icmp_ctx_t* luat_icmp_get(uint8_t adapter_id);
-int luat_icmp_ping(luat_icmp_ctx_t* ctx, ip_addr_t* dst, size_t size);
+void luat_icmp_ping(luat_icmp_ctx_t* ctx);
 
 #endif

+ 6 - 13
components/network/icmp/src/luat_icmp.c

@@ -120,21 +120,16 @@ luat_icmp_ctx_t* luat_icmp_init(uint8_t adapter_id) {
     return ctx;
 }
 
-int luat_icmp_ping(luat_icmp_ctx_t* ctx, ip_addr_t* dst, size_t size) {
+void luat_icmp_ping(luat_icmp_ctx_t* ctx) {
     int ret = 0;
-
     struct icmp_echo_hdr *iecho;
-
-    if (ctx == NULL || dst == NULL) {
-        return -1;
-    }
-    memcpy(&ctx->dst, dst, sizeof(ip_addr_t));
+    memcpy(&ctx->dst, &ctx->tmpdst, sizeof(ip_addr_t));
     ctx->send_time = 0;
     ctx->recv_time = 0;
-    int ping_size = sizeof(struct icmp_echo_hdr) + size;
+    int ping_size = sizeof(struct icmp_echo_hdr) + ctx->len;
     struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, ping_size, PBUF_RAM);
     if (p == NULL) {
-        return -2;
+        return;
     }
     raw_bind(ctx->pcb, &ctx->netif->ip_addr);
     iecho = (struct icmp_echo_hdr *)(p->payload);
@@ -146,14 +141,12 @@ int luat_icmp_ping(luat_icmp_ctx_t* ctx, ip_addr_t* dst, size_t size) {
     char buff[32] = {0};
     char buff2[32] = {0};
     ipaddr_ntoa_r(&ctx->netif->ip_addr, buff, 32);
-    ipaddr_ntoa_r(dst, buff2, 32);
+    ipaddr_ntoa_r(&ctx->dst, buff2, 32);
     // LLOGD("ICMP sendto %s --> %s", buff, buff2);
     ctx->send_time = luat_mcu_tick64_ms();
-    ret = raw_sendto(ctx->pcb, p, dst);
+    ret = raw_sendto(ctx->pcb, p, &ctx->dst);
     pbuf_free(p);
     if (ret) {
         LLOGW("ICMP sendto error %d %s --> %s", ret, buff, buff2);
-        return ret;
     }
-    return 0;
 }