1/** 2 * @file 3 * Ping sender module 4 * 5 */ 6 7/* 8 * Redistribution and use in source and binary forms, with or without modification, 9 * are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 22 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 24 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 28 * OF SUCH DAMAGE. 29 * 30 * This file is part of the lwIP TCP/IP stack. 31 * 32 */ 33 34/** 35 * This is an example of a "ping" sender (with raw API and socket API). 36 * It can be used as a start point to maintain opened a network connection, or 37 * like a network "watchdog" for your device. 38 * 39 */ 40 41#include "lwip/opt.h" 42 43#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */ 44 45#include "ping.h" 46 47#include "lwip/mem.h" 48#include "lwip/raw.h" 49#include "lwip/icmp.h" 50#include "lwip/netif.h" 51#include "lwip/sys.h" 52#include "lwip/timeouts.h" 53#include "lwip/inet_chksum.h" 54#include "lwip/prot/ip4.h" 55 56#if PING_USE_SOCKETS 57#include "lwip/sockets.h" 58#include "lwip/inet.h" 59#include <string.h> 60#endif /* PING_USE_SOCKETS */ 61 62 63/** 64 * PING_DEBUG: Enable debugging for PING. 65 */ 66#ifndef PING_DEBUG 67#define PING_DEBUG LWIP_DBG_ON 68#endif 69 70/** ping receive timeout - in milliseconds */ 71#ifndef PING_RCV_TIMEO 72#define PING_RCV_TIMEO 1000 73#endif 74 75/** ping delay - in milliseconds */ 76#ifndef PING_DELAY 77#define PING_DELAY 1000 78#endif 79 80/** ping identifier - must fit on a u16_t */ 81#ifndef PING_ID 82#define PING_ID 0xAFAF 83#endif 84 85/** ping additional data size to include in the packet */ 86#ifndef PING_DATA_SIZE 87#define PING_DATA_SIZE 32 88#endif 89 90/** ping result action - no default action */ 91#ifndef PING_RESULT 92#define PING_RESULT(ping_ok) 93#endif 94 95/* ping variables */ 96static const ip_addr_t* ping_target; 97static u16_t ping_seq_num; 98static u32_t ping_time; 99#if !PING_USE_SOCKETS 100static struct raw_pcb *ping_pcb; 101#endif /* PING_USE_SOCKETS */ 102 103/** Prepare a echo ICMP request */ 104static void 105ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len) 106{ 107 size_t i; 108 size_t data_len = len - sizeof(struct icmp_echo_hdr); 109 110 ICMPH_TYPE_SET(iecho, ICMP_ECHO); 111 ICMPH_CODE_SET(iecho, 0); 112 iecho->chksum = 0; 113 iecho->id = PING_ID; 114 iecho->seqno = lwip_htons(++ping_seq_num); 115 116 /* fill the additional data buffer with some data */ 117 for(i = 0; i < data_len; i++) { 118 ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; 119 } 120 121 iecho->chksum = inet_chksum(iecho, len); 122} 123 124#if PING_USE_SOCKETS 125 126/* Ping using the socket ip */ 127static err_t 128ping_send(int s, const ip_addr_t *addr) 129{ 130 int err; 131 struct icmp_echo_hdr *iecho; 132 struct sockaddr_storage to; 133 size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; 134 LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); 135 136#if LWIP_IPV6 137 if(IP_IS_V6(addr) && !ip6_addr_isipv6mappedipv4(ip_2_ip6(addr))) { 138 /* todo: support ICMP6 echo */ 139 return ERR_VAL; 140 } 141#endif /* LWIP_IPV6 */ 142 143 iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size); 144 if (!iecho) { 145 return ERR_MEM; 146 } 147 148 ping_prepare_echo(iecho, (u16_t)ping_size); 149 150#if LWIP_IPV4 151 if(IP_IS_V4(addr)) { 152 struct sockaddr_in *to4 = (struct sockaddr_in*)&to; 153 to4->sin_len = sizeof(to4); 154 to4->sin_family = AF_INET; 155 inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr)); 156 } 157#endif /* LWIP_IPV4 */ 158 159#if LWIP_IPV6 160 if(IP_IS_V6(addr)) { 161 struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to; 162 to6->sin6_len = sizeof(to6); 163 to6->sin6_family = AF_INET6; 164 inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr)); 165 } 166#endif /* LWIP_IPV6 */ 167 168 err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); 169 170 mem_free(iecho); 171 172 return (err ? ERR_OK : ERR_VAL); 173} 174 175static void 176ping_recv(int s) 177{ 178 char buf[64]; 179 int len; 180 struct sockaddr_storage from; 181 int fromlen = sizeof(from); 182 183 while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) { 184 if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) { 185 ip_addr_t fromaddr; 186 memset(&fromaddr, 0, sizeof(fromaddr)); 187 188#if LWIP_IPV4 189 if(from.ss_family == AF_INET) { 190 struct sockaddr_in *from4 = (struct sockaddr_in*)&from; 191 inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr); 192 IP_SET_TYPE(&fromaddr, IPADDR_TYPE_V4); 193 } 194#endif /* LWIP_IPV4 */ 195 196#if LWIP_IPV6 197 if(from.ss_family == AF_INET6) { 198 struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from; 199 inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr); 200 IP_SET_TYPE(&fromaddr, IPADDR_TYPE_V6); 201 } 202#endif /* LWIP_IPV6 */ 203 204 LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); 205 ip_addr_debug_print(PING_DEBUG, &fromaddr); 206 LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time))); 207 208 /* todo: support ICMP6 echo */ 209#if LWIP_IPV4 210 if (IP_IS_V4_VAL(fromaddr)) { 211 struct ip_hdr *iphdr; 212 struct icmp_echo_hdr *iecho; 213 214 iphdr = (struct ip_hdr *)buf; 215 iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4)); 216 if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { 217 /* do some ping result processing */ 218 PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER)); 219 return; 220 } else { 221 LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n")); 222 } 223 } 224#endif /* LWIP_IPV4 */ 225 } 226 fromlen = sizeof(from); 227 } 228 229 if (len == 0) { 230 LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time))); 231 } 232 233 /* do some ping result processing */ 234 PING_RESULT(0); 235} 236 237static void 238ping_thread(void *arg) 239{ 240 int s; 241 int ret; 242 243#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD 244 int timeout = PING_RCV_TIMEO; 245#else 246 struct timeval timeout; 247 timeout.tv_sec = PING_RCV_TIMEO/1000; 248 timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000; 249#endif 250 LWIP_UNUSED_ARG(arg); 251 252#if LWIP_IPV6 253 if(IP_IS_V4(ping_target) || ip6_addr_isipv6mappedipv4(ip_2_ip6(ping_target))) { 254 s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP); 255 } else { 256 s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6); 257 } 258#else 259 s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP); 260#endif 261 if (s < 0) { 262 return; 263 } 264 265 ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); 266 LWIP_ASSERT("setting receive timeout failed", ret == 0); 267 LWIP_UNUSED_ARG(ret); 268 269 while (1) { 270 if (ping_send(s, ping_target) == ERR_OK) { 271 LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); 272 ip_addr_debug_print(PING_DEBUG, ping_target); 273 LWIP_DEBUGF( PING_DEBUG, ("\n")); 274 275 ping_time = sys_now(); 276 ping_recv(s); 277 } else { 278 LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); 279 ip_addr_debug_print(PING_DEBUG, ping_target); 280 LWIP_DEBUGF( PING_DEBUG, (" - error\n")); 281 } 282 sys_msleep(PING_DELAY); 283 } 284} 285 286#else /* PING_USE_SOCKETS */ 287 288/* Ping using the raw ip */ 289static u8_t 290ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr) 291{ 292 struct icmp_echo_hdr *iecho; 293 LWIP_UNUSED_ARG(arg); 294 LWIP_UNUSED_ARG(pcb); 295 LWIP_UNUSED_ARG(addr); 296 LWIP_ASSERT("p != NULL", p != NULL); 297 298 if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) && 299 pbuf_header(p, -PBUF_IP_HLEN) == 0) { 300 iecho = (struct icmp_echo_hdr *)p->payload; 301 302 if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) { 303 LWIP_DEBUGF( PING_DEBUG, ("ping: recv ")); 304 ip_addr_debug_print(PING_DEBUG, addr); 305 LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time))); 306 307 /* do some ping result processing */ 308 PING_RESULT(1); 309 pbuf_free(p); 310 return 1; /* eat the packet */ 311 } 312 /* not eaten, restore original packet */ 313 pbuf_header(p, PBUF_IP_HLEN); 314 } 315 316 return 0; /* don't eat the packet */ 317} 318 319static void 320ping_send(struct raw_pcb *raw, ip_addr_t *addr) 321{ 322 struct pbuf *p; 323 struct icmp_echo_hdr *iecho; 324 size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE; 325 326 LWIP_DEBUGF( PING_DEBUG, ("ping: send ")); 327 ip_addr_debug_print(PING_DEBUG, addr); 328 LWIP_DEBUGF( PING_DEBUG, ("\n")); 329 LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff); 330 331 p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM); 332 if (!p) { 333 return; 334 } 335 if ((p->len == p->tot_len) && (p->next == NULL)) { 336 iecho = (struct icmp_echo_hdr *)p->payload; 337 338 ping_prepare_echo(iecho, (u16_t)ping_size); 339 340 raw_sendto(raw, p, addr); 341 ping_time = sys_now(); 342 } 343 pbuf_free(p); 344} 345 346static void 347ping_timeout(void *arg) 348{ 349 struct raw_pcb *pcb = (struct raw_pcb*)arg; 350 ip_addr_t ping_target; 351 352 LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL); 353 354 ip_addr_copy_from_ip4(ping_target, PING_TARGET); 355 ping_send(pcb, &ping_target); 356 357 sys_timeout(PING_DELAY, ping_timeout, pcb); 358} 359 360static void 361ping_raw_init(void) 362{ 363 ping_pcb = raw_new(IP_PROTO_ICMP); 364 LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); 365 366 raw_recv(ping_pcb, ping_recv, NULL); 367 raw_bind(ping_pcb, IP_ADDR_ANY); 368 sys_timeout(PING_DELAY, ping_timeout, ping_pcb); 369} 370 371void 372ping_send_now(void) 373{ 374 ip_addr_t ping_target; 375 LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL); 376 ip_addr_copy_from_ip4(ping_target, PING_TARGET); 377 ping_send(ping_pcb, &ping_target); 378} 379 380#endif /* PING_USE_SOCKETS */ 381 382void 383ping_init(const ip_addr_t* ping_addr) 384{ 385 ping_target = ping_addr; 386 387#if PING_USE_SOCKETS 388 sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); 389#else /* PING_USE_SOCKETS */ 390 ping_raw_init(); 391#endif /* PING_USE_SOCKETS */ 392} 393 394#endif /* LWIP_RAW */ 395