diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/examples/README.txt | 41 | ||||
-rw-r--r-- | apps/examples/ftpd/Makefile | 9 | ||||
-rw-r--r-- | apps/examples/ftpd/ftpd.h | 67 | ||||
-rwxr-xr-x | apps/examples/ftpd/ftpd_main.c | 151 | ||||
-rwxr-xr-x | apps/include/netutils/ftpd.h | 32 | ||||
-rwxr-xr-x | apps/netutils/ftpd/ftpd.c | 479 | ||||
-rwxr-xr-x | apps/netutils/ftpd/ftpd.h | 111 | ||||
-rw-r--r-- | apps/nshlib/Makefile | 4 | ||||
-rw-r--r-- | apps/nshlib/nsh_init.c | 102 | ||||
-rw-r--r-- | apps/nshlib/nsh_parse.c | 31 |
10 files changed, 563 insertions, 464 deletions
diff --git a/apps/examples/README.txt b/apps/examples/README.txt index bb401c99a..d598180e5 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -282,15 +282,42 @@ examples/ftpd configuration if the network is configuration prior to running the example. - If CONFIG_EXAMPELS_FTPD_NONETINIT is not defined, then the following may + NSH always initializes the network so if CONFIG_NSH_BUILTIN_APPS is + defined, so is CONFIG_EXAMPLES_FTPD_NONETINIT (se it does not explicitly + need to be defined in that case): + + CONFIG_NSH_BUILTIN_APPS - Build the FTPD daemon example test as an + NSH built-in function. By default the FTPD daemon will be built + as a standalone application. + + If CONFIG_EXAMPLES_FTPD_NONETINIT is not defined, then the following may be specified to customized the network configuration: - CONFIG_EXAMPLE_FTPD_NOMAC - If the hardware has no MAC address of its - own, define this =y to provide a bogus address for testing. - CONFIG_EXAMPLE_FTPD_IPADDR - The target IP address. Default 10.0.0.2 - CONFIG_EXAMPLE_FTPD_DRIPADDR - The default router address. Default - 10.0.0.1 - CONFIG_EXAMPLE_FTPD_NETMASK - The network mask. Default: 255.255.255.0 + CONFIG_EXAMPLE_FTPD_NOMAC - If the hardware has no MAC address of its + own, define this =y to provide a bogus address for testing. + CONFIG_EXAMPLE_FTPD_IPADDR - The target IP address. Default 10.0.0.2 + CONFIG_EXAMPLE_FTPD_DRIPADDR - The default router address. Default + 10.0.0.1 + CONFIG_EXAMPLE_FTPD_NETMASK - The network mask. Default: 255.255.255.0 + + Other required configuration settings: Of course TCP networking support + is required. But here are a couple that are less obvious: + + CONFIG_DISABLE_PTHREAD - pthread support is required + CONFIG_DISABLE_POLL - poll() support is required + + Other FTPD configuration options thay may be of interest: + + CONFIG_FTPD_VENDORID - The vendor name to use in FTP communications. + Default: "NuttX" + CONFIG_FTPD_SERVERID - The server name to use in FTP communications. + Default: "NuttX FTP Server" + CONFIG_FTPD_CMDBUFFERSIZE - The maximum size of one command. Default: + 512 bytes. + CONFIG_FTPD_DATABUFFERSIZE - The size of the I/O buffer for data + transfers. Default: 2048 bytes. + CONFIG_FTPD_WORKERSTACKSIZE - The stacksize to allocate for each + FTP daemon worker thread. Default: 2048 bytes. The appconfig file (apps/.config) should include: diff --git a/apps/examples/ftpd/Makefile b/apps/examples/ftpd/Makefile index d3ccda7cc..4eb25c9e9 100644 --- a/apps/examples/ftpd/Makefile +++ b/apps/examples/ftpd/Makefile @@ -56,12 +56,6 @@ endif ROOTDEPPATH = --dep-path . -# Buttons built-in application info - -APPNAME = ftpd -PRIORITY = SCHED_PRIORITY_DEFAULT -STACKSIZE = 2048 - # Common build VPATH = @@ -84,7 +78,8 @@ $(COBJS): %$(OBJEXT): %.c .context: ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) - $(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main) + $(call REGISTER,ftpd_start,SCHED_PRIORITY_DEFAULT,2048,ftpd_start) + $(call REGISTER,ftpd_stop,SCHED_PRIORITY_DEFAULT,2048,ftpd_stop) @touch $@ endif diff --git a/apps/examples/ftpd/ftpd.h b/apps/examples/ftpd/ftpd.h index 619a9fa51..98ee3b3b6 100644 --- a/apps/examples/ftpd/ftpd.h +++ b/apps/examples/ftpd/ftpd.h @@ -38,6 +38,16 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> +#include <stdbool.h> +#include <stdlib.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ /* Configuration ************************************************************/ /* CONFIG_EXAMPLES_FTPD_PRIO - Priority of the FTP daemon. * Default: SCHED_PRIORITY_DEFAULT @@ -48,7 +58,7 @@ * configuration if the network is configuration prior to running the * example. * - * If CONFIG_EXAMPELS_FTPD_NONETINIT is not defined, then the following may + * If CONFIG_EXAMPLES_FTPD_NONETINIT is not defined, then the following may * be specified to customized the network configuration: * * CONFIG_EXAMPLE_FTPD_NOMAC - If the hardware has no MAC address of its @@ -75,21 +85,33 @@ # define CONFIG_EXAMPLES_FTPD_CLIENTSTACKSIZE 2048 #endif -#ifndef CONFIG_EXAMPLE_FTPD_IPADDR -# define CONFIG_EXAMPLE_FTPD_IPADDR 0x0a000002 -#endif -#ifndef CONFIG_EXAMPLE_FTPD_DRIPADDR -# define CONFIG_EXAMPLE_FTPD_DRIPADDR 0x0a000002 +/* NSH always initializes the network */ + +#if defined(CONFIG_NSH_BUILTIN_APPS) && !defined(CONFIG_EXAMPLES_FTPD_NONETINIT) +# define CONFIG_EXAMPLES_FTPD_NONETINIT 1 #endif -#ifndef CONFIG_EXAMPLE_FTPD_NETMASK -# define CONFIG_EXAMPLE_FTPD_NETMASK 0xffffff00 + +#ifdef CONFIG_EXAMPLES_FTPD_NONETINIT +# undef CONFIG_EXAMPLE_FTPD_IPADDR +# undef CONFIG_EXAMPLE_FTPD_DRIPADDR +# undef CONFIG_EXAMPLE_FTPD_NETMASK +#else +# ifndef CONFIG_EXAMPLE_FTPD_IPADDR +# define CONFIG_EXAMPLE_FTPD_IPADDR 0x0a000002 +# endif +# ifndef CONFIG_EXAMPLE_FTPD_DRIPADDR +# define CONFIG_EXAMPLE_FTPD_DRIPADDR 0x0a000001 +# endif +# ifndef CONFIG_EXAMPLE_FTPD_NETMASK +# define CONFIG_EXAMPLE_FTPD_NETMASK 0xffffff00 +# endif #endif /* Is this being built as an NSH built-in application? */ #ifdef CONFIG_NSH_BUILTIN_APPS -# define MAIN_NAME ftpd_main -# define MAIN_STRING "ftpd_main: " +# define MAIN_NAME ftpd_start +# define MAIN_STRING "ftpd_start: " #else # define MAIN_NAME user_start # define MAIN_STRING "user_start: " @@ -99,6 +121,8 @@ * Public Types ****************************************************************************/ +/* This structure describes one entry in a table of accounts */ + struct fptd_account_s { uint8_t flags; @@ -107,17 +131,34 @@ struct fptd_account_s FAR const char *home; }; +/* To minimize the probability of name collisitions, all FTPD example + * global data is maintained in single structure. + */ + struct ftpd_globals_s { - bool initialized; - volatile bool stop; + bool initialized; /* True: Networking is initialized. The + * network must be initialized only once. + */ +#ifdef CONFIG_NSH_BUILTIN_APPS + volatile bool stop; /* True: Request daemon to exit */ + volatile bool running; /* True: The daemon is running */ +#endif + pid_t pid; /* Task ID of the FTPD daemon. The value + * -1 is a redundant indication that the + * daemon is not running. + */ }; /**************************************************************************** * Public Function Prototypes ****************************************************************************/ -extern struct ftpd_globls_s g_ftpdglobls; +/* To minimize the probability of name collisitions, all FTPD example + * global data is maintained in a single instance of a structure. + */ + +extern struct ftpd_globals_s g_ftpdglob; /**************************************************************************** * Public Function Prototypes diff --git a/apps/examples/ftpd/ftpd_main.c b/apps/examples/ftpd/ftpd_main.c index ee3481d80..1f43e317c 100755 --- a/apps/examples/ftpd/ftpd_main.c +++ b/apps/examples/ftpd/ftpd_main.c @@ -34,6 +34,20 @@ * Included Files ****************************************************************************/ +#include <sys/types.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sched.h> +#include <errno.h> +#include <debug.h> + +#include <netinet/in.h> + +#include <apps/netutils/uiplib.h> +#include <apps/netutils/ftpd.h> + #include "ftpd.h" /**************************************************************************** @@ -49,13 +63,23 @@ static const struct fptd_account_s g_ftpdaccounts[] = #define NACCOUNTS (sizeof(g_ftpdaccounts) / sizeof(struct fptd_account_s)) /**************************************************************************** + * Public Data + ****************************************************************************/ + +/* To minimize the probability of name collisitions, all FTPD example + * global data is maintained in a single instance of a structure. + */ + +struct ftpd_globals_s g_ftpdglob; + +/**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** - * Name: shell_netinit + * Name: fptd_netinit ****************************************************************************/ -static void shell_netinit(void) +static void fptd_netinit(void) { #ifndef CONFIG_EXAMPLES_FTPD_NONETINIT struct in_addr addr; @@ -98,14 +122,22 @@ static void shell_netinit(void) static void ftpd_accounts(FTPD_SESSION handle) { - FAR onst struct fptd_account_s *account; + FAR const struct fptd_account_s *account; int i; + printf("Adding accounts:\n"); for (i = 0; i < NACCOUNTS; i++) { account = &g_ftpdaccounts[i]; - ftpd_add_user(handle, account->flags, account->user, - account->password, account->home); + + printf("%d. %s account: USER=%s PASSWORD=%s HOME=%s\n", + (account->flags & FTPD_ACCOUNTFLAG_SYSTEM) != 0 ? "Root" : "User", + (account->user) ? "(none)" : account->user, + (account->password) ? "(none)" : account->password, + (account->home) ? "(none)" : account->home); + + ftpd_adduser(handle, account->flags, account->user, + account->password, account->home); } } @@ -118,12 +150,24 @@ int ftpd_daemon(int s_argc, char **s_argv) FTPD_SESSION handle; int ret; + /* The FTPD daemon has been started */ + +#ifdef CONFIG_NSH_BUILTIN_APPS + g_ftpdglob.running = true; +#endif + printf("FTP daemon [%d] started\n", g_ftpdglob.pid); + /* Open FTPD */ handle = ftpd_open(); if (!handle) { - ndbg("Failed to open FTPD\n"); + printf("FTP daemon [%d] failed to open FTPD\n", g_ftpdglob.pid); +#ifdef CONFIG_NSH_BUILTIN_APPS + g_ftpdglob.running = false; + g_ftpdglob.stop = false; +#endif + g_ftpdglob.pid = -1; return EXIT_FAILURE; } @@ -131,16 +175,33 @@ int ftpd_daemon(int s_argc, char **s_argv) (void)ftpd_accounts(handle); - /* Then drive the FTPD server */ + /* Then drive the FTPD server. */ - while (g_ftpdglobls.stop == 0) +#ifdef CONFIG_NSH_BUILTIN_APPS + while (g_ftpdglob.stop == 0) +#else + for (;;) +#endif { - (void)ftpd_run(handle, 1000); + /* If ftpd_session returns success, it means that a new FTP session + * has been started. + */ + + ret = ftpd_session(handle, 1000); + printf("FTP daemon [%d] ftpd_session returned %d\n", g_ftpdglob.pid, ret); } - /* Close the FTPD server and exit */ + /* Close the FTPD server and exit (we can get here only if + * CONFIG_NSH_BUILTIN_APPS is defined). + */ +#ifdef CONFIG_NSH_BUILTIN_APPS + printf("FTP daemon [%d] stopping\n", g_ftpdglob.pid); + g_ftpdglob.running = false; + g_ftpdglob.stop = false; + g_ftpdglob.pid = -1; ftpd_close(handle); +#endif return EXIT_SUCCESS; } @@ -148,44 +209,76 @@ int ftpd_daemon(int s_argc, char **s_argv) * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: user_start/ftpd_main + * Name: user_start/ftpd_start ****************************************************************************/ int MAIN_NAME(int s_argc, char **s_argv) { - FTPD_SESSION handle; - pid_t pid; - int ret; - /* Check if we have already initialized the network */ - if (!g_ftpdglobls.initialized) + if (!g_ftpdglob.initialized) { /* Bring up the network */ - ret = ftpd_netinit(); - if (ret < 0) + printf("Initializing the network\n"); + fptd_netinit(); + + /* Initialize daemon state */ + + g_ftpdglob.initialized = true; + g_ftpdglob.pid = -1; +#ifdef CONFIG_NSH_BUILTIN_APPS + g_ftpdglob.stop = false; + g_ftpdglob.running = false; +#endif + } + + /* Then start the new daemon (if it is not already running) */ + +#ifdef CONFIG_NSH_BUILTIN_APPS + if (g_ftpdglob.stop && g_ftpdglob.running) + { + printf("Waiting for FTP daemon [%d] to stop\n", g_ftpdglob.pid); + return EXIT_FAILURE; + } + else +#endif + if (!g_ftpdglob.running) + { + printf("Starting the FTP daemon\n"); + g_ftpdglob.pid = TASK_CREATE("FTP daemon", CONFIG_EXAMPLES_FTPD_PRIO, + CONFIG_EXAMPLES_FTPD_STACKSIZE, + ftpd_daemon, NULL); + if (g_ftpdglob.pid < 0) { - ndbg("Failed to initialize the network\n"); + printf("Failed to start the FTP daemon: %d\n", errno); return EXIT_FAILURE; } - - g_ftpdglobls.initialized = true; - g_ftpdglobls.stop = false; } + else + { + printf("FTP daemon [%d] is running\n", g_ftpdglob.pid); + } + + return EXIT_SUCCESS; +} - /* Then start the new daemon */ +/**************************************************************************** + * Name: ftpd_stop + ****************************************************************************/ - g_telnetdcommon.daemon = daemon; - pid = TASK_CREATE("Telnet daemon", CONFIG_EXAMPLES_FTPD_PRIO, - CONFIG_EXAMPLES_FTPD_STACKSIZE, ftpd_daemon, NULL); - if (pid < 0) +#ifdef CONFIG_NSH_BUILTIN_APPS +int ftpd_stop(int s_argc, char **s_argv) +{ + if (!g_ftpdglob.initialized || g_ftpdglob.running) { - ndbg("Failed to start the telnet daemon: %d\n", errno); + printf("The FTP daemon not running\n"); return EXIT_FAILURE; } - printf("The FTP daemon is running, pid=%d\n", pid); + printf("Stopping the FTP daemon, pid=%d\n", g_ftpdglob.pid); + g_ftpdglob.stop = true; return EXIT_SUCCESS; } +#endif diff --git a/apps/include/netutils/ftpd.h b/apps/include/netutils/ftpd.h index 870b5cf94..f7581c89e 100755 --- a/apps/include/netutils/ftpd.h +++ b/apps/include/netutils/ftpd.h @@ -48,11 +48,34 @@ * Pre-processor Definitions ****************************************************************************/ /* Configuration ************************************************************/ +/* Required configuration settings: Of course TCP networking support is + * required. But here are a couple that are less obvious: + * + * CONFIG_DISABLE_PTHREAD - pthread support is required + * CONFIG_DISABLE_POLL - poll() support is required + * + * Other FTPD configuration options thay may be of interest: + * + * CONFIG_FTPD_VENDORID - The vendor name to use in FTP communications. + * Default: "NuttX" + * CONFIG_FTPD_SERVERID - The server name to use in FTP communications. + * Default: "NuttX FTP Server" + * CONFIG_FTPD_CMDBUFFERSIZE - The maximum size of one command. Default: + * 512 bytes. + * CONFIG_FTPD_DATABUFFERSIZE - The size of the I/O buffer for data + * transfers. Default: 2048 bytes. + * CONFIG_FTPD_WORKERSTACKSIZE - The stacksize to allocate for each + * FTP daemon worker thread. Default: 2048 bytes. + */ #ifdef CONFIG_DISABLE_PTHREAD # error "pthread support is required (CONFIG_DISABLE_PTHREAD=n)" #endif +#ifdef CONFIG_DISABLE_POLL +# error "poll() support is required (CONFIG_DISABLE_POLL=n)" +#endif + #ifndef CONFIG_FTPD_VENDORID # define CONFIG_FTPD_VENDORID "NuttX" #endif @@ -73,6 +96,13 @@ # define CONFIG_FTPD_WORKERSTACKSIZE 2048 #endif +/* Interface definitions ****************************************************/ + +#define FTPD_ACCOUNTFLAG_NONE (0) +#define FTPD_ACCOUNTFLAG_ADMIN (1 << 0) +#define FTPD_ACCOUNTFLAG_SYSTEM (1 << 1) +#define FTPD_ACCOUNTFLAG_GUEST (1 << 2) + /**************************************************************************** * Public Types ****************************************************************************/ @@ -123,7 +153,7 @@ EXTERN FTPD_SESSION ftpd_open(void); * Input Parameters: * handle - A handle previously returned by ftpd_open * accountflags - The characteristics of this user (see FTPD_ACCOUNTFLAGS_* - * defintiions. + * definitions above). * user - The user login name. May be NULL indicating that no login is * required. * passwd - The user password. May be NULL indicating that no password diff --git a/apps/netutils/ftpd/ftpd.c b/apps/netutils/ftpd/ftpd.c index fa15afeec..3dead6cb1 100755 --- a/apps/netutils/ftpd/ftpd.c +++ b/apps/netutils/ftpd/ftpd.c @@ -7,8 +7,8 @@ * Includes original code as well as logic adapted from hwport_ftpd, written * by Jaehyuk Cho <minzkn@minzkn.com> which is released under a BSD license. * - * Copyright (C) HWPORT.COM. All rights reserved. - * Author: JAEHYUK CHO <mailto:minzkn@minzkn.com> + * Copyright (C) hwport.com. All rights reserved. + * Author: Jaehyuk Cho <mailto:minzkn@minzkn.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -46,19 +46,32 @@ #include <nuttx/config.h> #include <sys/socket.h> +#include <sys/stat.h> #include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <dirent.h> #include <string.h> +#include <ctype.h> +#include <fcntl.h> +#include <poll.h> +#include <libgen.h> #include <errno.h> +#include <debug.h> #include <arpa/inet.h> +#include <apps/netutils/ftpd.h> + #include "ftpd.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ +#define __NUTTX__ 1 /* Flags some unusual NuttX dependencies */ + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -86,11 +99,9 @@ static FAR char *ftpd_strtok(bool skipspace, FAR const char *delimiters, FAR char **str); static FAR char *ftpd_strtok_alloc(bool skipspace, FAR const char *delimiters, FAR const char **str); -static int ftpd_patternmatch(FAR const char *pattern, FAR const char *str); /* Socket helpers */ -static int ftpd_getprotocol(FAR const char *protocol) static int ftpd_rxpoll(int sd, int timeout); static int ftpd_txpoll(int sd, int timeout); static int ftpd_accept(int sd, FAR void *addr, FAR socklen_t *addrlen, @@ -115,13 +126,13 @@ static FAR struct ftpd_pathnode_s * ftpd_nodeappend(FAR struct ftpd_pathnode_s *head, FAR struct ftpd_pathnode_s *node, bool override); static int ftpd_getpath(FAR struct ftpd_session_s *session, - FAR const char *chdirectory, FAR char **abspath, + FAR const char *path, FAR char **abspath, FAR char **workpath); /* Commmand helpers */ static int ftpd_changedir(FAR struct ftpd_session_s *session, - FAR char *rempath); + FAR const char *rempath); static off_t ftpd_offsatoi(FAR const char *filename, off_t offset); static int ftpd_stream(FAR struct ftpd_session_s *session, int cmdtype); static uint8_t ftpd_listoption(FAR char **param); @@ -224,7 +235,7 @@ static const struct ftpd_cmd_s g_ftpdcmdtab[] = {"OPTS", ftpd_command_opts, FTPD_CMDFLAG_LOGIN}, /* OPTS <SP> <option> <value> <CRLF> */ {"SITE", ftpd_command_site, FTPD_CMDFLAG_LOGIN}, /* SITE <SP> <string> <CRLF> */ {"HELP", ftpd_command_help, FTPD_CMDFLAG_LOGIN}, /* HELP [<SP> <string>] <CRLF> */ -#if 0L /* TODO */ +#if 0 {"SMNT", ftpd_command_smnt, FTPD_CMDFLAG_LOGIN}, /* SMNT <SP> <pathname> <CRLF> */ {"REIN", ftpd_command_rein, FTPD_CMDFLAG_LOGIN}, /* REIN <CRLF> */ {"STOU", ftpd_command_stou, FTPD_CMDFLAG_LOGIN}, /* STOU <CRLF> */ @@ -254,34 +265,11 @@ static const char *g_ftpdhelp[] = NULL }; -static const struct ftpd_protocol_s g_ftpdprotocols[] = -{ -#if defined(IPPROTO_TCP) - {"tcp", IPPROTO_TCP}, -#endif -#if defined(IPPROTO_UDP) - {"udp", IPPROTO_UDP}, -#endif -#if defined(IPPROTO_ICMP) - {"icmp", IPPROTO_ICMP}, -#endif -#if defined(IPPROTO_ICMPV6) - {"ipv6-icmp", IPPROTO_ICMPV6}, -#endif -#if defined(IPPROTO_IP) - {"ip", IPPROTO_IP}, -#endif -#if defined(IPPROTO_IPV6) - {"ipv6", IPPROTO_IPV6}, -#endif - {NULL, (int)0} -}; - /**************************************************************************** * Private Functions ****************************************************************************/ - /**************************************************************************** +/**************************************************************************** * Account Functions ****************************************************************************/ /**************************************************************************** @@ -298,7 +286,7 @@ static FAR struct ftpd_account_s *ftpd_account_new(FAR const char *user, /* Get the size of the allocation */ allocsize = sizeof(struct ftpd_account_s); - if (user == NULL) + if (!user) { usersize = 0; } @@ -311,7 +299,7 @@ static FAR struct ftpd_account_s *ftpd_account_new(FAR const char *user, /* Allocate the account and user string */ ret = (struct ftpd_account_s *)zalloc(allocsize); - if (ret == NULL) + if (!ret) { ndbg("Failed to allocate account\n"); return NULL; @@ -341,26 +329,26 @@ static void ftpd_account_free(FAR struct ftpd_account_s *account) /* Back up to the first entry in the list */ - while (account->blink != NULL) + while (account->blink) { account = account->blink; } /* Then free the entire list */ - while (account != NULL) + while (account) { prev = account; account = account->flink; /* Free the home path and the password */ - if (prev->home != NULL) + if (prev->home) { free(prev->home); } - if (prev->password != NULL) + if (prev->password) { free(prev->password); } @@ -405,7 +393,6 @@ static int ftpd_account_setpassord(FAR struct ftpd_account_s *account, account->password = temp; return OK; } -} /**************************************************************************** * Name: ftpd_account_add @@ -421,7 +408,7 @@ static int ftpd_account_add(FAR struct ftpd_server_s *server, /* Find the beginning of the list */ head = account; - while (head->blink != NULL + while (head->blink) { head = head->blink; } @@ -429,14 +416,14 @@ static int ftpd_account_add(FAR struct ftpd_server_s *server, /* Find the tail of the list */ tail = account; - while (tail->flink != NULL + while (tail->flink) { tail = tail->flink; } /* Handle the case where the list is empty */ - if (server->tail == NULL + if (server->tail) { server->head = head; } @@ -464,10 +451,10 @@ static int ftpd_account_sethome(FAR struct ftpd_account_s *account, /* Make a copy of the home path string (unless it is NULL) */ temp = NULL; - if (home != NULL) + if (home) { temp = strdup(home); - if (!tmp) + if (!temp) { return -ENOMEM; } @@ -515,7 +502,7 @@ ftpd_account_search_user(FAR struct ftpd_session_s *session, * that is good enough. */ - if (user && (account->user || strcmp(account->user, user) == 0) && newacount) + if (user && (account->user || strcmp(account->user, user) == 0) && newaccount) { break; } @@ -560,7 +547,7 @@ ftpd_account_search_user(FAR struct ftpd_session_s *session, /* Yes.. create the account */ newaccount = ftpd_account_new(account->user, accountflags); - if (newaccount != NULL + if (newaccount) { if (ftpd_account_setpassord(newaccount, account->password) != 0) { @@ -611,10 +598,10 @@ ftpd_account_login(FAR struct ftpd_session_s *session, account = ftpd_account_search_user(session, user, &dupaccount); if (!dupaccount) { - return(NULL; + return NULL; } - if (dupaccount->password == NULL) + if (!dupaccount->password) { if (!passwd) { @@ -664,13 +651,13 @@ ftpd_account_login(FAR struct ftpd_session_s *session, /* admin user */ session->home = strdup("/"); - session->work = strdup((home == NULL) ? "/" : home); + session->work = strdup(!home ? "/" : home); } else { /* normal user */ - session->home = strdup((home == NULL) ? "/" : home); + session->home = strdup(!home ? "/" : home); session->work = strdup("/"); } @@ -767,7 +754,7 @@ static FAR char *ftpd_strtok_alloc(bool skipspace, FAR const char *delimiters, while (*sptr != '\0') { dptr = delimiters; - while (*sptr |= *dptr && *dptr != '\0') + while (*sptr != *dptr && *dptr != '\0') { dptr++; } @@ -811,169 +798,10 @@ static FAR char *ftpd_strtok_alloc(bool skipspace, FAR const char *delimiters, } /**************************************************************************** - * Name: ftpd_strtok_alloc - ****************************************************************************/ - -static int ftpd_patternmatch(FAR const char *pattern, FAR const char *str) -{ - size_t patoffset; - size_t stroffset; - char patbyte; - char strbyte; - - patoffset = 0; - stroffset = 0; - - for (;;) - { - patbyte = pattern[patoffset]; - strbyte = str[stroffset]; - - if (patbyte == '/0') - { - break; - } - - if (patbyte == '*') - { - patoffset++; - patbyte = pattern[patoffset]; - if (patbyte == '\\') - { - patoffset++; - patbyte = pattern[patoffset]; - } - - while (strbyte != '/0') - { - if (patbyte == strbyte) - { - break; - } - - stroffset++; - strbyte = str[stroffset]; - } - - if (patbyte == '/0') - { - break; - } - } - else if (patbyte == '?') - { - if (strbyte == '/0') - { - return -1; - } - } - else - { - if (patbyte == '\\') - { - patoffset++; - patbyte = pattern[patoffset]; - if (patbyte == '/0') - { - break; - } - } - - if (patbyte != strbyte) - { - return -1; - } - } - - patoffset++; - if (strbyte != '/0') - { - stroffset++; - } - } - - return (patbyte == strbyte) ? 0 : -1; -} - -/**************************************************************************** * Socket Helpers ****************************************************************************/ /**************************************************************************** * Name: ftpd_rxpoll - * - * ip IP internet protocol, pseudo protocol number - * icmp ICMP internet control message protocol - * igmp IGMP Internet Group Management - * ggp GGP gateway-gateway protocol - * ipencap IP-ENCAP IP encapsulated in IP (officially ``IP'') - * st ST ST datagram mode - * tcp TCP transmission control protocol - * egp EGP exterior gateway protocol - * pup PUP PARC universal packet protocol - * udp UDP user datagram protocol - * hmp HMP host monitoring protocol - * xns-idp XNS-IDP Xerox NS IDP - * rdp RDP "reliable datagram" protocol - * iso-tp4 ISO-TP4 ISO Transport Protocol class 4 - * xtp XTP Xpress Tranfer Protocol - * ddp DDP Datagram Delivery Protocol - * idpr-cmtp IDPR-CMTP IDPR Control Message Transport - * ipv6 IPv6 IPv6 - * ipv6-route IPv6-Route Routing Header for IPv6 - * ipv6-frag IPv6-Frag Fragment Header for IPv6 - * idrp IDRP Inter-Domain Routing Protocol - * rsvp RSVP Reservation Protocol - * gre GRE General Routing Encapsulation - * esp ESP Encap Security Payload for IPv6 - * ah AH Authentication Header for IPv6 - * skip SKIP SKIP - * ipv6-icmp IPv6-ICMP ICMP for IPv6 - * ipv6-nonxt IPv6-NoNxt No Next Header for IPv6 - * ipv6-opts IPv6-Opts Destination Options for IPv6 - * rspf RSPF Radio Shortest Path First. - * vmtp VMTP Versatile Message Transport - * ospf OSPFIGP Open Shortest Path First IGP - * ipip IPIP IP-within-IP Encapsulation Protocol - * encap ENCAP Yet Another IP encapsulation - * pim PIM Protocol Independent Multicast - * - ****************************************************************************/ - -static int ftpd_getprotocol(FAR const char *protocol) -{ - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - FAR struct protoent *entry; - int index; - int ret; - - if (!protocol) - { - return 0; - } - - for (index = 0; g_ftpdprotocols[index].name != NULL; index++) - { - if (strcmp(protocol, g_ftpdprotocols[index].name) == 0) - { - return g_ftpdprotocols[index].value; - } - } - - ret = pthread_mutex_lock(&mutex); - if (ret != 0) - { - return 0; - } - - entry = getprotobyname(protocol); - ret = (entry != NULL) ? entry->p_proto : 0); - - (void)pthread_mutex_unlock(&mutex); - return ret; -} - -/**************************************************************************** - * Name: ftpd_rxpoll ****************************************************************************/ static int ftpd_rxpoll(int sd, int timeout) @@ -1063,7 +891,7 @@ static int ftpd_txpoll(int sd, int timeout) static int ftpd_accept(int sd, FAR void *addr, FAR socklen_t *addrlen, int timeout) { - int sd; + int acceptsd; int ret; /* Handle any requested timeout */ @@ -1080,15 +908,15 @@ static int ftpd_accept(int sd, FAR void *addr, FAR socklen_t *addrlen, /* Accept the connection -- waiting if necessary */ - sd = accept(sd, (FAR struct sockaddr *)addr, addrlen); - if (sd < 0) + acceptsd = accept(sd, (FAR struct sockaddr *)addr, addrlen); + if (acceptsd < 0) { int errval = errno; ndbg("accept() failed: %d\n", errval); return -errval; } - return sd; + return acceptsd; } /**************************************************************************** @@ -1137,7 +965,7 @@ static ssize_t ftpd_send(int sd, FAR const void *data, size_t size, int timeout) if (timeout >= 0) { - status = ftpd_txpoll(sd, timeout); + int status = ftpd_txpoll(sd, timeout); if (status < 0) { nvdbg("ftpd_rxpoll: %d\n", status); @@ -1164,7 +992,7 @@ static ssize_t ftpd_send(int sd, FAR const void *data, size_t size, int timeout) static ssize_t ftpd_response(int sd, int timeout, FAR const char *fmt, ...) { - FAR void *buffer; + FAR char *buffer; ssize_t bytessent; va_list ap; @@ -1172,7 +1000,7 @@ static ssize_t ftpd_response(int sd, int timeout, FAR const char *fmt, ...) avsprintf(&buffer, fmt, ap); va_end(ap); - if (buffer == NULL) + if (!buffer) { return -ENOMEM; } @@ -1199,14 +1027,14 @@ static int ftpd_dataopen(FAR struct ftpd_session_s *session) #ifdef CONFIG_NET_IPv6 if (session->data.addr.ss.ss_family == AF_INET6) { - session->data.sd = socket(PF_INET6, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); } else { - session->data.sd = socket(PF_INET, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); } #else - session->data.sd = socket(PF_INET, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #endif if (session->data.sd < 0) @@ -1220,7 +1048,7 @@ static int ftpd_dataopen(FAR struct ftpd_session_s *session) session->data.addrlen = (socklen_t)sizeof(session->data.addr); ret = connect(session->data.sd, (FAR const struct sockaddr *)(&session->data.addr), - session->data.addrlen, -1); + session->data.addrlen); if (ret < 0) { int errval = errno; @@ -1314,7 +1142,7 @@ static FAR struct ftpd_server_s *ftpd_openserver(int port) if (!server) { ndbg("Failed to allocate server\n"); - return -ENOMEM; + return NULL; } /* Initialize the server instance */ @@ -1326,7 +1154,7 @@ static FAR struct ftpd_server_s *ftpd_openserver(int port) /* Create the server listen socket */ #ifdef CONFIG_NET_IPv6 - server->sd = socket(PF_INET6, SOCK_STREAM, ftpd_getprotocol("tcp")); + server->sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); if (server->sd < 0) { ftpd_close((FTPD_SESSION)server); @@ -1342,7 +1170,7 @@ static FAR struct ftpd_server_s *ftpd_openserver(int port) server->addr.in6.sin6_addr = in6addr_any; server->addr.in6.sin6_port = htons(port); #else - server->sd = socket(PF_INET, SOCK_STREAM, ftpd_getprotocol("tcp")); + server->sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (server->sd < 0) { ftpd_close((FTPD_SESSION)server); @@ -1367,7 +1195,7 @@ static FAR struct ftpd_server_s *ftpd_openserver(int port) /* Bind the socket to the address */ - ret = bind(server->sd, (FAR const struct sockaddr *)addr, addrlen) + ret = bind(server->sd, (FAR const struct sockaddr *)addr, addrlen); if (ret < 0) { ftpd_close((FTPD_SESSION)server); @@ -1376,7 +1204,7 @@ static FAR struct ftpd_server_s *ftpd_openserver(int port) /* Listen on the socket */ - ret = listen(server->sd, backlog) + ret = listen(server->sd, backlog); if (ret < 0) { ftpd_close((FTPD_SESSION)server); @@ -1398,11 +1226,11 @@ static int ftpd_pathignore(FAR struct ftpd_pathnode_s *currpath) FAR struct ftpd_pathnode_s *node; size_t namelen; - namelen = (currpath->name == NULL) ? 0 : strlen(currpath->name); + namelen = !currpath->name ? 0 : strlen(currpath->name); if (namelen == 0) { - if (currpath->blink != NULL) + if (currpath->blink) { currpath->ignore = true; } @@ -1419,7 +1247,7 @@ static int ftpd_pathignore(FAR struct ftpd_pathnode_s *currpath) { if (!node->ignore) { - namelen = (node->name == NULL) ? 0 : strlen(node->name); + namelen = !node->name ? 0 : strlen(node->name); if (namelen > 0) { @@ -1455,7 +1283,7 @@ static void ftpd_nodefree(FAR struct ftpd_pathnode_s *node) prev = node; node = node->flink; - if (prev->name != NULL) + if (prev->name) { free(prev->name); } @@ -1469,13 +1297,10 @@ static void ftpd_nodefree(FAR struct ftpd_pathnode_s *node) static FAR struct ftpd_pathnode_s *ftpd_path2node(FAR const char *path) { -struct ftpd_pathnode_s *ftpd_path2node(const char *path) -{ FAR struct ftpd_pathnode_s *head = NULL; FAR struct ftpd_pathnode_s *tail = NULL; FAR struct ftpd_pathnode_s *newnode; FAR char *name; - int ret; if (!path) { @@ -1537,7 +1362,6 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, FAR char *path; FAR size_t allocsize; FAR size_t namelen; - int ret; if (!node) { @@ -1546,7 +1370,7 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, allocsize = 0; node1 = node; - while (node1 != NULL) + while (node1) { if (strip) { @@ -1558,7 +1382,7 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, } node2 = node1->flink; - while (strip && node2 != NULL) + while (strip && node2) { if (!node2->ignore) { @@ -1568,8 +1392,8 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, node2 = node2->flink; } - namelen = (node1->name == NULL) ? 0 : strlen(node1->name); - if (node2 == NULL) + namelen = !node1->name ? 0 : strlen(node1->name); + if (!node2) { if (namelen <= 0) { @@ -1596,7 +1420,7 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, allocsize = 0; node1 = node; - while (node1 != NULL) + while (node1) { if (strip != 0) { @@ -1608,7 +1432,7 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, } node2 = node1->flink; - while (strip && node2 != NULL) + while (strip && node2) { if (!node2->ignore) { @@ -1618,9 +1442,9 @@ static FAR char *ftpd_node2path(FAR struct ftpd_pathnode_s *node, node2 = node2->flink; } - namelen = (node1->name == NULL) ? 0 : strlen(node1->name); + namelen = !node1->name ? 0 : strlen(node1->name); - if (node2 == NULL) + if (!node2) { if (namelen <= 0) { @@ -1706,17 +1530,18 @@ ftpd_nodeappend(FAR struct ftpd_pathnode_s *head, } /**************************************************************************** - * Name: + * Name: ftpd_getpath ****************************************************************************/ -static int ftpd_getpath(FAR struct ftpd_session_s *session, FAR const char *chdirectory, FAR char **abspath, FAR char **workpath) +static int ftpd_getpath(FAR struct ftpd_session_s *session, + FAR const char *path, FAR char **abspath, + FAR char **workpath) { FAR struct ftpd_pathnode_s *abspath_node; FAR struct ftpd_pathnode_s *worknode; FAR struct ftpd_pathnode_s *appendnode; FAR char *abspath_local; FAR char *workpath_local; - int ret; if (abspath) { @@ -1728,13 +1553,13 @@ static int ftpd_getpath(FAR struct ftpd_session_s *session, FAR const char *chdi *workpath = NULL; } - worknode = ftpd_path2node((session->work == NULL) ? "" : session->work); + worknode = ftpd_path2node(!session->work ? "" : session->work); if (!worknode) { return -ENOMEM; } - appendnode = ftpd_path2node(chdirectory); + appendnode = ftpd_path2node(path); worknode = ftpd_nodeappend(worknode, appendnode, true); workpath_local = ftpd_node2path(worknode, 1); @@ -1744,7 +1569,7 @@ static int ftpd_getpath(FAR struct ftpd_session_s *session, FAR const char *chdi return -ENOMEM; } - abspath_node = ftpd_path2node((session->home == NULL) ? "" : session->home); + abspath_node = ftpd_path2node(!session->home ? "" : session->home); if (!abspath_node) { free(workpath_local); @@ -1786,7 +1611,6 @@ static int ftpd_getpath(FAR struct ftpd_session_s *session, FAR const char *chdi ftpd_nodefree(worknode); return OK; } -} /**************************************************************************** * Command Helpers @@ -1796,7 +1620,7 @@ static int ftpd_getpath(FAR struct ftpd_session_s *session, FAR const char *chdi ****************************************************************************/ static int ftpd_changedir(FAR struct ftpd_session_s *session, - FAR char *rempath) + FAR const char *rempath) { FAR char *abspath; FAR char *workpath; @@ -1853,7 +1677,7 @@ static off_t ftpd_offsatoi(FAR const char *filename, off_t offset) int ch; outstream = fopen(filename, "r"); - if (!outstream == NULL) + if (!outstream) { int errval = errno; ndbg("Failed to open %s: %d\n", filename, errval); @@ -1917,7 +1741,7 @@ static int ftpd_stream(FAR struct ftpd_session_s *session, int cmdtype) FAR char *path; bool isnew; int oflags; - uint8_t *buffer; + FAR char *buffer; size_t buflen; size_t wantsize; ssize_t rdbytes; @@ -2090,7 +1914,7 @@ static int ftpd_stream(FAR struct ftpd_session_s *session, int cmdtype) ndbg("ftp_recv failed: %d\n", rdbytes); (void)ftpd_response(session->cmd.sd, session->txtimeout, g_respfmt, 550, ' ', "Data read error !"); - ret = redbytes; + ret = rdbytes; break; } @@ -2161,7 +1985,7 @@ errout_with_session:; if (isnew && ret < 0) { - (void)remove(path); + (void)unlink(path); } errout_with_data:; @@ -2235,8 +2059,8 @@ static uint8_t ftpd_listoption(FAR char **param) ****************************************************************************/ static int ftpd_listbuffer(FAR struct ftpd_session_s *session, FAR char *path, - FAR struct stat *st, FAR char *buffer, size_t buflen, - unsigned int opton) + FAR struct stat *st, FAR char *buffer, + size_t buflen, unsigned int opton) { FAR char *name; size_t offset = 0; @@ -2282,7 +2106,7 @@ static int ftpd_listbuffer(FAR struct ftpd_session_s *session, FAR char *path, str = "-"; } - offset += snprint(&buffer[offset], buflen - offset, "%s", str); + offset += snprintf(&buffer[offset], buflen - offset, "%s", str); /* User */ @@ -2367,15 +2191,27 @@ static int ftpd_listbuffer(FAR struct ftpd_session_s *session, FAR char *path, /* nlink */ +#ifdef __NUTTX__ + offset += snprintf(&buffer[offset], buflen - offset, "%4u", 1); +#else offset += snprintf(&buffer[offset], buflen - offset, "%4u", st->st_nlink); +#endif /* username */ +#ifdef __NUTTX__ + offset += snprintf(&buffer[offset], buflen - offset, "user"); +#else offset += snprintf(&buffer[offset], buflen - offset, " %8u", st->st_uid); +#endif /* groupname */ +#ifdef __NUTTX__ + offset += snprintf(&buffer[offset], buflen - offset, "users"); +#else offset += snprintf(&buffer[offset], buflen - offset, " %8u", st->st_gid); +#endif /* size */ @@ -2383,11 +2219,11 @@ static int ftpd_listbuffer(FAR struct ftpd_session_s *session, FAR char *path, /* time */ - memcpy(&tm, localtime((GST const time_t *)&st->st_mtime), sizeof(tm)); + memcpy(&tm, localtime((FAR const time_t *)&st->st_mtime), sizeof(tm)); offset += snprintf(&buffer[offset], buflen - offset, " %s %2u", g_monthtab[tm.tm_mon], tm.tm_mday); now = time(0); - if ((now - st->st_mtime) > ((time_t)(60 * 60 * 24 * 180))) + if ((now - st->st_mtime) > (time_t)(60 * 60 * 24 * 180)) { offset += snprintf(&buffer[offset], buflen - offset, " %5u", tm.tm_year + 1900); @@ -2404,13 +2240,14 @@ static int ftpd_listbuffer(FAR struct ftpd_session_s *session, FAR char *path, /* linkname */ +#ifndef __NUTTX__ if (S_ISLNK(st->st_mode) != 0) { FAR char *temp; int namelen; temp = (FAR char *)malloc(PATH_MAX + 1); - if (temp != NULL) + if (temp) { namelen = readlink(path, temp, PATH_MAX); if (namelen != (-1))\ @@ -2422,7 +2259,8 @@ static int ftpd_listbuffer(FAR struct ftpd_session_s *session, FAR char *path, free(temp); } } - +#endif + /* end */ offset += snprintf(&buffer[offset], buflen - offset, "\r\n"); @@ -2458,12 +2296,12 @@ static int fptd_listscan(FAR struct ftpd_session_s *session, FAR char *path, if (!S_ISDIR(st.st_mode)) { - ret = ftpd_listbuffer(session, path, (&st, session->data.buffer, + ret = ftpd_listbuffer(session, path, &st, session->data.buffer, session->data.buflen, opton); if (ret == 0) { ret = ftpd_response(session->data.sd, session->txtimeout, - "%s", (char *)session->data.buffer); + "%s", (FAR char *)session->data.buffer); } return ret; @@ -2493,7 +2331,7 @@ static int fptd_listscan(FAR struct ftpd_session_s *session, FAR char *path, } } - asprintf(temp, "%s/%s", path, entry->d_name); + asprintf(&temp, "%s/%s", path, entry->d_name); if (!temp) { continue; @@ -2569,7 +2407,7 @@ static int ftpd_command_user(FAR struct ftpd_session_s *session) home = getenv("HOME"); session->curr = NULL; - session->home = strdup((home == NULL) ? "/" : home); + session->home = strdup(!home ? "/" : home); session->work = strdup("/"); ret = ftpd_response(session->cmd.sd, session->txtimeout, @@ -2625,7 +2463,7 @@ static int ftpd_command_pass(FAR struct ftpd_session_s *session) } session->curr = ftpd_account_login(session, session->user, session->param); - if (!session->curr != NULL + if (session->curr) { ret = ftpd_response(session->cmd.sd, session->txtimeout, g_respfmt, 230, ' ', "Login successful."); @@ -2829,7 +2667,7 @@ static int ftpd_command_port(FAR struct ftpd_session_s *session) #if 1 /* Follow param */ - memset(session->data.addr, 0, sizeof(session->data.addr)); + memset(&session->data.addr, 0, sizeof(session->data.addr)); session->data.addr.in4.sin_family = AF_INET; @@ -3092,7 +2930,7 @@ static int ftpd_command_rmd(FAR struct ftpd_session_s *session) "Can not remove current directory !"); } - ret = rmdir(abspath) + ret = rmdir(abspath); if (ret < 0) { free(abspath); @@ -3138,7 +2976,7 @@ static int ftpd_command_mkd(FAR struct ftpd_session_s *session) free(abspath); return ftpd_response(session->cmd.sd, session->txtimeout, - "g_respfmt, 250, ' ', "MKD command successful"); + g_respfmt, 250, ' ', "MKD command successful"); } /**************************************************************************** @@ -3178,7 +3016,7 @@ static int ftpd_command_dele(FAR struct ftpd_session_s *session) "Can not delete current directory !"); } - ret = remove(abspath); + ret = unlink(abspath); if (ret < 0) { free(abspath); @@ -3203,12 +3041,13 @@ static int ftpd_command_pasv(FAR struct ftpd_session_s *session) { unsigned int value[6]; unsigned int temp; + int ret; (void)ftpd_dataclose(session); session->data.addrlen = sizeof(session->data.addr); - session->data.sd = socket(PF_INET, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (session->data.sd < 0) { (void)ftpd_dataclose(session); @@ -3263,7 +3102,7 @@ static int ftpd_command_pasv(FAR struct ftpd_session_s *session) session->data.addr.in4.sin_port = 0; ret = bind(session->data.sd, (FAR const struct sockaddr *)&session->data.addr, - session->data.addrlen) + session->data.addrlen); if (ret < 0) { (void)ftpd_dataclose(session); @@ -3323,17 +3162,19 @@ static int ftpd_command_pasv(FAR struct ftpd_session_s *session) * Name: ftpd_command_epsv ****************************************************************************/ -static int ftpd_command_epsv(fAR struct ftpd_session_s *session) +static int ftpd_command_epsv(FAR struct ftpd_session_s *session) { + int ret; + (void)ftpd_dataclose(session); session->data.addrlen = sizeof(session->data.addr); #ifdef CONFIG_NET_IPv6 - session->data.sd = socket(PF_INET6, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP); if (session->data.sd < 0) { - session->data.sd = socket(PF_INET, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); } else { @@ -3343,7 +3184,7 @@ static int ftpd_command_epsv(fAR struct ftpd_session_s *session) #endif } #else - session->data.sd = socket(PF_INET, SOCK_STREAM, ftpd_getprotocol("tcp")); + session->data.sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); #endif if (session->data.sd < 0) @@ -3436,9 +3277,12 @@ static int ftpd_command_epsv(fAR struct ftpd_session_s *session) else #endif { - return ftpd_response(session->cmd.sd, session->txtimeout, - g_respfmt, 502, ' ', "EPSV command not implemented !"); + ret = ftpd_response(session->cmd.sd, session->txtimeout, + g_respfmt, 502, ' ', + "EPSV command not implemented !"); } + + return ret; } /**************************************************************************** @@ -3531,7 +3375,7 @@ static int ftpd_command_size(FAR struct ftpd_session_s *session) FAR FILE *outstream; off_t offset; int ch; - int status + int status; int ret; ret = ftpd_getpath(session, session->param, &abspath, NULL); @@ -3661,7 +3505,7 @@ static int ftpd_command_rnfr(FAR struct ftpd_session_s *session) struct stat st; int ret; - if (session->renamefrom != NULL) + if (session->renamefrom) { free(session->renamefrom); session->renamefrom = NULL; @@ -3861,8 +3705,7 @@ static int ftpd_command_opts(FAR struct ftpd_session_s *session) lang = getenv("LANG"); if (lang) { - if (ftpd_strcasestr(lang, "UTF8") != NULL || - ftpd_strcasestr(lang, "UTF-8") != NULL) + if (strcasestr(lang, "UTF8") || strcasestr(lang, "UTF-8")) { local = true; } @@ -3913,13 +3756,13 @@ static int ftpd_command_help(FAR struct ftpd_session_s *session) int ret; index = 0; - while (g_ftpdhelp[index] != NULL) + while (g_ftpdhelp[index]) { - if (index == 0 || g_ftpdhelp[index + 1] == NULL) + if (index == 0 || !g_ftpdhelp[index + 1]) { ret = ftpd_response(session->cmd.sd, session->txtimeout, g_respfmt, 214, - (g_ftpdhelp[index + 1] == NULL) ? ' ' : '-', + !g_ftpdhelp[index + 1] ? ' ' : '-', g_ftpdhelp[index]); } else @@ -3946,18 +3789,17 @@ static int ftpd_command_help(FAR struct ftpd_session_s *session) static int ftpd_command(FAR struct ftpd_session_s *session) { int index = 0; - int ret; /* Clear immediately status (USER, REST, RNFR) */ session->flags &= ~(FTPD_SESSIONFLAG_USER|FTPD_SESSIONFLAG_RESTARTPOS|FTPD_SESSIONFLAG_RENAMEFROM); - if (session->user != NULL) + if (session->user) { free(session->user); session->user = NULL; } - if (session->renamefrom != NULL) + if (session->renamefrom) { free(session->renamefrom); session->renamefrom = NULL; @@ -3967,11 +3809,11 @@ static int ftpd_command(FAR struct ftpd_session_s *session) /* Search the command table for a matching command */ - for (index = 0; g_ftpdcmdtab[index].cmd != NULL; index++) + for (index = 0; g_ftpdcmdtab[index].command; index++) { /* Does the command string match this entry? */ - if (strcmp(session->cmd, g_ftpdcmdtab[index].cmd) == 0) + if (strcmp(session->command, g_ftpdcmdtab[index].command) == 0) { /* Yes.. is a login required to execute this command? */ @@ -3979,7 +3821,7 @@ static int ftpd_command(FAR struct ftpd_session_s *session) { /* Yes... Check if the user is logged in */ - if (session->curr == NULL && session->head != NULL) + if (!session->curr && session->head) { return ftpd_response(session->cmd.sd, session->txtimeout, g_respfmt, 530, ' ', @@ -3989,7 +3831,7 @@ static int ftpd_command(FAR struct ftpd_session_s *session) /* Check if there is a handler for the command */ - if (g_ftpdcmdtab[index].handler != NULL) + if (g_ftpdcmdtab[index].handler) { /* Yess.. invoke the command handler. */ @@ -4005,7 +3847,7 @@ static int ftpd_command(FAR struct ftpd_session_s *session) /* There is nothing in the command table matching this command */ return ftpd_response(session->cmd.sd, session->txtimeout, - "%03u%c%s%s\r\n", 500, ' ', session->cmd, + "%03u%c%s%s\r\n", 500, ' ', session->command, " not understood"); } @@ -4032,7 +3874,7 @@ static int ftpd_startworker(pthread_startroutine_t handler, FAR void *arg, goto errout; } - /* The the thread stack size */ + /* The set the thread stack size */ ret = pthread_attr_setstacksize(&attr, stacksize); if (ret != 0) @@ -4041,21 +3883,21 @@ static int ftpd_startworker(pthread_startroutine_t handler, FAR void *arg, goto errout_with_attr; } - /* Start the thread in the detached state */ + /* And create the thread */ - ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) + ret = pthread_create(&threadid, &attr, handler, arg); if (ret != 0) { - ndbg("pthread_attr_setdetachstate() failed: %d\n", ret); + ndbg("pthread_create() failed: %d\n", ret); goto errout_with_attr; } - /* And create the thread */ + /* Put the thread in the detached stated */ - ret = pthread_create(&threadid, &attr, handler, arg); + ret = pthread_detach(threadid); if (ret != 0) { - ndbg("pthread_create() failed: %d\n", ret); + ndbg("pthread_detach() failed: %d\n", ret); } errout_with_attr: @@ -4089,7 +3931,7 @@ static void ftpd_freesession(FAR struct ftpd_session_s *session) if (session->user) { - free((session->user); + free(session->user); } if (session->fd < 0) @@ -4102,11 +3944,11 @@ static void ftpd_freesession(FAR struct ftpd_session_s *session) free(session->data.buffer); } - void)ftpd_dataclose(session); + (void)ftpd_dataclose(session); if (session->cmd.buffer) { - free((session->cmd.buffer); + free(session->cmd.buffer); } if (session->cmd.sd <0) @@ -4125,6 +3967,7 @@ static void ftpd_workersetup(FAR struct ftpd_session_s *session) { #if defined(CONFIG_NET_HAVE_IPTOS) || defined(CONFIG_NET_HAVE_OOBINLINE) int temp; +#endif #ifdef CONFIG_NET_HAVE_SOLINGER struct linger ling; #endif @@ -4184,7 +4027,7 @@ static FAR void *ftpd_worker(FAR void *arg) /* Receive the next command */ recvbytes = ftpd_recv(session->cmd.sd, session->cmd.buffer, - session->cmd.buflen - 1), session->rxtimeout); + session->cmd.buflen - 1, session->rxtimeout); /* recbytes < 0 is a receive failure (posibily a timeout); * recbytes == 0 indicates that we have lost the connection. @@ -4231,7 +4074,7 @@ static FAR void *ftpd_worker(FAR void *arg) /* Make command message */ - session->cmd = &session->cmd.buffer[offset]; + session->command = &session->cmd.buffer[offset]; while (session->cmd.buffer[offset] != '\0') { if (session->cmd.buffer[offset] == '\r' && @@ -4245,8 +4088,8 @@ static FAR void *ftpd_worker(FAR void *arg) /* Parse command and param tokens */ - session->param = session->cmd; - session->cmd = ftpd_strtok(true, " \t", &session->param); + session->param = session->command; + session->command = ftpd_strtok(true, " \t", &session->param); /* Unlike the "real" strtok, ftpd_strtok does not NUL-terminate * the returned string. @@ -4297,7 +4140,7 @@ FTPD_SESSION ftpd_open(void) FAR struct ftpd_server_s *server; server = ftpd_openserver(21); - if (server == NULL) + if (!server) { server = ftpd_openserver(2211); } @@ -4314,7 +4157,7 @@ FTPD_SESSION ftpd_open(void) * Input Parameters: * handle - A handle previously returned by ftpd_open * accountflags - The characteristics of this user (see FTPD_ACCOUNTFLAGS_* - * defintiions. + * definitions). * user - The user login name. May be NULL indicating that no login is * required. * passwd - The user password. May be NULL indicating that no password @@ -4338,7 +4181,7 @@ int ftpd_adduser(FTPD_SESSION handle, uint8_t accountflags, DEBUGASSERT(handle); newaccount = ftpd_account_new(user, accountflags); - if (newaccount == NULL) + if (!newaccount) { ndbg("Failed to allocte memory to the account\n"); ret = -ENOMEM; @@ -4411,7 +4254,7 @@ int ftpd_session(FTPD_SESSION handle, int timeout) /* Allocate a session */ session = (FAR struct ftpd_session_s *)zalloc(sizeof(struct ftpd_session_s)); - if (session == NULL) + if (!session) { ret = -ENOMEM; goto errout; @@ -4429,7 +4272,7 @@ int ftpd_session(FTPD_SESSION handle, int timeout) session->cmd.addrlen = (socklen_t)sizeof(session->cmd.addr); session->cmd.buflen = (size_t)CONFIG_FTPD_CMDBUFFERSIZE; session->cmd.buffer = NULL; - session->cmd = NULL; + session->command = NULL; session->param = NULL; session->data.sd = -1; session->data.addrlen = sizeof(session->data.addr); @@ -4445,8 +4288,8 @@ int ftpd_session(FTPD_SESSION handle, int timeout) /* Allocate a command buffer */ - session->cmd.buffer = (uint8_t *)malloc(session->cmd.buflen); - if (session->cmd.buffer == NULL) + session->cmd.buffer = (FAR char *)malloc(session->cmd.buflen); + if (!session->cmd.buffer) { ret = -ENOMEM; goto errout_with_session; @@ -4454,8 +4297,8 @@ int ftpd_session(FTPD_SESSION handle, int timeout) /* Allocate a data buffer */ - session->data.buffer = (uint8_t *)malloc(session->data.buflen); - if (session->data.buffer == NULL) + session->data.buffer = (FAR char *)malloc(session->data.buflen); + if (!session->data.buffer) { ret = -ENOMEM; goto errout_with_session; @@ -4463,9 +4306,9 @@ int ftpd_session(FTPD_SESSION handle, int timeout) /* Accept a connection */ - session->cmd.sd = ftpd_accept(server->sd, (FAR void *)(&session->cmd.addr), + session->cmd.sd = ftpd_accept(server->sd, (FAR void *)&session->cmd.addr, &session->cmd.addrlen, timeout); - if (session->cmd.sd < 0)) + if (session->cmd.sd < 0) { ret = -errno; goto errout_with_session; @@ -4512,7 +4355,7 @@ void ftpd_close(FTPD_SESSION handle) server = (struct ftpd_server_s *)handle; ftpd_account_free(server->head); - if (server->sd != -1)) + if (server->sd >= 0) { close(server->sd); server->sd = -1; diff --git a/apps/netutils/ftpd/ftpd.h b/apps/netutils/ftpd/ftpd.h index 0332b0a19..4397e96e3 100755 --- a/apps/netutils/ftpd/ftpd.h +++ b/apps/netutils/ftpd/ftpd.h @@ -50,24 +50,23 @@ /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -/* FPTD Definitions *********************************************************/ +/* Networking definitions ***************************************************/ + +#define FTPD_PROTOCOL_TCP (6) /* TCP protocol number */ -# define FTPD_ACCOUNTFLAG_NONE (0) -# define FTPD_ACCOUNTFLAG_ADMIN (1 << 0) -# define FTPD_ACCOUNTFLAG_SYSTEM (1 << 1) -# define FTPD_ACCOUNTFLAG_GUEST (1 << 2) +/* FPTD Definitions *********************************************************/ -# define FTPD_SESSIONFLAG_USER (1 << 0) -# define FTPD_SESSIONFLAG_RESTARTPOS (1 << 1) -# define FTPD_SESSIONFLAG_RENAMEFROM (1 << 2) +#define FTPD_SESSIONFLAG_USER (1 << 0) +#define FTPD_SESSIONFLAG_RESTARTPOS (1 << 1) +#define FTPD_SESSIONFLAG_RENAMEFROM (1 << 2) -# define FTPD_LISTOPTION_A (1 << 0) -# define FTPD_LISTOPTION_L (1 << 1) -# define FTPD_LISTOPTION_F (1 << 2) -# define FTPD_LISTOPTION_R (1 << 3) -# define FTPD_LISTOPTION_UNKNOWN (1 << 7) +#define FTPD_LISTOPTION_A (1 << 0) +#define FTPD_LISTOPTION_L (1 << 1) +#define FTPD_LISTOPTION_F (1 << 2) +#define FTPD_LISTOPTION_R (1 << 3) +#define FTPD_LISTOPTION_UNKNOWN (1 << 7) -# define FTPD_CMDFLAG_LOGIN (1 << 0) +#define FTPD_CMDFLAG_LOGIN (1 << 0) /**************************************************************************** * Public Types @@ -84,21 +83,21 @@ enum ftpd_sessiontype_e struct ftpd_pathnode_s { - struct ftpd_pathnode_s *flink; - struct ftpd_pathnode_s *blink; - bool ignore; - FAR char *name; + struct ftpd_pathnode_s *flink; + struct ftpd_pathnode_s *blink; + bool ignore; + FAR char *name; }; union ftpd_sockaddr_u { - uint8_t raw[sizeof(struct sockaddr_storage)]; - struct sockaddr_storage ss; - struct sockaddr sa; + uint8_t raw[sizeof(struct sockaddr_storage)]; + struct sockaddr_storage ss; + struct sockaddr sa; #ifdef CONFIG_NET_IPv6 - struct sockaddr_in6 in6; + struct sockaddr_in6 in6; #else - struct sockaddr_in in4; + struct sockaddr_in in4; #endif }; @@ -106,52 +105,52 @@ union ftpd_sockaddr_u struct ftpd_account_s { - struct ftpd_account_s *blink; - struct ftpd_account_s *flink; - uint8_t flags; /* See FTPD_ACCOUNTFLAG_* definitions */ - FAR char *user; /* User name */ - FAR char *password; /* Un-encrypted password */ - FAR char *home; /* Home directory path */ + struct ftpd_account_s *blink; + struct ftpd_account_s *flink; + uint8_t flags; /* See FTPD_ACCOUNTFLAG_* definitions */ + FAR char *user; /* User name */ + FAR char *password; /* Un-encrypted password */ + FAR char *home; /* Home directory path */ }; /* This structures describes an FTP session a list of associated accounts */ struct ftpd_server_s { - int sd; /* Listen socket descriptor */ - union ftpd_sockaddr_u addr; /* Listen address */ - struct ftpd_account_s *head; /* Head of a list of accounts */ - struct ftpd_account_s *tail; /* Tail of a list of accounts */ + int sd; /* Listen socket descriptor */ + union ftpd_sockaddr_u addr; /* Listen address */ + struct ftpd_account_s *head; /* Head of a list of accounts */ + struct ftpd_account_s *tail; /* Tail of a list of accounts */ }; struct ftpd_stream_s { - int sd; /* Socket descriptor */ - union ftpd_sockaddr_u addr; /* Network address */ - socklen_t addrlen; /* Length of the address */ - size_t buflen; /* Length of the buffer */ - uint8_t *buffer; /* Pointer to the buffer */ + int sd; /* Socket descriptor */ + union ftpd_sockaddr_u addr; /* Network address */ + socklen_t addrlen; /* Length of the address */ + size_t buflen; /* Length of the buffer */ + char *buffer; /* Pointer to the buffer */ }; struct ftpd_session_s { - FAR struct ftpd_server_s shadow; + FAR struct ftpd_server_s *server; FAR struct ftpd_account_s *head; FAR struct ftpd_account_s *curr; - uint8_t flags; /* See TPD_SESSIONFLAG_* definitions */ - int txtimeout; - int txtimeout; + uint8_t flags; /* See TPD_SESSIONFLAG_* definitions */ + int rxtimeout; + int txtimeout; /* Command */ - struct ftpd_stream_s cmd; - FAR char *cmd; - FAR char *param; + struct ftpd_stream_s cmd; + FAR char *command; + FAR char *param; /* Data */ - struct ftpd_stream_s data; - off_t restartpos; + struct ftpd_stream_s data; + off_t restartpos; /* File */ @@ -159,27 +158,27 @@ struct ftpd_session_s /* Current user */ - FAR char *user; - uint8_t m_type; /* See enum ftpd_sessiontype_e */ - FAR char *home; - FAR char *work; - FAR char *renamefrom; + FAR char *user; + uint8_t type; /* See enum ftpd_sessiontype_e */ + FAR char *home; + FAR char *work; + FAR char *renamefrom; }; typedef int (*ftpd_cmdhandler_t)(struct ftpd_session_s *); struct ftpd_cmd_s { - FAR const char *cmd; /* The command string */ - ftpd_cmdhandler_t handler; /* The function that handles the command */ - uint8_t flags; /* See FTPD_CMDFLAGS_* definitions */ + FAR const char *command; /* The command string */ + ftpd_cmdhandler_t handler; /* The function that handles the command */ + uint8_t flags; /* See FTPD_CMDFLAGS_* definitions */ }; /* Used to maintain a list of protocol names */ struct ftpd_protocol_s { - FAR const char *name; + FAR const char *name; int value; }; diff --git a/apps/nshlib/Makefile b/apps/nshlib/Makefile index 853d24baf..cbe1c8201 100644 --- a/apps/nshlib/Makefile +++ b/apps/nshlib/Makefile @@ -40,8 +40,8 @@ include $(APPDIR)/Make.defs # NSH Library ASRCS = -CSRCS = nsh_parse.c nsh_console.c nsh_fscmds.c nsh_ddcmd.c nsh_proccmds.c \ - nsh_mmcmds.c nsh_envcmds.c nsh_dbgcmds.c +CSRCS = nsh_init.c nsh_parse.c nsh_console.c nsh_fscmds.c nsh_ddcmd.c \ + nsh_proccmds.c nsh_mmcmds.c nsh_envcmds.c nsh_dbgcmds.c ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) CSRCS += nsh_apps.c diff --git a/apps/nshlib/nsh_init.c b/apps/nshlib/nsh_init.c new file mode 100644 index 000000000..7c7e78ea1 --- /dev/null +++ b/apps/nshlib/nsh_init.c @@ -0,0 +1,102 @@ +/**************************************************************************** + * apps/nshlib/nsh_init.c + * + * Copyright (C) 2007-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 "nsh.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nsh_initialize + * + * Description: + * This nterfaces is used to initialize the NuttShell (NSH). + * nsh_initialize() should be called one during application start-up prior + * to executing either nsh_consolemain() or nsh_telnetstart(). + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +void nsh_initialize(void) +{ + /* Mount the /etc filesystem */ + + (void)nsh_romfsetc(); + + /* Perform architecture-specific initialization (if available) */ + + (void)nsh_archinitialize(); + + /* Bring up the network */ + + (void)nsh_netinit(); +} + diff --git a/apps/nshlib/nsh_parse.c b/apps/nshlib/nsh_parse.c index cec167f05..ff1a50962 100644 --- a/apps/nshlib/nsh_parse.c +++ b/apps/nshlib/nsh_parse.c @@ -1018,37 +1018,6 @@ static inline int nsh_nice(FAR struct nsh_vtbl_s *vtbl, FAR char **ppcmd, FAR ch ****************************************************************************/ /**************************************************************************** - * Name: nsh_initialize - * - * Description: - * This nterfaces is used to initialize the NuttShell (NSH). - * nsh_initialize() should be called one during application start-up prior - * to executing either nsh_consolemain() or nsh_telnetstart(). - * - * Input Parameters: - * None - * - * Returned Value: - * None - * - ****************************************************************************/ - -void nsh_initialize(void) -{ - /* Mount the /etc filesystem */ - - (void)nsh_romfsetc(); - - /* Perform architecture-specific initialization (if available) */ - - (void)nsh_archinitialize(); - - /* Bring up the network */ - - (void)nsh_netinit(); -} - -/**************************************************************************** * Name: nsh_parse * * Description: |