8 * Redistribution and use in source and binary forms, with or without modification,
9 * are permitted provided that the following conditions are met:
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.
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
30 * This file is part of the lwIP TCP/IP stack.
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.
43 #if LWIP_RAW && LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
47 #include "lwip/icmp.h"
48 #include "lwip/netif.h"
50 #include "lwip/sockets.h"
51 #include "lwip/inet.h"
52 #include "lwip/inet_chksum.h"
54 #include "apps/ping.h"
56 #include <geekos/timer.h>
57 #include <geekos/ktypes.h>
60 * PING_DEBUG: Enable debugging for PING.
63 #define PING_DEBUG LWIP_DBG_ON
66 /** ping target - should be a "struct ip_addr" */
68 #define PING_TARGET (netif_default?netif_default->gw:ip_addr_any)
71 /** ping receive timeout - in milliseconds */
72 #ifndef PING_RCV_TIMEO
73 #define PING_RCV_TIMEO 1000
76 /** ping delay - in milliseconds */
78 #define PING_DELAY 1000
81 /** ping identifier - must fit on a u16_t */
83 #define PING_ID 0xAFAF
86 /** ping additional data size to include in the packet */
87 #ifndef PING_DATA_SIZE
88 #define PING_DATA_SIZE 32
91 /** ping result action - no default action */
93 #define PING_RESULT(ping_ok)
97 static u16_t ping_seq_num;
98 static u32_t ping_time;
101 /* port-defined functions used for timer execution */
102 void sys_msleep(u32_t ms);
106 /** Prepare a echo ICMP request */
108 ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
112 ICMPH_TYPE_SET(iecho,ICMP_ECHO);
113 ICMPH_CODE_SET(iecho, 0);
116 iecho->seqno = htons(++ping_seq_num);
117 iecho->chksum = inet_chksum(iecho, len);
119 /* fill the additional data buffer with some data */
120 for(i = 0; i < PING_DATA_SIZE; i++) {
121 ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = i;
127 /* Ping using the socket ip */
129 ping_send(int s, struct ip_addr *addr)
132 struct icmp_echo_hdr *iecho;
133 struct sockaddr_in to;
134 size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
136 if (!(iecho = mem_malloc(ping_size))) {
140 ping_prepare_echo(iecho, ping_size);
142 to.sin_len = sizeof(to);
143 to.sin_family = AF_INET;
144 to.sin_addr.s_addr = addr->addr;
146 err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
150 return (err ? ERR_OK : ERR_VAL);
158 struct sockaddr_in from;
159 struct ip_hdr *iphdr;
160 struct icmp_echo_hdr *iecho;
162 while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
163 if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
164 LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
165 ip_addr_debug_print(PING_DEBUG, (struct ip_addr *)&(from.sin_addr));
166 LWIP_DEBUGF( PING_DEBUG, (" %lu ms\n", (sys_now()-ping_time)));
168 iphdr = (struct ip_hdr *)buf;
169 iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4));
170 if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
171 /* do some ping result processing */
172 PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
175 LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
181 LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %lu ms - timeout\n", (sys_now()-ping_time)));
184 /* do some ping result processing */
189 ping_thread(void *arg)
192 int timeout = PING_RCV_TIMEO;
193 struct ip_addr ping_target;
195 LWIP_UNUSED_ARG(arg);
197 if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
201 lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
204 ping_target = PING_TARGET;
206 if (ping_send(s, &ping_target) == ERR_OK) {
207 LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
208 ip_addr_debug_print(PING_DEBUG, &ping_target);
209 LWIP_DEBUGF( PING_DEBUG, ("\n"));
211 ping_time = sys_now();
214 LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
215 ip_addr_debug_print(PING_DEBUG, &ping_target);
216 LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
218 sys_msleep(PING_DELAY);
222 #else /* LWIP_SOCKET */
224 /* Ping using the raw ip */
226 ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *addr)
228 struct icmp_echo_hdr *iecho;
230 if (pbuf_header( p, -PBUF_IP_HLEN)==0) {
233 if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
234 LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
235 ip_addr_debug_print(PING_DEBUG, addr);
236 LWIP_DEBUGF( PING_DEBUG, (" %lu ms\n", (sys_now()-ping_time)));
238 /* do some ping result processing */
243 return 1; /* eat the event */
247 ping_send(struct raw_pcb *raw, struct ip_addr *addr)
250 struct icmp_echo_hdr *iecho;
251 size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
253 if (!(p = pbuf_alloc(PBUF_IP, ping_size, PBUF_RAM))) {
256 if ((p->len == p->tot_len) && (p->next == NULL)) {
259 ping_prepare_echo(iecho, ping_size);
261 raw_sendto(raw, p, addr);
262 ping_time = sys_now();
268 ping_timeout(void *arg)
270 struct raw_pcb *pcb = (struct raw_pcb*)arg;
271 struct ip_addr ping_target = PING_TARGET;
273 LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
275 LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
276 ip_addr_debug_print(PING_DEBUG, &ping_target);
277 LWIP_DEBUGF( PING_DEBUG, ("\n"));
279 ping_send(pcb, &ping_target);
281 sys_timeout(PING_DELAY, ping_timeout, pcb);
289 if (!(pcb = raw_new(IP_PROTO_ICMP))) {
293 raw_recv(pcb, ping_recv, NULL);
294 raw_bind(pcb, IP_ADDR_ANY);
295 sys_timeout(PING_DELAY, ping_timeout, pcb);
298 #endif /* LWIP_SOCKET */
304 sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
305 #else /* LWIP_SOCKET */
307 #endif /* LWIP_SOCKET */
321 #endif /* LWIP_RAW && LWIP_ICMP */