mikroSDK Reference Manual
ftp_server.h
Go to the documentation of this file.
1
31#ifndef _FTP_SERVER_H
32#define _FTP_SERVER_H
33
34//Dependencies
35#include "core/net.h"
36#include "core/socket.h"
37#include "fs_port.h"
38
39//FTP server support
40#ifndef FTP_SERVER_SUPPORT
41 #define FTP_SERVER_SUPPORT ENABLED
42#elif (FTP_SERVER_SUPPORT != ENABLED && FTP_SERVER_SUPPORT != DISABLED)
43 #error FTP_SERVER_SUPPORT parameter is not valid
44#endif
45
46//FTP over TLS
47#ifndef FTP_SERVER_TLS_SUPPORT
48 #define FTP_SERVER_TLS_SUPPORT DISABLED
49#elif (FTP_SERVER_TLS_SUPPORT != ENABLED && FTP_SERVER_TLS_SUPPORT != DISABLED)
50 #error FTP_SERVER_TLS_SUPPORT parameter is not valid
51#endif
52
53//Stack size required to run the FTP server
54#ifndef FTP_SERVER_STACK_SIZE
55 #define FTP_SERVER_STACK_SIZE 650
56#elif (FTP_SERVER_STACK_SIZE < 1)
57 #error FTP_SERVER_STACK_SIZE parameter is not valid
58#endif
59
60//Priority at which the FTP server should run
61#ifndef FTP_SERVER_PRIORITY
62 #define FTP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
63#endif
64
65//Maximum number of simultaneous connections
66#ifndef FTP_SERVER_MAX_CONNECTIONS
67 #define FTP_SERVER_MAX_CONNECTIONS 10
68#elif (FTP_SERVER_MAX_CONNECTIONS < 1)
69 #error FTP_SERVER_MAX_CONNECTIONS parameter is not valid
70#endif
71
72//Maximum time the server will wait before closing the connection
73#ifndef FTP_SERVER_TIMEOUT
74 #define FTP_SERVER_TIMEOUT 60000
75#elif (FTP_SERVER_TIMEOUT < 1000)
76 #error FTP_SERVER_TIMEOUT parameter is not valid
77#endif
78
79//FTP server tick interval
80#ifndef FTP_SERVER_TICK_INTERVAL
81 #define FTP_SERVER_TICK_INTERVAL 1000
82#elif (FTP_SERVER_TICK_INTERVAL < 100)
83 #error FTP_SERVER_TICK_INTERVAL parameter is not valid
84#endif
85
86//Maximum length of the pending connection queue
87#ifndef FTP_SERVER_BACKLOG
88 #define FTP_SERVER_BACKLOG 4
89#elif (FTP_SERVER_BACKLOG < 1)
90 #error FTP_SERVER_BACKLOG parameter is not valid
91#endif
92
93//Maximum line length
94#ifndef FTP_SERVER_MAX_LINE_LEN
95 #define FTP_SERVER_MAX_LINE_LEN 255
96#elif (FTP_SERVER_MAX_LINE_LEN < 64)
97 #error FTP_SERVER_MAX_LINE_LEN parameter is not valid
98#endif
99
100//Size of buffer used for input/output operations
101#ifndef FTP_SERVER_BUFFER_SIZE
102 #define FTP_SERVER_BUFFER_SIZE 1024
103#elif (FTP_SERVER_BUFFER_SIZE < 128)
104 #error FTP_SERVER_BUFFER_SIZE parameter is not valid
105#endif
106
107//Maximum size of root directory
108#ifndef FTP_SERVER_MAX_ROOT_DIR_LEN
109 #define FTP_SERVER_MAX_ROOT_DIR_LEN 63
110#elif (FTP_SERVER_MAX_ROOT_DIR_LEN < 7)
111 #error FTP_SERVER_MAX_ROOT_DIR_LEN parameter is not valid
112#endif
113
114//Maximum size of home directory
115#ifndef FTP_SERVER_MAX_HOME_DIR_LEN
116 #define FTP_SERVER_MAX_HOME_DIR_LEN 63
117#elif (FTP_SERVER_MAX_HOME_DIR_LEN < 7)
118 #error FTP_SERVER_MAX_HOME_DIR_LEN parameter is not valid
119#endif
120
121//Maximum user name length
122#ifndef FTP_SERVER_MAX_USERNAME_LEN
123 #define FTP_SERVER_MAX_USERNAME_LEN 63
124#elif (FTP_SERVER_MAX_USERNAME_LEN < 7)
125 #error FTP_SERVER_MAX_USERNAME_LEN parameter is not valid
126#endif
127
128//Maximum path length
129#ifndef FTP_SERVER_MAX_PATH_LEN
130 #define FTP_SERVER_MAX_PATH_LEN 255
131#elif (FTP_SERVER_MAX_PATH_LEN < 7)
132 #error FTP_SERVER_MAX_PATH_LEN parameter is not valid
133#endif
134
135//Minimum buffer size for TCP sockets
136#ifndef FTP_SERVER_MIN_TCP_BUFFER_SIZE
137 #define FTP_SERVER_MIN_TCP_BUFFER_SIZE 1430
138#elif (FTP_SERVER_MIN_TCP_BUFFER_SIZE < 512)
139 #error FTP_SERVER_MIN_TCP_BUFFER_SIZE parameter is not valid
140#endif
141
142//Maximum buffer size for TCP sockets
143#ifndef FTP_SERVER_MAX_TCP_BUFFER_SIZE
144 #define FTP_SERVER_MAX_TCP_BUFFER_SIZE 2860
145#elif (FTP_SERVER_MAX_TCP_BUFFER_SIZE < 512)
146 #error FTP_SERVER_MAX_TCP_BUFFER_SIZE parameter is not valid
147#endif
148
149//TX buffer size for TLS connections
150#ifndef FTP_SERVER_TLS_TX_BUFFER_SIZE
151 #define FTP_SERVER_TLS_TX_BUFFER_SIZE 4096
152#elif (FTP_SERVER_TLS_TX_BUFFER_SIZE < 512)
153 #error FTP_SERVER_TLS_TX_BUFFER_SIZE parameter is not valid
154#endif
155
156//Minimum RX buffer size for TLS connections
157#ifndef FTP_SERVER_MIN_TLS_RX_BUFFER_SIZE
158 #define FTP_SERVER_MIN_TLS_RX_BUFFER_SIZE 2048
159#elif (FTP_SERVER_MIN_TLS_RX_BUFFER_SIZE < 512)
160 #error FTP_SERVER_MIN_TLS_RX_BUFFER_SIZE parameter is not valid
161#endif
162
163//Maximum RX buffer size for TLS connections
164#ifndef FTP_SERVER_MAX_TLS_RX_BUFFER_SIZE
165 #define FTP_SERVER_MAX_TLS_RX_BUFFER_SIZE 16384
166#elif (FTP_SERVER_MAX_TLS_RX_BUFFER_SIZE < 512)
167 #error FTP_SERVER_MAX_TLS_RX_BUFFER_SIZE parameter is not valid
168#endif
169
170//Passive port range (lower limit)
171#ifndef FTP_SERVER_PASSIVE_PORT_MIN
172 #define FTP_SERVER_PASSIVE_PORT_MIN 48128
173#elif (FTP_SERVER_PASSIVE_PORT_MIN < 1024)
174 #error FTP_SERVER_PASSIVE_PORT_MIN parameter is not valid
175#endif
176
177//Passive port range (upper limit)
178#ifndef FTP_SERVER_PASSIVE_PORT_MAX
179 #define FTP_SERVER_PASSIVE_PORT_MAX 49151
180#elif (FTP_SERVER_PASSIVE_PORT_MAX <= FTP_SERVER_PASSIVE_PORT_MIN || FTP_SERVER_PASSIVE_PORT_MAX > 65535)
181 #error FTP_SERVER_PASSIVE_PORT_MAX parameter is not valid
182#endif
183
184//Application specific context
185#ifndef FTP_SERVER_PRIVATE_CONTEXT
186 #define FTP_SERVER_PRIVATE_CONTEXT
187#endif
188
189//TLS supported?
190#if (FTP_SERVER_TLS_SUPPORT == ENABLED)
191 #include "core/crypto.h"
192 #include "tls.h"
193 #include "tls_ticket.h"
194#endif
195
196//FTP port number
197#define FTP_PORT 21
198//FTP data port number
199#define FTP_DATA_PORT 20
200
201//FTPS port number (implicit mode)
202#define FTPS_PORT 990
203//FTPS data port number (implicit mode)
204#define FTPS_DATA_PORT 989
205
206//Forward declaration of FtpServerContext structure
207struct _FtpServerContext;
208#define FtpServerContext struct _FtpServerContext
209
210//Forward declaration of FtpClientConnection structure
212#define FtpClientConnection struct _FtpClientConnection
213
214//C++ guard
215#ifdef __cplusplus
216extern "C" {
217#endif
218
219
224typedef enum
225{
226 FTP_CHANNEL_STATE_CLOSED = 0,
227 FTP_CHANNEL_STATE_CONNECT_TLS = 1,
228 FTP_CHANNEL_STATE_LISTEN = 2,
229 FTP_CHANNEL_STATE_IDLE = 3,
230 FTP_CHANNEL_STATE_SEND = 4,
231 FTP_CHANNEL_STATE_RECEIVE = 5,
232 FTP_CHANNEL_STATE_DISCARD = 6,
233 FTP_CHANNEL_STATE_AUTH_TLS_1 = 7,
234 FTP_CHANNEL_STATE_AUTH_TLS_2 = 8,
235 FTP_CHANNEL_STATE_USER = 9,
236 FTP_CHANNEL_STATE_LIST = 10,
237 FTP_CHANNEL_STATE_NLST = 11,
238 FTP_CHANNEL_STATE_RETR = 12,
239 FTP_CHANNEL_STATE_STOR = 13,
240 FTP_CHANNEL_STATE_APPE = 14,
241 FTP_CHANNEL_STATE_RNFR = 15,
242 FTP_CHANNEL_STATE_SHUTDOWN_TLS = 16,
243 FTP_CHANNEL_STATE_WAIT_ACK = 17,
244 FTP_CHANNEL_STATE_SHUTDOWN_TX = 18,
245 FTP_CHANNEL_STATE_SHUTDOWN_RX = 19
247
248
253typedef enum
254{
255 FTP_SERVER_MODE_PLAINTEXT = 1,
256 FTP_SERVER_MODE_IMPLICIT_TLS = 2,
257 FTP_SERVER_MODE_EXPLICIT_TLS = 4
259
260
265typedef enum
266{
267 FTP_ACCESS_DENIED = 0,
268 FTP_ACCESS_ALLOWED = 1,
269 FTP_PASSWORD_REQUIRED = 2
271
272
277typedef enum
278{
279 FTP_FILE_PERM_LIST = 0x01,
280 FTP_FILE_PERM_READ = 0x02,
281 FTP_FILE_PERM_WRITE = 0x04
283
284
289typedef error_t (*FtpServerConnectCallback)(FtpClientConnection *connection,
290 const IpAddr *clientIpAddr, uint16_t clientPort);
291
292
297typedef void (*FtpServerDisconnectCallback)(FtpClientConnection *connection,
298 const IpAddr *clientIpAddr, uint16_t clientPort);
299
300
301//TLS supported?
302#if (FTP_SERVER_TLS_SUPPORT == ENABLED)
303
308typedef error_t (*FtpServerTlsInitCallback)(FtpClientConnection *connection,
309 TlsContext *tlsContext);
310
311#endif
312
313
318typedef uint_t (*FtpServerCheckUserCallback)(FtpClientConnection *connection,
319 const char_t *user);
320
321
326typedef uint_t (*FtpServerCheckPasswordCallback)(FtpClientConnection *connection,
327 const char_t *user, const char_t *password);
328
329
334typedef uint_t (*FtpServerGetFilePermCallback)(FtpClientConnection *connection,
335 const char_t *user, const char_t *path);
336
337
342typedef error_t (*FtpServerUnknownCommandCallback)(FtpClientConnection *connection,
343 const char_t *command, const char_t *param);
344
345
372
373
378typedef struct
379{
381 Socket *socket;
382#if (FTP_SERVER_TLS_SUPPORT == ENABLED)
383 TlsContext *tlsContext;
384#endif
386
387
393{
394 FtpServerContext *context;
395 NetInterface *interface;
402 bool_t passiveMode;
404 uint16_t remotePort;
405 char_t user[FTP_SERVER_MAX_USERNAME_LEN + 1];
406 char_t homeDir[FTP_SERVER_MAX_HOME_DIR_LEN + 1];
407 char_t currentDir[FTP_SERVER_MAX_PATH_LEN + 1];
408 char_t path[FTP_SERVER_MAX_PATH_LEN + 1];
409 char_t command[FTP_SERVER_MAX_LINE_LEN + 1];
410 size_t commandLen;
411 char_t response[FTP_SERVER_MAX_LINE_LEN + 1];
412 size_t responseLen;
413 size_t responsePos;
414 char_t buffer[FTP_SERVER_BUFFER_SIZE];
416 size_t bufferPos;
417};
418
419
425{
427 bool_t running;
428 bool_t stop;
431#if (OS_STATIC_TASK_SUPPORT == ENABLED)
432 OsTaskTcb taskTcb;
433 OsStackType taskStack[FTP_SERVER_STACK_SIZE];
434#endif
435 Socket *socket;
436 uint16_t passivePort;
437 FtpClientConnection *connections;
438 SocketEventDesc eventDesc[2 * FTP_SERVER_MAX_CONNECTIONS + 1];
439#if (FTP_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
440 TlsTicketContext tlsTicketContext;
441#endif
442 FTP_SERVER_PRIVATE_CONTEXT
443};
444
445
446//FTP server related functions
447void ftpServerGetDefaultSettings(FtpServerSettings *settings);
448
449error_t ftpServerInit(FtpServerContext *context,
450 const FtpServerSettings *settings);
451
452error_t ftpServerStart(FtpServerContext *context);
453error_t ftpServerStop(FtpServerContext *context);
454
455error_t ftpServerSetHomeDir(FtpClientConnection *connection,
456 const char_t *homeDir);
457
458void ftpServerTask(FtpServerContext *context);
459
460void ftpServerDeinit(FtpServerContext *context);
461
462//C++ guard
463#ifdef __cplusplus
464}
465#endif
466
467#endif
error_t
Error codes.
Definition error.h:43
File system abstraction layer.
void FsFile
File descriptor.
Definition fs_port_fatfs.h:60
error_t(* FtpServerUnknownCommandCallback)(FtpClientConnection *connection, const char_t *command, const char_t *param)
Unknown command callback function.
Definition ftp_server.h:342
error_t(* FtpServerConnectCallback)(FtpClientConnection *connection, const IpAddr *clientIpAddr, uint16_t clientPort)
Connection callback function.
Definition ftp_server.h:289
uint_t(* FtpServerGetFilePermCallback)(FtpClientConnection *connection, const char_t *user, const char_t *path)
Callback used to retrieve file permissions.
Definition ftp_server.h:334
FtpAccessStatus
FTP server access status.
Definition ftp_server.h:266
void(* FtpServerDisconnectCallback)(FtpClientConnection *connection, const IpAddr *clientIpAddr, uint16_t clientPort)
Disconnection callback function.
Definition ftp_server.h:297
FtpServerMode
Security modes.
Definition ftp_server.h:254
error_t(* FtpServerTlsInitCallback)(FtpClientConnection *connection, TlsContext *tlsContext)
TLS initialization callback function.
Definition ftp_server.h:308
FtpServerChannelState
Channel state.
Definition ftp_server.h:225
FtpFilePerm
File permissions.
Definition ftp_server.h:278
uint_t(* FtpServerCheckPasswordCallback)(FtpClientConnection *connection, const char_t *user, const char_t *password)
Password verification callback function.
Definition ftp_server.h:326
uint_t(* FtpServerCheckUserCallback)(FtpClientConnection *connection, const char_t *user)
User verification callback function.
Definition ftp_server.h:318
uint32_t Ipv4Addr
IPv4 network address.
Definition ipv4.h:267
TCP/IP stack core.
uint_t OsEvent
Event object.
Definition os_port_none.h:104
uint_t OsTaskId
Task identifier.
Definition os_port_none.h:97
uint32_t systime_t
System time.
Definition os_port_none.h:90
Socket API.
Directory descriptor.
Definition fs_port_posix.h:60
Control or data channel.
Definition ftp_server.h:379
TlsContext * tlsContext
TLS context.
Definition ftp_server.h:383
Socket * socket
Underlying TCP socket.
Definition ftp_server.h:381
FtpServerChannelState state
Channel state.
Definition ftp_server.h:380
FTP server settings.
Definition ftp_server.h:351
uint16_t port
FTP command port number.
Definition ftp_server.h:353
uint_t mode
Security modes.
Definition ftp_server.h:358
FtpServerGetFilePermCallback getFilePermCallback
Callback used to retrieve file permissions.
Definition ftp_server.h:369
FtpServerConnectCallback connectCallback
Connection callback function.
Definition ftp_server.h:362
FtpServerDisconnectCallback disconnectCallback
Disconnection callback function.
Definition ftp_server.h:363
uint16_t passivePortMax
Passive port range (upper value)
Definition ftp_server.h:356
FtpServerCheckPasswordCallback checkPasswordCallback
Password verification callback function.
Definition ftp_server.h:368
uint16_t passivePortMin
Passive port range (lower value)
Definition ftp_server.h:355
FtpServerUnknownCommandCallback unknownCommandCallback
Unknown command callback function.
Definition ftp_server.h:370
FtpServerTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition ftp_server.h:365
FtpServerCheckUserCallback checkUserCallback
User verification callback function.
Definition ftp_server.h:367
Ipv4Addr publicIpv4Addr
Public IPv4 address to be used in PASV replies.
Definition ftp_server.h:357
FtpClientConnection * connections
Client connections.
Definition ftp_server.h:360
uint16_t dataPort
FTP data port number.
Definition ftp_server.h:354
uint_t maxConnections
Maximum number of client connections.
Definition ftp_server.h:359
NetInterface * interface
Underlying network interface.
Definition ftp_server.h:352
IP network address.
Definition ip.h:72
Structure describing socket events.
Definition socket.h:395
FTP client connection.
Definition ftp_server.h:393
char_t path[FTP_SERVER_MAX_PATH_LEN+1]
Pathname.
Definition ftp_server.h:408
bool_t passiveMode
Passive data transfer.
Definition ftp_server.h:402
NetInterface * interface
Underlying network interface.
Definition ftp_server.h:395
FtpServerChannel controlChannel
Control channel.
Definition ftp_server.h:398
FtpServerContext * context
FTP server context.
Definition ftp_server.h:394
char_t homeDir[FTP_SERVER_MAX_HOME_DIR_LEN+1]
Home directory.
Definition ftp_server.h:406
char_t command[FTP_SERVER_MAX_LINE_LEN+1]
Incoming command.
Definition ftp_server.h:409
size_t commandLen
Number of bytes available in the command buffer.
Definition ftp_server.h:410
FsDir * dir
Directory pointer.
Definition ftp_server.h:401
bool_t userLoggedIn
This flag tells whether the user is logged in.
Definition ftp_server.h:396
char_t user[FTP_SERVER_MAX_USERNAME_LEN+1]
User name.
Definition ftp_server.h:405
FtpServerChannel dataChannel
Data channel.
Definition ftp_server.h:399
IpAddr remoteIpAddr
Remote IP address.
Definition ftp_server.h:403
char_t currentDir[FTP_SERVER_MAX_PATH_LEN+1]
Current directory.
Definition ftp_server.h:407
systime_t timestamp
Time stamp to manage timeout.
Definition ftp_server.h:397
size_t responsePos
Current position in the response buffer.
Definition ftp_server.h:413
char_t buffer[FTP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition ftp_server.h:414
char_t response[FTP_SERVER_MAX_LINE_LEN+1]
Response buffer.
Definition ftp_server.h:411
size_t responseLen
Number of bytes available in the response buffer.
Definition ftp_server.h:412
uint16_t remotePort
Remote port number.
Definition ftp_server.h:404
size_t bufferLength
Length of the buffer, in bytes.
Definition ftp_server.h:415
size_t bufferPos
Current position in the buffer.
Definition ftp_server.h:416
FsFile * file
File pointer.
Definition ftp_server.h:400
FTP server context.
Definition ftp_server.h:425
bool_t stop
Stop request.
Definition ftp_server.h:428
Socket * socket
Listening socket.
Definition ftp_server.h:435
OsStackType taskStack[FTP_SERVER_STACK_SIZE]
Task stack.
Definition ftp_server.h:433
OsTaskTcb taskTcb
Task control block.
Definition ftp_server.h:432
FtpServerSettings settings
User settings.
Definition ftp_server.h:426
bool_t running
Operational state of the FTP server.
Definition ftp_server.h:427
OsTaskId taskId
Task identifier.
Definition ftp_server.h:430
FtpClientConnection * connections
Client connections.
Definition ftp_server.h:437
SocketEventDesc eventDesc[2 *FTP_SERVER_MAX_CONNECTIONS+1]
The events the application is interested in.
Definition ftp_server.h:438
OsEvent event
Event object used to poll the sockets.
Definition ftp_server.h:429
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
Definition ftp_server.h:440
uint16_t passivePort
Current passive port number.
Definition ftp_server.h:436