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_IPV4 && LWIP_RAW /* don't build if not configured for use in lwipopts.h */
44 
45 #include "ping/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 
55 #if PING_USE_SOCKETS
56 #include "lwip/sockets.h"
57 #include "lwip/inet.h"
58 #include "ping/ping_sock.h"
59 #endif /* PING_USE_SOCKETS */
60 
61 #ifdef ESP_PING
62 #include "esp_ping.h"
63 #include "lwip/ip_addr.h"
64 #endif
65 /**
66  * PING_DEBUG: Enable debugging for PING.
67  */
68 #ifndef PING_DEBUG
69 #define PING_DEBUG     LWIP_DBG_OFF
70 #endif
71 
72 /** ping target - should be an "ip4_addr_t" */
73 #ifndef PING_TARGET
74 #define PING_TARGET   (netif_default ? *netif_ip4_gw(netif_default) : (*IP4_ADDR_ANY))
75 #endif
76 
77 /** ping receive timeout - in milliseconds */
78 #ifndef PING_RCV_TIMEO
79 #define PING_RCV_TIMEO 1000
80 #endif
81 
82 /** ping delay - in milliseconds */
83 #ifndef PING_DELAY
84 #define PING_DELAY     1000
85 #endif
86 
87 /** ping identifier - must fit on a u16_t */
88 #ifndef PING_ID
89 #define PING_ID        0xAFAF
90 #endif
91 
92 /** ping additional data size to include in the packet */
93 #ifndef PING_DATA_SIZE
94 #define PING_DATA_SIZE 32
95 #endif
96 
97 /** ping result action - no default action */
98 #ifndef PING_RESULT
99 #define PING_RESULT(ping_ok)
100 #endif
101 
102 #define PING_TIME_DIFF_MS(_end, _start)  ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + (_end.tv_usec - _start.tv_usec)/1000))
103 #define PING_TIME_DIFF_SEC(_end, _start) ((uint32_t)((_end).tv_sec - (_start).tv_sec))
104 
105 #if PING_USE_SOCKETS
106 /* ping handle */
107 static esp_ping_handle_t ping = NULL;
108 #else
109 static struct raw_pcb *ping_pcb;
110 static u16_t ping_seq_num;
111 static struct timeval ping_time;
112 
113 /** Prepare a echo ICMP request */
114 static void
ping_prepare_echo(struct icmp_echo_hdr * iecho,u16_t len)115 ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
116 {
117   size_t i;
118   size_t data_len = len - sizeof(struct icmp_echo_hdr);
119 
120   ICMPH_TYPE_SET(iecho, ICMP_ECHO);
121   ICMPH_CODE_SET(iecho, 0);
122   iecho->chksum = 0;
123   iecho->id     = PING_ID;
124   iecho->seqno  = htons(++ping_seq_num);
125 
126   /* fill the additional data buffer with some data */
127   for(i = 0; i < data_len; i++) {
128     ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
129   }
130 
131   iecho->chksum = inet_chksum(iecho, len);
132 }
133 
134 /* Ping using the raw ip */
135 static u8_t
ping_recv(void * arg,struct raw_pcb * pcb,struct pbuf * p,const ip_addr_t * addr)136 ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
137 {
138   struct icmp_echo_hdr *iecho;
139   struct timeval now;
140 
141   LWIP_UNUSED_ARG(arg);
142   LWIP_UNUSED_ARG(pcb);
143   LWIP_UNUSED_ARG(addr);
144   LWIP_ASSERT("p != NULL", p != NULL);
145 
146   if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
147       pbuf_header(p, -PBUF_IP_HLEN) == 0) {
148     iecho = (struct icmp_echo_hdr *)p->payload;
149 
150     if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
151       LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
152       ip_addr_debug_print(PING_DEBUG, addr);
153       gettimeofday(&now, NULL);
154       LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", PING_TIME_DIFF_MS(now, ping_time)));
155 
156       /* do some ping result processing */
157       PING_RESULT(1);
158       pbuf_free(p);
159       return 1; /* eat the packet */
160     }
161     /* not eaten, restore original packet */
162     pbuf_header(p, PBUF_IP_HLEN);
163   }
164 
165   return 0; /* don't eat the packet */
166 }
167 
168 static void
ping_send(struct raw_pcb * raw,ip_addr_t * addr)169 ping_send(struct raw_pcb *raw, ip_addr_t *addr)
170 {
171   struct pbuf *p;
172   struct icmp_echo_hdr *iecho;
173   size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
174 
175   LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
176   ip_addr_debug_print(PING_DEBUG, addr);
177   LWIP_DEBUGF( PING_DEBUG, ("\n"));
178   LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
179 
180   p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
181   if (!p) {
182     return;
183   }
184   if ((p->len == p->tot_len) && (p->next == NULL)) {
185     iecho = (struct icmp_echo_hdr *)p->payload;
186 
187     ping_prepare_echo(iecho, (u16_t)ping_size);
188 
189     raw_sendto(raw, p, addr);
190     ping_time = system_get_time();
191   }
192   pbuf_free(p);
193 }
194 
195 static void
ping_timeout(void * arg)196 ping_timeout(void *arg)
197 {
198   struct raw_pcb *pcb = (struct raw_pcb*)arg;
199   ip_addr_t ping_target;
200 
201   LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
202 
203   ip_addr_copy_from_ip4(ping_target, PING_TARGET);
204   ping_send(pcb, &ping_target);
205   sys_timeout(PING_DELAY, ping_timeout, pcb);
206 }
207 
208 static void
ping_raw_init(void)209 ping_raw_init(void)
210 {
211   ping_pcb = raw_new(IP_PROTO_ICMP);
212   LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
213 
214   raw_recv(ping_pcb, ping_recv, NULL);
215   raw_bind(ping_pcb, IP_ADDR_ANY);
216   sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
217 }
218 
219 void
ping_send_now(void)220 ping_send_now(void)
221 {
222   ip_addr_t ping_target;
223   LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
224   ip_addr_copy_from_ip4(ping_target, PING_TARGET);
225   ping_send(ping_pcb, &ping_target);
226 }
227 
228 #endif /* PING_USE_SOCKETS */
229 
230 /**
231  *
232  * Re-implement ping_init and ping_deinit with the APIs from ping_sock.h for back-ward compatibility sake.
233  * It's highly recommended that users should use the new APIs from ping_sock.h.
234  * ToDo: ping.h and esp_ping.h are deprecated now and should be removed in idf v5.x.
235  *
236  */
237 
238 static void
on_ping_success(esp_ping_handle_t hdl,void * args)239 on_ping_success(esp_ping_handle_t hdl, void *args)
240 {
241   uint32_t elapsed_time, recv_len;
242   esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
243   esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
244   esp_ping_result(PING_RES_OK, recv_len, elapsed_time);
245 }
246 
247 static void
on_ping_timeout(esp_ping_handle_t hdl,void * args)248 on_ping_timeout(esp_ping_handle_t hdl, void *args)
249 {
250   uint32_t elapsed_time, recv_len;
251   esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
252   esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
253   esp_ping_result(PING_RES_TIMEOUT, recv_len, elapsed_time);
254 }
255 
256 static void
on_ping_end(esp_ping_handle_t hdl,void * args)257 on_ping_end(esp_ping_handle_t hdl, void *args)
258 {
259   esp_ping_result(PING_RES_FINISH, 0, 0);
260   esp_ping_delete_session(hdl);
261 }
262 
263 int
ping_init(void)264 ping_init(void)
265 {
266 #if PING_USE_SOCKETS
267   uint32_t tos = 0;
268   uint32_t ping_timeout = PING_RCV_TIMEO;
269   uint32_t ping_delay = PING_DELAY;
270   uint32_t ping_count_max = 3;
271   uint32_t interface = 0;
272   ip_addr_t ipaddr;
273 
274   esp_ping_get_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count_max, sizeof(ping_count_max));
275   esp_ping_get_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(ping_timeout));
276   esp_ping_get_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(ping_delay));
277   esp_ping_get_target(PING_TARGET_IP_ADDRESS, &ipaddr, sizeof(ip_addr_t));
278   esp_ping_get_target(PING_TARGET_IP_TOS, &tos, sizeof(tos));
279   esp_ping_get_target(PING_TARGET_IF_INDEX, &interface, sizeof(interface));
280 
281   esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
282   config.count = ping_count_max;
283   config.timeout_ms = ping_timeout;
284   config.interval_ms = ping_delay;
285   config.target_addr = ipaddr;
286   config.tos = tos;
287   config.interface = interface;
288 
289   esp_ping_callbacks_t cbs = {
290     .on_ping_end = on_ping_end,
291     .on_ping_success = on_ping_success,
292     .on_ping_timeout = on_ping_timeout,
293   };
294 
295   esp_ping_new_session(&config, &cbs, &ping);
296   esp_ping_start(ping);
297 #else /* PING_USE_SOCKETS */
298   ping_raw_init();
299 #endif /* PING_USE_SOCKETS */
300   return ERR_OK;
301 }
302 
303 void
ping_deinit(void)304 ping_deinit(void)
305 {
306   esp_ping_stop(ping);
307   esp_ping_delete_session(ping);
308 }
309 
310 #endif /* LWIP_IPV4 && LWIP_RAW */
311