ftp_server_transport.c
Go to the documentation of this file.
1 /**
2  * @file ftp_server_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-2024 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.4.4
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_server.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (FTP_SERVER_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open secure connection
46  * @param[in] context Pointer to the FTP server 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  FtpServerChannel *channel, size_t txBufferSize, size_t rxBufferSize)
55 {
56 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
57  error_t error;
58 
59  //Allocate TLS context
60  channel->tlsContext = tlsInit();
61  //Failed to allocate TLS context?
62  if(channel->tlsContext == NULL)
63  return ERROR_OPEN_FAILED;
64 
65  //Select server operation mode
67  //Any error to report?
68  if(error)
69  return error;
70 
71  //Bind TLS to the relevant socket
72  error = tlsSetSocket(channel->tlsContext, channel->socket);
73  //Any error to report?
74  if(error)
75  return error;
76 
77  //Set TX and RX buffer size
78  error = tlsSetBufferSize(channel->tlsContext, txBufferSize,
79  rxBufferSize);
80  //Any error to report?
81  if(error)
82  return error;
83 
84 #if (TLS_TICKET_SUPPORT == ENABLED)
85  //Enable session ticket mechanism
86  error = tlsEnableSessionTickets(channel->tlsContext, TRUE);
87  //Any error to report?
88  if(error)
89  return error;
90 
91  //Register ticket encryption/decryption callbacks
93  tlsDecryptTicket, &context->tlsTicketContext);
94  //Any error to report?
95  if(error)
96  return error;
97 #endif
98 
99  //Invoke user-defined callback, if any
100  if(context->settings.tlsInitCallback != NULL)
101  {
102  //Perform TLS related initialization
103  error = context->settings.tlsInitCallback(NULL, channel->tlsContext);
104  //Any error to report?
105  if(error)
106  return error;
107  }
108 
109  //Successful processing
110  return NO_ERROR;
111 #else
112  //Not implemented
113  return ERROR_NOT_IMPLEMENTED;
114 #endif
115 }
116 
117 
118 /**
119  * @brief Establish secure connection
120  * @param[in] channel Control or data channel
121  * @return Error code
122  **/
123 
125 {
126 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
127  //Establish a TLS connection
128  return tlsConnect(channel->tlsContext);
129 #else
130  //Not implemented
131  return ERROR_NOT_IMPLEMENTED;
132 #endif
133 }
134 
135 
136 /**
137  * @brief Send data using the relevant transport protocol
138  * @param[in] channel Control or data channel
139  * @param[in] data Pointer to a buffer containing the data to be transmitted
140  * @param[in] length Number of bytes to be transmitted
141  * @param[out] written Actual number of bytes written (optional parameter)
142  * @param[in] flags Set of flags that influences the behavior of this function
143  * @return Error code
144  **/
145 
147  size_t length, size_t *written, uint_t flags)
148 {
149  error_t error;
150 
151 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
152  //TLS-secured connection?
153  if(channel->tlsContext != NULL)
154  {
155  //Send TLS-encrypted data
156  error = tlsWrite(channel->tlsContext, data, length, written, flags);
157  }
158  else
159 #endif
160  {
161  //Transmit data
162  error = socketSend(channel->socket, data, length, written, flags);
163  }
164 
165  //Return status code
166  return error;
167 }
168 
169 
170 /**
171  * @brief Receive data using the relevant transport protocol
172  * @param[in] channel Control or data channel
173  * @param[out] data Buffer into which received data will be placed
174  * @param[in] size Maximum number of bytes that can be received
175  * @param[out] received Number of bytes that have been received
176  * @param[in] flags Set of flags that influences the behavior of this function
177  * @return Error code
178  **/
179 
181  size_t size, size_t *received, uint_t flags)
182 {
183  error_t error;
184 
185 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
186  //TLS-secured connection?
187  if(channel->tlsContext != NULL)
188  {
189  //Receive TLS-encrypted data
190  error = tlsRead(channel->tlsContext, data, size, received, flags);
191  }
192  else
193 #endif
194  {
195  //Receive data
196  error = socketReceive(channel->socket, data, size, received, flags);
197  }
198 
199  //Return status code
200  return error;
201 }
202 
203 #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:1486
#define FtpServerContext
Definition: ftp_server.h:208
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:65
Control or data channel.
Definition: ftp_server.h:380
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:349
error_t tlsDecryptTicket(TlsContext *context, const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *plaintext, size_t *plaintextLen, void *param)
Session ticket decryption.
Definition: tls_ticket.c:221
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:222
Transport protocol abstraction layer.
@ ERROR_OPEN_FAILED
Definition: error.h:75
#define tlsSetSocket(context, socket)
Definition: tls.h:927
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:1692
@ TLS_CONNECTION_END_SERVER
Definition: tls.h:969
error_t tlsSetTicketCallbacks(TlsContext *context, TlsTicketEncryptCallback ticketEncryptCallback, TlsTicketDecryptCallback ticketDecryptCallback, void *param)
Set ticket encryption/decryption callbacks.
Definition: tls.c:1517
TlsContext * tlsContext
TLS context.
Definition: ftp_server.h:384
error_t ftpServerEstablishSecureChannel(FtpServerChannel *channel)
Establish secure connection.
error_t tlsEnableSessionTickets(TlsContext *context, bool_t enabled)
Enable session ticket mechanism.
Definition: tls.c:1437
uint8_t length
Definition: tcp.h:368
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:1989
error_t ftpServerOpenSecureChannel(FtpServerContext *context, FtpServerChannel *channel, size_t txBufferSize, size_t rxBufferSize)
Open secure connection.
uint8_t flags
Definition: tcp.h:351
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:521
error_t tlsEncryptTicket(TlsContext *context, const uint8_t *plaintext, size_t plaintextLen, uint8_t *ciphertext, size_t *ciphertextLen, void *param)
Session ticket encryption.
Definition: tls_ticket.c:81
error_t ftpServerReadChannel(FtpServerChannel *channel, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
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:1854
FTP server (File Transfer Protocol)
Socket * socket
Underlying TCP socket.
Definition: ftp_server.h:382
error_t ftpServerWriteChannel(FtpServerChannel *channel, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1763
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.