ping.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <lwip/opt.h>
  26. #include <lwip/mem.h>
  27. #include <lwip/icmp.h>
  28. #include <lwip/netif.h>
  29. #include <lwip/sys.h>
  30. #include <lwip/inet.h>
  31. #include <lwip/inet_chksum.h>
  32. #include <lwip/ip.h>
  33. #include <lwip/netdb.h>
  34. #include <lwip/sockets.h>
  35. /**
  36. * PING_DEBUG: Enable debugging for PING.
  37. */
  38. #ifndef PING_DEBUG
  39. #define PING_DEBUG LWIP_DBG_ON
  40. #endif
  41. /** ping receive timeout - in milliseconds */
  42. #define PING_RCV_TIMEO rt_tick_from_millisecond(2000)
  43. /** ping delay - in milliseconds */
  44. #define PING_DELAY rt_tick_from_millisecond(1000)
  45. /** ping identifier - must fit on a u16_t */
  46. #ifndef PING_ID
  47. #define PING_ID 0xAFAF
  48. #endif
  49. /** ping additional data size to include in the packet */
  50. #ifndef PING_DATA_SIZE
  51. #define PING_DATA_SIZE 32
  52. #endif
  53. /* ping variables */
  54. static u16_t ping_seq_num;
  55. struct _ip_addr
  56. {
  57. rt_uint8_t addr0, addr1, addr2, addr3;
  58. };
  59. /** Prepare a echo ICMP request */
  60. static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  61. {
  62. size_t i;
  63. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  64. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  65. ICMPH_CODE_SET(iecho, 0);
  66. iecho->chksum = 0;
  67. iecho->id = PING_ID;
  68. iecho->seqno = htons(++ping_seq_num);
  69. /* fill the additional data buffer with some data */
  70. for (i = 0; i < data_len; i++)
  71. {
  72. ((char*) iecho)[sizeof(struct icmp_echo_hdr) + i] = (char) i;
  73. }
  74. iecho->chksum = inet_chksum(iecho, len);
  75. }
  76. /* Ping using the socket ip */
  77. static err_t ping_send(int s, ip_addr_t *addr, int size)
  78. {
  79. int err;
  80. struct icmp_echo_hdr *iecho;
  81. struct sockaddr_in to;
  82. int ping_size = sizeof(struct icmp_echo_hdr) + size;
  83. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  84. iecho = rt_malloc(ping_size);
  85. if (iecho == RT_NULL)
  86. {
  87. return ERR_MEM;
  88. }
  89. ping_prepare_echo(iecho, (u16_t) ping_size);
  90. to.sin_len = sizeof(to);
  91. to.sin_family = AF_INET;
  92. to.sin_addr.s_addr = addr->addr;
  93. err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
  94. rt_free(iecho);
  95. return (err == ping_size ? ERR_OK : ERR_VAL);
  96. }
  97. static int ping_recv(int s, int *ttl)
  98. {
  99. char buf[64];
  100. int fromlen = sizeof(struct sockaddr_in), len;
  101. struct sockaddr_in from;
  102. struct ip_hdr *iphdr;
  103. struct icmp_echo_hdr *iecho;
  104. while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
  105. {
  106. if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
  107. {
  108. iphdr = (struct ip_hdr *) buf;
  109. iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
  110. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
  111. {
  112. *ttl = iphdr->_ttl;
  113. return len;
  114. }
  115. }
  116. }
  117. return len;
  118. }
  119. rt_err_t ping(char* target_name, rt_uint32_t times, rt_size_t size)
  120. {
  121. int s, ttl, recv_len;
  122. struct timeval timeout = { PING_RCV_TIMEO / RT_TICK_PER_SECOND, PING_RCV_TIMEO % RT_TICK_PER_SECOND };
  123. ip_addr_t target_addr;
  124. rt_uint32_t send_times;
  125. rt_tick_t recv_start_tick;
  126. struct addrinfo hint, *res = NULL;
  127. struct sockaddr_in *h = NULL;
  128. struct in_addr ina;
  129. send_times = 0;
  130. ping_seq_num = 0;
  131. if (size == 0)
  132. {
  133. size = PING_DATA_SIZE;
  134. }
  135. memset(&hint, 0, sizeof(hint));
  136. /* convert URL to IP */
  137. if (lwip_getaddrinfo(target_name, NULL, &hint, &res) != 0)
  138. {
  139. rt_kprintf("ping: unknown host %s\n", target_name);
  140. return -RT_ERROR;
  141. }
  142. memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  143. memcpy(&ina, &h->sin_addr, sizeof(ina));
  144. lwip_freeaddrinfo(res);
  145. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  146. {
  147. rt_kprintf("ping: unknown host %s\n", target_name);
  148. return -RT_ERROR;
  149. }
  150. /* new a socket */
  151. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  152. {
  153. rt_kprintf("ping: create socket failed\n");
  154. return -RT_ERROR;
  155. }
  156. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval));
  157. while (1)
  158. {
  159. if (ping_send(s, &target_addr, size) == ERR_OK)
  160. {
  161. recv_start_tick = rt_tick_get();
  162. if ((recv_len = ping_recv(s, &ttl)) >= 0)
  163. {
  164. rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ticks\n", recv_len, inet_ntoa(ina), send_times,
  165. ttl, rt_tick_get() - recv_start_tick);
  166. }
  167. else
  168. {
  169. rt_kprintf("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), send_times);
  170. }
  171. }
  172. else
  173. {
  174. rt_kprintf("Send %s - error\n", inet_ntoa(ina));
  175. }
  176. send_times++;
  177. if (send_times >= times)
  178. {
  179. /* send ping times reached, stop */
  180. break;
  181. }
  182. rt_thread_delay(PING_DELAY); /* take a delay */
  183. }
  184. lwip_close(s);
  185. return RT_EOK;
  186. }
  187. #ifdef RT_USING_FINSH
  188. #include <finsh.h>
  189. FINSH_FUNCTION_EXPORT(ping, ping network host);
  190. int cmd_ping(int argc, char **argv)
  191. {
  192. if (argc == 1)
  193. {
  194. rt_kprintf("Please input: ping <host address>\n");
  195. }
  196. else
  197. {
  198. ping(argv[1], 4, 0);
  199. }
  200. return 0;
  201. }
  202. FINSH_FUNCTION_EXPORT_ALIAS(cmd_ping, __cmd_ping, ping network host);
  203. #endif