2016-12-07 09:34:26 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lwip/opt.h"
|
|
|
|
|
|
|
|
#if LWIP_IPV4 && LWIP_RAW /* don't build if not configured for use in lwipopts.h */
|
|
|
|
|
2018-09-06 11:43:08 +00:00
|
|
|
#include "ping/ping.h"
|
2016-12-07 09:34:26 +00:00
|
|
|
|
|
|
|
#include "lwip/mem.h"
|
|
|
|
#include "lwip/raw.h"
|
|
|
|
#include "lwip/icmp.h"
|
|
|
|
#include "lwip/netif.h"
|
|
|
|
#include "lwip/sys.h"
|
2018-09-06 11:43:08 +00:00
|
|
|
#include "lwip/timeouts.h"
|
2016-12-07 09:34:26 +00:00
|
|
|
#include "lwip/inet_chksum.h"
|
|
|
|
|
|
|
|
#if PING_USE_SOCKETS
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/inet.h"
|
|
|
|
#endif /* PING_USE_SOCKETS */
|
|
|
|
|
2017-06-15 03:37:23 +00:00
|
|
|
#ifdef ESP_PING
|
2018-09-10 04:21:59 +00:00
|
|
|
#include "esp_ping.h"
|
2016-12-07 09:34:26 +00:00
|
|
|
#include "lwip/ip_addr.h"
|
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* PING_DEBUG: Enable debugging for PING.
|
|
|
|
*/
|
|
|
|
#ifndef PING_DEBUG
|
2017-06-15 03:37:23 +00:00
|
|
|
#define PING_DEBUG LWIP_DBG_OFF
|
2016-12-07 09:34:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/** ping target - should be an "ip4_addr_t" */
|
|
|
|
#ifndef PING_TARGET
|
|
|
|
#define PING_TARGET (netif_default ? *netif_ip4_gw(netif_default) : (*IP4_ADDR_ANY))
|
|
|
|
#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
|
|
|
|
|
|
|
|
/** ping additional data size to include in the packet */
|
|
|
|
#ifndef PING_DATA_SIZE
|
|
|
|
#define PING_DATA_SIZE 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** ping result action - no default action */
|
|
|
|
#ifndef PING_RESULT
|
|
|
|
#define PING_RESULT(ping_ok)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ping variables */
|
|
|
|
static u16_t ping_seq_num;
|
2017-06-15 03:37:23 +00:00
|
|
|
static struct timeval ping_time;
|
|
|
|
#if ESP_PING
|
|
|
|
static sys_sem_t ping_sem = NULL;
|
|
|
|
static bool ping_init_flag = false;
|
|
|
|
#endif
|
2016-12-07 09:34:26 +00:00
|
|
|
#if !PING_USE_SOCKETS
|
|
|
|
static struct raw_pcb *ping_pcb;
|
|
|
|
#endif /* PING_USE_SOCKETS */
|
|
|
|
|
2017-06-15 03:37:23 +00:00
|
|
|
#define PING_TIME_DIFF_MS(_end, _start) ((uint32_t)(((_end).tv_sec - (_start).tv_sec) * 1000 + (_end.tv_usec - _start.tv_usec)/1000))
|
|
|
|
#define PING_TIME_DIFF_SEC(_end, _start) ((uint32_t)((_end).tv_sec - (_start).tv_sec))
|
|
|
|
|
2016-12-07 09:34:26 +00:00
|
|
|
/** Prepare a echo ICMP request */
|
|
|
|
static void
|
|
|
|
ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t data_len = len - sizeof(struct icmp_echo_hdr);
|
|
|
|
|
|
|
|
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
|
|
|
|
ICMPH_CODE_SET(iecho, 0);
|
|
|
|
iecho->chksum = 0;
|
|
|
|
iecho->id = PING_ID;
|
2018-01-09 09:44:26 +00:00
|
|
|
iecho->seqno = htons(++ping_seq_num);
|
2016-12-07 09:34:26 +00:00
|
|
|
|
|
|
|
/* fill the additional data buffer with some data */
|
|
|
|
for(i = 0; i < data_len; i++) {
|
|
|
|
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
|
|
|
|
}
|
|
|
|
|
|
|
|
iecho->chksum = inet_chksum(iecho, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if PING_USE_SOCKETS
|
|
|
|
|
|
|
|
/* Ping using the socket ip */
|
|
|
|
static err_t
|
|
|
|
ping_send(int s, ip_addr_t *addr)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
struct sockaddr_in to;
|
2018-09-05 13:02:38 +00:00
|
|
|
size_t ping_size;
|
|
|
|
|
|
|
|
#ifdef ESP_PING
|
|
|
|
size_t ping_data_len = 0;
|
|
|
|
esp_ping_get_target(PING_TARGET_DATA_LEN, &ping_data_len, sizeof(ping_data_len));
|
|
|
|
|
|
|
|
if (ping_data_len > 0) {
|
|
|
|
ping_size = sizeof(struct icmp_echo_hdr) + ping_data_len;
|
|
|
|
} else {
|
|
|
|
ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
|
|
|
#endif
|
|
|
|
|
2016-12-07 09:34:26 +00:00
|
|
|
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
|
|
|
|
LWIP_ASSERT("ping: expect IPv4 address", !IP_IS_V6(addr));
|
|
|
|
|
|
|
|
iecho = (struct icmp_echo_hdr *)mem_malloc((mem_size_t)ping_size);
|
|
|
|
if (!iecho) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
ping_prepare_echo(iecho, (u16_t)ping_size);
|
|
|
|
|
|
|
|
to.sin_len = sizeof(to);
|
|
|
|
to.sin_family = AF_INET;
|
2018-09-06 11:43:08 +00:00
|
|
|
inet_addr_from_ip4addr(&to.sin_addr, ip_2_ip4(addr));
|
2016-12-07 09:34:26 +00:00
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
err = sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
|
2016-12-07 09:34:26 +00:00
|
|
|
|
|
|
|
mem_free(iecho);
|
|
|
|
|
|
|
|
return (err ? ERR_OK : ERR_VAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_recv(int s)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
int len;
|
|
|
|
struct sockaddr_in from;
|
|
|
|
struct ip_hdr *iphdr;
|
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
int fromlen = sizeof(from);
|
2017-06-15 03:37:23 +00:00
|
|
|
struct timeval now;
|
2016-12-07 09:34:26 +00:00
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
while((len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) {
|
2016-12-07 09:34:26 +00:00
|
|
|
if (len >= (int)(sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) {
|
|
|
|
if (from.sin_family != AF_INET) {
|
|
|
|
/* Ping is not IPv4 */
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: invalid sin_family\n"));
|
|
|
|
} else {
|
|
|
|
ip4_addr_t fromaddr;
|
2018-09-06 11:43:08 +00:00
|
|
|
inet_addr_to_ip4addr(&fromaddr, &from.sin_addr);
|
2016-12-07 09:34:26 +00:00
|
|
|
iphdr = (struct ip_hdr *)buf;
|
|
|
|
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
|
2017-06-15 03:37:23 +00:00
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv seq=%d ", ntohs(iecho->seqno)));
|
2017-06-15 03:37:23 +00:00
|
|
|
ip4_addr_debug_print(PING_DEBUG, &fromaddr);
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", PING_TIME_DIFF_MS(now, ping_time)));
|
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
|
2016-12-07 09:34:26 +00:00
|
|
|
/* do some ping result processing */
|
2017-06-15 03:37:23 +00:00
|
|
|
#ifdef ESP_PING
|
|
|
|
esp_ping_result((ICMPH_TYPE(iecho) == ICMP_ER), len, PING_TIME_DIFF_MS(now, ping_time));
|
2016-12-07 09:34:26 +00:00
|
|
|
#else
|
|
|
|
PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: drop\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fromlen = sizeof(from);
|
|
|
|
}
|
|
|
|
|
2017-06-15 03:37:23 +00:00
|
|
|
gettimeofday(&now, NULL);
|
2016-12-07 09:34:26 +00:00
|
|
|
if (len == 0) {
|
2017-06-15 03:37:23 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %"U32_F" ms - timeout\n", PING_TIME_DIFF_MS(now, ping_time)));
|
2016-12-07 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do some ping result processing */
|
2017-06-15 03:37:23 +00:00
|
|
|
#ifdef ESP_PING
|
|
|
|
esp_ping_result(0, len, PING_TIME_DIFF_MS(now, ping_time));
|
2016-12-07 09:34:26 +00:00
|
|
|
#else
|
|
|
|
PING_RESULT(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_thread(void *arg)
|
|
|
|
{
|
2017-06-15 03:37:23 +00:00
|
|
|
uint32_t ping_timeout = PING_RCV_TIMEO;
|
|
|
|
uint32_t ping_delay = PING_DELAY;
|
2016-12-07 09:34:26 +00:00
|
|
|
ip_addr_t ping_target;
|
2017-06-15 03:37:23 +00:00
|
|
|
int ret;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
#ifdef ESP_PING
|
|
|
|
uint32_t ping_count_cur = 0;
|
|
|
|
uint32_t ping_count_max = 3;
|
|
|
|
ip4_addr_t ipaddr;
|
|
|
|
int lev;
|
|
|
|
|
|
|
|
esp_ping_get_target(PING_TARGET_IP_ADDRESS_COUNT, &ping_count_max, sizeof(ping_count_max));
|
|
|
|
esp_ping_get_target(PING_TARGET_RCV_TIMEO, &ping_timeout, sizeof(ping_timeout));
|
|
|
|
esp_ping_get_target(PING_TARGET_DELAY_TIME, &ping_delay, sizeof(ping_delay));
|
|
|
|
esp_ping_get_target(PING_TARGET_IP_ADDRESS, &ipaddr.addr, sizeof(uint32_t));
|
|
|
|
ip_addr_copy_from_ip4(ping_target, ipaddr);
|
|
|
|
#else
|
|
|
|
ip_addr_copy_from_ip4(ping_target, PING_TARGET);
|
|
|
|
#endif
|
|
|
|
|
2016-12-07 09:34:26 +00:00
|
|
|
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
|
2017-06-15 03:37:23 +00:00
|
|
|
int timeout = ping_timeout;
|
2016-12-07 09:34:26 +00:00
|
|
|
#else
|
|
|
|
struct timeval timeout;
|
2017-06-15 03:37:23 +00:00
|
|
|
timeout.tv_sec = ping_timeout/1000;
|
|
|
|
timeout.tv_usec = (ping_timeout%1000)*1000;
|
2016-12-07 09:34:26 +00:00
|
|
|
#endif
|
2017-06-15 03:37:23 +00:00
|
|
|
|
2016-12-07 09:34:26 +00:00
|
|
|
LWIP_UNUSED_ARG(arg);
|
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
if ((s = socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) {
|
|
|
|
goto _exit_new_socket_failed;
|
2016-12-07 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
ret = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
2016-12-07 09:34:26 +00:00
|
|
|
LWIP_ASSERT("setting receive timeout failed", ret == 0);
|
|
|
|
LWIP_UNUSED_ARG(ret);
|
|
|
|
|
2018-09-05 13:02:38 +00:00
|
|
|
#ifdef ESP_PING
|
|
|
|
int tos = 0;
|
|
|
|
esp_ping_get_target(PING_TARGET_IP_TOS, &tos, sizeof(int));
|
|
|
|
if (tos > 0) {
|
|
|
|
tos <<= 5;
|
|
|
|
ret = setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
|
|
|
|
LWIP_ASSERT("setting IP_TOS failed", ret == 0);
|
|
|
|
LWIP_UNUSED_ARG(ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-12-07 09:34:26 +00:00
|
|
|
while (1) {
|
2017-06-15 03:37:23 +00:00
|
|
|
#ifdef ESP_PING
|
|
|
|
if (ping_count_cur++ >= ping_count_max) {
|
|
|
|
goto _exit;
|
|
|
|
}
|
2016-12-07 09:34:26 +00:00
|
|
|
|
2017-06-15 03:37:23 +00:00
|
|
|
if (ping_init_flag == false) {
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-07 09:34:26 +00:00
|
|
|
if (ping_send(s, &ping_target) == ERR_OK) {
|
2017-06-15 03:37:23 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send seq=%d ", ping_seq_num));
|
2016-12-07 09:34:26 +00:00
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("\n"));
|
|
|
|
|
2017-06-15 03:37:23 +00:00
|
|
|
gettimeofday(&ping_time, NULL);
|
2016-12-07 09:34:26 +00:00
|
|
|
ping_recv(s);
|
|
|
|
} else {
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, &ping_target);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" - error\n"));
|
|
|
|
}
|
2017-06-15 03:37:23 +00:00
|
|
|
sys_msleep(ping_delay);
|
2016-12-07 09:34:26 +00:00
|
|
|
}
|
2017-06-15 03:37:23 +00:00
|
|
|
|
|
|
|
#ifdef ESP_PING
|
|
|
|
_exit:
|
2018-01-09 09:44:26 +00:00
|
|
|
close(s);
|
2017-06-15 03:37:23 +00:00
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
_exit_new_socket_failed:
|
2017-06-15 03:37:23 +00:00
|
|
|
esp_ping_result(PING_RES_FINISH, 0, 0);
|
|
|
|
SYS_ARCH_PROTECT(lev);
|
|
|
|
if (ping_init_flag) { /* Ping closed by this thread */
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: closed by self "));
|
|
|
|
if (ping_sem) {
|
|
|
|
sys_sem_free(&ping_sem);
|
|
|
|
}
|
|
|
|
ping_sem = NULL;
|
|
|
|
ping_init_flag = false;
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
} else { /* Ping closed by task calls ping_deinit */
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: closed by other"));
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
if (ping_sem) {
|
|
|
|
sys_sem_signal(&ping_sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
#endif
|
2016-12-07 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* PING_USE_SOCKETS */
|
|
|
|
|
|
|
|
/* Ping using the raw ip */
|
|
|
|
static u8_t
|
|
|
|
ping_recv(void *arg, struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *addr)
|
|
|
|
{
|
|
|
|
struct icmp_echo_hdr *iecho;
|
2017-06-15 03:37:23 +00:00
|
|
|
struct timeval now;
|
|
|
|
|
2016-12-07 09:34:26 +00:00
|
|
|
LWIP_UNUSED_ARG(arg);
|
|
|
|
LWIP_UNUSED_ARG(pcb);
|
|
|
|
LWIP_UNUSED_ARG(addr);
|
|
|
|
LWIP_ASSERT("p != NULL", p != NULL);
|
|
|
|
|
|
|
|
if ((p->tot_len >= (PBUF_IP_HLEN + sizeof(struct icmp_echo_hdr))) &&
|
|
|
|
pbuf_header(p, -PBUF_IP_HLEN) == 0) {
|
|
|
|
iecho = (struct icmp_echo_hdr *)p->payload;
|
|
|
|
|
2018-01-09 09:44:26 +00:00
|
|
|
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) {
|
2016-12-07 09:34:26 +00:00
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: recv "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, addr);
|
2017-06-15 03:37:23 +00:00
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, (" %"U32_F" ms\n", PING_TIME_DIFF_MS(now, ping_time)));
|
2016-12-07 09:34:26 +00:00
|
|
|
|
|
|
|
/* do some ping result processing */
|
|
|
|
PING_RESULT(1);
|
|
|
|
pbuf_free(p);
|
|
|
|
return 1; /* eat the packet */
|
|
|
|
}
|
|
|
|
/* not eaten, restore original packet */
|
|
|
|
pbuf_header(p, PBUF_IP_HLEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* don't eat the packet */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_send(struct raw_pcb *raw, ip_addr_t *addr)
|
|
|
|
{
|
|
|
|
struct pbuf *p;
|
|
|
|
struct icmp_echo_hdr *iecho;
|
|
|
|
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
|
|
|
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("ping: send "));
|
|
|
|
ip_addr_debug_print(PING_DEBUG, addr);
|
|
|
|
LWIP_DEBUGF( PING_DEBUG, ("\n"));
|
|
|
|
LWIP_ASSERT("ping_size <= 0xffff", ping_size <= 0xffff);
|
|
|
|
|
|
|
|
p = pbuf_alloc(PBUF_IP, (u16_t)ping_size, PBUF_RAM);
|
|
|
|
if (!p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((p->len == p->tot_len) && (p->next == NULL)) {
|
|
|
|
iecho = (struct icmp_echo_hdr *)p->payload;
|
|
|
|
|
|
|
|
ping_prepare_echo(iecho, (u16_t)ping_size);
|
|
|
|
|
|
|
|
raw_sendto(raw, p, addr);
|
2017-06-15 03:37:23 +00:00
|
|
|
ping_time = system_get_time();
|
2016-12-07 09:34:26 +00:00
|
|
|
}
|
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_timeout(void *arg)
|
|
|
|
{
|
|
|
|
struct raw_pcb *pcb = (struct raw_pcb*)arg;
|
|
|
|
ip_addr_t ping_target;
|
|
|
|
|
|
|
|
LWIP_ASSERT("ping_timeout: no pcb given!", pcb != NULL);
|
|
|
|
|
|
|
|
ip_addr_copy_from_ip4(ping_target, PING_TARGET);
|
|
|
|
ping_send(pcb, &ping_target);
|
|
|
|
sys_timeout(PING_DELAY, ping_timeout, pcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ping_raw_init(void)
|
|
|
|
{
|
|
|
|
ping_pcb = raw_new(IP_PROTO_ICMP);
|
|
|
|
LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
|
|
|
|
|
|
|
|
raw_recv(ping_pcb, ping_recv, NULL);
|
|
|
|
raw_bind(ping_pcb, IP_ADDR_ANY);
|
|
|
|
sys_timeout(PING_DELAY, ping_timeout, ping_pcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ping_send_now(void)
|
|
|
|
{
|
|
|
|
ip_addr_t ping_target;
|
|
|
|
LWIP_ASSERT("ping_pcb != NULL", ping_pcb != NULL);
|
|
|
|
ip_addr_copy_from_ip4(ping_target, PING_TARGET);
|
|
|
|
ping_send(ping_pcb, &ping_target);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* PING_USE_SOCKETS */
|
|
|
|
|
2017-06-15 03:37:23 +00:00
|
|
|
int
|
2016-12-07 09:34:26 +00:00
|
|
|
ping_init(void)
|
|
|
|
{
|
2017-06-15 03:37:23 +00:00
|
|
|
int ret;
|
|
|
|
int lev;
|
|
|
|
|
|
|
|
SYS_ARCH_PROTECT(lev);
|
|
|
|
if (ping_init_flag) {
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
/* Currently we only support one ping, call ping_deinit to kill the running ping before start new ping */
|
|
|
|
return ERR_INPROGRESS;
|
|
|
|
}
|
|
|
|
ret = sys_sem_new(&ping_sem, 0);
|
|
|
|
if (ERR_OK != ret) {
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
ping_init_flag = true;
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
2016-12-07 09:34:26 +00:00
|
|
|
#if PING_USE_SOCKETS
|
|
|
|
sys_thread_new("ping_thread", ping_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
|
|
|
|
#else /* PING_USE_SOCKETS */
|
|
|
|
ping_raw_init();
|
|
|
|
#endif /* PING_USE_SOCKETS */
|
2017-06-15 03:37:23 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ping_deinit(void)
|
|
|
|
{
|
|
|
|
int lev;
|
|
|
|
|
|
|
|
SYS_ARCH_PROTECT(lev);
|
|
|
|
if (ping_init_flag == false) {
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ping_init_flag = false;
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
if (ping_sem) {
|
|
|
|
sys_sem_wait(&ping_sem);
|
|
|
|
|
|
|
|
SYS_ARCH_PROTECT(lev);
|
|
|
|
sys_sem_free(&ping_sem);
|
|
|
|
ping_sem = NULL;
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
}
|
2016-12-07 09:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* LWIP_IPV4 && LWIP_RAW */
|