1 | /** @file
|
---|
2 | * IPRT - Network Sockets.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2011 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_socket_h
|
---|
27 | #define ___iprt_socket_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 RTSocket APIs available Ring-0 Host Context!"
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | RT_C_DECLS_BEGIN
|
---|
41 |
|
---|
42 | /** @defgroup grp_rt_tcp RTSocket - Network Sockets
|
---|
43 | * @ingroup grp_rt
|
---|
44 | * @{
|
---|
45 | */
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Retains a reference to the socket handle.
|
---|
49 | *
|
---|
50 | * @returns New reference count, UINT32_MAX on invalid handle (asserted).
|
---|
51 | *
|
---|
52 | * @param hSocket The socket handle.
|
---|
53 | */
|
---|
54 | RTDECL(uint32_t) RTSocketRetain(RTSOCKET hSocket);
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Release a reference to the socket handle.
|
---|
58 | *
|
---|
59 | * When the reference count reaches zero, the socket handle is shut down and
|
---|
60 | * destroyed. This will not be graceful shutdown, use the protocol specific
|
---|
61 | * close method if this is desired.
|
---|
62 | *
|
---|
63 | * @returns New reference count, UINT32_MAX on invalid handle (asserted).
|
---|
64 | *
|
---|
65 | * @param hSocket The socket handle. The NIL handle is quietly
|
---|
66 | * ignored and 0 is returned.
|
---|
67 | */
|
---|
68 | RTDECL(uint32_t) RTSocketRelease(RTSOCKET hSocket);
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Shuts down the socket, close it and then release one handle reference.
|
---|
72 | *
|
---|
73 | * This is slightly different from RTSocketRelease which will first do the
|
---|
74 | * shutting down and closing when the reference count reaches zero.
|
---|
75 | *
|
---|
76 | * @returns IPRT status code.
|
---|
77 | * @param hSocket The socket handle. NIL is ignored.
|
---|
78 | *
|
---|
79 | * @remarks This will not perform a graceful shutdown of the socket, it will
|
---|
80 | * just destroy it. Use the protocol specific close method if this is
|
---|
81 | * desired.
|
---|
82 | */
|
---|
83 | RTDECL(int) RTSocketClose(RTSOCKET hSocket);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Creates an IPRT socket handle from a native one.
|
---|
87 | *
|
---|
88 | * Do NOT use the native handle after passing it to this function, IPRT owns it
|
---|
89 | * and might even have closed upon a successful return.
|
---|
90 | *
|
---|
91 | * @returns IPRT status code.
|
---|
92 | * @param phSocket Where to store the IPRT socket handle.
|
---|
93 | * @param uNative The native handle.
|
---|
94 | */
|
---|
95 | RTDECL(int) RTSocketFromNative(PRTSOCKET phSocket, RTHCINTPTR uNative);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Gets the native socket handle.
|
---|
99 | *
|
---|
100 | * @returns The native socket handle or RTHCUINTPTR_MAX if not invalid.
|
---|
101 | * @param hSocket The socket handle.
|
---|
102 | */
|
---|
103 | RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Helper that ensures the correct inheritability of a socket.
|
---|
107 | *
|
---|
108 | * We're currently ignoring failures.
|
---|
109 | *
|
---|
110 | * @returns IPRT status code
|
---|
111 | * @param hSocket The socket handle.
|
---|
112 | * @param fInheritable The desired inheritability state.
|
---|
113 | */
|
---|
114 | RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Parse Internet style addresses, getting a generic IPRT network address.
|
---|
118 | *
|
---|
119 | * @returns IPRT status code
|
---|
120 | * @param pszAddress Name or IP address.
|
---|
121 | * @param uPort Port number (host byte order).
|
---|
122 | * @param pAddr Where to return the generic IPRT network address.
|
---|
123 | */
|
---|
124 | RTDECL(int) RTSocketParseInetAddress(const char *pszAddress, unsigned uPort, PRTNETADDR pAddr);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Receive data from a socket.
|
---|
128 | *
|
---|
129 | * @returns IPRT status code.
|
---|
130 | * @param hSocket The socket handle.
|
---|
131 | * @param pvBuffer Where to put the data we read.
|
---|
132 | * @param cbBuffer Read buffer size.
|
---|
133 | * @param pcbRead Number of bytes read. If NULL the entire buffer
|
---|
134 | * will be filled upon successful return. If not NULL a
|
---|
135 | * partial read can be done successfully.
|
---|
136 | */
|
---|
137 | RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Receive data from a socket, including sender address. Mainly useful
|
---|
141 | * for datagram sockets.
|
---|
142 | *
|
---|
143 | * @returns IPRT status code.
|
---|
144 | * @param hSocket The socket handle.
|
---|
145 | * @param pvBuffer Where to put the data we read.
|
---|
146 | * @param cbBuffer Read buffer size.
|
---|
147 | * @param pcbRead Number of bytes read. Must be non-NULL.
|
---|
148 | * @param pSrcAddr Pointer to sender address buffer. May be NULL.
|
---|
149 | */
|
---|
150 | RTDECL(int) RTSocketReadFrom(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr);
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Send data to a socket.
|
---|
154 | *
|
---|
155 | * @returns IPRT status code.
|
---|
156 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
157 | *
|
---|
158 | * @param hSocket The socket handle.
|
---|
159 | * @param pvBuffer Buffer to write data to socket.
|
---|
160 | * @param cbBuffer How much to write.
|
---|
161 | */
|
---|
162 | RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer);
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Send data to a socket, including destination address. Mainly useful
|
---|
166 | * for datagram sockets.
|
---|
167 | *
|
---|
168 | * @returns IPRT status code.
|
---|
169 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
170 | *
|
---|
171 | * @param hSocket The socket handle.
|
---|
172 | * @param pvBuffer Buffer to write data to socket.
|
---|
173 | * @param cbBuffer How much to write.
|
---|
174 | * @param pDstAddr Pointer to destination address. May be NULL.
|
---|
175 | */
|
---|
176 | RTDECL(int) RTSocketWriteTo(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pDstAddr);
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Checks if the socket is ready for reading (for I/O multiplexing).
|
---|
180 | *
|
---|
181 | * @returns IPRT status code.
|
---|
182 | * @param hSocket The socket handle.
|
---|
183 | * @param cMillies Number of milliseconds to wait for the socket. Use
|
---|
184 | * RT_INDEFINITE_WAIT to wait for ever.
|
---|
185 | */
|
---|
186 | RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
|
---|
187 |
|
---|
188 | /** @name Select events
|
---|
189 | * @{ */
|
---|
190 | /** Readable without blocking. */
|
---|
191 | #define RTSOCKET_EVT_READ RT_BIT_32(0)
|
---|
192 | /** Writable without blocking. */
|
---|
193 | #define RTSOCKET_EVT_WRITE RT_BIT_32(1)
|
---|
194 | /** Error condition, hangup, exception or similar. */
|
---|
195 | #define RTSOCKET_EVT_ERROR RT_BIT_32(2)
|
---|
196 | /** Mask of the valid bits. */
|
---|
197 | #define RTSOCKET_EVT_VALID_MASK UINT32_C(0x00000007)
|
---|
198 | /** @} */
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Socket I/O multiplexing
|
---|
202 | * Checks if the socket is ready for one of the given events.
|
---|
203 | *
|
---|
204 | * @returns iprt status code.
|
---|
205 | * @param Sock Socket descriptor.
|
---|
206 | * @param fEvents Event mask to wait for.
|
---|
207 | * @param pfEvents Where to store the event mask on return.
|
---|
208 | * @param cMillies Number of milliseconds to wait for the socket.
|
---|
209 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
210 | */
|
---|
211 | RTR3DECL(int) RTSocketSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
|
---|
212 | RTMSINTERVAL cMillies);
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Shuts down one or both directions of communciation.
|
---|
216 | *
|
---|
217 | * @returns IPRT status code.
|
---|
218 | * @param hSocket The socket handle.
|
---|
219 | * @param fRead Whether to shutdown our read direction.
|
---|
220 | * @param fWrite Whether to shutdown our write direction.
|
---|
221 | */
|
---|
222 | RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite);
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Gets the address of the local side.
|
---|
226 | *
|
---|
227 | * @returns IPRT status code.
|
---|
228 | * @param Sock Socket descriptor.
|
---|
229 | * @param pAddr Where to store the local address on success.
|
---|
230 | */
|
---|
231 | RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Gets the address of the other party.
|
---|
235 | *
|
---|
236 | * @returns IPRT status code.
|
---|
237 | * @param Sock Socket descriptor.
|
---|
238 | * @param pAddr Where to store the peer address on success.
|
---|
239 | */
|
---|
240 | RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Send data from a scatter/gather buffer to a socket.
|
---|
244 | *
|
---|
245 | * @returns IPRT status code.
|
---|
246 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
247 | *
|
---|
248 | * @param hSocket The socket handle.
|
---|
249 | * @param pSgBuf Scatter/gather buffer to write data to socket.
|
---|
250 | */
|
---|
251 | RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Send data from multiple buffers to a socket.
|
---|
255 | *
|
---|
256 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
257 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
258 | * in the execl libc API.
|
---|
259 | *
|
---|
260 | * @returns IPRT status code.
|
---|
261 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
262 | *
|
---|
263 | * @param hSocket The socket handle.
|
---|
264 | * @param cSegs The number of data segments in the following
|
---|
265 | * ellipsis.
|
---|
266 | * @param ... Pairs of buffer pointers (void const *) and buffer
|
---|
267 | * sizes (size_t). Make 101% sure the pointer is
|
---|
268 | * really size_t.
|
---|
269 | */
|
---|
270 | RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Send data from multiple buffers to a socket.
|
---|
274 | *
|
---|
275 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
276 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
277 | * in the execl libc API.
|
---|
278 | *
|
---|
279 | * @returns IPRT status code.
|
---|
280 | * @retval VERR_INTERRUPTED if interrupted before anything was written.
|
---|
281 | *
|
---|
282 | * @param hSocket The socket handle.
|
---|
283 | * @param cSegs The number of data segments in the following
|
---|
284 | * argument list.
|
---|
285 | * @param va Pairs of buffer pointers (void const *) and buffer
|
---|
286 | * sizes (size_t). Make 101% sure the pointer is
|
---|
287 | * really size_t.
|
---|
288 | */
|
---|
289 | RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Receive data from a socket.
|
---|
293 | *
|
---|
294 | * This version doesn't block if there is no data on the socket.
|
---|
295 | *
|
---|
296 | * @returns IPRT status code.
|
---|
297 | *
|
---|
298 | * @param hSocket The socket handle.
|
---|
299 | * @param pvBuffer Where to put the data we read.
|
---|
300 | * @param cbBuffer Read buffer size.
|
---|
301 | * @param pcbRead Number of bytes read.
|
---|
302 | */
|
---|
303 | RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Send data to a socket.
|
---|
307 | *
|
---|
308 | * This version doesn't block if there is not enough room for the message.
|
---|
309 | *
|
---|
310 | * @returns IPRT status code.
|
---|
311 | *
|
---|
312 | * @param hSocket The socket handle.
|
---|
313 | * @param pvBuffer Buffer to write data to socket.
|
---|
314 | * @param cbBuffer How much to write.
|
---|
315 | * @param pcbWritten Number of bytes written.
|
---|
316 | */
|
---|
317 | RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Send data from a scatter/gather buffer to a socket.
|
---|
321 | *
|
---|
322 | * This version doesn't block if there is not enough room for the message.
|
---|
323 | *
|
---|
324 | * @returns iprt status code.
|
---|
325 | *
|
---|
326 | * @param Sock Socket descriptor.
|
---|
327 | * @param pSgBuf Scatter/gather buffer to write data to socket.
|
---|
328 | * @param pcbWritten Number of bytes written.
|
---|
329 | */
|
---|
330 | RTR3DECL(int) RTSocketSgWriteNB(RTSOCKET Sock, PCRTSGBUF pSgBuf, size_t *pcbWritten);
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Send data from multiple buffers to a socket.
|
---|
335 | *
|
---|
336 | * This version doesn't block if there is not enough room for the message.
|
---|
337 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
338 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
339 | * in the execl libc API.
|
---|
340 | *
|
---|
341 | * @returns IPRT status code.
|
---|
342 | *
|
---|
343 | * @param hSocket The socket handle.
|
---|
344 | * @param cSegs The number of data segments in the following
|
---|
345 | * ellipsis.
|
---|
346 | * @param pcbWritten Number of bytes written.
|
---|
347 | * @param ... Pairs of buffer pointers (void const *) and buffer
|
---|
348 | * sizes (size_t). Make 101% sure the pointer is
|
---|
349 | * really size_t.
|
---|
350 | */
|
---|
351 | RTR3DECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Send data from multiple buffers to a socket.
|
---|
355 | *
|
---|
356 | * This version doesn't block if there is not enough room for the message.
|
---|
357 | * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
|
---|
358 | * for lazy coders. The "L" in the function name is short for "list" just like
|
---|
359 | * in the execl libc API.
|
---|
360 | *
|
---|
361 | * @returns IPRT status code.
|
---|
362 | *
|
---|
363 | * @param hSocket The socket handle.
|
---|
364 | * @param cSegs The number of data segments in the following
|
---|
365 | * argument list.
|
---|
366 | * @param pcbWritten Number of bytes written.
|
---|
367 | * @param va Pairs of buffer pointers (void const *) and buffer
|
---|
368 | * sizes (size_t). Make 101% sure the pointer is
|
---|
369 | * really size_t.
|
---|
370 | */
|
---|
371 | RTR3DECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
|
---|
372 |
|
---|
373 | /** @} */
|
---|
374 | RT_C_DECLS_END
|
---|
375 |
|
---|
376 | #endif
|
---|
377 |
|
---|