ping.c 8.6 KB

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