VirtualBox

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

Last change on this file since 104384 was 100171, checked in by vboxsync, 17 months ago

IPRT: tcp.h+tcp.cpp,socket.h+socket.cpp: Add RTTcpSetKeepAlive() which
allows one to enable or disable sending periodic keep-alive messages on
a socket (SO_KEEPALIVE) as described in RFC 1122. RTTcpSetKeepAlive()
also allows one to adjust several keep-alive options on a per-socket
basis: the idle time before keep-alive probes are sent (TCP_KEEPIDLE
(TCP_KEEPALIVE on macOS)), the amount of time between keep-alive probes
(TCP_KEEPINTVL), and the number of keep-alive probes to send before
closing the connection (TCP_KEEPCNT).

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