1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdlib.h>
16 #include <stdbool.h>
17 #include "freertos/FreeRTOS.h"
18 #include "freertos/task.h"
19 #include "lwip/opt.h"
20 #include "lwip/init.h"
21 #include "lwip/mem.h"
22 #include "lwip/icmp.h"
23 #include "lwip/netif.h"
24 #include "lwip/sys.h"
25 #include "lwip/timeouts.h"
26 #include "lwip/inet.h"
27 #include "lwip/inet_chksum.h"
28 #include "lwip/ip.h"
29 #include "lwip/netdb.h"
30 #include "lwip/sockets.h"
31 #include "esp_log.h"
32 #include "ping/ping_sock.h"
33
34 const static char *TAG = "ping_sock";
35
36 #define PING_CHECK(a, str, goto_tag, ret_value, ...) \
37 do \
38 { \
39 if (!(a)) \
40 { \
41 ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
42 ret = ret_value; \
43 goto goto_tag; \
44 } \
45 } while (0)
46
47 #define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + \
48 ((_end).tv_usec - (_start).tv_usec) / 1000))
49
50 #define PING_CHECK_START_TIMEOUT_MS (1000)
51
52 #define PING_FLAGS_INIT (1 << 0)
53 #define PING_FLAGS_START (1 << 1)
54
55 typedef struct {
56 int sock;
57 struct sockaddr_storage target_addr;
58 TaskHandle_t ping_task_hdl;
59 struct icmp_echo_hdr *packet_hdr;
60 ip_addr_t recv_addr;
61 uint32_t recv_len;
62 uint32_t icmp_pkt_size;
63 uint32_t count;
64 uint32_t transmitted;
65 uint32_t received;
66 uint32_t interval_ms;
67 uint32_t elapsed_time_ms;
68 uint32_t total_time_ms;
69 uint8_t ttl;
70 uint32_t flags;
71 void (*on_ping_success)(esp_ping_handle_t hdl, void *args);
72 void (*on_ping_timeout)(esp_ping_handle_t hdl, void *args);
73 void (*on_ping_end)(esp_ping_handle_t hdl, void *args);
74 void *cb_args;
75 } esp_ping_t;
76
esp_ping_send(esp_ping_t * ep)77 static esp_err_t esp_ping_send(esp_ping_t *ep)
78 {
79 esp_err_t ret = ESP_OK;
80 ep->packet_hdr->seqno++;
81 /* generate checksum since "seqno" has changed */
82 ep->packet_hdr->chksum = 0;
83 if (ep->packet_hdr->type == ICMP_ECHO) {
84 ep->packet_hdr->chksum = inet_chksum(ep->packet_hdr, ep->icmp_pkt_size);
85 }
86
87 ssize_t sent = sendto(ep->sock, ep->packet_hdr, ep->icmp_pkt_size, 0,
88 (struct sockaddr *)&ep->target_addr, sizeof(ep->target_addr));
89
90 if (sent != (ssize_t)ep->icmp_pkt_size) {
91 int opt_val;
92 socklen_t opt_len = sizeof(opt_val);
93 getsockopt(ep->sock, SOL_SOCKET, SO_ERROR, &opt_val, &opt_len);
94 ESP_LOGE(TAG, "send error=%d", opt_val);
95 ret = ESP_FAIL;
96 } else {
97 ep->transmitted++;
98 }
99 return ret;
100 }
101
esp_ping_receive(esp_ping_t * ep)102 static int esp_ping_receive(esp_ping_t *ep)
103 {
104 char buf[64]; // 64 bytes are enough to cover IP header and ICMP header
105 int len = 0;
106 struct sockaddr_storage from;
107 int fromlen = sizeof(from);
108 uint16_t data_head = 0;
109
110 while ((len = recvfrom(ep->sock, buf, sizeof(buf), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen)) > 0) {
111 if (from.ss_family == AF_INET) {
112 // IPv4
113 struct sockaddr_in *from4 = (struct sockaddr_in *)&from;
114 inet_addr_to_ip4addr(ip_2_ip4(&ep->recv_addr), &from4->sin_addr);
115 IP_SET_TYPE_VAL(ep->recv_addr, IPADDR_TYPE_V4);
116 data_head = (uint16_t)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr));
117 }
118 #if CONFIG_LWIP_IPV6
119 else {
120 // IPv6
121 struct sockaddr_in6 *from6 = (struct sockaddr_in6 *)&from;
122 inet6_addr_to_ip6addr(ip_2_ip6(&ep->recv_addr), &from6->sin6_addr);
123 IP_SET_TYPE_VAL(ep->recv_addr, IPADDR_TYPE_V6);
124 data_head = (uint16_t)(sizeof(struct ip6_hdr) + sizeof(struct icmp6_echo_hdr));
125 }
126 #endif
127 if (len >= data_head) {
128 if (IP_IS_V4_VAL(ep->recv_addr)) { // Currently we process IPv4
129 struct ip_hdr *iphdr = (struct ip_hdr *)buf;
130 struct icmp_echo_hdr *iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
131 if ((iecho->id == ep->packet_hdr->id) && (iecho->seqno == ep->packet_hdr->seqno)) {
132 ep->received++;
133 ep->ttl = iphdr->_ttl;
134 ep->recv_len = lwip_ntohs(IPH_LEN(iphdr)) - data_head; // The data portion of ICMP
135 return len;
136 }
137 }
138 #if CONFIG_LWIP_IPV6
139 else if (IP_IS_V6_VAL(ep->recv_addr)) { // Currently we process IPv6
140 struct ip6_hdr *iphdr = (struct ip6_hdr *)buf;
141 struct icmp6_echo_hdr *iecho6 = (struct icmp6_echo_hdr *)(buf + sizeof(struct ip6_hdr)); // IPv6 head length is 40
142 if ((iecho6->id == ep->packet_hdr->id) && (iecho6->seqno == ep->packet_hdr->seqno)) {
143 ep->received++;
144 ep->recv_len = IP6H_PLEN(iphdr) - sizeof(struct icmp6_echo_hdr); //The data portion of ICMPv6
145 return len;
146 }
147 }
148 #endif
149 }
150 fromlen = sizeof(from);
151 }
152 // if timeout, len will be -1
153 return len;
154 }
155
esp_ping_thread(void * args)156 static void esp_ping_thread(void *args)
157 {
158 esp_ping_t *ep = (esp_ping_t *)(args);
159 TickType_t last_wake;
160 struct timeval start_time, end_time;
161 int recv_ret;
162
163 while (1) {
164 /* wait for ping start signal */
165 if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(PING_CHECK_START_TIMEOUT_MS))) {
166 /* initialize runtime statistics */
167 ep->packet_hdr->seqno = 0;
168 ep->transmitted = 0;
169 ep->received = 0;
170 ep->total_time_ms = 0;
171
172 last_wake = xTaskGetTickCount();
173 while ((ep->flags & PING_FLAGS_START) && ((ep->count == 0) || (ep->packet_hdr->seqno < ep->count))) {
174 esp_ping_send(ep);
175 gettimeofday(&start_time, NULL);
176 recv_ret = esp_ping_receive(ep);
177 gettimeofday(&end_time, NULL);
178 ep->elapsed_time_ms = PING_TIME_DIFF_MS(end_time, start_time);
179 ep->total_time_ms += ep->elapsed_time_ms;
180 if (recv_ret >= 0) {
181 if (ep->on_ping_success) {
182 ep->on_ping_success((esp_ping_handle_t)ep, ep->cb_args);
183 }
184 } else {
185 if (ep->on_ping_timeout) {
186 ep->on_ping_timeout((esp_ping_handle_t)ep, ep->cb_args);
187 }
188 }
189 if (pdMS_TO_TICKS(ep->interval_ms)) {
190 vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(ep->interval_ms)); // to get a more accurate delay
191 }
192 }
193 /* batch of ping operations finished */
194 if (ep->on_ping_end) {
195 ep->on_ping_end((esp_ping_handle_t)ep, ep->cb_args);
196 }
197 } else {
198 // check if ping has been de-initialized
199 if (!(ep->flags & PING_FLAGS_INIT)) {
200 break;
201 }
202 }
203 }
204 /* before exit task, free all resources */
205 if (ep->packet_hdr) {
206 free(ep->packet_hdr);
207 }
208 if (ep->sock > 0) {
209 close(ep->sock);
210 }
211 free(ep);
212 vTaskDelete(NULL);
213 }
214
esp_ping_new_session(const esp_ping_config_t * config,const esp_ping_callbacks_t * cbs,esp_ping_handle_t * hdl_out)215 esp_err_t esp_ping_new_session(const esp_ping_config_t *config, const esp_ping_callbacks_t *cbs, esp_ping_handle_t *hdl_out)
216 {
217 esp_err_t ret = ESP_FAIL;
218 esp_ping_t *ep = NULL;
219 PING_CHECK(config, "ping config can't be null", err, ESP_ERR_INVALID_ARG);
220 PING_CHECK(hdl_out, "ping handle can't be null", err, ESP_ERR_INVALID_ARG);
221
222 ep = mem_calloc(1, sizeof(esp_ping_t));
223 PING_CHECK(ep, "no memory for esp_ping object", err, ESP_ERR_NO_MEM);
224
225 /* set INIT flag, so that ping task won't exit (must set before create ping task) */
226 ep->flags |= PING_FLAGS_INIT;
227
228 /* create ping thread */
229 BaseType_t xReturned = xTaskCreate(esp_ping_thread, "ping", config->task_stack_size, ep,
230 config->task_prio, &ep->ping_task_hdl);
231 PING_CHECK(xReturned == pdTRUE, "create ping task failed", err, ESP_ERR_NO_MEM);
232
233 /* callback functions */
234 if (cbs) {
235 ep->cb_args = cbs->cb_args;
236 ep->on_ping_end = cbs->on_ping_end;
237 ep->on_ping_timeout = cbs->on_ping_timeout;
238 ep->on_ping_success = cbs->on_ping_success;
239 }
240 /* set parameters for ping */
241 ep->recv_addr = config->target_addr;
242 ep->count = config->count;
243 ep->interval_ms = config->interval_ms;
244 ep->icmp_pkt_size = sizeof(struct icmp_echo_hdr) + config->data_size;
245 ep->packet_hdr = mem_calloc(1, ep->icmp_pkt_size);
246 PING_CHECK(ep->packet_hdr, "no memory for echo packet", err, ESP_ERR_NO_MEM);
247 /* set ICMP type and code field */
248 ep->packet_hdr->code = 0;
249 /* ping id should be unique, treat task handle as ping ID */
250 ep->packet_hdr->id = ((uint32_t)ep->ping_task_hdl) & 0xFFFF;
251 /* fill the additional data buffer with some data */
252 char *d = (char *)(ep->packet_hdr) + sizeof(struct icmp_echo_hdr);
253 for (uint32_t i = 0; i < config->data_size; i++) {
254 d[i] = 'A' + i;
255 }
256
257 /* create socket */
258 if (IP_IS_V4(&config->target_addr)
259 #if CONFIG_LWIP_IPV6
260 || ip6_addr_isipv4mappedipv6(ip_2_ip6(&config->target_addr))
261 #endif
262 ) {
263 ep->sock = socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
264 }
265 #if CONFIG_LWIP_IPV6
266 else {
267 ep->sock = socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
268 }
269 #endif
270 PING_CHECK(ep->sock > 0, "create socket failed: %d", err, ESP_FAIL, ep->sock);
271 /* set if index */
272 if(config->interface) {
273 struct ifreq iface;
274 if(netif_index_to_name(config->interface, iface.ifr_name) == NULL) {
275 ESP_LOGE(TAG, "fail to find interface name with netif index %d", config->interface);
276 goto err;
277 }
278 if(setsockopt(ep->sock, SOL_SOCKET, SO_BINDTODEVICE, &iface, sizeof(iface)) != 0) {
279 ESP_LOGE(TAG, "fail to setsockopt SO_BINDTODEVICE");
280 goto err;
281 }
282 }
283 struct timeval timeout;
284 timeout.tv_sec = config->timeout_ms / 1000;
285 timeout.tv_usec = (config->timeout_ms % 1000) * 1000;
286 /* set receive timeout */
287 setsockopt(ep->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
288
289 /* set tos */
290 setsockopt(ep->sock, IPPROTO_IP, IP_TOS, &config->tos, sizeof(config->tos));
291
292 /* set socket address */
293 if (IP_IS_V4(&config->target_addr)) {
294 struct sockaddr_in *to4 = (struct sockaddr_in *)&ep->target_addr;
295 to4->sin_family = AF_INET;
296 inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(&config->target_addr));
297 ep->packet_hdr->type = ICMP_ECHO;
298 }
299 #if CONFIG_LWIP_IPV6
300 if (IP_IS_V6(&config->target_addr)) {
301 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ep->target_addr;
302 to6->sin6_family = AF_INET6;
303 inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(&config->target_addr));
304 ep->packet_hdr->type = ICMP6_TYPE_EREQ;
305 }
306 #endif
307 /* return ping handle to user */
308 *hdl_out = (esp_ping_handle_t)ep;
309 return ESP_OK;
310 err:
311 if (ep) {
312 if (ep->sock > 0) {
313 close(ep->sock);
314 }
315 if (ep->packet_hdr) {
316 free(ep->packet_hdr);
317 }
318 if (ep->ping_task_hdl) {
319 vTaskDelete(ep->ping_task_hdl);
320 }
321 free(ep);
322 }
323 return ret;
324 }
325
esp_ping_delete_session(esp_ping_handle_t hdl)326 esp_err_t esp_ping_delete_session(esp_ping_handle_t hdl)
327 {
328 esp_err_t ret = ESP_OK;
329 esp_ping_t *ep = (esp_ping_t *)hdl;
330 PING_CHECK(ep, "ping handle can't be null", err, ESP_ERR_INVALID_ARG);
331 /* reset init flags, then ping task will exit */
332 ep->flags &= ~PING_FLAGS_INIT;
333 return ESP_OK;
334 err:
335 return ret;
336 }
337
esp_ping_start(esp_ping_handle_t hdl)338 esp_err_t esp_ping_start(esp_ping_handle_t hdl)
339 {
340 esp_err_t ret = ESP_OK;
341 esp_ping_t *ep = (esp_ping_t *)hdl;
342 PING_CHECK(ep, "ping handle can't be null", err, ESP_ERR_INVALID_ARG);
343 ep->flags |= PING_FLAGS_START;
344 xTaskNotifyGive(ep->ping_task_hdl);
345 return ESP_OK;
346 err:
347 return ret;
348 }
349
esp_ping_stop(esp_ping_handle_t hdl)350 esp_err_t esp_ping_stop(esp_ping_handle_t hdl)
351 {
352 esp_err_t ret = ESP_OK;
353 esp_ping_t *ep = (esp_ping_t *)hdl;
354 PING_CHECK(ep, "ping handle can't be null", err, ESP_ERR_INVALID_ARG);
355 ep->flags &= ~PING_FLAGS_START;
356 return ESP_OK;
357 err:
358 return ret;
359 }
360
esp_ping_get_profile(esp_ping_handle_t hdl,esp_ping_profile_t profile,void * data,uint32_t size)361 esp_err_t esp_ping_get_profile(esp_ping_handle_t hdl, esp_ping_profile_t profile, void *data, uint32_t size)
362 {
363 esp_err_t ret = ESP_OK;
364 esp_ping_t *ep = (esp_ping_t *)hdl;
365 const void *from = NULL;
366 uint32_t copy_size = 0;
367 PING_CHECK(ep, "ping handle can't be null", err, ESP_ERR_INVALID_ARG);
368 PING_CHECK(data, "profile data can't be null", err, ESP_ERR_INVALID_ARG);
369 switch (profile) {
370 case ESP_PING_PROF_SEQNO:
371 from = &ep->packet_hdr->seqno;
372 copy_size = sizeof(ep->packet_hdr->seqno);
373 break;
374 case ESP_PING_PROF_TTL:
375 from = &ep->ttl;
376 copy_size = sizeof(ep->ttl);
377 break;
378 case ESP_PING_PROF_REQUEST:
379 from = &ep->transmitted;
380 copy_size = sizeof(ep->transmitted);
381 break;
382 case ESP_PING_PROF_REPLY:
383 from = &ep->received;
384 copy_size = sizeof(ep->received);
385 break;
386 case ESP_PING_PROF_IPADDR:
387 from = &ep->recv_addr;
388 copy_size = sizeof(ep->recv_addr);
389 break;
390 case ESP_PING_PROF_SIZE:
391 from = &ep->recv_len;
392 copy_size = sizeof(ep->recv_len);
393 break;
394 case ESP_PING_PROF_TIMEGAP:
395 from = &ep->elapsed_time_ms;
396 copy_size = sizeof(ep->elapsed_time_ms);
397 break;
398 case ESP_PING_PROF_DURATION:
399 from = &ep->total_time_ms;
400 copy_size = sizeof(ep->total_time_ms);
401 break;
402 default:
403 PING_CHECK(false, "unknow profile: %d", err, ESP_ERR_INVALID_ARG, profile);
404 break;
405 }
406 PING_CHECK(size >= copy_size, "unmatched data size for profile %d", err, ESP_ERR_INVALID_SIZE, profile);
407 memcpy(data, from, copy_size);
408 return ESP_OK;
409 err:
410 return ret;
411 }
412