ping.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * netutils: ping implementation
  3. */
  4. #include "lwip/opt.h"
  5. #include "lwip/mem.h"
  6. #include "lwip/icmp.h"
  7. #include "lwip/netif.h"
  8. #include "lwip/sys.h"
  9. #include "lwip/sockets.h"
  10. #include "lwip/inet.h"
  11. #include "lwip/inet_chksum.h"
  12. #include "lwip/ip.h"
  13. /**
  14. * PING_DEBUG: Enable debugging for PING.
  15. */
  16. #ifndef PING_DEBUG
  17. #define PING_DEBUG LWIP_DBG_ON
  18. #endif
  19. /** ping receive timeout - in milliseconds */
  20. #define PING_RCV_TIMEO 1000
  21. /** ping delay - in milliseconds */
  22. #define PING_DELAY 100
  23. /** ping identifier - must fit on a u16_t */
  24. #ifndef PING_ID
  25. #define PING_ID 0xAFAF
  26. #endif
  27. /** ping additional data size to include in the packet */
  28. #ifndef PING_DATA_SIZE
  29. #define PING_DATA_SIZE 32
  30. #endif
  31. /* ping variables */
  32. static u16_t ping_seq_num;
  33. struct _ip_addr
  34. {
  35. rt_uint8_t addr0, addr1, addr2, addr3;
  36. };
  37. /** Prepare a echo ICMP request */
  38. static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
  39. {
  40. size_t i;
  41. size_t data_len = len - sizeof(struct icmp_echo_hdr);
  42. ICMPH_TYPE_SET(iecho, ICMP_ECHO);
  43. ICMPH_CODE_SET(iecho, 0);
  44. iecho->chksum = 0;
  45. iecho->id = PING_ID;
  46. iecho->seqno = htons(++ping_seq_num);
  47. /* fill the additional data buffer with some data */
  48. for(i = 0; i < data_len; i++)
  49. {
  50. ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
  51. }
  52. iecho->chksum = inet_chksum(iecho, len);
  53. }
  54. /* Ping using the socket ip */
  55. static err_t ping_send(int s, struct ip_addr *addr)
  56. {
  57. int err;
  58. struct icmp_echo_hdr *iecho;
  59. struct sockaddr_in to;
  60. size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
  61. LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
  62. iecho = rt_malloc(ping_size);
  63. if (iecho == RT_NULL)
  64. {
  65. return ERR_MEM;
  66. }
  67. ping_prepare_echo(iecho, (u16_t)ping_size);
  68. to.sin_len = sizeof(to);
  69. to.sin_family = AF_INET;
  70. to.sin_addr.s_addr = addr->addr;
  71. err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
  72. rt_free(iecho);
  73. return (err ? ERR_OK : ERR_VAL);
  74. }
  75. static void ping_recv(int s)
  76. {
  77. char buf[64];
  78. int fromlen, len;
  79. struct sockaddr_in from;
  80. struct ip_hdr *iphdr;
  81. struct icmp_echo_hdr *iecho;
  82. struct _ip_addr *addr;
  83. while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0)
  84. {
  85. if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr)))
  86. {
  87. addr = (struct _ip_addr *)&(from.sin_addr);
  88. rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  89. iphdr = (struct ip_hdr *)buf;
  90. iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4));
  91. if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
  92. {
  93. return;
  94. }
  95. else
  96. {
  97. rt_kprintf("ping: drop\n");
  98. }
  99. }
  100. }
  101. if (len <= 0)
  102. {
  103. rt_kprintf("ping: timeout\n");
  104. }
  105. }
  106. rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size)
  107. {
  108. int s;
  109. int timeout = PING_RCV_TIMEO;
  110. struct ip_addr ping_target;
  111. rt_uint32_t send_time;
  112. struct _ip_addr
  113. {
  114. rt_uint8_t addr0, addr1, addr2, addr3;
  115. } *addr;
  116. send_time = 0;
  117. if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR;
  118. addr = (struct _ip_addr*)&ping_target;
  119. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  120. {
  121. rt_kprintf("create socket failled\n");
  122. return -RT_ERROR;
  123. }
  124. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  125. while (1)
  126. {
  127. if (ping_send(s, &ping_target) == ERR_OK)
  128. {
  129. rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  130. ping_recv(s);
  131. }
  132. else
  133. {
  134. rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
  135. }
  136. send_time ++;
  137. if (send_time >= time) break; /* send ping times reached, stop */
  138. rt_thread_delay(PING_DELAY); /* take a delay */
  139. }
  140. return RT_EOK;
  141. }
  142. #ifdef RT_USING_FINSH
  143. #include <finsh.h>
  144. FINSH_FUNCTION_EXPORT(ping, ping network host);
  145. #endif