Skip to content
Snippets Groups Projects
nsh_netcmds.c 19.8 KiB
Newer Older
patacongo's avatar
patacongo committed
/****************************************************************************
 * examples/nsh/nsh_netcmds.c
 *
patacongo's avatar
patacongo committed
 *   Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
patacongo's avatar
patacongo committed
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * 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.
patacongo's avatar
patacongo committed
 * 3. Neither the name NuttX nor the names of its contributors may be
patacongo's avatar
patacongo committed
 *    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>
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET
patacongo's avatar
patacongo committed

#include <sys/types.h>
patacongo's avatar
patacongo committed
#include <sys/stat.h>    /* Needed for open */
patacongo's avatar
patacongo committed
#include <stdio.h>
#include <stdlib.h>
patacongo's avatar
patacongo committed
#include <unistd.h>
#include <string.h>
patacongo's avatar
patacongo committed
#include <sched.h>
patacongo's avatar
patacongo committed
#include <fcntl.h>       /* Needed for open */
#include <libgen.h>      /* Needed for basename */
#include <errno.h>
patacongo's avatar
patacongo committed

#include <nuttx/net.h>
patacongo's avatar
patacongo committed
#include <nuttx/clock.h>
patacongo's avatar
patacongo committed
#include <net/ethernet.h>
#include <net/uip/uip.h>
#include <net/uip/uip-arch.h>
#include <netinet/ether.h>

patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_STATISTICS
#  include <net/uip/uip.h>
patacongo's avatar
patacongo committed
#endif

patacongo's avatar
patacongo committed
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
   !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS)
#  include <net/uip/uip-lib.h>
#endif

#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
#  include <net/uip/uip-lib.h>
#  include <net/uip/tftp.h>
patacongo's avatar
patacongo committed
#endif

patacongo's avatar
patacongo committed
#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0
#  ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET
#    include <net/uip/uip-lib.h>
#    include <net/uip/webclient.h>
#  endif
#endif

patacongo's avatar
patacongo committed
#include "nsh.h"

/****************************************************************************
 * Definitions
 ****************************************************************************/

patacongo's avatar
patacongo committed
#define DEFAULT_PING_DATALEN 56

patacongo's avatar
patacongo committed
/****************************************************************************
 * Private Types
 ****************************************************************************/

#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
struct tftpc_args_s
{
  boolean     binary;    /* TRUE:binary ("octect") FALSE:text ("netascii") */
  boolean     allocated; /* TRUE: destpath is allocated */
  char       *destpath;  /* Path at destination */
  const char *srcpath;   /* Path at src */
  in_addr_t   ipaddr;    /* Host IP address */
};
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

patacongo's avatar
patacongo committed
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
   !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS)
static uint16 g_pingid = 0;
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: ping_newid
 ****************************************************************************/

#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
   !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS)
static inline uint16 ping_newid(void)
{
  irqstate_t save = irqsave();
  uint16 ret = ++g_pingid;
  irqrestore(save);
  return ret;
}
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: uip_statistics
 ****************************************************************************/

#if defined(CONFIG_NET_STATISTICS) && !defined(CONFIG_EXAMPLES_NSH_DISABLE_IFCONFIG)
patacongo's avatar
patacongo committed
static inline void uip_statistics(FAR struct nsh_vtbl_s *vtbl)
patacongo's avatar
patacongo committed
{
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "uIP         IP ");
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  TCP");
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  UDP");
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  ICMP");
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed

  /* Received packets */

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "Received    %04x",uip_stat.ip.recv);
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.tcp.recv);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.udp.recv);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.icmp.recv);
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed

  /* Dropped packets */

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "Dropped     %04x",uip_stat.ip.drop);
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.tcp.drop);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.udp.drop);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.icmp.drop);
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  IP        VHL: %04x HBL: %04x\n",
patacongo's avatar
patacongo committed
             uip_stat.ip.vhlerr, uip_stat.ip.hblenerr);
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "            LBL: %04x Frg: %04x\n",
patacongo's avatar
patacongo committed
             uip_stat.ip.lblenerr, uip_stat.ip.fragerr);

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  Checksum  %04x",uip_stat.ip.chkerr);
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.tcp.chkerr);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.udp.chkerr);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " ----");
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed

#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  TCP       ACK: %04x SYN: %04x\n", 
patacongo's avatar
patacongo committed
            uip_stat.tcp.ackerr, uip_stat.tcp.syndrop);
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "            RST: %04x %04x\n", 
patacongo's avatar
patacongo committed
            uip_stat.tcp.rst, uip_stat.tcp.synrst);
#endif

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  Type      %04x",uip_stat.ip.protoerr);
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " ----");
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " ----");
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.icmp.typeerr);
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed

  /* Sent packets */

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "Sent        ----",uip_stat.ip.sent);
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.tcp.sent);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.udp.sent);
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " %04x",uip_stat.icmp.sent);
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed

#ifdef CONFIG_NET_TCP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "  Rexmit    ---- %04x",uip_stat.tcp.rexmit);
patacongo's avatar
patacongo committed
#ifdef CONFIG_NET_UDP
patacongo's avatar
patacongo committed
  nsh_output(vtbl, " ----");
patacongo's avatar
patacongo committed
#endif
#ifdef CONFIG_NET_ICMP
patacongo's avatar
patacongo committed
   nsh_output(vtbl, " ----");
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed
#endif
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\n");
patacongo's avatar
patacongo committed
}
#else
patacongo's avatar
patacongo committed
# define uip_statistics(vtbl)
patacongo's avatar
patacongo committed
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: ifconfig_callback
 ****************************************************************************/

int ifconfig_callback(FAR struct uip_driver_s *dev, void *arg)
{
patacongo's avatar
patacongo committed
  struct nsh_vtbl_s *vtbl = (struct nsh_vtbl_s*)arg;
patacongo's avatar
patacongo committed
  struct in_addr addr;

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "%s\tHWaddr %s\n", dev->d_ifname, ether_ntoa(&dev->d_mac));
patacongo's avatar
patacongo committed
  addr.s_addr = dev->d_ipaddr;
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "\tIPaddr:%s ", inet_ntoa(addr));
patacongo's avatar
patacongo committed
  addr.s_addr = dev->d_draddr;
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "DRaddr:%s ", inet_ntoa(addr));
patacongo's avatar
patacongo committed
  addr.s_addr = dev->d_netmask;
patacongo's avatar
patacongo committed
  nsh_output(vtbl, "Mask:%s\n\n", inet_ntoa(addr));
/****************************************************************************
 * 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;
  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':
            if (!uiplib_ipaddrconv(optarg, (FAR unsigned char*)&args->ipaddr))
              {
                nsh_output(vtbl, g_fmtarginvalid, argv[0]);
                goto errout;
              }
            break;

          case ':':
            fmt = g_fmtargrequired;
patacongo's avatar
patacongo committed
            goto errout;

          case '?':
          default:
patacongo's avatar
patacongo committed
            fmt = g_fmtarginvalid;
            goto errout;
        }
    }

  /* There should be exactly on parameter left on the command-line */

  if (optind == argc-1)
    {
      args->srcpath = argv[optind];
    }
  else if (optind >= argc)
    {
      fmt = g_fmttoomanyargs;
      goto errout;
    }
  else
    {
      fmt = g_fmtargrequired;
      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;
        }
      args->allocated = TRUE;
    }

  return OK;

errout:
  nsh_output(vtbl, fmt, argv[0]);
  return ERROR;
}
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: wget_callback
 ****************************************************************************/

#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET
static void wget_callback(FAR char **buffer, int offset, int datend,
                          FAR int *buflen, FAR void *arg)
{
  (void)write((int)arg, &((*buffer)[offset]), datend - offset);
}
#endif
#endif

patacongo's avatar
patacongo committed
/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: cmd_get
 ****************************************************************************/

#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_GET
int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
  struct tftpc_args_s args;
patacongo's avatar
patacongo committed
  char *fullpath;

  /* Parse the input parameter list */

  if (tftpc_parseargs(vtbl, argc, argv, &args) != OK)
    {
      return ERROR;
    }

patacongo's avatar
patacongo committed
  /* Get the full path to the local file */

  fullpath = nsh_getfullpath(vtbl, args.srcpath);

  /* Then perform the TFTP get operation */

patacongo's avatar
patacongo committed
  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);
    }
patacongo's avatar
patacongo committed
  free(fullpath);
  return OK;
}
#endif
patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: cmd_ifconfig
 ****************************************************************************/

#ifndef CONFIG_EXAMPLES_NSH_DISABLE_IFCONFIG
int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
patacongo's avatar
patacongo committed
  netdev_foreach(ifconfig_callback, vtbl);
  uip_statistics(vtbl);
  return OK;
patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: cmd_ping
 ****************************************************************************/

#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
   !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS)
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_PING
patacongo's avatar
patacongo committed
int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
{
  FAR const char *fmt = g_fmtarginvalid;
  const char *staddr;
  uip_ipaddr_t ipaddr;
  uint32 start;
  uint32 next;
patacongo's avatar
patacongo committed
  uint32 dsec  = 10;
patacongo's avatar
patacongo committed
  uint16 id;
  int count    = 10;
  int option;
  int seqno;
  int replies  = 0;
  int elapsed;
patacongo's avatar
patacongo committed
  int tmp;
patacongo's avatar
patacongo committed
  int i;

  /* Get the ping options */

  while ((option = getopt(argc, argv, ":c:i:")) != ERROR)
    {
      switch (option)
        {
          case 'c':
            count = atoi(optarg);
            if (count < 1 || count > 10000)
              {
                fmt = g_fmtargrange;
                goto errout;
              }
            break;

          case 'i':
patacongo's avatar
patacongo committed
            tmp = atoi(optarg);
            if (tmp < 1 || tmp >= 4294)
patacongo's avatar
patacongo committed
              {
                fmt = g_fmtargrange;
                goto errout;
              }
patacongo's avatar
patacongo committed
            dsec = 10 * tmp;
patacongo's avatar
patacongo committed
            break;

          case ':':
            fmt = g_fmtargrequired;

          case '?':
          default:
            goto errout;
        }
    }

  /* There should be exactly on parameter left on the command-line */

  if (optind == argc-1)
    {
      staddr = argv[optind];
      if (!uiplib_ipaddrconv(staddr, (FAR unsigned char*)&ipaddr))
        {
          goto errout;
        }
    }
  else if (optind >= argc)
    {
      fmt = g_fmttoomanyargs;
      goto errout;
    }
  else
    {
      fmt = g_fmtargrequired;
      goto errout;
    }

  /* Get the ID to use */

  id = ping_newid();

  /* Loop for the specified count */

patacongo's avatar
patacongo committed
  nsh_output(vtbl, "PING %s %d bytes of data\n", staddr, DEFAULT_PING_DATALEN);
patacongo's avatar
patacongo committed
  start = g_system_timer;
patacongo's avatar
patacongo committed
  for (i = 1; i <= count; i++)
patacongo's avatar
patacongo committed
    {
patacongo's avatar
patacongo committed
      /* Send the ECHO request and wait for the response */

      next  = g_system_timer;
patacongo's avatar
patacongo committed
      seqno = uip_ping(ipaddr, id, i, DEFAULT_PING_DATALEN, dsec);
patacongo's avatar
patacongo committed

      /* Was any response returned? We can tell if a non-negative sequence
       * number was returned.
       */

      if (seqno >= 0 && seqno <= i)
patacongo's avatar
patacongo committed
        {
patacongo's avatar
patacongo committed
          /* Get the elpased 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 elpased time.
           */

          elapsed = TICK2MSEC(g_system_timer - next);
          if (seqno < i)
            {
              elapsed += 100*dsec*(i - seqno);
            }

          /* Report the receipt of the reply */

patacongo's avatar
patacongo committed
          nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%d ms\n",
                     DEFAULT_PING_DATALEN, staddr, seqno, elapsed);
          replies++;
        }
patacongo's avatar
patacongo committed

      /* 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!
       */

patacongo's avatar
patacongo committed
      elapsed = TICK2DSEC(g_system_timer - next);
      if (elapsed < dsec)
        {
          usleep(100000*dsec);
        }
    }
patacongo's avatar
patacongo committed

  /* Get the total elapsed time */

patacongo's avatar
patacongo committed
  elapsed = TICK2MSEC(g_system_timer - start);

patacongo's avatar
patacongo committed
  /* Calculate the percentage of lost packets */

  tmp = (100*(count - replies) + (count >> 1)) / count;

  nsh_output(vtbl, "%d packets transmitted, %d received, %d%% packet loss, time %d ms\n",
             count, replies, tmp, elapsed);
patacongo's avatar
patacongo committed
  return OK;

errout:
  nsh_output(vtbl, fmt, argv[0]);
  return ERROR;
}
patacongo's avatar
patacongo committed
#endif /* CONFIG_NET_ICMP && CONFIG_NET_ICMP_PING */

/****************************************************************************
 * Name: cmd_put
 ****************************************************************************/

#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_PUT
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);
    }
  free(fullpath);
  return OK;
}
#endif
patacongo's avatar
patacongo committed
/****************************************************************************
 * Name: cmd_wget
 ****************************************************************************/

#if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_WGET
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;
  char *fmt;
  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 ':':
            fmt = g_fmtargrequired;
            goto errout;

          case '?':
          default:
            fmt = g_fmtarginvalid;
            goto errout;
        }
    }

  /* There should be exactly on parameter left on the command-line */

  if (optind == argc-1)
    {
      url = argv[optind];
    }
  else if (optind >= argc)
    {
      fmt = g_fmttoomanyargs;
      goto errout;
    }
  else
    {
      fmt = g_fmtargrequired;
      goto errout;
    }

  /* Get the local file name */

  if (!localfile)
    {
      allocfile = strdup(url);
      localfile = basename(allocfile);
    }

  /* Get the full path to the local file */

  fullpath = nsh_getfullpath(vtbl, localfile);

  /* Open the the local file for writing */

  fd = open(fullpath, O_WRONLY|O_CREAT|O_TRUNC, 0644);
  if (fd < 0)
    {
      nsh_output(vtbl, g_fmtcmdfailed, argv[0], "open", NSH_ERRNO);
      ret = ERROR;
      goto exit;
    }

  /* Allocate an I/O buffer */

  buffer = malloc(512);
  if (!buffer)
    {
      fmt = g_fmtcmdoutofmemory;
      goto errout;
    }

  /* And perform the wget */

  ret = wget(url, buffer, 512, wget_callback, (FAR void *)fd);
patacongo's avatar
patacongo committed
  if (ret < 0)
    {
      nsh_output(vtbl, g_fmtcmdfailed, argv[0], "wget", NSH_ERRNO);
      goto exit;
     }

  /* Free allocated resources */

exit:
  if (fd >= 0)
    {
      close(fd);
    }
  if (allocfile)
    {
      free(allocfile);
    }
  if (fullpath)
    {
      free(fullpath);
    }
  if (buffer)
    {
      free(buffer);
    }
  return ret;

errout:
  nsh_output(vtbl, fmt, argv[0]);
  ret = ERROR;
  goto exit;
}
#endif
#endif

patacongo's avatar
patacongo committed
#endif /* CONFIG_NET */