diff --git a/examples/README.txt b/examples/README.txt index a67d9ad04..5a9d050b4 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -60,9 +60,11 @@ examples/ftpc an example, your configration could have different mass storage devices, mount paths, and FTP directories: - mount -t vfat /dev/mmcsd0 /tmp # Mount the SD card at /tmp - cd /tmp # cd into the /tmp directory - ftpc xx.xx.xx.xx[:pp] # Start the FTP client + nsh> mount -t vfat /dev/mmcsd0 /tmp # Mount the SD card at /tmp + nsh> cd /tmp # cd into the /tmp directory + nsh> ftpc xx.xx.xx.xx[:pp] # Start the FTP client + nfc> login <name> <password> # Log into the FTP server + nfc> help # See a list of FTP commands where xx.xx.xx.xx is the IP address of the FTP server and pp is an optional port number. diff --git a/netutils/ftpc/ftpc_getfile.c b/netutils/ftpc/ftpc_getfile.c index 30595d942..846a1e1db 100644 --- a/netutils/ftpc/ftpc_getfile.c +++ b/netutils/ftpc/ftpc_getfile.c @@ -149,13 +149,15 @@ static int ftpc_recvinit(struct ftpc_session_s *session, FAR const char *path, return ERROR; } - /* Accept a connection on the data socket */ + /* Accept a connection on the data socket (unless passive mode then the + * function does nothing). + */ ret = ftpc_sockaccept(&session->data, FTPC_IS_PASSIVE(session)); if (ret != OK) - { - ndbg("Data connection not accepted\n"); - } + { + ndbg("Data connection not accepted\n"); + } return ret; } diff --git a/netutils/ftpc/ftpc_socket.c b/netutils/ftpc/ftpc_socket.c index 6d917cf36..bfa8ec7c1 100644 --- a/netutils/ftpc/ftpc_socket.c +++ b/netutils/ftpc/ftpc_socket.c @@ -37,6 +37,7 @@ * Included Files ****************************************************************************/ +#include "ftpc_config.h" #include <sys/socket.h> #include <stdlib.h> @@ -208,7 +209,8 @@ void ftpc_sockcopy(FAR struct ftpc_socket_s *dest, * Name: ftpc_sockaccept * * Description: - * Accept a connection from the server. + * Accept a connection on the data socket (unless passive mode then this + * function does nothing). * ****************************************************************************/ @@ -217,16 +219,6 @@ int ftpc_sockaccept(struct ftpc_socket_s *sock, bool passive) struct sockaddr addr; socklen_t addrlen; - /* 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 (sock->sd > 0) - { - ftpc_sockclose(sock); - } - /* 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 @@ -246,6 +238,17 @@ int ftpc_sockaccept(struct ftpc_socket_s *sock, bool passive) if (!passive) { + /* 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) @@ -255,26 +258,32 @@ int ftpc_sockaccept(struct ftpc_socket_s *sock, bool passive) } 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; + } } - - /* 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) + else { - ndbg("fdopen() failed: %d\n", errno); - goto errout_with_sd; - } + /* Should already be set up from ftpc_sockinit() call */ - /* Create the outgoing stream */ - - sock->outstream = fdopen(sock->sd, "w"); - if (!sock->outstream) - { - ndbg("fdopen() failed: %d\n", errno); - goto errout_with_instream; + DEBUGASSERT(sock->sd >= 0 && sock->instream && sock->outstream); } return OK;