ping.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * File : ping.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-04-01 Bernard first version
  23. * 2018-01-25 armink Add ping domain name
  24. */
  25. #include <rtthread.h>
  26. #ifdef PKG_NETUTILS_PING
  27. #include <lwip/opt.h>
  28. #include <lwip/init.h>
  29. #include <lwip/mem.h>
  30. #include <lwip/icmp.h>
  31. #include <lwip/netif.h>
  32. #include <lwip/sys.h>
  33. #include <lwip/inet.h>
  34. #include <lwip/inet_chksum.h>
  35. #include <lwip/ip.h>
  36. #include <lwip/netdb.h>
  37. #include <lwip/sockets.h>
  38. /**
  39. * PING_DEBUG: Enable debugging for PING.
  40. */
  41. #ifndef PING_DEBUG
  42. #define PING_DEBUG LWIP_DBG_ON
  43. #endif
  44. /** ping receive timeout - in milliseconds */
  45. #define PING_RCV_TIMEO (2*RT_TICK_PER_SECOND)
  46. /** ping delay - in milliseconds */
  47. #define PING_DELAY (1*RT_TICK_PER_SECOND)
  48. /** ping identifier - must fit on a u16_t */
  49. #ifndef PING_ID
  50. #define PING_ID 0xAFAF
  51. #endif
  52. /** ping additional data size to include in the packet */
  53. #ifndef PING_DATA_SIZE
  54. #define PING_DATA_SIZE 32
  55. #endif
  56. /* ping variables */
  57. static u16_t ping_seq_num;
  58. struct _ip_addr
  59. {
  60. rt_uint8_t addr0, addr1, addr2, addr3;
  61. };
  62. /** Prepare a echo ICMP request */
  63. static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  64. {
  65. size_t i;
  66. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  67. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  68. ICMPH_CODE_SET(iecho, 0);
  69. iecho->chksum = 0;
  70. iecho->id = PING_ID;
  71. iecho->seqno = htons(++ping_seq_num);
  72. /* fill the additional data buffer with some data */
  73. for (i = 0; i < data_len; i++)
  74. {
  75. ((char*) iecho)[sizeof(struct icmp_echo_hdr) + i] = (char) i;
  76. }
  77. #ifdef RT_LWIP_USING_HW_CHECKSUM
  78. iecho->chksum = 0;
  79. #else
  80. iecho->chksum = inet_chksum(iecho, len);
  81. #endif
  82. }
  83. /* Ping using the socket ip */
  84. static err_t ping_send(int s, ip_addr_t *addr, int size)
  85. {
  86. int err;
  87. struct icmp_echo_hdr *iecho;
  88. struct sockaddr_in to;
  89. int ping_size = sizeof(struct icmp_echo_hdr) + size;
  90. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  91. iecho = rt_malloc(ping_size);
  92. if (iecho == RT_NULL)
  93. {
  94. return ERR_MEM;
  95. }
  96. ping_prepare_echo(iecho, (u16_t) ping_size);
  97. to.sin_len = sizeof(to);
  98. to.sin_family = AF_INET;
  99. #if LWIP_IPV4 && LWIP_IPV6
  100. to.sin_addr.s_addr = addr->u_addr.ip4.addr;
  101. #elif LWIP_IPV4
  102. to.sin_addr.s_addr = addr->addr;
  103. #elif LWIP_IPV6
  104. #error Not supported IPv6.
  105. #endif
  106. err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
  107. rt_free(iecho);
  108. return (err == ping_size ? ERR_OK : ERR_VAL);
  109. }
  110. static int ping_recv(int s, int *ttl)
  111. {
  112. char buf[64];
  113. int fromlen = sizeof(struct sockaddr_in), len;
  114. struct sockaddr_in from;
  115. struct ip_hdr *iphdr;
  116. struct icmp_echo_hdr *iecho;
  117. while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
  118. {
  119. if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
  120. {
  121. iphdr = (struct ip_hdr *) buf;
  122. iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
  123. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
  124. {
  125. *ttl = iphdr->_ttl;
  126. return len;
  127. }
  128. }
  129. }
  130. return len;
  131. }
  132. rt_err_t ping(char* target_name, rt_uint32_t times, rt_size_t size)
  133. {
  134. #if LWIP_VERSION_MAJOR >= 2U
  135. struct timeval timeout = { PING_RCV_TIMEO / RT_TICK_PER_SECOND, PING_RCV_TIMEO % RT_TICK_PER_SECOND };
  136. #else
  137. int timeout = PING_RCV_TIMEO * 1000UL / RT_TICK_PER_SECOND;
  138. #endif
  139. int s, ttl, recv_len;
  140. ip_addr_t target_addr;
  141. rt_uint32_t send_times;
  142. rt_tick_t recv_start_tick;
  143. struct addrinfo hint, *res = NULL;
  144. struct sockaddr_in *h = NULL;
  145. struct in_addr ina;
  146. send_times = 0;
  147. ping_seq_num = 0;
  148. if (size == 0)
  149. {
  150. size = PING_DATA_SIZE;
  151. }
  152. memset(&hint, 0, sizeof(hint));
  153. /* convert URL to IP */
  154. if (lwip_getaddrinfo(target_name, NULL, &hint, &res) != 0)
  155. {
  156. rt_kprintf("ping: unknown host %s\n", target_name);
  157. return -RT_ERROR;
  158. }
  159. memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  160. memcpy(&ina, &h->sin_addr, sizeof(ina));
  161. lwip_freeaddrinfo(res);
  162. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  163. {
  164. rt_kprintf("ping: unknown host %s\n", target_name);
  165. return -RT_ERROR;
  166. }
  167. /* new a socket */
  168. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  169. {
  170. rt_kprintf("ping: create socket failed\n");
  171. return -RT_ERROR;
  172. }
  173. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  174. while (1)
  175. {
  176. int elapsed_time;
  177. if (ping_send(s, &target_addr, size) == ERR_OK)
  178. {
  179. recv_start_tick = rt_tick_get();
  180. if ((recv_len = ping_recv(s, &ttl)) >= 0)
  181. {
  182. elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
  183. rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), send_times,
  184. ttl, elapsed_time);
  185. }
  186. else
  187. {
  188. rt_kprintf("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), send_times);
  189. }
  190. }
  191. else
  192. {
  193. rt_kprintf("Send %s - error\n", inet_ntoa(ina));
  194. }
  195. send_times++;
  196. if (send_times >= times)
  197. {
  198. /* send ping times reached, stop */
  199. break;
  200. }
  201. rt_thread_delay(PING_DELAY); /* take a delay */
  202. }
  203. lwip_close(s);
  204. return RT_EOK;
  205. }
  206. #ifdef RT_USING_FINSH
  207. #include <finsh.h>
  208. FINSH_FUNCTION_EXPORT(ping, ping network host);
  209. int cmd_ping(int argc, char **argv)
  210. {
  211. if (argc == 1)
  212. {
  213. rt_kprintf("Please input: ping <host address>\n");
  214. }
  215. else
  216. {
  217. ping(argv[1], 4, 0);
  218. }
  219. return 0;
  220. }
  221. FINSH_FUNCTION_EXPORT_ALIAS(cmd_ping, __cmd_ping, ping network host);
  222. #endif
  223. #endif /* PKG_NETUTILS_PING */