ftp_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file ftp_client_transport.c
3  * @brief Transport protocol abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL FTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ftp/ftp_client.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (FTP_CLIENT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open network connection
46  * @param[in] context Pointer to the FTP client context
47  * @param[in] channel Control or data channel
48  * @param[in] txBufferSize TX buffer size
49  * @param[in] rxBufferSize RX buffer size
50  * @return Error code
51  **/
52 
54  FtpClientChannel *channel, size_t txBufferSize, size_t rxBufferSize)
55 {
56  error_t error;
57 
58  //Open a TCP socket
59  channel->socket = socketOpenEx(context->netContext, SOCKET_TYPE_STREAM,
61  //Failed to open socket?
62  if(channel->socket == NULL)
63  return ERROR_OPEN_FAILED;
64 
65  //Associate the socket with the relevant interface
66  error = socketBindToInterface(channel->socket, context->interface);
67  //Any error to report?
68  if(error)
69  return error;
70 
71  //Set timeout
72  error = socketSetTimeout(channel->socket, context->timeout);
73  //Any error to report?
74  if(error)
75  return error;
76 
77  //Specify the size of the send buffer
78  error = socketSetTxBufferSize(channel->socket, txBufferSize);
79  //Any error to report?
80  if(error)
81  return error;
82 
83  //Specify the size of the receive buffer
84  error = socketSetRxBufferSize(channel->socket, rxBufferSize);
85  //Any error to report?
86  if(error)
87  return error;
88 
89  //Successful processing
90  return NO_ERROR;
91 }
92 
93 
94 /**
95  * @brief Open secure connection
96  * @param[in] context Pointer to the FTP client context
97  * @param[in] channel Control or data channel
98  * @param[in] txBufferSize TX buffer size
99  * @param[in] rxBufferSize RX buffer size
100  * @return Error code
101  **/
102 
104  FtpClientChannel *channel, size_t txBufferSize, size_t rxBufferSize)
105 {
106 #if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
107  error_t error;
108 
109  //Allocate TLS context
110  channel->tlsContext = tlsInit();
111  //Failed to allocate TLS context?
112  if(channel->tlsContext == NULL)
113  return ERROR_OPEN_FAILED;
114 
115  //Select client operation mode
117  //Any error to report?
118  if(error)
119  return error;
120 
121  //Bind TLS to the relevant socket
122  error = tlsSetSocket(channel->tlsContext, channel->socket);
123  //Any error to report?
124  if(error)
125  return error;
126 
127  //Set TX and RX buffer size
128  error = tlsSetBufferSize(channel->tlsContext, txBufferSize, rxBufferSize);
129  //Any error to report?
130  if(error)
131  return error;
132 
133  //Data channel?
134  if(channel == &context->dataChannel)
135  {
136  //Save TLS session from control connection
137  error = tlsSaveSessionState(context->controlChannel.tlsContext,
138  &context->tlsSession);
139  //Any error to report?
140  if(error)
141  return error;
142  }
143 
144  //Restore TLS session
145  error = tlsRestoreSessionState(channel->tlsContext, &context->tlsSession);
146  //Any error to report?
147  if(error)
148  return error;
149 
150  //Invoke user-defined callback, if any
151  if(context->tlsInitCallback != NULL)
152  {
153  //Perform TLS related initialization
154  error = context->tlsInitCallback(context, channel->tlsContext);
155  //Any error to report?
156  if(error)
157  return error;
158  }
159 
160  //Successful processing
161  return NO_ERROR;
162 #else
163  //Not implemented
164  return ERROR_NOT_IMPLEMENTED;
165 #endif
166 }
167 
168 
169 /**
170  * @brief Establish secure connection
171  * @param[in] channel Control or data channel
172  * @return Error code
173  **/
174 
176 {
177 #if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
178  //Establish TLS connection
179  return tlsConnect(channel->tlsContext);
180 #else
181  //Not implemented
182  return ERROR_NOT_IMPLEMENTED;
183 #endif
184 }
185 
186 
187 /**
188  * @brief Shutdown network connection
189  * @param[in] channel Control or data channel
190  * @return Error code
191  **/
192 
194 {
195  error_t error;
196 
197  //Initialize status code
198  error = NO_ERROR;
199 
200 #if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
201  //Valid TLS context?
202  if(channel->tlsContext != NULL)
203  {
204  //Shutdown TLS session
205  error = tlsShutdown(channel->tlsContext);
206  }
207 #endif
208 
209  //Check status code
210  if(!error)
211  {
212  //Valid TCP socket?
213  if(channel->socket != NULL)
214  {
215  //Shutdown TCP connection
216  error = socketShutdown(channel->socket, SOCKET_SD_BOTH);
217  }
218  }
219 
220  //Return status code
221  return error;
222 }
223 
224 
225 /**
226  * @brief Close network connection
227  * @param[in] channel Control or data channel
228  **/
229 
231 {
232 #if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
233  //Release TLS context
234  if(channel->tlsContext != NULL)
235  {
236  tlsFree(channel->tlsContext);
237  channel->tlsContext = NULL;
238  }
239 #endif
240 
241  //Close TCP connection
242  if(channel->socket != NULL)
243  {
244  socketClose(channel->socket);
245  channel->socket = NULL;
246  }
247 }
248 
249 
250 /**
251  * @brief Send data using the relevant transport protocol
252  * @param[in] channel Control or data channel
253  * @param[in] data Pointer to a buffer containing the data to be transmitted
254  * @param[in] length Number of bytes to be transmitted
255  * @param[out] written Actual number of bytes written (optional parameter)
256  * @param[in] flags Set of flags that influences the behavior of this function
257  * @return Error code
258  **/
259 
261  size_t length, size_t *written, uint_t flags)
262 {
263  error_t error;
264 
265 #if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
266  //TLS-secured connection?
267  if(channel->tlsContext != NULL)
268  {
269  //Send TLS-encrypted data
270  error = tlsWrite(channel->tlsContext, data, length, written, flags);
271  }
272  else
273 #endif
274  {
275  //Transmit data
276  error = socketSend(channel->socket, data, length, written, flags);
277  }
278 
279  //Return status code
280  return error;
281 }
282 
283 
284 /**
285  * @brief Receive data using the relevant transport protocol
286  * @param[in] channel Control or data channel
287  * @param[out] data Buffer into which received data will be placed
288  * @param[in] size Maximum number of bytes that can be received
289  * @param[out] received Number of bytes that have been received
290  * @param[in] flags Set of flags that influences the behavior of this function
291  * @return Error code
292  **/
293 
295  size_t size, size_t *received, uint_t flags)
296 {
297  error_t error;
298 
299 #if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
300  //TLS-secured connection?
301  if(channel->tlsContext != NULL)
302  {
303  //Receive TLS-encrypted data
304  error = tlsRead(channel->tlsContext, data, size, received, flags);
305  }
306  else
307 #endif
308  {
309  //Receive data
310  error = socketReceive(channel->socket, data, size, received, flags);
311  }
312 
313  //Return status code
314  return error;
315 }
316 
317 #endif
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:1514
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:67
TlsContext * tlsContext
TLS context.
Definition: ftp_client.h:237
Control or data channel.
Definition: ftp_client.h:234
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:385
error_t ftpClientOpenChannel(FtpClientContext *context, FtpClientChannel *channel, size_t txBufferSize, size_t rxBufferSize)
Open network connection.
error_t ftpClientReadChannel(FtpClientChannel *channel, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
error_t ftpClientShutdownChannel(FtpClientChannel *channel)
Shutdown network connection.
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:3012
error_t socketSetTxBufferSize(Socket *socket, size_t size)
Specify the size of the TCP send buffer.
Definition: socket.c:1224
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2603
error_t socketSetRxBufferSize(Socket *socket, size_t size)
Specify the size of the TCP receive buffer.
Definition: socket.c:1261
#define tlsSetSocket(context, socket)
Definition: tls.h:970
error_t
Error codes.
Definition: error.h:43
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:1724
error_t ftpClientWriteChannel(FtpClientChannel *channel, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2052
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2943
uint8_t length
Definition: tcp.h:375
error_t tlsRead(TlsContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive application data from a the remote host using TLS.
Definition: tls.c:2285
#define socketBindToInterface
Definition: net_legacy.h:193
FTP client (File Transfer Protocol)
error_t ftpClientEstablishSecureChannel(FtpClientChannel *channel)
Establish secure connection.
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:557
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
#define FtpClientContext
Definition: ftp_client.h:128
Transport protocol abstraction layer.
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1012
error_t tlsWrite(TlsContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send application data to the remote host using TLS.
Definition: tls.c:2148
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2765
error_t ftpClientOpenSecureChannel(FtpClientContext *context, FtpClientChannel *channel, size_t txBufferSize, size_t rxBufferSize)
Open secure connection.
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ SOCKET_SD_BOTH
Definition: socket.h:161
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:169
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1819
Socket * socket
Underlying TCP socket.
Definition: ftp_client.h:235
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void ftpClientCloseChannel(FtpClientChannel *channel)
Close network connection.