2007-11-07 20:52:32 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Ping sender module
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is an example of a "ping" sender (with raw API and socket API).
|
|
|
|
* It can be used as a start point to maintain opened a network connection, or
|
|
|
|
* like a network "watchdog" for your device.
|
|
|
|
*
|
|
|
|
*/
|
2007-11-04 21:52:57 +00:00
|
|
|
|
|
|
|
#include "lwip/opt.h"
|
2007-11-07 20:52:32 +00:00
|
|
|
|
2007-11-13 00:58:51 +00:00
|
|
|
#if LWIP_RAW && LWIP_ICMP /* don't build if not configured for use in lwipopts.h */
|
2007-11-07 20:52:32 +00:00
|
|
|
|
2007-11-04 21:52:57 +00:00
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/raw.h"
|
|
|
|
#include "lwip/icmp.h"
|
|
|
|
#include "lwip/netif.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/sockets.h"
|
2007-11-16 19:35:03 +00:00
|
|
|
#include "lwip/inet.h"
|
|
|
|
#include "lwip/inet_chksum.h"
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-07 20:52:32 +00:00
|
|
|
/**
|
|
|
|
* PING_DEBUG: Enable debugging for PING.
|
2007-11-04 22:07:42 +00:00
|
|
|
*/
|
2007-11-07 20:52:32 +00:00
|
|
|
#ifndef PING_DEBUG
|
|
|
|
#define PING_DEBUG LWIP_DBG_ON
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** ping target - should be a "struct ip_addr" */
|
|
|
|
#ifndef PING_TARGET
|
2007-11-30 16:39:16 +00:00
|
|
|
#define PING_TARGET (netif_default?netif_default->gw:ip_addr_any)
|
2007-11-07 20:52:32 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** ping receive timeout - in milliseconds */
|
|
|
|
#ifndef PING_RCV_TIMEO
|
|
|
|
#define PING_RCV_TIMEO 1000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** ping delay - in milliseconds */
|
|
|
|
#ifndef PING_DELAY
|
|
|
|
#define PING_DELAY 1000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** ping identifier - must fit on a u16_t */
|
|
|
|
#ifndef PING_ID
|
|
|
|
#define PING_ID 0xAFAF
|
|
|
|
#endif
|
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
/** ping additional data size to include in the packet */
|
|
|
|
#ifndef PING_DATA_SIZE
|
|
|
|
#define PING_DATA_SIZE 32
|
|
|
|
#endif
|
|
|
|
|
2007-11-25 00:57:51 +00:00
|
|
|
/** ping result action - no default action */
|
|
|
|
#ifndef PING_RESULT
|
|
|
|
#define PING_RESULT(ping_ok)
|
|
|
|
#endif
|
|
|
|
|
2007-11-04 21:52:57 +00:00
|
|
|
/* ping variables */
|
2007-11-07 20:52:32 +00:00
|
|
|
static u16_t ping_seq_num;
|
2007-11-04 21:52:57 +00:00
|
|
|
static u32_t ping_time;
|
|
|
|
|
|
|
|
#if NO_SYS
|
|
|
|
/* port-defined functions used for timer execution */
|
|
|
|
void sys_msleep(u32_t ms);
|
|
|
|
u32_t sys_now();
|
|
|
|
#endif /* NO_SYS */
|
|
|
|
|
2007-11-07 20:52:32 +00:00
|
|
|
/** Prepare a echo ICMP request */
|
|
|
|
static void
|
|
|
|
ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
|
2007-11-25 00:57:51 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-11-07 20:52:32 +00:00
|
|
|
ICMPH_TYPE_SET(iecho,ICMP_ECHO);
|
2007-11-24 15:00:00 +00:00
|
|
|
ICMPH_CODE_SET(iecho, 0);
|
2007-11-07 20:52:32 +00:00
|
|
|
iecho->chksum = 0;
|
|
|
|
iecho->id = PING_ID;
|
|
|
|
iecho->seqno = htons(++ping_seq_num);
|
|
|
|
iecho->chksum = inet_chksum(iecho, len);
|
2007-11-25 00:57:51 +00:00
|
|
|
|
|
|
|
/* fill the additional data buffer with some data */
|
|
|
|
for(i = 0; i < PING_DATA_SIZE; i++) {
|
|
|
|
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = i;
|
|
|
|
}
|
2007-11-07 20:52:32 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 21:52:57 +00:00
|
|
|
#if LWIP_SOCKET
|
2007-11-07 20:52:32 +00:00
|
|
|
|
2007-11-04 21:52:57 +00:00
|
|
|
/* Ping using the socket ip */
|
2008-01-06 20:24:56 +00:00
|
|
|
static err_t
|
2007-11-04 21:52:57 +00:00
|
|
|
ping_send(int s, struct ip_addr *addr)
|
|
|
|
{
|
2008-01-06 20:24:56 +00:00
|
|
|
int err;
|
2007-11-04 21:52:57 +00:00
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
struct sockaddr_in to;
|
2007-11-24 15:00:00 +00:00
|
|
|
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
if (!(iecho = mem_malloc(ping_size))) {
|
2008-01-06 20:24:56 +00:00
|
|
|
return ERR_MEM;
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
ping_prepare_echo(iecho, ping_size);
|
2007-11-04 21:52:57 +00:00
|
|
|
|
|
|
|
to.sin_len = sizeof(to);
|
|
|
|
to.sin_family = AF_INET;
|
|
|
|
to.sin_addr.s_addr = addr->addr;
|
|
|
|
|
2008-01-06 20:24:56 +00:00
|
|
|
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
|
2007-11-04 21:52:57 +00:00
|
|
|
|
|
|
|
mem_free(iecho);
|
2008-01-06 20:24:56 +00:00
|
|
|
|
|
|
|
return (err ? ERR_OK : ERR_VAL);
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-07 20:52:32 +00:00
|
|
|
ping_recv(int s)
|
2007-11-04 21:52:57 +00:00
|
|
|
{
|
2007-11-07 20:52:32 +00:00
|
|
|
char buf[64];
|
2007-11-04 21:52:57 +00:00
|
|
|
int fromlen, len;
|
|
|
|
struct sockaddr_in from;
|
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
|
2008-01-04 22:49:56 +00:00
|
|
|
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
|
2007-11-06 19:36:07 +00:00
|
|
|
if (len >= sizeof(struct icmp_echo_hdr)) {
|
2007-11-07 20:52:32 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, (struct ip_addr *)&(from.sin_addr));
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" %lu ms\n", (sys_now()-ping_time)));
|
|
|
|
|
2007-11-06 19:36:07 +00:00
|
|
|
iecho = (struct icmp_echo_hdr *)buf;
|
2007-11-07 20:52:32 +00:00
|
|
|
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
|
|
|
|
/* do some ping result processing */
|
2008-01-06 20:24:56 +00:00
|
|
|
PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
|
2007-11-06 19:36:07 +00:00
|
|
|
return;
|
2008-01-06 20:24:56 +00:00
|
|
|
} else {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
|
2007-11-06 19:36:07 +00:00
|
|
|
}
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-07 20:52:32 +00:00
|
|
|
|
2008-01-06 20:24:56 +00:00
|
|
|
if (len == 0) {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %lu ms - timeout\n", (sys_now()-ping_time)));
|
|
|
|
}
|
|
|
|
|
2007-11-07 20:52:32 +00:00
|
|
|
/* do some ping result processing */
|
|
|
|
PING_RESULT(0);
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_thread(void *arg)
|
|
|
|
{
|
|
|
|
int s;
|
2007-11-30 16:39:16 +00:00
|
|
|
int timeout = PING_RCV_TIMEO;
|
2007-12-23 14:39:50 +00:00
|
|
|
struct ip_addr ping_target;
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2008-01-04 22:49:56 +00:00
|
|
|
LWIP_UNUSED_ARG(arg);
|
|
|
|
|
2007-11-04 21:52:57 +00:00
|
|
|
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-06 19:36:07 +00:00
|
|
|
lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
|
|
|
|
2007-11-04 21:52:57 +00:00
|
|
|
while (1) {
|
2007-12-23 14:39:50 +00:00
|
|
|
ping_target = PING_TARGET;
|
|
|
|
|
2008-01-06 20:24:56 +00:00
|
|
|
if (ping_send(s, &ping_target) == ERR_OK) {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("\n"));
|
|
|
|
|
|
|
|
ping_time = sys_now();
|
|
|
|
ping_recv(s);
|
|
|
|
} else {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
|
|
|
|
}
|
2007-11-07 20:52:32 +00:00
|
|
|
sys_msleep(PING_DELAY);
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* LWIP_SOCKET */
|
|
|
|
|
|
|
|
/* Ping using the raw ip */
|
|
|
|
static u8_t
|
|
|
|
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *addr)
|
|
|
|
{
|
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
|
|
|
|
if (pbuf_header( p, -PBUF_IP_HLEN)==0) {
|
|
|
|
iecho = p->payload;
|
|
|
|
|
2007-11-07 20:52:32 +00:00
|
|
|
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, addr);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" %lu ms\n", (sys_now()-ping_time)));
|
|
|
|
|
|
|
|
/* do some ping result processing */
|
|
|
|
PING_RESULT(1);
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /* eat the event */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_send(struct raw_pcb *raw, struct ip_addr *addr)
|
|
|
|
{
|
|
|
|
struct pbuf *p;
|
|
|
|
struct icmp_echo_hdr *iecho;
|
2007-11-24 15:00:00 +00:00
|
|
|
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
if (!(p = pbuf_alloc(PBUF_IP, ping_size, PBUF_RAM))) {
|
2007-11-04 21:52:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-11-24 15:00:00 +00:00
|
|
|
if ((p->len == p->tot_len) && (p->next == NULL)) {
|
|
|
|
iecho = p->payload;
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
ping_prepare_echo(iecho, ping_size);
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
raw_sendto(raw, p, addr);
|
|
|
|
ping_time = sys_now();
|
|
|
|
}
|
2007-11-04 21:52:57 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-11-24 15:00:00 +00:00
|
|
|
ping_timeout(void *arg)
|
2007-11-04 21:52:57 +00:00
|
|
|
{
|
2007-11-24 15:00:00 +00:00
|
|
|
struct raw_pcb *pcb = (struct raw_pcb*)arg;
|
2007-11-30 16:39:16 +00:00
|
|
|
struct ip_addr ping_target = PING_TARGET;
|
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
2007-11-30 16:39:16 +00:00
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target);
|
2007-11-24 15:00:00 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("\n"));
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-30 16:39:16 +00:00
|
|
|
ping_send(pcb, &ping_target);
|
2007-11-04 21:52:57 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
sys_timeout(PING_DELAY, ping_timeout, pcb);
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
2007-11-25 00:57:51 +00:00
|
|
|
static void
|
|
|
|
ping_raw_init(void)
|
2007-11-24 15:00:00 +00:00
|
|
|
{
|
|
|
|
struct raw_pcb *pcb;
|
2007-11-25 00:57:51 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
if (!(pcb = raw_new(IP_PROTO_ICMP))) {
|
|
|
|
return;
|
|
|
|
}
|
2007-11-25 00:57:51 +00:00
|
|
|
|
2007-11-24 15:00:00 +00:00
|
|
|
raw_recv(pcb, ping_recv, NULL);
|
|
|
|
raw_bind(pcb, IP_ADDR_ANY);
|
|
|
|
sys_timeout(PING_DELAY, ping_timeout, pcb);
|
2007-11-25 00:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LWIP_SOCKET */
|
|
|
|
|
|
|
|
void
|
|
|
|
ping_init(void)
|
|
|
|
{
|
|
|
|
#if LWIP_SOCKET
|
|
|
|
sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
|
|
|
#else /* LWIP_SOCKET */
|
|
|
|
ping_raw_init();
|
2007-11-24 15:00:00 +00:00
|
|
|
#endif /* LWIP_SOCKET */
|
2007-11-04 21:52:57 +00:00
|
|
|
}
|
|
|
|
|
2007-11-07 20:52:32 +00:00
|
|
|
#endif /* LWIP_RAW && LWIP_ICMP */
|