ping.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. iecho->chksum = inet_chksum(iecho, len);
  78. }
  79. /* Ping using the socket ip */
  80. static err_t ping_send(int s, ip_addr_t *addr, int size)
  81. {
  82. int err;
  83. struct icmp_echo_hdr *iecho;
  84. struct sockaddr_in to;
  85. int ping_size = sizeof(struct icmp_echo_hdr) + size;
  86. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  87. iecho = rt_malloc(ping_size);
  88. if (iecho == RT_NULL)
  89. {
  90. return ERR_MEM;
  91. }
  92. ping_prepare_echo(iecho, (u16_t) ping_size);
  93. to.sin_len = sizeof(to);
  94. to.sin_family = AF_INET;
  95. #if LWIP_IPV4 && LWIP_IPV6
  96. to.sin_addr.s_addr = addr->u_addr.ip4.addr;
  97. #elif LWIP_IPV4
  98. to.sin_addr.s_addr = addr->addr;
  99. #elif LWIP_IPV6
  100. #error Not supported IPv6.
  101. #endif
  102. err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
  103. rt_free(iecho);
  104. return (err == ping_size ? ERR_OK : ERR_VAL);
  105. }
  106. static int ping_recv(int s, int *ttl)
  107. {
  108. char buf[64];
  109. int fromlen = sizeof(struct sockaddr_in), len;
  110. struct sockaddr_in from;
  111. struct ip_hdr *iphdr;
  112. struct icmp_echo_hdr *iecho;
  113. while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
  114. {
  115. if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
  116. {
  117. iphdr = (struct ip_hdr *) buf;
  118. iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
  119. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
  120. {
  121. *ttl = iphdr->_ttl;
  122. return len;
  123. }
  124. }
  125. }
  126. return len;
  127. }
  128. rt_err_t ping(char* target_name, rt_uint32_t times, rt_size_t size)
  129. {
  130. #if LWIP_VERSION_MAJOR >= 2U
  131. struct timeval timeout = { PING_RCV_TIMEO / RT_TICK_PER_SECOND, PING_RCV_TIMEO % RT_TICK_PER_SECOND };
  132. #else
  133. int timeout = PING_RCV_TIMEO * 1000UL / RT_TICK_PER_SECOND;
  134. #endif
  135. int s, ttl, recv_len;
  136. ip_addr_t target_addr;
  137. rt_uint32_t send_times;
  138. rt_tick_t recv_start_tick;
  139. struct addrinfo hint, *res = NULL;
  140. struct sockaddr_in *h = NULL;
  141. struct in_addr ina;
  142. send_times = 0;
  143. ping_seq_num = 0;
  144. if (size == 0)
  145. {
  146. size = PING_DATA_SIZE;
  147. }
  148. memset(&hint, 0, sizeof(hint));
  149. /* convert URL to IP */
  150. if (lwip_getaddrinfo(target_name, NULL, &hint, &res) != 0)
  151. {
  152. rt_kprintf("ping: unknown host %s\n", target_name);
  153. return -RT_ERROR;
  154. }
  155. memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  156. memcpy(&ina, &h->sin_addr, sizeof(ina));
  157. lwip_freeaddrinfo(res);
  158. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  159. {
  160. rt_kprintf("ping: unknown host %s\n", target_name);
  161. return -RT_ERROR;
  162. }
  163. /* new a socket */
  164. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  165. {
  166. rt_kprintf("ping: create socket failed\n");
  167. return -RT_ERROR;
  168. }
  169. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  170. while (1)
  171. {
  172. if (ping_send(s, &target_addr, size) == ERR_OK)
  173. {
  174. recv_start_tick = rt_tick_get();
  175. if ((recv_len = ping_recv(s, &ttl)) >= 0)
  176. {
  177. rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ticks\n", recv_len, inet_ntoa(ina), send_times,
  178. ttl, rt_tick_get() - recv_start_tick);
  179. }
  180. else
  181. {
  182. rt_kprintf("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), send_times);
  183. }
  184. }
  185. else
  186. {
  187. rt_kprintf("Send %s - error\n", inet_ntoa(ina));
  188. }
  189. send_times++;
  190. if (send_times >= times)
  191. {
  192. /* send ping times reached, stop */
  193. break;
  194. }
  195. rt_thread_delay(PING_DELAY); /* take a delay */
  196. }
  197. lwip_close(s);
  198. return RT_EOK;
  199. }
  200. #ifdef RT_USING_FINSH
  201. #include <finsh.h>
  202. FINSH_FUNCTION_EXPORT(ping, ping network host);
  203. int cmd_ping(int argc, char **argv)
  204. {
  205. if (argc == 1)
  206. {
  207. rt_kprintf("Please input: ping <host address>\n");
  208. }
  209. else
  210. {
  211. ping(argv[1], 4, 0);
  212. }
  213. return 0;
  214. }
  215. FINSH_FUNCTION_EXPORT_ALIAS(cmd_ping, __cmd_ping, ping network host);
  216. #endif
  217. #endif /* PKG_NETUTILS_PING */