ping.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #endif /* PING_USE_SOCKETS */
  52. #ifdef ESP_PING
  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_OFF
  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 struct timeval ping_time;
  89. #if ESP_PING
  90. static sys_sem_t ping_sem = NULL;
  91. static bool ping_init_flag = false;
  92. #endif
  93. #if !PING_USE_SOCKETS
  94. static struct raw_pcb *ping_pcb;
  95. #endif /* PING_USE_SOCKETS */
  96. #define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + (_end.tv_usec - _start.tv_usec)/1000))
  97. #define PING_TIME_DIFF_SEC(_end, _start) ((uint32_t)((_end).tv_sec - (_start).tv_sec))
  98. /** Prepare a echo ICMP request */
  99. static void
  100. ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  101. {
  102. size_t i;
  103. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  104. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  105. ICMPH_CODE_SET(iecho, 0);
  106. iecho->chksum = 0;
  107. iecho->id = PING_ID;
  108. iecho->seqno = htons(++ping_seq_num);
  109. /* fill the additional data buffer with some data */
  110. for(i = 0; i < data_len; i++) {
  111. ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
  112. }
  113. iecho->chksum = inet_chksum(iecho, len);
  114. }
  115. #if PING_USE_SOCKETS
  116. /* Ping using the socket ip */
  117. static err_t
  118. ping_send(int s, ip_addr_t *addr)
  119. {
  120. int err;
  121. struct icmp_echo_hdr *iecho;
  122. struct sockaddr_in to;
  123. size_t ping_size;
  124. #ifdef ESP_PING
  125. size_t ping_data_len = 0;
  126. esp_ping_get_target(PING_TARGET_DATA_LEN, &ping_data_len, sizeof(ping_data_len));
  127. if (ping_data_len > 0) {
  128. ping_size = sizeof(struct icmp_echo_hdr) + ping_data_len;
  129. } else {
  130. ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  131. }
  132. #else
  133. ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  134. #endif
  135. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  136. LWIP_ASSERT("ping: expect IPv4 address", !IP_IS_V6(addr));
  137. iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
  138. if (!iecho) {
  139. return ERR_MEM;
  140. }
  141. ping_prepare_echo(iecho, (u16_t)ping_size);
  142. to.sin_len = sizeof(to);
  143. to.sin_family = AF_INET;
  144. inet_addr_from_ip4addr(&to.sin_addr, ip_2_ip4(addr));
  145. err = sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
  146. mem_free(iecho);
  147. return (err ? ERR_OK : ERR_VAL);
  148. }
  149. static void
  150. ping_recv(int s)
  151. {
  152. char buf[64];
  153. int len;
  154. struct sockaddr_in from;
  155. struct ip_hdr *iphdr;
  156. struct icmp_echo_hdr *iecho;
  157. int fromlen = sizeof(from);
  158. struct timeval now;
  159. while((len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
  160. if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
  161. if (from.sin_family != AF_INET) {
  162. /* Ping is not IPv4 */
  163. LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n"));
  164. } else {
  165. ip4_addr_t fromaddr;
  166. inet_addr_to_ip4addr(&fromaddr, &from.sin_addr);
  167. iphdr = (struct ip_hdr *)buf;
  168. iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
  169. LWIP_DEBUGF( PING_DEBUG, ("ping: recv seq=%d ", ntohs(iecho->seqno)));
  170. ip4_addr_debug_print(PING_DEBUG, &fromaddr);
  171. gettimeofday(&now, NULL);
  172. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", PING_TIME_DIFF_MS(now, ping_time)));
  173. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
  174. /* do some ping result processing */
  175. #ifdef ESP_PING
  176. esp_ping_result((ICMPH_TYPE(iecho) == ICMP_ER), len, PING_TIME_DIFF_MS(now, ping_time));
  177. #else
  178. PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
  179. #endif
  180. return;
  181. } else {
  182. LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
  183. }
  184. }
  185. }
  186. fromlen = sizeof(from);
  187. }
  188. gettimeofday(&now, NULL);
  189. if (len == 0) {
  190. LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", PING_TIME_DIFF_MS(now, ping_time)));
  191. }
  192. /* do some ping result processing */
  193. #ifdef ESP_PING
  194. esp_ping_result(0, len, PING_TIME_DIFF_MS(now, ping_time));
  195. #else
  196. PING_RESULT(0);
  197. #endif
  198. }
  199. static void
  200. ping_thread(void *arg)
  201. {
  202. uint32_t ping_timeout = PING_RCV_TIMEO;
  203. uint32_t ping_delay = PING_DELAY;
  204. ip_addr_t ping_target;
  205. int ret;
  206. int s;
  207. #ifdef ESP_PING
  208. uint32_t ping_count_cur = 0;
  209. uint32_t ping_count_max = 3;
  210. ip4_addr_t ipaddr;
  211. int lev;
  212. esp_ping_get_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count_max, sizeof(ping_count_max));
  213. esp_ping_get_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(ping_timeout));
  214. esp_ping_get_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(ping_delay));
  215. esp_ping_get_target(PING_TARGET_IP_ADDRESS, &ipaddr.addr, sizeof(uint32_t));
  216. ip_addr_copy_from_ip4(ping_target, ipaddr);
  217. #else
  218. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  219. #endif
  220. #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
  221. int timeout = ping_timeout;
  222. #else
  223. struct timeval timeout;
  224. timeout.tv_sec = ping_timeout/1000;
  225. timeout.tv_usec = (ping_timeout%1000)*1000;
  226. #endif
  227. LWIP_UNUSED_ARG(arg);
  228. if ((s = socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
  229. goto _exit_new_socket_failed;
  230. }
  231. ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  232. LWIP_ASSERT("setting receive timeout failed", ret == 0);
  233. LWIP_UNUSED_ARG(ret);
  234. #ifdef ESP_PING
  235. int tos = 0;
  236. esp_ping_get_target(PING_TARGET_IP_TOS, &tos, sizeof(int));
  237. if (tos > 0) {
  238. tos <<= 5;
  239. ret = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
  240. LWIP_ASSERT("setting IP_TOS failed", ret == 0);
  241. LWIP_UNUSED_ARG(ret);
  242. }
  243. #endif
  244. while (1) {
  245. #ifdef ESP_PING
  246. if (ping_count_cur++ >= ping_count_max) {
  247. goto _exit;
  248. }
  249. if (ping_init_flag == false) {
  250. goto _exit;
  251. }
  252. #endif
  253. if (ping_send(s, &ping_target) == ERR_OK) {
  254. LWIP_DEBUGF( PING_DEBUG, ("ping: send seq=%d ", ping_seq_num));
  255. ip_addr_debug_print(PING_DEBUG, &ping_target);
  256. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  257. gettimeofday(&ping_time, NULL);
  258. ping_recv(s);
  259. } else {
  260. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  261. ip_addr_debug_print(PING_DEBUG, &ping_target);
  262. LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
  263. }
  264. sys_msleep(ping_delay);
  265. }
  266. #ifdef ESP_PING
  267. _exit:
  268. close(s);
  269. _exit_new_socket_failed:
  270. esp_ping_result(PING_RES_FINISH, 0, 0);
  271. SYS_ARCH_PROTECT(lev);
  272. if (ping_init_flag) { /* Ping closed by this thread */
  273. LWIP_DEBUGF( PING_DEBUG, ("ping: closed by self "));
  274. if (ping_sem) {
  275. sys_sem_free(&ping_sem);
  276. }
  277. ping_sem = NULL;
  278. ping_init_flag = false;
  279. SYS_ARCH_UNPROTECT(lev);
  280. } else { /* Ping closed by task calls ping_deinit */
  281. LWIP_DEBUGF( PING_DEBUG, ("ping: closed by other"));
  282. SYS_ARCH_UNPROTECT(lev);
  283. if (ping_sem) {
  284. sys_sem_signal(&ping_sem);
  285. }
  286. }
  287. vTaskDelete(NULL);
  288. #endif
  289. }
  290. #else /* PING_USE_SOCKETS */
  291. /* Ping using the raw ip */
  292. static u8_t
  293. ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
  294. {
  295. struct icmp_echo_hdr *iecho;
  296. struct timeval now;
  297. LWIP_UNUSED_ARG(arg);
  298. LWIP_UNUSED_ARG(pcb);
  299. LWIP_UNUSED_ARG(addr);
  300. LWIP_ASSERT("p != NULL", p != NULL);
  301. if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
  302. pbuf_header(p, -PBUF_IP_HLEN) == 0) {
  303. iecho = (struct icmp_echo_hdr *)p->payload;
  304. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
  305. LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
  306. ip_addr_debug_print(PING_DEBUG, addr);
  307. gettimeofday(&now, NULL);
  308. LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", PING_TIME_DIFF_MS(now, ping_time)));
  309. /* do some ping result processing */
  310. PING_RESULT(1);
  311. pbuf_free(p);
  312. return 1; /* eat the packet */
  313. }
  314. /* not eaten, restore original packet */
  315. pbuf_header(p, PBUF_IP_HLEN);
  316. }
  317. return 0; /* don't eat the packet */
  318. }
  319. static void
  320. ping_send(struct raw_pcb *raw, ip_addr_t *addr)
  321. {
  322. struct pbuf *p;
  323. struct icmp_echo_hdr *iecho;
  324. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  325. LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
  326. ip_addr_debug_print(PING_DEBUG, addr);
  327. LWIP_DEBUGF( PING_DEBUG, ("\n"));
  328. LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
  329. p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
  330. if (!p) {
  331. return;
  332. }
  333. if ((p->len == p->tot_len) && (p->next == NULL)) {
  334. iecho = (struct icmp_echo_hdr *)p->payload;
  335. ping_prepare_echo(iecho, (u16_t)ping_size);
  336. raw_sendto(raw, p, addr);
  337. ping_time = system_get_time();
  338. }
  339. pbuf_free(p);
  340. }
  341. static void
  342. ping_timeout(void *arg)
  343. {
  344. struct raw_pcb *pcb = (struct raw_pcb*)arg;
  345. ip_addr_t ping_target;
  346. LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
  347. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  348. ping_send(pcb, &ping_target);
  349. sys_timeout(PING_DELAY, ping_timeout, pcb);
  350. }
  351. static void
  352. ping_raw_init(void)
  353. {
  354. ping_pcb = raw_new(IP_PROTO_ICMP);
  355. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  356. raw_recv(ping_pcb, ping_recv, NULL);
  357. raw_bind(ping_pcb, IP_ADDR_ANY);
  358. sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
  359. }
  360. void
  361. ping_send_now(void)
  362. {
  363. ip_addr_t ping_target;
  364. LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
  365. ip_addr_copy_from_ip4(ping_target, PING_TARGET);
  366. ping_send(ping_pcb, &ping_target);
  367. }
  368. #endif /* PING_USE_SOCKETS */
  369. int
  370. ping_init(void)
  371. {
  372. int ret;
  373. int lev;
  374. SYS_ARCH_PROTECT(lev);
  375. if (ping_init_flag) {
  376. SYS_ARCH_UNPROTECT(lev);
  377. /* Currently we only support one ping, call ping_deinit to kill the running ping before start new ping */
  378. return ERR_INPROGRESS;
  379. }
  380. ret = sys_sem_new(&ping_sem, 0);
  381. if (ERR_OK != ret) {
  382. SYS_ARCH_UNPROTECT(lev);
  383. return ERR_MEM;
  384. }
  385. ping_init_flag = true;
  386. SYS_ARCH_UNPROTECT(lev);
  387. #if PING_USE_SOCKETS
  388. sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  389. #else /* PING_USE_SOCKETS */
  390. ping_raw_init();
  391. #endif /* PING_USE_SOCKETS */
  392. return ERR_OK;
  393. }
  394. void
  395. ping_deinit(void)
  396. {
  397. int lev;
  398. SYS_ARCH_PROTECT(lev);
  399. if (ping_init_flag == false) {
  400. SYS_ARCH_UNPROTECT(lev);
  401. return;
  402. }
  403. ping_init_flag = false;
  404. SYS_ARCH_UNPROTECT(lev);
  405. if (ping_sem) {
  406. sys_sem_wait(&ping_sem);
  407. SYS_ARCH_PROTECT(lev);
  408. sys_sem_free(&ping_sem);
  409. ping_sem = NULL;
  410. SYS_ARCH_UNPROTECT(lev);
  411. }
  412. }
  413. #endif /* LWIP_IPV4 && LWIP_RAW */