ftp_server_data.c
Go to the documentation of this file.
1 /**
2  * @file ftp_server_data.c
3  * @brief FTP data connection
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 "ftp/ftp_server.h"
36 #include "ftp/ftp_server_data.h"
38 #include "ftp/ftp_server_misc.h"
39 #include "path.h"
40 #include "debug.h"
41 
42 //Check TCP/IP stack configuration
43 #if (FTP_SERVER_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Register data connection events
48  * @param[in] connection Pointer to the client connection
49  * @param[in] eventDesc Socket events to be registered
50  **/
51 
53  SocketEventDesc *eventDesc)
54 {
55  //Check the state of the data connection
56  if(connection->dataChannel.state == FTP_CHANNEL_STATE_LISTEN)
57  {
58  //Wait for data to be available for reading
59  eventDesc->socket = connection->dataChannel.socket;
60  eventDesc->eventMask = SOCKET_EVENT_RX_READY;
61  }
62  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_CONNECT_TLS)
63  {
64 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
65  //Any data pending in the send buffer?
66  if(tlsIsTxReady(connection->dataChannel.tlsContext))
67  {
68  //Wait until there is more room in the send buffer
69  eventDesc->socket = connection->dataChannel.socket;
70  eventDesc->eventMask = SOCKET_EVENT_TX_READY;
71  }
72  else
73  {
74  //Wait for data to be available for reading
75  eventDesc->socket = connection->dataChannel.socket;
76  eventDesc->eventMask = SOCKET_EVENT_RX_READY;
77  }
78 #endif
79  }
80  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_RECEIVE)
81  {
82 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
83  //Any data pending in the receive buffer?
84  if(connection->dataChannel.tlsContext != NULL &&
85  tlsIsRxReady(connection->dataChannel.tlsContext))
86  {
87  //No need to poll the underlying socket for incoming traffic
88  eventDesc->eventFlags = SOCKET_EVENT_RX_READY;
89  }
90  else
91 #endif
92  {
93  //Wait for data to be available for reading
94  eventDesc->socket = connection->dataChannel.socket;
95  eventDesc->eventMask = SOCKET_EVENT_RX_READY;
96  }
97  }
98  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SEND)
99  {
100  //Wait until there is more room in the send buffer
101  eventDesc->socket = connection->dataChannel.socket;
102  eventDesc->eventMask = SOCKET_EVENT_TX_READY;
103  }
104  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SHUTDOWN_TLS)
105  {
106  //Wait until there is more room in the send buffer
107  eventDesc->socket = connection->dataChannel.socket;
108  eventDesc->eventMask = SOCKET_EVENT_TX_READY;
109  }
110  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_WAIT_ACK)
111  {
112  //Wait for all the data to be transmitted and acknowledged
113  eventDesc->socket = connection->dataChannel.socket;
114  eventDesc->eventMask = SOCKET_EVENT_TX_ACKED;
115  }
116  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SHUTDOWN_TX)
117  {
118  //Wait for the FIN to be acknowledged
119  eventDesc->socket = connection->dataChannel.socket;
120  eventDesc->eventMask = SOCKET_EVENT_TX_SHUTDOWN;
121  }
122  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SHUTDOWN_RX)
123  {
124  //Wait for a FIN to be received
125  eventDesc->socket = connection->dataChannel.socket;
126  eventDesc->eventMask = SOCKET_EVENT_RX_SHUTDOWN;
127  }
128 }
129 
130 
131 /**
132  * @brief Data connection event handler
133  * @param[in] connection Pointer to the client connection
134  * @param[in] eventFlags Event to be processed
135  **/
136 
138  uint_t eventFlags)
139 {
140  //Check current state
141  if(connection->dataChannel.state == FTP_CHANNEL_STATE_LISTEN)
142  {
143  //Accept data connection
144  ftpServerAcceptDataChannel(connection);
145  }
146 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
147  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_CONNECT_TLS)
148  {
149  error_t error;
150 
151  //Perform TLS handshake
152  error = ftpServerEstablishSecureChannel(&connection->dataChannel);
153 
154  //Check status code
155  if(error == NO_ERROR)
156  {
157  //Update the state of the data connection
158  if(connection->controlChannel.state == FTP_CHANNEL_STATE_LIST ||
159  connection->controlChannel.state == FTP_CHANNEL_STATE_NLST ||
160  connection->controlChannel.state == FTP_CHANNEL_STATE_RETR)
161  {
162  //Prepare to send data
163  connection->dataChannel.state = FTP_CHANNEL_STATE_SEND;
164  }
165  else if(connection->controlChannel.state == FTP_CHANNEL_STATE_STOR ||
166  connection->controlChannel.state == FTP_CHANNEL_STATE_APPE)
167  {
168  //Prepare to receive data
169  connection->dataChannel.state = FTP_CHANNEL_STATE_RECEIVE;
170  }
171  else
172  {
173  //Data transfer direction is unknown...
174  connection->dataChannel.state = FTP_CHANNEL_STATE_IDLE;
175  }
176  }
177  else if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
178  {
179  }
180  else
181  {
182  //Close the data connection
183  ftpServerCloseDataChannel(connection);
184 
185  //Release previously allocated resources
186  fsCloseFile(connection->file);
187  connection->file = NULL;
188 
189  //Back to idle state
190  connection->controlChannel.state = FTP_CHANNEL_STATE_IDLE;
191 
192  //Transfer status
193  osStrcpy(connection->response, "451 Transfer aborted\r\n");
194  //Debug message
195  TRACE_DEBUG("FTP server: %s", connection->response);
196 
197  //Number of bytes in the response buffer
198  connection->responseLen = osStrlen(connection->response);
199  connection->responsePos = 0;
200  }
201  }
202 #endif
203  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SEND)
204  {
205  //Send more data to the remote host
206  ftpServerWriteDataChannel(connection);
207  }
208  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_RECEIVE)
209  {
210  //Process incoming data
211  ftpServerReadDataChannel(connection);
212  }
213 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
214  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SHUTDOWN_TLS)
215  {
216  error_t error;
217 
218  //Gracefully close TLS session
219  error = tlsShutdown(connection->dataChannel.tlsContext);
220 
221  //Check status code
222  if(error != ERROR_WOULD_BLOCK && error != ERROR_TIMEOUT)
223  {
224  //Wait for all the data to be transmitted and acknowledged
225  connection->dataChannel.state = FTP_CHANNEL_STATE_WAIT_ACK;
226  }
227  }
228 #endif
229  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_WAIT_ACK)
230  {
231  //Disable transmission
232  socketShutdown(connection->dataChannel.socket, SOCKET_SD_SEND);
233  //Next state
234  connection->dataChannel.state = FTP_CHANNEL_STATE_SHUTDOWN_TX;
235  }
236  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SHUTDOWN_TX)
237  {
238  //Disable reception
239  socketShutdown(connection->dataChannel.socket, SOCKET_SD_RECEIVE);
240  //Next state
241  connection->dataChannel.state = FTP_CHANNEL_STATE_SHUTDOWN_RX;
242  }
243  else if(connection->dataChannel.state == FTP_CHANNEL_STATE_SHUTDOWN_RX)
244  {
245  //Close the data connection
246  ftpServerCloseDataChannel(connection);
247 
248  //Back to idle state
249  connection->controlChannel.state = FTP_CHANNEL_STATE_IDLE;
250 
251  //Transfer status
252  osStrcpy(connection->response, "226 Transfer complete\r\n");
253  //Debug message
254  TRACE_DEBUG("FTP server: %s", connection->response);
255 
256  //Number of bytes in the response buffer
257  connection->responseLen = osStrlen(connection->response);
258  connection->responsePos = 0;
259  }
260 }
261 
262 
263 /**
264  * @brief Open data connection
265  * @param[in] connection Pointer to the client connection
266  * @return Error code
267  **/
268 
270 {
271  error_t error;
272  FtpServerContext *context;
273 
274  //Point to the FTP server context
275  context = connection->context;
276 
277  //Release previously allocated resources
278  ftpServerCloseDataChannel(connection);
279 
280  //No port specified?
281  if(!connection->remotePort)
282  return ERROR_FAILURE;
283 
284  //Debug message
285  TRACE_INFO("FTP server: Opening data connection with client %s port %" PRIu16 "...\r\n",
286  ipAddrToString(&connection->remoteIpAddr, NULL), connection->remotePort);
287 
288  //Open data socket
289  connection->dataChannel.socket = socketOpen(SOCKET_TYPE_STREAM,
291  //Failed to open socket?
292  if(!connection->dataChannel.socket)
293  return ERROR_OPEN_FAILED;
294 
295  //Start of exception handling block
296  do
297  {
298  //Force the socket to operate in non-blocking mode
299  error = socketSetTimeout(connection->dataChannel.socket, 0);
300  //Any error to report?
301  if(error)
302  break;
303 
304  //Adjust the size of the TX buffer
305  error = socketSetTxBufferSize(connection->dataChannel.socket,
307  //Any error to report?
308  if(error)
309  break;
310 
311  //Adjust the size of the RX buffer
312  error = socketSetRxBufferSize(connection->dataChannel.socket,
314  //Any error to report?
315  if(error)
316  break;
317 
318  //Associate the socket with the relevant interface
319  error = socketBindToInterface(connection->dataChannel.socket,
320  connection->interface);
321  //Unable to bind the socket to the desired interface?
322  if(error)
323  break;
324 
325  //The server initiates the data connection from port 20
326  error = socketBind(connection->dataChannel.socket, &IP_ADDR_ANY,
327  context->settings.dataPort);
328  //Any error to report?
329  if(error)
330  break;
331 
332  //Establish data connection
333  error = socketConnect(connection->dataChannel.socket,
334  &connection->remoteIpAddr, connection->remotePort);
335  //Any error to report?
336  if(error != NO_ERROR && error != ERROR_TIMEOUT)
337  break;
338 
339  //Connection is being established
340  error = NO_ERROR;
341 
342  //End of exception handling block
343  } while(0);
344 
345  //Any error to report?
346  if(error)
347  {
348  //Clean up side effects
349  ftpServerCloseDataChannel(connection);
350  //Exit immediately
351  return error;
352  }
353 
354  //Successful processing
355  return NO_ERROR;
356 }
357 
358 
359 /**
360  * @brief Accept data connection
361  * @param[in] connection Pointer to the client connection
362  **/
363 
365 {
366  error_t error;
367  Socket *socket;
368  IpAddr clientIpAddr;
369  uint16_t clientPort;
370 
371  //Accept incoming connection
372  socket = socketAccept(connection->dataChannel.socket, &clientIpAddr,
373  &clientPort);
374  //Failure detected?
375  if(socket == NULL)
376  return;
377 
378  //Debug message
379  TRACE_INFO("FTP server: Data connection established with client %s port %" PRIu16 "...\r\n",
380  ipAddrToString(&clientIpAddr, NULL), clientPort);
381 
382  //Close the listening socket
383  socketClose(connection->dataChannel.socket);
384  //Save socket handle
385  connection->dataChannel.socket = socket;
386 
387  //Force the socket to operate in non-blocking mode
388  error = socketSetTimeout(connection->dataChannel.socket, 0);
389  //Any error to report?
390  if(error)
391  {
392  //Clean up side effects
393  socketClose(connection->dataChannel.socket);
394  //Exit immediately
395  return;
396  }
397 
398 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
399  //TLS-secured connection?
400  if(connection->controlChannel.tlsContext != NULL)
401  {
402  //Check data transfer direction
403  if(connection->controlChannel.state == FTP_CHANNEL_STATE_STOR ||
404  connection->controlChannel.state == FTP_CHANNEL_STATE_APPE)
405  {
406  //TLS initialization
407  error = ftpServerOpenSecureChannel(connection->context,
408  &connection->dataChannel, FTP_SERVER_TLS_TX_BUFFER_SIZE,
410  }
411  else
412  {
413  //TLS initialization
414  error = ftpServerOpenSecureChannel(connection->context,
415  &connection->dataChannel, FTP_SERVER_TLS_TX_BUFFER_SIZE,
417  }
418 
419  //Perform TLS handshake
420  connection->dataChannel.state = FTP_CHANNEL_STATE_CONNECT_TLS;
421  }
422  else
423 #endif
424  {
425  //Check current state
426  if(connection->controlChannel.state == FTP_CHANNEL_STATE_LIST ||
427  connection->controlChannel.state == FTP_CHANNEL_STATE_NLST ||
428  connection->controlChannel.state == FTP_CHANNEL_STATE_RETR)
429  {
430  //Prepare to send data
431  connection->dataChannel.state = FTP_CHANNEL_STATE_SEND;
432  }
433  else if(connection->controlChannel.state == FTP_CHANNEL_STATE_STOR ||
434  connection->controlChannel.state == FTP_CHANNEL_STATE_APPE)
435  {
436  //Prepare to receive data
437  connection->dataChannel.state = FTP_CHANNEL_STATE_RECEIVE;
438  }
439  else
440  {
441  //Data transfer direction is unknown...
442  connection->dataChannel.state = FTP_CHANNEL_STATE_IDLE;
443  }
444  }
445 }
446 
447 
448 /**
449  * @brief Write data to the data connection
450  * @param[in] connection Pointer to the client connection
451  **/
452 
454 {
455  error_t error;
456  size_t n;
457 
458  //Any data waiting for transmission?
459  if(connection->bufferLength > 0)
460  {
461  //Transmit data
462  error = ftpServerWriteChannel(&connection->dataChannel,
463  connection->buffer + connection->bufferPos,
464  connection->bufferLength, &n, 0);
465 
466  //Failed to send data?
467  if(error != NO_ERROR && error != ERROR_TIMEOUT)
468  {
469  //Close the data connection
470  ftpServerCloseDataChannel(connection);
471 
472  //Release previously allocated resources
473  if(connection->file != NULL)
474  {
475  fsCloseFile(connection->file);
476  connection->file = NULL;
477  }
478 
479  if(connection->dir != NULL)
480  {
481  fsCloseDir(connection->dir);
482  connection->dir = NULL;
483  }
484 
485  //Back to idle state
486  connection->controlChannel.state = FTP_CHANNEL_STATE_IDLE;
487 
488  //Transfer status
489  osStrcpy(connection->response, "451 Transfer aborted\r\n");
490  //Debug message
491  TRACE_DEBUG("FTP server: %s", connection->response);
492 
493  //Number of bytes in the response buffer
494  connection->responseLen = osStrlen(connection->response);
495  connection->responsePos = 0;
496 
497  //Exit immediately
498  return;
499  }
500 
501  //Advance data pointer
502  connection->bufferPos += n;
503  //Number of bytes still available in the buffer
504  connection->bufferLength -= n;
505  }
506 
507  //Empty transmission buffer?
508  if(connection->bufferLength == 0)
509  {
510  //File transfer in progress?
511  if(connection->controlChannel.state == FTP_CHANNEL_STATE_RETR)
512  {
513  //Read more data
514  error = fsReadFile(connection->file,
515  connection->buffer, FTP_SERVER_BUFFER_SIZE, &n);
516 
517  //End of stream?
518  if(error)
519  {
520  //Close file
521  fsCloseFile(connection->file);
522  connection->file = NULL;
523 
524 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
525  //TLS-secured connection?
526  if(connection->dataChannel.tlsContext != NULL)
527  {
528  //Gracefully close TLS session
529  connection->dataChannel.state = FTP_CHANNEL_STATE_SHUTDOWN_TLS;
530  }
531  else
532 #endif
533  {
534  //Wait for all the data to be transmitted and acknowledged
535  connection->dataChannel.state = FTP_CHANNEL_STATE_WAIT_ACK;
536  }
537 
538  //Exit immediately
539  return;
540  }
541  }
542  //Directory listing in progress?
543  else if(connection->controlChannel.state == FTP_CHANNEL_STATE_LIST ||
544  connection->controlChannel.state == FTP_CHANNEL_STATE_NLST)
545  {
546  uint_t perm;
547  char_t *path;
548  FsDirEntry dirEntry;
549 
550  //Read a new entry from the directory
551  error = fsReadDir(connection->dir, &dirEntry);
552 
553  //End of stream?
554  if(error)
555  {
556  //Close directory
557  fsCloseDir(connection->dir);
558  connection->dir = NULL;
559 
560 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
561  //TLS-secured connection?
562  if(connection->dataChannel.tlsContext != NULL)
563  {
564  //Gracefully close TLS session
565  connection->dataChannel.state = FTP_CHANNEL_STATE_SHUTDOWN_TLS;
566  }
567  else
568 #endif
569  {
570  //Wait for all the data to be transmitted and acknowledged
571  connection->dataChannel.state = FTP_CHANNEL_STATE_WAIT_ACK;
572  }
573 
574  //Exit immediately
575  return;
576  }
577 
578  //Point to the scratch buffer
579  path = connection->buffer;
580 
581  //Get the pathname of the directory being listed
582  osStrcpy(path, connection->path);
583  //Retrieve the full pathname
584  pathCombine(path, dirEntry.name, FTP_SERVER_MAX_PATH_LEN);
585  pathCanonicalize(path);
586 
587  //Get permissions for the specified file
588  perm = ftpServerGetFilePermissions(connection, path);
589 
590  //Enforce access rights
591  if((perm & FTP_FILE_PERM_LIST) != 0)
592  {
593  //LIST or NLST command?
594  if(connection->controlChannel.state == FTP_CHANNEL_STATE_LIST)
595  {
596  //Format the directory entry in UNIX-style format
597  n = ftpServerFormatDirEntry(&dirEntry, perm, connection->buffer);
598  }
599  else
600  {
601  //The server returns a stream of names of files and no other
602  //information (refer to RFC 959, section 4.1.3)
603  osStrcpy(connection->buffer, dirEntry.name);
604 
605  //Check whether the current entry is a directory
606  if((dirEntry.attributes & FS_FILE_ATTR_DIRECTORY) != 0)
607  {
608  osStrcat(connection->buffer, "/");
609  }
610 
611  //Terminate the name with a CRLF sequence
612  osStrcat(connection->buffer, "\r\n");
613  //Calculate the length of the resulting string
614  n = osStrlen(connection->buffer);
615  }
616 
617  //Debug message
618  TRACE_DEBUG("FTP server: %s", connection->buffer);
619  }
620  else
621  {
622  //Insufficient access rights
623  n = 0;
624  }
625  }
626  //Invalid state?
627  else
628  {
629  //The FTP server has encountered a critical error
630  ftpServerCloseConnection(connection);
631  //Exit immediately
632  return;
633  }
634 
635  //Number of bytes in the buffer
636  connection->bufferPos = 0;
637  connection->bufferLength = n;
638  }
639 }
640 
641 
642 /**
643  * @brief Read data from the data connection
644  * @param[in] connection Pointer to the client connection
645  **/
646 
648 {
649  error_t error;
650  bool_t eof;
651  size_t n;
652 
653  //File transfer in progress?
654  if(connection->controlChannel.state == FTP_CHANNEL_STATE_STOR ||
655  connection->controlChannel.state == FTP_CHANNEL_STATE_APPE)
656  {
657  //Receive data
658  error = ftpServerReadChannel(&connection->dataChannel,
659  connection->buffer + connection->bufferPos,
660  FTP_SERVER_BUFFER_SIZE - connection->bufferLength, &n, 0);
661 
662  //Check status code
663  if(error == NO_ERROR || error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
664  {
665  //Successful read operation
666  eof = FALSE;
667 
668  //Advance data pointer
669  connection->bufferPos += n;
670  connection->bufferLength += n;
671  }
672  else
673  {
674  //Cannot read more data
675  eof = TRUE;
676  }
677 
678  //Read data until the buffer is full or the end of the file is reached
679  if(eof || connection->bufferLength >= FTP_SERVER_BUFFER_SIZE)
680  {
681  //Any data to be written?
682  if(connection->bufferLength > 0)
683  {
684  //Write data to the specified file
685  error = fsWriteFile(connection->file,
686  connection->buffer, connection->bufferLength);
687 
688  //Any error to report?
689  if(error)
690  {
691  //Close the data connection
692  ftpServerCloseDataChannel(connection);
693 
694  //Release previously allocated resources
695  fsCloseFile(connection->file);
696  connection->file = NULL;
697 
698  //Back to idle state
699  connection->controlChannel.state = FTP_CHANNEL_STATE_IDLE;
700 
701  //Transfer status
702  osStrcpy(connection->response, "451 Transfer aborted\r\n");
703  //Debug message
704  TRACE_DEBUG("FTP server: %s", connection->response);
705 
706  //Number of bytes in the response buffer
707  connection->responseLen = osStrlen(connection->response);
708  connection->responsePos = 0;
709 
710  //Exit immediately
711  return;
712  }
713  }
714 
715  //Flush reception buffer
716  connection->bufferLength = 0;
717  connection->bufferPos = 0;
718  }
719 
720  //End of stream?
721  if(eof)
722  {
723  //Close file
724  fsCloseFile(connection->file);
725  connection->file = NULL;
726 
727 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
728  //TLS-secured connection?
729  if(connection->dataChannel.tlsContext != NULL)
730  {
731  //Gracefully close TLS session
732  connection->dataChannel.state = FTP_CHANNEL_STATE_SHUTDOWN_TLS;
733  }
734  else
735 #endif
736  {
737  //Wait for all the data to be transmitted and acknowledged
738  connection->dataChannel.state = FTP_CHANNEL_STATE_WAIT_ACK;
739  }
740  }
741  }
742  //Invalid state?
743  else
744  {
745  //The FTP server has encountered a critical error
746  ftpServerCloseConnection(connection);
747  }
748 }
749 
750 
751 /**
752  * @brief Close data connection
753  * @param[in] connection Pointer to the client connection
754  **/
755 
757 {
758  IpAddr clientIpAddr;
759  uint16_t clientPort;
760 
761  //Check whether the data connection is active
762  if(connection->dataChannel.socket != NULL)
763  {
764  //Retrieve the address of the peer to which a socket is connected
765  socketGetRemoteAddr(connection->dataChannel.socket, &clientIpAddr,
766  &clientPort);
767 
768  //Check whether the data connection is established
769  if(clientPort != 0)
770  {
771  //Debug message
772  TRACE_INFO("FTP server: Closing data connection with client %s port %"
773  PRIu16 "...\r\n", ipAddrToString(&clientIpAddr, NULL), clientPort);
774  }
775 
776 #if (FTP_SERVER_TLS_SUPPORT == ENABLED)
777  //Valid TLS context?
778  if(connection->dataChannel.tlsContext != NULL)
779  {
780  //Release TLS context
781  tlsFree(connection->dataChannel.tlsContext);
782  connection->dataChannel.tlsContext = NULL;
783  }
784 #endif
785 
786  //Valid socket?
787  if(connection->dataChannel.socket != NULL)
788  {
789  //Close data connection
790  socketClose(connection->dataChannel.socket);
791  connection->dataChannel.socket = NULL;
792  }
793 
794  //Re initialize data connection
795  connection->passiveMode = FALSE;
796  connection->remotePort = 0;
797 
798  //Mark the connection as closed
799  connection->dataChannel.state = FTP_CHANNEL_STATE_CLOSED;
800  }
801 }
802 
803 #endif
804 
#define FtpServerContext
Definition: ftp_server.h:208
@ FTP_CHANNEL_STATE_RECEIVE
Definition: ftp_server.h:231
Path manipulation helper functions.
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:1316
int bool_t
Definition: compiler_port.h:53
@ ERROR_WOULD_BLOCK
Definition: error.h:96
IP network address.
Definition: ip.h:90
@ FTP_CHANNEL_STATE_IDLE
Definition: ftp_server.h:229
FTP data connection.
size_t ftpServerFormatDirEntry(const FsDirEntry *dirEntry, uint_t perm, char_t *buffer)
Format a directory entry in UNIX-style format.
error_t ftpServerOpenDataChannel(FtpClientConnection *connection)
Open data connection.
#define TRUE
Definition: os_port.h:50
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2062
#define FTP_SERVER_MAX_PATH_LEN
Definition: ftp_server.h:130
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:805
Transport protocol abstraction layer.
@ FTP_CHANNEL_STATE_SHUTDOWN_TLS
Definition: ftp_server.h:242
#define osStrlen(s)
Definition: os_port.h:165
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t socketGetRemoteAddr(Socket *socket, IpAddr *remoteIpAddr, uint16_t *remotePort)
Retrieve the address of the peer to which a socket is connected.
Definition: socket.c:1985
Helper functions for FTP server.
Structure describing socket events.
Definition: socket.h:426
error_t socketSetTxBufferSize(Socket *socket, size_t size)
Specify the size of the TCP send buffer.
Definition: socket.c:1201
@ FTP_CHANNEL_STATE_CLOSED
Definition: ftp_server.h:226
error_t fsReadFile(FsFile *file, void *data, size_t size, size_t *length)
Read data from the specified file.
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ FS_FILE_ATTR_DIRECTORY
Definition: fs_port.h:61
const IpAddr IP_ADDR_ANY
Definition: ip.c:53
@ SOCKET_SD_SEND
Definition: socket.h:160
void pathCanonicalize(char_t *path)
Simplify a path.
Definition: path.c:158
@ FTP_CHANNEL_STATE_RETR
Definition: ftp_server.h:238
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2307
#define FALSE
Definition: os_port.h:46
@ FTP_CHANNEL_STATE_WAIT_ACK
Definition: ftp_server.h:243
void ftpServerReadDataChannel(FtpClientConnection *connection)
Read data from the data connection.
error_t socketSetRxBufferSize(Socket *socket, size_t size)
Specify the size of the TCP receive buffer.
Definition: socket.c:1237
@ FTP_CHANNEL_STATE_NLST
Definition: ftp_server.h:237
error_t
Error codes.
Definition: error.h:43
@ FTP_CHANNEL_STATE_LISTEN
Definition: ftp_server.h:228
void ftpServerCloseDataChannel(FtpClientConnection *connection)
Close data connection.
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
void fsCloseFile(FsFile *file)
Close a file.
@ SOCKET_EVENT_TX_READY
Definition: socket.h:175
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ FTP_CHANNEL_STATE_SHUTDOWN_TX
Definition: ftp_server.h:244
void ftpServerRegisterDataChannelEvents(FtpClientConnection *connection, SocketEventDesc *eventDesc)
Register data connection events.
error_t fsReadDir(FsDir *dir, FsDirEntry *dirEntry)
Read an entry from the specified directory stream.
#define FTP_SERVER_MAX_TLS_RX_BUFFER_SIZE
Definition: ftp_server.h:165
bool_t tlsIsTxReady(TlsContext *context)
Check whether some data is ready for transmission.
Definition: tls.c:2228
@ SOCKET_EVENT_RX_SHUTDOWN
Definition: socket.h:180
void ftpServerCloseConnection(FtpClientConnection *connection)
Close client connection properly.
error_t ftpServerEstablishSecureChannel(FtpServerChannel *channel)
Establish secure connection.
@ FTP_CHANNEL_STATE_SHUTDOWN_RX
Definition: ftp_server.h:245
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1349
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2020
#define TRACE_INFO(...)
Definition: debug.h:95
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
@ FTP_CHANNEL_STATE_APPE
Definition: ftp_server.h:240
error_t ftpServerOpenSecureChannel(FtpServerContext *context, FtpServerChannel *channel, size_t txBufferSize, size_t rxBufferSize)
Open secure connection.
uint_t eventFlags
Returned events.
Definition: socket.h:429
#define FTP_SERVER_BUFFER_SIZE
Definition: ftp_server.h:102
#define socketBindToInterface
Definition: net_legacy.h:193
@ FTP_FILE_PERM_LIST
Definition: ftp_server.h:279
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:1451
#define FTP_SERVER_TLS_TX_BUFFER_SIZE
Definition: ftp_server.h:151
bool_t tlsIsRxReady(TlsContext *context)
Check whether some data is available in the receive buffer.
Definition: tls.c:2262
char_t name[FS_MAX_NAME_LEN+1]
Definition: fs_port.h:112
#define TRACE_DEBUG(...)
Definition: debug.h:107
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:48
#define osStrcat(s1, s2)
Definition: os_port.h:219
@ SOCKET_EVENT_TX_ACKED
Definition: socket.h:177
@ FTP_CHANNEL_STATE_LIST
Definition: ftp_server.h:236
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
uint_t ftpServerGetFilePermissions(FtpClientConnection *connection, const char_t *path)
Get permissions for the specified file or directory.
uint8_t n
@ SOCKET_EVENT_TX_SHUTDOWN
Definition: socket.h:178
Directory entry.
Definition: fs_port.h:108
@ SOCKET_SD_RECEIVE
Definition: socket.h:159
uint32_t attributes
Definition: fs_port.h:109
#define Socket
Definition: socket.h:36
@ FTP_CHANNEL_STATE_SEND
Definition: ftp_server.h:230
error_t fsWriteFile(FsFile *file, void *data, size_t length)
Write data to the specified file.
error_t ftpServerReadChannel(FtpServerChannel *channel, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
FTP server (File Transfer Protocol)
#define FTP_SERVER_MIN_TLS_RX_BUFFER_SIZE
Definition: ftp_server.h:158
#define FtpClientConnection
Definition: ftp_server.h:212
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2469
@ FTP_CHANNEL_STATE_STOR
Definition: ftp_server.h:239
error_t ftpServerWriteChannel(FtpServerChannel *channel, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
void fsCloseDir(FsDir *dir)
Close a directory stream.
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:427
#define FTP_SERVER_MAX_TCP_BUFFER_SIZE
Definition: ftp_server.h:144
unsigned int uint_t
Definition: compiler_port.h:50
void ftpServerAcceptDataChannel(FtpClientConnection *connection)
Accept data connection.
#define osStrcpy(s1, s2)
Definition: os_port.h:207
@ 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:148
void ftpServerProcessDataChannelEvents(FtpClientConnection *connection, uint_t eventFlags)
Data connection event handler.
@ FTP_CHANNEL_STATE_CONNECT_TLS
Definition: ftp_server.h:227
uint_t eventMask
Requested events.
Definition: socket.h:428
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void pathCombine(char_t *path, const char_t *more, size_t maxLen)
Concatenate two paths.
Definition: path.c:394
void ftpServerWriteDataChannel(FtpClientConnection *connection)
Write data to the data connection.