VirtualBox

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

Last change on this file since 95897 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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