ping.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * @file
  3. * Ping sender module
  4. *
  5. */
  6. /*
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  21. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  23. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  26. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. *
  29. * This file is part of the lwIP TCP/IP stack.
  30. *
  31. */
  32. /**
  33. * This is an example of a "ping" sender (with raw API and socket API).
  34. * It can be used as a start point to maintain opened a network connection, or
  35. * like a network "watchdog" for your device.
  36. *
  37. */
  38. #include "lwip/opt.h"
  39. #if LWIP_IPV4 && LWIP_RAW /* don't build if not configured for use in lwipopts.h */
  40. #include "ping/ping.h"
  41. #include "lwip/mem.h"
  42. #include "lwip/raw.h"
  43. #include "lwip/icmp.h"
  44. #include "lwip/netif.h"
  45. #include "lwip/sys.h"
  46. #include "lwip/timeouts.h"
  47. #include "lwip/inet_chksum.h"
  48. #if PING_USE_SOCKETS
  49. #include "lwip/sockets.h"
  50. #include "lwip/inet.h"
  51. #include "esp_task.h"
  52. #include "ping/ping_sock.h"
  53. #endif /* PING_USE_SOCKETS */
  54. #ifdef ESP_PING
  55. #include "esp_ping.h"
  56. #include "lwip/ip_addr.h"
  57. #endif
  58. /**
  59. * PING_DEBUG: Enable debugging for PING.
  60. */
  61. #ifndef PING_DEBUG
  62. #define PING_DEBUG LWIP_DBG_OFF
  63. #endif
  64. /** ping target - should be an "ip4_addr_t" */
  65. #ifndef PING_TARGET
  66. #define PING_TARGET (netif_default ? *netif_ip4_gw(netif_default) : (*IP4_ADDR_ANY))
  67. #endif
  68. /** ping receive timeout - in milliseconds */
  69. #ifndef PING_RCV_TIMEO
  70. #define PING_RCV_TIMEO 1000
  71. #endif
  72. /** ping delay - in milliseconds */
  73. #ifndef PING_DELAY
  74. #define PING_DELAY 1000
  75. #endif
  76. /** ping identifier - must fit on a u16_t */
  77. #ifndef PING_ID
  78. #define PING_ID 0xAFAF
  79. #endif
  80. /** ping additional data size to include in the packet */
  81. #ifndef PING_DATA_SIZE
  82. #define PING_DATA_SIZE 32
  83. #endif
  84. /** ping result action - no default action */
  85. #ifndef PING_RESULT
  86. #define PING_RESULT(ping_ok)
  87. #endif
  88. #define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + (_end.tv_usec - _start.tv_usec)/1000))
  89. #define PING_TIME_DIFF_SEC(_end, _start) ((uint32_t)((_end).tv_sec - (_start).tv_sec))
  90. #if PING_USE_SOCKETS
  91. /* ping handle */
  92. static esp_ping_handle_t ping = NULL;
  93. #else
  94. static struct raw_pcb *ping_pcb;
  95. static u16_t ping_seq_num;
  96. static struct timeval ping_time;
  97. /** Prepare a echo ICMP request */
  98. static void
  99. ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  100. {
  101. size_t i;
  102. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  103. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  104. ICMPH_CODE_SET(iecho, 0);
  105. iecho->chksum = 0;
  106. iecho->id = PING_ID;
  107. iecho->seqno = htons(++ping_seq_num);
  108. /* fill the additional data buffer with some data */
  109. for(i = 0; i < data_len; i++) {
  110. ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
  111. }
  112. iecho->chksum = inet_chksum(iecho, len);
  113. }
  114. /* Ping using the raw ip */
  115. static u8_t
  116. ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
  117. {
  118. struct icmp_echo_hdr *iecho;
  119. struct timeval now;
  120. LWIP_UNUSED_ARG(arg);
  121. LWIP_UNUSED_ARG(pcb);
  122. LWIP_UNUSED_ARG(addr);
  123. LWIP_ASSERT("p != NULL", p != NULL);
  124. if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
  125. pbuf_header(p, -PBUF_IP_HLEN) == 0) {
  126. iecho = (struct icmp_echo_hdr *)p->payload;
  127. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
  128. LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
  129. ip_addr_debug_print(PING_DEBUG, addr);
  130. gettimeofday(&now, NULL);
  131. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", PING_TIME_DIFF_MS(now, ping_time)));
  132. /* do some ping result processing */
  133. PING_RESULT(1);
  134. pbuf_free(p);
  135. return 1; /* eat the packet */
  136. }
  137. /* not eaten, restore original packet */
  138. pbuf_header(p, PBUF_IP_HLEN);
  139. }
  140. return 0; /* don't eat the packet */
  141. }
  142. static void
  143. ping_send(struct raw_pcb *raw, ip_addr_t *addr)
  144. {
  145. struct pbuf *p;
  146. struct icmp_echo_hdr *iecho;
  147. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  148. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  149. ip_addr_debug_print(PING_DEBUG, addr);
  150. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  151. LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
  152. p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
  153. if (!p) {
  154. return;
  155. }
  156. if ((p->len == p->tot_len) && (p->next == NULL)) {
  157. iecho = (struct icmp_echo_hdr *)p->payload;
  158. ping_prepare_echo(iecho, (u16_t)ping_size);
  159. raw_sendto(raw, p, addr);
  160. ping_time = system_get_time();
  161. }
  162. pbuf_free(p);
  163. }
  164. static void
  165. ping_timeout(void *arg)
  166. {
  167. struct raw_pcb *pcb = (struct raw_pcb*)arg;
  168. ip_addr_t ping_target;
  169. LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
  170. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  171. ping_send(pcb, &ping_target);
  172. sys_timeout(PING_DELAY, ping_timeout, pcb);
  173. }
  174. static void
  175. ping_raw_init(void)
  176. {
  177. ping_pcb = raw_new(IP_PROTO_ICMP);
  178. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  179. raw_recv(ping_pcb, ping_recv, NULL);
  180. raw_bind(ping_pcb, IP_ADDR_ANY);
  181. sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
  182. }
  183. void
  184. ping_send_now(void)
  185. {
  186. ip_addr_t ping_target;
  187. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  188. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  189. ping_send(ping_pcb, &ping_target);
  190. }
  191. #endif /* PING_USE_SOCKETS */
  192. /**
  193. *
  194. * Re-implement ping_init and ping_deinit with the APIs from ping_sock.h for back-ward compatibility sake.
  195. * It's highly recommended that users should use the new APIs from ping_sock.h.
  196. * ToDo: ping.h and esp_ping.h are deprecated now and should be removed in idf v5.x.
  197. *
  198. */
  199. static void
  200. on_ping_success(esp_ping_handle_t hdl, void *args)
  201. {
  202. uint32_t elapsed_time, recv_len;
  203. esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  204. esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  205. esp_ping_result(PING_RES_OK, recv_len, elapsed_time);
  206. }
  207. static void
  208. on_ping_timeout(esp_ping_handle_t hdl, void *args)
  209. {
  210. uint32_t elapsed_time, recv_len;
  211. esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
  212. esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
  213. esp_ping_result(PING_RES_TIMEOUT, recv_len, elapsed_time);
  214. }
  215. static void
  216. on_ping_end(esp_ping_handle_t hdl, void *args)
  217. {
  218. esp_ping_result(PING_RES_FINISH, 0, 0);
  219. esp_ping_delete_session(hdl);
  220. }
  221. int
  222. ping_init(void)
  223. {
  224. #if PING_USE_SOCKETS
  225. uint32_t tos = 0;
  226. uint32_t ping_timeout = PING_RCV_TIMEO;
  227. uint32_t ping_delay = PING_DELAY;
  228. uint32_t ping_count_max = 3;
  229. uint32_t interface = 0;
  230. ip_addr_t ipaddr;
  231. esp_ping_get_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count_max, sizeof(ping_count_max));
  232. esp_ping_get_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(ping_timeout));
  233. esp_ping_get_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(ping_delay));
  234. esp_ping_get_target(PING_TARGET_IP_ADDRESS, &ipaddr, sizeof(ip_addr_t));
  235. esp_ping_get_target(PING_TARGET_IP_TOS, &tos, sizeof(tos));
  236. esp_ping_get_target(PING_TARGET_IF_INDEX, &interface, sizeof(interface));
  237. esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
  238. config.count = ping_count_max;
  239. config.timeout_ms = ping_timeout;
  240. config.interval_ms = ping_delay;
  241. config.target_addr = ipaddr;
  242. config.tos = tos;
  243. config.interface = interface;
  244. esp_ping_callbacks_t cbs = {
  245. .on_ping_end = on_ping_end,
  246. .on_ping_success = on_ping_success,
  247. .on_ping_timeout = on_ping_timeout,
  248. };
  249. esp_ping_new_session(&config, &cbs, &ping);
  250. esp_ping_start(ping);
  251. #else /* PING_USE_SOCKETS */
  252. ping_raw_init();
  253. #endif /* PING_USE_SOCKETS */
  254. return ERR_OK;
  255. }
  256. void
  257. ping_deinit(void)
  258. {
  259. esp_ping_stop(ping);
  260. esp_ping_delete_session(ping);
  261. }
  262. #endif /* LWIP_IPV4 && LWIP_RAW */