VirtualBox

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

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

IPRT/FTP: First working version for listing stuff. bugref:9646

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