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