• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
96 static const ip_addr_t* ping_target;
97 static u16_t ping_seq_num;
98 #ifdef LWIP_DEBUG
99 static u32_t ping_time;
100 #endif /* LWIP_DEBUG */
101 #if !PING_USE_SOCKETS
102 static struct raw_pcb *ping_pcb;
103 #endif /* PING_USE_SOCKETS */
104 
105 /** Prepare a echo ICMP request */
106 static void
ping_prepare_echo(struct icmp_echo_hdr * iecho,u16_t len)107 ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
108 {
109   size_t i;
110   size_t data_len = len - sizeof(struct icmp_echo_hdr);
111 
112   ICMPH_TYPE_SET(iecho, ICMP_ECHO);
113   ICMPH_CODE_SET(iecho, 0);
114   iecho->chksum = 0;
115   iecho->id     = PING_ID;
116   iecho->seqno  = lwip_htons(++ping_seq_num);
117 
118   /* fill the additional data buffer with some data */
119   for(i = 0; i < data_len; i++) {
120     ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
121   }
122 
123   iecho->chksum = inet_chksum(iecho, len);
124 }
125 
126 #if PING_USE_SOCKETS
127 
128 /* Ping using the socket ip */
129 static err_t
ping_send(int s,const ip_addr_t * addr)130 ping_send(int s, const ip_addr_t *addr)
131 {
132   int err;
133   struct icmp_echo_hdr *iecho;
134   struct sockaddr_storage to;
135   size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
136   LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
137 
138 #if LWIP_IPV6
139   if(IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) {
140     /* todo: support ICMP6 echo */
141     return ERR_VAL;
142   }
143 #endif /* LWIP_IPV6 */
144 
145   iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
146   if (!iecho) {
147     return ERR_MEM;
148   }
149 
150   ping_prepare_echo(iecho, (u16_t)ping_size);
151 
152 #if LWIP_IPV4
153   if(IP_IS_V4(addr)) {
154     struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
155     to4->sin_len    = sizeof(*to4);
156     to4->sin_family = AF_INET;
157     inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
158   }
159 #endif /* LWIP_IPV4 */
160 
161 #if LWIP_IPV6
162   if(IP_IS_V6(addr)) {
163     struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
164     to6->sin6_len    = sizeof(*to6);
165     to6->sin6_family = AF_INET6;
166     inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
167   }
168 #endif /* LWIP_IPV6 */
169 
170   err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
171 
172   mem_free(iecho);
173 
174   return (err ? ERR_OK : ERR_VAL);
175 }
176 
177 static void
ping_recv(int s)178 ping_recv(int s)
179 {
180   char buf[64];
181   int len;
182   struct sockaddr_storage from;
183   int fromlen = sizeof(from);
184 
185   while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
186     if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
187       ip_addr_t fromaddr;
188       memset(&fromaddr, 0, sizeof(fromaddr));
189 
190 #if LWIP_IPV4
191       if(from.ss_family == AF_INET) {
192         struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
193         inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr);
194         IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4);
195       }
196 #endif /* LWIP_IPV4 */
197 
198 #if LWIP_IPV6
199       if(from.ss_family == AF_INET6) {
200         struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
201         inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr);
202         IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6);
203       }
204 #endif /* LWIP_IPV6 */
205 
206       LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
207       ip_addr_debug_print_val(PING_DEBUG, fromaddr);
208       LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
209 
210       /* todo: support ICMP6 echo */
211 #if LWIP_IPV4
212       if (IP_IS_V4_VAL(fromaddr)) {
213         struct ip_hdr *iphdr;
214         struct icmp_echo_hdr *iecho;
215 
216         iphdr = (struct ip_hdr *)buf;
217         iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
218         if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
219           /* do some ping result processing */
220           PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
221           return;
222         } else {
223           LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
224         }
225       }
226 #endif /* LWIP_IPV4 */
227     }
228     fromlen = sizeof(from);
229   }
230 
231   if (len == 0) {
232     LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
233   }
234 
235   /* do some ping result processing */
236   PING_RESULT(0);
237 }
238 
239 static void
ping_thread(void * arg)240 ping_thread(void *arg)
241 {
242   int s;
243   int ret;
244 
245 #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
246   int timeout = PING_RCV_TIMEO;
247 #else
248   struct timeval timeout;
249   timeout.tv_sec = PING_RCV_TIMEO/1000;
250   timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000;
251 #endif
252   LWIP_UNUSED_ARG(arg);
253 
254 #if LWIP_IPV6
255   if(IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) {
256     s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP);
257   } else {
258     s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
259   }
260 #else
261   s = lwip_socket(AF_INET,  SOCK_RAW, IP_PROTO_ICMP);
262 #endif
263   if (s < 0) {
264     return;
265   }
266 
267   ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
268   LWIP_ASSERT("setting receive timeout failed", ret == 0);
269   LWIP_UNUSED_ARG(ret);
270 
271   while (ping_target != NULL) {
272     if (ping_send(s, ping_target) == ERR_OK) {
273       LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
274       ip_addr_debug_print(PING_DEBUG, ping_target);
275       LWIP_DEBUGF( PING_DEBUG, ("\n"));
276 
277 #ifdef LWIP_DEBUG
278       ping_time = sys_now();
279 #endif /* LWIP_DEBUG */
280       ping_recv(s);
281     } else {
282       LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
283       ip_addr_debug_print(PING_DEBUG, ping_target);
284       LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
285     }
286     sys_msleep(PING_DELAY);
287   }
288   lwip_close(s);
289 }
290 
291 #else /* PING_USE_SOCKETS */
292 
293 /* Ping using the raw ip */
294 static u8_t
ping_recv(void * arg,struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * addr)295 ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
296 {
297   struct icmp_echo_hdr *iecho;
298   LWIP_UNUSED_ARG(arg);
299   LWIP_UNUSED_ARG(pcb);
300   LWIP_UNUSED_ARG(addr);
301   LWIP_ASSERT("addr != NULL", addr != NULL);
302   LWIP_ASSERT("p != NULL", p != NULL);
303 
304   if ((p->tot_len >= (IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
305       pbuf_remove_header(p, IP_HLEN) == 0) {
306     iecho = (struct icmp_echo_hdr *)p->payload;
307 
308     if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
309       LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
310       ip_addr_debug_print(PING_DEBUG, addr);
311       LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
312 
313       /* do some ping result processing */
314       PING_RESULT(1);
315       pbuf_free(p);
316       return 1; /* eat the packet */
317     }
318     /* not eaten, restore original packet */
319     pbuf_add_header(p, IP_HLEN);
320   }
321 
322   return 0; /* don't eat the packet */
323 }
324 
325 static void
ping_send(struct raw_pcb * raw,const ip_addr_t * addr)326 ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
327 {
328   struct pbuf *p;
329   struct icmp_echo_hdr *iecho;
330   size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
331 
332   LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
333   ip_addr_debug_print(PING_DEBUG, addr);
334   LWIP_DEBUGF( PING_DEBUG, ("\n"));
335   LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
336 
337   p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
338   if (!p) {
339     return;
340   }
341   if ((p->len == p->tot_len) && (p->next == NULL)) {
342     iecho = (struct icmp_echo_hdr *)p->payload;
343 
344     ping_prepare_echo(iecho, (u16_t)ping_size);
345 
346     raw_sendto(raw, p, addr);
347 #ifdef LWIP_DEBUG
348     ping_time = sys_now();
349 #endif /* LWIP_DEBUG */
350   }
351   pbuf_free(p);
352 }
353 
354 static void
ping_timeout(void * arg)355 ping_timeout(void *arg)
356 {
357   struct raw_pcb *pcb = (struct raw_pcb*)arg;
358 
359   LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
360 
361   ping_send(pcb, ping_target);
362 
363   sys_timeout(PING_DELAY, ping_timeout, pcb);
364 }
365 
366 static void
ping_raw_init(void)367 ping_raw_init(void)
368 {
369   ping_pcb = raw_new(IP_PROTO_ICMP);
370   LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
371 
372   raw_recv(ping_pcb, ping_recv, NULL);
373   raw_bind(ping_pcb, IP_ADDR_ANY);
374   sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
375 }
376 
377 void
ping_send_now(void)378 ping_send_now(void)
379 {
380   LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
381   LWIP_ASSERT("ping_target != NULL", ping_target != NULL);
382   ping_send(ping_pcb, ping_target);
383 }
384 
385 static void
ping_raw_stop(void)386 ping_raw_stop(void)
387 {
388   sys_untimeout(ping_timeout, ping_pcb);
389   if (ping_pcb != NULL) {
390     raw_remove(ping_pcb);
391     ping_pcb = NULL;
392   }
393 }
394 
395 #endif /* PING_USE_SOCKETS */
396 
397 /**
398  * Initialize thread (socket mode) or timer (callback mode) to cyclically send pings
399  * to a target.
400  * Running ping is implicitly stopped.
401  */
402 void
ping_init(const ip_addr_t * ping_addr)403 ping_init(const ip_addr_t* ping_addr)
404 {
405   ping_stop();
406 
407   LWIP_ASSERT("ping_addr != NULL", ping_addr != NULL);
408   ping_target = ping_addr;
409 
410 #if PING_USE_SOCKETS
411   sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
412 #else /* PING_USE_SOCKETS */
413   ping_raw_init();
414 #endif /* PING_USE_SOCKETS */
415 }
416 
417 /**
418  * Stop sending more pings.
419  */
ping_stop(void)420 void ping_stop(void)
421 {
422 #if !PING_USE_SOCKETS
423   ping_raw_stop();
424 #endif /* !PING_USE_SOCKETS */
425   ping_target = NULL;
426 }
427 
428 #endif /* LWIP_RAW */
429