Add a simple shell and configuration to verify the FTP client library

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3656 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-06-01 23:09:29 +00:00
parent d6c53df4f6
commit c4b747f9f9
17 changed files with 1034 additions and 38 deletions

View File

@ -53,4 +53,8 @@
* nshlib/nsh_netcmds.c: If a network device name and IP address are provided
with the ifconfig command, then this command will now set the network address.
(Contributed by Yu Qiang).
* netutils/ftpc: A library to support client-side FTP.
* examples/ftpc: A simple add-on to the NSH. From NSH, you can start
this simple FTP shell to transfer files to/from a remote FTP server.

View File

@ -37,7 +37,7 @@
# Sub-directories
SUBDIRS = dhcpd hello helloxx hidkbd igmp mm mount nettest nsh null nx \
SUBDIRS = dhcpd ftpc hello helloxx hidkbd igmp mm mount nettest nsh null nx \
nxffs nxflat ostest pashello pipe poll rgmp romfs sendmail serloop \
thttpd udp uip usbserial usbstorage wget wlan

View File

@ -48,6 +48,21 @@ examples/dhcpd
CONFIGURED_APPS += uiplib
examples/ftpc
^^^^^^^^^^^^^
This is a simple FTP client shell used to exercise the capabilities
of the FTPC library (apps/netutils/ftpc). This example is configured
to that it will only work as a "built-in" program that can be run from
NSH when CONFIG_NSH_BUILTIN_APPS is defined.
From NSH, the startup command line is then:
ftpc xx.xx.xx.xx[:pp]
where xx.xx.xx.xx is the IP address of the FTP server and pp is an
optional port number.
examples/hello
^^^^^^^^^^^^^^

105
examples/ftpc/Makefile Normal file
View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/ftpc/Makefile
#
# Copyright (C) 2011 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.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# FTPC Client Application
APPNAME = ftpc
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 4096
ASRCS =
CSRCS = ftpc_main.c ftpc_cmds.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: context depend clean distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
# Register application
.context:
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
context: .context
# Create dependencies
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f .context Make.dep .depend
-include Make.dep

125
examples/ftpc/ftpc.h Normal file
View File

@ -0,0 +1,125 @@
/****************************************************************************
* apps/examples/ftpc/ftpc.h
*
* Copyright (C) 2011 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.
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_FTPC_FTPC_H
#define __APPS_EXAMPLES_FTPC_FTPC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <apps/ftpc.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Maximum size of one command line */
#ifndef CONFIG_FTPC_LINELEN
# define CONFIG_FTPC_LINELEN 80
#endif
/* If CONFIG_STDIO_LINEBUFFER is defined, the STDIO buffer will be flushed
* on each new line. Otherwise, STDIO needs to be explicitly flushed to
* see the output in context.
*/
#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && \
CONFIG_STDIO_BUFFER_SIZE > 0 && !defined(CONFIG_STDIO_LINEBUFFER)
# define FFLUSH() fflush(stdout)
#else
# define FFLUSH()
#endif
/****************************************************************************
* Public Types
****************************************************************************/
typedef int (*cmd_t)(SESSION handle, int argc, char **argv);
/****************************************************************************
* Public Data
****************************************************************************/
extern const char g_ftpcgreeting[];
extern const char g_ftpcprompt[];
extern const char g_fmtcmdfailed[];
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Network initialization */
extern int ftpc_netinit(void);
/* Current working directory support */
extern char *ftpc_getfullpath(const char *relpath);
extern void ftpc_freefullpath(char *relpath);
/* FTP command handlers */
extern int cmd_login(SESSION handle, int argc, char **argv);
extern int cmd_quit(SESSION handle, int argc, char **argv);
extern int cmd_chdir(SESSION handle, int argc, char **argv);
extern int cmd_rpwd(SESSION handle, int argc, char **argv);
extern int cmd_cdup(SESSION handle, int argc, char **argv);
extern int cmd_mkdir(SESSION handle, int argc, char **argv);
extern int cmd_rmdir(SESSION handle, int argc, char **argv);
extern int cmd_unlink(SESSION handle, int argc, char **argv);
extern int cmd_chmod(SESSION handle, int argc, char **argv);
extern int cmd_rename(SESSION handle, int argc, char **argv);
extern int cmd_size(SESSION handle, int argc, char **argv);
extern int cmd_time(SESSION handle, int argc, char **argv);
extern int cmd_idle(SESSION handle, int argc, char **argv);
extern int cmd_noop(SESSION handle, int argc, char **argv);
extern int cmd_rhelp(SESSION handle, int argc, char **argv);
extern int cmd_rls(SESSION handle, int argc, char **argv);
extern int cmd_get(SESSION handle, int argc, char **argv);
extern int cmd_put(SESSION handle, int argc, char **argv);
#endif /* __APPS_EXAMPLES_FTPC_FTPC_H */

300
examples/ftpc/ftpc_cmds.c Executable file
View File

@ -0,0 +1,300 @@
/****************************************************************************
* examples/ftpc/ftpc_cmds.c
*
* Copyright (C) 2011 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 <stdio.h>
#include <stdlib.h>
#include <apps/ftpc.h>
#include "ftpc.h"
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_login
****************************************************************************/
int cmd_login(SESSION handle, int argc, char **argv)
{
struct ftpc_login_s login = {0, 0, 0, true};
login.uname = argv[1];
if (argc > 2)
{
login.pwd = argv[2];
}
return ftpc_login(handle, &login);
}
/****************************************************************************
* Name: cmd_quit
****************************************************************************/
int cmd_quit(SESSION handle, int argc, char **argv)
{
int ret = ftpc_quit(handle);
if (ret < 0)
{
printf("quit failed: %d\n", errno);
}
printf("Exitting...\n");
exit(0);
return ERROR;
}
/****************************************************************************
* Name: cmd_chdir
****************************************************************************/
int cmd_chdir(SESSION handle, int argc, char **argv)
{
return ftpc_chdir(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_rpwd
****************************************************************************/
int cmd_rpwd(SESSION handle, int argc, char **argv)
{
FAR char *pwd = ftpc_pwd(handle);
if (pwd)
{
printf("PWD: %s\n", pwd);
free(pwd);
return OK;
}
return ERROR;
}
/****************************************************************************
* Name: cmd_cdup
****************************************************************************/
int cmd_cdup(SESSION handle, int argc, char **argv)
{
return ftpc_cdup(handle);
}
/****************************************************************************
* Name: cmd_mkdir
****************************************************************************/
int cmd_mkdir(SESSION handle, int argc, char **argv)
{
return ftpc_mkdir(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_rmdir
****************************************************************************/
int cmd_rmdir(SESSION handle, int argc, char **argv)
{
return ftpc_rmdir(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_unlink
****************************************************************************/
int cmd_unlink(SESSION handle, int argc, char **argv)
{
return ftpc_unlink(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_chmod
****************************************************************************/
int cmd_chmod(SESSION handle, int argc, char **argv)
{
return ftpc_chmod(handle, argv[1], argv[2]);
}
/****************************************************************************
* Name: cmd_rename
****************************************************************************/
int cmd_rename(SESSION handle, int argc, char **argv)
{
return ftpc_rename(handle, argv[1], argv[2]);
}
/****************************************************************************
* Name: cmd_size
****************************************************************************/
int cmd_size(SESSION handle, int argc, char **argv)
{
uint64_t size = ftpc_filesize(handle, argv[1]);
printf("SIZE: %ull\n", size);
return OK;
}
/****************************************************************************
* Name: cmd_time
****************************************************************************/
int cmd_time(SESSION handle, int argc, char **argv)
{
time_t filetime = ftpc_filetime(handle, argv[1]);
printf("TIME: %ul\n", (long)filetime);
return OK;
}
/****************************************************************************
* Name: cmd_idle
****************************************************************************/
int cmd_idle(SESSION handle, int argc, char **argv)
{
unsigned int idletime = 0;
if (argc > 1)
{
idletime = atoi(argv[1]);
}
return ftpc_idle(handle, idletime);
}
/****************************************************************************
* Name: cmd_noop
****************************************************************************/
int cmd_noop(SESSION handle, int argc, char **argv)
{
return ftpc_noop(handle);
}
/****************************************************************************
* Name: cmd_rhelp
****************************************************************************/
int cmd_rhelp(SESSION handle, int argc, char **argv)
{
FAR const char *cmd = NULL;
int ret;
if (argc > 1)
{
cmd = argv[1];
}
ret = ftpc_help(handle, cmd);
if (ret == OK)
{
FAR char *msg = ftpc_response(handle);
puts(msg);
free(msg);
}
return ret;
}
/****************************************************************************
* Name: cmd_rls
****************************************************************************/
int cmd_rls(SESSION handle, int argc, char **argv)
{
FAR struct ftpc_dirlist_s *dirlist;
int i;
dirlist = ftpc_listdir(handle, argv[1]);
if (!dirlist)
{
return ERROR;
}
printf("%s/\n", argv[1]);
for (i = 0; i < dirlist->nnames; i++)
{
printf(" %s\n", dirlist->name[i]);
}
ftpc_dirfree(dirlist);
return OK;
}
/****************************************************************************
* Name: cmd_get
****************************************************************************/
int cmd_get(SESSION handle, int argc, char **argv)
{
FAR const char *lname = argv[1];
if (argc > 2)
{
lname = argv[2];
}
return ftpc_getfile(handle, argv[1], lname, FTPC_GET_NORMAL, FTPC_XFRMODE_ASCII);
}
/****************************************************************************
* Name: cmd_put
****************************************************************************/
int cmd_put(SESSION handle, int argc, char **argv)
{
FAR const char *rname = argv[1];
if (argc > 2)
{
rname = argv[2];
}
return ftp_putfile(handle, argv[1], rname, FTPC_PUT_NORMAL, FTPC_XFRMODE_ASCII);
}

410
examples/ftpc/ftpc_main.c Executable file
View File

@ -0,0 +1,410 @@
/****************************************************************************
* examples/ftpc/ftpc_main.c
*
* Copyright (C) 2011 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <apps/ftpc.h>
#include "ftpc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define FTPC_MAX_ARGUMENTS 3
/****************************************************************************
* Public Types
****************************************************************************/
struct cmdmap_s
{
const char *cmd; /* Name of the command */
cmd_t handler; /* Function that handles the command */
uint8_t minargs; /* Minimum number of arguments (including command) */
uint8_t maxargs; /* Maximum number of arguments (including command) */
const char *usage; /* Usage instructions for 'help' command */
};
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_delim[] = " \t\n";
static int cmd_help(SESSION handle, int argc, char **argv);
static int cmd_unrecognized(SESSION handle, int argc, char **argv);
static const struct cmdmap_s g_cmdmap[] =
{
{ "cd", cmd_chdir, 2, 2, "<directory>" },
{ "chmod", cmd_chmod, 3, 3, "<permissions> <path>" },
{ "get", cmd_unlink, 2, 3, "<rname> [<lname>]" },
{ "help", cmd_help, 1, 2, "" },
{ "idle", cmd_idle, 1, 2, "[<idletime>]" },
{ "login", cmd_login, 2, 3, "<uname> [<password>]" },
{ "ls", cmd_login, 1, 2, "[<dirpath>]" },
{ "quit", cmd_login, 1, 1, "" },
{ "mkdir", cmd_mkdir, 2, 2, "<directory>" },
{ "noop", cmd_noop, 1, 1, "" },
{ "put", cmd_unlink, 2, 3, "<lname> [<rname>]" },
{ "pwd", cmd_rpwd, 1, 1, "" },
{ "rename", cmd_rename, 3, 3, "<oldname> <newname>" },
{ "rhelp", cmd_rhelp, 1, 2, "[<command>]" },
{ "rm", cmd_unlink, 2, 2, "" },
{ "rmdir", cmd_rmdir, 2, 2, "<directory>" },
{ "size", cmd_size, 2, 2, "<filepath>" },
{ "time", cmd_time, 2, 2, "<filepath>" },
{ "up", cmd_cdup, 1, 1, "" },
};
static char g_line[CONFIG_FTPC_LINELEN];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_help
****************************************************************************/
static int cmd_help(SESSION handle, int argc, char **argv)
{
const struct cmdmap_s *ptr;
printf("Local FTPC commands:\n");
for (ptr = g_cmdmap; ptr->cmd; ptr++)
{
if (ptr->usage)
{
printf(" %s %s\n", ptr->cmd, ptr->usage);
}
else
{
printf(" %s\n", ptr->cmd);
}
}
return OK;
}
/****************************************************************************
* Name: cmd_unrecognized
****************************************************************************/
static int cmd_unrecognized(SESSION handle, int argc, char **argv)
{
printf("Command %s unrecognized\n", argv[0]);
return ERROR;
}
/****************************************************************************
* Name: ftpc_argument
****************************************************************************/
char *ftpc_argument(char **saveptr)
{
char *pbegin = *saveptr;
char *pend = NULL;
const char *term;
/* Find the beginning of the next token */
for (;
*pbegin && strchr(g_delim, *pbegin) != NULL;
pbegin++);
/* If we are at the end of the string with nothing
* but delimiters found, then return NULL.
*/
if (!*pbegin)
{
return NULL;
}
/* Does the token begin with '#' -- comment */
else if (*pbegin == '#')
{
/* Return NULL meaning that we are at the end of the line */
*saveptr = pbegin;
pbegin = NULL;
}
else
{
/* Otherwise, we are going to have to parse to find the end of
* the token. Does the token begin with '"'?
*/
if (*pbegin == '"')
{
/* Yes.. then only another '"' can terminate the string */
pbegin++;
term = "\"";
}
else
{
/* No, then any of the usual terminators will terminate the argument */
term = g_delim;
}
/* Find the end of the string */
for (pend = pbegin + 1;
*pend && strchr(term, *pend) == NULL;
pend++);
/* pend either points to the end of the string or to
* the first delimiter after the string.
*/
if (*pend)
{
/* Turn the delimiter into a null terminator */
*pend++ = '\0';
}
/* Save the pointer where we left off */
*saveptr = pend;
}
/* Return the beginning of the token. */
return pbegin;
}
/****************************************************************************
* Name: ftpc_execute
****************************************************************************/
static int ftpc_execute(SESSION handle, int argc, char *argv[])
{
const struct cmdmap_s *cmdmap;
const char *cmd;
cmd_t handler = cmd_unrecognized;
int ret;
/* The form of argv is:
*
* argv[0]: The command name. This is argv[0] when the arguments
* are, finally, received by the command handler
* argv[1]: The beginning of argument (up to FTPC_MAX_ARGUMENTS)
* argv[argc]: NULL terminating pointer
*/
cmd = argv[0];
/* See if the command is one that we understand */
for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++)
{
if (strcmp(cmdmap->cmd, cmd) == 0)
{
/* Check if a valid number of arguments was provided. We
* do this simple, imperfect checking here so that it does
* not have to be performed in each command.
*/
if (argc < cmdmap->minargs)
{
/* Fewer than the minimum number were provided */
printf("Too few arguments for '%s'\n", cmd);
return ERROR;
}
else if (argc > cmdmap->maxargs)
{
/* More than the maximum number were provided */
printf("Too many arguments for '%s'\n", cmd);
return ERROR;
}
else
{
/* A valid number of arguments were provided (this does
* not mean they are right).
*/
handler = cmdmap->handler;
break;
}
}
}
ret = handler(handle, argc, argv);
if (ret < 0)
{
printf("%s failed: %d\n", cmd, errno);
}
return ret;
}
/****************************************************************************
* Name: ftpc_parse
****************************************************************************/
int ftpc_parse(SESSION handle, char *cmdline)
{
FAR char *argv[FTPC_MAX_ARGUMENTS];
FAR char *saveptr;
FAR char *cmd;
int argc;
int ret;
/* Initialize parser state */
memset(argv, 0, FTPC_MAX_ARGUMENTS*sizeof(FAR char *));
/* Parse out the command at the beginning of the line */
saveptr = cmdline;
cmd = ftpc_argument(&saveptr);
/* Check if any command was provided -OR- if command processing is
* currently disabled.
*/
if (!cmd)
{
/* An empty line is not an error */
return OK;
}
/* Parse all of the arguments following the command name. */
argv[0] = cmd;
for (argc = 1; argc < FTPC_MAX_ARGUMENTS; argc++)
{
argv[argc] = ftpc_argument(&saveptr);
if (!argv[argc])
{
break;
}
}
argv[argc] = NULL;
/* Check if the maximum number of arguments was exceeded */
if (argc > FTPC_MAX_ARGUMENTS)
{
printf("Too many arguments\n");
ret = -EINVAL;
}
else
{
/* Then execute the command */
ret = ftpc_execute(handle, argc, argv);
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int ftpc_main(int argc, char **argv, char **envp)
{
struct ftpc_connect_s connect = {{0}, 0};
SESSION handle;
FAR char *ptr;
if (argc != 2)
{
printf("Usage:\n");
printf(" %s xx.xx.xx.xx[:pp]\n", argv[0]);
printf("Where\n");
printf(" xx.xx.xx.xx is the IP address of the FTP server\n");
printf(" pp is option port to use with the FTP server\n");
exit(1);
}
ptr = strchr(argv[1], ':');
if (ptr)
{
*ptr = '\0';
connect.port = atoi(ptr+1);
}
connect.addr.s_addr = inet_addr(argv[1]);
handle = ftpc_connect(&connect);
if (!handle)
{
printf("Failed to connect to the server: %d\n", errno);
exit(1);
}
/* Present a greeting */
printf("FTPC:\n");
FFLUSH();
/* Then enter the command line parsing loop */
for (;;)
{
/* Display the prompt string */
fputs("FTPC>", stdout);
FFLUSH();
/* Get the next line of input */
if (fgets(g_line, CONFIG_FTPC_LINELEN, stdin))
{
/* Parse process the command */
(void)ftpc_parse(handle, g_line);
FFLUSH();
}
}
return 0;
}

View File

@ -43,7 +43,9 @@
#include <nuttx/config.h>
#include <stdbool.h>
#include <signal.h>
#include <time.h>
#include <netinet/in.h>
/****************************************************************************
@ -79,6 +81,10 @@
# define CONFIG_FTP_MAXPATH 256
#endif
#ifndef CONFIG_FTP_SIGNAL
# define CONFIG_FTP_SIGNAL SIGUSR1
#endif
/* Interface arguments ******************************************************/
/* These definitions describe how a put operation should be performed */
@ -185,7 +191,7 @@ EXTERN int ftpc_idle(SESSION handle, unsigned int idletime);
EXTERN int ftpc_noop(SESSION handle);
EXTERN int ftpc_help(SESSION handle, FAR const char *arg);
/* Director listings ********************************************************/
/* Directory listings *******************************************************/
EXTERN FAR struct ftpc_dirlist_s *ftpc_listdir(SESSION handle,
FAR const char *dirpath);

View File

@ -21,6 +21,7 @@ highly influenced by uIP) include:
dhcpd - Dynamic Host Configuration Protocol (DHCP) server
tftpc - TFTP client
ftpc - FTP client
thttpd - This is a port of Jef Poskanzer's THTTPD HTPPD server.
See http://acme.com/software/thttpd/.

View File

@ -40,6 +40,7 @@
#include <nuttx/config.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <debug.h>
@ -98,6 +99,7 @@ SESSION ftpc_connect(FAR struct ftpc_connect_s *server)
session->addr.s_addr = server->addr.s_addr;
session->port = server->port;
session->pid = getpid();
/* Create up a timer to prevent hangs */
@ -143,7 +145,7 @@ int ftpc_reconnect(FAR struct ftpc_session_s *session)
/* Set up a timer to prevent hangs */
ret = wd_start(session->wdog, session->conntimeo, ftpc_conntimeo, 1, session);
ret = wd_start(session->wdog, session->conntimeo, ftpc_timeout, 1, session);
if (ret != OK)
{
ndbg("wd_start() failed\n");

View File

@ -188,8 +188,8 @@ static int ftpc_recvbinary(FAR struct ftpc_session_s *session,
err = ENOMEM;
goto errout_with_err;
}
while (!feof(rinstream))
for (;;)
{
if (ftpc_waitinput(session) != 0)
{
@ -201,8 +201,13 @@ static int ftpc_recvbinary(FAR struct ftpc_session_s *session,
nread = fread(buf, sizeof(char), CONFIG_FTP_BUFSIZE, rinstream);
if (nread <= 0)
{
(void)ftpc_xfrabort(session, rinstream);
ret = ERROR;
/* nread == 0 means end of file */
if (nread < 0)
{
(void)ftpc_xfrabort(session, rinstream);
ret = ERROR;
}
break;
}

View File

@ -189,7 +189,7 @@ int fptc_getreply(struct ftpc_session_s *session)
if (session->replytimeo)
{
ret = wd_start(session->wdog, session->replytimeo, ftpc_replytimeo, 1, session);
ret = wd_start(session->wdog, session->replytimeo, ftpc_timeout, 1, session);
}
/* Get the next line from the server */

View File

@ -160,6 +160,7 @@ struct ftpc_session_s
FAR char *prevdir; /* Previous directory */
FAR char *rname; /* Remote file name */
FAR char *lname; /* Local file name */
pid_t pid; /* Task ID of FTP client */
uint8_t xfrmode; /* Previous data transfer type (See FTPC_XFRMODE_* defines) */
uint16_t port; /* Server/proxy port number (probably 21) */
uint16_t flags; /* Connection flags (see FTPC_FLAGS_* defines) */
@ -238,11 +239,7 @@ EXTERN void ftpc_sockcopy(FAR struct ftpc_socket_s *dest,
/* Socket I/O helpers */
EXTERN int ftpc_sockprintf(FAR struct ftpc_socket_s *sock, const char *str, ...);
/* Timeout handlers */
EXTERN void ftpc_replytimeo(int argc, uint32_t arg1, ...);
EXTERN void ftpc_conntimeo(int argc, uint32_t arg1, ...);
EXTERN void ftpc_timeout(int argc, uint32_t arg1, ...);
/* Transfer helpers */

View File

@ -198,7 +198,7 @@ static void ftpc_nlstparse(FAR FILE *instream, callback_t callback,
/* Read every filename from the temporary file */
while (!feof(instream))
for (;;)
{
/* Read the next line from the file */

View File

@ -111,32 +111,37 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session,
int ret = OK;
buf = (char *)malloc(CONFIG_FTP_BUFSIZE);
while (!feof(linstream))
for (;;)
{
nread = fread(buf, sizeof(char), CONFIG_FTP_BUFSIZE, linstream);
if (nread <= 0)
{
(void)ftpc_xfrabort(session, linstream);
ret = ERROR;
break;
}
if (nread <= 0)
{
/* nread == 0 is just EOF */
if (ftpc_waitoutput(session) != 0)
{
ret = ERROR;
break;
}
if (nread < 0)
{
(void)ftpc_xfrabort(session, linstream);
ret = ERROR;
}
break;
}
nwritten = fwrite(buf, sizeof(char), nread, routstream);
if (nwritten != nread)
{
(void)ftpc_xfrabort(session, routstream);
ret = ERROR;
break;
}
if (ftpc_waitoutput(session) != 0)
{
ret = ERROR;
break;
}
session->size += nread;
}
nwritten = fwrite(buf, sizeof(char), nread, routstream);
if (nwritten != nread)
{
(void)ftpc_xfrabort(session, routstream);
ret = ERROR;
break;
}
session->size += nread;
}
free(buf);
return ret;

View File

@ -44,11 +44,14 @@
#include <unistd.h>
#include <string.h>
#include <poll.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <apps/ftpc.h>
#include "ftpc_internal.h"
/****************************************************************************
@ -396,5 +399,23 @@ int ftpc_waitdata(FAR struct ftpc_session_s *session, FAR FILE *stream, bool rdw
return ret;
}
/****************************************************************************
* Name: ftpc_timeout
*
* Description:
* A timeout occurred -- either on a specific command or while waiting
* for a reply.
*
* NOTE:
* This function executes in the context of a timer interrupt handler.
*
****************************************************************************/
void ftpc_timeout(int argc, uint32_t arg1, ...)
{
FAR struct ftpc_session_s *session = (FAR struct ftpc_session_s *)arg1;
nlldbg("Timeout!\n");
DEBUGASSERT(argc == 1 && session);
kill(session->pid, CONFIG_FTP_SIGNAL);
}

View File

@ -33,8 +33,8 @@
*
****************************************************************************/
#ifndef __NSH_H
#define __HSH_H
#ifndef __APPS_NSHLIB_NSH_H
#define __APPS_NSHLIB_NSH_H
/****************************************************************************
* Included Files
@ -488,4 +488,4 @@ extern int cmd_lbracket(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv);
# endif
#endif /* CONFIG_DISABLE_SIGNALS */
#endif /* __NSH_H */
#endif /* __APPS_NSHLIB_NSH_H */