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,
#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);
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;
}
/* 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);
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) {