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 |
|
---|
35 | #ifdef IN_RING0
|
---|
36 | # error "There are no RTFile APIs available Ring-0 Host Context!"
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | RT_C_DECLS_BEGIN
|
---|
41 |
|
---|
42 | /** @defgroup grp_rt_tcp RTTcp - TCP/IP
|
---|
43 | * @ingroup grp_rt
|
---|
44 | * @{
|
---|
45 | */
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Serve a TCP Server connection.
|
---|
50 | *
|
---|
51 | * @returns iprt status code.
|
---|
52 | * @returns VERR_TCP_SERVER_STOP to terminate the server loop forcing
|
---|
53 | * the RTTcpCreateServer() call to return.
|
---|
54 | * @param Sock The socket which the client is connected to.
|
---|
55 | * The call will close this socket.
|
---|
56 | * @param pvUser User argument.
|
---|
57 | */
|
---|
58 | typedef DECLCALLBACK(int) FNRTTCPSERVE(RTSOCKET Sock, void *pvUser);
|
---|
59 | /** Pointer to a RTTCPSERVE(). */
|
---|
60 | typedef FNRTTCPSERVE *PFNRTTCPSERVE;
|
---|
61 |
|
---|
62 | /** Pointer to a RTTCPSERVER handle. */
|
---|
63 | typedef struct RTTCPSERVER *PRTTCPSERVER;
|
---|
64 | /** Pointer to a RTTCPSERVER handle. */
|
---|
65 | typedef PRTTCPSERVER *PPRTTCPSERVER;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Create single connection at a time TCP Server in a separate thread.
|
---|
69 | *
|
---|
70 | * The thread will loop accepting connections and call pfnServe for
|
---|
71 | * each of the incoming connections in turn. The pfnServe function can
|
---|
72 | * return VERR_TCP_SERVER_STOP too terminate this loop. RTTcpServerDestroy()
|
---|
73 | * should be used to terminate the server.
|
---|
74 | *
|
---|
75 | * @returns iprt status code.
|
---|
76 | * @param pszAddress The address for creating a listening socket.
|
---|
77 | * If NULL or empty string the server is bound to all interfaces.
|
---|
78 | * @param uPort The port for creating a listening socket.
|
---|
79 | * @param enmType The thread type.
|
---|
80 | * @param pszThrdName The name of the worker thread.
|
---|
81 | * @param pfnServe The function which will serve a new client connection.
|
---|
82 | * @param pvUser User argument passed to pfnServe.
|
---|
83 | * @param ppServer Where to store the serverhandle.
|
---|
84 | */
|
---|
85 | RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
|
---|
86 | PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Create single connection at a time TCP Server.
|
---|
90 | * The caller must call RTTcpServerListen() to actually start the server.
|
---|
91 | *
|
---|
92 | * @returns iprt status code.
|
---|
93 | * @param pszAddress The address for creating a listening socket.
|
---|
94 | * If NULL the server is bound to all interfaces.
|
---|
95 | * @param uPort The port for creating a listening socket.
|
---|
96 | * @param ppServer Where to store the serverhandle.
|
---|
97 | */
|
---|
98 | RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer);
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Closes down and frees a TCP Server.
|
---|
102 | * This will also terminate any open connections to the server.
|
---|
103 | *
|
---|
104 | * @returns iprt status code.
|
---|
105 | * @param pServer Handle to the server.
|
---|
106 | */
|
---|
107 | RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer);
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Listen for incoming connections.
|
---|
111 | *
|
---|
112 | * The function will loop accepting connections and call pfnServe for
|
---|
113 | * each of the incoming connections in turn. The pfnServe function can
|
---|
114 | * return VERR_TCP_SERVER_STOP too terminate this loop. A stopped server
|
---|
115 | * can only be destroyed.
|
---|
116 | *
|
---|
117 | * @returns iprt status code.
|
---|
118 | * @param pServer The server handle as returned from RTTcpServerCreateEx().
|
---|
119 | * @param pfnServe The function which will serve a new client connection.
|
---|
120 | * @param pvUser User argument passed to pfnServe.
|
---|
121 | */
|
---|
122 | RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser);
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Listen and accept one incomming connection.
|
---|
126 | *
|
---|
127 | * This is an alternative to RTTcpServerListen for the use the callbacks are not
|
---|
128 | * possible.
|
---|
129 | *
|
---|
130 | * @returns IPRT status code.
|
---|
131 | * @retval VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
|
---|
132 | * @retval VERR_INTERRUPTED if the listening was interrupted.
|
---|
133 | *
|
---|
134 | * @param pServer The server handle as returned from RTTcpServerCreateEx().
|
---|
135 | * @param phClientSocket Where to return the socket handle to the client
|
---|
136 | * connection (on success only). This must be closed
|
---|
137 | * by calling RTTcpServerDisconnectClient2().
|
---|
138 | */
|
---|
139 | RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET phClientSocket);
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Terminate the open connection to the server.
|
---|
143 | *
|
---|
144 | * @returns iprt status code.
|
---|
145 | * @param pServer Handle to the server.
|
---|
146 | */
|
---|
147 | RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer);
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Terminates an open client connect when using RTTcpListen2
|
---|
151 | *
|
---|
152 | * @returns IPRT status code.
|
---|
153 | * @param hClientSocket The client socket handle. This will be invalid upon
|
---|
154 | * return, whether successful or not. NIL is quietly
|
---|
155 | * ignored (VINF_SUCCESS).
|
---|
156 | */
|
---|
157 | RTR3DECL(int) RTTcpServerDisconnectClient2(RTSOCKET hClientSocket);
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Shuts down the server, leaving client connections open.
|
---|
161 | *
|
---|
162 | * @returns IPRT status code.
|
---|
163 | * @param pServer Handle to the server.
|
---|
164 | */
|
---|
165 | RTR3DECL(int) RTTcpServerShutdown(PRTTCPSERVER pServer);
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Connect (as a client) to a TCP Server.
|
---|
169 | *
|
---|
170 | * @returns iprt status code.
|
---|
171 | * @param pszAddress The address to connect to.
|
---|
172 | * @param uPort The port to connect to.
|
---|
173 | * @param pSock Where to store the handle to the established connection.
|
---|
174 | */
|
---|
175 | RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Close a socket returned by RTTcpClientConnect().
|
---|
179 | *
|
---|
180 | * @returns iprt status code.
|
---|
181 | * @param Sock Socket descriptor.
|
---|
182 | */
|
---|
183 | RTR3DECL(int) RTTcpClientClose(RTSOCKET Sock);
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Receive data from a socket.
|
---|
187 | *
|
---|
188 | * @returns iprt status code.
|
---|
189 | * @param Sock Socket descriptor.
|
---|
190 | * @param pvBuffer Where to put the data we read.
|
---|
191 | * @param cbBuffer Read buffer size.
|
---|
192 | * @param pcbRead Number of bytes read.
|
---|
193 | * If NULL the entire buffer will be filled upon successful return.
|
---|
194 | * If not NULL a partial read can be done successfully.
|
---|
195 | */
|
---|
196 | RTR3DECL(int) RTTcpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Send data to a socket.
|
---|
200 | *
|
---|
201 | * @returns iprt status code.
|
---|
202 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
203 | *
|
---|
204 | * @param Sock Socket descriptor.
|
---|
205 | * @param pvBuffer Buffer to write data to socket.
|
---|
206 | * @param cbBuffer How much to write.
|
---|
207 | */
|
---|
208 | RTR3DECL(int) RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Flush socket write buffers.
|
---|
212 | *
|
---|
213 | * @returns iprt status code.
|
---|
214 | * @param Sock Socket descriptor.
|
---|
215 | */
|
---|
216 | RTR3DECL(int) RTTcpFlush(RTSOCKET Sock);
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Enables or disables delaying sends to coalesce packets.
|
---|
220 | *
|
---|
221 | * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
|
---|
222 | * coalescing.
|
---|
223 | *
|
---|
224 | * @returns iprt status code.
|
---|
225 | * @param Sock Socket descriptor.
|
---|
226 | * @param fEnable When set to true enables coalescing.
|
---|
227 | */
|
---|
228 | RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET Sock, bool fEnable);
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Socket I/O multiplexing.
|
---|
232 | * Checks if the socket is ready for reading.
|
---|
233 | *
|
---|
234 | * @returns iprt status code.
|
---|
235 | * @param Sock Socket descriptor.
|
---|
236 | * @param cMillies Number of milliseconds to wait for the socket.
|
---|
237 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
238 | */
|
---|
239 | RTR3DECL(int) RTTcpSelectOne(RTSOCKET Sock, RTMSINTERVAL cMillies);
|
---|
240 |
|
---|
241 |
|
---|
242 | #if 0 /* skipping these for now - RTTcpServer* handles this. */
|
---|
243 | /**
|
---|
244 | * Listen for connection on a socket.
|
---|
245 | *
|
---|
246 | * @returns iprt status code.
|
---|
247 | * @param Sock Socket descriptor.
|
---|
248 | * @param cBackLog The maximum length the queue of pending connections
|
---|
249 | * may grow to.
|
---|
250 | */
|
---|
251 | RTR3DECL(int) RTTcpListen(RTSOCKET Sock, int cBackLog);
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Accept a connection on a socket.
|
---|
255 | *
|
---|
256 | * @returns iprt status code.
|
---|
257 | * @param Sock Socket descriptor.
|
---|
258 | * @param uPort The port for accepting connection.
|
---|
259 | * @param pSockAccepted Where to store the handle to the accepted connection.
|
---|
260 | */
|
---|
261 | RTR3DECL(int) RTTcpAccept(RTSOCKET Sock, unsigned uPort, PRTSOCKET pSockAccepted);
|
---|
262 |
|
---|
263 | #endif
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Gets the address of the local side.
|
---|
267 | *
|
---|
268 | * @returns IPRT status code.
|
---|
269 | * @param Sock Socket descriptor.
|
---|
270 | * @param pAddr Where to store the local address on success.
|
---|
271 | */
|
---|
272 | RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr);
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Gets the address of the other party.
|
---|
276 | *
|
---|
277 | * @returns IPRT status code.
|
---|
278 | * @param Sock Socket descriptor.
|
---|
279 | * @param pAddr Where to store the peer address on success.
|
---|
280 | */
|
---|
281 | RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr);
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Send data from a scatter/gather buffer to a socket.
|
---|
285 | *
|
---|
286 | * @returns iprt status code.
|
---|
287 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
288 | *
|
---|
289 | * @param Sock Socket descriptor.
|
---|
290 | * @param pSgBuf Scatter/gather buffer to write data to socket.
|
---|
291 | */
|
---|
292 | RTR3DECL(int) RTTcpSgWrite(RTSOCKET Sock, PCRTSGBUF pSgBuf);
|
---|
293 |
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Send data from multiple buffers to a socket.
|
---|
297 | *
|
---|
298 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
299 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
300 | * in the execl libc API.
|
---|
301 | *
|
---|
302 | * @returns IPRT status code.
|
---|
303 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
304 | *
|
---|
305 | * @param hSocket The socket handle.
|
---|
306 | * @param cSegs The number of data segments in the following
|
---|
307 | * ellipsis.
|
---|
308 | * @param ... Pairs of buffer pointers (void const *) and buffer
|
---|
309 | * sizes (size_t). Make 101% sure the pointer is
|
---|
310 | * really size_t.
|
---|
311 | */
|
---|
312 | RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Send data from multiple buffers to a socket.
|
---|
316 | *
|
---|
317 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
318 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
319 | * in the execl libc API.
|
---|
320 | *
|
---|
321 | * @returns IPRT status code.
|
---|
322 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
323 | *
|
---|
324 | * @param hSocket The socket handle.
|
---|
325 | * @param cSegs The number of data segments in the following
|
---|
326 | * argument list.
|
---|
327 | * @param va Pairs of buffer pointers (void const *) and buffer
|
---|
328 | * sizes (size_t). Make 101% sure the pointer is
|
---|
329 | * really size_t.
|
---|
330 | */
|
---|
331 | RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Receive data from a socket.
|
---|
335 | *
|
---|
336 | * This version doesn't block if there is no data on the socket.
|
---|
337 | *
|
---|
338 | * @returns IPRT status code.
|
---|
339 | *
|
---|
340 | * @param Sock Socket descriptor.
|
---|
341 | * @param pvBuffer Where to put the data we read.
|
---|
342 | * @param cbBuffer Read buffer size.
|
---|
343 | * @param pcbRead Number of bytes read.
|
---|
344 | */
|
---|
345 | RTR3DECL(int) RTTcpReadNB(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Send data to a socket.
|
---|
349 | *
|
---|
350 | * This version doesn't block if there is not enough room for the message.
|
---|
351 | *
|
---|
352 | * @returns IPRT status code.
|
---|
353 | *
|
---|
354 | * @param Sock Socket descriptor.
|
---|
355 | * @param pvBuffer Buffer to write data to socket.
|
---|
356 | * @param cbBuffer How much to write.
|
---|
357 | * @param pcbWritten Number of bytes written.
|
---|
358 | */
|
---|
359 | RTR3DECL(int) RTTcpWriteNB(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Send data from a scatter/gather buffer to a socket.
|
---|
363 | *
|
---|
364 | * This version doesn't block if there is not enough room for the message.
|
---|
365 | *
|
---|
366 | * @returns iprt status code.
|
---|
367 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
368 | *
|
---|
369 | * @param Sock Socket descriptor.
|
---|
370 | * @param pSgBuf Scatter/gather buffer to write data to socket.
|
---|
371 | * @param pcbWritten Number of bytes written.
|
---|
372 | */
|
---|
373 | RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET Sock, PCRTSGBUF pSgBuf, size_t *pcbWritten);
|
---|
374 |
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Send data from multiple buffers to a socket.
|
---|
378 | *
|
---|
379 | * This version doesn't block if there is not enough room for the message.
|
---|
380 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
381 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
382 | * in the execl libc API.
|
---|
383 | *
|
---|
384 | * @returns IPRT status code.
|
---|
385 | *
|
---|
386 | * @param hSocket The socket handle.
|
---|
387 | * @param cSegs The number of data segments in the following
|
---|
388 | * ellipsis.
|
---|
389 | * @param pcbWritten Number of bytes written.
|
---|
390 | * @param ... Pairs of buffer pointers (void const *) and buffer
|
---|
391 | * sizes (size_t). Make 101% sure the pointer is
|
---|
392 | * really size_t.
|
---|
393 | */
|
---|
394 | RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Send data from multiple buffers to a socket.
|
---|
398 | *
|
---|
399 | * This version doesn't block if there is not enough room for the message.
|
---|
400 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
401 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
402 | * in the execl libc API.
|
---|
403 | *
|
---|
404 | * @returns IPRT status code.
|
---|
405 | *
|
---|
406 | * @param hSocket The socket handle.
|
---|
407 | * @param cSegs The number of data segments in the following
|
---|
408 | * argument list.
|
---|
409 | * @param pcbWritten Number of bytes written.
|
---|
410 | * @param va Pairs of buffer pointers (void const *) and buffer
|
---|
411 | * sizes (size_t). Make 101% sure the pointer is
|
---|
412 | * really size_t.
|
---|
413 | */
|
---|
414 | RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
|
---|
415 |
|
---|
416 | /** @} */
|
---|
417 | RT_C_DECLS_END
|
---|
418 |
|
---|
419 | #endif
|
---|
420 |
|
---|