1 /*
2  * netutils: ping implementation
3  */
4 
5 #include <rtthread.h>
6 
7 #ifdef RT_LWIP_ICMP    /* don't build if not configured for use in rtconfig.h */
8 #include <lwip/opt.h>
9 #include <lwip/init.h>
10 #include <lwip/mem.h>
11 #include <lwip/icmp.h>
12 #include <lwip/netif.h>
13 #include <lwip/sys.h>
14 #include <lwip/inet.h>
15 #include <lwip/inet_chksum.h>
16 #include <lwip/ip.h>
17 #include <lwip/netdb.h>
18 #include <lwip/sockets.h>
19 
20 /**
21  * PING_DEBUG: Enable debugging for PING.
22  */
23 #ifndef PING_DEBUG
24 #define PING_DEBUG     LWIP_DBG_ON
25 #endif
26 
27 /** ping receive timeout - in milliseconds */
28 #define PING_RCV_TIMEO (2 * RT_TICK_PER_SECOND)
29 /** ping delay - in milliseconds */
30 #define PING_DELAY     (1 * RT_TICK_PER_SECOND)
31 
32 /** ping identifier - must fit on a u16_t */
33 #ifndef PING_ID
34 #define PING_ID        0xAFAF
35 #endif
36 
37 /** ping additional data size to include in the packet */
38 #ifndef PING_DATA_SIZE
39 #define PING_DATA_SIZE 32
40 #endif
41 
42 /* ping variables */
43 static u16_t ping_seq_num;
44 struct _ip_addr
45 {
46     rt_uint8_t addr0, addr1, addr2, addr3;
47 };
48 
49 /** Prepare a echo ICMP request */
ping_prepare_echo(struct icmp_echo_hdr * iecho,u16_t len)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 
55     ICMPH_TYPE_SET(iecho, ICMP_ECHO);
56     ICMPH_CODE_SET(iecho, 0);
57     iecho->chksum = 0;
58     iecho->id     = PING_ID;
59     iecho->seqno  = htons(++ping_seq_num);
60 
61     /* fill the additional data buffer with some data */
62     for (i = 0; i < data_len; i++)
63     {
64         ((char*) iecho)[sizeof(struct icmp_echo_hdr) + i] = (char) i;
65     }
66 
67 #ifdef RT_LWIP_USING_HW_CHECKSUM
68       iecho->chksum = 0;
69 #else
70       iecho->chksum = inet_chksum(iecho, len);
71 #endif
72 
73 }
74 
75 /* Ping using the socket ip */
lwip_ping_send(int s,ip_addr_t * addr,int size)76 err_t lwip_ping_send(int s, ip_addr_t *addr, int size)
77 {
78     int err;
79     struct icmp_echo_hdr *iecho;
80     struct sockaddr_in to;
81     int ping_size = sizeof(struct icmp_echo_hdr) + size;
82     LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
83 
84     iecho = rt_malloc(ping_size);
85     if (iecho == RT_NULL)
86     {
87         return ERR_MEM;
88     }
89 
90     ping_prepare_echo(iecho, (u16_t) ping_size);
91 
92     to.sin_len = sizeof(to);
93     to.sin_family = AF_INET;
94 #if LWIP_IPV4 && LWIP_IPV6
95     to.sin_addr.s_addr = addr->u_addr.ip4.addr;
96 #elif LWIP_IPV4
97     to.sin_addr.s_addr = addr->addr;
98 #elif LWIP_IPV6
99 #error Not supported IPv6.
100 #endif
101 
102     err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*) &to, sizeof(to));
103     rt_free(iecho);
104 
105     return (err == ping_size ? ERR_OK : ERR_VAL);
106 }
107 
lwip_ping_recv(int s,int * ttl)108 int lwip_ping_recv(int s, int *ttl)
109 {
110     char buf[64];
111     int fromlen = sizeof(struct sockaddr_in), len;
112     struct sockaddr_in from;
113     struct ip_hdr *iphdr;
114     struct icmp_echo_hdr *iecho;
115 
116     while ((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*) &from, (socklen_t*) &fromlen)) > 0)
117     {
118         if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr)))
119         {
120             iphdr = (struct ip_hdr *) buf;
121             iecho = (struct icmp_echo_hdr *) (buf + (IPH_HL(iphdr) * 4));
122             if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
123             {
124                 *ttl = iphdr->_ttl;
125                 return len;
126             }
127         }
128     }
129 
130     return len;
131 }
132 
133 #ifndef RT_USING_NETDEV
134 
135 /* using the lwIP custom ping */
ping(char * target_name,rt_uint32_t times,rt_size_t size)136 rt_err_t ping(char* target_name, rt_uint32_t times, rt_size_t size)
137 {
138 #if LWIP_VERSION_MAJOR >= 2U
139     struct timeval timeout = { PING_RCV_TIMEO / RT_TICK_PER_SECOND, PING_RCV_TIMEO % RT_TICK_PER_SECOND };
140 #else
141     int timeout = PING_RCV_TIMEO * 1000UL / RT_TICK_PER_SECOND;
142 #endif
143 
144     int s, ttl, recv_len;
145     ip_addr_t target_addr;
146     rt_uint32_t send_times;
147     rt_tick_t recv_start_tick;
148     struct addrinfo hint, *res = NULL;
149     struct sockaddr_in *h = NULL;
150     struct in_addr ina;
151 
152     send_times = 0;
153     ping_seq_num = 0;
154 
155     if (size == 0)
156     {
157         size = PING_DATA_SIZE;
158     }
159 
160     rt_memset(&hint, 0, sizeof(hint));
161     /* convert URL to IP */
162     if (lwip_getaddrinfo(target_name, NULL, &hint, &res) != 0)
163     {
164         rt_kprintf("ping: unknown host %s\n", target_name);
165         return -RT_ERROR;
166     }
167     rt_memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
168     rt_memcpy(&ina, &h->sin_addr, sizeof(ina));
169     lwip_freeaddrinfo(res);
170     if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
171     {
172         rt_kprintf("ping: unknown host %s\n", target_name);
173         return -RT_ERROR;
174     }
175     /* new a socket */
176     if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
177     {
178         rt_kprintf("ping: create socket failed\n");
179         return -RT_ERROR;
180     }
181 
182     lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
183 
184     while (1)
185     {
186         int elapsed_time;
187 
188         if (lwip_ping_send(s, &target_addr, size) == ERR_OK)
189         {
190             recv_start_tick = rt_tick_get();
191             if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
192             {
193                 elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
194                 rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n", recv_len, inet_ntoa(ina), send_times,
195                         ttl, elapsed_time);
196             }
197             else
198             {
199                 rt_kprintf("From %s icmp_seq=%d timeout\n", inet_ntoa(ina), send_times);
200             }
201         }
202         else
203         {
204             rt_kprintf("Send %s - error\n", inet_ntoa(ina));
205         }
206 
207         send_times++;
208         if (send_times >= times)
209         {
210             /* send ping times reached, stop */
211             break;
212         }
213 
214         rt_thread_delay(PING_DELAY); /* take a delay */
215     }
216 
217     lwip_close(s);
218 
219     return RT_EOK;
220 }
221 #ifdef RT_USING_FINSH
222 #include <finsh.h>
223 
224 FINSH_FUNCTION_EXPORT(ping, ping network host);
225 
cmd_ping(int argc,char ** argv)226 int cmd_ping(int argc, char **argv)
227 {
228     if (argc == 1)
229     {
230         rt_kprintf("Please input: ping <host address>\n");
231     }
232     else
233     {
234         ping(argv[1], 4, 0);
235     }
236 
237     return 0;
238 }
239 MSH_CMD_EXPORT_ALIAS(cmd_ping, ping, ping network host);
240 #endif /* RT_USING_FINSH */
241 
242 #endif /* RT_USING_NETDEV */
243 
244 #endif /* RT_LWIP_ICMP */
245 
246