2011-06-01 21:15:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/netutils/ftpc/ftpc_connect.c
|
|
|
|
*
|
2020-05-07 22:07:08 +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
|
|
|
*
|
2020-05-07 22:07:08 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-06-01 21:15:14 +02:00
|
|
|
*
|
2020-05-07 22:07:08 +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-03 01:45:31 +02:00
|
|
|
#include "ftpc_config.h"
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-06-02 01:09:29 +02:00
|
|
|
#include <unistd.h>
|
2011-06-03 22:27:30 +02:00
|
|
|
#include <string.h>
|
2011-06-01 21:15:14 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/ftpc.h"
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
#include "ftpc_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_connect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a session handle and connect to the server.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
SESSION ftpc_connect(FAR union ftpc_sockaddr_u *server)
|
2011-06-01 21:15:14 +02:00
|
|
|
{
|
|
|
|
FAR struct ftpc_session_s *session;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Allocate a session structure */
|
|
|
|
|
|
|
|
session = (struct ftpc_session_s *)zalloc(sizeof(struct ftpc_session_s));
|
|
|
|
if (!session)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to allocate a session\n");
|
2020-05-07 22:07:08 +02:00
|
|
|
errno = ENOMEM;
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-06-03 17:38:26 +02:00
|
|
|
/* Initialize the session structure with all non-zero and variable values */
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
memcpy(&session->server, server, sizeof(union ftpc_sockaddr_u));
|
|
|
|
|
2011-06-04 15:32:26 +02:00
|
|
|
session->flags &= ~FTPC_FLAGS_CLEAR;
|
|
|
|
session->flags |= FTPC_FLAGS_SET;
|
2011-06-03 17:38:26 +02:00
|
|
|
session->replytimeo = CONFIG_FTP_DEFTIMEO * CLOCKS_PER_SEC;
|
|
|
|
session->conntimeo = CONFIG_FTP_DEFTIMEO * CLOCKS_PER_SEC;
|
|
|
|
session->pid = getpid();
|
2011-06-01 21:15:14 +02:00
|
|
|
|
2021-07-03 05:03:47 +02:00
|
|
|
session->cmd.sd = -1;
|
|
|
|
session->data.sd = -1;
|
|
|
|
session->dacceptor.sd = -1;
|
|
|
|
|
2011-06-02 20:36:20 +02:00
|
|
|
/* Use the default port if the user specified port number zero */
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (session->server.sa.sa_family == AF_INET6)
|
2011-06-02 20:36:20 +02:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
if (!session->server.in6.sin6_port)
|
|
|
|
{
|
|
|
|
session->server.in6.sin6_port = HTONS(CONFIG_FTP_DEFPORT);
|
|
|
|
}
|
2011-06-02 20:36:20 +02:00
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
#endif
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (session->server.sa.sa_family == AF_INET)
|
2011-06-02 20:36:20 +02:00
|
|
|
{
|
2017-11-07 00:41:30 +01:00
|
|
|
if (!session->server.in4.sin_port)
|
|
|
|
{
|
|
|
|
session->server.in4.sin_port = HTONS(CONFIG_FTP_DEFPORT);
|
|
|
|
}
|
2011-06-02 20:36:20 +02:00
|
|
|
}
|
2017-11-07 00:41:30 +01:00
|
|
|
#endif
|
2011-06-02 20:36:20 +02:00
|
|
|
|
2011-06-03 22:27:30 +02:00
|
|
|
/* Get the local home directory, i.e., the value of the PWD environment
|
|
|
|
* variable at the time of the connection. We keep a local copy so that
|
|
|
|
* we can change the current working directory without effecting any other
|
|
|
|
* logic that may be in same context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
session->homeldir = strdup(ftpc_lpwd());
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
/* And (Re-)connect to the server */
|
|
|
|
|
|
|
|
ret = ftpc_reconnect(session);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpc_reconnect() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
2011-06-03 17:38:26 +02:00
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
return (SESSION)session;
|
|
|
|
|
|
|
|
errout_with_alloc:
|
2017-07-27 15:29:06 +02:00
|
|
|
if (session->homeldir != NULL)
|
|
|
|
{
|
|
|
|
free(session->homeldir);
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
free(session);
|
|
|
|
errout:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftpc_reconnect
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* re-connect to the server either initially, or after loss of connection.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftpc_reconnect(FAR struct ftpc_session_s *session)
|
|
|
|
{
|
2016-06-13 20:06:44 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_ERROR
|
2017-11-07 00:41:30 +01:00
|
|
|
char buffer[48];
|
2011-06-01 21:15:14 +02:00
|
|
|
#endif
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Re-initialize the session structure */
|
|
|
|
|
|
|
|
session->replytimeo = CONFIG_FTP_DEFTIMEO * CLOCKS_PER_SEC;
|
|
|
|
session->conntimeo = CONFIG_FTP_DEFTIMEO * CLOCKS_PER_SEC;
|
|
|
|
session->xfrmode = FTPC_XFRMODE_UNKNOWN;
|
|
|
|
|
|
|
|
/* Set up a timer to prevent hangs */
|
|
|
|
|
2020-08-09 20:31:34 +02:00
|
|
|
ret = wd_start(&session->wdog, session->conntimeo,
|
2020-08-10 18:54:08 +02:00
|
|
|
ftpc_timeout, (wdparm_t)session);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: wd_start() failed\n");
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a socket */
|
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
ret = ftpc_sockinit(&session->cmd, session->server.sa.sa_family);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpc_sockinit() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect the socket to the server */
|
|
|
|
|
2016-06-13 20:06:44 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_ERROR
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (session->server.sa.sa_family == AF_INET6)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
if (inet_ntop(AF_INET6, &session->server.in6.sin6_addr, buffer, 48))
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ninfo("Connecting to server address %s:%d\n", buffer,
|
|
|
|
ntohs(session->server.in6.sin6_port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (session->server.sa.sa_family == AF_INET)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
if (inet_ntop(AF_INET, &session->server.in4.sin_addr, buffer, 48))
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ninfo("Connecting to server address %s:%d\n", buffer,
|
|
|
|
ntohs(session->server.in4.sin_port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
#endif /* CONFIG_DEBUG_NET_ERROR */
|
2011-06-03 17:38:26 +02:00
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
ret = ftpc_sockconnect(&session->cmd,
|
|
|
|
(FAR struct sockaddr *)&session->server);
|
2011-06-01 21:15:14 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: ftpc_sockconnect() failed: %d\n", errno);
|
2011-06-01 21:15:14 +02:00
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read startup message from server */
|
|
|
|
|
|
|
|
fptc_getreply(session);
|
|
|
|
|
|
|
|
/* Check for "120 Service ready in nnn minutes" */
|
|
|
|
|
|
|
|
if (session->code == 120)
|
|
|
|
{
|
|
|
|
fptc_getreply(session);
|
|
|
|
}
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2020-08-09 20:31:34 +02:00
|
|
|
wd_cancel(&session->wdog);
|
2011-06-01 21:15:14 +02:00
|
|
|
|
|
|
|
if (!ftpc_sockconnected(&session->cmd))
|
|
|
|
{
|
|
|
|
ftpc_reset(session);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for "220 Service ready for new user" */
|
|
|
|
|
|
|
|
if (session->code == 220)
|
|
|
|
{
|
|
|
|
FTPC_SET_CONNECTED(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FTPC_IS_CONNECTED(session))
|
|
|
|
{
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
2016-06-13 20:06:44 +02:00
|
|
|
#ifdef CONFIG_DEBUG_NET_ERROR
|
2016-06-15 01:53:47 +02:00
|
|
|
ninfo("Connected\n");
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (session->server.sa.sa_family == AF_INET6)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
if (inet_ntop(AF_INET6, &session->server.in6.sin6_addr, buffer, 48))
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ninfo(" Remote address: %s:%d\n", buffer,
|
|
|
|
ntohs(session->server.in6.sin6_port));
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
if (inet_ntop(AF_INET6, &session->cmd.laddr.in6.sin6_addr, buffer, 48))
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ninfo(" Local address: %s:%d\n", buffer,
|
|
|
|
ntohs(session->cmd.laddr.in6.sin6_port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2020-05-02 15:30:54 +02:00
|
|
|
|
2017-11-07 00:41:30 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
|
|
|
if (session->server.sa.sa_family == AF_INET)
|
|
|
|
{
|
2020-05-02 15:30:54 +02:00
|
|
|
if (inet_ntop(AF_INET, &session->server.in4.sin_addr, buffer, 48))
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ninfo(" Remote address: %s:%d\n", buffer,
|
|
|
|
ntohs(session->server.in4.sin_port));
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:30:54 +02:00
|
|
|
if (inet_ntop(AF_INET, &session->cmd.laddr.in4.sin_addr, buffer, 48))
|
2017-11-07 00:41:30 +01:00
|
|
|
{
|
|
|
|
ninfo(" Local address: %s:%d\n", buffer,
|
|
|
|
ntohs(session->cmd.laddr.in4.sin_port));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_NET_IPv4 */
|
|
|
|
#endif /* CONFIG_DEBUG_NET_ERROR */
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_socket:
|
|
|
|
ftpc_sockclose(&session->cmd);
|
|
|
|
errout:
|
2023-03-20 10:35:06 +01:00
|
|
|
if (WDOG_ISACTIVE(&session->wdog))
|
|
|
|
{
|
|
|
|
wd_cancel(&session->wdog);
|
|
|
|
}
|
|
|
|
|
2011-06-01 21:15:14 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|