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