2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_netcmds.c
|
|
|
|
*
|
2015-01-23 19:49:49 +01:00
|
|
|
* Copyright (C) 2007-2012, 2014-2015 Gregory Nutt. All rights reserved.
|
2012-02-02 17:04:09 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
|
|
|
#include <sys/stat.h> /* Needed for open */
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <fcntl.h> /* Needed for open */
|
|
|
|
#include <libgen.h> /* Needed for basename */
|
|
|
|
#include <errno.h>
|
2012-10-20 15:47:30 +02:00
|
|
|
#include <debug.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2013-01-18 17:37:37 +01:00
|
|
|
#include <net/ethernet.h>
|
2014-01-21 17:21:45 +01:00
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2013-01-18 17:37:37 +01:00
|
|
|
#include <netinet/ether.h>
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <nuttx/clock.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/netstats.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2015-01-23 19:49:49 +01:00
|
|
|
#include <nuttx/net/icmp.h>
|
|
|
|
#include <nuttx/net/icmpv6.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
|
2014-08-07 20:35:24 +02:00
|
|
|
!defined(CONFIG_DISABLE_SIGNALS)
|
2014-07-03 00:04:25 +02:00
|
|
|
# include <apps/netutils/netlib.h>
|
2014-04-11 20:41:13 +02:00
|
|
|
# include <apps/netutils/dnsclient.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2014-07-03 00:04:25 +02:00
|
|
|
# include <apps/netutils/netlib.h>
|
2011-03-19 18:29:36 +01:00
|
|
|
# include <apps/netutils/tftp.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
# ifndef CONFIG_NSH_DISABLE_WGET
|
2014-07-03 00:04:25 +02:00
|
|
|
# include <apps/netutils/netlib.h>
|
2011-03-19 18:29:36 +01:00
|
|
|
# include <apps/netutils/webclient.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-11-27 17:26:54 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
|
|
|
# ifdef CONFIG_HAVE_GETHOSTBYNAME
|
|
|
|
# include <netdb.h>
|
|
|
|
# else
|
2014-04-11 20:41:13 +02:00
|
|
|
# include <apps/netutils/dnsclient.h>
|
2012-11-27 17:26:54 +01:00
|
|
|
# endif
|
|
|
|
# include <apps/netutils/dhcpc.h>
|
2012-10-20 15:47:30 +02:00
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#include "nsh.h"
|
2012-02-02 17:04:09 +01:00
|
|
|
#include "nsh_console.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-01-23 19:49:49 +01:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
#undef HAVE_PING
|
|
|
|
#undef HAVE_PING6
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
|
|
|
|
!defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_NSH_DISABLE_PING)
|
|
|
|
# define HAVE_PING
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ICMPv6) && defined(CONFIG_NET_ICMPv6_PING) && \
|
|
|
|
!defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_NSH_DISABLE_PING6)
|
|
|
|
# define HAVE_PING6
|
|
|
|
#endif
|
|
|
|
|
2012-10-20 16:15:59 +02:00
|
|
|
/* Size of the ECHO data */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#define DEFAULT_PING_DATALEN 56
|
|
|
|
|
2012-10-20 16:15:59 +02:00
|
|
|
/* Get the larger value */
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MAX(a,b) (a > b ? a : b)
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
struct tftpc_args_s
|
|
|
|
{
|
2013-12-29 22:02:51 +01:00
|
|
|
bool binary; /* true:binary ("octet") false:text ("netascii") */
|
2011-03-18 20:46:25 +01:00
|
|
|
bool allocated; /* true: destpath is allocated */
|
|
|
|
char *destpath; /* Path at destination */
|
|
|
|
const char *srcpath; /* Path at src */
|
|
|
|
in_addr_t ipaddr; /* Host IP address */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
#if defined(HAVE_PING) || defined(HAVE_PING6)
|
2011-03-18 20:46:25 +01:00
|
|
|
static uint16_t g_pingid = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ping_newid
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
#if defined(HAVE_PING) || defined(HAVE_PING6)
|
|
|
|
static uint16_t ping_newid(void)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
irqstate_t save = irqsave();
|
|
|
|
uint16_t ret = ++g_pingid;
|
|
|
|
irqrestore(save);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-01-23 19:49:49 +01:00
|
|
|
#endif /* HAVE_PING || HAVE_PINg */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ping_options
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(HAVE_PING) || defined(HAVE_PING6)
|
|
|
|
static int ping_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 ping 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_PING || HAVE_PING6 */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-26 17:32:39 +02:00
|
|
|
* Name: net_statistics
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#if defined(CONFIG_NET_STATISTICS) && !defined(CONFIG_NSH_DISABLE_IFCONFIG)
|
2014-06-26 17:32:39 +02:00
|
|
|
static inline void net_statistics(FAR struct nsh_vtbl_s *vtbl)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-24 15:26:12 +01:00
|
|
|
/* Headings */
|
|
|
|
|
|
|
|
nsh_output(vtbl, " ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " IPv4");
|
|
|
|
#endif
|
2015-01-24 15:38:13 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " IPv6");
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " TCP");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " UDP");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
|
|
|
nsh_output(vtbl, " ICMP");
|
2015-01-24 15:26:12 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
2015-01-24 16:04:28 +01:00
|
|
|
nsh_output(vtbl, " ICMPv6");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
|
|
|
/* Received packets */
|
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, "Received ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv4.recv);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv6.recv);
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.tcp.recv);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.udp.recv);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmp.recv);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmpv6.recv);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
|
|
|
/* Dropped packets */
|
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, "Dropped ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv4.drop);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv6.drop);
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.tcp.drop);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.udp.drop);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmp.drop);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmpv6.drop);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
/* Dropped IP packets */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-01-24 16:11:12 +01:00
|
|
|
nsh_output(vtbl, " IPv4 VHL: %04x Frg: %04x\n",
|
2015-01-24 15:26:12 +01:00
|
|
|
g_netstats.ipv4.vhlerr, g_netstats.ipv4.fragerr);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " IPv6 VHL: %04x\n",
|
|
|
|
g_netstats.ipv6.vhlerr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Checksum errors */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " Checksum ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv4.chkerr);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " ----");
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.tcp.chkerr);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.udp.chkerr);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " ----");
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
nsh_output(vtbl, " ----");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 16:11:12 +01:00
|
|
|
nsh_output(vtbl, " TCP ACK: %04x SYN: %04x\n",
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.ackerr, g_netstats.tcp.syndrop);
|
2015-01-24 16:11:12 +01:00
|
|
|
nsh_output(vtbl, " RST: %04x %04x\n",
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.rst, g_netstats.tcp.synrst);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
/* Prototype errors */
|
|
|
|
|
|
|
|
nsh_output(vtbl, " Type ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv4.protoerr);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv6.protoerr);
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " ----");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " ----");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmp.typeerr);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmpv6.typeerr);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
|
|
|
/* Sent packets */
|
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, "Sent ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv4.sent);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.ipv6.sent);
|
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.tcp.sent);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.udp.sent);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmp.sent);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
nsh_output(vtbl, " %04x", g_netstats.icmpv6.sent);
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
|
2015-01-24 15:26:12 +01:00
|
|
|
/* TCP retransmissions */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " Rexmit ");
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
nsh_output(vtbl, " ----");
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
nsh_output(vtbl, " ----");
|
|
|
|
#endif
|
2015-01-24 16:11:12 +01:00
|
|
|
nsh_output(vtbl, " %04x", g_netstats.tcp.rexmit);
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " ----");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_ICMP
|
2015-01-24 15:26:12 +01:00
|
|
|
nsh_output(vtbl, " ----");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
2015-01-24 15:26:12 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6
|
|
|
|
nsh_output(vtbl, " ----");
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
nsh_output(vtbl, "\n");
|
2015-01-24 15:26:12 +01:00
|
|
|
#endif /* CONFIG_NET_TCP */
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#else
|
2014-06-26 17:32:39 +02:00
|
|
|
# define net_statistics(vtbl)
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ifconfig_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-29 16:33:45 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_IFUPDOWN) || !defined(CONFIG_NSH_DISABLE_IFCONFIG)
|
|
|
|
static int ifconfig_callback(FAR struct net_driver_s *dev, void *arg)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
struct nsh_vtbl_s *vtbl = (struct nsh_vtbl_s*)arg;
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2011-03-18 20:46:25 +01:00
|
|
|
struct in_addr addr;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
char addrstr[INET6_ADDRSTRLEN];
|
2015-02-06 17:23:15 +01:00
|
|
|
uint8_t preflen;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2014-01-21 17:21:45 +01:00
|
|
|
uint8_t iff;
|
|
|
|
const char *status;
|
2012-11-04 19:54:04 +01:00
|
|
|
int ret;
|
|
|
|
|
2014-07-03 00:52:02 +02:00
|
|
|
ret = netlib_getifstatus(dev->d_ifname, &iff);
|
2012-11-04 19:54:04 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "\tGet %s interface flags error: %d\n",
|
|
|
|
dev->d_ifname, ret);
|
|
|
|
}
|
|
|
|
|
2014-01-21 17:21:45 +01:00
|
|
|
if (iff & IFF_RUNNING)
|
|
|
|
{
|
|
|
|
status = "RUNNING";
|
|
|
|
}
|
|
|
|
else if (iff & IFF_UP)
|
|
|
|
{
|
|
|
|
status = "UP";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = "DOWN";
|
|
|
|
}
|
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
2014-06-27 20:46:54 +02:00
|
|
|
/* REVISIT: How will we handle Ethernet and SLIP networks together? */
|
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
nsh_output(vtbl, "%s\tLink encap: Ethernet HWaddr %s at %s\n",
|
2014-01-21 17:21:45 +01:00
|
|
|
dev->d_ifname, ether_ntoa(&dev->d_mac), status);
|
2015-01-19 16:59:43 +01:00
|
|
|
#elif defined(CONFIG_NET_SLIP)
|
|
|
|
nsh_output(vtbl, "%s\tLink encap:SLIP\n");
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2015-02-06 17:23:15 +01:00
|
|
|
/* Show the IPv4 address */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
addr.s_addr = dev->d_ipaddr;
|
2015-01-19 16:59:43 +01:00
|
|
|
nsh_output(vtbl, "\tinet addr:%s ", inet_ntoa(addr));
|
2012-11-04 19:54:04 +01:00
|
|
|
|
2015-02-06 17:23:15 +01:00
|
|
|
/* Show the IPv4 default router address */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
addr.s_addr = dev->d_draddr;
|
|
|
|
nsh_output(vtbl, "DRaddr:%s ", inet_ntoa(addr));
|
2012-11-04 19:54:04 +01:00
|
|
|
|
2015-02-06 17:23:15 +01:00
|
|
|
/* Show the IPv4 network mask */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
addr.s_addr = dev->d_netmask;
|
2012-11-04 19:54:04 +01:00
|
|
|
nsh_output(vtbl, "Mask:%s\n", inet_ntoa(addr));
|
|
|
|
|
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
2014-04-11 20:25:32 +02:00
|
|
|
dns_getserver(&addr);
|
2012-11-04 19:54:04 +01:00
|
|
|
nsh_output(vtbl, "\tDNSaddr:%s\n", inet_ntoa(addr));
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-02-06 17:23:15 +01:00
|
|
|
/* Convert the 128 network mask to a human friendly prefix length */
|
|
|
|
|
|
|
|
preflen = netlib_ipv6netmask2prefix(dev->d_ipv6netmask);
|
|
|
|
|
|
|
|
/* Show the assigned IPv6 address */
|
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
if (inet_ntop(AF_INET6, dev->d_ipv6addr, addrstr, INET6_ADDRSTRLEN))
|
|
|
|
{
|
2015-02-06 17:23:15 +01:00
|
|
|
nsh_output(vtbl, "\tinet6 addr:%s/%d\n", addrstr, preflen);
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
|
|
|
|
2015-02-06 17:23:15 +01:00
|
|
|
/* REVISIT: Show the IPv6 default router address */
|
2015-01-19 16:59:43 +01:00
|
|
|
|
2015-02-06 17:23:15 +01:00
|
|
|
if (inet_ntop(AF_INET6, dev->d_ipv6draddr, addrstr, INET6_ADDRSTRLEN))
|
2015-01-19 16:59:43 +01:00
|
|
|
{
|
2015-02-06 17:23:15 +01:00
|
|
|
nsh_output(vtbl, "\tinet6 DRaddr:%s/%d\n", addrstr, preflen);
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
|
|
|
|
2015-02-06 17:23:15 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPCv6) || defined(CONFIG_NSH_DNS)
|
2015-01-19 16:59:43 +01:00
|
|
|
# warning Missing logic
|
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
nsh_output(vtbl, "\n");
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2015-01-29 16:33:45 +01:00
|
|
|
#endif /* !CONFIG_NSH_DISABLE_IFUPDOWN || !CONFIG_NSH_DISABLE_IFCONFIG */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tftpc_parseargs
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
|
|
|
int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv,
|
|
|
|
struct tftpc_args_s *args)
|
|
|
|
{
|
|
|
|
FAR const char *fmt = g_fmtarginvalid;
|
|
|
|
bool badarg = false;
|
|
|
|
int option;
|
|
|
|
|
|
|
|
/* Get the ping options */
|
|
|
|
|
|
|
|
memset(args, 0, sizeof(struct tftpc_args_s));
|
|
|
|
while ((option = getopt(argc, argv, ":bnf:h:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'b':
|
|
|
|
args->binary = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
args->binary = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
args->destpath = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
2014-07-03 00:04:25 +02:00
|
|
|
if (!netlib_ipaddrconv(optarg, (FAR unsigned char*)&args->ipaddr))
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* There should be exactly one parameter left on the command-line */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
if (optind == argc-1)
|
|
|
|
{
|
|
|
|
args->srcpath = argv[optind];
|
|
|
|
}
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
/* optind == argc means that there is nothing left on the command-line */
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
else if (optind >= argc)
|
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
fmt = g_fmtargrequired;
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
/* optind < argc-1 means that there are too many arguments on the
|
|
|
|
* command-line
|
|
|
|
*/
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
else
|
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
fmt = g_fmttoomanyargs;
|
2011-03-18 20:46:25 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The HOST IP address is also required */
|
|
|
|
|
|
|
|
if (!args->ipaddr)
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the destpath was not provided, then we have do a little work. */
|
|
|
|
|
|
|
|
if (!args->destpath)
|
|
|
|
{
|
|
|
|
char *tmp1;
|
|
|
|
char *tmp2;
|
|
|
|
|
|
|
|
/* Copy the srcpath... baseanme might modify it */
|
|
|
|
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
tmp1 = strdup(args->srcpath);
|
|
|
|
if (!tmp1)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the basename of the srcpath */
|
|
|
|
|
|
|
|
tmp2 = basename(tmp1);
|
|
|
|
if (!tmp2)
|
|
|
|
{
|
|
|
|
free(tmp1);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use that basename as the destpath */
|
|
|
|
|
|
|
|
args->destpath = strdup(tmp2);
|
|
|
|
free(tmp1);
|
|
|
|
if (!args->destpath)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2015-01-16 19:30:18 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
args->allocated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
nsh_output(vtbl, fmt, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: wget_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_WGET
|
2011-03-18 20:46:25 +01:00
|
|
|
static void wget_callback(FAR char **buffer, int offset, int datend,
|
|
|
|
FAR int *buflen, FAR void *arg)
|
|
|
|
{
|
|
|
|
(void)write((int)arg, &((*buffer)[offset]), datend - offset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_get
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_GET
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct tftpc_args_s args;
|
|
|
|
char *fullpath;
|
|
|
|
|
|
|
|
/* Parse the input parameter list */
|
|
|
|
|
|
|
|
if (tftpc_parseargs(vtbl, argc, argv, &args) != OK)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the full path to the local file */
|
|
|
|
|
2014-12-11 01:37:43 +01:00
|
|
|
fullpath = nsh_getfullpath(vtbl, args.destpath);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Then perform the TFTP get operation */
|
|
|
|
|
|
|
|
if (tftpget(args.srcpath, fullpath, args.ipaddr, args.binary) != OK)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "tftpget", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release any allocated memory */
|
|
|
|
|
|
|
|
if (args.allocated)
|
|
|
|
{
|
|
|
|
free(args.destpath);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
free(fullpath);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-11-04 19:54:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ifup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_IFUPDOWN
|
|
|
|
int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *intf = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "Please select nic_name:\n");
|
|
|
|
netdev_foreach(ifconfig_callback, vtbl);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
intf = argv[1];
|
2014-07-03 00:52:02 +02:00
|
|
|
ret = netlib_ifup(intf);
|
2012-11-04 19:54:04 +01:00
|
|
|
nsh_output(vtbl, "ifup %s...%s\n", intf, (ret == OK) ? "OK" : "Failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ifdown
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_IFUPDOWN
|
|
|
|
int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR char *intf = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, "Please select nic_name:\n");
|
|
|
|
netdev_foreach(ifconfig_callback, vtbl);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
intf = argv[1];
|
2014-07-03 00:52:02 +02:00
|
|
|
ret = netlib_ifdown(intf);
|
2012-11-04 19:54:04 +01:00
|
|
|
nsh_output(vtbl, "ifdown %s...%s\n", intf, (ret == OK) ? "OK" : "Failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ifconfig
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_IFCONFIG
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2011-05-21 20:25:31 +02:00
|
|
|
struct in_addr addr;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct in6_addr addr6;
|
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
in_addr_t gip;
|
|
|
|
int i;
|
|
|
|
FAR char *intf = NULL;
|
|
|
|
FAR char *hostip = NULL;
|
|
|
|
FAR char *gwip = NULL;
|
|
|
|
FAR char *mask = NULL;
|
|
|
|
FAR char *tmp = NULL;
|
2014-11-15 20:52:39 +01:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2012-11-04 19:54:04 +01:00
|
|
|
FAR char *hw = NULL;
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
2013-09-17 18:48:30 +02:00
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
2012-11-04 19:54:04 +01:00
|
|
|
FAR char *dns = NULL;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
bool inet6 = false;
|
2013-09-17 18:48:30 +02:00
|
|
|
#endif
|
2012-11-27 17:26:54 +01:00
|
|
|
bool badarg = false;
|
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#if defined(CONFIG_NSH_DHCPC)
|
|
|
|
FAR void *handle;
|
|
|
|
#endif
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
/* With one or no arguments, ifconfig simply shows the status of Ethernet
|
2011-05-21 20:25:31 +02:00
|
|
|
* device:
|
|
|
|
*
|
|
|
|
* ifconfig
|
|
|
|
* ifconfig [nic_name]
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (argc <= 2)
|
|
|
|
{
|
2012-03-13 20:21:04 +01:00
|
|
|
netdev_foreach(ifconfig_callback, vtbl);
|
2014-06-26 17:32:39 +02:00
|
|
|
net_statistics(vtbl);
|
2012-03-13 20:21:04 +01:00
|
|
|
return OK;
|
2011-05-21 20:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If both the network interface name and an IP address are supplied as
|
2013-12-29 22:02:51 +01:00
|
|
|
* arguments, then ifconfig will set the address of the Ethernet device:
|
2011-05-21 20:25:31 +02:00
|
|
|
*
|
|
|
|
* ifconfig nic_name ip_address
|
|
|
|
*/
|
|
|
|
|
2012-11-04 19:54:04 +01:00
|
|
|
if (argc > 2)
|
|
|
|
{
|
2014-04-12 21:28:22 +02:00
|
|
|
for (i = 0; i < argc; i++)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
|
|
|
if (i == 1)
|
|
|
|
{
|
|
|
|
intf = argv[i];
|
|
|
|
}
|
|
|
|
else if (i == 2)
|
|
|
|
{
|
|
|
|
hostip = argv[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = argv[i];
|
|
|
|
if (!strcmp(tmp, "dr") || !strcmp(tmp, "gw") || !strcmp(tmp, "gateway"))
|
|
|
|
{
|
|
|
|
if (argc-1 >= i+1)
|
|
|
|
{
|
|
|
|
gwip = argv[i+1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-12 20:53:19 +02:00
|
|
|
else if (!strcmp(tmp, "netmask"))
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
|
|
|
if (argc-1 >= i+1)
|
|
|
|
{
|
|
|
|
mask = argv[i+1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
else if (!strcmp(tmp, "inet"))
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
inet6 = false;
|
|
|
|
#elif !defined(CONFIG_NET_IPv4)
|
|
|
|
badarg = true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (!strcmp(tmp, "inet6"))
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
|
|
|
|
inet6 = true;
|
|
|
|
#elif !defined(CONFIG_NET_IPv6)
|
|
|
|
badarg = true;
|
|
|
|
#endif
|
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2014-06-27 20:46:54 +02:00
|
|
|
/* REVISIT: How will we handle Ethernet and SLIP networks together? */
|
|
|
|
|
2013-09-17 18:48:30 +02:00
|
|
|
else if (!strcmp(tmp, "hw"))
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
|
|
|
if (argc-1>=i+1)
|
|
|
|
{
|
|
|
|
hw = argv[i+1];
|
|
|
|
i++;
|
2014-07-03 00:04:25 +02:00
|
|
|
badarg = !netlib_hwmacconv(hw, mac);
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
|
|
|
|
2013-09-17 18:48:30 +02:00
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
|
|
|
else if (!strcmp(tmp, "dns"))
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
|
|
|
if (argc-1 >= i+1)
|
|
|
|
{
|
|
|
|
dns = argv[i+1];
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-17 18:48:30 +02:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2013-12-29 22:02:51 +01:00
|
|
|
/* Set Hardware Ethernet MAC address */
|
2014-06-27 20:46:54 +02:00
|
|
|
/* REVISIT: How will we handle Ethernet and SLIP networks together? */
|
2012-11-04 19:54:04 +01:00
|
|
|
|
|
|
|
if (hw)
|
|
|
|
{
|
|
|
|
ndbg("HW MAC: %s\n", hw);
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr(intf, mac);
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
/* Set IP address */
|
2012-11-27 17:26:54 +01:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (inet6)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#warning Missing Logic
|
|
|
|
UNUSED(addr6);
|
|
|
|
UNUSED(gip);
|
|
|
|
UNUSED(hostip);
|
2012-11-27 17:26:54 +01:00
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2012-11-27 17:26:54 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC)
|
|
|
|
if (!strcmp(hostip, "dhcp"))
|
|
|
|
{
|
|
|
|
/* Set DHCP addr */
|
2012-11-27 17:26:54 +01:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
ndbg("DHCPC Mode\n");
|
|
|
|
gip = addr.s_addr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Set host IP address */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
ndbg("Host IP: %s\n", hostip);
|
|
|
|
gip = addr.s_addr = inet_addr(hostip);
|
|
|
|
}
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
netlib_set_ipv4addr(intf, &addr);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (inet6)
|
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#warning Missing Logic
|
|
|
|
UNUSED(gwip);
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2012-11-04 19:54:04 +01:00
|
|
|
else
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
/* Set gateway */
|
|
|
|
|
|
|
|
if (gwip)
|
2012-11-27 17:26:54 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
ndbg("Gateway: %s\n", gwip);
|
|
|
|
gip = addr.s_addr = inet_addr(gwip);
|
2012-11-27 17:26:54 +01:00
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gip)
|
|
|
|
{
|
|
|
|
ndbg("Gateway: default\n");
|
|
|
|
gip = NTOHL(gip);
|
|
|
|
gip &= ~0x000000ff;
|
|
|
|
gip |= 0x00000001;
|
|
|
|
gip = HTONL(gip);
|
|
|
|
}
|
2012-11-27 17:26:54 +01:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
addr.s_addr = gip;
|
|
|
|
}
|
2012-11-04 19:54:04 +01:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
netlib_set_dripv4addr(intf, &addr);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2012-11-04 19:54:04 +01:00
|
|
|
|
|
|
|
/* Set network mask */
|
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (inet6)
|
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#warning Missing Logic
|
|
|
|
UNUSED(mask);
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2012-11-04 19:54:04 +01:00
|
|
|
else
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
if (mask)
|
|
|
|
{
|
|
|
|
ndbg("Netmask: %s\n",mask);
|
|
|
|
addr.s_addr = inet_addr(mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ndbg("Netmask: Default\n");
|
|
|
|
addr.s_addr = inet_addr("255.255.255.0");
|
|
|
|
}
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
netlib_set_ipv4netmask(intf, &addr);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2012-11-04 19:54:04 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (inet6)
|
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#warning Missing Logic
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2012-11-04 19:54:04 +01:00
|
|
|
else
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
if (dns)
|
|
|
|
{
|
|
|
|
ndbg("DNS: %s\n", dns);
|
|
|
|
addr.s_addr = inet_addr(dns);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ndbg("DNS: Default\n");
|
|
|
|
addr.s_addr = gip;
|
|
|
|
}
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
dns_setserver(&addr);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
#endif /* CONFIG_NSH_DHCPC || CONFIG_NSH_DNS */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2012-11-27 17:26:54 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC)
|
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
if (!gip)
|
|
|
|
{
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_getmacaddr("eth0", mac);
|
2012-11-27 17:26:54 +01:00
|
|
|
|
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
|
|
|
handle = dhcpc_open(&mac, IFHWADDRLEN);
|
|
|
|
|
2015-01-16 19:30:18 +01:00
|
|
|
/* Get an IP address. Note that there is no logic for renewing the IP
|
|
|
|
* address in this example. The address should be renewed in
|
|
|
|
* ds.lease_time/2 seconds.
|
2012-11-27 17:26:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
|
|
|
|
(void)dhcpc_request(handle, &ds);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr("eth0", &ds.ipaddr);
|
2012-11-27 17:26:54 +01:00
|
|
|
|
|
|
|
if (ds.netmask.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask("eth0", &ds.netmask);
|
2012-11-27 17:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.default_router.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr("eth0", &ds.default_router);
|
2012-11-27 17:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ds.dnsaddr.s_addr != 0)
|
|
|
|
{
|
2014-04-11 20:25:32 +02:00
|
|
|
dns_setserver(&ds.dnsaddr);
|
2012-11-27 17:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dhcpc_close(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ping
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
#ifdef HAVE_PING
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
FAR char *staddr;
|
2015-01-16 19:30:18 +01:00
|
|
|
in_addr_t ipaddr;
|
2011-03-18 20:46:25 +01:00
|
|
|
uint32_t start;
|
|
|
|
uint32_t next;
|
|
|
|
uint32_t dsec = 10;
|
2012-10-20 16:15:59 +02:00
|
|
|
uint32_t maxwait;
|
2011-03-18 20:46:25 +01:00
|
|
|
uint16_t id;
|
|
|
|
int count = 10;
|
|
|
|
int seqno;
|
|
|
|
int replies = 0;
|
|
|
|
int elapsed;
|
2015-01-23 19:49:49 +01:00
|
|
|
int ret;
|
2011-03-18 20:46:25 +01:00
|
|
|
int tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the ping options */
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
ret = ping_options(vtbl, argc, argv, &count, &dsec, &staddr);
|
|
|
|
if (ret < 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
/* Get the IP address in binary form */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
if (dns_gethostip(staddr, &ipaddr) < 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
/* Get the ID to use */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
id = ping_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, "PING %d.%d.%d.%d %d bytes of data\n",
|
|
|
|
(ipaddr ) & 0xff, (ipaddr >> 8 ) & 0xff,
|
|
|
|
(ipaddr >> 16 ) & 0xff, (ipaddr >> 24 ) & 0xff,
|
|
|
|
DEFAULT_PING_DATALEN);
|
|
|
|
|
|
|
|
start = clock_systimer();
|
|
|
|
for (i = 1; i <= count; i++)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
/* Send the ECHO request and wait for the response */
|
|
|
|
|
|
|
|
next = clock_systimer();
|
|
|
|
seqno = icmp_ping(ipaddr, id, i, DEFAULT_PING_DATALEN, maxwait);
|
|
|
|
|
|
|
|
/* Was any response returned? We can tell if a non-negative sequence
|
|
|
|
* number was returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (seqno >= 0 && seqno <= i)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
/* 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 = 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=%d ms\n",
|
|
|
|
DEFAULT_PING_DATALEN, staddr, seqno, 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 = TICK2DSEC(clock_systimer() - next);
|
|
|
|
if (elapsed < dsec)
|
|
|
|
{
|
|
|
|
usleep(100000 * (dsec - elapsed));
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 19:49:49 +01:00
|
|
|
|
|
|
|
/* Get the total elapsed time */
|
|
|
|
|
|
|
|
elapsed = 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 %d ms\n",
|
|
|
|
count, replies, tmp, elapsed);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_PING */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
uint32_t start;
|
|
|
|
uint32_t next;
|
|
|
|
uint32_t dsec = 10;
|
|
|
|
uint32_t maxwait;
|
|
|
|
uint16_t id;
|
|
|
|
int count = 10;
|
|
|
|
int seqno;
|
|
|
|
int replies = 0;
|
|
|
|
int elapsed;
|
|
|
|
int ret;
|
|
|
|
int tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the ping options */
|
|
|
|
|
|
|
|
ret = ping_options(vtbl, argc, argv, &count, &dsec, &staddr);
|
|
|
|
if (ret < 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2015-01-23 19:49:49 +01:00
|
|
|
|
|
|
|
/* Get the IP address in binary form
|
|
|
|
* REVISIT: DNS hostname look-up not yet supported
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
ret = dns_gethostip(staddr, &ipaddr);
|
|
|
|
#else
|
|
|
|
ret = inet_pton(AF_INET6, staddr, &ipaddr);
|
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-23 19:49:49 +01:00
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the ID to use */
|
|
|
|
|
|
|
|
id = ping_newid();
|
|
|
|
|
2015-01-16 19:30:18 +01:00
|
|
|
/* The maximum wait for a response will be the larger of the inter-ping
|
|
|
|
* time and the configured maximum round-trip time.
|
2012-10-20 16:15:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
maxwait = MAX(dsec, CONFIG_NSH_MAX_ROUNDTRIP);
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Loop for the specified count */
|
|
|
|
|
2015-01-23 19:49:49 +01:00
|
|
|
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]),
|
2015-01-23 21:40:09 +01:00
|
|
|
DEFAULT_PING_DATALEN);
|
2012-10-20 15:47:30 +02:00
|
|
|
|
2013-12-29 22:02:51 +01:00
|
|
|
start = clock_systimer();
|
2011-03-18 20:46:25 +01:00
|
|
|
for (i = 1; i <= count; i++)
|
|
|
|
{
|
|
|
|
/* Send the ECHO request and wait for the response */
|
|
|
|
|
2013-12-29 22:02:51 +01:00
|
|
|
next = clock_systimer();
|
2015-01-23 19:49:49 +01:00
|
|
|
seqno = icmpv6_ping(ipaddr.s6_addr16, id, i, DEFAULT_PING_DATALEN, maxwait);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Was any response returned? We can tell if a non-negative sequence
|
|
|
|
* number was returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (seqno >= 0 && seqno <= i)
|
|
|
|
{
|
2012-10-20 16:15:59 +02:00
|
|
|
/* Get the elapsed time from the time that the request was
|
2011-03-18 20:46:25 +01:00
|
|
|
* sent until the response was received. If we got a response
|
2015-01-16 19:30:18 +01:00
|
|
|
* to an earlier request, then fudge the elapsed time.
|
2011-03-18 20:46:25 +01:00
|
|
|
*/
|
|
|
|
|
2013-12-29 22:02:51 +01:00
|
|
|
elapsed = TICK2MSEC(clock_systimer() - next);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (seqno < i)
|
|
|
|
{
|
2012-10-20 16:15:59 +02:00
|
|
|
elapsed += 100 * dsec * (i - seqno);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Report the receipt of the reply */
|
|
|
|
|
|
|
|
nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%d ms\n",
|
|
|
|
DEFAULT_PING_DATALEN, staddr, seqno, 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!
|
|
|
|
*/
|
|
|
|
|
2013-12-29 22:02:51 +01:00
|
|
|
elapsed = TICK2DSEC(clock_systimer() - next);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (elapsed < dsec)
|
|
|
|
{
|
2012-10-20 16:15:59 +02:00
|
|
|
usleep(100000 * (dsec - elapsed));
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the total elapsed time */
|
|
|
|
|
2013-12-29 22:02:51 +01:00
|
|
|
elapsed = TICK2MSEC(clock_systimer() - start);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* 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 %d ms\n",
|
|
|
|
count, replies, tmp, elapsed);
|
|
|
|
return OK;
|
|
|
|
}
|
2015-01-23 19:49:49 +01:00
|
|
|
#endif /* CONFIG_NET_ICMPv6 && CONFIG_NET_ICMPv6_PING */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_put
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PUT
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_put(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct tftpc_args_s args;
|
|
|
|
char *fullpath;
|
|
|
|
|
|
|
|
/* Parse the input parameter list */
|
|
|
|
|
|
|
|
if (tftpc_parseargs(vtbl, argc, argv, &args) != OK)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the full path to the local file */
|
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, args.srcpath);
|
|
|
|
|
|
|
|
/* Then perform the TFTP put operation */
|
|
|
|
|
|
|
|
if (tftpput(fullpath, args.destpath, args.ipaddr, args.binary) != OK)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "tftpput", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release any allocated memory */
|
|
|
|
|
|
|
|
if (args.allocated)
|
|
|
|
{
|
|
|
|
free(args.destpath);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
free(fullpath);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_wget
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_WGET
|
2011-03-18 20:46:25 +01:00
|
|
|
int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *localfile = NULL;
|
|
|
|
char *allocfile = NULL;
|
|
|
|
char *buffer = NULL;
|
|
|
|
char *fullpath = NULL;
|
|
|
|
char *url;
|
|
|
|
const char *fmt;
|
|
|
|
bool badarg = false;
|
|
|
|
int option;
|
|
|
|
int fd = -1;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the wget options */
|
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, ":o:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'o':
|
|
|
|
localfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:30:18 +01:00
|
|
|
/* If a bad argument was encountered, then return without processing the
|
|
|
|
* command
|
|
|
|
*/
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be exactly on parameter left on the command-line */
|
|
|
|
|
|
|
|
if (optind == argc-1)
|
|
|
|
{
|
|
|
|
url = argv[optind];
|
|
|
|
}
|
|
|
|
else if (optind >= argc)
|
|
|
|
{
|
|
|
|
fmt = g_fmttoomanyargs;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the local file name */
|
|
|
|
|
|
|
|
if (!localfile)
|
|
|
|
{
|
|
|
|
allocfile = strdup(url);
|
|
|
|
localfile = basename(allocfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the full path to the local file */
|
|
|
|
|
|
|
|
fullpath = nsh_getfullpath(vtbl, localfile);
|
|
|
|
|
|
|
|
/* Open the local file for writing */
|
|
|
|
|
|
|
|
fd = open(fullpath, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate an I/O buffer */
|
|
|
|
|
|
|
|
buffer = malloc(512);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And perform the wget */
|
|
|
|
|
|
|
|
ret = wget(url, buffer, 512, wget_callback, (FAR void *)fd);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "wget", NSH_ERRNO);
|
|
|
|
goto exit;
|
2013-12-29 22:02:51 +01:00
|
|
|
}
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Free allocated resources */
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
if (allocfile)
|
|
|
|
{
|
|
|
|
free(allocfile);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
if (fullpath)
|
|
|
|
{
|
|
|
|
free(fullpath);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
if (buffer)
|
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
nsh_output(vtbl, fmt, argv[0]);
|
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET */
|