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>
|
2015-07-13 19:19:02 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
2015-07-13 19:19:02 +02:00
|
|
|
#include <nuttx/compiler.h>
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#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 */
|
2015-11-28 00:47:09 +01:00
|
|
|
#include <dirent.h>
|
2015-07-11 16:15:29 +02:00
|
|
|
#include <netdb.h> /* Needed for gethostbyname */
|
2011-03-18 20:46:25 +01:00
|
|
|
#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
|
|
|
|
2015-07-13 19:19:02 +02:00
|
|
|
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
|
|
|
|
# ifndef CONFIG_NSH_DISABLE_NSLOOKUP
|
|
|
|
# include <netdb.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
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>
|
|
|
|
|
2016-02-14 22:15:37 +01:00
|
|
|
#include <nuttx/irq.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
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
#include <nuttx/net/sixlowpan.h>
|
|
|
|
#endif
|
|
|
|
|
2017-04-25 16:25:23 +02:00
|
|
|
#ifdef CONFIG_NETUTILS_NETLIB
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/netlib.h"
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/netlib.h"
|
2017-04-29 16:41:55 +02:00
|
|
|
# if !defined(CONFIG_NSH_DISABLE_GET) || !defined(CONFIG_NSH_DISABLE_PUT)
|
|
|
|
# include "netutils/tftp.h"
|
|
|
|
# endif
|
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
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "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)
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "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
|
|
|
|
|
2015-11-28 00:47:09 +01:00
|
|
|
typedef int (*nsh_netdev_callback_t)(FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char *devname);
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
{
|
2016-02-14 22:15:37 +01:00
|
|
|
irqstate_t save = enter_critical_section();
|
2011-03-18 20:46:25 +01:00
|
|
|
uint16_t ret = ++g_pingid;
|
2016-02-14 22:15:37 +01:00
|
|
|
leave_critical_section(save);
|
2011-03-18 20:46:25 +01:00
|
|
|
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 */
|
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
if (optind == argc - 1)
|
2015-01-23 19:49:49 +01:00
|
|
|
{
|
|
|
|
*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
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-27 20:04:11 +01:00
|
|
|
#if defined(CONFIG_NET_STATISTICS) && defined(CONFIG_FS_PROCFS) && \
|
|
|
|
!defined(CONFIG_FS_PROCFS_EXCLUDE_NET) && \
|
|
|
|
!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-11-28 14:23:08 +01:00
|
|
|
(void)nsh_catfile(vtbl, "ifconfig", CONFIG_NSH_PROC_MOUNTPOINT "/net/stat");
|
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)
|
2015-11-28 00:47:09 +01:00
|
|
|
static int ifconfig_callback(FAR struct nsh_vtbl_s *vtbl, FAR char *devname)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-11-27 23:57:22 +01:00
|
|
|
char buffer[IFNAMSIZ + 12];
|
2015-05-19 20:26:20 +02:00
|
|
|
|
2015-11-28 00:47:09 +01:00
|
|
|
DEBUGASSERT(vtbl != NULL && devname != NULL);
|
2012-11-04 19:54:04 +01:00
|
|
|
|
2015-11-27 23:57:22 +01:00
|
|
|
/* Construct the full path to the /proc/net entry for this device */
|
2015-11-26 21:43:34 +01:00
|
|
|
|
2015-11-28 14:23:08 +01:00
|
|
|
snprintf(buffer, IFNAMSIZ + 12, CONFIG_NSH_PROC_MOUNTPOINT "/net/%s", devname);
|
2015-11-27 23:57:22 +01:00
|
|
|
(void)nsh_catfile(vtbl, "ifconfig", buffer);
|
2015-11-26 21:43:34 +01:00
|
|
|
|
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':
|
2016-02-08 23:09:24 +01:00
|
|
|
if (!netlib_ipv4addrconv(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
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
if (optind == argc - 1)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
/* optind < argc - 1 means that there are too many arguments on the
|
2011-06-05 16:08:26 +02:00
|
|
|
* 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
|
|
|
|
|
2015-07-11 16:15:29 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(HAVE_PING) || defined(HAVE_PING6)
|
|
|
|
static int nsh_gethostip(FAR char *hostname, FAR union ip_addr_u *ipaddr,
|
|
|
|
int addrtype)
|
|
|
|
{
|
2015-07-17 16:33:22 +02:00
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
|
|
|
|
/* Netdb support is enabled */
|
|
|
|
|
2015-07-11 16:15:29 +02:00
|
|
|
FAR struct hostent *he;
|
|
|
|
|
|
|
|
he = gethostbyname(hostname);
|
|
|
|
if (he == NULL)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
nerr("ERROR: gethostbyname failed: %d\n", h_errno);
|
2015-07-11 16:15:29 +02:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2015-07-17 16:33:22 +02:00
|
|
|
|
2015-07-11 16:15:29 +02:00
|
|
|
#if defined(HAVE_PING) && defined(HAVE_PING6)
|
|
|
|
|
|
|
|
else if (he->h_addrtype != addrtype)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
nerr("ERROR: gethostbyname returned an address of type: %d\n",
|
|
|
|
he->h_addrtype);
|
2015-07-11 16:15:29 +02:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
else if (addrtype == AF_INET)
|
|
|
|
{
|
|
|
|
memcpy(&ipaddr->ipv4, he->h_addr, sizeof(in_addr_t));
|
|
|
|
}
|
|
|
|
else /* if (addrtype == AF_INET6) */
|
|
|
|
{
|
|
|
|
memcpy(ipaddr->ipv6, he->h_addr, sizeof(net_ipv6addr_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_PING)
|
|
|
|
|
|
|
|
else if (he->h_addrtype != AF_INET)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
nerr("ERROR: gethostbyname returned an address of type: %d\n",
|
|
|
|
he->h_addrtype);
|
2015-07-11 16:15:29 +02:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(&ipaddr->ipv4, he->h_addr, sizeof(in_addr_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* if defined(HAVE_PING6) */
|
|
|
|
|
|
|
|
else if (he->h_addrtype != AF_INET6)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
nerr("ERROR: gethostbyname returned an address of type: %d\n",
|
|
|
|
he->h_addrtype);
|
2015-07-11 16:15:29 +02:00
|
|
|
return -ENOEXEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(ipaddr->ipv6, he->h_addr, sizeof(net_ipv6addr_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return OK;
|
2015-07-17 16:33:22 +02:00
|
|
|
|
|
|
|
#else /* CONFIG_LIBC_NETDB */
|
|
|
|
|
|
|
|
/* No host name support */
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#ifdef HAVE_PING
|
|
|
|
/* Convert strings to numeric IPv4 address */
|
|
|
|
|
|
|
|
#ifdef HAVE_PING6
|
|
|
|
if (addrtype == AF_INET)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret = inet_pton(AF_INET, hostname, &ipaddr->ipv4);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_PING6
|
|
|
|
/* Convert strings to numeric IPv6 address */
|
|
|
|
|
|
|
|
#ifdef HAVE_PING
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ret = inet_pton(AF_INET6, hostname, ipaddr->ipv6);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-03 19:03:42 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2015-07-17 16:33:22 +02:00
|
|
|
|
|
|
|
return (ret > 0) ? OK : ERROR;
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIBC_NETDB */
|
2015-07-11 16:15:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
2017-04-03 17:26:10 +02:00
|
|
|
(void)write((int)((intptr_t)arg), &((*buffer)[offset]), datend - offset);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-11-28 00:47:09 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_foreach_netdev
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_NSH_DISABLE_IFUPDOWN) || !defined(CONFIG_NSH_DISABLE_IFCONFIG)
|
|
|
|
static int nsh_foreach_netdev(nsh_netdev_callback_t callback,
|
|
|
|
FAR struct nsh_vtbl_s *vtbl,
|
|
|
|
FAR char *cmd)
|
|
|
|
{
|
|
|
|
FAR struct dirent *entry;
|
|
|
|
FAR DIR *dir;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Open the /proc/net directory */
|
|
|
|
|
2015-11-28 14:23:08 +01:00
|
|
|
dir = opendir(CONFIG_NSH_PROC_MOUNTPOINT "/net");
|
2015-11-28 00:47:09 +01:00
|
|
|
if (dir == NULL)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, cmd, "opendir", NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for device configuration "regular" files */
|
|
|
|
|
|
|
|
while ((entry = readdir(dir)) != NULL)
|
|
|
|
{
|
|
|
|
/* Skip anything that is not a regular file and skip the file
|
|
|
|
* /proc/dev/stat which does not correspond to a network driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (entry->d_type == DTYPE_FILE &&
|
|
|
|
strcmp(entry->d_name, "stat") != 0)
|
|
|
|
{
|
|
|
|
/* Performt he callback. It returns any non-zero value, then
|
|
|
|
* terminate the serach.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = callback(vtbl, entry->d_name);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the directory */
|
|
|
|
|
|
|
|
(void)closedir(dir);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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");
|
2015-11-28 00:47:09 +01:00
|
|
|
return nsh_foreach_netdev(ifconfig_callback, vtbl, "ifup");
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2015-11-28 00:47:09 +01:00
|
|
|
return nsh_foreach_netdev(ifconfig_callback, vtbl, "ifdown");
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-04-08 20:21:40 +02:00
|
|
|
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_NET_6LOWPAN)
|
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;
|
2017-04-08 20:21:40 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2012-11-27 17:26:54 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
2017-04-08 20:21:40 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_6LOWPAN
|
|
|
|
uint8_t nodeaddr[NET_6LOWPAN_RIMEADDR_SIZE];
|
|
|
|
#endif
|
2012-11-27 17:26:54 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC)
|
|
|
|
FAR void *handle;
|
|
|
|
#endif
|
2015-11-28 00:47:09 +01:00
|
|
|
int ret;
|
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)
|
|
|
|
{
|
2015-11-28 00:47:09 +01:00
|
|
|
ret = nsh_foreach_netdev(ifconfig_callback, vtbl, "ifconfig");
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
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"))
|
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
if (argc - 1 >= i + 1)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
gwip = argv[i + 1];
|
2012-11-04 19:54:04 +01:00
|
|
|
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
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
if (argc - 1 >= i + 1)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
mask = argv[i + 1];
|
2012-11-04 19:54:04 +01:00
|
|
|
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
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_NET_6LOWPAN)
|
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
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
if (argc - 1 >= i + 1)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
hw = argv[i + 1];
|
2012-11-04 19:54:04 +01:00
|
|
|
i++;
|
2017-04-08 20:21:40 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2016-02-08 23:09:24 +01:00
|
|
|
badarg = !netlib_ethaddrconv(hw, mac);
|
2017-04-08 20:21:40 +02:00
|
|
|
#else
|
|
|
|
badarg = !netlib_nodeaddrconv(hw, nodeaddr);
|
|
|
|
#endif
|
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
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
if (argc - 1 >= i + 1)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-04-08 20:21:40 +02:00
|
|
|
dns = argv[i + 1];
|
2012-11-04 19:54:04 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_NET_6LOWPAN)
|
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)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("HW MAC: %s\n", hw);
|
2017-04-08 20:21:40 +02:00
|
|
|
#ifdef CONFIG_NET_ETHERNET
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr(intf, mac);
|
2017-04-08 20:21:40 +02:00
|
|
|
#else
|
|
|
|
netlib_setnodeaddr(intf, nodeaddr);
|
|
|
|
#endif
|
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
|
|
|
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("DHCPC Mode\n");
|
2015-01-19 16:59:43 +01:00
|
|
|
gip = addr.s_addr = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Set host IP address */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("Host IP: %s\n", hostip);
|
2015-01-19 16:59:43 +01:00
|
|
|
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
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("Gateway: %s\n", gwip);
|
2015-01-19 16:59:43 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("Gateway: default\n");
|
2015-01-19 16:59:43 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("Netmask: %s\n",mask);
|
2015-01-19 16:59:43 +01:00
|
|
|
addr.s_addr = inet_addr(mask);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("Netmask: Default\n");
|
2015-01-19 16:59:43 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("DNS: %s\n", dns);
|
2015-01-19 16:59:43 +01:00
|
|
|
addr.s_addr = inet_addr(dns);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("DNS: Default\n");
|
2015-01-19 16:59:43 +01:00
|
|
|
addr.s_addr = gip;
|
|
|
|
}
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2015-07-12 19:53:23 +02:00
|
|
|
netlib_set_ipv4dnsaddr(&addr);
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
|
|
|
#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)
|
|
|
|
{
|
2015-07-12 19:53:23 +02:00
|
|
|
netlib_set_ipv4dnsaddr(&ds.dnsaddr);
|
2012-11-27 17:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dhcpc_close(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
2015-07-13 19:19:02 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_nslookup
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT) && \
|
|
|
|
!defined(CONFIG_NSH_DISABLE_NSLOOKUP)
|
|
|
|
int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
FAR struct hostent *host;
|
|
|
|
FAR const char *addrtype;
|
|
|
|
char buffer[48];
|
|
|
|
|
|
|
|
/* We should be guaranteed this by the command line parser */
|
|
|
|
|
|
|
|
DEBUGASSERT(argc == 2);
|
|
|
|
|
|
|
|
/* Get the matching address + any aliases */
|
|
|
|
|
|
|
|
host = gethostbyname(argv[1]);
|
|
|
|
if (!host)
|
|
|
|
{
|
|
|
|
/* REVISIT: gethostbyname() does not set errno, but h_errno */
|
|
|
|
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "gethostbyname", NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the address to a string */
|
|
|
|
/* Handle IPv4 addresses */
|
|
|
|
|
|
|
|
if (host->h_addrtype == AF_INET)
|
|
|
|
{
|
|
|
|
if (inet_ntop(AF_INET, host->h_addr, buffer, 48) == NULL)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "inet_ntop", NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrtype = "IPv4";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle IPv6 addresses */
|
|
|
|
|
|
|
|
else /* if (host->h_addrtype == AF_INET6) */
|
|
|
|
{
|
|
|
|
DEBUGASSERT(host->h_addrtype == AF_INET6);
|
|
|
|
|
|
|
|
if (inet_ntop(AF_INET6, host->h_addr, buffer, 48) == NULL)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "inet_ntop", NSH_ERRNO);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
addrtype = "IPv6";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the host name / address mapping */
|
|
|
|
|
|
|
|
nsh_output(vtbl, "Host: %s %s Addr: %s\n", host->h_name, addrtype, buffer);
|
|
|
|
|
|
|
|
/* Print any host name aliases */
|
|
|
|
|
|
|
|
if (host->h_aliases != NULL && *host->h_aliases != NULL)
|
|
|
|
{
|
|
|
|
FAR char **alias;
|
|
|
|
|
|
|
|
nsh_output(vtbl, "Aliases:");
|
|
|
|
for (alias = host->h_aliases; *alias != NULL; alias++)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, " %s", *alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsh_output(vtbl, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-08 21:49:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_arp
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ARP) && !defined(CONFIG_NSH_DISABLE_ARP)
|
|
|
|
int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct sockaddr_in inaddr;
|
|
|
|
struct ether_addr mac;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Forms:
|
|
|
|
*
|
|
|
|
* aap -a <ipaddr>
|
|
|
|
* arp -d <ipdaddr>
|
|
|
|
* arp -s <ipaddr> <hwaddr>
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "-a") == 0)
|
|
|
|
{
|
|
|
|
if (argc != 3)
|
|
|
|
{
|
|
|
|
goto errout_toomany;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show the corresponding hardware address */
|
|
|
|
|
|
|
|
inaddr.sin_family = AF_INET;
|
2016-02-09 18:24:30 +01:00
|
|
|
inaddr.sin_port = 0;
|
2016-02-08 21:49:05 +01:00
|
|
|
inaddr.sin_addr.s_addr = inet_addr(argv[2]);
|
|
|
|
|
|
|
|
ret = netlib_get_arpmapping(&inaddr, mac.ether_addr_octet);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_cmdfaild;
|
|
|
|
}
|
|
|
|
|
2016-02-08 23:09:24 +01:00
|
|
|
nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa(&mac));
|
2016-02-08 21:49:05 +01:00
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "-d") == 0)
|
|
|
|
{
|
|
|
|
if (argc != 3)
|
|
|
|
{
|
|
|
|
goto errout_toomany;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete the corresponding address mapping from the arp table */
|
|
|
|
|
|
|
|
inaddr.sin_family = AF_INET;
|
2016-02-09 18:24:30 +01:00
|
|
|
inaddr.sin_port = 0;
|
2016-02-08 21:49:05 +01:00
|
|
|
inaddr.sin_addr.s_addr = inet_addr(argv[2]);
|
|
|
|
|
|
|
|
ret = netlib_del_arpmapping(&inaddr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_cmdfaild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[1], "-s") == 0)
|
|
|
|
{
|
|
|
|
if (argc != 4)
|
|
|
|
{
|
|
|
|
goto errout_missing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the MAC address string to a binary */
|
|
|
|
|
2016-02-08 23:19:21 +01:00
|
|
|
if (!netlib_ethaddrconv(argv[3], mac.ether_addr_octet))
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2016-02-08 23:19:21 +01:00
|
|
|
goto errout_invalid;
|
2016-02-08 21:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the address mapping to the arp table */
|
|
|
|
|
|
|
|
inaddr.sin_family = AF_INET;
|
2016-02-09 18:24:30 +01:00
|
|
|
inaddr.sin_port = 0;
|
2016-02-08 21:49:05 +01:00
|
|
|
inaddr.sin_addr.s_addr = inet_addr(argv[2]);
|
|
|
|
|
|
|
|
ret = netlib_set_arpmapping(&inaddr, mac.ether_addr_octet);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_cmdfaild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout_invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
|
|
|
errout_cmdfaild:
|
2016-02-08 23:29:54 +01:00
|
|
|
if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtnosuch, argv[0], "ARP entry", argv[2]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsh_output(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
|
|
|
|
}
|
|
|
|
|
2016-02-08 21:49:05 +01:00
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
errout_missing:
|
|
|
|
nsh_output(vtbl, g_fmttoomanyargs, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
errout_toomany:
|
|
|
|
nsh_output(vtbl, g_fmtargrequired, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
errout_invalid:
|
|
|
|
nsh_output(vtbl, g_fmtarginvalid, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
2016-01-21 19:03:04 +01:00
|
|
|
systime_t start;
|
|
|
|
systime_t next;
|
|
|
|
int32_t elapsed;
|
2011-03-18 20:46:25 +01:00
|
|
|
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;
|
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-07-11 16:15:29 +02:00
|
|
|
ret = nsh_gethostip(staddr, (FAR union ip_addr_u *)&ipaddr, AF_INET);
|
|
|
|
if (ret < 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-07-11 16:15:29 +02:00
|
|
|
nsh_output(vtbl, "nsh: %s: unable to resolve hostname '%s'\n", argv[0], staddr);
|
|
|
|
return ERROR;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
elapsed = (int32_t)TICK2MSEC(clock_systimer() - next);
|
2015-01-23 19:49:49 +01:00
|
|
|
if (seqno < i)
|
|
|
|
{
|
|
|
|
elapsed += 100 * dsec * (i - seqno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Report the receipt of the reply */
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%ld ms\n",
|
|
|
|
DEFAULT_PING_DATALEN, staddr, seqno, (long)elapsed);
|
2015-01-23 19:49:49 +01:00
|
|
|
replies++;
|
|
|
|
}
|
2015-05-27 16:25:26 +02:00
|
|
|
else if (seqno < 0)
|
|
|
|
{
|
|
|
|
if (seqno == -ETIMEDOUT)
|
|
|
|
{
|
2015-05-27 16:43:50 +02:00
|
|
|
nsh_output(vtbl, "icmp_seq=%d Request timeout\n", i);
|
2015-05-27 16:25:26 +02:00
|
|
|
}
|
|
|
|
else if (seqno == -ENETUNREACH)
|
|
|
|
{
|
2015-05-27 16:43:50 +02:00
|
|
|
nsh_output(vtbl, "icmp_seq=%d Network is unreachable\n", i);
|
2015-05-27 16:25:26 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 19:49:49 +01:00
|
|
|
|
|
|
|
/* 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!
|
|
|
|
*/
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
elapsed = (int32_t)TICK2DSEC(clock_systimer() - next);
|
2015-01-23 19:49:49 +01:00
|
|
|
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 */
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
elapsed = (int32_t)TICK2MSEC(clock_systimer() - start);
|
2015-01-23 19:49:49 +01:00
|
|
|
|
|
|
|
/* Calculate the percentage of lost packets */
|
|
|
|
|
|
|
|
tmp = (100*(count - replies) + (count >> 1)) / count;
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
nsh_output(vtbl, "%d packets transmitted, %d received, %d%% packet loss, time %ld ms\n",
|
2015-01-23 19:49:49 +01:00
|
|
|
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;
|
2016-01-21 19:03:04 +01:00
|
|
|
systime_t start;
|
|
|
|
systime_t next;
|
|
|
|
int32_t elapsed;
|
2015-01-23 19:49:49 +01:00
|
|
|
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 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
|
|
|
|
2015-07-11 16:15:29 +02:00
|
|
|
/* Get the IP address in binary form */
|
2015-01-23 19:49:49 +01:00
|
|
|
|
2015-07-11 16:15:29 +02:00
|
|
|
ret = nsh_gethostip(staddr, (FAR union ip_addr_u *)&ipaddr, AF_INET6);
|
2015-01-23 19:49:49 +01:00
|
|
|
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
|
|
|
*/
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
elapsed = (int32_t)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 */
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%ld ms\n",
|
|
|
|
DEFAULT_PING_DATALEN, staddr, seqno, (long)elapsed);
|
2011-03-18 20:46:25 +01:00
|
|
|
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!
|
|
|
|
*/
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
elapsed = (int32_t)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 */
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
elapsed = (int32_t)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;
|
|
|
|
|
2016-01-21 19:03:04 +01:00
|
|
|
nsh_output(vtbl, "%d packets transmitted, %d received, %d%% packet loss, time %ld ms\n",
|
|
|
|
count, replies, tmp, (long)elapsed);
|
2011-03-18 20:46:25 +01:00
|
|
|
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 */
|
|
|
|
|
2017-04-08 20:21:40 +02:00
|
|
|
if (optind == argc - 1)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2017-04-03 17:26:10 +02:00
|
|
|
ret = wget(url, buffer, 512, wget_callback, (FAR void *)((intptr_t)fd));
|
2011-03-18 20:46:25 +01:00
|
|
|
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 */
|