VirtualBox

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

Last change on this file since 99901 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 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 * Socket I/O multiplexing.
313 * Checks if the socket is ready for reading.
314 *
315 * @returns iprt status code.
316 * @param hSocket Socket descriptor.
317 * @param cMillies Number of milliseconds to wait for the socket.
318 * Use RT_INDEFINITE_WAIT to wait for ever.
319 */
320RTR3DECL(int) RTTcpSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
321
322/**
323 * Socket I/O multiplexing
324 * Checks if the socket is ready for one of the given events.
325 *
326 * @returns iprt status code.
327 * @param hSocket Socket descriptor.
328 * @param fEvents Event mask to wait for.
329 * Use the RTSOCKET_EVT_* defines.
330 * @param pfEvents Where to store the event mask on return.
331 * @param cMillies Number of milliseconds to wait for the socket.
332 * Use RT_INDEFINITE_WAIT to wait for ever.
333 */
334RTR3DECL(int) RTTcpSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies);
335
336#if 0 /* skipping these for now - RTTcpServer* handles this. */
337/**
338 * Listen for connection on a socket.
339 *
340 * @returns iprt status code.
341 * @param hSocket Socket descriptor.
342 * @param cBackLog The maximum length the queue of pending connections
343 * may grow to.
344 */
345RTR3DECL(int) RTTcpListen(RTSOCKET hSocket, int cBackLog);
346
347/**
348 * Accept a connection on a socket.
349 *
350 * @returns iprt status code.
351 * @param hSocket Socket descriptor.
352 * @param uPort The port for accepting connection.
353 * @param pSockAccepted Where to store the handle to the accepted connection.
354 */
355RTR3DECL(int) RTTcpAccept(RTSOCKET hSocket, unsigned uPort, PRTSOCKET pSockAccepted);
356
357#endif
358
359/**
360 * Gets the address of the local side.
361 *
362 * @returns IPRT status code.
363 * @param hSocket Socket descriptor.
364 * @param pAddr Where to store the local address on success.
365 */
366RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
367
368/**
369 * Gets the address of the other party.
370 *
371 * @returns IPRT status code.
372 * @param hSocket Socket descriptor.
373 * @param pAddr Where to store the peer address on success.
374 */
375RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
376
377/**
378 * Send data from a scatter/gather buffer to a socket.
379 *
380 * @returns iprt status code.
381 * @retval VERR_INTERRUPTED if interrupted before anything was written.
382 *
383 * @param hSocket Socket descriptor.
384 * @param pSgBuf Scatter/gather buffer to write data to socket.
385 */
386RTR3DECL(int) RTTcpSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
387
388
389/**
390 * Send data from multiple buffers to a socket.
391 *
392 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
393 * for lazy coders. The "L" in the function name is short for "list" just like
394 * in the execl libc API.
395 *
396 * @returns IPRT status code.
397 * @retval VERR_INTERRUPTED if interrupted before anything was written.
398 *
399 * @param hSocket The socket handle.
400 * @param cSegs The number of data segments in the following
401 * ellipsis.
402 * @param ... Pairs of buffer pointers (void const *) and buffer
403 * sizes (size_t). Make 101% sure the pointer is
404 * really size_t.
405 */
406RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
407
408/**
409 * Send data from multiple buffers to a socket.
410 *
411 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
412 * for lazy coders. The "L" in the function name is short for "list" just like
413 * in the execl libc API.
414 *
415 * @returns IPRT status code.
416 * @retval VERR_INTERRUPTED if interrupted before anything was written.
417 *
418 * @param hSocket The socket handle.
419 * @param cSegs The number of data segments in the following
420 * argument list.
421 * @param va Pairs of buffer pointers (void const *) and buffer
422 * sizes (size_t). Make 101% sure the pointer is
423 * really size_t.
424 */
425RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
426
427/**
428 * Receive data from a socket.
429 *
430 * This version doesn't block if there is no data on the socket.
431 *
432 * @returns IPRT status code.
433 *
434 * @param hSocket Socket descriptor.
435 * @param pvBuffer Where to put the data we read.
436 * @param cbBuffer Read buffer size.
437 * @param pcbRead Number of bytes read.
438 */
439RTR3DECL(int) RTTcpReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
440
441/**
442 * Send data to a socket.
443 *
444 * This version doesn't block if there is not enough room for the message.
445 *
446 * @returns IPRT status code.
447 *
448 * @param hSocket Socket descriptor.
449 * @param pvBuffer Buffer to write data to socket.
450 * @param cbBuffer How much to write.
451 * @param pcbWritten Number of bytes written.
452 */
453RTR3DECL(int) RTTcpWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
454
455/**
456 * Send data from a scatter/gather buffer to a socket.
457 *
458 * This version doesn't block if there is not enough room for the message.
459 *
460 * @returns iprt status code.
461 * @retval VERR_INTERRUPTED if interrupted before anything was written.
462 *
463 * @param hSocket Socket descriptor.
464 * @param pSgBuf Scatter/gather buffer to write data to socket.
465 * @param pcbWritten Number of bytes written.
466 */
467RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten);
468
469
470/**
471 * Send data from multiple buffers to a socket.
472 *
473 * This version doesn't block if there is not enough room for the message.
474 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
475 * for lazy coders. The "L" in the function name is short for "list" just like
476 * in the execl libc API.
477 *
478 * @returns IPRT status code.
479 *
480 * @param hSocket The socket handle.
481 * @param cSegs The number of data segments in the following
482 * ellipsis.
483 * @param pcbWritten Number of bytes written.
484 * @param ... Pairs of buffer pointers (void const *) and buffer
485 * sizes (size_t). Make 101% sure the pointer is
486 * really size_t.
487 */
488RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
489
490/**
491 * Send data from multiple buffers to a socket.
492 *
493 * This version doesn't block if there is not enough room for the message.
494 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
495 * for lazy coders. The "L" in the function name is short for "list" just like
496 * in the execl libc API.
497 *
498 * @returns IPRT status code.
499 *
500 * @param hSocket The socket handle.
501 * @param cSegs The number of data segments in the following
502 * argument list.
503 * @param pcbWritten Number of bytes written.
504 * @param va Pairs of buffer pointers (void const *) and buffer
505 * sizes (size_t). Make 101% sure the pointer is
506 * really size_t.
507 */
508RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
509
510/** @} */
511RT_C_DECLS_END
512
513#endif /* !IPRT_INCLUDED_tcp_h */
514
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use