2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_netcmds.c
|
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
2021-06-10 16:34:12 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2019-11-03 23:19:14 +01:00
|
|
|
#include <inttypes.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#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>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <libgen.h> /* Needed for basename */
|
2021-05-20 05:32:57 +02:00
|
|
|
#include <assert.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <errno.h>
|
2012-10-20 15:47:30 +02:00
|
|
|
#include <debug.h>
|
2022-12-06 06:42:15 +01:00
|
|
|
#include <getopt.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
|
|
|
|
# include <netdb.h>
|
2015-07-13 19:19:02 +02:00
|
|
|
#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>
|
2022-12-15 11:57:43 +01:00
|
|
|
#include <netinet/arp.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
|
2017-08-02 21:13:19 +02:00
|
|
|
# include <nuttx/net/sixlowpan.h>
|
|
|
|
# ifdef CONFIG_WIRELESS_PKTRADIO
|
|
|
|
# include <nuttx/wireless/pktradio.h>
|
|
|
|
# endif
|
2017-04-08 20:21:40 +02:00
|
|
|
#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
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
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
|
|
|
|
|
2019-02-11 20:25:48 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
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
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_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
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
#undef HAVE_HWADDR
|
2017-07-31 00:03:56 +02:00
|
|
|
#undef HAVE_EADDR
|
2017-08-02 21:13:19 +02:00
|
|
|
#undef HAVE_RADIOADDR
|
2015-01-23 19:49:49 +01:00
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
|
|
|
# define HAVE_HWADDR 1
|
|
|
|
#elif defined(CONFIG_NET_6LOWPAN)
|
2017-07-31 00:03:56 +02:00
|
|
|
# if defined(CONFIG_WIRELESS_IEEE802154)
|
2017-08-02 21:13:19 +02:00
|
|
|
# define HAVE_HWADDR 1
|
|
|
|
# define HAVE_EADDR 1
|
2017-07-31 00:03:56 +02:00
|
|
|
# elif defined(CONFIG_WIRELESS_PKTRADIO)
|
2017-08-02 21:13:19 +02:00
|
|
|
# define HAVE_HWADDR 1
|
|
|
|
# define HAVE_RADIOADDR 1
|
2017-07-31 00:03:56 +02:00
|
|
|
# endif
|
2015-01-23 19:49:49 +01:00
|
|
|
#endif
|
|
|
|
|
2012-10-20 16:15:59 +02:00
|
|
|
/* Get the larger value */
|
|
|
|
|
|
|
|
#ifndef MAX
|
2022-03-16 12:13:12 +01:00
|
|
|
# define MAX(a,b) ((a) > (b) ? (a) : (b))
|
2012-10-20 16:15:59 +02:00
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
/* Describes the MAC address of the selected device */
|
|
|
|
|
|
|
|
#ifdef HAVE_HWADDR
|
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
|
|
|
typedef uint8_t mac_addr_t[IFHWADDRLEN];
|
|
|
|
#elif defined(HAVE_EADDR)
|
|
|
|
typedef uint8_t mac_addr_t[8];
|
|
|
|
#elif defined(HAVE_RADIOADDR)
|
|
|
|
typedef struct pktradio_addr_s mac_addr_t;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2011-03-18 20:46:25 +01:00
|
|
|
struct tftpc_args_s
|
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
bool binary; /* true:binary ("octet") false:text ("netascii") */
|
|
|
|
bool allocated; /* true: destpath is allocated */
|
|
|
|
FAR char *destpath; /* Path at destination */
|
|
|
|
FAR const char *srcpath; /* Path at src */
|
|
|
|
in_addr_t ipaddr; /* Host IP address */
|
2011-03-18 20:46:25 +01:00
|
|
|
};
|
|
|
|
#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 Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
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
|
|
|
{
|
2020-01-02 13:09:50 +01:00
|
|
|
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
|
|
|
{
|
2022-08-22 15:17:00 +02:00
|
|
|
char buffer[NAME_MAX + 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
|
|
|
|
2022-08-22 15:17:00 +02:00
|
|
|
snprintf(buffer, NAME_MAX + 12,
|
2020-03-30 13:55:46 +02:00
|
|
|
CONFIG_NSH_PROC_MOUNTPOINT "/net/%s", devname);
|
2020-01-02 13:09:50 +01:00
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2022-09-22 05:38:12 +02:00
|
|
|
int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv,
|
|
|
|
FAR struct tftpc_args_s *args)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
FAR const char *fmt = g_fmtarginvalid;
|
|
|
|
bool badarg = false;
|
|
|
|
int option;
|
|
|
|
|
2017-10-24 19:19:41 +02:00
|
|
|
/* Get the get/put options */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
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':
|
2020-03-30 13:55:46 +02:00
|
|
|
if (!netlib_ipv4addrconv(optarg, (FAR uint8_t *)&args->ipaddr))
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
/* If a bad argument was encountered, then return without processing */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (args->ipaddr == 0)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the destpath was not provided, then we have do a little work. */
|
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (args->destpath == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
FAR char *tmp1;
|
|
|
|
FAR char *tmp2;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Copy the srcpath... baseanme might modify it */
|
|
|
|
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
tmp1 = strdup(args->srcpath);
|
2017-10-25 01:26:17 +02:00
|
|
|
if (tmp1 == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the basename of the srcpath */
|
|
|
|
|
|
|
|
tmp2 = basename(tmp1);
|
2017-10-25 01:26:17 +02:00
|
|
|
if (tmp2 == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
free(tmp1);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use that basename as the destpath */
|
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
args->destpath = strdup(tmp2);
|
2011-03-18 20:46:25 +01:00
|
|
|
free(tmp1);
|
2017-10-25 01:26:17 +02:00
|
|
|
if (args->destpath == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_WGET
|
2020-09-29 02:42:40 +02:00
|
|
|
static int wget_callback(FAR char **buffer, int offset, int datend,
|
2022-09-22 05:38:12 +02:00
|
|
|
FAR int *buflen, FAR void *arg)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2020-09-29 02:42:40 +02:00
|
|
|
ssize_t written = write((int)((intptr_t)arg), &((*buffer)[offset]),
|
|
|
|
datend - offset);
|
|
|
|
if (written == -1)
|
|
|
|
{
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Revisit: Do we want to check and report short writes? */
|
|
|
|
|
|
|
|
return 0;
|
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;
|
2022-09-05 16:25:19 +02:00
|
|
|
uint8_t flags;
|
2015-11-28 00:47:09 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-10-28 11:04:16 +01:00
|
|
|
nsh_error(vtbl,
|
2022-07-17 08:05:23 +02:00
|
|
|
"nsh: %s: Could not open %s/net (is procfs mounted?)\n",
|
|
|
|
cmd, CONFIG_NSH_PROC_MOUNTPOINT);
|
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, cmd, "opendir", NSH_ERRNO);
|
2015-11-28 00:47:09 +01:00
|
|
|
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 &&
|
2022-09-05 16:25:19 +02:00
|
|
|
netlib_getifstatus(entry->d_name, &flags) >= 0)
|
2015-11-28 00:47:09 +01:00
|
|
|
{
|
|
|
|
/* Performt he callback. It returns any non-zero value, then
|
2020-02-23 05:51:44 +01:00
|
|
|
* terminate the search.
|
2015-11-28 00:47:09 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = callback(vtbl, entry->d_name);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the directory */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
closedir(dir);
|
2015-11-28 00:47:09 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_addrconv
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-08 17:13:09 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_IFCONFIG) && defined(HAVE_HWADDR)
|
2020-03-30 13:55:46 +02:00
|
|
|
static inline bool nsh_addrconv(FAR const char *hwstr,
|
|
|
|
FAR mac_addr_t *macaddr)
|
2017-08-02 21:13:19 +02:00
|
|
|
{
|
|
|
|
/* REVISIT: How will we handle Ethernet and SLIP networks together? */
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
|
|
|
return !netlib_ethaddrconv(hwstr, *macaddr);
|
|
|
|
#elif defined(HAVE_EADDR)
|
|
|
|
return !netlib_eaddrconv(hwstr, *macaddr);
|
|
|
|
#elif defined(HAVE_RADIOADDR)
|
|
|
|
return !netlib_nodeaddrconv(hwstr, macaddr);
|
|
|
|
#else
|
|
|
|
return -ENOSUPP;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_sethwaddr
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-08 17:13:09 +01:00
|
|
|
#if !defined(CONFIG_NSH_DISABLE_IFCONFIG) && defined(HAVE_HWADDR)
|
2020-03-30 13:55:46 +02:00
|
|
|
static inline void nsh_sethwaddr(FAR const char *ifname,
|
|
|
|
FAR mac_addr_t *macaddr)
|
2017-08-02 21:13:19 +02:00
|
|
|
{
|
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
|
|
|
netlib_setmacaddr(ifname, *macaddr);
|
|
|
|
#elif defined(HAVE_EADDR)
|
|
|
|
netlib_seteaddr(ifname, *macaddr);
|
|
|
|
#elif defined(HAVE_RADIOADDR)
|
|
|
|
netlib_setnodeaddr(ifname, macaddr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_get
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_GET
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
struct tftpc_args_s args;
|
2017-10-25 01:26:17 +02:00
|
|
|
FAR char *fullpath;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "tftpget", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_ifup(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-08-02 21:13:19 +02:00
|
|
|
FAR char *ifname = NULL;
|
2012-11-04 19:54:04 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
2017-08-02 22:30:34 +02:00
|
|
|
nsh_output(vtbl, "Please select ifname:\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
|
|
|
}
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
ifname = argv[1];
|
|
|
|
ret = netlib_ifup(ifname);
|
|
|
|
nsh_output(vtbl, "ifup %s...%s\n", ifname, (ret == OK) ? "OK" : "Failed");
|
2012-11-04 19:54:04 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_ifdown
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NSH_DISABLE_IFUPDOWN
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_ifdown(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-08-02 21:13:19 +02:00
|
|
|
FAR char *ifname = NULL;
|
2012-11-04 19:54:04 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc != 2)
|
|
|
|
{
|
2017-08-02 22:30:34 +02:00
|
|
|
nsh_output(vtbl, "Please select ifname:\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
|
|
|
}
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
ifname = argv[1];
|
|
|
|
ret = netlib_ifdown(ifname);
|
2020-03-30 13:55:46 +02:00
|
|
|
nsh_output(vtbl, "ifdown %s...%s\n",
|
|
|
|
ifname, (ret == OK) ? "OK" : "Failed");
|
2012-11-04 19:54:04 +01:00
|
|
|
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
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2011-05-21 20:25:31 +02:00
|
|
|
struct in_addr addr;
|
2018-06-17 23:37:42 +02:00
|
|
|
in_addr_t gip = INADDR_ANY;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
struct in6_addr addr6;
|
2022-08-03 11:34:31 +02:00
|
|
|
struct in6_addr gip6 = IN6ADDR_ANY_INIT;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
int i;
|
2017-08-02 21:13:19 +02:00
|
|
|
FAR char *ifname = NULL;
|
2012-11-04 19:54:04 +01:00
|
|
|
FAR char *hostip = NULL;
|
|
|
|
FAR char *gwip = NULL;
|
|
|
|
FAR char *mask = NULL;
|
|
|
|
FAR char *tmp = NULL;
|
2017-08-02 21:13:19 +02:00
|
|
|
#ifdef HAVE_HWADDR
|
2012-11-04 19:54:04 +01:00
|
|
|
FAR char *hw = NULL;
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_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
|
2017-08-02 22:30:34 +02:00
|
|
|
bool missingarg = true;
|
2012-11-27 17:26:54 +01:00
|
|
|
bool badarg = false;
|
2017-08-02 21:13:19 +02:00
|
|
|
#ifdef HAVE_HWADDR
|
|
|
|
mac_addr_t macaddr;
|
2017-04-08 20:21:40 +02:00
|
|
|
#endif
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC)
|
2012-11-27 17:26:54 +01:00
|
|
|
FAR void *handle;
|
|
|
|
#endif
|
2015-11-28 00:47:09 +01:00
|
|
|
int ret;
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2017-07-02 19:28:12 +02:00
|
|
|
/* With one or no arguments, ifconfig simply shows the status of the
|
|
|
|
* network device:
|
2011-05-21 20:25:31 +02:00
|
|
|
*
|
|
|
|
* ifconfig
|
2017-07-02 19:28:12 +02:00
|
|
|
* ifconfig [interface]
|
2011-05-21 20:25:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2017-08-02 22:30:34 +02:00
|
|
|
* ifconfig ifname [ip_address] [named options]
|
2011-05-21 20:25:31 +02:00
|
|
|
*/
|
|
|
|
|
2012-11-04 19:54:04 +01:00
|
|
|
if (argc > 2)
|
|
|
|
{
|
2017-08-02 22:30:34 +02:00
|
|
|
for (i = 1; i < argc; i++)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
|
|
|
if (i == 1)
|
|
|
|
{
|
2017-08-02 21:13:19 +02:00
|
|
|
ifname = argv[i];
|
2017-08-02 22:30:34 +02:00
|
|
|
missingarg = false;
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp = argv[i];
|
2017-08-02 22:30:34 +02:00
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (!strcmp(tmp, "dr") || !strcmp(tmp, "gw") ||
|
|
|
|
!strcmp(tmp, "gateway"))
|
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
|
|
|
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-08-02 21:13:19 +02:00
|
|
|
#ifdef HAVE_HWADDR
|
2020-04-13 05:56:53 +02:00
|
|
|
/* REVISIT: How will we handle Ethernet and SLIP together? */
|
2014-06-27 20:46:54 +02:00
|
|
|
|
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-08-02 21:13:19 +02:00
|
|
|
|
|
|
|
badarg = nsh_addrconv(hw, &macaddr);
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)
|
2013-09-17 18:48:30 +02:00
|
|
|
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
|
2017-08-02 22:30:34 +02:00
|
|
|
else if (i == 2)
|
|
|
|
{
|
|
|
|
hostip = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
badarg = true;
|
|
|
|
}
|
2012-11-04 19:54:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 22:30:34 +02:00
|
|
|
if (missingarg)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2012-11-04 19:54:04 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-08-02 22:30:34 +02:00
|
|
|
if (badarg)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2017-08-02 22:30:34 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
#ifdef HAVE_HWADDR
|
2013-12-29 22:02:51 +01:00
|
|
|
/* Set Hardware Ethernet MAC address */
|
2012-11-04 19:54:04 +01:00
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
if (hw != NULL)
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2016-06-14 22:18:07 +02:00
|
|
|
ninfo("HW MAC: %s\n", hw);
|
2017-08-02 21:13:19 +02:00
|
|
|
nsh_sethwaddr(ifname, &macaddr);
|
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
|
|
|
|
2020-03-30 13:55:46 +02: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
|
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
if (hostip != NULL)
|
|
|
|
{
|
|
|
|
/* REVISIT: Should DHCPC check be used here too? */
|
|
|
|
|
|
|
|
ninfo("Host IP: %s\n", hostip);
|
|
|
|
inet_pton(AF_INET6, hostip, &addr6);
|
|
|
|
}
|
|
|
|
|
|
|
|
netlib_set_ipv6addr(ifname, &addr6);
|
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
|
|
|
|
{
|
2017-09-25 15:34:34 +02:00
|
|
|
if (hostip != NULL)
|
2015-01-19 16:59:43 +01:00
|
|
|
{
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC)
|
2017-10-25 01:26:17 +02:00
|
|
|
if (strcmp(hostip, "dhcp") == 0)
|
2017-09-25 15:34:34 +02:00
|
|
|
{
|
|
|
|
/* Set DHCP addr */
|
2012-11-27 17:26:54 +01:00
|
|
|
|
2017-09-25 15:34:34 +02:00
|
|
|
ninfo("DHCPC Mode\n");
|
2017-10-25 01:26:17 +02:00
|
|
|
addr.s_addr = 0;
|
|
|
|
gip = 0;
|
2017-09-25 15:34:34 +02:00
|
|
|
}
|
|
|
|
else
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2017-09-25 15:34:34 +02:00
|
|
|
{
|
|
|
|
/* Set host IP address */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2017-09-25 15:34:34 +02:00
|
|
|
ninfo("Host IP: %s\n", hostip);
|
2017-10-25 01:26:17 +02:00
|
|
|
addr.s_addr = inet_addr(hostip);
|
|
|
|
gip = addr.s_addr;
|
2017-09-25 15:34:34 +02:00
|
|
|
}
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
2022-05-05 05:55:42 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
addr.s_addr = 0;
|
|
|
|
}
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
netlib_set_ipv4addr(ifname, &addr);
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
/* Set gateway */
|
|
|
|
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2017-09-27 20:05:19 +02:00
|
|
|
if (inet6)
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2012-11-04 19:54:04 +01:00
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
/* Only set the gateway address if it was explicitly provided. */
|
|
|
|
|
|
|
|
if (gwip != NULL)
|
|
|
|
{
|
|
|
|
ninfo("Gateway: %s\n", gwip);
|
|
|
|
inet_pton(AF_INET6, gwip, &addr6);
|
|
|
|
|
|
|
|
netlib_set_dripv6addr(ifname, &addr6);
|
2022-08-03 11:34:31 +02:00
|
|
|
gip6 = addr6;
|
2017-10-25 01:26:17 +02:00
|
|
|
}
|
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
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
if (gwip != NULL)
|
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
|
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
if (gip != 0)
|
2015-01-19 16:59:43 +01:00
|
|
|
{
|
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
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
netlib_set_dripv4addr(ifname, &addr);
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
|
|
|
#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
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
if (mask != NULL)
|
|
|
|
{
|
2020-03-30 13:55:46 +02:00
|
|
|
ninfo("Netmask: %s\n", mask);
|
2017-10-25 01:26:17 +02:00
|
|
|
inet_pton(AF_INET6, mask, &addr6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ninfo("Netmask: Default\n");
|
|
|
|
inet_pton(AF_INET6, "ffff:ffff:ffff:ffff::", &addr6);
|
|
|
|
}
|
|
|
|
|
|
|
|
netlib_set_ipv6netmask(ifname, &addr6);
|
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
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
if (mask != NULL)
|
2015-01-19 16:59:43 +01:00
|
|
|
{
|
2017-10-25 01:26:17 +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
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
netlib_set_ipv4netmask(ifname, &addr);
|
2015-01-19 16:59:43 +01:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
UNUSED(ifname); /* Not used in all configurations */
|
2017-07-31 00:03:56 +02:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_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
|
|
|
{
|
2022-08-03 11:34:31 +02:00
|
|
|
if (dns != NULL)
|
|
|
|
{
|
|
|
|
ninfo("DNS: %s\n", dns);
|
|
|
|
inet_pton(AF_INET6, dns, &addr6);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ninfo("DNS: Default\n");
|
|
|
|
addr6 = gip6;
|
|
|
|
}
|
|
|
|
|
|
|
|
netlib_set_ipv6dnsaddr(&addr6);
|
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
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
if (dns != NULL)
|
2015-01-19 16:59:43 +01:00
|
|
|
{
|
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 */
|
2019-04-29 18:22:56 +02:00
|
|
|
#endif /* CONFIG_NETINIT_DHCPC || CONFIG_NETINIT_DNS */
|
2011-05-21 20:25:31 +02:00
|
|
|
|
2019-04-29 18:22:56 +02:00
|
|
|
#if defined(CONFIG_NETINIT_DHCPC)
|
2012-11-27 17:26:54 +01:00
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
if (!gip)
|
|
|
|
{
|
2017-08-02 21:13:19 +02:00
|
|
|
netlib_getmacaddr("eth0", macaddr);
|
2012-11-27 17:26:54 +01:00
|
|
|
|
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
2017-08-02 21:13:19 +02:00
|
|
|
handle = dhcpc_open("eth0", &macaddr, IFHWADDRLEN);
|
2012-11-27 17:26:54 +01:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (handle != NULL)
|
2012-11-27 17:26:54 +01:00
|
|
|
{
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
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
|
|
|
|
|
2017-07-14 21:43:04 +02:00
|
|
|
#if !defined(CONFIG_NET_IPv4) && !defined(CONFIG_NET_IPv6)
|
|
|
|
UNUSED(hostip);
|
|
|
|
UNUSED(mask);
|
|
|
|
UNUSED(gwip);
|
2018-03-31 17:27:52 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
2017-07-14 21:43:04 +02:00
|
|
|
UNUSED(gip);
|
|
|
|
#endif
|
2022-08-03 11:34:31 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
UNUSED(gip6);
|
|
|
|
#endif
|
2017-07-14 21:43:04 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return OK;
|
2015-07-13 19:19:02 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_nslookup
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NSH_DISABLE_NSLOOKUP)
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_nslookup(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2015-07-13 19:19:02 +02:00
|
|
|
{
|
2020-03-30 12:06:47 +02:00
|
|
|
FAR struct addrinfo *info;
|
|
|
|
FAR struct addrinfo *next;
|
2015-07-13 19:19:02 +02:00
|
|
|
char buffer[48];
|
2020-03-30 12:06:47 +02:00
|
|
|
int ret;
|
2015-07-13 19:19:02 +02:00
|
|
|
|
|
|
|
/* We should be guaranteed this by the command line parser */
|
|
|
|
|
|
|
|
DEBUGASSERT(argc == 2);
|
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
/* Get the matching address + canonical name */
|
2015-07-13 19:19:02 +02:00
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
ret = getaddrinfo(argv[1], NULL, NULL, &info);
|
|
|
|
if (ret != OK)
|
2015-07-13 19:19:02 +02:00
|
|
|
{
|
2020-03-30 12:06:47 +02:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed,
|
|
|
|
argv[0], "getaddrinfo", NSH_HERRNO_OF(ret));
|
|
|
|
return ERROR;
|
2015-07-13 19:19:02 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
for (next = info; next != NULL; next = next->ai_next)
|
2015-07-13 19:19:02 +02:00
|
|
|
{
|
2020-03-30 12:06:47 +02:00
|
|
|
/* Convert the address to a string */
|
2015-07-13 19:19:02 +02:00
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
ret = getnameinfo(next->ai_addr, next->ai_addrlen,
|
|
|
|
buffer, sizeof(buffer), NULL, 0, NI_NUMERICHOST);
|
|
|
|
if (ret != OK)
|
2015-07-13 19:19:02 +02:00
|
|
|
{
|
2020-03-30 12:06:47 +02:00
|
|
|
freeaddrinfo(info);
|
|
|
|
nsh_error(vtbl, g_fmtcmdfailed,
|
|
|
|
argv[0], "getnameinfo", NSH_HERRNO_OF(ret));
|
2015-07-13 19:19:02 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
/* Print the host name / address mapping */
|
2015-07-13 19:19:02 +02:00
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
nsh_output(vtbl, "Host: %s Addr: %s\n", next->ai_canonname, buffer);
|
2015-07-13 19:19:02 +02:00
|
|
|
}
|
|
|
|
|
2020-03-30 12:06:47 +02:00
|
|
|
freeaddrinfo(info);
|
2015-07-13 19:19:02 +02:00
|
|
|
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)
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_arp(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2022-12-06 06:42:15 +01:00
|
|
|
enum opt_type_e
|
|
|
|
{
|
|
|
|
OPT_TYPE_ARP_LIST,
|
|
|
|
OPT_TYPE_ARP_GET,
|
|
|
|
OPT_TYPE_ARP_DELETE,
|
|
|
|
OPT_TYPE_ARP_SET
|
|
|
|
} opt_type = OPT_TYPE_ARP_LIST;
|
|
|
|
|
2016-02-08 21:49:05 +01:00
|
|
|
struct sockaddr_in inaddr;
|
|
|
|
struct ether_addr mac;
|
2022-12-06 06:42:15 +01:00
|
|
|
FAR const char *ifname = NULL;
|
2022-12-11 14:36:23 +01:00
|
|
|
bool badarg = false;
|
2022-12-06 06:42:15 +01:00
|
|
|
int option;
|
2016-02-08 21:49:05 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Forms:
|
|
|
|
*
|
2022-12-06 06:42:15 +01:00
|
|
|
* arp [-i <ifname>]
|
|
|
|
* arp [-i <ifname>] -a <ipaddr>
|
|
|
|
* arp [-i <ifname>] -d <ipdaddr>
|
|
|
|
* arp [-i <ifname>] -s <ipaddr> <hwaddr>
|
2016-02-08 21:49:05 +01:00
|
|
|
*/
|
|
|
|
|
2022-05-05 05:46:50 +02:00
|
|
|
memset(&inaddr, 0, sizeof(inaddr));
|
2022-12-06 06:42:15 +01:00
|
|
|
|
|
|
|
while ((option = getopt(argc, argv, "adsi:")) != ERROR)
|
|
|
|
{
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
opt_type = OPT_TYPE_ARP_GET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
opt_type = OPT_TYPE_ARP_DELETE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
opt_type = OPT_TYPE_ARP_SET;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
ifname = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2022-12-11 14:36:23 +01:00
|
|
|
badarg = true;
|
|
|
|
break;
|
2022-12-06 06:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 14:36:23 +01:00
|
|
|
if (badarg)
|
|
|
|
{
|
|
|
|
goto errout_invalid;
|
|
|
|
}
|
|
|
|
|
2019-11-03 23:19:14 +01:00
|
|
|
#ifdef CONFIG_NETLINK_ROUTE
|
2022-12-06 06:42:15 +01:00
|
|
|
if (opt_type == OPT_TYPE_ARP_LIST)
|
2019-11-03 23:19:14 +01:00
|
|
|
{
|
2022-12-11 14:36:23 +01:00
|
|
|
FAR struct arpreq *arptab;
|
2019-11-03 23:19:14 +01:00
|
|
|
size_t arpsize;
|
|
|
|
ssize_t nentries;
|
|
|
|
char ipaddr[16];
|
|
|
|
char ethaddr[24];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Allocate a buffer to hold the ARP table */
|
|
|
|
|
2022-12-11 14:36:23 +01:00
|
|
|
arpsize = CONFIG_NET_ARPTAB_SIZE * sizeof(struct arpreq);
|
|
|
|
arptab = (FAR struct arpreq *)malloc(arpsize);
|
2019-11-03 23:19:14 +01:00
|
|
|
if (arptab == NULL)
|
|
|
|
{
|
|
|
|
nsh_error(vtbl, g_fmtcmdoutofmemory, argv[0]);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the ARP table */
|
|
|
|
|
|
|
|
nentries = netlib_get_arptable(arptab, CONFIG_NET_ARPTAB_SIZE);
|
|
|
|
if (nentries < 0)
|
|
|
|
{
|
2020-01-31 15:13:45 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], netlib_get_arptable,
|
2019-11-03 23:19:14 +01:00
|
|
|
NSH_ERRNO_OF(-nentries));
|
|
|
|
free(arptab);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump the ARP table
|
|
|
|
*
|
|
|
|
* xx.xx.xx.xx xx:xx:xx:xx:xx:xx xxxxxxxx[xxxxxxxx]
|
|
|
|
*/
|
|
|
|
|
2022-12-11 14:36:23 +01:00
|
|
|
nsh_output(vtbl, "%-12s %-17s %s\n",
|
|
|
|
"IP Address", "Ethernet Address", "Interface");
|
2019-11-03 23:19:14 +01:00
|
|
|
|
|
|
|
for (i = 0; i < nentries; i++)
|
|
|
|
{
|
2022-12-11 14:36:23 +01:00
|
|
|
FAR struct sockaddr_in *addr;
|
2019-11-03 23:19:14 +01:00
|
|
|
FAR uint8_t *ptr;
|
|
|
|
|
2022-12-11 14:36:23 +01:00
|
|
|
if (ifname != NULL &&
|
|
|
|
strcmp(ifname, (FAR char *)arptab[i].arp_dev) != 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-03 23:19:14 +01:00
|
|
|
/* Convert the IPv4 address to a string */
|
|
|
|
|
2022-12-11 14:36:23 +01:00
|
|
|
addr = (FAR struct sockaddr_in *)&arptab[i].arp_pa;
|
|
|
|
ptr = (FAR uint8_t *)&addr->sin_addr.s_addr;
|
2019-11-03 23:19:14 +01:00
|
|
|
snprintf(ipaddr, 16, "%u.%u.%u.%u",
|
|
|
|
ptr[0], ptr[1], ptr[2], ptr[3]);
|
|
|
|
|
|
|
|
/* Convert the MAC address string to a binary */
|
|
|
|
|
|
|
|
snprintf(ethaddr, 24, "%02x:%02x:%02x:%02x:%02x:%02x",
|
2022-12-11 14:36:23 +01:00
|
|
|
(uint8_t)arptab[i].arp_ha.sa_data[0],
|
|
|
|
(uint8_t)arptab[i].arp_ha.sa_data[1],
|
|
|
|
(uint8_t)arptab[i].arp_ha.sa_data[2],
|
|
|
|
(uint8_t)arptab[i].arp_ha.sa_data[3],
|
|
|
|
(uint8_t)arptab[i].arp_ha.sa_data[4],
|
|
|
|
(uint8_t)arptab[i].arp_ha.sa_data[5]);
|
|
|
|
|
|
|
|
nsh_output(vtbl, "%-12s %-17s %s\n",
|
|
|
|
ipaddr, ethaddr, arptab[i].arp_dev);
|
2019-11-03 23:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(arptab);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2022-12-06 06:42:15 +01:00
|
|
|
if (opt_type == OPT_TYPE_ARP_GET)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2022-08-01 11:18:52 +02:00
|
|
|
char hwaddr[20];
|
|
|
|
|
2022-12-06 06:42:15 +01:00
|
|
|
if (argc - optind < 1)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2022-12-06 06:42:15 +01:00
|
|
|
goto errout_missing;
|
2016-02-08 21:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Show the corresponding hardware address */
|
|
|
|
|
|
|
|
inaddr.sin_family = AF_INET;
|
2016-02-09 18:24:30 +01:00
|
|
|
inaddr.sin_port = 0;
|
2022-12-06 06:42:15 +01:00
|
|
|
inaddr.sin_addr.s_addr = inet_addr(argv[optind]);
|
2016-02-08 21:49:05 +01:00
|
|
|
|
2022-12-06 06:42:15 +01:00
|
|
|
ret = netlib_get_arpmapping(&inaddr, mac.ether_addr_octet, ifname);
|
2016-02-08 21:49:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_cmdfaild;
|
|
|
|
}
|
|
|
|
|
2022-08-01 11:18:52 +02:00
|
|
|
nsh_output(vtbl, "HWaddr: %s\n", ether_ntoa_r(&mac, hwaddr));
|
2016-02-08 21:49:05 +01:00
|
|
|
}
|
2022-12-06 06:42:15 +01:00
|
|
|
else if (opt_type == OPT_TYPE_ARP_DELETE)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2022-12-06 06:42:15 +01:00
|
|
|
if (argc - optind < 1)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2022-12-06 06:42:15 +01:00
|
|
|
goto errout_missing;
|
2016-02-08 21:49:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2022-12-06 06:42:15 +01:00
|
|
|
inaddr.sin_addr.s_addr = inet_addr(argv[optind]);
|
2016-02-08 21:49:05 +01:00
|
|
|
|
2022-12-06 06:42:15 +01:00
|
|
|
ret = netlib_del_arpmapping(&inaddr, ifname);
|
2016-02-08 21:49:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_cmdfaild;
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 06:42:15 +01:00
|
|
|
else if (opt_type == OPT_TYPE_ARP_SET)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
2022-12-06 06:42:15 +01:00
|
|
|
if (argc - optind < 2)
|
2016-02-08 21:49:05 +01:00
|
|
|
{
|
|
|
|
goto errout_missing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the MAC address string to a binary */
|
|
|
|
|
2022-12-06 06:42:15 +01:00
|
|
|
if (!netlib_ethaddrconv(argv[optind + 1], 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;
|
2022-12-06 06:42:15 +01:00
|
|
|
inaddr.sin_addr.s_addr = inet_addr(argv[optind]);
|
2016-02-08 21:49:05 +01:00
|
|
|
|
2022-12-06 06:42:15 +01:00
|
|
|
ret = netlib_set_arpmapping(&inaddr, mac.ether_addr_octet, ifname);
|
2016-02-08 21:49:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_cmdfaild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout_invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
2020-03-30 13:55:46 +02:00
|
|
|
/* Error exits */
|
2016-02-08 21:49:05 +01:00
|
|
|
|
|
|
|
errout_cmdfaild:
|
2016-02-08 23:29:54 +01:00
|
|
|
if (ret == -ENOENT)
|
|
|
|
{
|
2022-12-06 06:42:15 +01:00
|
|
|
nsh_error(vtbl, g_fmtnosuch, argv[0], "ARP entry", argv[optind]);
|
2016-02-08 23:29:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "ioctl", NSH_ERRNO);
|
2016-02-08 23:29:54 +01:00
|
|
|
}
|
|
|
|
|
2016-02-08 21:49:05 +01:00
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
errout_missing:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2016-02-08 21:49:05 +01:00
|
|
|
return ERROR;
|
|
|
|
|
|
|
|
errout_invalid:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2016-02-08 21:49:05 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cmd_put
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_UDP
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_PUT
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_put(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
struct tftpc_args_s args;
|
2017-10-25 01:26:17 +02:00
|
|
|
FAR char *fullpath;
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "tftpput", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-11 20:10:10 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2011-03-18 21:35:31 +01:00
|
|
|
#ifndef CONFIG_NSH_DISABLE_WGET
|
2022-09-22 05:38:12 +02:00
|
|
|
int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2017-10-25 01:26:17 +02:00
|
|
|
FAR char *localfile = NULL;
|
|
|
|
FAR char *allocfile = NULL;
|
|
|
|
FAR char *buffer = NULL;
|
2017-11-01 04:18:40 +01:00
|
|
|
FAR char *fullpath = NULL;
|
2017-10-25 01:26:17 +02:00
|
|
|
FAR char *url;
|
|
|
|
FAR const char *fmt;
|
2011-03-18 20:46:25 +01:00
|
|
|
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 ':':
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
badarg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
default:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtarginvalid, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
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];
|
|
|
|
}
|
2021-12-31 10:58:04 +01:00
|
|
|
else if (optind < argc)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
fmt = g_fmttoomanyargs;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt = g_fmtargrequired;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the local file name */
|
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (localfile == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2020-03-30 13:55:46 +02:00
|
|
|
fd = open(fullpath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate an I/O buffer */
|
|
|
|
|
2020-05-19 02:00:58 +02:00
|
|
|
buffer = malloc(CONFIG_NSH_WGET_BUFF_SIZE);
|
2017-10-25 01:26:17 +02:00
|
|
|
if (buffer == NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
fmt = g_fmtcmdoutofmemory;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And perform the wget */
|
|
|
|
|
2020-09-29 02:42:40 +02:00
|
|
|
struct webclient_context ctx;
|
|
|
|
webclient_set_defaults(&ctx);
|
|
|
|
ctx.method = "GET";
|
|
|
|
ctx.url = url;
|
|
|
|
ctx.buffer = buffer;
|
|
|
|
ctx.buflen = CONFIG_NSH_WGET_BUFF_SIZE;
|
|
|
|
ctx.sink_callback = wget_callback;
|
|
|
|
ctx.sink_callback_arg = (FAR void *)((intptr_t)fd);
|
|
|
|
ret = webclient_perform(&ctx);
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-09-29 02:42:40 +02:00
|
|
|
errno = -ret;
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "wget", NSH_ERRNO);
|
2011-03-18 20:46:25 +01:00
|
|
|
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
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (allocfile != NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
free(allocfile);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (fullpath != NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
free(fullpath);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2017-10-25 01:26:17 +02:00
|
|
|
if (buffer != NULL)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
|
|
|
free(buffer);
|
|
|
|
}
|
2013-12-29 22:02:51 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
errout:
|
2018-12-09 01:53:54 +01:00
|
|
|
nsh_error(vtbl, fmt, argv[0]);
|
2011-03-18 20:46:25 +01:00
|
|
|
ret = ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET */
|