source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/contrib-2.1.0/apps/ping/ping.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 11.1 KB
Line 
1/**
2 * @file
3 * Ping sender module
4 *
5 */
6
7/*
8 * Redistribution and use in source and binary forms, with or without modification,
9 * are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
22 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
28 * OF SUCH DAMAGE.
29 *
30 * This file is part of the lwIP TCP/IP stack.
31 *
32 */
33
34/**
35 * This is an example of a "ping" sender (with raw API and socket API).
36 * It can be used as a start point to maintain opened a network connection, or
37 * like a network "watchdog" for your device.
38 *
39 */
40
41#include "lwip/opt.h"
42
43#if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
44
45#include "ping.h"
46
47#include "lwip/mem.h"
48#include "lwip/raw.h"
49#include "lwip/icmp.h"
50#include "lwip/netif.h"
51#include "lwip/sys.h"
52#include "lwip/timeouts.h"
53#include "lwip/inet_chksum.h"
54#include "lwip/prot/ip4.h"
55
56#if PING_USE_SOCKETS
57#include "lwip/sockets.h"
58#include "lwip/inet.h"
59#include <string.h>
60#endif /* PING_USE_SOCKETS */
61
62
63/**
64 * PING_DEBUG: Enable debugging for PING.
65 */
66#ifndef PING_DEBUG
67#define PING_DEBUG LWIP_DBG_ON
68#endif
69
70/** ping receive timeout - in milliseconds */
71#ifndef PING_RCV_TIMEO
72#define PING_RCV_TIMEO 1000
73#endif
74
75/** ping delay - in milliseconds */
76#ifndef PING_DELAY
77#define PING_DELAY 1000
78#endif
79
80/** ping identifier - must fit on a u16_t */
81#ifndef PING_ID
82#define PING_ID 0xAFAF
83#endif
84
85/** ping additional data size to include in the packet */
86#ifndef PING_DATA_SIZE
87#define PING_DATA_SIZE 32
88#endif
89
90/** ping result action - no default action */
91#ifndef PING_RESULT
92#define PING_RESULT(ping_ok)
93#endif
94
95/* ping variables */
96static const ip_addr_t* ping_target;
97static u16_t ping_seq_num;
98#if defined(LWIP_DEBUG) || defined(USE_PINGSEND)
99static u32_t ping_time;
100#endif /* LWIP_DEBUG */
101#if !PING_USE_SOCKETS
102static struct raw_pcb *ping_pcb;
103#endif /* PING_USE_SOCKETS */
104
105/** Prepare a echo ICMP request */
106static void
107ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
108{
109 size_t i;
110 size_t data_len = len - sizeof(struct icmp_echo_hdr);
111
112 ICMPH_TYPE_SET(iecho, ICMP_ECHO);
113 ICMPH_CODE_SET(iecho, 0);
114 iecho->chksum = 0;
115 iecho->id = PING_ID;
116 iecho->seqno = lwip_htons(++ping_seq_num);
117
118 /* fill the additional data buffer with some data */
119 for(i = 0; i < data_len; i++) {
120 ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
121 }
122
123 iecho->chksum = inet_chksum(iecho, len);
124}
125
126#if PING_USE_SOCKETS
127
128/* Ping using the socket ip */
129static err_t
130ping_send(int s, const ip_addr_t *addr)
131{
132 int err;
133 struct icmp_echo_hdr *iecho;
134 struct sockaddr_storage to;
135 size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
136 LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
137
138#if LWIP_IPV6
139 if(IP_IS_V6(addr) && !ip6_addr_isipv4mappedipv6(ip_2_ip6(addr))) {
140 /* todo: support ICMP6 echo */
141 return ERR_VAL;
142 }
143#endif /* LWIP_IPV6 */
144
145 iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
146 if (!iecho) {
147 return ERR_MEM;
148 }
149
150 ping_prepare_echo(iecho, (u16_t)ping_size);
151
152#if LWIP_IPV4
153 if(IP_IS_V4(addr)) {
154 struct sockaddr_in *to4 = (struct sockaddr_in*)&to;
155 to4->sin_len = sizeof(to4);
156 to4->sin_family = AF_INET;
157 inet_addr_from_ip4addr(&to4->sin_addr, ip_2_ip4(addr));
158 }
159#endif /* LWIP_IPV4 */
160
161#if LWIP_IPV6
162 if(IP_IS_V6(addr)) {
163 struct sockaddr_in6 *to6 = (struct sockaddr_in6*)&to;
164 to6->sin6_len = sizeof(to6);
165 to6->sin6_family = AF_INET6;
166 inet6_addr_from_ip6addr(&to6->sin6_addr, ip_2_ip6(addr));
167 }
168#endif /* LWIP_IPV6 */
169
170 err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
171
172 mem_free(iecho);
173
174 return (err ? ERR_OK : ERR_VAL);
175}
176
177static void
178ping_recv(int s)
179{
180 char buf[64];
181 int len;
182 struct sockaddr_storage from;
183 int fromlen = sizeof(from);
184
185 while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
186 if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
187 ip_addr_t fromaddr;
188 memset(&fromaddr, 0, sizeof(fromaddr));
189
190#if LWIP_IPV4
191 if(from.ss_family == AF_INET) {
192 struct sockaddr_in *from4 = (struct sockaddr_in*)&from;
193 inet_addr_to_ip4addr(ip_2_ip4(&fromaddr), &from4->sin_addr);
194 IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V4);
195 }
196#endif /* LWIP_IPV4 */
197
198#if LWIP_IPV6
199 if(from.ss_family == AF_INET6) {
200 struct sockaddr_in6 *from6 = (struct sockaddr_in6*)&from;
201 inet6_addr_to_ip6addr(ip_2_ip6(&fromaddr), &from6->sin6_addr);
202 IP_SET_TYPE_VAL(fromaddr, IPADDR_TYPE_V6);
203 }
204#endif /* LWIP_IPV6 */
205
206 LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
207 ip_addr_debug_print_val(PING_DEBUG, fromaddr);
208 LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now() - ping_time)));
209
210 /* todo: support ICMP6 echo */
211#if LWIP_IPV4
212 if (IP_IS_V4_VAL(fromaddr)) {
213 struct ip_hdr *iphdr;
214 struct icmp_echo_hdr *iecho;
215
216 iphdr = (struct ip_hdr *)buf;
217 iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
218 if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
219 /* do some ping result processing */
220 PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
221#ifdef USE_PINGSEND
222 syslog_2(LOG_NOTICE, "ping: ok [%08x] %dus", fromaddr.u_addr.ip4.addr, (sys_now() - ping_time));
223#endif
224 return;
225 } else {
226#ifdef USE_PINGSEND
227 syslog_0(LOG_NOTICE, "ping: drop");
228#else
229 LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
230#endif
231 }
232 }
233#endif /* LWIP_IPV4 */
234 }
235 fromlen = sizeof(from);
236 }
237#ifdef USE_PINGSEND
238 syslog_1(LOG_NOTICE, "ping: drop (%d)", len);
239#endif
240
241 if (len == 0) {
242 LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", (sys_now()-ping_time)));
243 }
244
245 /* do some ping result processing */
246 PING_RESULT(0);
247}
248
249static void
250ping_thread(void *arg)
251{
252 int s;
253 int ret;
254#ifdef USE_PINGSEND
255 int count = 0;
256#endif
257
258#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
259 int timeout = PING_RCV_TIMEO;
260#else
261 struct timeval timeout;
262 timeout.tv_sec = PING_RCV_TIMEO/1000;
263 timeout.tv_usec = (PING_RCV_TIMEO%1000)*1000;
264#endif
265 LWIP_UNUSED_ARG(arg);
266
267#if LWIP_IPV6
268 if(IP_IS_V4(ping_target) || ip6_addr_isipv4mappedipv6(ip_2_ip6(ping_target))) {
269 s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
270 } else {
271 s = lwip_socket(AF_INET6, SOCK_RAW, IP6_NEXTH_ICMP6);
272 }
273#else
274 s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP);
275#endif
276 if (s < 0) {
277 return;
278 }
279
280 ret = lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
281 LWIP_ASSERT("setting receive timeout failed", ret == 0);
282 LWIP_UNUSED_ARG(ret);
283
284 while (1) {
285#ifdef USE_PINGSEND
286 while(ping_target->u_addr.ip4.addr == 0){
287 dly_tsk(500);
288 }
289#endif
290 if (ping_send(s, ping_target) == ERR_OK) {
291 LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
292 ip_addr_debug_print(PING_DEBUG, ping_target);
293 LWIP_DEBUGF( PING_DEBUG, ("\n"));
294
295 ping_time = sys_now();
296 ping_recv(s);
297 } else {
298 LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
299 ip_addr_debug_print(PING_DEBUG, ping_target);
300 LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
301 }
302 sys_msleep(PING_DELAY);
303#ifdef USE_PINGSEND
304 if(++count > 3){
305 ((ip_addr_t*)ping_target)->u_addr.ip4.addr = 0;
306 count = 0;
307 }
308#endif
309 }
310}
311
312#ifdef USE_PINGSEND
313void
314set_pingaddr(uint32_t paddr)
315{
316 ((ip_addr_t*)ping_target)->u_addr.ip4.addr = paddr;
317}
318#endif
319
320#else /* PING_USE_SOCKETS */
321
322/* Ping using the raw ip */
323static u8_t
324ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
325{
326 struct icmp_echo_hdr *iecho;
327 LWIP_UNUSED_ARG(arg);
328 LWIP_UNUSED_ARG(pcb);
329 LWIP_UNUSED_ARG(addr);
330 LWIP_ASSERT("p != NULL", p != NULL);
331
332 if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
333 pbuf_remove_header(p, PBUF_IP_HLEN) == 0) {
334 iecho = (struct icmp_echo_hdr *)p->payload;
335
336 if ((iecho->id == PING_ID) && (iecho->seqno == lwip_htons(ping_seq_num))) {
337 LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
338 ip_addr_debug_print(PING_DEBUG, addr);
339 LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", (sys_now()-ping_time)));
340
341 /* do some ping result processing */
342 PING_RESULT(1);
343 pbuf_free(p);
344 return 1; /* eat the packet */
345 }
346 /* not eaten, restore original packet */
347 pbuf_add_header(p, PBUF_IP_HLEN);
348 }
349
350 return 0; /* don't eat the packet */
351}
352
353static void
354ping_send(struct raw_pcb *raw, const ip_addr_t *addr)
355{
356 struct pbuf *p;
357 struct icmp_echo_hdr *iecho;
358 size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
359
360 LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
361 ip_addr_debug_print(PING_DEBUG, addr);
362 LWIP_DEBUGF( PING_DEBUG, ("\n"));
363 LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
364
365 p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
366 if (!p) {
367 return;
368 }
369 if ((p->len == p->tot_len) && (p->next == NULL)) {
370 iecho = (struct icmp_echo_hdr *)p->payload;
371
372 ping_prepare_echo(iecho, (u16_t)ping_size);
373
374 raw_sendto(raw, p, addr);
375#if defined(LWIP_DEBUG) || defined(USE_PINGSEND)
376 ping_time = sys_now();
377#endif /* LWIP_DEBUG */
378 }
379 pbuf_free(p);
380}
381
382static void
383ping_timeout(void *arg)
384{
385 struct raw_pcb *pcb = (struct raw_pcb*)arg;
386
387 LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
388
389 ping_send(ping_pcb, ping_target);
390
391 sys_timeout(PING_DELAY, ping_timeout, pcb);
392}
393
394static void
395ping_raw_init(void)
396{
397 ping_pcb = raw_new(IP_PROTO_ICMP);
398 LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
399
400 raw_recv(ping_pcb, ping_recv, NULL);
401 raw_bind(ping_pcb, IP_ADDR_ANY);
402 sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
403}
404
405void
406ping_send_now(void)
407{
408 LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
409 ping_send(ping_pcb, ping_target);
410}
411
412#endif /* PING_USE_SOCKETS */
413
414void
415ping_init(const ip_addr_t* ping_addr)
416{
417 ping_target = ping_addr;
418
419#if PING_USE_SOCKETS
420 sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
421#else /* PING_USE_SOCKETS */
422 ping_raw_init();
423#endif /* PING_USE_SOCKETS */
424}
425
426#endif /* LWIP_RAW */
Note: See TracBrowser for help on using the repository browser.