VirtualBox

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

Last change on this file since 53420 was 45948, checked in by vboxsync, 12 years ago

IPRT: Added RTTcpClientConnectEx and RTTcpClientCancelConnect.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.0 KB
Line 
1/** @file
2 * IPRT - TCP/IP.
3 */
4
5/*
6 * Copyright (C) 2006-2010 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 Sock The socket which the client is connected to.
56 * The call will close this socket.
57 * @param pvUser User argument.
58 */
59typedef DECLCALLBACK(int) FNRTTCPSERVE(RTSOCKET Sock, 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 ppCancelCookie Where to store information for canceling the
184 * operation (from a different thread). Optional.
185 *
186 * The pointer _must_ be initialized to NULL before a
187 * series of connection attempts begins, i.e. at a time
188 * where there will be no RTTcpClientCancelConnect
189 * calls racing access. RTTcpClientCancelConnect will
190 * set it to a special non-NULL value that causes the
191 * current or/and next connect call to fail.
192 *
193 * @sa RTTcpClientCancelConnect
194 */
195RTR3DECL(int) RTTcpClientConnectEx(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock,
196 PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
197
198/**
199 * Cancels a RTTcpClientConnectEx call on a different thread.
200 *
201 * @returns iprt status code.
202 * @param ppCancelCookie The address of the cookie pointer shared with the
203 * connect call.
204 */
205RTR3DECL(int) RTTcpClientCancelConnect(PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie);
206
207/**
208 * Close a socket returned by RTTcpClientConnect().
209 *
210 * @returns iprt status code.
211 * @param Sock Socket descriptor.
212 */
213RTR3DECL(int) RTTcpClientClose(RTSOCKET Sock);
214
215/**
216 * Close a socket returned by RTTcpClientConnect().
217 *
218 * @returns iprt status code.
219 * @param hSocket The socket handle.
220 * @param fGracefulShutdown If true, try do a graceful shutdown of the
221 * outgoing pipe and draining any lingering input.
222 * This is sometimes better for the server side.
223 * If false, just close the connection without
224 * further ado.
225 */
226RTR3DECL(int) RTTcpClientCloseEx(RTSOCKET Sock, bool fGracefulShutdown);
227
228/**
229 * Receive data from a socket.
230 *
231 * @returns iprt status code.
232 * @param Sock Socket descriptor.
233 * @param pvBuffer Where to put the data we read.
234 * @param cbBuffer Read buffer size.
235 * @param pcbRead Number of bytes read.
236 * If NULL the entire buffer will be filled upon successful return.
237 * If not NULL a partial read can be done successfully.
238 */
239RTR3DECL(int) RTTcpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
240
241/**
242 * Send data to a socket.
243 *
244 * @returns iprt status code.
245 * @retval VERR_INTERRUPTED if interrupted before anything was written.
246 *
247 * @param Sock Socket descriptor.
248 * @param pvBuffer Buffer to write data to socket.
249 * @param cbBuffer How much to write.
250 */
251RTR3DECL(int) RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
252
253/**
254 * Flush socket write buffers.
255 *
256 * @returns iprt status code.
257 * @param Sock Socket descriptor.
258 */
259RTR3DECL(int) RTTcpFlush(RTSOCKET Sock);
260
261/**
262 * Enables or disables delaying sends to coalesce packets.
263 *
264 * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
265 * coalescing.
266 *
267 * @returns iprt status code.
268 * @param Sock Socket descriptor.
269 * @param fEnable When set to true enables coalescing.
270 */
271RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET Sock, bool fEnable);
272
273/**
274 * Socket I/O multiplexing.
275 * Checks if the socket is ready for reading.
276 *
277 * @returns iprt status code.
278 * @param Sock Socket descriptor.
279 * @param cMillies Number of milliseconds to wait for the socket.
280 * Use RT_INDEFINITE_WAIT to wait for ever.
281 */
282RTR3DECL(int) RTTcpSelectOne(RTSOCKET Sock, RTMSINTERVAL cMillies);
283
284/**
285 * Socket I/O multiplexing
286 * Checks if the socket is ready for one of the given events.
287 *
288 * @returns iprt status code.
289 * @param Sock Socket descriptor.
290 * @param fEvents Event mask to wait for.
291 * Use the RTSOCKET_EVT_* defines.
292 * @param pfEvents Where to store the event mask on return.
293 * @param cMillies Number of milliseconds to wait for the socket.
294 * Use RT_INDEFINITE_WAIT to wait for ever.
295 */
296RTR3DECL(int) RTTcpSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
297 RTMSINTERVAL cMillies);
298
299#if 0 /* skipping these for now - RTTcpServer* handles this. */
300/**
301 * Listen for connection on a socket.
302 *
303 * @returns iprt status code.
304 * @param Sock Socket descriptor.
305 * @param cBackLog The maximum length the queue of pending connections
306 * may grow to.
307 */
308RTR3DECL(int) RTTcpListen(RTSOCKET Sock, int cBackLog);
309
310/**
311 * Accept a connection on a socket.
312 *
313 * @returns iprt status code.
314 * @param Sock Socket descriptor.
315 * @param uPort The port for accepting connection.
316 * @param pSockAccepted Where to store the handle to the accepted connection.
317 */
318RTR3DECL(int) RTTcpAccept(RTSOCKET Sock, unsigned uPort, PRTSOCKET pSockAccepted);
319
320#endif
321
322/**
323 * Gets the address of the local side.
324 *
325 * @returns IPRT status code.
326 * @param Sock Socket descriptor.
327 * @param pAddr Where to store the local address on success.
328 */
329RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr);
330
331/**
332 * Gets the address of the other party.
333 *
334 * @returns IPRT status code.
335 * @param Sock Socket descriptor.
336 * @param pAddr Where to store the peer address on success.
337 */
338RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr);
339
340/**
341 * Send data from a scatter/gather buffer to a socket.
342 *
343 * @returns iprt status code.
344 * @retval VERR_INTERRUPTED if interrupted before anything was written.
345 *
346 * @param Sock Socket descriptor.
347 * @param pSgBuf Scatter/gather buffer to write data to socket.
348 */
349RTR3DECL(int) RTTcpSgWrite(RTSOCKET Sock, PCRTSGBUF pSgBuf);
350
351
352/**
353 * Send data from multiple buffers to a socket.
354 *
355 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
356 * for lazy coders. The "L" in the function name is short for "list" just like
357 * in the execl libc API.
358 *
359 * @returns IPRT status code.
360 * @retval VERR_INTERRUPTED if interrupted before anything was written.
361 *
362 * @param hSocket The socket handle.
363 * @param cSegs The number of data segments in the following
364 * ellipsis.
365 * @param ... Pairs of buffer pointers (void const *) and buffer
366 * sizes (size_t). Make 101% sure the pointer is
367 * really size_t.
368 */
369RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
370
371/**
372 * Send data from multiple buffers to a socket.
373 *
374 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
375 * for lazy coders. The "L" in the function name is short for "list" just like
376 * in the execl libc API.
377 *
378 * @returns IPRT status code.
379 * @retval VERR_INTERRUPTED if interrupted before anything was written.
380 *
381 * @param hSocket The socket handle.
382 * @param cSegs The number of data segments in the following
383 * argument list.
384 * @param va Pairs of buffer pointers (void const *) and buffer
385 * sizes (size_t). Make 101% sure the pointer is
386 * really size_t.
387 */
388RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
389
390/**
391 * Receive data from a socket.
392 *
393 * This version doesn't block if there is no data on the socket.
394 *
395 * @returns IPRT status code.
396 *
397 * @param Sock Socket descriptor.
398 * @param pvBuffer Where to put the data we read.
399 * @param cbBuffer Read buffer size.
400 * @param pcbRead Number of bytes read.
401 */
402RTR3DECL(int) RTTcpReadNB(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
403
404/**
405 * Send data to a socket.
406 *
407 * This version doesn't block if there is not enough room for the message.
408 *
409 * @returns IPRT status code.
410 *
411 * @param Sock Socket descriptor.
412 * @param pvBuffer Buffer to write data to socket.
413 * @param cbBuffer How much to write.
414 * @param pcbWritten Number of bytes written.
415 */
416RTR3DECL(int) RTTcpWriteNB(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
417
418/**
419 * Send data from a scatter/gather buffer to a socket.
420 *
421 * This version doesn't block if there is not enough room for the message.
422 *
423 * @returns iprt status code.
424 * @retval VERR_INTERRUPTED if interrupted before anything was written.
425 *
426 * @param Sock Socket descriptor.
427 * @param pSgBuf Scatter/gather buffer to write data to socket.
428 * @param pcbWritten Number of bytes written.
429 */
430RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET Sock, PCRTSGBUF pSgBuf, size_t *pcbWritten);
431
432
433/**
434 * Send data from multiple buffers to a socket.
435 *
436 * This version doesn't block if there is not enough room for the message.
437 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
438 * for lazy coders. The "L" in the function name is short for "list" just like
439 * in the execl libc API.
440 *
441 * @returns IPRT status code.
442 *
443 * @param hSocket The socket handle.
444 * @param cSegs The number of data segments in the following
445 * ellipsis.
446 * @param pcbWritten Number of bytes written.
447 * @param ... Pairs of buffer pointers (void const *) and buffer
448 * sizes (size_t). Make 101% sure the pointer is
449 * really size_t.
450 */
451RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
452
453/**
454 * Send data from multiple buffers to a socket.
455 *
456 * This version doesn't block if there is not enough room for the message.
457 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
458 * for lazy coders. The "L" in the function name is short for "list" just like
459 * in the execl libc API.
460 *
461 * @returns IPRT status code.
462 *
463 * @param hSocket The socket handle.
464 * @param cSegs The number of data segments in the following
465 * argument list.
466 * @param pcbWritten Number of bytes written.
467 * @param va Pairs of buffer pointers (void const *) and buffer
468 * sizes (size_t). Make 101% sure the pointer is
469 * really size_t.
470 */
471RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
472
473/** @} */
474RT_C_DECLS_END
475
476#endif
477
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