1 | /** @file
|
---|
2 | * IPRT - TCP/IP.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_tcp_h
|
---|
31 | #define ___iprt_tcp_h
|
---|
32 |
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <iprt/thread.h>
|
---|
36 | #include <iprt/net.h>
|
---|
37 |
|
---|
38 | #ifdef IN_RING0
|
---|
39 | # error "There are no RTFile APIs available Ring-0 Host Context!"
|
---|
40 | #endif
|
---|
41 |
|
---|
42 |
|
---|
43 | RT_C_DECLS_BEGIN
|
---|
44 |
|
---|
45 | /** @defgroup grp_rt_tcp RTTcp - TCP/IP
|
---|
46 | * @ingroup grp_rt
|
---|
47 | * @{
|
---|
48 | */
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Serve a TCP Server connection.
|
---|
53 | *
|
---|
54 | * @returns iprt status code.
|
---|
55 | * @returns VERR_TCP_SERVER_STOP to terminate the server loop forcing
|
---|
56 | * the RTTcpCreateServer() call to return.
|
---|
57 | * @param Sock The socket which the client is connected to.
|
---|
58 | * The call will close this socket.
|
---|
59 | * @param pvUser User argument.
|
---|
60 | */
|
---|
61 | typedef DECLCALLBACK(int) FNRTTCPSERVE(RTSOCKET Sock, void *pvUser);
|
---|
62 | /** Pointer to a RTTCPSERVE(). */
|
---|
63 | typedef FNRTTCPSERVE *PFNRTTCPSERVE;
|
---|
64 |
|
---|
65 | /** Pointer to a RTTCPSERVER handle. */
|
---|
66 | typedef struct RTTCPSERVER *PRTTCPSERVER;
|
---|
67 | /** Pointer to a RTTCPSERVER handle. */
|
---|
68 | typedef PRTTCPSERVER *PPRTTCPSERVER;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Create single connection at a time TCP Server in a separate thread.
|
---|
72 | *
|
---|
73 | * The thread will loop accepting connections and call pfnServe for
|
---|
74 | * each of the incoming connections in turn. The pfnServe function can
|
---|
75 | * return VERR_TCP_SERVER_STOP too terminate this loop. RTTcpServerDestroy()
|
---|
76 | * should be used to terminate the server.
|
---|
77 | *
|
---|
78 | * @returns iprt status code.
|
---|
79 | * @param pszAddress The address for creating a listening socket.
|
---|
80 | * If NULL or empty string the server is bound to all interfaces.
|
---|
81 | * @param uPort The port for creating a listening socket.
|
---|
82 | * @param enmType The thread type.
|
---|
83 | * @param pszThrdName The name of the worker thread.
|
---|
84 | * @param pfnServe The function which will serve a new client connection.
|
---|
85 | * @param pvUser User argument passed to pfnServe.
|
---|
86 | * @param ppServer Where to store the serverhandle.
|
---|
87 | */
|
---|
88 | RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
|
---|
89 | PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Create single connection at a time TCP Server.
|
---|
93 | * The caller must call RTTcpServerListen() to actually start the server.
|
---|
94 | *
|
---|
95 | * @returns iprt status code.
|
---|
96 | * @param pszAddress The address for creating a listening socket.
|
---|
97 | * If NULL the server is bound to all interfaces.
|
---|
98 | * @param uPort The port for creating a listening socket.
|
---|
99 | * @param ppServer Where to store the serverhandle.
|
---|
100 | */
|
---|
101 | RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer);
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Closes down and frees a TCP Server.
|
---|
105 | * This will also terminate any open connections to the server.
|
---|
106 | *
|
---|
107 | * @returns iprt status code.
|
---|
108 | * @param pServer Handle to the server.
|
---|
109 | */
|
---|
110 | RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Listen for incoming connections.
|
---|
114 | *
|
---|
115 | * The function will loop accepting connections and call pfnServe for
|
---|
116 | * each of the incoming connections in turn. The pfnServe function can
|
---|
117 | * return VERR_TCP_SERVER_STOP too terminate this loop. A stopped server
|
---|
118 | * can only be destroyed.
|
---|
119 | *
|
---|
120 | * @returns iprt status code.
|
---|
121 | * @param pServer The server handle as returned from RTTcpServerCreateEx().
|
---|
122 | * @param pfnServe The function which will serve a new client connection.
|
---|
123 | * @param pvUser User argument passed to pfnServe.
|
---|
124 | */
|
---|
125 | RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser);
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Listen and accept one incomming connection.
|
---|
129 | *
|
---|
130 | * This is an alternative to RTTcpServerListen for the use the callbacks are not
|
---|
131 | * possible.
|
---|
132 | *
|
---|
133 | * @returns IPRT status code.
|
---|
134 | * @retval VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
|
---|
135 | * @retval VERR_INTERRUPTED if the listening was interrupted.
|
---|
136 | *
|
---|
137 | * @param pServer The server handle as returned from RTTcpServerCreateEx().
|
---|
138 | * @param phClientSocket Where to return the socket handle to the client
|
---|
139 | * connection (on success only). This must be closed
|
---|
140 | * by calling RTTcpServerDisconnectClient2().
|
---|
141 | */
|
---|
142 | RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET phClientSocket);
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Terminate the open connection to the server.
|
---|
146 | *
|
---|
147 | * @returns iprt status code.
|
---|
148 | * @param pServer Handle to the server.
|
---|
149 | */
|
---|
150 | RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer);
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Terminates an open client connect when using RTTcpListen2
|
---|
154 | *
|
---|
155 | * @returns IPRT status code.
|
---|
156 | * @param hClientSocket The client socket handle. This will be invalid upon
|
---|
157 | * return, whether successful or not. NIL is quietly
|
---|
158 | * ignored (VINF_SUCCESS).
|
---|
159 | */
|
---|
160 | RTR3DECL(int) RTTcpServerDisconnectClient2(RTSOCKET hClientSocket);
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Shuts down the server, leaving client connections open.
|
---|
164 | *
|
---|
165 | * @returns IPRT status code.
|
---|
166 | * @param pServer Handle to the server.
|
---|
167 | */
|
---|
168 | RTR3DECL(int) RTTcpServerShutdown(PRTTCPSERVER pServer);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Connect (as a client) to a TCP Server.
|
---|
172 | *
|
---|
173 | * @returns iprt status code.
|
---|
174 | * @param pszAddress The address to connect to.
|
---|
175 | * @param uPort The port to connect to.
|
---|
176 | * @param pSock Where to store the handle to the established connection.
|
---|
177 | */
|
---|
178 | RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock);
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Close a socket returned by RTTcpClientConnect().
|
---|
182 | *
|
---|
183 | * @returns iprt status code.
|
---|
184 | * @param Sock Socket descriptor.
|
---|
185 | */
|
---|
186 | RTR3DECL(int) RTTcpClientClose(RTSOCKET Sock);
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Receive data from a socket.
|
---|
190 | *
|
---|
191 | * @returns iprt status code.
|
---|
192 | * @param Sock Socket descriptor.
|
---|
193 | * @param pvBuffer Where to put the data we read.
|
---|
194 | * @param cbBuffer Read buffer size.
|
---|
195 | * @param pcbRead Number of bytes read.
|
---|
196 | * If NULL the entire buffer will be filled upon successful return.
|
---|
197 | * If not NULL a partial read can be done successfully.
|
---|
198 | */
|
---|
199 | RTR3DECL(int) RTTcpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Send data to a socket.
|
---|
203 | *
|
---|
204 | * @returns iprt status code.
|
---|
205 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
206 | *
|
---|
207 | * @param Sock Socket descriptor.
|
---|
208 | * @param pvBuffer Buffer to write data to socket.
|
---|
209 | * @param cbBuffer How much to write.
|
---|
210 | */
|
---|
211 | RTR3DECL(int) RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer);
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Flush socket write buffers.
|
---|
215 | *
|
---|
216 | * @returns iprt status code.
|
---|
217 | * @param Sock Socket descriptor.
|
---|
218 | */
|
---|
219 | RTR3DECL(int) RTTcpFlush(RTSOCKET Sock);
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Enables or disable delaying sends to coalesce packets.
|
---|
223 | *
|
---|
224 | * The TCP/IP stack usually uses the Nagle algorithm (RFC 896) to implement the
|
---|
225 | * coalescing.
|
---|
226 | *
|
---|
227 | * @returns iprt status code.
|
---|
228 | * @param Sock Socket descriptor.
|
---|
229 | */
|
---|
230 | RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET Sock, bool fEnable);
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Socket I/O multiplexing.
|
---|
234 | * Checks if the socket is ready for reading.
|
---|
235 | *
|
---|
236 | * @returns iprt status code.
|
---|
237 | * @param Sock Socket descriptor.
|
---|
238 | * @param cMillies Number of milliseconds to wait for the socket.
|
---|
239 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
240 | */
|
---|
241 | RTR3DECL(int) RTTcpSelectOne(RTSOCKET Sock, RTMSINTERVAL cMillies);
|
---|
242 |
|
---|
243 |
|
---|
244 | #if 0 /* skipping these for now - RTTcpServer* handles this. */
|
---|
245 | /**
|
---|
246 | * Listen for connection on a socket.
|
---|
247 | *
|
---|
248 | * @returns iprt status code.
|
---|
249 | * @param Sock Socket descriptor.
|
---|
250 | * @param cBackLog The maximum length the queue of pending connections
|
---|
251 | * may grow to.
|
---|
252 | */
|
---|
253 | RTR3DECL(int) RTTcpListen(RTSOCKET Sock, int cBackLog);
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Accept a connection on a socket.
|
---|
257 | *
|
---|
258 | * @returns iprt status code.
|
---|
259 | * @param Sock Socket descriptor.
|
---|
260 | * @param uPort The port for accepting connection.
|
---|
261 | * @param pSockAccepted Where to store the handle to the accepted connection.
|
---|
262 | */
|
---|
263 | RTR3DECL(int) RTTcpAccept(RTSOCKET Sock, unsigned uPort, PRTSOCKET pSockAccepted);
|
---|
264 |
|
---|
265 | #endif
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Gets the address of the local side.
|
---|
269 | *
|
---|
270 | * @returns IPRT status code.
|
---|
271 | * @param Sock Socket descriptor.
|
---|
272 | * @param pAddr Where to store the local address on success.
|
---|
273 | */
|
---|
274 | RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr);
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Gets the address of the other party.
|
---|
278 | *
|
---|
279 | * @returns IPRT status code.
|
---|
280 | * @param Sock Socket descriptor.
|
---|
281 | * @param pAddr Where to store the peer address on success.
|
---|
282 | */
|
---|
283 | RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr);
|
---|
284 |
|
---|
285 | /** @} */
|
---|
286 | RT_C_DECLS_END
|
---|
287 |
|
---|
288 | #endif
|
---|
289 |
|
---|