ping.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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_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/timeouts.h"
  47. #include "lwip/inet_chksum.h"
  48. #include "lwip/prot/ip4.h"
  49. #if PING_USE_SOCKETS
  50. #include "lwip/sockets.h"
  51. #include "lwip/inet.h"
  52. #include <string.h>
  53. #endif /* PING_USE_SOCKETS */
  54. /**
  55. * PING_DEBUG: Enable debugging for PING.
  56. */
  57. #ifndef PING_DEBUG
  58. #define PING_DEBUG LWIP_DBG_ON
  59. #endif
  60. /** ping receive timeout - in milliseconds */
  61. #ifndef PING_RCV_TIMEO
  62. #define PING_RCV_TIMEO 1000
  63. #endif
  64. /** ping delay - in milliseconds */
  65. #ifndef PING_DELAY
  66. #define PING_DELAY 1000
  67. #endif
  68. /** ping identifier - must fit on a u16_t */
  69. #ifndef PING_ID
  70. #define PING_ID 0xAFAF
  71. #endif
  72. /** ping additional data size to include in the packet */
  73. #ifndef PING_DATA_SIZE
  74. #define PING_DATA_SIZE 32
  75. #endif
  76. /** ping result action - no default action */
  77. #ifndef PING_RESULT
  78. #define PING_RESULT(ping_ok)
  79. #endif
  80. /* ping variables */
  81. static const ip_addr_t* ping_target;
  82. static u16_t ping_seq_num;
  83. #ifdef LWIP_DEBUG
  84. static u32_t ping_time;
  85. #endif /* LWIP_DEBUG */
  86. #if !PING_USE_SOCKETS
  87. static struct raw_pcb *ping_pcb;
  88. #endif /* PING_USE_SOCKETS */
  89. /** Prepare a echo ICMP request */
  90. static void
  91. ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  92. {
  93. size_t i;
  94. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  95. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  96. ICMPH_CODE_SET(iecho, 0);
  97. iecho->chksum = 0;
  98. iecho->id = PING_ID;
  99. iecho->seqno = lwip_htons(++ping_seq_num);
  100. /* fill the additional data buffer with some data */
  101. for(i = 0; i < data_len; i++) {
  102. ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
  103. }
  104. iecho->chksum = inet_chksum(iecho, len);
  105. }
  106. #if PING_USE_SOCKETS
  107. /* Ping using the socket ip */
  108. static err_t
  109. ping_send(int s, const ip_addr_t *addr)
  110. {
  111. int err;
  112. struct icmp_echo_hdr *iecho;
  113. struct sockaddr_storage to;
  114. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  115. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  116. #if LWIP_IPV6
  117. if(IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) {
  118. /* todo: support ICMP6 echo */
  119. return ERR_VAL;
  120. }
  121. #endif /* LWIP_IPV6 */
  122. iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
  123. if (!iecho) {
  124. return ERR_MEM;
  125. }
  126. ping_prepare_echo(iecho, (u16_t)ping_size);
  127. #if LWIP_IPV4
  128. if(IP_IS_V4(addr)) {
  129. struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
  130. to4->sin_len = sizeof(*to4);
  131. to4->sin_family = AF_INET;
  132. inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
  133. }
  134. #endif /* LWIP_IPV4 */
  135. #if LWIP_IPV6
  136. if(IP_IS_V6(addr)) {
  137. struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
  138. to6->sin6_len = sizeof(*to6);
  139. to6->sin6_family = AF_INET6;
  140. inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
  141. }
  142. #endif /* LWIP_IPV6 */
  143. err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
  144. mem_free(iecho);
  145. return (err ? ERR_OK : ERR_VAL);
  146. }
  147. static void
  148. ping_recv(int s)
  149. {
  150. char buf[64];
  151. int len;
  152. struct sockaddr_storage from;
  153. int fromlen = sizeof(from);
  154. while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
  155. if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
  156. ip_addr_t fromaddr;
  157. memset(&fromaddr, 0, sizeof(fromaddr));
  158. #if LWIP_IPV4
  159. if(from.ss_family == AF_INET) {
  160. struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
  161. inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr);
  162. IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4);
  163. }
  164. #endif /* LWIP_IPV4 */
  165. #if LWIP_IPV6
  166. if(from.ss_family == AF_INET6) {
  167. struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
  168. inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr);
  169. IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6);
  170. }
  171. #endif /* LWIP_IPV6 */
  172. LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
  173. ip_addr_debug_print_val(PING_DEBUG, fromaddr);
  174. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
  175. /* todo: support ICMP6 echo */
  176. #if LWIP_IPV4
  177. if (IP_IS_V4_VAL(fromaddr)) {
  178. struct ip_hdr *iphdr;
  179. struct icmp_echo_hdr *iecho;
  180. iphdr = (struct ip_hdr *)buf;
  181. iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
  182. if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
  183. /* do some ping result processing */
  184. PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
  185. return;
  186. } else {
  187. LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
  188. }
  189. }
  190. #endif /* LWIP_IPV4 */
  191. }
  192. fromlen = sizeof(from);
  193. }
  194. if (len == 0) {
  195. LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
  196. }
  197. /* do some ping result processing */
  198. PING_RESULT(0);
  199. }
  200. static void
  201. ping_thread(void *arg)
  202. {
  203. int s;
  204. int ret;
  205. #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
  206. int timeout = PING_RCV_TIMEO;
  207. #else
  208. struct timeval timeout;
  209. timeout.tv_sec = PING_RCV_TIMEO/1000;
  210. timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000;
  211. #endif
  212. LWIP_UNUSED_ARG(arg);
  213. #if LWIP_IPV6
  214. if(IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) {
  215. s = lwip_socket(AF_INET6, SOCK_RAW, IP_PROTO_ICMP);
  216. } else {
  217. s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
  218. }
  219. #else
  220. s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
  221. #endif
  222. if (s < 0) {
  223. return;
  224. }
  225. ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  226. LWIP_ASSERT("setting receive timeout failed", ret == 0);
  227. LWIP_UNUSED_ARG(ret);
  228. while (ping_target != NULL) {
  229. if (ping_send(s, ping_target) == ERR_OK) {
  230. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  231. ip_addr_debug_print(PING_DEBUG, ping_target);
  232. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  233. #ifdef LWIP_DEBUG
  234. ping_time = sys_now();
  235. #endif /* LWIP_DEBUG */
  236. ping_recv(s);
  237. } else {
  238. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  239. ip_addr_debug_print(PING_DEBUG, ping_target);
  240. LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
  241. }
  242. sys_msleep(PING_DELAY);
  243. }
  244. lwip_close(s);
  245. }
  246. #else /* PING_USE_SOCKETS */
  247. /* Ping using the raw ip */
  248. static u8_t
  249. ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
  250. {
  251. struct icmp_echo_hdr *iecho;
  252. LWIP_UNUSED_ARG(arg);
  253. LWIP_UNUSED_ARG(pcb);
  254. LWIP_UNUSED_ARG(addr);
  255. LWIP_ASSERT("addr != NULL", addr != NULL);
  256. LWIP_ASSERT("p != NULL", p != NULL);
  257. if ((p->tot_len >= (IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
  258. pbuf_remove_header(p, IP_HLEN) == 0) {
  259. iecho = (struct icmp_echo_hdr *)p->payload;
  260. if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
  261. LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
  262. ip_addr_debug_print(PING_DEBUG, addr);
  263. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
  264. /* do some ping result processing */
  265. PING_RESULT(1);
  266. pbuf_free(p);
  267. return 1; /* eat the packet */
  268. }
  269. /* not eaten, restore original packet */
  270. pbuf_add_header(p, IP_HLEN);
  271. }
  272. return 0; /* don't eat the packet */
  273. }
  274. static void
  275. ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
  276. {
  277. struct pbuf *p;
  278. struct icmp_echo_hdr *iecho;
  279. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  280. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  281. ip_addr_debug_print(PING_DEBUG, addr);
  282. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  283. LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
  284. p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
  285. if (!p) {
  286. return;
  287. }
  288. if ((p->len == p->tot_len) && (p->next == NULL)) {
  289. iecho = (struct icmp_echo_hdr *)p->payload;
  290. ping_prepare_echo(iecho, (u16_t)ping_size);
  291. raw_sendto(raw, p, addr);
  292. #ifdef LWIP_DEBUG
  293. ping_time = sys_now();
  294. #endif /* LWIP_DEBUG */
  295. }
  296. pbuf_free(p);
  297. }
  298. static void
  299. ping_timeout(void *arg)
  300. {
  301. struct raw_pcb *pcb = (struct raw_pcb*)arg;
  302. LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
  303. ping_send(pcb, ping_target);
  304. sys_timeout(PING_DELAY, ping_timeout, pcb);
  305. }
  306. static void
  307. ping_raw_init(void)
  308. {
  309. ping_pcb = raw_new(IP_PROTO_ICMP);
  310. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  311. raw_recv(ping_pcb, ping_recv, NULL);
  312. raw_bind(ping_pcb, IP_ADDR_ANY);
  313. sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
  314. }
  315. void
  316. ping_send_now(void)
  317. {
  318. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  319. LWIP_ASSERT("ping_target != NULL", ping_target != NULL);
  320. ping_send(ping_pcb, ping_target);
  321. }
  322. static void
  323. ping_raw_stop(void)
  324. {
  325. sys_untimeout(ping_timeout, ping_pcb);
  326. if (ping_pcb != NULL) {
  327. raw_remove(ping_pcb);
  328. ping_pcb = NULL;
  329. }
  330. }
  331. #endif /* PING_USE_SOCKETS */
  332. /**
  333. * Initialize thread (socket mode) or timer (callback mode) to cyclically send pings
  334. * to a target.
  335. * Running ping is implicitly stopped.
  336. */
  337. void
  338. ping_init(const ip_addr_t* ping_addr)
  339. {
  340. ping_stop();
  341. LWIP_ASSERT("ping_addr != NULL", ping_addr != NULL);
  342. ping_target = ping_addr;
  343. #if PING_USE_SOCKETS
  344. sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  345. #else /* PING_USE_SOCKETS */
  346. ping_raw_init();
  347. #endif /* PING_USE_SOCKETS */
  348. }
  349. /**
  350. * Stop sending more pings.
  351. */
  352. void ping_stop(void)
  353. {
  354. #if !PING_USE_SOCKETS
  355. ping_raw_stop();
  356. #endif /* !PING_USE_SOCKETS */
  357. ping_target = NULL;
  358. }
  359. #endif /* LWIP_RAW */