mikroSDK Reference Manual
ftp_client.h
Go to the documentation of this file.
1
31#ifndef _FTP_CLIENT_H
32#define _FTP_CLIENT_H
33
34//Dependencies
35#include "core/net.h"
36#include "date_time.h"
37
38//FTP client support
39#ifndef FTP_CLIENT_SUPPORT
40 #define FTP_CLIENT_SUPPORT ENABLED
41#elif (FTP_CLIENT_SUPPORT != ENABLED && FTP_CLIENT_SUPPORT != DISABLED)
42 #error FTP_CLIENT_SUPPORT parameter is not valid
43#endif
44
45//FTP over TLS
46#ifndef FTP_CLIENT_TLS_SUPPORT
47 #define FTP_CLIENT_TLS_SUPPORT DISABLED
48#elif (FTP_CLIENT_TLS_SUPPORT != ENABLED && FTP_CLIENT_TLS_SUPPORT != DISABLED)
49 #error FTP_CLIENT_TLS_SUPPORT parameter is not valid
50#endif
51
52//Default timeout
53#ifndef FTP_CLIENT_DEFAULT_TIMEOUT
54 #define FTP_CLIENT_DEFAULT_TIMEOUT 20000
55#elif (FTP_CLIENT_DEFAULT_TIMEOUT < 1000)
56 #error FTP_CLIENT_DEFAULT_TIMEOUT parameter is not valid
57#endif
58
59//Size of the buffer for input/output operations
60#ifndef FTP_CLIENT_BUFFER_SIZE
61 #define FTP_CLIENT_BUFFER_SIZE 512
62#elif (FTP_CLIENT_BUFFER_SIZE < 64)
63 #error FTP_CLIENT_BUFFER_SIZE parameter is not valid
64#endif
65
66//Minimum buffer size for TCP sockets
67#ifndef FTP_CLIENT_MIN_TCP_BUFFER_SIZE
68 #define FTP_CLIENT_MIN_TCP_BUFFER_SIZE 1430
69#elif (FTP_CLIENT_MIN_TCP_BUFFER_SIZE < 512)
70 #error FTP_CLIENT_MIN_TCP_BUFFER_SIZE parameter is not valid
71#endif
72
73//Maximum buffer size for TCP sockets
74#ifndef FTP_CLIENT_MAX_TCP_BUFFER_SIZE
75 #define FTP_CLIENT_MAX_TCP_BUFFER_SIZE 2860
76#elif (FTP_CLIENT_MAX_TCP_BUFFER_SIZE < 512)
77 #error FTP_CLIENT_MAX_TCP_BUFFER_SIZE parameter is not valid
78#endif
79
80//TX buffer size for TLS connections
81#ifndef FTP_CLIENT_TLS_TX_BUFFER_SIZE
82 #define FTP_CLIENT_TLS_TX_BUFFER_SIZE 2048
83#elif (FTP_CLIENT_TLS_TX_BUFFER_SIZE < 512)
84 #error FTP_CLIENT_TLS_TX_BUFFER_SIZE parameter is not valid
85#endif
86
87//Minimum RX buffer size for TLS connections
88#ifndef FTP_CLIENT_MIN_TLS_RX_BUFFER_SIZE
89 #define FTP_CLIENT_MIN_TLS_RX_BUFFER_SIZE 4096
90#elif (FTP_CLIENT_MIN_TLS_RX_BUFFER_SIZE < 512)
91 #error FTP_CLIENT_MIN_TLS_RX_BUFFER_SIZE parameter is not valid
92#endif
93
94//Maximum RX buffer size for TLS connections
95#ifndef FTP_CLIENT_MAX_TLS_RX_BUFFER_SIZE
96 #define FTP_CLIENT_MAX_TLS_RX_BUFFER_SIZE 16384
97#elif (FTP_CLIENT_MAX_TLS_RX_BUFFER_SIZE < 512)
98 #error FTP_CLIENT_MAX_TLS_RX_BUFFER_SIZE parameter is not valid
99#endif
100
101//Maximum length of file names
102#ifndef FTP_CLIENT_MAX_FILENAME_LEN
103 #define FTP_CLIENT_MAX_FILENAME_LEN 64
104#elif (FTP_CLIENT_MAX_FILENAME_LEN < 16)
105 #error FTP_CLIENT_MAX_FILENAME_LEN parameter is not valid
106#endif
107
108//Application specific context
109#ifndef FTP_CLIENT_PRIVATE_CONTEXT
110 #define FTP_CLIENT_PRIVATE_CONTEXT
111#endif
112
113//TLS supported?
114#if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
115 #include "core/crypto.h"
116 #include "tls.h"
117#endif
118
119//Test macros for FTP response codes
120#define FTP_REPLY_CODE_1YZ(code) ((code) >= 100 && (code) < 200)
121#define FTP_REPLY_CODE_2YZ(code) ((code) >= 200 && (code) < 300)
122#define FTP_REPLY_CODE_3YZ(code) ((code) >= 300 && (code) < 400)
123#define FTP_REPLY_CODE_4YZ(code) ((code) >= 400 && (code) < 500)
124#define FTP_REPLY_CODE_5YZ(code) ((code) >= 500 && (code) < 600)
125
126//Forward declaration of FtpClientContext structure
127struct _FtpClientContext;
128#define FtpClientContext struct _FtpClientContext
129
130//C++ guard
131#ifdef __cplusplus
132extern "C" {
133#endif
134
135
140typedef enum
141{
142 FTP_MODE_PLAINTEXT = 0,
143 FTP_MODE_IMPLICIT_TLS = 1,
144 FTP_MODE_EXPLICIT_TLS = 2,
145 FTP_MODE_ACTIVE = 0,
146 FTP_MODE_PASSIVE = 4
148
149
154typedef enum
155{
156 FTP_FILE_MODE_READ = 0,
157 FTP_FILE_MODE_WRITE = 1,
158 FTP_FILE_MODE_APPEND = 2,
159 FTP_FILE_MODE_BINARY = 0,
160 FTP_FILE_MODE_TEXT = 4
162
163
168typedef enum
169{
170 FTP_FLAG_WAIT_ALL = 0x0800,
171 FTP_FLAG_BREAK_CHAR = 0x1000,
172 FTP_FLAG_BREAK_CRLF = 0x100A,
173 FTP_FLAG_NO_DELAY = 0x4000,
174 FTP_FLAG_DELAY = 0x8000
176
177
182typedef enum
183{
184 FTP_FILE_ATTR_DIRECTORY = 1,
185 FTP_FILE_ATTR_READ_ONLY = 2
187
188
193typedef enum
194{
195 FTP_CLIENT_STATE_DISCONNECTED = 0,
196 FTP_CLIENT_STATE_ACCEPTING_TCP = 1,
197 FTP_CLIENT_STATE_CONNECTING_TCP = 2,
198 FTP_CLIENT_STATE_CONNECTING_TLS = 3,
199 FTP_CLIENT_STATE_CONNECTED = 4,
200 FTP_CLIENT_STATE_SUB_COMMAND_1 = 5,
201 FTP_CLIENT_STATE_SUB_COMMAND_2 = 6,
202 FTP_CLIENT_STATE_SUB_COMMAND_3 = 7,
203 FTP_CLIENT_STATE_SUB_COMMAND_4 = 8,
204 FTP_CLIENT_STATE_SUB_COMMAND_5 = 9,
205 FTP_CLIENT_STATE_SUB_COMMAND_6 = 10,
206 FTP_CLIENT_STATE_SUB_COMMAND_7 = 11,
207 FTP_CLIENT_STATE_SUB_COMMAND_8 = 12,
208 FTP_CLIENT_STATE_SUB_COMMAND_9 = 13,
209 FTP_CLIENT_STATE_WRITING_DATA = 14,
210 FTP_CLIENT_STATE_READING_DATA = 15,
211 FTP_CLIENT_STATE_DISCONNECTING_1 = 16,
212 FTP_CLIENT_STATE_DISCONNECTING_2 = 17
214
215
216//TLS supported?
217#if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
218
223typedef error_t (*FtpClientTlsInitCallback)(FtpClientContext *context,
224 TlsContext *tlsContext);
225
226#endif
227
228
233typedef struct
234{
235 Socket *socket;
236#if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
237 TlsContext *tlsContext;
238#endif
240
241
247{
249 NetInterface *interface;
253 uint16_t serverPort;
254 bool_t passiveMode;
255#if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
256 TlsSessionState tlsSession;
258#endif
261 char_t buffer[FTP_CLIENT_BUFFER_SIZE];
262 size_t bufferPos;
263 size_t commandLen;
264 size_t replyLen;
265 uint_t replyCode;
266 FTP_CLIENT_PRIVATE_CONTEXT
267};
268
269
274typedef struct
275{
276 char_t name[FTP_CLIENT_MAX_FILENAME_LEN + 1];
277 uint32_t attributes;
278 uint32_t size;
279 DateTime modified;
281
282
283//FTP client related functions
284error_t ftpClientInit(FtpClientContext *context);
285
286#if (FTP_CLIENT_TLS_SUPPORT == ENABLED)
287
288error_t ftpClientRegisterTlsInitCallback(FtpClientContext *context,
289 FtpClientTlsInitCallback callback);
290
291#endif
292
293error_t ftpClientSetTimeout(FtpClientContext *context, systime_t timeout);
294
295error_t ftpClientBindToInterface(FtpClientContext *context,
296 NetInterface *interface);
297
298error_t ftpClientConnect(FtpClientContext *context,
299 const IpAddr *serverIpAddr, uint16_t serverPort, uint_t mode);
300
301error_t ftpClientLogin(FtpClientContext *context, const char_t *username,
302 const char_t *password);
303
304error_t ftpClientLoginEx(FtpClientContext *context, const char_t *username,
305 const char_t *password, const char_t *account);
306
307error_t ftpClientGetWorkingDir(FtpClientContext *context, char_t *path,
308 size_t maxLen);
309
310error_t ftpClientChangeWorkingDir(FtpClientContext *context,
311 const char_t *path);
312
313error_t ftpClientChangeToParentDir(FtpClientContext *context);
314
315error_t ftpClientOpenDir(FtpClientContext *context, const char_t *path);
316error_t ftpClientReadDir(FtpClientContext *context, FtpDirEntry *dirEntry);
317error_t ftpClientCloseDir(FtpClientContext *context);
318
319error_t ftpClientCreateDir(FtpClientContext *context, const char_t *path);
320error_t ftpClientDeleteDir(FtpClientContext *context, const char_t *path);
321
322error_t ftpClientOpenFile(FtpClientContext *context, const char_t *path,
323 uint_t mode);
324
325error_t ftpClientWriteFile(FtpClientContext *context, const void *data,
326 size_t length, size_t *written, uint_t flags);
327
328error_t ftpClientReadFile(FtpClientContext *context, void *data, size_t size,
329 size_t *received, uint_t flags);
330
331error_t ftpClientCloseFile(FtpClientContext *context);
332
333error_t ftpClientRenameFile(FtpClientContext *context, const char_t *oldPath,
334 const char_t *newPath);
335
336error_t ftpClientDeleteFile(FtpClientContext *context, const char_t *path);
337
338uint_t ftpClientGetReplyCode(FtpClientContext *context);
339
340error_t ftpClientDisconnect(FtpClientContext *context);
341error_t ftpClientClose(FtpClientContext *context);
342
343void ftpClientDeinit(FtpClientContext *context);
344
345//C++ guard
346#ifdef __cplusplus
347}
348#endif
349
350#endif
Date and time management.
error_t
Error codes.
Definition error.h:43
FtpClientState
FTP client states.
Definition ftp_client.h:194
FtpFileAttributes
File attributes.
Definition ftp_client.h:183
FtpFileFlags
Flags used by I/O functions.
Definition ftp_client.h:169
error_t(* FtpClientTlsInitCallback)(FtpClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition ftp_client.h:223
FtpConnectionModes
FTP connection modes.
Definition ftp_client.h:141
FtpFileModes
File access modes.
Definition ftp_client.h:155
TCP/IP stack core.
uint32_t systime_t
System time.
Definition os_port_none.h:90
Date and time representation.
Definition date_time.h:47
Control or data channel.
Definition ftp_client.h:234
Socket * socket
Underlying TCP socket.
Definition ftp_client.h:235
TlsContext * tlsContext
TLS context.
Definition ftp_client.h:237
Directory entry.
Definition ftp_client.h:275
IP network address.
Definition ip.h:72
FTP client context.
Definition ftp_client.h:247
uint_t replyCode
FTP reply code.
Definition ftp_client.h:265
size_t bufferPos
Current position in the buffer.
Definition ftp_client.h:262
NetInterface * interface
Underlying network interface.
Definition ftp_client.h:249
uint16_t serverPort
TCP port number.
Definition ftp_client.h:253
systime_t timeout
Timeout value.
Definition ftp_client.h:250
char_t buffer[FTP_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition ftp_client.h:261
FtpClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition ftp_client.h:257
IpAddr serverIpAddr
IP address of the FTP server.
Definition ftp_client.h:252
size_t commandLen
Length of the FTP command, in bytes.
Definition ftp_client.h:263
systime_t timestamp
Timestamp to manage timeout.
Definition ftp_client.h:251
TlsSessionState tlsSession
TLS session state.
Definition ftp_client.h:256
FtpClientState state
FTP client state.
Definition ftp_client.h:248
FtpClientChannel controlChannel
Control channel.
Definition ftp_client.h:259
FtpClientChannel dataChannel
Data channel.
Definition ftp_client.h:260
bool_t passiveMode
Passive mode.
Definition ftp_client.h:254
size_t replyLen
Length of the FTP reply, in bytes.
Definition ftp_client.h:264