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-2019 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 1.9.6
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
87  tlsDecryptTicket, &context->tlsTicketContext);
88  //Any error to report?
89  if(error)
90  return error;
91 #endif
92 
93  //Invoke user-defined callback, if any
94  if(context->settings.tlsInitCallback != NULL)
95  {
96  //Perform TLS related initialization
97  error = context->settings.tlsInitCallback(NULL, channel->tlsContext);
98  //Any error to report?
99  if(error)
100  return error;
101  }
102 
103  //Successful processing
104  return NO_ERROR;
105 #else
106  //Not implemented
107  return ERROR_NOT_IMPLEMENTED;
108 #endif
109 }
110 
111 
112 /**
113  * @brief Establish secure connection
114  * @param[in] channel Control or data channel
115  * @return Error code
116  **/
117 
119 {
120 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
121  //Establish a TLS connection
122  return tlsConnect(channel->tlsContext);
123 #else
124  //Not implemented
125  return ERROR_NOT_IMPLEMENTED;
126 #endif
127 }
128 
129 
130 
131 
132 
133 /**
134  * @brief Send data using the relevant transport protocol
135  * @param[in] channel Control or data channel
136  * @param[in] data Pointer to a buffer containing the data to be transmitted
137  * @param[in] length Number of bytes to be transmitted
138  * @param[out] written Actual number of bytes written (optional parameter)
139  * @param[in] flags Set of flags that influences the behavior of this function
140  * @return Error code
141  **/
142 
144  size_t length, size_t *written, uint_t flags)
145 {
146  error_t error;
147 
148 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
149  //TLS-secured connection?
150  if(channel->tlsContext != NULL)
151  {
152  //Send TLS-encrypted data
153  error = tlsWrite(channel->tlsContext, data, length, written, flags);
154  }
155  else
156 #endif
157  {
158  //Transmit data
159  error = socketSend(channel->socket, data, length, written, flags);
160  }
161 
162  //Return status code
163  return error;
164 }
165 
166 
167 /**
168  * @brief Receive data using the relevant transport protocol
169  * @param[in] channel Control or data channel
170  * @param[out] data Buffer into which received data will be placed
171  * @param[in] size Maximum number of bytes that can be received
172  * @param[out] received Number of bytes that have been received
173  * @param[in] flags Set of flags that influences the behavior of this function
174  * @return Error code
175  **/
176 
178  size_t size, size_t *received, uint_t flags)
179 {
180  error_t error;
181 
182 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
183  //TLS-secured connection?
184  if(channel->tlsContext != NULL)
185  {
186  //Receive TLS-encrypted data
187  error = tlsRead(channel->tlsContext, data, size, received, flags);
188  }
189  else
190 #endif
191  {
192  //Receive data
193  error = socketReceive(channel->socket, data, size, received, flags);
194  }
195 
196  //Return status code
197  return error;
198 }
199 
200 #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:514
#define FtpServerContext
Definition: ftp_server.h:203
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:65
uint8_t length
Definition: dtls_misc.h:149
Control or data channel.
Definition: ftp_server.h:372
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:312
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:211
Transport protocol abstraction layer.
#define tlsSetSocket(context, socket)
Definition: tls.h:823
error_t
Error codes.
Definition: error.h:42
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:609
error_t tlsSetTicketCallbacks(TlsContext *context, TlsTicketEncryptCallback ticketEncryptCallback, TlsTicketDecryptCallback ticketDecryptCallback, void *param)
Set ticket encryption/decryption callbacks.
Definition: tls.c:1326
TlsContext * tlsContext
TLS context.
Definition: ftp_server.h:377
error_t ftpServerEstablishSecureChannel(FtpServerChannel *channel)
Establish secure connection.
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:1798
error_t ftpServerOpenSecureChannel(FtpServerContext *context, FtpServerChannel *channel, size_t txBufferSize, size_t rxBufferSize)
Open secure connection.
uint8_t flags
Definition: tcp.h:314
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:481
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:82
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:1663
FTP server (File Transfer Protocol)
Socket * socket
Underlying TCP socket.
Definition: ftp_server.h:375
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:45
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1572
Success.
Definition: error.h:44
Debugging facilities.