|
|
|
|
@ -65,11 +65,6 @@
|
|
|
|
|
#define PING_DEBUG LWIP_DBG_ON |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/** ping target - should be an "ip4_addr_t" */ |
|
|
|
|
#ifndef PING_TARGET |
|
|
|
|
#define PING_TARGET (netif_default ? *netif_ip4_gw(netif_default) : (*IP4_ADDR_ANY4)) |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/** ping receive timeout - in milliseconds */ |
|
|
|
|
#ifndef PING_RCV_TIMEO |
|
|
|
|
#define PING_RCV_TIMEO 1000 |
|
|
|
|
@ -96,6 +91,7 @@
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
/* ping variables */ |
|
|
|
|
static const ip_addr_t* ping_target; |
|
|
|
|
static u16_t ping_seq_num; |
|
|
|
|
static u32_t ping_time; |
|
|
|
|
#if !PING_USE_SOCKETS |
|
|
|
|
@ -127,14 +123,18 @@ ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
|
|
|
|
|
|
|
|
|
|
/* Ping using the socket ip */ |
|
|
|
|
static err_t |
|
|
|
|
ping_send(int s, ip_addr_t *addr) |
|
|
|
|
ping_send(int s, const ip_addr_t *addr) |
|
|
|
|
{ |
|
|
|
|
int err; |
|
|
|
|
struct icmp_echo_hdr *iecho; |
|
|
|
|
struct sockaddr_in to; |
|
|
|
|
struct sockaddr_storage to; |
|
|
|
|
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; |
|
|
|
|
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); |
|
|
|
|
LWIP_ASSERT("ping: expect IPv4 address", !IP_IS_V6(addr)); |
|
|
|
|
|
|
|
|
|
if(IP_IS_V6(addr) && !ip6_addr_isipv6mappedipv4(ip_2_ip6(addr))) { |
|
|
|
|
/* todo: support ICMP6 echo */ |
|
|
|
|
return ERR_VAL; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size); |
|
|
|
|
if (!iecho) { |
|
|
|
|
@ -142,10 +142,24 @@ ping_send(int s, ip_addr_t *addr)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ping_prepare_echo(iecho, (u16_t)ping_size); |
|
|
|
|
|
|
|
|
|
to.sin_len = sizeof(to); |
|
|
|
|
to.sin_family = AF_INET; |
|
|
|
|
inet_addr_from_ip4addr(&to.sin_addr, ip_2_ip4(addr)); |
|
|
|
|
|
|
|
|
|
#if LWIP_IPV4 |
|
|
|
|
if(IP_IS_V4(addr)) { |
|
|
|
|
struct sockaddr_in *to4 = (struct sockaddr_in*)&to; |
|
|
|
|
to4->sin_len = sizeof(to4); |
|
|
|
|
to4->sin_family = AF_INET; |
|
|
|
|
inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr)); |
|
|
|
|
} |
|
|
|
|
#endif /* LWIP_IPV4 */ |
|
|
|
|
|
|
|
|
|
#if LWIP_IPV6 |
|
|
|
|
if(IP_IS_V6(addr)) { |
|
|
|
|
struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to; |
|
|
|
|
to6->sin6_len = sizeof(to6); |
|
|
|
|
to6->sin6_family = AF_INET6; |
|
|
|
|
inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr)); |
|
|
|
|
} |
|
|
|
|
#endif /* LWIP_IPV6 */ |
|
|
|
|
|
|
|
|
|
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); |
|
|
|
|
|
|
|
|
|
@ -159,23 +173,37 @@ ping_recv(int s)
|
|
|
|
|
{ |
|
|
|
|
char buf[64]; |
|
|
|
|
int len; |
|
|
|
|
struct sockaddr_in from; |
|
|
|
|
struct sockaddr_storage from; |
|
|
|
|
struct ip_hdr *iphdr; |
|
|
|
|
struct icmp_echo_hdr *iecho; |
|
|
|
|
int fromlen = sizeof(from); |
|
|
|
|
|
|
|
|
|
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) { |
|
|
|
|
if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) { |
|
|
|
|
if (from.sin_family != AF_INET) { |
|
|
|
|
/* Ping is not IPv4 */
|
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n")); |
|
|
|
|
} else { |
|
|
|
|
ip4_addr_t fromaddr; |
|
|
|
|
inet_addr_to_ip4addr(&fromaddr, &from.sin_addr); |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); |
|
|
|
|
ip4_addr_debug_print(PING_DEBUG, &fromaddr); |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time))); |
|
|
|
|
ip_addr_t fromaddr; |
|
|
|
|
|
|
|
|
|
#if LWIP_IPV4 |
|
|
|
|
if(from.ss_family == AF_INET) { |
|
|
|
|
struct sockaddr_in *from4 = (struct sockaddr_in*)&from; |
|
|
|
|
inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr); |
|
|
|
|
IP_SET_TYPE(&fromaddr, IPADDR_TYPE_V4); |
|
|
|
|
} |
|
|
|
|
#endif /* LWIP_IPV4 */ |
|
|
|
|
|
|
|
|
|
#if LWIP_IPV6 |
|
|
|
|
if(from.ss_family == AF_INET6) { |
|
|
|
|
struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from; |
|
|
|
|
inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr); |
|
|
|
|
IP_SET_TYPE(&fromaddr, IPADDR_TYPE_V6); |
|
|
|
|
} |
|
|
|
|
#endif /* LWIP_IPV6 */ |
|
|
|
|
|
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); |
|
|
|
|
ip_addr_debug_print(PING_DEBUG, &fromaddr); |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time))); |
|
|
|
|
|
|
|
|
|
/* todo: support ICMP6 echo */ |
|
|
|
|
if (IP_IS_V4_VAL(fromaddr)) { |
|
|
|
|
iphdr = (struct ip_hdr *)buf; |
|
|
|
|
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); |
|
|
|
|
if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { |
|
|
|
|
@ -203,7 +231,7 @@ ping_thread(void *arg)
|
|
|
|
|
{ |
|
|
|
|
int s; |
|
|
|
|
int ret; |
|
|
|
|
ip_addr_t ping_target; |
|
|
|
|
|
|
|
|
|
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD |
|
|
|
|
int timeout = PING_RCV_TIMEO; |
|
|
|
|
#else |
|
|
|
|
@ -213,7 +241,16 @@ ping_thread(void *arg)
|
|
|
|
|
#endif |
|
|
|
|
LWIP_UNUSED_ARG(arg); |
|
|
|
|
|
|
|
|
|
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { |
|
|
|
|
#if LWIP_IPV6 |
|
|
|
|
if(IP_IS_V4(ping_target) || ip6_addr_isipv6mappedipv4(ip_2_ip6(ping_target))) { |
|
|
|
|
s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP); |
|
|
|
|
} else { |
|
|
|
|
s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6); |
|
|
|
|
} |
|
|
|
|
#else |
|
|
|
|
s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP); |
|
|
|
|
#endif |
|
|
|
|
if (s < 0) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -221,18 +258,16 @@ ping_thread(void *arg)
|
|
|
|
|
LWIP_ASSERT("setting receive timeout failed", ret == 0); |
|
|
|
|
|
|
|
|
|
while (1) { |
|
|
|
|
ip_addr_copy_from_ip4(ping_target, PING_TARGET); |
|
|
|
|
|
|
|
|
|
if (ping_send(s, &ping_target) == ERR_OK) { |
|
|
|
|
if (ping_send(s, ping_target) == ERR_OK) { |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); |
|
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target); |
|
|
|
|
ip_addr_debug_print(PING_DEBUG, ping_target); |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("\n")); |
|
|
|
|
|
|
|
|
|
ping_time = sys_now(); |
|
|
|
|
ping_recv(s); |
|
|
|
|
} else { |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); |
|
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target); |
|
|
|
|
ip_addr_debug_print(PING_DEBUG, ping_target); |
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" - error\n")); |
|
|
|
|
} |
|
|
|
|
sys_msleep(PING_DELAY); |
|
|
|
|
@ -336,8 +371,10 @@ ping_send_now(void)
|
|
|
|
|
#endif /* PING_USE_SOCKETS */ |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
ping_init(void) |
|
|
|
|
ping_init(const ip_addr_t* ping_addr) |
|
|
|
|
{ |
|
|
|
|
ping_target = ping_addr; |
|
|
|
|
|
|
|
|
|
#if PING_USE_SOCKETS |
|
|
|
|
sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); |
|
|
|
|
#else /* PING_USE_SOCKETS */ |
|
|
|
|
|