VirtualBox

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

Last change on this file since 68113 was 62473, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/** @file
2 * IPRT - TCP/IP.
3 */
4
5/*
6 * Copyright (C) 2006-2016 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 * Receive data from a socket.
234 *
235 * @returns iprt status code.
236 * @param hSocket Socket descriptor.
237 * @param pvBuffer Where to put the data we read.
238 * @param cbBuffer Read buffer size.
239 * @param pcbRead Number of bytes read.
240 * If NULL the entire buffer will be filled upon successful return.
241 * If not NULL a partial read can be done successfully.
242 */
243RTR3DECL(int) RTTcpRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
244
245/**
246 * Send data to a socket.
247 *
248 * @returns iprt status code.
249 * @retval VERR_INTERRUPTED if interrupted before anything was written.
250 *
251 * @param hSocket Socket descriptor.
252 * @param pvBuffer Buffer to write data to socket.
253 * @param cbBuffer How much to write.
254 */
255RTR3DECL(int) RTTcpWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer);
256
257/**
258 * Flush socket write buffers.
259 *
260 * @returns iprt status code.
261 * @param hSocket Socket descriptor.
262 */
263RTR3DECL(int) RTTcpFlush(RTSOCKET hSocket);
264
265/**
266 * Enables or disables delaying sends to coalesce packets.
267 *
268 * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
269 * coalescing.
270 *
271 * @returns iprt status code.
272 * @param hSocket Socket descriptor.
273 * @param fEnable When set to true enables coalescing.
274 */
275RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET hSocket, bool fEnable);
276
277/**
278 * Socket I/O multiplexing.
279 * Checks if the socket is ready for reading.
280 *
281 * @returns iprt status code.
282 * @param hSocket Socket descriptor.
283 * @param cMillies Number of milliseconds to wait for the socket.
284 * Use RT_INDEFINITE_WAIT to wait for ever.
285 */
286RTR3DECL(int) RTTcpSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
287
288/**
289 * Socket I/O multiplexing
290 * Checks if the socket is ready for one of the given events.
291 *
292 * @returns iprt status code.
293 * @param hSocket Socket descriptor.
294 * @param fEvents Event mask to wait for.
295 * Use the RTSOCKET_EVT_* defines.
296 * @param pfEvents Where to store the event mask on return.
297 * @param cMillies Number of milliseconds to wait for the socket.
298 * Use RT_INDEFINITE_WAIT to wait for ever.
299 */
300RTR3DECL(int) RTTcpSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies);
301
302#if 0 /* skipping these for now - RTTcpServer* handles this. */
303/**
304 * Listen for connection on a socket.
305 *
306 * @returns iprt status code.
307 * @param hSocket Socket descriptor.
308 * @param cBackLog The maximum length the queue of pending connections
309 * may grow to.
310 */
311RTR3DECL(int) RTTcpListen(RTSOCKET hSocket, int cBackLog);
312
313/**
314 * Accept a connection on a socket.
315 *
316 * @returns iprt status code.
317 * @param hSocket Socket descriptor.
318 * @param uPort The port for accepting connection.
319 * @param pSockAccepted Where to store the handle to the accepted connection.
320 */
321RTR3DECL(int) RTTcpAccept(RTSOCKET hSocket, unsigned uPort, PRTSOCKET pSockAccepted);
322
323#endif
324
325/**
326 * Gets the address of the local side.
327 *
328 * @returns IPRT status code.
329 * @param hSocket Socket descriptor.
330 * @param pAddr Where to store the local address on success.
331 */
332RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
333
334/**
335 * Gets the address of the other party.
336 *
337 * @returns IPRT status code.
338 * @param hSocket Socket descriptor.
339 * @param pAddr Where to store the peer address on success.
340 */
341RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
342
343/**
344 * Send data from a scatter/gather buffer to a socket.
345 *
346 * @returns iprt status code.
347 * @retval VERR_INTERRUPTED if interrupted before anything was written.
348 *
349 * @param hSocket Socket descriptor.
350 * @param pSgBuf Scatter/gather buffer to write data to socket.
351 */
352RTR3DECL(int) RTTcpSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
353
354
355/**
356 * Send data from multiple buffers to a socket.
357 *
358 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
359 * for lazy coders. The "L" in the function name is short for "list" just like
360 * in the execl libc API.
361 *
362 * @returns IPRT status code.
363 * @retval VERR_INTERRUPTED if interrupted before anything was written.
364 *
365 * @param hSocket The socket handle.
366 * @param cSegs The number of data segments in the following
367 * ellipsis.
368 * @param ... Pairs of buffer pointers (void const *) and buffer
369 * sizes (size_t). Make 101% sure the pointer is
370 * really size_t.
371 */
372RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
373
374/**
375 * Send data from multiple buffers to a socket.
376 *
377 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
378 * for lazy coders. The "L" in the function name is short for "list" just like
379 * in the execl libc API.
380 *
381 * @returns IPRT status code.
382 * @retval VERR_INTERRUPTED if interrupted before anything was written.
383 *
384 * @param hSocket The socket handle.
385 * @param cSegs The number of data segments in the following
386 * argument list.
387 * @param va Pairs of buffer pointers (void const *) and buffer
388 * sizes (size_t). Make 101% sure the pointer is
389 * really size_t.
390 */
391RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
392
393/**
394 * Receive data from a socket.
395 *
396 * This version doesn't block if there is no data on the socket.
397 *
398 * @returns IPRT status code.
399 *
400 * @param hSocket Socket descriptor.
401 * @param pvBuffer Where to put the data we read.
402 * @param cbBuffer Read buffer size.
403 * @param pcbRead Number of bytes read.
404 */
405RTR3DECL(int) RTTcpReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
406
407/**
408 * Send data to a socket.
409 *
410 * This version doesn't block if there is not enough room for the message.
411 *
412 * @returns IPRT status code.
413 *
414 * @param hSocket Socket descriptor.
415 * @param pvBuffer Buffer to write data to socket.
416 * @param cbBuffer How much to write.
417 * @param pcbWritten Number of bytes written.
418 */
419RTR3DECL(int) RTTcpWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
420
421/**
422 * Send data from a scatter/gather buffer to a socket.
423 *
424 * This version doesn't block if there is not enough room for the message.
425 *
426 * @returns iprt status code.
427 * @retval VERR_INTERRUPTED if interrupted before anything was written.
428 *
429 * @param hSocket Socket descriptor.
430 * @param pSgBuf Scatter/gather buffer to write data to socket.
431 * @param pcbWritten Number of bytes written.
432 */
433RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten);
434
435
436/**
437 * Send data from multiple buffers to a socket.
438 *
439 * This version doesn't block if there is not enough room for the message.
440 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
441 * for lazy coders. The "L" in the function name is short for "list" just like
442 * in the execl libc API.
443 *
444 * @returns IPRT status code.
445 *
446 * @param hSocket The socket handle.
447 * @param cSegs The number of data segments in the following
448 * ellipsis.
449 * @param pcbWritten Number of bytes written.
450 * @param ... Pairs of buffer pointers (void const *) and buffer
451 * sizes (size_t). Make 101% sure the pointer is
452 * really size_t.
453 */
454RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
455
456/**
457 * Send data from multiple buffers to a socket.
458 *
459 * This version doesn't block if there is not enough room for the message.
460 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
461 * for lazy coders. The "L" in the function name is short for "list" just like
462 * in the execl libc API.
463 *
464 * @returns IPRT status code.
465 *
466 * @param hSocket The socket handle.
467 * @param cSegs The number of data segments in the following
468 * argument list.
469 * @param pcbWritten Number of bytes written.
470 * @param va Pairs of buffer pointers (void const *) and buffer
471 * sizes (size_t). Make 101% sure the pointer is
472 * really size_t.
473 */
474RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
475
476/** @} */
477RT_C_DECLS_END
478
479#endif
480
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