2017-10-24 19:19:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/system/ping/ping.c
|
|
|
|
*
|
2018-03-14 14:59:35 +01:00
|
|
|
* Copyright (C) 2017-2018 Gregory Nutt. All rights reserved.
|
2017-10-24 19:19:41 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "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
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2018-03-14 14:59:35 +01:00
|
|
|
#include <time.h>
|
2017-10-24 19:19:41 +02:00
|
|
|
#include <poll.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/icmpv6.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
/* Positive number represent information */
|
|
|
|
|
|
|
|
#define ICMPv6_I_BEGIN 0 /* extra: not used */
|
|
|
|
#define ICMPv6_I_ROUNDTRIP 1 /* extra: packet delay */
|
|
|
|
#define ICMPv6_I_FINISH 2 /* extra: elapsed time */
|
|
|
|
|
|
|
|
/* Negative odd number represent error(unrecoverable) */
|
|
|
|
|
|
|
|
#define ICMPv6_E_HOSTIP -1 /* extra: not used */
|
|
|
|
#define ICMPv6_E_MEMORY -3 /* extra: not used */
|
|
|
|
#define ICMPv6_E_SOCKET -5 /* extra: error code */
|
|
|
|
#define ICMPv6_E_SENDTO -7 /* extra: error code */
|
|
|
|
#define ICMPv6_E_SENDSMALL -9 /* extra: sent bytes */
|
|
|
|
#define ICMPv6_E_POLL -11 /* extra: error code */
|
|
|
|
#define ICMPv6_E_RECVFROM -13 /* extra: error code */
|
|
|
|
#define ICMPv6_E_RECVSMALL -15 /* extra: recv bytes */
|
|
|
|
|
|
|
|
/* Negative even number represent warning(recoverable) */
|
|
|
|
|
|
|
|
#define ICMPv6_W_TIMEOUT -2 /* extra: timeout value */
|
|
|
|
#define ICMPv6_W_IDDIFF -4 /* extra: recv id */
|
|
|
|
#define ICMPv6_W_SEQNOBIG -6 /* extra: recv seqno */
|
|
|
|
#define ICMPv6_W_SEQNOSMALL -8 /* extra: recv seqno */
|
|
|
|
#define ICMPv6_W_RECVBIG -10 /* extra: recv bytes */
|
|
|
|
#define ICMPv6_W_DATADIFF -12 /* extra: not used */
|
|
|
|
#define ICMPv6_W_TYPE -14 /* extra: recv type */
|
|
|
|
|
2017-10-24 23:38:37 +02:00
|
|
|
#define ICMPv6_PING6_DATALEN 56
|
2018-11-08 16:05:54 +01:00
|
|
|
#define ICMPv6_IOBUFFER_SIZE(x) (SIZEOF_ICMPV6_ECHO_REQUEST_S(0) + (x))
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
#define ICMPv6_NPINGS 10 /* Default number of pings */
|
|
|
|
#define ICMPv6_POLL_DELAY 1000 /* 1 second in milliseconds */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
struct ping6_result_s;
|
|
|
|
|
2017-10-24 19:19:41 +02:00
|
|
|
struct ping6_info_s
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
FAR const char *hostname; /* Host name to ping */
|
|
|
|
uint16_t count; /* Number of pings requested */
|
|
|
|
uint16_t datalen; /* Number of bytes to be sent */
|
|
|
|
uint16_t nrequests; /* Number of ICMP ECHO requests sent */
|
|
|
|
uint16_t nreplies; /* Number of matching ICMP ECHO replies received */
|
|
|
|
uint16_t delay; /* Deciseconds to delay between pings */
|
|
|
|
uint16_t timeout; /* Deciseconds to wait response before timeout */
|
|
|
|
FAR void *priv; /* Private context for callback */
|
|
|
|
void (*callback)(FAR const struct ping6_result_s *result);
|
|
|
|
};
|
2017-10-24 23:38:37 +02:00
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
struct ping6_result_s
|
|
|
|
{
|
|
|
|
int code; /* Notice code ICMPv6_I/E/W_XXX */
|
|
|
|
int extra; /* Extra information for code */
|
|
|
|
struct in6_addr dest; /* Target address to ping */
|
|
|
|
uint16_t nrequests; /* Number of ICMP ECHO requests sent */
|
|
|
|
uint16_t nreplies; /* Number of matching ICMP ECHO replies received */
|
|
|
|
uint16_t outsize; /* Bytes(include ICMP header) to be sent */
|
|
|
|
uint16_t id; /* ICMPv6_ECHO id */
|
|
|
|
uint16_t seqno; /* ICMPv6_ECHO seqno */
|
|
|
|
FAR const struct ping6_info_s *info;
|
2017-10-24 19:19:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* NOTE: This will not work in the kernel build where there will be a
|
|
|
|
* separate instance of g_ping6_id in every process space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static uint16_t g_ping6_id = 0;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ping6_newid
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline uint16_t ping6_newid(void)
|
|
|
|
{
|
|
|
|
/* Revisit: No thread safe */
|
|
|
|
|
|
|
|
return ++g_ping6_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ping6_gethostip
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Call gethostbyname() to get the IP address associated with a hostname.
|
|
|
|
*
|
|
|
|
* Input Parameters
|
|
|
|
* hostname - The host name to use in the nslookup.
|
|
|
|
* ipv4addr - The location to return the IPv4 address.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
static int ping6_gethostip(FAR const char *hostname, FAR struct in6_addr *dest)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
|
2018-11-08 16:05:54 +01:00
|
|
|
/* Netdb DNS client support is enabled */
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
FAR struct hostent *he;
|
|
|
|
|
|
|
|
he = gethostbyname(hostname);
|
|
|
|
if (he == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: gethostbyname failed: %d\n", h_errno);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
else if (he->h_addrtype == AF_INET6)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
memcpy(dest, he->h_addr, sizeof(struct in6_addr));
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("ERROR: gethostbyname returned an address of type: %d\n",
|
|
|
|
he->h_addrtype);
|
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
#else /* CONFIG_LIBC_NETDB */
|
|
|
|
|
|
|
|
/* No host name support */
|
|
|
|
/* Convert strings to numeric IPv6 address */
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
int ret = inet_pton(AF_INET6, hostname, dest->s6_addr16);
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
/* The inet_pton() function returns 1 if the conversion succeeds. It will
|
|
|
|
* return 0 if the input is not a valid IPv6 address string, or -1 with
|
|
|
|
* errno set to EAFNOSUPPORT if the address family argument is unsupported.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (ret > 0) ? OK : ERROR;
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIBC_NETDB */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-11-08 16:05:54 +01:00
|
|
|
* Name: icmp6_callback
|
2017-10-24 19:19:41 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
static void icmp6_callback(FAR struct ping6_result_s *result, int code,
|
|
|
|
int extra)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
result->code = code;
|
|
|
|
result->extra = extra;
|
|
|
|
result->info->callback(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmp6_ping
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void icmp6_ping(FAR const struct ping6_info_s *info)
|
|
|
|
{
|
|
|
|
struct ping6_result_s result;
|
2017-10-24 19:19:41 +02:00
|
|
|
struct sockaddr_in6 destaddr;
|
|
|
|
struct sockaddr_in6 fromaddr;
|
|
|
|
struct icmpv6_echo_request_s outhdr;
|
|
|
|
FAR struct icmpv6_echo_reply_s *inhdr;
|
|
|
|
struct pollfd recvfd;
|
2018-11-08 16:05:54 +01:00
|
|
|
FAR uint8_t *iobuffer;
|
2017-10-24 19:19:41 +02:00
|
|
|
FAR uint8_t *ptr;
|
|
|
|
int32_t elapsed;
|
2018-11-08 16:05:54 +01:00
|
|
|
clock_t kickoff;
|
2018-03-14 14:59:35 +01:00
|
|
|
clock_t start;
|
2017-10-24 19:19:41 +02:00
|
|
|
socklen_t addrlen;
|
|
|
|
ssize_t nsent;
|
|
|
|
ssize_t nrecvd;
|
|
|
|
bool retry;
|
2018-11-08 16:05:54 +01:00
|
|
|
int sockfd;
|
2017-10-24 19:19:41 +02:00
|
|
|
int ret;
|
|
|
|
int ch;
|
|
|
|
int i;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
/* Initialize result structure */
|
|
|
|
|
|
|
|
memset(&result, 0, sizeof(result));
|
|
|
|
result.info = info;
|
|
|
|
result.id = ping6_newid();
|
|
|
|
result.outsize = ICMPv6_IOBUFFER_SIZE(info->datalen);
|
|
|
|
if (ping6_gethostip(info->hostname, &result.dest) < 0)
|
|
|
|
{
|
|
|
|
icmp6_callback(&result, ICMPv6_E_HOSTIP, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate memory to hold ping buffer */
|
|
|
|
|
|
|
|
iobuffer = (FAR uint8_t *)malloc(result.outsize);
|
|
|
|
if (iobuffer == NULL)
|
|
|
|
{
|
|
|
|
icmp6_callback(&result, ICMPv6_E_MEMORY, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockfd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMP6);
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
icmp6_callback(&result, ICMPv6_E_SOCKET, errno);
|
|
|
|
free(iobuffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kickoff = clock();
|
|
|
|
|
2017-10-24 19:19:41 +02:00
|
|
|
memset(&destaddr, 0, sizeof(struct sockaddr_in6));
|
|
|
|
destaddr.sin6_family = AF_INET6;
|
|
|
|
destaddr.sin6_port = 0;
|
2018-11-08 16:05:54 +01:00
|
|
|
memcpy(&destaddr.sin6_addr, &result.dest, sizeof(struct in6_addr));
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
memset(&outhdr, 0, SIZEOF_ICMPV6_ECHO_REQUEST_S(0));
|
|
|
|
outhdr.type = ICMPv6_ECHO_REQUEST;
|
2018-11-08 16:05:54 +01:00
|
|
|
outhdr.id = htons(result.id);
|
|
|
|
outhdr.seqno = htons(result.seqno);
|
2017-10-24 19:19:41 +02:00
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_I_BEGIN, 0);
|
2017-10-24 19:19:41 +02:00
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
while (result.nrequests < info->count)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
|
|
|
/* Copy the ICMP header into the I/O buffer */
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
memcpy(iobuffer, &outhdr, SIZEOF_ICMPV6_ECHO_REQUEST_S(0));
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
/* Add some easily verifiable payload data */
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
ptr = &iobuffer[SIZEOF_ICMPV6_ECHO_REQUEST_S(0)];
|
2017-10-24 19:19:41 +02:00
|
|
|
ch = 0x20;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
for (i = 0; i < info->datalen; i++)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
|
|
|
*ptr++ = ch;
|
|
|
|
if (++ch > 0x7e)
|
|
|
|
{
|
|
|
|
ch = 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
start = clock();
|
|
|
|
nsent = sendto(sockfd, iobuffer, result.outsize, 0,
|
|
|
|
(FAR struct sockaddr*)&destaddr,
|
|
|
|
sizeof(struct sockaddr_in6));
|
2017-10-24 19:19:41 +02:00
|
|
|
if (nsent < 0)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_E_SENDTO, errno);
|
|
|
|
goto done;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
2018-11-08 16:05:54 +01:00
|
|
|
else if (nsent != result.outsize)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_E_SENDSMALL, nsent);
|
|
|
|
goto done;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
result.nrequests++;
|
2017-10-24 19:19:41 +02:00
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
elapsed = 0;
|
2017-10-24 19:19:41 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
retry = false;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
recvfd.fd = sockfd;
|
2017-10-24 19:19:41 +02:00
|
|
|
recvfd.events = POLLIN;
|
|
|
|
recvfd.revents = 0;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
ret = poll(&recvfd, 1, info->timeout - elapsed);
|
2017-10-24 19:19:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_E_POLL, errno);
|
|
|
|
goto done;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_TIMEOUT, info->timeout);
|
2017-10-24 19:19:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the ICMP response (ignoring the sender) */
|
|
|
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2018-11-08 16:05:54 +01:00
|
|
|
nrecvd = recvfrom(sockfd, iobuffer, result.outsize, 0,
|
2017-10-24 19:19:41 +02:00
|
|
|
(FAR struct sockaddr *)&fromaddr, &addrlen);
|
|
|
|
if (nrecvd < 0)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_E_RECVFROM, errno);
|
|
|
|
goto done;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
else if (nrecvd < SIZEOF_ICMPV6_ECHO_REPLY_S(0))
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_E_RECVSMALL, nrecvd);
|
|
|
|
goto done;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-14 14:59:35 +01:00
|
|
|
elapsed = (unsigned int)TICK2MSEC(clock() - start);
|
2018-11-08 16:05:54 +01:00
|
|
|
inhdr = (FAR struct icmpv6_echo_reply_s *)iobuffer;
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
if (inhdr->type == ICMPv6_ECHO_REPLY)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
if (ntohs(inhdr->id) != result.id)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_IDDIFF, ntohs(inhdr->id));
|
2017-10-24 19:19:41 +02:00
|
|
|
retry = true;
|
|
|
|
}
|
2018-11-08 16:05:54 +01:00
|
|
|
else if (ntohs(inhdr->seqno) > result.seqno)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_SEQNOBIG, ntohs(inhdr->seqno));
|
2017-10-24 19:19:41 +02:00
|
|
|
retry = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool verified = true;
|
|
|
|
int32_t pktdelay = elapsed;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
if (ntohs(inhdr->seqno) < result.seqno)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_SEQNOSMALL, ntohs(inhdr->seqno));
|
2017-10-24 19:19:41 +02:00
|
|
|
pktdelay += info->delay;
|
|
|
|
retry = true;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_I_ROUNDTRIP, pktdelay);
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
/* Verify the payload data */
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
if (nrecvd != result.outsize)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_RECVBIG, nrecvd);
|
2017-10-24 19:19:41 +02:00
|
|
|
verified = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
ptr = &iobuffer[SIZEOF_ICMPV6_ECHO_REPLY_S(0)];
|
2017-10-24 19:19:41 +02:00
|
|
|
ch = 0x20;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
for (i = 0; i < info->datalen; i++, ptr++)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
|
|
|
if (*ptr != ch)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_DATADIFF, 0);
|
2017-10-24 19:19:41 +02:00
|
|
|
verified = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++ch > 0x7e)
|
|
|
|
{
|
|
|
|
ch = 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only count the number of good replies */
|
|
|
|
|
|
|
|
if (verified)
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
result.nreplies++;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
icmp6_callback(&result, ICMPv6_W_TYPE, inhdr->type);
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 16:05:54 +01:00
|
|
|
while (retry && info->delay > elapsed && info->timeout > elapsed);
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
/* Wait if necessary to preserved the requested ping rate */
|
|
|
|
|
2018-03-14 14:59:35 +01:00
|
|
|
elapsed = (unsigned int)TICK2MSEC(clock() - start);
|
2017-10-24 19:19:41 +02:00
|
|
|
if (elapsed < info->delay)
|
|
|
|
{
|
|
|
|
struct timespec rqt;
|
|
|
|
unsigned int remaining;
|
|
|
|
unsigned int sec;
|
|
|
|
unsigned int frac; /* In deciseconds */
|
|
|
|
|
|
|
|
remaining = info->delay - elapsed;
|
|
|
|
sec = remaining / MSEC_PER_SEC;
|
|
|
|
frac = remaining - MSEC_PER_SEC * sec;
|
|
|
|
|
|
|
|
rqt.tv_sec = sec;
|
|
|
|
rqt.tv_nsec = frac * NSEC_PER_MSEC;
|
|
|
|
|
|
|
|
(void)nanosleep(&rqt, NULL);
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
outhdr.seqno = htons(++result.seqno);
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
2018-11-08 16:05:54 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
icmp6_callback(&result, ICMPv6_I_FINISH, TICK2MSEC(clock() - kickoff));
|
|
|
|
close(sockfd);
|
|
|
|
free(iobuffer);
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: show_usage
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void show_usage(FAR const char *progname, int exitcode) noreturn_function;
|
|
|
|
static void show_usage(FAR const char *progname, int exitcode)
|
|
|
|
{
|
2017-10-24 23:38:37 +02:00
|
|
|
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
|
2018-11-08 16:05:54 +01:00
|
|
|
printf("\nUsage: %s [-c <count>] [-i <interval>] [-W <timeout>] [-s <size>] <hostname>\n", progname);
|
2017-10-24 23:38:37 +02:00
|
|
|
printf(" %s -h\n", progname);
|
|
|
|
printf("\nWhere:\n");
|
|
|
|
printf(" <hostname> is either an IPv6 address or the name of the remote host\n");
|
|
|
|
printf(" that is requested the ICMPv6 ECHO reply.\n");
|
|
|
|
#else
|
2018-11-08 16:05:54 +01:00
|
|
|
printf("\nUsage: %s [-c <count>] [-i <interval>] [-W <timeout>] [-s <size>] <ip-address>\n", progname);
|
2017-10-24 19:19:41 +02:00
|
|
|
printf(" %s -h\n", progname);
|
|
|
|
printf("\nWhere:\n");
|
|
|
|
printf(" <ip-address> is the IPv6 address request the ICMPv6 ECHO reply.\n");
|
2017-10-24 23:38:37 +02:00
|
|
|
#endif
|
2017-10-24 19:19:41 +02:00
|
|
|
printf(" -c <count> determines the number of pings. Default %u.\n",
|
|
|
|
ICMPv6_NPINGS);
|
|
|
|
printf(" -i <interval> is the default delay between pings (milliseconds).\n");
|
|
|
|
printf(" Default %d.\n", ICMPv6_POLL_DELAY);
|
2018-11-08 16:05:54 +01:00
|
|
|
printf(" -W <timeout> is the timeout for wait response (milliseconds).\n");
|
|
|
|
printf(" Default %d.\n", ICMPv6_POLL_DELAY);
|
|
|
|
printf(" -s <size> specifies the number of data bytes to be sent. Default %u.\n",
|
|
|
|
ICMPv6_PING6_DATALEN);
|
2017-10-24 20:27:27 +02:00
|
|
|
printf(" -h shows this text and exits.\n");
|
2017-10-24 19:19:41 +02:00
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ping_result
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void ping6_result(FAR const struct ping6_result_s *result)
|
|
|
|
{
|
|
|
|
char strbuffer[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
switch (result->code)
|
|
|
|
{
|
|
|
|
case ICMPv6_E_HOSTIP:
|
|
|
|
fprintf(stderr, "ERROR: ping6_gethostip(%s) failed\n",
|
|
|
|
result->info->hostname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_MEMORY:
|
|
|
|
fprintf(stderr, "ERROR: Failed to allocate memory\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_SOCKET:
|
|
|
|
fprintf(stderr, "ERROR: socket() failed: %d\n", result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_I_BEGIN:
|
|
|
|
inet_ntop(AF_INET6, result->dest.s6_addr16, strbuffer, INET6_ADDRSTRLEN);
|
|
|
|
printf("PING6 %s: %u bytes of data\n",
|
|
|
|
strbuffer, result->info->datalen);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_SENDTO:
|
|
|
|
fprintf(stderr, "ERROR: sendto failed at seqno %u: %d\n",
|
|
|
|
result->seqno, result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_SENDSMALL:
|
|
|
|
fprintf(stderr, "ERROR: sendto returned %d, expected %u\n",
|
|
|
|
result->extra, result->outsize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_POLL:
|
|
|
|
fprintf(stderr, "ERROR: poll failed: %d\n", result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_TIMEOUT:
|
|
|
|
inet_ntop(AF_INET6, result->dest.s6_addr16, strbuffer,
|
|
|
|
INET6_ADDRSTRLEN);
|
|
|
|
printf("No response from %s: icmp_seq=%u time=%d ms\n",
|
|
|
|
strbuffer, result->seqno, result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_RECVFROM:
|
|
|
|
fprintf(stderr, "ERROR: recvfrom failed: %d\n", result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_E_RECVSMALL:
|
|
|
|
fprintf(stderr, "ERROR: short ICMP packet: %d\n", result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_IDDIFF:
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: Ignoring ICMP reply with ID %d. "
|
|
|
|
"Expected %u\n",
|
|
|
|
result->extra, result->id);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_SEQNOBIG:
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: Ignoring ICMP reply to sequence %d. "
|
|
|
|
"Expected <= %u\n",
|
|
|
|
result->extra, result->seqno);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_SEQNOSMALL:
|
|
|
|
fprintf(stderr, "WARNING: Received after timeout\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_I_ROUNDTRIP:
|
|
|
|
inet_ntop(AF_INET6, result->dest.s6_addr16, strbuffer,
|
|
|
|
INET6_ADDRSTRLEN);
|
|
|
|
printf("%ld bytes from %s icmp_seq=%u time=%u ms\n",
|
|
|
|
result->info->datalen, strbuffer, result->seqno,
|
|
|
|
result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_RECVBIG:
|
|
|
|
fprintf(stderr,
|
|
|
|
"WARNING: Ignoring ICMP reply with different payload "
|
|
|
|
"size: %d vs %u\n",
|
|
|
|
result->extra, result->outsize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_DATADIFF:
|
|
|
|
fprintf(stderr, "WARNING: Echoed data corrupted\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_W_TYPE:
|
|
|
|
fprintf(stderr, "WARNING: ICMP packet with unknown type: %d\n",
|
|
|
|
result->extra);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ICMPv6_I_FINISH:
|
|
|
|
if (result->nrequests > 0)
|
|
|
|
{
|
|
|
|
unsigned int tmp;
|
|
|
|
|
|
|
|
/* Calculate the percentage of lost packets */
|
|
|
|
|
|
|
|
tmp = (100 * (result->nrequests - result->nreplies) +
|
|
|
|
(result->nrequests >> 1)) /
|
|
|
|
result->nrequests;
|
|
|
|
|
|
|
|
printf("%u packets transmitted, %u received, %u%% packet loss, time %d ms\n",
|
|
|
|
result->nrequests, result->nreplies, tmp, result->extra);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 19:19:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-09-03 17:29:56 +02:00
|
|
|
#ifdef BUILD_MODULE
|
2017-10-24 19:19:41 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
#else
|
|
|
|
int ping6_main(int argc, char **argv)
|
|
|
|
#endif
|
|
|
|
{
|
2018-11-08 16:05:54 +01:00
|
|
|
struct ping6_info_s info;
|
2017-10-24 19:19:41 +02:00
|
|
|
FAR char *endptr;
|
|
|
|
int exitcode;
|
|
|
|
int option;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.count = ICMPv6_NPINGS;
|
|
|
|
info.datalen = ICMPv6_PING6_DATALEN;
|
|
|
|
info.delay = ICMPv6_POLL_DELAY;
|
|
|
|
info.timeout = ICMPv6_POLL_DELAY;
|
|
|
|
info.callback = ping6_result;
|
2017-10-24 19:19:41 +02:00
|
|
|
|
|
|
|
/* Parse command line options */
|
|
|
|
|
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
while ((option = getopt(argc, argv, ":c:i:W:s:h")) != ERROR)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
long count = strtol(optarg, &endptr, 10);
|
|
|
|
if (count < 1 || count > UINT16_MAX)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: <count> out of range: %ld\n", count);
|
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.count = (uint16_t)count;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
long delay = strtol(optarg, &endptr, 10);
|
2018-11-08 16:05:54 +01:00
|
|
|
if (delay < 1 || delay > UINT16_MAX)
|
2017-10-24 19:19:41 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: <interval> out of range: %ld\n", delay);
|
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.delay = (int16_t)delay;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'W':
|
|
|
|
{
|
|
|
|
long timeout = strtol(optarg, &endptr, 10);
|
|
|
|
if (timeout < 1 || timeout > UINT16_MAX)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: <timeout> out of range: %ld\n", timeout);
|
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.timeout = (int16_t)timeout;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
long datalen = strtol(optarg, &endptr, 10);
|
|
|
|
if (datalen < 1 || datalen > UINT16_MAX)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: <size> out of range: %ld\n", datalen);
|
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.datalen = (uint16_t)datalen;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
exitcode = EXIT_SUCCESS;
|
|
|
|
goto errout_with_usage;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
fprintf(stderr, "ERROR: Missing required argument\n");
|
|
|
|
goto errout_with_usage;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "ERROR: Unrecognized option\n");
|
|
|
|
goto errout_with_usage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be one final parameters remaining on the command line */
|
|
|
|
|
|
|
|
if (optind >= argc)
|
|
|
|
{
|
|
|
|
printf("ERROR: Missing required <ip-address> argument\n");
|
2018-11-08 16:05:54 +01:00
|
|
|
goto errout_with_usage;
|
2017-10-24 19:19:41 +02:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:05:54 +01:00
|
|
|
info.hostname = argv[optind];
|
|
|
|
icmp6_ping(&info);
|
2017-10-24 19:19:41 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
errout_with_usage:
|
2018-11-08 16:05:54 +01:00
|
|
|
optind = 0;
|
2017-10-24 19:19:41 +02:00
|
|
|
show_usage(argv[0], exitcode);
|
|
|
|
return exitcode; /* Not reachable */
|
|
|
|
}
|