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