2011-06-01 21:15:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/ftpc/ftpc_socket.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-04 22:13:03 +02:00
|
|
|
#include "ftpc_config.h"
|
2011-06-01 21:15:14 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "ftpc_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize a socket. Create the socket and "wrap" it as C standard
|
|
|
|
* incoming and outgoing streams.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_sockinit(FAR struct ftpc_socket_s *sock)
|
|
|
|
{
|
|
|
|
/* Initialize the socket structure */
|
|
|
|
|
|
|
|
memset(sock, 0, sizeof(struct ftpc_socket_s));
|
|
|
|
|
|
|
|
/* Create a socket descriptor */
|
|
|
|
|
|
|
|
sock->sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (sock->sd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: socket() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Call fdopen to "wrap" the socket descriptor as an input stream using C
|
|
|
|
* buffered I/O.
|
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
sock->instream = fdopen(sock->sd, "r");
|
2011-06-01 21:15:14 +02:00
|
|
|
if (!sock->instream)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: fdopen() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout_with_sd;
|
|
|
|
}
|
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Call fdopen to "wrap" the socket descriptor as an output stream using C
|
|
|
|
* buffered I/O.
|
|
|
|
*/
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
sock->outstream = fdopen(sock->sd, "w");
|
2011-06-01 21:15:14 +02:00
|
|
|
if (!sock->outstream)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: fdopen() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout_with_instream;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Close the instream. NOTE: Since the underlying socket descriptor is
|
|
|
|
* *not* dup'ed, the following close should fail harmlessly.
|
|
|
|
*/
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
errout_with_instream:
|
|
|
|
fclose(sock->instream);
|
2011-06-02 22:45:35 +02:00
|
|
|
sock->instream = NULL;
|
2011-06-01 21:15:14 +02:00
|
|
|
errout_with_sd:
|
|
|
|
close(sock->sd);
|
2011-06-02 22:45:35 +02:00
|
|
|
sock->sd = -1;
|
2011-06-01 21:15:14 +02:00
|
|
|
errout:
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockclose
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Close a socket
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ftpc_sockclose(struct ftpc_socket_s *sock)
|
|
|
|
{
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Note that the same underlying socket descriptor is used for both streams.
|
|
|
|
* There should be harmless failures on the second fclose and the close.
|
|
|
|
*/
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
fclose(sock->instream);
|
|
|
|
fclose(sock->outstream);
|
|
|
|
close(sock->sd);
|
|
|
|
memset(sock, 0, sizeof(struct ftpc_socket_s));
|
2011-06-02 22:45:35 +02:00
|
|
|
sock->sd = -1;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockconnect
|
|
|
|
*
|
|
|
|
* Description:
|
2011-06-02 22:45:35 +02:00
|
|
|
* Connect the socket to the host. On a failure, the caller should call.
|
|
|
|
* ftpc_sockclose() to clean up.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_sockconnect(struct ftpc_socket_s *sock, struct sockaddr_in *addr)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Connect to the server */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
ret = connect(sock->sd, (struct sockaddr *)addr, sizeof(struct sockaddr));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: connect() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the local address of the socket */
|
|
|
|
|
|
|
|
ret = ftpc_sockgetsockname(sock, &sock->laddr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpc_sockgetsockname() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-06-05 18:13:50 +02:00
|
|
|
sock->connected = true;
|
2011-06-01 21:15:14 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockcopy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Copy the socket state from one location to another.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ftpc_sockcopy(FAR struct ftpc_socket_s *dest,
|
|
|
|
FAR const struct ftpc_socket_s *src)
|
|
|
|
{
|
|
|
|
memcpy(&dest->laddr, &src->laddr, sizeof(struct sockaddr_in));
|
|
|
|
dest->connected = ftpc_sockconnected(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockaccept
|
|
|
|
*
|
|
|
|
* Description:
|
2011-06-05 16:08:26 +02:00
|
|
|
* Accept a connection on the data socket. This function is onlly used
|
2014-04-14 00:24:28 +02:00
|
|
|
* in active mode.
|
2011-06-05 16:08:26 +02:00
|
|
|
*
|
|
|
|
* 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
|
2014-04-14 00:24:28 +02:00
|
|
|
* both connections to the server, solving the problem of firewalls filtering
|
2011-06-05 16:08:26 +02:00
|
|
|
* 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
|
2014-04-14 00:24:28 +02:00
|
|
|
* initiates the connection from port N+1 to port P on the server to transfer
|
2011-06-05 16:08:26 +02:00
|
|
|
* data.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
int ftpc_sockaccept(FAR struct ftpc_socket_s *sock)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
struct sockaddr addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
/* 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.
|
2011-06-01 21:15:14 +02:00
|
|
|
*/
|
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
if (sock->sd > 0)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2011-06-05 16:08:26 +02:00
|
|
|
ftpc_sockclose(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
addrlen = sizeof(addr);
|
|
|
|
sock->sd = accept(sock->sd, &addr, &addrlen);
|
|
|
|
if (sock->sd == -1)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: accept() failed: %d\n", errno);
|
2011-06-05 16:08:26 +02:00
|
|
|
return ERROR;
|
2011-06-04 22:13:03 +02:00
|
|
|
}
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
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)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: fdopen() failed: %d\n", errno);
|
2011-06-05 16:08:26 +02:00
|
|
|
goto errout_with_sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the outgoing stream */
|
2011-06-04 22:13:03 +02:00
|
|
|
|
2011-06-05 16:08:26 +02:00
|
|
|
sock->outstream = fdopen(sock->sd, "w");
|
|
|
|
if (!sock->outstream)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: fdopen() failed: %d\n", errno);
|
2011-06-05 16:08:26 +02:00
|
|
|
goto errout_with_instream;
|
2011-06-01 21:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Close the instream. NOTE: Since the underlying socket descriptor is
|
|
|
|
* *not* dup'ed, the following close should fail harmlessly.
|
|
|
|
*/
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
errout_with_instream:
|
|
|
|
fclose(sock->instream);
|
2011-06-02 22:45:35 +02:00
|
|
|
sock->instream = NULL;
|
2011-06-01 21:15:14 +02:00
|
|
|
errout_with_sd:
|
|
|
|
close(sock->sd);
|
|
|
|
sock->sd = -1;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_socklisten
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Bind the socket to local address and wait for connection from the server.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_socklisten(struct ftpc_socket_s *sock)
|
|
|
|
{
|
|
|
|
unsigned int addrlen = sizeof(sock->laddr);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Bind the local socket to the local address */
|
|
|
|
|
|
|
|
sock->laddr.sin_port = 0;
|
|
|
|
ret = bind(sock->sd, (struct sockaddr *)&sock->laddr, addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: bind() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the connection to the server */
|
|
|
|
|
|
|
|
if (listen(sock->sd, 1) == -1)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then get the local address selected by NuttX */
|
|
|
|
|
|
|
|
ret = ftpc_sockgetsockname(sock, &sock->laddr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockprintf
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* printf to a socket stream
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-06-03 01:45:31 +02:00
|
|
|
int ftpc_sockprintf(struct ftpc_socket_s *sock, const char *fmt, ...)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int r;
|
|
|
|
|
2011-06-03 01:45:31 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
r = vfprintf(sock->outstream, fmt, ap);
|
2011-06-01 21:15:14 +02:00
|
|
|
va_end(ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockgetsockname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the address of the local socket
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_sockgetsockname(FAR struct ftpc_socket_s *sock,
|
|
|
|
FAR struct sockaddr_in *addr)
|
|
|
|
{
|
2011-06-02 20:36:20 +02:00
|
|
|
socklen_t len = sizeof(struct sockaddr_in);
|
2011-06-01 21:15:14 +02:00
|
|
|
int ret;
|
|
|
|
|
2011-06-02 20:36:20 +02:00
|
|
|
ret = getsockname(sock->sd, (FAR struct sockaddr *)addr, &len);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: getsockname failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|