VirtualBox

source: vbox/trunk/include/iprt/socket.h@ 68113

Last change on this file since 68113 was 62473, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/** @file
2 * IPRT - Network Sockets.
3 */
4
5/*
6 * Copyright (C) 2006-2016 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
40RT_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 */
57RTDECL(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 */
71RTDECL(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 */
86RTDECL(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 */
98RTDECL(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 */
106RTDECL(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 */
117RTDECL(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 */
130RTDECL(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 */
149RTDECL(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 */
162RTDECL(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 */
175RTDECL(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 */
187RTDECL(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 */
201RTDECL(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 */
211RTDECL(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 hSocket The Socket handle.
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. Use
234 * RT_INDEFINITE_WAIT to wait for ever.
235 */
236RTR3DECL(int) RTSocketSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies);
237
238/**
239 * Shuts down one or both directions of communciation.
240 *
241 * @returns IPRT status code.
242 * @param hSocket The socket handle.
243 * @param fRead Whether to shutdown our read direction.
244 * @param fWrite Whether to shutdown our write direction.
245 */
246RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite);
247
248/**
249 * Gets the address of the local side.
250 *
251 * @returns IPRT status code.
252 * @param hSocket The Socket handle.
253 * @param pAddr Where to store the local address on success.
254 */
255RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
256
257/**
258 * Gets the address of the other party.
259 *
260 * @returns IPRT status code.
261 * @param hSocket The Socket handle.
262 * @param pAddr Where to store the peer address on success.
263 */
264RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
265
266/**
267 * Send data from a scatter/gather buffer to a socket.
268 *
269 * @returns IPRT status code.
270 * @retval VERR_INTERRUPTED if interrupted before anything was written.
271 *
272 * @param hSocket The socket handle.
273 * @param pSgBuf Scatter/gather buffer to write data to socket.
274 */
275RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
276
277/**
278 * Send data from multiple buffers to a socket.
279 *
280 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
281 * for lazy coders. The "L" in the function name is short for "list" just like
282 * in the execl libc API.
283 *
284 * @returns IPRT status code.
285 * @retval VERR_INTERRUPTED if interrupted before anything was written.
286 *
287 * @param hSocket The socket handle.
288 * @param cSegs The number of data segments in the following
289 * ellipsis.
290 * @param ... Pairs of buffer pointers (void const *) and buffer
291 * sizes (size_t). Make 101% sure the pointer is
292 * really size_t.
293 */
294RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
295
296/**
297 * Send data from multiple buffers to a socket.
298 *
299 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
300 * for lazy coders. The "L" in the function name is short for "list" just like
301 * in the execl libc API.
302 *
303 * @returns IPRT status code.
304 * @retval VERR_INTERRUPTED if interrupted before anything was written.
305 *
306 * @param hSocket The socket handle.
307 * @param cSegs The number of data segments in the following
308 * argument list.
309 * @param va Pairs of buffer pointers (void const *) and buffer
310 * sizes (size_t). Make 101% sure the pointer is
311 * really size_t.
312 */
313RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
314
315/**
316 * Receive data from a socket.
317 *
318 * This version doesn't block if there is no data on the socket.
319 *
320 * @returns IPRT status code.
321 *
322 * @param hSocket The socket handle.
323 * @param pvBuffer Where to put the data we read.
324 * @param cbBuffer Read buffer size.
325 * @param pcbRead Number of bytes read.
326 */
327RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
328
329/**
330 * Send data to a socket.
331 *
332 * This version doesn't block if there is not enough room for the message.
333 *
334 * @returns IPRT status code.
335 *
336 * @param hSocket The socket handle.
337 * @param pvBuffer Buffer to write data to socket.
338 * @param cbBuffer How much to write.
339 * @param pcbWritten Number of bytes written.
340 */
341RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
342
343/**
344 * Send data to a socket, including destination address. Mainly useful
345 * for datagram sockets.
346 *
347 * This version doesn't block if there is not enough room for the message.
348 *
349 * @returns IPRT status code.
350 * @retval VERR_INTERRUPTED if interrupted before anything was written.
351 *
352 * @param hSocket The socket handle.
353 * @param pvBuffer Buffer to write data to socket.
354 * @param cbBuffer How much to write.
355 * @param pDstAddr Pointer to destination address. May be NULL.
356 */
357RTDECL(int) RTSocketWriteToNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pDstAddr);
358
359/**
360 * Send data from a scatter/gather buffer to a socket.
361 *
362 * This version doesn't block if there is not enough room for the message.
363 *
364 * @returns iprt status code.
365 *
366 * @param hSocket The Socket handle.
367 * @param pSgBuf Scatter/gather buffer to write data to socket.
368 * @param pcbWritten Number of bytes written.
369 */
370RTR3DECL(int) RTSocketSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten);
371
372
373/**
374 * Send data from multiple buffers to a socket.
375 *
376 * This version doesn't block if there is not enough room for the message.
377 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
378 * for lazy coders. The "L" in the function name is short for "list" just like
379 * in the execl libc API.
380 *
381 * @returns IPRT status code.
382 *
383 * @param hSocket The socket handle.
384 * @param cSegs The number of data segments in the following
385 * ellipsis.
386 * @param pcbWritten Number of bytes written.
387 * @param ... Pairs of buffer pointers (void const *) and buffer
388 * sizes (size_t). Make 101% sure the pointer is
389 * really size_t.
390 */
391RTR3DECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
392
393/**
394 * Send data from multiple buffers to a socket.
395 *
396 * This version doesn't block if there is not enough room for the message.
397 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
398 * for lazy coders. The "L" in the function name is short for "list" just like
399 * in the execl libc API.
400 *
401 * @returns IPRT status code.
402 *
403 * @param hSocket The socket handle.
404 * @param cSegs The number of data segments in the following
405 * argument list.
406 * @param pcbWritten Number of bytes written.
407 * @param va Pairs of buffer pointers (void const *) and buffer
408 * sizes (size_t). Make 101% sure the pointer is
409 * really size_t.
410 */
411RTR3DECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
412
413/** @} */
414RT_C_DECLS_END
415
416#endif
417
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette