From b20e2a5a26d3c9bbbea0dbea71227085bb8a135a Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 4 Jun 2011 20:13:03 +0000 Subject: Fix more FTP bugs git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3668 42af7a65-404d-4744-a932-0658087f49c3 --- apps/netutils/ftpc/ftpc_getfile.c | 10 ++++--- apps/netutils/ftpc/ftpc_socket.c | 61 ++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 30 deletions(-) (limited to 'apps/netutils') diff --git a/apps/netutils/ftpc/ftpc_getfile.c b/apps/netutils/ftpc/ftpc_getfile.c index 30595d942..846a1e1db 100644 --- a/apps/netutils/ftpc/ftpc_getfile.c +++ b/apps/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/apps/netutils/ftpc/ftpc_socket.c b/apps/netutils/ftpc/ftpc_socket.c index 6d917cf36..bfa8ec7c1 100644 --- a/apps/netutils/ftpc/ftpc_socket.c +++ b/apps/netutils/ftpc/ftpc_socket.c @@ -37,6 +37,7 @@ * Included Files ****************************************************************************/ +#include "ftpc_config.h" #include #include @@ -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. - */ + /* 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; - } + sock->instream = fdopen(sock->sd, "r"); + if (!sock->instream) + { + ndbg("fdopen() failed: %d\n", errno); + goto errout_with_sd; + } - /* Create the outgoing stream */ + /* Create the outgoing stream */ - sock->outstream = fdopen(sock->sd, "w"); - if (!sock->outstream) + sock->outstream = fdopen(sock->sd, "w"); + if (!sock->outstream) + { + ndbg("fdopen() failed: %d\n", errno); + goto errout_with_instream; + } + } + else { - ndbg("fdopen() failed: %d\n", errno); - goto errout_with_instream; + /* Should already be set up from ftpc_sockinit() call */ + + DEBUGASSERT(sock->sd >= 0 && sock->instream && sock->outstream); } return OK; -- cgit v1.2.3