ping.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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.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/timers.h"
  47. #include "lwip/inet_chksum.h"
  48. #if PING_USE_SOCKETS
  49. #include "lwip/sockets.h"
  50. #include "lwip/inet.h"
  51. #endif /* PING_USE_SOCKETS */
  52. #ifdef ESP_LWIP
  53. #include "esp_ping.h"
  54. #include "lwip/ip_addr.h"
  55. #endif
  56. /**
  57. * PING_DEBUG: Enable debugging for PING.
  58. */
  59. #ifndef PING_DEBUG
  60. #define PING_DEBUG LWIP_DBG_ON
  61. #endif
  62. /** ping target - should be an "ip4_addr_t" */
  63. #ifndef PING_TARGET
  64. #define PING_TARGET (netif_default ? *netif_ip4_gw(netif_default) : (*IP4_ADDR_ANY))
  65. #endif
  66. /** ping receive timeout - in milliseconds */
  67. #ifndef PING_RCV_TIMEO
  68. #define PING_RCV_TIMEO 1000
  69. #endif
  70. /** ping delay - in milliseconds */
  71. #ifndef PING_DELAY
  72. #define PING_DELAY 1000
  73. #endif
  74. /** ping identifier - must fit on a u16_t */
  75. #ifndef PING_ID
  76. #define PING_ID 0xAFAF
  77. #endif
  78. /** ping additional data size to include in the packet */
  79. #ifndef PING_DATA_SIZE
  80. #define PING_DATA_SIZE 32
  81. #endif
  82. /** ping result action - no default action */
  83. #ifndef PING_RESULT
  84. #define PING_RESULT(ping_ok)
  85. #endif
  86. /* ping variables */
  87. static u16_t ping_seq_num;
  88. static u32_t ping_time;
  89. #if !PING_USE_SOCKETS
  90. static struct raw_pcb *ping_pcb;
  91. #endif /* PING_USE_SOCKETS */
  92. /** Prepare a echo ICMP request */
  93. static void
  94. ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  95. {
  96. size_t i;
  97. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  98. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  99. ICMPH_CODE_SET(iecho, 0);
  100. iecho->chksum = 0;
  101. iecho->id = PING_ID;
  102. iecho->seqno = lwip_htons(++ping_seq_num);
  103. /* fill the additional data buffer with some data */
  104. for(i = 0; i < data_len; i++) {
  105. ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
  106. }
  107. iecho->chksum = inet_chksum(iecho, len);
  108. }
  109. #if PING_USE_SOCKETS
  110. /* Ping using the socket ip */
  111. static err_t
  112. ping_send(int s, ip_addr_t *addr)
  113. {
  114. int err;
  115. struct icmp_echo_hdr *iecho;
  116. struct sockaddr_in to;
  117. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  118. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  119. LWIP_ASSERT("ping: expect IPv4 address", !IP_IS_V6(addr));
  120. iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
  121. if (!iecho) {
  122. return ERR_MEM;
  123. }
  124. ping_prepare_echo(iecho, (u16_t)ping_size);
  125. to.sin_len = sizeof(to);
  126. to.sin_family = AF_INET;
  127. inet_addr_from_ipaddr(&to.sin_addr, ip_2_ip4(addr));
  128. err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
  129. mem_free(iecho);
  130. return (err ? ERR_OK : ERR_VAL);
  131. }
  132. static void
  133. ping_recv(int s)
  134. {
  135. char buf[64];
  136. int len;
  137. struct sockaddr_in from;
  138. struct ip_hdr *iphdr;
  139. struct icmp_echo_hdr *iecho;
  140. int fromlen = sizeof(from);
  141. while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
  142. if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
  143. if (from.sin_family != AF_INET) {
  144. /* Ping is not IPv4 */
  145. LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n"));
  146. } else {
  147. ip4_addr_t fromaddr;
  148. inet_addr_to_ipaddr(&fromaddr, &from.sin_addr);
  149. LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
  150. ip4_addr_debug_print(PING_DEBUG, &fromaddr);
  151. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
  152. iphdr = (struct ip_hdr *)buf;
  153. iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
  154. if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
  155. /* do some ping result processing */
  156. #ifdef ESP_LWIP
  157. esp_ping_result((ICMPH_TYPE(iecho) == ICMP_ER), len, (sys_now() - ping_time));
  158. #else
  159. PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
  160. #endif
  161. return;
  162. } else {
  163. LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
  164. }
  165. }
  166. }
  167. fromlen = sizeof(from);
  168. }
  169. if (len == 0) {
  170. LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
  171. }
  172. /* do some ping result processing */
  173. #ifdef ESP_LWIP
  174. esp_ping_result(0, len, (sys_now()-ping_time));
  175. #else
  176. PING_RESULT(0);
  177. #endif
  178. }
  179. static void
  180. ping_thread(void *arg)
  181. {
  182. int s;
  183. int ret;
  184. ip_addr_t ping_target;
  185. #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
  186. int timeout = PING_RCV_TIMEO;
  187. #else
  188. struct timeval timeout;
  189. timeout.tv_sec = PING_RCV_TIMEO/1000;
  190. timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000;
  191. #endif
  192. LWIP_UNUSED_ARG(arg);
  193. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
  194. return;
  195. }
  196. ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  197. LWIP_ASSERT("setting receive timeout failed", ret == 0);
  198. LWIP_UNUSED_ARG(ret);
  199. while (1) {
  200. #ifdef ESP_LWIP
  201. ip4_addr_t ipaddr;
  202. esp_ping_get_target(PING_TARGET_IP_ADDRESS, &ipaddr.addr, sizeof(uint32_t));
  203. ip_addr_copy_from_ip4(ping_target, ipaddr);
  204. #else
  205. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  206. #endif
  207. if (ping_send(s, &ping_target) == ERR_OK) {
  208. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  209. ip_addr_debug_print(PING_DEBUG, &ping_target);
  210. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  211. ping_time = sys_now();
  212. ping_recv(s);
  213. } else {
  214. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  215. ip_addr_debug_print(PING_DEBUG, &ping_target);
  216. LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
  217. }
  218. sys_msleep(PING_DELAY);
  219. }
  220. }
  221. #else /* PING_USE_SOCKETS */
  222. /* Ping using the raw ip */
  223. static u8_t
  224. ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
  225. {
  226. struct icmp_echo_hdr *iecho;
  227. LWIP_UNUSED_ARG(arg);
  228. LWIP_UNUSED_ARG(pcb);
  229. LWIP_UNUSED_ARG(addr);
  230. LWIP_ASSERT("p != NULL", p != NULL);
  231. if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
  232. pbuf_header(p, -PBUF_IP_HLEN) == 0) {
  233. iecho = (struct icmp_echo_hdr *)p->payload;
  234. if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
  235. LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
  236. ip_addr_debug_print(PING_DEBUG, addr);
  237. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
  238. /* do some ping result processing */
  239. PING_RESULT(1);
  240. pbuf_free(p);
  241. return 1; /* eat the packet */
  242. }
  243. /* not eaten, restore original packet */
  244. pbuf_header(p, PBUF_IP_HLEN);
  245. }
  246. return 0; /* don't eat the packet */
  247. }
  248. static void
  249. ping_send(struct raw_pcb *raw, ip_addr_t *addr)
  250. {
  251. struct pbuf *p;
  252. struct icmp_echo_hdr *iecho;
  253. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  254. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  255. ip_addr_debug_print(PING_DEBUG, addr);
  256. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  257. LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
  258. p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
  259. if (!p) {
  260. return;
  261. }
  262. if ((p->len == p->tot_len) && (p->next == NULL)) {
  263. iecho = (struct icmp_echo_hdr *)p->payload;
  264. ping_prepare_echo(iecho, (u16_t)ping_size);
  265. raw_sendto(raw, p, addr);
  266. ping_time = sys_now();
  267. }
  268. pbuf_free(p);
  269. }
  270. static void
  271. ping_timeout(void *arg)
  272. {
  273. struct raw_pcb *pcb = (struct raw_pcb*)arg;
  274. ip_addr_t ping_target;
  275. LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
  276. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  277. ping_send(pcb, &ping_target);
  278. sys_timeout(PING_DELAY, ping_timeout, pcb);
  279. }
  280. static void
  281. ping_raw_init(void)
  282. {
  283. ping_pcb = raw_new(IP_PROTO_ICMP);
  284. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  285. raw_recv(ping_pcb, ping_recv, NULL);
  286. raw_bind(ping_pcb, IP_ADDR_ANY);
  287. sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
  288. }
  289. void
  290. ping_send_now(void)
  291. {
  292. ip_addr_t ping_target;
  293. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  294. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  295. ping_send(ping_pcb, &ping_target);
  296. }
  297. #endif /* PING_USE_SOCKETS */
  298. void
  299. ping_init(void)
  300. {
  301. #if PING_USE_SOCKETS
  302. sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  303. #else /* PING_USE_SOCKETS */
  304. ping_raw_init();
  305. #endif /* PING_USE_SOCKETS */
  306. }
  307. #endif /* LWIP_IPV4 && LWIP_RAW */