From ab784567291b1925d616ea9b9d2577f42d2699a5 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sun, 5 Jun 2011 14:08:26 +0000 Subject: More FTP bug fixes git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3669 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/ftpc/ftpc_cmds.c | 685 +++++++++++++++++------------- apps/examples/ftpc/ftpc_main.c | 837 +++++++++++++++++++------------------ apps/netutils/ftpc/ftpc_getfile.c | 93 +++-- apps/netutils/ftpc/ftpc_internal.h | 2 +- apps/netutils/ftpc/ftpc_listdir.c | 16 +- apps/netutils/ftpc/ftpc_putfile.c | 45 +- apps/netutils/ftpc/ftpc_socket.c | 114 +++-- apps/nshlib/nsh_netcmds.c | 14 +- 8 files changed, 991 insertions(+), 815 deletions(-) diff --git a/apps/examples/ftpc/ftpc_cmds.c b/apps/examples/ftpc/ftpc_cmds.c index 3acd56ee3..d01215753 100755 --- a/apps/examples/ftpc/ftpc_cmds.c +++ b/apps/examples/ftpc/ftpc_cmds.c @@ -1,301 +1,384 @@ -/**************************************************************************** - * examples/ftpc/ftpc_cmds.c - * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 -#include - -#include - -#include "ftpc.h" - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: cmd_rlogin - ****************************************************************************/ - -int cmd_rlogin(SESSION handle, int argc, char **argv) -{ - struct ftpc_login_s login = {NULL, NULL, NULL, true}; - - login.uname = argv[1]; - if (argc > 2) - { - login.pwd = argv[2]; - } - - return ftpc_login(handle, &login); -} - -/**************************************************************************** - * Name: cmd_rquit - ****************************************************************************/ - -int cmd_rquit(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_rchdir - ****************************************************************************/ - -int cmd_rchdir(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_rpwd(handle); - if (pwd) - { - printf("PWD: %s\n", pwd); - free(pwd); - return OK; - } - return ERROR; -} - -/**************************************************************************** - * Name: cmd_rcdup - ****************************************************************************/ - -int cmd_rcdup(SESSION handle, int argc, char **argv) -{ - return ftpc_cdup(handle); -} - -/**************************************************************************** - * Name: cmd_rmkdir - ****************************************************************************/ - -int cmd_rmkdir(SESSION handle, int argc, char **argv) -{ - return ftpc_mkdir(handle, argv[1]); -} - -/**************************************************************************** - * Name: cmd_rrmdir - ****************************************************************************/ - -int cmd_rrmdir(SESSION handle, int argc, char **argv) -{ - return ftpc_rmdir(handle, argv[1]); -} - -/**************************************************************************** - * Name: cmd_runlink - ****************************************************************************/ - -int cmd_runlink(SESSION handle, int argc, char **argv) -{ - return ftpc_unlink(handle, argv[1]); -} - -/**************************************************************************** - * Name: cmd_rchmod - ****************************************************************************/ - -int cmd_rchmod(SESSION handle, int argc, char **argv) -{ - return ftpc_chmod(handle, argv[1], argv[2]); -} - -/**************************************************************************** - * Name: cmd_rrename - ****************************************************************************/ - -int cmd_rrename(SESSION handle, int argc, char **argv) -{ - return ftpc_rename(handle, argv[1], argv[2]); -} - -/**************************************************************************** - * Name: cmd_rsize - ****************************************************************************/ - -int cmd_rsize(SESSION handle, int argc, char **argv) -{ - off_t size = ftpc_filesize(handle, argv[1]); - printf("SIZE: %lu\n", size); - return OK; -} - -/**************************************************************************** - * Name: cmd_rtime - ****************************************************************************/ - -int cmd_rtime(SESSION handle, int argc, char **argv) -{ - time_t filetime = ftpc_filetime(handle, argv[1]); - printf("TIME: %lu\n", (long)filetime); - return OK; -} - -/**************************************************************************** - * Name: cmd_ridle - ****************************************************************************/ - -int cmd_ridle(SESSION handle, int argc, char **argv) -{ - unsigned int idletime = 0; - - if (argc > 1) - { - idletime = atoi(argv[1]); - } - - return ftpc_idle(handle, idletime); -} - -/**************************************************************************** - * Name: cmd_rnoop - ****************************************************************************/ - -int cmd_rnoop(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]); - } - FFLUSH(); - - ftpc_dirfree(dirlist); - return OK; -} - -/**************************************************************************** - * Name: cmd_rget - ****************************************************************************/ - -int cmd_rget(SESSION handle, int argc, char **argv) -{ - FAR const char *lname = NULL; - - if (argc > 2) - { - lname = argv[2]; - } - return ftpc_getfile(handle, argv[1], lname, FTPC_GET_NORMAL, FTPC_XFRMODE_ASCII); -} - -/**************************************************************************** - * Name: cmd_rput - ****************************************************************************/ - -int cmd_rput(SESSION handle, int argc, char **argv) -{ - FAR const char *rname = NULL; - - if (argc > 2) - { - rname = argv[2]; - } - return ftp_putfile(handle, argv[1], rname, FTPC_PUT_NORMAL, FTPC_XFRMODE_ASCII); -} +/**************************************************************************** + * examples/ftpc/ftpc_cmds.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include +#include + +#include + +#include "ftpc.h" + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: cmd_rlogin + ****************************************************************************/ + +int cmd_rlogin(SESSION handle, int argc, char **argv) +{ + struct ftpc_login_s login = {NULL, NULL, NULL, true}; + + login.uname = argv[1]; + if (argc > 2) + { + login.pwd = argv[2]; + } + + return ftpc_login(handle, &login); +} + +/**************************************************************************** + * Name: cmd_rquit + ****************************************************************************/ + +int cmd_rquit(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_rchdir + ****************************************************************************/ + +int cmd_rchdir(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_rpwd(handle); + if (pwd) + { + printf("PWD: %s\n", pwd); + free(pwd); + return OK; + } + return ERROR; +} + +/**************************************************************************** + * Name: cmd_rcdup + ****************************************************************************/ + +int cmd_rcdup(SESSION handle, int argc, char **argv) +{ + return ftpc_cdup(handle); +} + +/**************************************************************************** + * Name: cmd_rmkdir + ****************************************************************************/ + +int cmd_rmkdir(SESSION handle, int argc, char **argv) +{ + return ftpc_mkdir(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_rrmdir + ****************************************************************************/ + +int cmd_rrmdir(SESSION handle, int argc, char **argv) +{ + return ftpc_rmdir(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_runlink + ****************************************************************************/ + +int cmd_runlink(SESSION handle, int argc, char **argv) +{ + return ftpc_unlink(handle, argv[1]); +} + +/**************************************************************************** + * Name: cmd_rchmod + ****************************************************************************/ + +int cmd_rchmod(SESSION handle, int argc, char **argv) +{ + return ftpc_chmod(handle, argv[1], argv[2]); +} + +/**************************************************************************** + * Name: cmd_rrename + ****************************************************************************/ + +int cmd_rrename(SESSION handle, int argc, char **argv) +{ + return ftpc_rename(handle, argv[1], argv[2]); +} + +/**************************************************************************** + * Name: cmd_rsize + ****************************************************************************/ + +int cmd_rsize(SESSION handle, int argc, char **argv) +{ + off_t size = ftpc_filesize(handle, argv[1]); + printf("SIZE: %lu\n", size); + return OK; +} + +/**************************************************************************** + * Name: cmd_rtime + ****************************************************************************/ + +int cmd_rtime(SESSION handle, int argc, char **argv) +{ + time_t filetime = ftpc_filetime(handle, argv[1]); + printf("TIME: %lu\n", (long)filetime); + return OK; +} + +/**************************************************************************** + * Name: cmd_ridle + ****************************************************************************/ + +int cmd_ridle(SESSION handle, int argc, char **argv) +{ + unsigned int idletime = 0; + + if (argc > 1) + { + idletime = atoi(argv[1]); + } + + return ftpc_idle(handle, idletime); +} + +/**************************************************************************** + * Name: cmd_rnoop + ****************************************************************************/ + +int cmd_rnoop(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]); + } + FFLUSH(); + + ftpc_dirfree(dirlist); + return OK; +} + +/**************************************************************************** + * Name: cmd_rget + ****************************************************************************/ + +int cmd_rget(SESSION handle, int argc, char **argv) +{ + FAR const char *rname; + FAR const char *lname = NULL; + int xfrmode = FTPC_XFRMODE_ASCII; + int option; + + while ((option = getopt(argc, argv, "ab")) != ERROR) + { + if (option == 'a') + { + xfrmode = FTPC_XFRMODE_ASCII; + } + else if (option == 'b') + { + xfrmode = FTPC_XFRMODE_BINARY; + } + else + { + printf("%s: Unrecognized option: '%c'\n", "put", option); + return ERROR; + } + } + + /* There should be one or two parameters remaining on the command line */ + + if (optind >= argc) + { + printf("%s: Missing required arguments\n", "get"); + return ERROR; + } + + rname = argv[optind]; + optind++; + + if (optind < argc) + { + lname = argv[optind]; + optind++; + } + + if (optind != argc) + { + printf("%s: Too many arguments\n", "get"); + return ERROR; + } + + /* Perform the transfer */ + + return ftpc_getfile(handle, rname, lname, FTPC_GET_NORMAL, xfrmode); +} + +/**************************************************************************** + * Name: cmd_rput + ****************************************************************************/ + +int cmd_rput(SESSION handle, int argc, char **argv) +{ + FAR const char *lname; + FAR const char *rname = NULL; + int xfrmode = FTPC_XFRMODE_ASCII; + int option; + + while ((option = getopt(argc, argv, "ab")) != ERROR) + { + if (option == 'a') + { + xfrmode = FTPC_XFRMODE_ASCII; + } + else if (option == 'b') + { + xfrmode = FTPC_XFRMODE_BINARY; + } + else + { + printf("%s: Unrecognized option: '%c'\n", "put", option); + return ERROR; + } + } + + /* There should be one or two parameters remaining on the command line */ + + if (optind >= argc) + { + printf("%s: Missing required arguments\n", "get"); + return ERROR; + } + + lname = argv[optind]; + optind++; + + if (optind < argc) + { + rname = argv[optind]; + optind++; + } + + if (optind >= argc) + { + printf("%s: Too many arguments\n "); + return ERROR; + } + + /* Perform the transfer */ + + return ftp_putfile(handle, lname, rname, FTPC_PUT_NORMAL, xfrmode); +} diff --git a/apps/examples/ftpc/ftpc_main.c b/apps/examples/ftpc/ftpc_main.c index 991b3e9da..7a1cb50f7 100755 --- a/apps/examples/ftpc/ftpc_main.c +++ b/apps/examples/ftpc/ftpc_main.c @@ -1,411 +1,426 @@ -/**************************************************************************** - * examples/ftpc/ftpc_main.c - * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * 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 -#include -#include - -#include -#include - -#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_lhelp(SESSION handle, int argc, char **argv); -static int cmd_lunrecognized(SESSION handle, int argc, char **argv); - -static const struct cmdmap_s g_cmdmap[] = -{ - { "cd", cmd_rchdir, 2, 2, "" }, - { "chmod", cmd_rchmod, 3, 3, " " }, - { "get", cmd_rget, 2, 3, " []" }, - { "help", cmd_lhelp, 1, 2, "" }, - { "idle", cmd_ridle, 1, 2, "[]" }, - { "login", cmd_rlogin, 2, 3, " []" }, - { "ls", cmd_rls, 1, 2, "[]" }, - { "quit", cmd_rquit, 1, 1, "" }, - { "mkdir", cmd_rmkdir, 2, 2, "" }, - { "noop", cmd_rnoop, 1, 1, "" }, - { "put", cmd_rput, 2, 3, " []" }, - { "pwd", cmd_rpwd, 1, 1, "" }, - { "rename", cmd_rrename, 3, 3, " " }, - { "rhelp", cmd_rhelp, 1, 2, "[]" }, - { "rm", cmd_runlink, 2, 2, "" }, - { "rmdir", cmd_rrmdir, 2, 2, "" }, - { "size", cmd_rsize, 2, 2, "" }, - { "time", cmd_rtime, 2, 2, "" }, - { "up", cmd_rcdup, 1, 1, "" }, - { NULL, NULL, 1, 1, NULL } -}; - -static char g_line[CONFIG_FTPC_LINELEN]; - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: cmd_lhelp - ****************************************************************************/ - -static int cmd_lhelp(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_lunrecognized - ****************************************************************************/ - -static int cmd_lunrecognized(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_lunrecognized; - 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("NuttX FTP Client:\n"); - FFLUSH(); - - /* Then enter the command line parsing loop */ - - for (;;) - { - /* Display the prompt string */ - - fputs("nfc> ", 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; -} +/**************************************************************************** + * examples/ftpc/ftpc_main.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include +#include +#include + +#include +#include + +#include "ftpc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define FTPC_MAX_ARGUMENTS 4 + +/**************************************************************************** + * 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_lhelp(SESSION handle, int argc, char **argv); +static int cmd_lunrecognized(SESSION handle, int argc, char **argv); + +static const struct cmdmap_s g_cmdmap[] = +{ + { "cd", cmd_rchdir, 2, 2, "" }, + { "chmod", cmd_rchmod, 3, 3, " " }, + { "get", cmd_rget, 2, 4, "[-a|b] []" }, + { "help", cmd_lhelp, 1, 2, "" }, + { "idle", cmd_ridle, 1, 2, "[]" }, + { "login", cmd_rlogin, 2, 3, " []" }, + { "ls", cmd_rls, 1, 2, "[]" }, + { "quit", cmd_rquit, 1, 1, "" }, + { "mkdir", cmd_rmkdir, 2, 2, "" }, + { "noop", cmd_rnoop, 1, 1, "" }, + { "put", cmd_rput, 2, 4, "[-a|b] []" }, + { "pwd", cmd_rpwd, 1, 1, "" }, + { "rename", cmd_rrename, 3, 3, " " }, + { "rhelp", cmd_rhelp, 1, 2, "[]" }, + { "rm", cmd_runlink, 2, 2, "" }, + { "rmdir", cmd_rrmdir, 2, 2, "" }, + { "size", cmd_rsize, 2, 2, "" }, + { "time", cmd_rtime, 2, 2, "" }, + { "up", cmd_rcdup, 1, 1, "" }, + { NULL, NULL, 1, 1, NULL } +}; + +static char g_line[CONFIG_FTPC_LINELEN]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: cmd_lhelp + ****************************************************************************/ + +static int cmd_lhelp(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_lunrecognized + ****************************************************************************/ + +static int cmd_lunrecognized(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_lunrecognized; + 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); + } + + /* Check if the argument includes a port number */ + + ptr = strchr(argv[1], ':'); + if (ptr) + { + *ptr = '\0'; + connect.port = atoi(ptr+1); + } + + /* In any event, we can now extract the IP address from the comman-line */ + + connect.addr.s_addr = inet_addr(argv[1]); + + /* Connect to the FTP server */ + + handle = ftpc_connect(&connect); + if (!handle) + { + printf("Failed to connect to the server: %d\n", errno); + exit(1); + } + + /* Present a greeting */ + + printf("NuttX FTP Client:\n"); + FFLUSH(); + + /* Setting optind to -1 is a non-standard, backdoor way to reinitialize + * getopt(). getopt() is not thread safe and we have no idea what state + * it is in now! + */ + + optind = -1; + + /* Then enter the command line parsing loop */ + + for (;;) + { + /* Display the prompt string */ + + fputs("nfc> ", 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; +} diff --git a/apps/netutils/ftpc/ftpc_getfile.c b/apps/netutils/ftpc/ftpc_getfile.c index 846a1e1db..8a60c514b 100644 --- a/apps/netutils/ftpc/ftpc_getfile.c +++ b/apps/netutils/ftpc/ftpc_getfile.c @@ -149,14 +149,18 @@ static int ftpc_recvinit(struct ftpc_session_s *session, FAR const char *path, return ERROR; } - /* Accept a connection on the data socket (unless passive mode then the - * function does nothing). + /* In active mode, we need to accept a connection on the data socket + * (in passive mode, we have already connected the data channel to + * the FTP server). */ - ret = ftpc_sockaccept(&session->data, FTPC_IS_PASSIVE(session)); - if (ret != OK) + if (!FTPC_IS_PASSIVE(session)) { - ndbg("Data connection not accepted\n"); + ret = ftpc_sockaccept(&session->data); + if (ret != OK) + { + ndbg("Data connection not accepted\n"); + } } return ret; @@ -204,6 +208,8 @@ static int ftpc_recvbinary(FAR struct ftpc_session_s *session, int err; int ret = OK; + /* Allocate an I/O buffer */ + buf = (FAR char *)malloc(CONFIG_FTP_BUFSIZE); if (!buf) { @@ -213,13 +219,18 @@ static int ftpc_recvbinary(FAR struct ftpc_session_s *session, for (;;) { - if (ftpc_waitinput(session) != 0) + /* Wait for input on the socket */ + + ret = ftpc_waitinput(session); + if (ret != OK) { nvdbg("ftpc_waitinput() failed\n"); err = EIO; goto errout_with_buf; } + /* Read the data from the socket */ + nread = fread(buf, sizeof(char), CONFIG_FTP_BUFSIZE, rinstream); if (nread <= 0) { @@ -227,12 +238,16 @@ static int ftpc_recvbinary(FAR struct ftpc_session_s *session, if (nread < 0) { + /* errno should already be set by fread */ + (void)ftpc_xfrabort(session, rinstream); ret = ERROR; } break; } + /* Write the data to the file */ + nwritten = fwrite(buf, sizeof(char), nread, loutstream); if (nwritten != nread) { @@ -372,11 +387,11 @@ int ftpc_getfile(SESSION handle, FAR const char *rname, FAR const char *lname, if (xfrmode == FTPC_XFRMODE_ASCII) { - ret = ftpc_recvbinary(session, session->data.instream, loutstream); + ret = ftpc_recvtext(session, session->data.instream, loutstream); } else { - ret = ftpc_recvtext(session, session->data.instream, loutstream); + ret = ftpc_recvbinary(session, session->data.instream, loutstream); } ftpc_sockclose(&session->data); @@ -419,40 +434,68 @@ errout: int ftpc_recvtext(FAR struct ftpc_session_s *session, FAR FILE *rinstream, FAR FILE *loutstream) { - char *buf = (char *)malloc(CONFIG_FTP_BUFSIZE); - int c; + FAR char *buf; + int ch; int ret = OK; - while((c = fgetc(rinstream)) != EOF) { + /* Allocate an I/O buffer */ - if (ftpc_waitinput(session) != 0) - { - break; - } + buf = (FAR char *)malloc(CONFIG_FTP_BUFSIZE); + if (!buf) + { + set_errno(ENOMEM); + return ERROR; + } + + /* Read the next character from the incoming data stream */ - if (c == '\r') + while ((ch = fgetc(rinstream)) != EOF) { - c = fgetc(rinstream); - if (c == EOF) + /* Wait for input on the socket */ + + ret = ftpc_waitinput(session); + if (ret != OK) { - (void)ftpc_xfrabort(session, rinstream); - ret = ERROR; + nvdbg("ftpc_waitinput() failed\n"); break; } - if (c != '\n') + + /* Is it a carriage return? Compress \r\n to \n */ + + if (ch == '\r') { - ungetc(c, rinstream); - c = '\r'; + /* Get the next character */ + + ch = fgetc(rinstream); + if (ch == EOF) + { + /* Ooops... */ + + (void)ftpc_xfrabort(session, rinstream); + ret = ERROR; + break; + } + + /* If its not a newline, then keep the carriage return */ + + if (ch != '\n') + { + ungetc(ch, rinstream); + ch = '\r'; + } } - } - if (fputc(c, loutstream) == EOF) + /* Then write the character to the output file */ + + if (fputc(ch, loutstream) == EOF) { (void)ftpc_xfrabort(session, loutstream); ret = ERROR; break; } + /* Increase the actual size of the file by one */ + session->size++; } diff --git a/apps/netutils/ftpc/ftpc_internal.h b/apps/netutils/ftpc/ftpc_internal.h index 6600b1220..20fbbccc4 100644 --- a/apps/netutils/ftpc/ftpc_internal.h +++ b/apps/netutils/ftpc/ftpc_internal.h @@ -257,7 +257,7 @@ EXTERN int ftpc_sockconnect(FAR struct ftpc_socket_s *sock, FAR struct sockaddr_in *addr); EXTERN int ftpc_sockgetsockname(FAR struct ftpc_socket_s *sock, FAR struct sockaddr_in *sa); -EXTERN int ftpc_sockaccept(FAR struct ftpc_socket_s *sock, bool passive); +EXTERN int ftpc_sockaccept(FAR struct ftpc_socket_s *sock); EXTERN int ftpc_socklisten(FAR struct ftpc_socket_s *sock); EXTERN void ftpc_sockcopy(FAR struct ftpc_socket_s *dest, FAR const struct ftpc_socket_s *src); diff --git a/apps/netutils/ftpc/ftpc_listdir.c b/apps/netutils/ftpc/ftpc_listdir.c index d5de10ec3..e3af9b122 100644 --- a/apps/netutils/ftpc/ftpc_listdir.c +++ b/apps/netutils/ftpc/ftpc_listdir.c @@ -199,13 +199,19 @@ static int ftpc_recvdir(FAR struct ftpc_session_s *session, return ERROR; } - /* Accept the connection from the server */ + /* In active mode, we need to accept a connection on the data socket + * (in passive mode, we have already connected the data channel to + * the FTP server). + */ - ret = ftpc_sockaccept(&session->data, FTPC_IS_PASSIVE(session)); - if (ret != OK) + if (!FTPC_IS_PASSIVE(session)) { - ndbg("ftpc_sockaccept() failed: %d\n", errno); - return ERROR; + ret = ftpc_sockaccept(&session->data); + if (ret != OK) + { + ndbg("ftpc_sockaccept() failed: %d\n", errno); + return ERROR; + } } /* Receive the NLST directory list */ diff --git a/apps/netutils/ftpc/ftpc_putfile.c b/apps/netutils/ftpc/ftpc_putfile.c index 94942edba..e008da3b1 100644 --- a/apps/netutils/ftpc/ftpc_putfile.c +++ b/apps/netutils/ftpc/ftpc_putfile.c @@ -114,6 +114,8 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session, buf = (char *)malloc(CONFIG_FTP_BUFSIZE); for (;;) { + /* Read data from the file */ + nread = fread(buf, sizeof(char), CONFIG_FTP_BUFSIZE, linstream); if (nread <= 0) { @@ -127,12 +129,17 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session, break; } - if (ftpc_waitoutput(session) != 0) + /* Wait to make sure that we send the data without blocking */ + + ret = ftpc_waitoutput(session); + if (ret != OK) { ret = ERROR; break; } + /* Send the data */ + nwritten = fwrite(buf, sizeof(char), nread, routstream); if (nwritten != nread) { @@ -141,6 +148,8 @@ static int ftpc_sendbinary(FAR struct ftpc_session_s *session, break; } + /* Increment the size of the file sent */ + session->size += nread; } @@ -163,13 +172,19 @@ static int ftpc_sendtext(FAR struct ftpc_session_s *session, int c; int ret = OK; + /* Write characters one at a time. */ + while((c = fgetc(linstream)) != EOF) { + /* Make sure that we can send the character without blocking */ + if (ftpc_waitoutput(session) != 0) { break; } + /* If it is a newline, send a carriage return too */ + if (c == '\n') { if (fputc('\r', routstream) == EOF) @@ -179,9 +194,13 @@ static int ftpc_sendtext(FAR struct ftpc_session_s *session, break; } + /* Increment the size of the file sent */ + session->size++; } + /* Send the character */ + if (fputc(c, routstream) == EOF) { (void)ftpc_xfrabort(session, routstream); @@ -189,6 +208,8 @@ static int ftpc_sendtext(FAR struct ftpc_session_s *session, break; } + /* Increment the size of the file sent */ + session->size++; } @@ -340,20 +361,30 @@ static int ftpc_sendfile(struct ftpc_session_s *session, const char *path, * with a mark. */ - ret = ftpc_sockaccept(&session->data, FTPC_IS_PASSIVE(session)); - if (ret != OK) + /* In active mode, we need to accept a connection on the data socket + * (in passive mode, we have already connected the data channel to + * the FTP server). + */ + + if (!FTPC_IS_PASSIVE(session)) { - ndbg("Data connection not accepted\n"); - return ERROR; + ret = ftpc_sockaccept(&session->data); + if (ret != OK) + { + ndbg("Data connection not accepted\n"); + return ERROR; + } } + /* Then perform the data transfer */ + if (xfrmode == FTPC_XFRMODE_ASCII) { - ret = ftpc_sendbinary(session, stream, session->data.instream); + ret = ftpc_sendtext(session, stream, session->data.instream); } else { - ret = ftpc_sendtext(session, stream, session->data.instream); + ret = ftpc_sendbinary(session, stream, session->data.instream); } ftpc_sockflush(&session->data); diff --git a/apps/netutils/ftpc/ftpc_socket.c b/apps/netutils/ftpc/ftpc_socket.c index bfa8ec7c1..d245c812d 100644 --- a/apps/netutils/ftpc/ftpc_socket.c +++ b/apps/netutils/ftpc/ftpc_socket.c @@ -209,81 +209,71 @@ void ftpc_sockcopy(FAR struct ftpc_socket_s *dest, * Name: ftpc_sockaccept * * Description: - * Accept a connection on the data socket (unless passive mode then this - * function does nothing). + * Accept a connection on the data socket. This function is onlly used + * in active mode. + * + * In active mode FTP the client connects from a random port (N>1023) to the + * FTP server's command port, port 21. Then, the client starts listening to + * port N+1 and sends the FTP command PORT N+1 to the FTP server. The server + * will then connect back to the client's specified data port from its local + * data port, which is port 20. In passive mode FTP the client initiates + * both connections to the server, solving the problem of firewalls filtering + * the incoming data port connection to the client from the server. When + * opening an FTP connection, the client opens two random ports locally + * (N>1023 and N+1). The first port contacts the server on port 21, but + * instead of then issuing a PORT command and allowing the server to connect + * back to its data port, the client will issue the PASV command. The result + * of this is that the server then opens a random unprivileged port (P > + * 1023) and sends the PORT P command back to the client. The client then + * initiates the connection from port N+1 to port P on the server to transfer + * data. * ****************************************************************************/ -int ftpc_sockaccept(struct ftpc_socket_s *sock, bool passive) +int ftpc_sockaccept(FAR struct ftpc_socket_s *sock) { struct sockaddr addr; socklen_t addrlen; - /* In active mode FTP the client connects from a random port (N>1023) to the - * FTP server's command port, port 21. Then, the client starts listening to - * port N+1 and sends the FTP command PORT N+1 to the FTP server. The server - * will then connect back to the client's specified data port from its local - * data port, which is port 20. In passive mode FTP the client initiates - * both connections to the server, solving the problem of firewalls filtering - * the incoming data port connection to the client from the server. When - * opening an FTP connection, the client opens two random ports locally - * (N>1023 and N+1). The first port contacts the server on port 21, but - * instead of then issuing a PORT command and allowing the server to connect - * back to its data port, the client will issue the PASV command. The result - * of this is that the server then opens a random unprivileged port (P > - * 1023) and sends the PORT P command back to the client. The client then - * initiates the connection from port N+1 to port P on the server to transfer - * data. + /* Any previous socket should have been uninitialized (0) or explicitly + * closed (-1). But the path to this function may include a call to + * ftpc_sockinit(). If so... close that socket and call accept to + * get a new one. */ - if (!passive) + if (sock->sd > 0) { - /* Any previous socket should have been uninitialized (0) or explicitly - * closed (-1). But the path to this function may include a call to - * ftpc_sockinit(). If so... close that socket and call accept to - * get a new one. - */ - - if (sock->sd > 0) - { - ftpc_sockclose(sock); - } - - addrlen = sizeof(addr); - sock->sd = accept(sock->sd, &addr, &addrlen); - if (sock->sd == -1) - { - ndbg("accept() failed: %d\n", errno); - return ERROR; - } - - memcpy(&sock->laddr, &addr, sizeof(sock->laddr)); - - /* Create in/out C buffer I/O streams on the data channel. First, - * create the incoming buffered stream. - */ - - sock->instream = fdopen(sock->sd, "r"); - if (!sock->instream) - { - ndbg("fdopen() failed: %d\n", errno); - goto errout_with_sd; - } - - /* Create the outgoing stream */ - - sock->outstream = fdopen(sock->sd, "w"); - if (!sock->outstream) - { - ndbg("fdopen() failed: %d\n", errno); - goto errout_with_instream; - } + ftpc_sockclose(sock); + } + + addrlen = sizeof(addr); + sock->sd = accept(sock->sd, &addr, &addrlen); + if (sock->sd == -1) + { + ndbg("accept() failed: %d\n", errno); + return ERROR; } - else + + memcpy(&sock->laddr, &addr, sizeof(sock->laddr)); + + /* Create in/out C buffer I/O streams on the data channel. First, + * create the incoming buffered stream. + */ + + sock->instream = fdopen(sock->sd, "r"); + if (!sock->instream) { - /* Should already be set up from ftpc_sockinit() call */ + ndbg("fdopen() failed: %d\n", errno); + goto errout_with_sd; + } + + /* Create the outgoing stream */ - DEBUGASSERT(sock->sd >= 0 && sock->instream && sock->outstream); + sock->outstream = fdopen(sock->sd, "w"); + if (!sock->outstream) + { + ndbg("fdopen() failed: %d\n", errno); + goto errout_with_instream; } return OK; diff --git a/apps/nshlib/nsh_netcmds.c b/apps/nshlib/nsh_netcmds.c index 6e210c020..8285da295 100644 --- a/apps/nshlib/nsh_netcmds.c +++ b/apps/nshlib/nsh_netcmds.c @@ -331,20 +331,28 @@ int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv, return ERROR; } - /* There should be exactly on parameter left on the command-line */ + /* There should be exactly one parameter left on the command-line */ if (optind == argc-1) { args->srcpath = argv[optind]; } + + /* optind == argc means that there is nothing left on the command-line */ + else if (optind >= argc) { - fmt = g_fmttoomanyargs; + fmt = g_fmtargrequired; goto errout; } + + /* optind < argc-1 means that there are too many arguments on the + * command-line + */ + else { - fmt = g_fmtargrequired; + fmt = g_fmttoomanyargs; goto errout; } -- cgit v1.2.3