2011-06-01 21:15:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/ftpc/ftpc_socket.c
|
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2021-05-20 05:32:57 +02:00
|
|
|
#include <assert.h>
|
2011-06-01 21:15:14 +02:00
|
|
|
#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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
int ftpc_sockinit(FAR struct ftpc_socket_s *sock, sa_family_t family)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
/* Initialize the socket structure */
|
|
|
|
|
|
|
|
memset(sock, 0, sizeof(struct ftpc_socket_s));
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
DEBUGASSERT(family == AF_INET || family == AF_INET6);
|
|
|
|
|
|
|
|
sock->laddr.sa.sa_family = family;
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
/* Create a socket descriptor */
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
sock->sd = socket(family, SOCK_STREAM, IPPROTO_TCP);
|
2011-06-01 21:15:14 +02:00
|
|
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
void ftpc_sockclose(FAR struct ftpc_socket_s *sock)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2021-05-20 05:32:57 +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-02 22:45:35 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-05-20 05:32:57 +02:00
|
|
|
int ftpc_sockconnect(FAR struct ftpc_socket_s *sock,
|
|
|
|
FAR struct sockaddr *addr)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-06-02 22:45:35 +02:00
|
|
|
/* Connect to the server */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (addr->sa_family == AF_INET6)
|
|
|
|
{
|
2021-05-20 05:32:57 +02:00
|
|
|
ret = connect(sock->sd, (struct sockaddr *)addr,
|
|
|
|
sizeof(struct sockaddr_in6));
|
2017-11-07 00:41:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (addr->sa_family == AF_INET)
|
|
|
|
{
|
2021-05-20 05:32:57 +02:00
|
|
|
ret = connect(sock->sd, (struct sockaddr *)addr,
|
|
|
|
sizeof(struct sockaddr_in));
|
2017-11-07 00:41:30 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
nerr("ERROR: Unsupported address family\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
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)
|
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
memcpy(&dest->laddr, &src->laddr, sizeof(dest->laddr));
|
2011-06-01 21:15:14 +02:00
|
|
|
dest->connected = ftpc_sockconnected(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_sockaccept
|
|
|
|
*
|
|
|
|
* Description:
|
2017-11-07 00:41:30 +01:00
|
|
|
* Accept a connection on the data socket. This function is only used
|
2014-04-14 00:24:28 +02:00
|
|
|
* in active mode.
|
2011-06-05 16:08:26 +02:00
|
|
|
*
|
2021-05-20 05:32:57 +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 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.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
int ftpc_sockaccept(FAR struct ftpc_socket_s *acceptor,
|
|
|
|
FAR struct ftpc_socket_s *sock)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
union ftpc_sockaddr_u addr;
|
2011-06-01 21:15:14 +02:00
|
|
|
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);
|
2017-11-07 00:41:30 +01:00
|
|
|
sock->sd = accept(acceptor->sd, (struct sockaddr *)&addr, &addrlen);
|
2011-06-05 16:08:26 +02:00
|
|
|
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
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
memcpy(&sock->laddr, &addr, sizeof(union ftpc_sockaddr_u));
|
2011-06-05 16:08:26 +02:00
|
|
|
|
|
|
|
/* 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:
|
2021-05-20 05:32:57 +02:00
|
|
|
* Bind the socket to local address and wait for connection from server.
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
int ftpc_socklisten(FAR struct ftpc_socket_s *sock)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Bind the local socket to the local address */
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (sock->laddr.sa.sa_family == AF_INET6)
|
|
|
|
{
|
|
|
|
sock->laddr.in6.sin6_port = 0;
|
|
|
|
ret = bind(sock->sd, (struct sockaddr *)&sock->laddr,
|
|
|
|
sizeof(struct sockaddr_in6));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (sock->laddr.sa.sa_family == AF_INET)
|
|
|
|
{
|
|
|
|
sock->laddr.in4.sin_port = 0;
|
|
|
|
ret = bind(sock->sd, (struct sockaddr *)&sock->laddr,
|
|
|
|
sizeof(struct sockaddr_in));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
nerr("ERROR: unsupported family\n");
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
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
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
int ftpc_sockprintf(FAR struct ftpc_socket_s *sock, FAR 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,
|
2017-11-07 00:41:30 +01:00
|
|
|
FAR union ftpc_sockaddr_u *addr)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
socklen_t len;
|
2011-06-01 21:15:14 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
len = sizeof(union ftpc_sockaddr_u);
|
|
|
|
|
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;
|
|
|
|
}
|
2021-05-20 05:32:57 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
return OK;
|
|
|
|
}
|