VirtualBox

source: vbox/trunk/include/iprt/tcp.h@ 70492

Last change on this file since 70492 was 70492, checked in by vboxsync, 7 years ago

iprt/tcp.h: doxygen fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/** @file
2 * IPRT - TCP/IP.
3 */
4
5/*
6 * Copyright (C) 2006-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_tcp_h
27#define ___iprt_tcp_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/thread.h>
32#include <iprt/net.h>
33#include <iprt/sg.h>
34#include <iprt/socket.h>
35
36#ifdef IN_RING0
37# error "There are no RTFile APIs available Ring-0 Host Context!"
38#endif
39
40
41RT_C_DECLS_BEGIN
42
43/** @defgroup grp_rt_tcp RTTcp - TCP/IP
44 * @ingroup grp_rt
45 * @{
46 */
47
48
49/**
50 * Serve a TCP Server connection.
51 *
52 * @returns iprt status code.
53 * @returns VERR_TCP_SERVER_STOP to terminate the server loop forcing
54 * the RTTcpCreateServer() call to return.
55 * @param hSocket The socket which the client is connected to. The call
56 * will close this socket.
57 * @param pvUser User argument.
58 */
59typedef DECLCALLBACK(int) FNRTTCPSERVE(RTSOCKET hSocket, void *pvUser);
60/** Pointer to a RTTCPSERVE(). */
61typedef FNRTTCPSERVE *PFNRTTCPSERVE;
62
63/**
64 * Create single connection at a time TCP Server in a separate thread.
65 *
66 * The thread will loop accepting connections and call pfnServe for
67 * each of the incoming connections in turn. The pfnServe function can
68 * return VERR_TCP_SERVER_STOP too terminate this loop. RTTcpServerDestroy()
69 * should be used to terminate the server.
70 *
71 * @returns iprt status code.
72 * @param pszAddress The address for creating a listening socket.
73 * If NULL or empty string the server is bound to all interfaces.
74 * @param uPort The port for creating a listening socket.
75 * @param enmType The thread type.
76 * @param pszThrdName The name of the worker thread.
77 * @param pfnServe The function which will serve a new client connection.
78 * @param pvUser User argument passed to pfnServe.
79 * @param ppServer Where to store the serverhandle.
80 */
81RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
82 PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer);
83
84/**
85 * Create single connection at a time TCP Server.
86 * The caller must call RTTcpServerListen() to actually start the server.
87 *
88 * @returns iprt status code.
89 * @param pszAddress The address for creating a listening socket.
90 * If NULL the server is bound to all interfaces.
91 * @param uPort The port for creating a listening socket.
92 * @param ppServer Where to store the serverhandle.
93 */
94RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer);
95
96/**
97 * Closes down and frees a TCP Server.
98 * This will also terminate any open connections to the server.
99 *
100 * @returns iprt status code.
101 * @param pServer Handle to the server.
102 */
103RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer);
104
105/**
106 * Listen for incoming connections.
107 *
108 * The function will loop accepting connections and call pfnServe for
109 * each of the incoming connections in turn. The pfnServe function can
110 * return VERR_TCP_SERVER_STOP too terminate this loop. A stopped server
111 * can only be destroyed.
112 *
113 * @returns iprt status code.
114 * @param pServer The server handle as returned from RTTcpServerCreateEx().
115 * @param pfnServe The function which will serve a new client connection.
116 * @param pvUser User argument passed to pfnServe.
117 */
118RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser);
119
120/**
121 * Listen and accept one incoming connection.
122 *
123 * This is an alternative to RTTcpServerListen for the use the callbacks are not
124 * possible.
125 *
126 * @returns IPRT status code.
127 * @retval VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
128 * @retval VERR_INTERRUPTED if the listening was interrupted.
129 *
130 * @param pServer The server handle as returned from RTTcpServerCreateEx().
131 * @param phClientSocket Where to return the socket handle to the client
132 * connection (on success only). This must be closed
133 * by calling RTTcpServerDisconnectClient2().
134 */
135RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET phClientSocket);
136
137/**
138 * Terminate the open connection to the server.
139 *
140 * @returns iprt status code.
141 * @param pServer Handle to the server.
142 */
143RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer);
144
145/**
146 * Terminates an open client connect when using RTTcpListen2
147 *
148 * @returns IPRT status code.
149 * @param hClientSocket The client socket handle. This will be invalid upon
150 * return, whether successful or not. NIL is quietly
151 * ignored (VINF_SUCCESS).
152 */
153RTR3DECL(int) RTTcpServerDisconnectClient2(RTSOCKET hClientSocket);
154
155/**
156 * Shuts down the server, leaving client connections open.
157 *
158 * @returns IPRT status code.
159 * @param pServer Handle to the server.
160 */
161RTR3DECL(int) RTTcpServerShutdown(PRTTCPSERVER pServer);
162
163/**
164 * Connect (as a client) to a TCP Server.
165 *
166 * @returns iprt status code.
167 * @param pszAddress The address to connect to.
168 * @param uPort The port to connect to.
169 * @param pSock Where to store the handle to the established connection.
170 */
171RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
172
173/** Opaque pointer used by RTTcpClientConnectEx and RTTcpClientCancelConnect. */
174typedef struct RTTCPCLIENTCONNECTCANCEL *PRTTCPCLIENTCONNECTCANCEL;
175
176/**
177 * Connect (as a client) to a TCP Server, extended version.
178 *
179 * @returns iprt status code.
180 * @param pszAddress The address to connect to.
181 * @param uPort The port to connect to.
182 * @param pSock Where to store the handle to the established connection.
183 * @param cMillies Number of milliseconds to wait for the connect attempt to complete.
184 * Use RT_INDEFINITE_WAIT to wait for ever.
185 * Use RT_SOCKETCONNECT_DEFAULT_WAIT to wait for the default time
186 * configured on the running system.
187 * @param ppCancelCookie Where to store information for canceling the
188 * operation (from a different thread). Optional.
189 *
190 * The pointer _must_ be initialized to NULL before a
191 * series of connection attempts begins, i.e. at a time
192 * where there will be no RTTcpClientCancelConnect
193 * calls racing access. RTTcpClientCancelConnect will
194 * set it to a special non-NULL value that causes the
195 * current or/and next connect call to fail.
196 *
197 * @sa RTTcpClientCancelConnect
198 */
199RTR3DECL(int) RTTcpClientConnectEx(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock,
200 RTMSINTERVAL cMillies, PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
201
202/**
203 * Cancels a RTTcpClientConnectEx call on a different thread.
204 *
205 * @returns iprt status code.
206 * @param ppCancelCookie The address of the cookie pointer shared with the
207 * connect call.
208 */
209RTR3DECL(int) RTTcpClientCancelConnect(PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
210
211/**
212 * Close a socket returned by RTTcpClientConnect().
213 *
214 * @returns iprt status code.
215 * @param hSocket Socket descriptor.
216 */
217RTR3DECL(int) RTTcpClientClose(RTSOCKET hSocket);
218
219/**
220 * Close a socket returned by RTTcpClientConnect().
221 *
222 * @returns iprt status code.
223 * @param hSocket The socket handle.
224 * @param fGracefulShutdown If true, try do a graceful shutdown of the
225 * outgoing pipe and draining any lingering input.
226 * This is sometimes better for the server side.
227 * If false, just close the connection without
228 * further ado.
229 */
230RTR3DECL(int) RTTcpClientCloseEx(RTSOCKET hSocket, bool fGracefulShutdown);
231
232/**
233 * Creates connected pair of TCP sockets.
234 *
235 * @returns IPRT status code.
236 * @param phServer Where to return the "server" side of the pair.
237 * @param phClient Where to return the "client" side of the pair.
238 * @param fFlags Reserved, must be zero.
239 *
240 * @note There is no server or client side, but we gotta call it something.
241 */
242RTR3DECL(int) RTTcpCreatePair(PRTSOCKET phServer, PRTSOCKET phClient, uint32_t fFlags);
243
244/**
245 * Receive data from a socket.
246 *
247 * @returns iprt status code.
248 * @param hSocket Socket descriptor.
249 * @param pvBuffer Where to put the data we read.
250 * @param cbBuffer Read buffer size.
251 * @param pcbRead Number of bytes read.
252 * If NULL the entire buffer will be filled upon successful return.
253 * If not NULL a partial read can be done successfully.
254 */
255RTR3DECL(int) RTTcpRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
256
257/**
258 * Send data to a socket.
259 *
260 * @returns iprt status code.
261 * @retval VERR_INTERRUPTED if interrupted before anything was written.
262 *
263 * @param hSocket Socket descriptor.
264 * @param pvBuffer Buffer to write data to socket.
265 * @param cbBuffer How much to write.
266 */
267RTR3DECL(int) RTTcpWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer);
268
269/**
270 * Flush socket write buffers.
271 *
272 * @returns iprt status code.
273 * @param hSocket Socket descriptor.
274 */
275RTR3DECL(int) RTTcpFlush(RTSOCKET hSocket);
276
277/**
278 * Enables or disables delaying sends to coalesce packets.
279 *
280 * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
281 * coalescing.
282 *
283 * @returns iprt status code.
284 * @param hSocket Socket descriptor.
285 * @param fEnable When set to true enables coalescing.
286 */
287RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET hSocket, bool fEnable);
288
289/**
290 * Socket I/O multiplexing.
291 * Checks if the socket is ready for reading.
292 *
293 * @returns iprt status code.
294 * @param hSocket Socket descriptor.
295 * @param cMillies Number of milliseconds to wait for the socket.
296 * Use RT_INDEFINITE_WAIT to wait for ever.
297 */
298RTR3DECL(int) RTTcpSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
299
300/**
301 * Socket I/O multiplexing
302 * Checks if the socket is ready for one of the given events.
303 *
304 * @returns iprt status code.
305 * @param hSocket Socket descriptor.
306 * @param fEvents Event mask to wait for.
307 * Use the RTSOCKET_EVT_* defines.
308 * @param pfEvents Where to store the event mask on return.
309 * @param cMillies Number of milliseconds to wait for the socket.
310 * Use RT_INDEFINITE_WAIT to wait for ever.
311 */
312RTR3DECL(int) RTTcpSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies);
313
314#if 0 /* skipping these for now - RTTcpServer* handles this. */
315/**
316 * Listen for connection on a socket.
317 *
318 * @returns iprt status code.
319 * @param hSocket Socket descriptor.
320 * @param cBackLog The maximum length the queue of pending connections
321 * may grow to.
322 */
323RTR3DECL(int) RTTcpListen(RTSOCKET hSocket, int cBackLog);
324
325/**
326 * Accept a connection on a socket.
327 *
328 * @returns iprt status code.
329 * @param hSocket Socket descriptor.
330 * @param uPort The port for accepting connection.
331 * @param pSockAccepted Where to store the handle to the accepted connection.
332 */
333RTR3DECL(int) RTTcpAccept(RTSOCKET hSocket, unsigned uPort, PRTSOCKET pSockAccepted);
334
335#endif
336
337/**
338 * Gets the address of the local side.
339 *
340 * @returns IPRT status code.
341 * @param hSocket Socket descriptor.
342 * @param pAddr Where to store the local address on success.
343 */
344RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
345
346/**
347 * Gets the address of the other party.
348 *
349 * @returns IPRT status code.
350 * @param hSocket Socket descriptor.
351 * @param pAddr Where to store the peer address on success.
352 */
353RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
354
355/**
356 * Send data from a scatter/gather buffer to a socket.
357 *
358 * @returns iprt status code.
359 * @retval VERR_INTERRUPTED if interrupted before anything was written.
360 *
361 * @param hSocket Socket descriptor.
362 * @param pSgBuf Scatter/gather buffer to write data to socket.
363 */
364RTR3DECL(int) RTTcpSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
365
366
367/**
368 * Send data from multiple buffers to a socket.
369 *
370 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
371 * for lazy coders. The "L" in the function name is short for "list" just like
372 * in the execl libc API.
373 *
374 * @returns IPRT status code.
375 * @retval VERR_INTERRUPTED if interrupted before anything was written.
376 *
377 * @param hSocket The socket handle.
378 * @param cSegs The number of data segments in the following
379 * ellipsis.
380 * @param ... Pairs of buffer pointers (void const *) and buffer
381 * sizes (size_t). Make 101% sure the pointer is
382 * really size_t.
383 */
384RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
385
386/**
387 * Send data from multiple buffers to a socket.
388 *
389 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
390 * for lazy coders. The "L" in the function name is short for "list" just like
391 * in the execl libc API.
392 *
393 * @returns IPRT status code.
394 * @retval VERR_INTERRUPTED if interrupted before anything was written.
395 *
396 * @param hSocket The socket handle.
397 * @param cSegs The number of data segments in the following
398 * argument list.
399 * @param va Pairs of buffer pointers (void const *) and buffer
400 * sizes (size_t). Make 101% sure the pointer is
401 * really size_t.
402 */
403RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
404
405/**
406 * Receive data from a socket.
407 *
408 * This version doesn't block if there is no data on the socket.
409 *
410 * @returns IPRT status code.
411 *
412 * @param hSocket Socket descriptor.
413 * @param pvBuffer Where to put the data we read.
414 * @param cbBuffer Read buffer size.
415 * @param pcbRead Number of bytes read.
416 */
417RTR3DECL(int) RTTcpReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
418
419/**
420 * Send data to a socket.
421 *
422 * This version doesn't block if there is not enough room for the message.
423 *
424 * @returns IPRT status code.
425 *
426 * @param hSocket Socket descriptor.
427 * @param pvBuffer Buffer to write data to socket.
428 * @param cbBuffer How much to write.
429 * @param pcbWritten Number of bytes written.
430 */
431RTR3DECL(int) RTTcpWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
432
433/**
434 * Send data from a scatter/gather buffer to a socket.
435 *
436 * This version doesn't block if there is not enough room for the message.
437 *
438 * @returns iprt status code.
439 * @retval VERR_INTERRUPTED if interrupted before anything was written.
440 *
441 * @param hSocket Socket descriptor.
442 * @param pSgBuf Scatter/gather buffer to write data to socket.
443 * @param pcbWritten Number of bytes written.
444 */
445RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten);
446
447
448/**
449 * Send data from multiple buffers to a socket.
450 *
451 * This version doesn't block if there is not enough room for the message.
452 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
453 * for lazy coders. The "L" in the function name is short for "list" just like
454 * in the execl libc API.
455 *
456 * @returns IPRT status code.
457 *
458 * @param hSocket The socket handle.
459 * @param cSegs The number of data segments in the following
460 * ellipsis.
461 * @param pcbWritten Number of bytes written.
462 * @param ... Pairs of buffer pointers (void const *) and buffer
463 * sizes (size_t). Make 101% sure the pointer is
464 * really size_t.
465 */
466RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
467
468/**
469 * Send data from multiple buffers to a socket.
470 *
471 * This version doesn't block if there is not enough room for the message.
472 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
473 * for lazy coders. The "L" in the function name is short for "list" just like
474 * in the execl libc API.
475 *
476 * @returns IPRT status code.
477 *
478 * @param hSocket The socket handle.
479 * @param cSegs The number of data segments in the following
480 * argument list.
481 * @param pcbWritten Number of bytes written.
482 * @param va Pairs of buffer pointers (void const *) and buffer
483 * sizes (size_t). Make 101% sure the pointer is
484 * really size_t.
485 */
486RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
487
488/** @} */
489RT_C_DECLS_END
490
491#endif
492
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