ping.c 4.5 KB

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