From 89bb062cf7c5323115299692d540d1cd3365972e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=AD=99=E6=98=A5=E9=98=B3?= Date: Sat, 23 Aug 2025 17:57:17 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E5=A4=8D=E5=A4=96=E7=BD=91=E4=B8=8D?= =?utf8?q?=E8=83=BD=E8=AE=BF=E9=97=AE=E9=97=AE=E9=A2=98=EF=BC=8Cdefault=20?= =?utf8?q?netif=E6=9C=AA=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../lwip_adapter/liteos/src/test/slip_test.c | 4 + .../lwip/lwip_v2.1.3/src/core/init.c | 2 +- .../zhihui/4g/cat1Service/Service/api_shell.c | 98 ++++++++++++++++--- 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/test/slip_test.c b/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/test/slip_test.c index 05bcbe078..eec78f58c 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/test/slip_test.c +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_adapter/liteos/src/test/slip_test.c @@ -53,6 +53,10 @@ void slip_test_init(void) netif_set_up(&slip_netif); netif_set_link_up(&slip_netif); + /* 设置SLIP接口为默认网络接口 - 这是关键!*/ + netif_set_default(&slip_netif); + printf("SLIP netif set as default interface\n"); + printf("SLIP netif initialized successfully\n"); printf("IP: %s\n", SLIP_TEST_IP_ADDR); printf("Netmask: %s\n", SLIP_TEST_NETMASK); diff --git a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c index 9bd7cd5a8..6327e8430 100644 --- a/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c +++ b/code/sdk/open_source/openharmony/third_party/lwip/lwip_v2.1.3/src/core/init.c @@ -353,7 +353,7 @@ lwip_init(void) mem_init(); memp_init(); pbuf_init(); - // netif_init(); + netif_init(); #if LWIP_IPV4 ip_init(); #if LWIP_ARP diff --git a/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c b/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c index b53e877ad..f230f4429 100644 --- a/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c +++ b/code/sdk/zhihui/4g/cat1Service/Service/api_shell.c @@ -405,30 +405,89 @@ LWIP_STATIC int OsPingFunc(u32_t *parg) u32_t data_len = parg[3]; char str[128] = {0}; iecho_len = sizeof(struct icmp_echo_hdr) + data_len; + PRINTK("OsPingFunc: Creating RAW socket for ICMP, destip=0x%X, cnt=%u, data_len=%u\n", + destip, cnt, data_len); + + /* Check network interface status */ + struct netif *netif = netif_default; + if (netif == NULL) { + PRINTK("OsPingFunc: No default network interface available\n"); + get_log("No default network interface",strlen("No default network interface")); + return -1; + } + if (!netif_is_up(netif)) { + PRINTK("OsPingFunc: Default network interface is down\n"); + get_log("Network interface is down",strlen("Network interface is down")); + return -1; + } + if (!netif_is_link_up(netif)) { + PRINTK("OsPingFunc: Network link is down\n"); + get_log("Network link is down",strlen("Network link is down")); + return -1; + } + PRINTK("OsPingFunc: Network interface OK, IP=%s\n", ipaddr_ntoa(&netif->ip_addr)); + + /* Check routing for external destinations */ + ip4_addr_t dest_addr; + dest_addr.addr = destip; + + /* Check if destination is local (172.16.1.x) */ + ip4_addr_t local_net, local_mask; + local_net.addr = ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask); + local_mask.addr = ip4_addr_get_u32(&netif->netmask); + + ip4_addr_t dest_net; + dest_net.addr = dest_addr.addr & local_mask.addr; + + if (local_net.addr == dest_net.addr) { + PRINTK("OsPingFunc: Destination is on LOCAL network (172.16.1.x)\n"); + } else { + PRINTK("OsPingFunc: Destination is EXTERNAL, requires gateway routing\n"); + PRINTK("OsPingFunc: Gateway: %s\n", ipaddr_ntoa(&netif->gw)); + + /* Check if gateway is configured */ + if (ip4_addr_get_u32(&netif->gw) == IPADDR_ANY) { + PRINTK("OsPingFunc: ❌ No gateway configured for external routing!\n"); + get_log("No gateway configured", strlen("No gateway configured")); + return -1; + } + } + sfd = lwip_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP); if (sfd < 0) { - PRINTK("Ping socket\n"); - get_log("Ping socket",strlen("Ping socket\n")); + PRINTK("Ping socket creation failed, errno=%d\n", errno); + get_log("Ping socket creation failed",strlen("Ping socket creation failed")); return -1; } + PRINTK("OsPingFunc: Socket created successfully, fd=%d\n", sfd); + PRINTK("OsPingFunc: Allocating pbuf, size=%u\n", IP_HLEN + sizeof(struct icmp_echo_hdr)); pbuf_resp = pbuf_alloc(PBUF_RAW, IP_HLEN + sizeof(struct icmp_echo_hdr), PBUF_RAM); if (pbuf_resp == NULL) { - PRINTK("Ping: pbuf_resp malloc failed\n"); - get_log("Ping: pbuf_resp malloc failed\n",strlen("Ping: pbuf_resp malloc failed\n")); + PRINTK("Ping: pbuf_resp malloc failed, size=%u\n", IP_HLEN + sizeof(struct icmp_echo_hdr)); + get_log("Ping: pbuf_resp malloc failed",strlen("Ping: pbuf_resp malloc failed")); ret = -1; goto FAILURE; } + PRINTK("OsPingFunc: pbuf allocated successfully\n"); + + PRINTK("OsPingFunc: Allocating ICMP echo request, size=%u\n", iecho_len); iecho = (struct icmp_echo_hdr *)mem_malloc(iecho_len); if (iecho == NULL) { - PRINTK("Ping: echo request malloc failed\n"); - get_log("Ping: echo request malloc failed\n",strlen("Ping: echo request malloc failed\n")); + PRINTK("Ping: echo request malloc failed, size=%u\n", iecho_len); + get_log("Ping: echo request malloc failed",strlen("Ping: echo request malloc failed")); ret = -1; goto FAILURE; } + PRINTK("OsPingFunc: ICMP echo request allocated successfully\n"); to.sin_family = AF_INET; to.sin_addr.s_addr = destip; /* already in network order */ to.sin_port = 0; + + /* Print destination info for debugging */ + char dest_ip_str[16]; + inet_ntop(AF_INET, &to.sin_addr, dest_ip_str, sizeof(dest_ip_str)); + PRINTK("OsPingFunc: Target IP=%s (0x%X), socket family=%d\n", dest_ip_str, destip, to.sin_family); if (data_len > PING_ZERO_DATA_LEN) { (void)memset_s(iecho, sizeof(struct icmp_echo_hdr) + PING_ZERO_DATA_LEN, @@ -450,12 +509,15 @@ LWIP_STATIC int OsPingFunc(u32_t *parg) #if (CHECKSUM_GEN_ICMP > 0) iecho->chksum = inet_chksum((void *)iecho, iecho_len); #endif + PRINTK("OsPingFunc: Sending ICMP echo request [%u], len=%u to 0x%X\n", i, iecho_len, destip); ret = sendto(sfd, iecho, iecho_len, 0, (struct sockaddr *)&to, (socklen_t)sizeof(to)); if (ret < 0) { - perror("Ping: sending ICMP echo request failed\n"); - get_log("Ping: sending ICMP echo request failed\n",strlen("Ping: sending ICMP echo request failed\n")); + PRINTK("Ping: sending ICMP echo request failed, ret=%d, errno=%d\n", ret, errno); + perror("Ping: sendto failed"); + get_log("Ping: sending ICMP echo request failed",strlen("Ping: sending ICMP echo request failed")); goto FAILURE; } + PRINTK("OsPingFunc: ICMP echo request sent successfully, bytes=%d\n", ret); /* capture the start time to calculate RTT */ (void)clock_gettime(CLOCK_MONOTONIC_RAW, &start); @@ -469,20 +531,25 @@ LWIP_STATIC int OsPingFunc(u32_t *parg) timeout_flag = FALSE; ret = lwip_poll(&pfd, 1, LWIP_SHELL_CMD_PING_TIMEOUT); if (ret < 0) { - perror("Ping: poll\n"); + PRINTK("Ping: poll failed, ret=%d, errno=%d\n", ret, errno); + perror("Ping: poll failed"); goto FAILURE; } else if (ret == 0) { /* first type timeout event */ + PRINTK("Ping: poll timeout after %dms\n", LWIP_SHELL_CMD_PING_TIMEOUT); timeout_flag = TRUE; break; } + PRINTK("OsPingFunc: poll returned %d, ready to receive\n", ret); ret = recv(sfd, pbuf_resp->payload, pbuf_resp->len, MSG_DONTWAIT); if (ret < 0) { - perror("Ping: recv echo reply failed\n"); - get_log("Ping: recv echo reply failed\n",strlen("Ping: recv echo reply failed\n")); + PRINTK("Ping: recv echo reply failed, ret=%d, errno=%d\n", ret, errno); + perror("Ping: recv failed"); + get_log("Ping: recv echo reply failed",strlen("Ping: recv echo reply failed")); goto FAILURE; } + PRINTK("OsPingFunc: received %d bytes\n", ret); /* Accessing ip header and icmp header */ iphdr_resp = pbuf_resp->payload; @@ -660,7 +727,12 @@ u32_t OsShellPing(int argc, const char *argv) } /* Try direct IP address parsing first */ - if (inet_aton(argv, (struct in_addr*)&dst_ipaddr.addr) == 0) { + struct in_addr temp_addr; + if (inet_aton(argv, &temp_addr) != 0) { + /* Direct IP parsing succeeded */ + dst_ipaddr.addr = temp_addr.s_addr; /* inet_aton returns network byte order */ + printf("%s %d dst_ip 0x%X (direct IP)\n",__func__,__LINE__,dst_ipaddr.addr); + } else { /* Direct IP parsing failed, try DNS resolution */ #if LWIP_DNS dst_ipaddr.addr = get_hostip(argv); @@ -669,8 +741,6 @@ u32_t OsShellPing(int argc, const char *argv) PRINTK("DNS not supported and invalid IP address format: %s\n", argv); goto ping_error; #endif /* LWIP_DNS */ - } else { - printf("%s %d dst_ip 0x%X (direct IP)\n",__func__,__LINE__,dst_ipaddr.addr); } if (dst_ipaddr.addr == IPADDR_NONE || dst_ipaddr.addr == IPADDR_ANY) { -- 2.17.1