"examples/nsh/nsh_romfsetc.c" did not exist on "3f1b80218e30ec455844da6963c3960d669a0125"
Newer
Older
/****************************************************************************
* examples/nsh/nsh_netcmds.c
*
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* 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.
* 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>
#include <fcntl.h> /* Needed for open */
#include <libgen.h> /* Needed for basename */
#include <net/ethernet.h>
#include <net/uip/uip.h>
#include <net/uip/uip-arch.h>
#include <netinet/ether.h>
#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>
#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
#include "nsh.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* 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
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
#if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \
!defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS)
static uint16 g_pingid = 0;
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* 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
/****************************************************************************
* Name: uip_statistics
****************************************************************************/
#if defined(CONFIG_NET_STATISTICS) && !defined(CONFIG_EXAMPLES_NSH_DISABLE_IFCONFIG)
static inline void uip_statistics(FAR struct nsh_vtbl_s *vtbl)
nsh_output(vtbl, " Checksum %04x",uip_stat.ip.chkerr);
uip_stat.tcp.rst, uip_stat.tcp.synrst);
#endif
nsh_output(vtbl, " Rexmit ---- %04x",uip_stat.tcp.rexmit);
/****************************************************************************
* Name: ifconfig_callback
****************************************************************************/
int ifconfig_callback(FAR struct uip_driver_s *dev, void *arg)
{
nsh_output(vtbl, "%s\tHWaddr %s\n", dev->d_ifname, ether_ntoa(&dev->d_mac));
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/****************************************************************************
* 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;
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
/****************************************************************************
* 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
/****************************************************************************
* 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;
/* 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 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);
}
/****************************************************************************
* Name: cmd_ifconfig
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_IFCONFIG
int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv)
netdev_foreach(ifconfig_callback, vtbl);
uip_statistics(vtbl);
/****************************************************************************
* 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
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;
uint16 id;
int count = 10;
int option;
int seqno;
int replies = 0;
int elapsed;
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':
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
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 */
nsh_output(vtbl, "PING %s %d bytes of data\n", staddr, DEFAULT_PING_DATALEN);
/* Send the ECHO request and wait for the response */
next = g_system_timer;
seqno = uip_ping(ipaddr, id, i, DEFAULT_PING_DATALEN, dsec);
/* Was any response returned? We can tell if a non-negative sequence
* number was returned.
*/
if (seqno >= 0 && seqno <= i)
/* 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 */
nsh_output(vtbl, "%d bytes from %s: icmp_seq=%d time=%d ms\n",
DEFAULT_PING_DATALEN, staddr, seqno, elapsed);
replies++;
}
/* Wait for the remainder of the interval. If the last seqno<i,
* then this is a bad idea... we will probably lose the response
* to the current request!
*/
elapsed = TICK2DSEC(g_system_timer - next);
if (elapsed < dsec)
{
usleep(100000*dsec);
}
}
/* Calculate the percentage of lost packets */
tmp = (100*(count - replies) + (count >> 1)) / count;
nsh_output(vtbl, "%d packets transmitted, %d received, %d%% packet loss, time %d ms\n",
count, replies, tmp, elapsed);
return OK;
errout:
nsh_output(vtbl, fmt, argv[0]);
return ERROR;
}
/****************************************************************************
* Name: cmd_put
****************************************************************************/
#if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0
#ifndef CONFIG_EXAMPLES_NSH_DISABLE_PUT
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/****************************************************************************
* 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);
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
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