VirtualBox

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

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

IPRT/FTP: More work on data connection handling. bugref:9646

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1/* $Id: ftp.h 82732 2020-01-14 09:53:20Z 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 /** Service ready for new user. */
129 RTFTPSERVER_REPLY_READY_FOR_NEW_USER = 220,
130 /** Service is closing control connection. */
131 RTFTPSERVER_REPLY_CLOSING_CTRL_CONN = 221,
132 /** Closing data connection. */
133 RTFTPSERVER_REPLY_CLOSING_DATA_CONN = 226,
134 /** User logged in, proceed. */
135 RTFTPSERVER_REPLY_LOGGED_IN_PROCEED = 230,
136 /** User name okay, need password. */
137 RTFTPSERVER_REPLY_USERNAME_OKAY_NEED_PASSWORD = 331,
138 /** Can't open data connection. */
139 RTFTPSERVER_REPLY_CANT_OPEN_DATA_CONN = 425,
140 /** Connection closed; transfer aborted. */
141 RTFTPSERVER_REPLY_CONN_CLOSED_TRANSFER_ABORTED = 426,
142 /** Syntax error, command unrecognized. */
143 RTFTPSERVER_REPLY_ERROR_CMD_NOT_RECOGNIZED = 500,
144 /** Syntax error in parameters or arguments. */
145 RTFTPSERVER_REPLY_ERROR_INVALID_PARAMETERS = 501,
146 /** Command not implemented. */
147 RTFTPSERVER_REPLY_ERROR_CMD_NOT_IMPL = 502,
148 /** Bad sequence of commands. */
149 RTFTPSERVER_REPLY_ERROR_BAD_SEQUENCE = 503,
150 /** Command not implemented for that parameter. */
151 RTFTPSERVER_REPLY_ERROR_CMD_NOT_IMPL_PARAM = 504,
152 /** Not logged in. */
153 RTFTPSERVER_REPLY_NOT_LOGGED_IN = 530,
154 /** Requested action not taken. */
155 RTFTPSERVER_REPLY_REQ_ACTION_NOT_TAKEN = 550,
156 /** The usual 32-bit hack. */
157 RTFTPSERVER_REPLY_32BIT_HACK = 0x7fffffff
158} RTFTPSERVER_REPLY;
159
160/**
161 * Structure for maintaining a FTP server client state.
162 */
163typedef struct RTFTPSERVERCLIENTSTATE
164{
165 /** User name. */
166 char *pszUser;
167 /** Number of failed login attempts. */
168 uint8_t cFailedLoginAttempts;
169 /** Timestamp (in ms) of last command issued by the client. */
170 uint64_t tsLastCmdMs;
171 /** Current set data type. */
172 RTFTPSERVER_DATA_TYPE enmDataType;
173 /** Current set struct type. */
174 RTFTPSERVER_STRUCT_TYPE enmStructType;
175} RTFTPSERVERCLIENTSTATE;
176/** Pointer to a FTP server client state. */
177typedef RTFTPSERVERCLIENTSTATE *PRTFTPSERVERCLIENTSTATE;
178
179/**
180 * Structure for storing FTP server callback data.
181 */
182typedef struct RTFTPCALLBACKDATA
183{
184 /** Pointer to the client state. */
185 PRTFTPSERVERCLIENTSTATE pClient;
186 /** Saved user pointer. */
187 void *pvUser;
188 /** Size (in bytes) of data at user pointer. */
189 size_t cbUser;
190} RTFTPCALLBACKDATA;
191/** Pointer to FTP server callback data. */
192typedef RTFTPCALLBACKDATA *PRTFTPCALLBACKDATA;
193
194/**
195 * Function callback table for the FTP server implementation.
196 *
197 * All callbacks are optional and therefore can be NULL.
198 */
199typedef struct RTFTPSERVERCALLBACKS
200{
201 /**
202 * Callback which gets invoked when a user connected.
203 *
204 * @returns VBox status code.
205 * @param pData Pointer to generic callback data.
206 * @param pcszUser User name.
207 */
208 DECLCALLBACKMEMBER(int, pfnOnUserConnect)(PRTFTPCALLBACKDATA pData, const char *pcszUser);
209 /**
210 * Callback which gets invoked when a user tries to authenticate with a password.
211 *
212 * @returns VBox status code.
213 * @param pData Pointer to generic callback data.
214 * @param pcszUser User name to authenticate.
215 * @param pcszPassword Password to authenticate with.
216 */
217 DECLCALLBACKMEMBER(int, pfnOnUserAuthenticate)(PRTFTPCALLBACKDATA pData, const char *pcszUser, const char *pcszPassword);
218 /**
219 * Callback which gets invoked when a user disconnected.
220 *
221 * @returns VBox status code.
222 * @param pData Pointer to generic callback data.
223 * @param pcszUser User name which disconnected.
224 */
225 DECLCALLBACKMEMBER(int, pfnOnUserDisconnect)(PRTFTPCALLBACKDATA pData, const char *pcszUser);
226 /**
227 * Callback which gets invoked when the client wants to start reading or writing a file.
228 *
229 * @returns VBox status code.
230 * @param pData Pointer to generic callback data.
231 * @param pcsszPath Path of file to handle.
232 * @param fMode File mode to use (IPRT stlye).
233 * @param ppvHandle Opaque file handle only known to the callback implementation.
234 */
235 DECLCALLBACKMEMBER(int, pfnOnFileOpen)(PRTFTPCALLBACKDATA pData, const char *pcszPath, uint32_t fMode, void **ppvHandle);
236 /**
237 * Callback which gets invoked when the client wants to read from a file.
238 *
239 * @returns VBox status code.
240 * @param pData Pointer to generic callback data.
241 * @param pvHandle Opaque file handle only known to the callback implementation.
242 * @param pvBuf Where to store the read file data.
243 * @param cbToRead How much (in bytes) to read. Must at least supply the size of pvBuf.
244 * @param pcbRead How much (in bytes) was read. Optional.
245 */
246 DECLCALLBACKMEMBER(int, pfnOnFileRead)(PRTFTPCALLBACKDATA pData, void *pvHandle, void *pvBuf, size_t cbToRead, size_t *pcbRead);
247 /**
248 * Callback which gets invoked when the client is done reading from or writing to a file.
249 *
250 * @returns VBox status code.
251 * @param pData Pointer to generic callback data.
252 * @param ppvHandle Opaque file handle only known to the callback implementation.
253 */
254 DECLCALLBACKMEMBER(int, pfnOnFileClose)(PRTFTPCALLBACKDATA pData, void *pvHandle);
255 /**
256 * Callback which gets invoked when the client wants to retrieve the size of a specific file.
257 *
258 * @returns VBox status code.
259 * @param pData Pointer to generic callback data.
260 * @param pcszPath Path of file to retrieve size for.
261 * @param puSize Where to store the file size on success.
262 */
263 DECLCALLBACKMEMBER(int, pfnOnFileGetSize)(PRTFTPCALLBACKDATA pData, const char *pcszPath, uint64_t *puSize);
264 /**
265 * Callback which gets invoked when the client wants to retrieve information about a file.
266 *
267 * @param pData Pointer to generic callback data.
268 * @param pcszPath Path of file / directory to "stat". Optional. If NULL, the current directory will be used.
269 * @param pFsObjInfo Where to return the RTFSOBJINFO data on success. Optional.
270 * @returns VBox status code.
271 */
272 DECLCALLBACKMEMBER(int, pfnOnFileStat)(PRTFTPCALLBACKDATA pData, const char *pcszPath, PRTFSOBJINFO pFsObjInfo);
273 /**
274 * Callback which gets invoked when setting the current working directory.
275 *
276 * @returns VBox status code.
277 * @param pData Pointer to generic callback data.
278 * @param pcszCWD Current working directory to set.
279 */
280 DECLCALLBACKMEMBER(int, pfnOnPathSetCurrent)(PRTFTPCALLBACKDATA pData, const char *pcszCWD);
281 /**
282 * Callback which gets invoked when a client wants to retrieve the current working directory.
283 *
284 * @returns VBox status code.
285 * @param pData Pointer to generic callback data.
286 * @param pszPWD Where to store the current working directory.
287 * @param cbPWD Size of buffer in bytes.
288 */
289 DECLCALLBACKMEMBER(int, pfnOnPathGetCurrent)(PRTFTPCALLBACKDATA pData, char *pszPWD, size_t cbPWD);
290 /**
291 * Callback which gets invoked when the client wants to move up a directory (relative to the current working directory).
292 *
293 * @returns VBox status code.
294 * @param pData Pointer to generic callback data.
295 */
296 DECLCALLBACKMEMBER(int, pfnOnPathUp)(PRTFTPCALLBACKDATA pData);
297 /**
298 * Callback which gets invoked when the client wants to list a directory or file.
299 *
300 * @param pData Pointer to generic callback data.
301 * @param pcszPath Path of file / directory to list. Optional. If NULL, the current directory will be listed.
302 * @param ppvData Where to return the listing data. Must be free'd by the caller.
303 * @param pcbvData Where to return the listing data size in bytes.
304 * @returns VBox status code.
305 */
306 DECLCALLBACKMEMBER(int, pfnOnList)(PRTFTPCALLBACKDATA pData, const char *pcszPath, void **ppvData, size_t *pcbData);
307} RTFTPSERVERCALLBACKS;
308/** Pointer to a FTP server callback data table. */
309typedef RTFTPSERVERCALLBACKS *PRTFTPSERVERCALLBACKS;
310
311/**
312 * Creates a FTP server instance.
313 *
314 * @returns IPRT status code.
315 * @param phFTPServer Where to store the FTP server handle.
316 * @param pcszAddress The address for creating a listening socket.
317 * If NULL or empty string the server is bound to all interfaces.
318 * @param uPort The port for creating a listening socket.
319 * @param pCallbacks Callback table to use.
320 * @param pvUser Pointer to user-specific data. Optional.
321 * @param cbUser Size of user-specific data. Optional.
322 */
323RTR3DECL(int) RTFtpServerCreate(PRTFTPSERVER phFTPServer, const char *pcszAddress, uint16_t uPort,
324 PRTFTPSERVERCALLBACKS pCallbacks, void *pvUser, size_t cbUser);
325
326/**
327 * Destroys a FTP server instance.
328 *
329 * @returns IPRT status code.
330 * @param hFTPServer Handle to the FTP server handle.
331 */
332RTR3DECL(int) RTFtpServerDestroy(RTFTPSERVER hFTPServer);
333
334/** @} */
335
336/** @} */
337RT_C_DECLS_END
338
339#endif /* !IPRT_INCLUDED_ftp_h */
340
Note: See TracBrowser for help on using the repository browser.

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