GuestHost/installation/VBoxWinDrvInst.cpp: Try harder if DiInstallDriverW() returns...
[vbox.git] / include / iprt / tcp.h
blob252a5257556fd51ebb45ee777d774c7bd7142ca0
1 /** @file
2 * IPRT - TCP/IP.
3 */
5 /*
6 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
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.
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.
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>.
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.
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.
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
36 #ifndef IPRT_INCLUDED_tcp_h
37 #define IPRT_INCLUDED_tcp_h
38 #ifndef RT_WITHOUT_PRAGMA_ONCE
39 # pragma once
40 #endif
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>
49 #ifdef IN_RING0
50 # error "There are no RTFile APIs available Ring-0 Host Context!"
51 #endif
54 RT_C_DECLS_BEGIN
56 /** @defgroup grp_rt_tcp RTTcp - TCP/IP
57 * @ingroup grp_rt
58 * @{
62 /**
63 * Serve a TCP Server connection.
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.
72 typedef DECLCALLBACKTYPE(int, FNRTTCPSERVE,(RTSOCKET hSocket, void *pvUser));
73 /** Pointer to a RTTCPSERVE(). */
74 typedef FNRTTCPSERVE *PFNRTTCPSERVE;
76 /**
77 * Create single connection at a time TCP Server in a separate thread.
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.
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.
94 RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
95 PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer);
97 /**
98 * Create single connection at a time TCP Server.
99 * The caller must call RTTcpServerListen() to actually start the server.
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.
107 RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer);
110 * Closes down and frees a TCP Server.
111 * This will also terminate any open connections to the server.
113 * @returns iprt status code.
114 * @param pServer Handle to the server.
116 RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer);
119 * Listen for incoming connections.
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.
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.
131 RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser);
134 * Listen and accept one incoming connection.
136 * This is an alternative to RTTcpServerListen for the use the callbacks are not
137 * possible.
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.
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().
148 RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET phClientSocket);
151 * Terminate the open connection to the server.
153 * @returns iprt status code.
154 * @param pServer Handle to the server.
156 RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer);
159 * Terminates an open client connect when using RTTcpListen2
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).
166 RTR3DECL(int) RTTcpServerDisconnectClient2(RTSOCKET hClientSocket);
169 * Shuts down the server, leaving client connections open.
171 * @returns IPRT status code.
172 * @param pServer Handle to the server.
174 RTR3DECL(int) RTTcpServerShutdown(PRTTCPSERVER pServer);
177 * Connect (as a client) to a TCP Server.
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.
184 RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
186 /** Opaque pointer used by RTTcpClientConnectEx and RTTcpClientCancelConnect. */
187 typedef struct RTTCPCLIENTCONNECTCANCEL *PRTTCPCLIENTCONNECTCANCEL;
190 * Connect (as a client) to a TCP Server, extended version.
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.
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.
210 * @sa RTTcpClientCancelConnect
212 RTR3DECL(int) RTTcpClientConnectEx(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock,
213 RTMSINTERVAL cMillies, PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
216 * Cancels a RTTcpClientConnectEx call on a different thread.
218 * @returns iprt status code.
219 * @param ppCancelCookie The address of the cookie pointer shared with the
220 * connect call.
222 RTR3DECL(int) RTTcpClientCancelConnect(PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
225 * Close a socket returned by RTTcpClientConnect().
227 * @returns iprt status code.
228 * @param hSocket Socket descriptor.
230 RTR3DECL(int) RTTcpClientClose(RTSOCKET hSocket);
233 * Close a socket returned by RTTcpClientConnect().
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.
243 RTR3DECL(int) RTTcpClientCloseEx(RTSOCKET hSocket, bool fGracefulShutdown);
246 * Creates connected pair of TCP sockets.
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.
253 * @note There is no server or client side, but we gotta call it something.
255 RTR3DECL(int) RTTcpCreatePair(PRTSOCKET phServer, PRTSOCKET phClient, uint32_t fFlags);
258 * Receive data from a socket.
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.
268 RTR3DECL(int) RTTcpRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
271 * Send data to a socket.
273 * @returns iprt status code.
274 * @retval VERR_INTERRUPTED if interrupted before anything was written.
276 * @param hSocket Socket descriptor.
277 * @param pvBuffer Buffer to write data to socket.
278 * @param cbBuffer How much to write.
280 RTR3DECL(int) RTTcpWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer);
283 * Flush socket write buffers.
285 * @returns iprt status code.
286 * @param hSocket Socket descriptor.
288 RTR3DECL(int) RTTcpFlush(RTSOCKET hSocket);
291 * Enables or disables delaying sends to coalesce packets.
293 * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
294 * coalescing.
296 * @returns iprt status code.
297 * @param hSocket Socket descriptor.
298 * @param fEnable When set to true enables coalescing.
300 RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET hSocket, bool fEnable);
303 * Sets send and receive buffer sizes.
305 * @returns iprt status code.
306 * @param hSocket Socket descriptor.
307 * @param cbSize Buffer size in bytes.
309 RTR3DECL(int) RTTcpSetBufferSize(RTSOCKET hSocket, uint32_t cbSize);
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.
321 * @returns iprt status code.
322 * @retval VERR_NOT_SUPPORTED is returned if these keep-alive options aren't
323 * supported by the OS.
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).
340 RTR3DECL(int) RTTcpSetKeepAlive(RTSOCKET hSocket, bool fEnable, uint32_t cSecsIdle, uint32_t cSecsInterval,
341 uint32_t cFailedPktsBeforeClose);
344 * Socket I/O multiplexing.
345 * Checks if the socket is ready for reading.
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.
352 RTR3DECL(int) RTTcpSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
355 * Socket I/O multiplexing
356 * Checks if the socket is ready for one of the given events.
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.
366 RTR3DECL(int) RTTcpSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies);
368 #if 0 /* skipping these for now - RTTcpServer* handles this. */
370 * Listen for connection on a socket.
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.
377 RTR3DECL(int) RTTcpListen(RTSOCKET hSocket, int cBackLog);
380 * Accept a connection on a socket.
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.
387 RTR3DECL(int) RTTcpAccept(RTSOCKET hSocket, unsigned uPort, PRTSOCKET pSockAccepted);
389 #endif
392 * Gets the address of the local side.
394 * @returns IPRT status code.
395 * @param hSocket Socket descriptor.
396 * @param pAddr Where to store the local address on success.
398 RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
401 * Gets the address of the other party.
403 * @returns IPRT status code.
404 * @param hSocket Socket descriptor.
405 * @param pAddr Where to store the peer address on success.
407 RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
410 * Send data from a scatter/gather buffer to a socket.
412 * @returns iprt status code.
413 * @retval VERR_INTERRUPTED if interrupted before anything was written.
415 * @param hSocket Socket descriptor.
416 * @param pSgBuf Scatter/gather buffer to write data to socket.
418 RTR3DECL(int) RTTcpSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
422 * Send data from multiple buffers to a socket.
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.
428 * @returns IPRT status code.
429 * @retval VERR_INTERRUPTED if interrupted before anything was written.
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.
438 RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
441 * Send data from multiple buffers to a socket.
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.
447 * @returns IPRT status code.
448 * @retval VERR_INTERRUPTED if interrupted before anything was written.
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.
457 RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
460 * Receive data from a socket.
462 * This version doesn't block if there is no data on the socket.
464 * @returns IPRT status code.
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.
471 RTR3DECL(int) RTTcpReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
474 * Send data to a socket.
476 * This version doesn't block if there is not enough room for the message.
478 * @returns IPRT status code.
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.
485 RTR3DECL(int) RTTcpWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
488 * Send data from a scatter/gather buffer to a socket.
490 * This version doesn't block if there is not enough room for the message.
492 * @returns iprt status code.
493 * @retval VERR_INTERRUPTED if interrupted before anything was written.
495 * @param hSocket Socket descriptor.
496 * @param pSgBuf Scatter/gather buffer to write data to socket.
497 * @param pcbWritten Number of bytes written.
499 RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten);
503 * Send data from multiple buffers to a socket.
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.
510 * @returns IPRT status code.
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.
520 RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
523 * Send data from multiple buffers to a socket.
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.
530 * @returns IPRT status code.
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.
540 RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
542 /** @} */
543 RT_C_DECLS_END
545 #endif /* !IPRT_INCLUDED_tcp_h */