VirtualBox

source: vbox/trunk/include/iprt/ftp.h@ 82756

Last change on this file since 82756 was 82733, checked in by vboxsync, 5 years ago

IPRT/FTP: Implemented FEAT command for reporting additional server features. Needed for the SIZE command. bugref:9646

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.4 KB
Line 
1/* $Id: ftp.h 82733 2020-01-14 12:20:17Z vboxsync $ */
2/** @file
3 * Header file for FTP client / server implementations.
4 */
5
6/*
7 * Copyright (C) 2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef IPRT_INCLUDED_ftp_h
28#define IPRT_INCLUDED_ftp_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33#include <iprt/types.h>
34#include <iprt/fs.h>
35
36RT_C_DECLS_BEGIN
37
38/** @defgroup grp_rt_ftp RTFtp - FTP server and client.
39 * @ingroup grp_rt
40 * @{
41 */
42
43/** @defgroup grp_rt_ftpserver RTFtpServer - FTP server implementation.
44 * @{
45 */
46
47/** @todo the following three definitions may move the iprt/types.h later. */
48/** FTP server handle. */
49typedef R3PTRTYPE(struct RTFTPSERVERINTERNAL *) RTFTPSERVER;
50/** Pointer to a FTP server handle. */
51typedef RTFTPSERVER *PRTFTPSERVER;
52/** Nil FTP client handle. */
53#define NIL_RTFTPSERVER ((RTFTPSERVER)0)
54
55/** Maximum length (in characters) a command can have (without parameters). */
56#define RTFTPSERVER_MAX_CMD_LEN 64
57
58/**
59 * Enumeration for defining the current server connection mode.
60 */
61typedef enum RTFTPSERVER_CONNECTION_MODE
62{
63 /** Normal mode, nothing to transfer. */
64 RTFTPSERVER_CONNECTION_MODE_NORMAL = 0,
65 /** Server is in passive mode (is listening). */
66 RTFTPSERVER_CONNECTION_MODE_PASSIVE,
67 /** Server connects via port to the client. */
68 RTFTPSERVER_CONNECTION_MODE_MODE_PORT,
69 /** The usual 32-bit hack. */
70 RTFTPSERVER_CONNECTION_MODE_32BIT_HACK = 0x7fffffff
71} RTFTPSERVER_CONNECTION_MODE;
72
73/**
74 * Enumeration for defining the data transfer mode.
75 */
76typedef enum RTFTPSERVER_TRANSFER_MODE
77{
78 /** Default if nothing else is set. */
79 RTFTPSERVER_TRANSFER_MODE_STREAM = 0,
80 RTFTPSERVER_TRANSFER_MODE_BLOCK,
81 RTFTPSERVER_TRANSFER_MODE_COMPRESSED,
82 /** The usual 32-bit hack. */
83 RTFTPSERVER_DATA_MODE_32BIT_HACK = 0x7fffffff
84} RTFTPSERVER_DATA_MODE;
85
86/**
87 * Enumeration for defining the data type.
88 */
89typedef enum RTFTPSERVER_DATA_TYPE
90{
91 /** Default if nothing else is set. */
92 RTFTPSERVER_DATA_TYPE_ASCII = 0,
93 RTFTPSERVER_DATA_TYPE_EBCDIC,
94 RTFTPSERVER_DATA_TYPE_IMAGE,
95 RTFTPSERVER_DATA_TYPE_LOCAL,
96 /** The usual 32-bit hack. */
97 RTFTPSERVER_DATA_TYPE_32BIT_HACK = 0x7fffffff
98} RTFTPSERVER_DATA_TYPE;
99
100/**
101 * Enumeration for defining the struct type.
102 */
103typedef enum RTFTPSERVER_STRUCT_TYPE
104{
105 /** Default if nothing else is set. */
106 RTFTPSERVER_STRUCT_TYPE_FILE = 0,
107 RTFTPSERVER_STRUCT_TYPE_RECORD,
108 RTFTPSERVER_STRUCT_TYPE_PAGE,
109 /** The usual 32-bit hack. */
110 RTFTPSERVER_STRUCT_TYPE_32BIT_HACK = 0x7fffffff
111} RTFTPSERVER_STRUCT_TYPE;
112
113/**
114 * Enumeration for FTP server reply codes.
115 *
116 ** @todo Might needs more codes, not complete yet.
117 */
118typedef enum RTFTPSERVER_REPLY
119{
120 /** Invalid reply type, do not use. */
121 RTFTPSERVER_REPLY_INVALID = 0,
122 /** Command okay. */
123 RTFTPSERVER_REPLY_FILE_STATUS_OKAY = 150,
124 /** Command okay. */
125 RTFTPSERVER_REPLY_OKAY = 200,
126 /** Command not implemented, superfluous at this site. */
127 RTFTPSERVER_REPLY_ERROR_CMD_NOT_IMPL_SUPERFLUOUS = 202,
128 /** System status report. */
129 RTFTPSERVER_REPLY_SYSTEM_STATUS = 211,
130 /** Service ready for new user. */
131 RTFTPSERVER_REPLY_READY_FOR_NEW_USER = 220,
132 /** Service is closing control connection. */
133 RTFTPSERVER_REPLY_CLOSING_CTRL_CONN = 221,
134 /** Closing data connection. */
135 RTFTPSERVER_REPLY_CLOSING_DATA_CONN = 226,
136 /** User logged in, proceed. */
137 RTFTPSERVER_REPLY_LOGGED_IN_PROCEED = 230,
138 /** User name okay, need password. */
139 RTFTPSERVER_REPLY_USERNAME_OKAY_NEED_PASSWORD = 331,
140 /** Can't open data connection. */
141 RTFTPSERVER_REPLY_CANT_OPEN_DATA_CONN = 425,
142 /** Connection closed; transfer aborted. */
143 RTFTPSERVER_REPLY_CONN_CLOSED_TRANSFER_ABORTED = 426,
144 /** Syntax error, command unrecognized. */
145 RTFTPSERVER_REPLY_ERROR_CMD_NOT_RECOGNIZED = 500,
146 /** Syntax error in parameters or arguments. */
147 RTFTPSERVER_REPLY_ERROR_INVALID_PARAMETERS = 501,
148 /** Command not implemented. */
149 RTFTPSERVER_REPLY_ERROR_CMD_NOT_IMPL = 502,
150 /** Bad sequence of commands. */
151 RTFTPSERVER_REPLY_ERROR_BAD_SEQUENCE = 503,
152 /** Command not implemented for that parameter. */
153 RTFTPSERVER_REPLY_ERROR_CMD_NOT_IMPL_PARAM = 504,
154 /** Not logged in. */
155 RTFTPSERVER_REPLY_NOT_LOGGED_IN = 530,
156 /** Requested action not taken. */
157 RTFTPSERVER_REPLY_REQ_ACTION_NOT_TAKEN = 550,
158 /** The usual 32-bit hack. */
159 RTFTPSERVER_REPLY_32BIT_HACK = 0x7fffffff
160} RTFTPSERVER_REPLY;
161
162/**
163 * Structure for maintaining a FTP server client state.
164 */
165typedef struct RTFTPSERVERCLIENTSTATE
166{
167 /** User name. */
168 char *pszUser;
169 /** Number of failed login attempts. */
170 uint8_t cFailedLoginAttempts;
171 /** Timestamp (in ms) of last command issued by the client. */
172 uint64_t tsLastCmdMs;
173 /** Current set data type. */
174 RTFTPSERVER_DATA_TYPE enmDataType;
175 /** Current set struct type. */
176 RTFTPSERVER_STRUCT_TYPE enmStructType;
177} RTFTPSERVERCLIENTSTATE;
178/** Pointer to a FTP server client state. */
179typedef RTFTPSERVERCLIENTSTATE *PRTFTPSERVERCLIENTSTATE;
180
181/**
182 * Structure for storing FTP server callback data.
183 */
184typedef struct RTFTPCALLBACKDATA
185{
186 /** Pointer to the client state. */
187 PRTFTPSERVERCLIENTSTATE pClient;
188 /** Saved user pointer. */
189 void *pvUser;
190 /** Size (in bytes) of data at user pointer. */
191 size_t cbUser;
192} RTFTPCALLBACKDATA;
193/** Pointer to FTP server callback data. */
194typedef RTFTPCALLBACKDATA *PRTFTPCALLBACKDATA;
195
196/**
197 * Function callback table for the FTP server implementation.
198 *
199 * All callbacks are optional and therefore can be NULL.
200 */
201typedef struct RTFTPSERVERCALLBACKS
202{
203 /**
204 * Callback which gets invoked when a user connected.
205 *
206 * @returns VBox status code.
207 * @param pData Pointer to generic callback data.
208 * @param pcszUser User name.
209 */
210 DECLCALLBACKMEMBER(int, pfnOnUserConnect)(PRTFTPCALLBACKDATA pData, const char *pcszUser);
211 /**
212 * Callback which gets invoked when a user tries to authenticate with a password.
213 *
214 * @returns VBox status code.
215 * @param pData Pointer to generic callback data.
216 * @param pcszUser User name to authenticate.
217 * @param pcszPassword Password to authenticate with.
218 */
219 DECLCALLBACKMEMBER(int, pfnOnUserAuthenticate)(PRTFTPCALLBACKDATA pData, const char *pcszUser, const char *pcszPassword);
220 /**
221 * Callback which gets invoked when a user disconnected.
222 *
223 * @returns VBox status code.
224 * @param pData Pointer to generic callback data.
225 * @param pcszUser User name which disconnected.
226 */
227 DECLCALLBACKMEMBER(int, pfnOnUserDisconnect)(PRTFTPCALLBACKDATA pData, const char *pcszUser);
228 /**
229 * Callback which gets invoked when the client wants to start reading or writing a file.
230 *
231 * @returns VBox status code.
232 * @param pData Pointer to generic callback data.
233 * @param pcsszPath Path of file to handle.
234 * @param fMode File mode to use (IPRT stlye).
235 * @param ppvHandle Opaque file handle only known to the callback implementation.
236 */
237 DECLCALLBACKMEMBER(int, pfnOnFileOpen)(PRTFTPCALLBACKDATA pData, const char *pcszPath, uint32_t fMode, void **ppvHandle);
238 /**
239 * Callback which gets invoked when the client wants to read from a file.
240 *
241 * @returns VBox status code.
242 * @param pData Pointer to generic callback data.
243 * @param pvHandle Opaque file handle only known to the callback implementation.
244 * @param pvBuf Where to store the read file data.
245 * @param cbToRead How much (in bytes) to read. Must at least supply the size of pvBuf.
246 * @param pcbRead How much (in bytes) was read. Optional.
247 */
248 DECLCALLBACKMEMBER(int, pfnOnFileRead)(PRTFTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbToRead, size_t *pcbRead);
249 /**
250 * Callback which gets invoked when the client is done reading from or writing to a file.
251 *
252 * @returns VBox status code.
253 * @param pData Pointer to generic callback data.
254 * @param ppvHandle Opaque file handle only known to the callback implementation.
255 */
256 DECLCALLBACKMEMBER(int, pfnOnFileClose)(PRTFTPCALLBACKDATA pData, void *pvHandle);
257 /**
258 * Callback which gets invoked when the client wants to retrieve the size of a specific file.
259 *
260 * @returns VBox status code.
261 * @param pData Pointer to generic callback data.
262 * @param pcszPath Path of file to retrieve size for.
263 * @param puSize Where to store the file size on success.
264 */
265 DECLCALLBACKMEMBER(int, pfnOnFileGetSize)(PRTFTPCALLBACKDATA pData, const char *pcszPath, uint64_t *puSize);
266 /**
267 * Callback which gets invoked when the client wants to retrieve information about a file.
268 *
269 * @param pData Pointer to generic callback data.
270 * @param pcszPath Path of file / directory to "stat". Optional. If NULL, the current directory will be used.
271 * @param pFsObjInfo Where to return the RTFSOBJINFO data on success. Optional.
272 * @returns VBox status code.
273 */
274 DECLCALLBACKMEMBER(int, pfnOnFileStat)(PRTFTPCALLBACKDATA pData, const char *pcszPath, PRTFSOBJINFO pFsObjInfo);
275 /**
276 * Callback which gets invoked when setting the current working directory.
277 *
278 * @returns VBox status code.
279 * @param pData Pointer to generic callback data.
280 * @param pcszCWD Current working directory to set.
281 */
282 DECLCALLBACKMEMBER(int, pfnOnPathSetCurrent)(PRTFTPCALLBACKDATA pData, const char *pcszCWD);
283 /**
284 * Callback which gets invoked when a client wants to retrieve the current working directory.
285 *
286 * @returns VBox status code.
287 * @param pData Pointer to generic callback data.
288 * @param pszPWD Where to store the current working directory.
289 * @param cbPWD Size of buffer in bytes.
290 */
291 DECLCALLBACKMEMBER(int, pfnOnPathGetCurrent)(PRTFTPCALLBACKDATA pData, char *pszPWD, size_t cbPWD);
292 /**
293 * Callback which gets invoked when the client wants to move up a directory (relative to the current working directory).
294 *
295 * @returns VBox status code.
296 * @param pData Pointer to generic callback data.
297 */
298 DECLCALLBACKMEMBER(int, pfnOnPathUp)(PRTFTPCALLBACKDATA pData);
299 /**
300 * Callback which gets invoked when the client wants to list a directory or file.
301 *
302 * @param pData Pointer to generic callback data.
303 * @param pcszPath Path of file / directory to list. Optional. If NULL, the current directory will be listed.
304 * @param ppvData Where to return the listing data. Must be free'd by the caller.
305 * @param pcbvData Where to return the listing data size in bytes.
306 * @returns VBox status code.
307 */
308 DECLCALLBACKMEMBER(int, pfnOnList)(PRTFTPCALLBACKDATA pData, const char *pcszPath, void **ppvData, size_t *pcbData);
309} RTFTPSERVERCALLBACKS;
310/** Pointer to a FTP server callback data table. */
311typedef RTFTPSERVERCALLBACKS *PRTFTPSERVERCALLBACKS;
312
313/**
314 * Creates a FTP server instance.
315 *
316 * @returns IPRT status code.
317 * @param phFTPServer Where to store the FTP server handle.
318 * @param pcszAddress The address for creating a listening socket.
319 * If NULL or empty string the server is bound to all interfaces.
320 * @param uPort The port for creating a listening socket.
321 * @param pCallbacks Callback table to use.
322 * @param pvUser Pointer to user-specific data. Optional.
323 * @param cbUser Size of user-specific data. Optional.
324 */
325RTR3DECL(int) RTFtpServerCreate(PRTFTPSERVER phFTPServer, const char *pcszAddress, uint16_t uPort,
326 PRTFTPSERVERCALLBACKS pCallbacks, void *pvUser, size_t cbUser);
327
328/**
329 * Destroys a FTP server instance.
330 *
331 * @returns IPRT status code.
332 * @param hFTPServer Handle to the FTP server handle.
333 */
334RTR3DECL(int) RTFtpServerDestroy(RTFTPSERVER hFTPServer);
335
336/** @} */
337
338/** @} */
339RT_C_DECLS_END
340
341#endif /* !IPRT_INCLUDED_ftp_h */
342
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette