mirror of
https://github.com/apache/nuttx-apps.git
synced 2025-10-20 04:26:04 +08:00
This commit removes the ping6 command from NSH and replaces it with the apps/system/ping6 built-in command. The NSH ping6 command had to be removed because it violated the portable POSIX OS interface. The apps/system/ping6 command uses the sem-standard IPPROTO_ICMP6 socket interface.
Squashed commit of the following: apps/system ping and ping6: Fix a backward test. apps/system/ping6: ping6 logic now builds without error. apps/system/ping and ping6: Minor clean-up to configuration settings. apps/system/ping6: Minor clean-up to be able to configure CONFIG_SYSTEM_PING6 apps/nshlib: Remove support for ping6 from NSH. apps/system/ping6: Add IPPROTO_ICMPv6 ping6. Initial commit is simply a clone of the IPPROTO_ICMP ping logic. Rename CONFIG_NET_ICMPv6_PING to CONFIG_NET_ICMPv6_SOCKET.
This commit is contained in:
@@ -53,7 +53,6 @@
|
||||
#include <sched.h>
|
||||
#include <fcntl.h> /* Needed for open */
|
||||
#include <dirent.h>
|
||||
#include <netdb.h> /* Needed for gethostbyname */
|
||||
#include <libgen.h> /* Needed for basename */
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
@@ -114,16 +113,10 @@
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#undef HAVE_PING6
|
||||
#undef HAVE_HWADDR
|
||||
#undef HAVE_EADDR
|
||||
#undef HAVE_RADIOADDR
|
||||
|
||||
#if defined(CONFIG_NET_ICMPv6) && defined(CONFIG_NET_ICMPv6_PING) && \
|
||||
!defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_NSH_DISABLE_PING6)
|
||||
# define HAVE_PING6 1
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_NET_ETHERNET)
|
||||
# define HAVE_HWADDR 1
|
||||
#elif defined(CONFIG_NET_6LOWPAN)
|
||||
@@ -136,10 +129,6 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* Size of the ECHO data */
|
||||
|
||||
#define DEFAULT_PING6_DATALEN 56
|
||||
|
||||
/* Get the larger value */
|
||||
|
||||
#ifndef MAX
|
||||
@@ -180,125 +169,10 @@ typedef int (*nsh_netdev_callback_t)(FAR struct nsh_vtbl_s *vtbl,
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_PING6
|
||||
static uint16_t g_pingid = 0;
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ping6_newid
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_PING6
|
||||
static uint16_t ping6_newid(void)
|
||||
{
|
||||
irqstate_t save = enter_critical_section();
|
||||
uint16_t ret = ++g_pingid;
|
||||
leave_critical_section(save);
|
||||
return ret;
|
||||
}
|
||||
#endif /* HAVE_PIN6 */
|
||||
|
||||
/****************************************************************************
|
||||
* Name: ping6_options
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_PING6
|
||||
static int ping6_options(FAR struct nsh_vtbl_s *vtbl,
|
||||
int argc, FAR char **argv,
|
||||
FAR int *count, FAR uint32_t *dsec, FAR char **staddr)
|
||||
{
|
||||
FAR const char *fmt = g_fmtarginvalid;
|
||||
bool badarg = false;
|
||||
int option;
|
||||
int tmp;
|
||||
|
||||
/* Get the ping6 options */
|
||||
|
||||
while ((option = getopt(argc, argv, ":c:i:")) != ERROR)
|
||||
{
|
||||
switch (option)
|
||||
{
|
||||
case 'c':
|
||||
tmp = atoi(optarg);
|
||||
if (tmp < 1 || tmp > 10000)
|
||||
{
|
||||
nsh_output(vtbl, g_fmtargrange, argv[0]);
|
||||
badarg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
*count = tmp;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'i':
|
||||
tmp = atoi(optarg);
|
||||
if (tmp < 1 || tmp >= 4294)
|
||||
{
|
||||
nsh_output(vtbl, g_fmtargrange, argv[0]);
|
||||
badarg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
*dsec = 10 * tmp;
|
||||
}
|
||||
break;
|
||||
|
||||
case ':':
|
||||
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
||||
badarg = true;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
default:
|
||||
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
||||
badarg = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* If a bad argument was encountered, then return without processing the
|
||||
* command
|
||||
*/
|
||||
|
||||
if (badarg)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* There should be exactly on parameter left on the command-line */
|
||||
|
||||
if (optind == argc - 1)
|
||||
{
|
||||
*staddr = argv[optind];
|
||||
}
|
||||
else if (optind >= argc)
|
||||
{
|
||||
fmt = g_fmttoomanyargs;
|
||||
goto errout;
|
||||
}
|
||||
else
|
||||
{
|
||||
fmt = g_fmtargrequired;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
return OK;
|
||||
|
||||
errout:
|
||||
nsh_output(vtbl, fmt, argv[0]);
|
||||
return ERROR;
|
||||
}
|
||||
#endif /* HAVE_PING6 */
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_statistics
|
||||
****************************************************************************/
|
||||
@@ -346,7 +220,7 @@ int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
|
||||
bool badarg = false;
|
||||
int option;
|
||||
|
||||
/* Get the ping6 options */
|
||||
/* Get the get/put options */
|
||||
|
||||
memset(args, 0, sizeof(struct tftpc_args_s));
|
||||
while ((option = getopt(argc, argv, ":bnf:h:")) != ERROR)
|
||||
@@ -471,68 +345,6 @@ errout:
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: nsh_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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_PING6
|
||||
static int nsh_gethostip(FAR char *hostname, FAR union ip_addr_u *ipaddr)
|
||||
{
|
||||
#ifdef CONFIG_LIBC_NETDB
|
||||
|
||||
/* Netdb support is enabled */
|
||||
|
||||
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)
|
||||
{
|
||||
memcpy(ipaddr->ipv6, he->h_addr, sizeof(net_ipv6addr_t));
|
||||
}
|
||||
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 */
|
||||
|
||||
int ret = inet_pton(AF_INET6, hostname, ipaddr->ipv6);
|
||||
|
||||
/* The inet_pton() function returns 1 if the conversion succeeds. It will
|
||||
* return 0 if the input is not a valid IPv4 dotted-decimal string or 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 */
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: wget_callback
|
||||
****************************************************************************/
|
||||
@@ -1316,123 +1128,6 @@ errout_invalid:
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: cmd_ping6
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_PING6
|
||||
int cmd_ping6(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
||||
{
|
||||
FAR char *staddr;
|
||||
struct in6_addr ipaddr;
|
||||
systime_t start;
|
||||
systime_t next;
|
||||
int32_t elapsed;
|
||||
uint32_t dsec = 10;
|
||||
uint32_t maxwait;
|
||||
uint16_t id;
|
||||
int count = 10;
|
||||
int seqno;
|
||||
int replies = 0;
|
||||
int ret;
|
||||
int tmp;
|
||||
int i;
|
||||
|
||||
/* Get the ping6 options */
|
||||
|
||||
ret = ping6_options(vtbl, argc, argv, &count, &dsec, &staddr);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Get the IP address in binary form */
|
||||
|
||||
ret = nsh_gethostip(staddr, (FAR union ip_addr_u *)&ipaddr);
|
||||
if (ret < 0)
|
||||
{
|
||||
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/* Get the ID to use */
|
||||
|
||||
id = ping6_newid();
|
||||
|
||||
/* The maximum wait for a response will be the larger of the inter-ping
|
||||
* time and the configured maximum round-trip time.
|
||||
*/
|
||||
|
||||
maxwait = MAX(dsec, CONFIG_NSH_MAX_ROUNDTRIP);
|
||||
|
||||
/* Loop for the specified count */
|
||||
|
||||
nsh_output(vtbl,
|
||||
"PING6 %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x %d bytes of data\n",
|
||||
ntohs(ipaddr.s6_addr16[0]), ntohs(ipaddr.s6_addr16[1]),
|
||||
ntohs(ipaddr.s6_addr16[2]), ntohs(ipaddr.s6_addr16[3]),
|
||||
ntohs(ipaddr.s6_addr16[4]), ntohs(ipaddr.s6_addr16[5]),
|
||||
ntohs(ipaddr.s6_addr16[6]), ntohs(ipaddr.s6_addr16[7]),
|
||||
DEFAULT_PING6_DATALEN);
|
||||
|
||||
start = clock_systimer();
|
||||
for (i = 1; i <= count; i++)
|
||||
{
|
||||
/* Send the ECHO request and wait for the response */
|
||||
|
||||
next = clock_systimer();
|
||||
seqno = icmpv6_ping(ipaddr.s6_addr16, id, i, DEFAULT_PING6_DATALEN, maxwait);
|
||||
|
||||
/* Was any response returned? We can tell if a non-negative sequence
|
||||
* number was returned.
|
||||
*/
|
||||
|
||||
if (seqno >= 0 && seqno <= i)
|
||||
{
|
||||
/* Get the elapsed time from the time that the request was
|
||||
* sent until the response was received. If we got a response
|
||||
* to an earlier request, then fudge the elapsed time.
|
||||
*/
|
||||
|
||||
elapsed = (int32_t)TICK2MSEC(clock_systimer() - next);
|
||||
if (seqno < i)
|
||||
{
|
||||
elapsed += 100 * dsec * (i - seqno);
|
||||
}
|
||||
|
||||
/* Report the receipt of the reply */
|
||||
|
||||
nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%ld ms\n",
|
||||
DEFAULT_PING6_DATALEN, staddr, seqno, (long)elapsed);
|
||||
replies++;
|
||||
}
|
||||
|
||||
/* Wait for the remainder of the interval. If the last seqno<i,
|
||||
* then this is a bad idea... we will probably lose the response
|
||||
* to the current request!
|
||||
*/
|
||||
|
||||
elapsed = (int32_t)TICK2DSEC(clock_systimer() - next);
|
||||
if (elapsed < dsec)
|
||||
{
|
||||
usleep(100000 * (dsec - elapsed));
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the total elapsed time */
|
||||
|
||||
elapsed = (int32_t)TICK2MSEC(clock_systimer() - start);
|
||||
|
||||
/* Calculate the percentage of lost packets */
|
||||
|
||||
tmp = (100*(count - replies) + (count >> 1)) / count;
|
||||
|
||||
nsh_output(vtbl, "%d packets transmitted, %d received, %d%% packet loss, time %ld ms\n",
|
||||
count, replies, tmp, (long)elapsed);
|
||||
return OK;
|
||||
}
|
||||
#endif /* CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING */
|
||||
|
||||
/****************************************************************************
|
||||
* Name: cmd_put
|
||||
****************************************************************************/
|
||||
|
Reference in New Issue
Block a user