VirtualBox

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

Last change on this file since 43171 was 43171, checked in by vboxsync, 12 years ago

Runtime: add IPv6 parsing/resolving functions (contributed by Oliver Loch)
ExtPacks/VNC: add IPv6 support (contributed by Oliver Loch)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/** @file
2 * IPRT - Network Sockets.
3 */
4
5/*
6 * Copyright (C) 2006-2012 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_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 */
54RTDECL(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 */
68RTDECL(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 */
83RTDECL(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 */
95RTDECL(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 */
103RTDECL(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 */
114RTDECL(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. NULL or empty string (no
121 * spaces) is taken to mean INADDR_ANY, which is
122 * meaningful when binding a server socket for
123 * instance.
124 * @param uPort Port number (host byte order).
125 * @param pAddr Where to return the generic IPRT network address.
126 */
127RTDECL(int) RTSocketParseInetAddress(const char *pszAddress, unsigned uPort, PRTNETADDR pAddr);
128
129/**
130 * Gets the ip addresses of a hostname via getaddrinfo()
131 * Returns only the first result for the moment
132 * This will change with the upcoming IPv6 struct.
133 *
134 * @returns IPRT status code.
135 * @param psz szString we want to lookup
136 * @param pszResult - memory used to write the result to
137 * @param resultSize - size of pszResult in bytes.
138 * Holds size of string of the returned
139 * address on out
140 * @param pAddrType Which address to lookup, valid values are:
141 * RTNETADDRTYPE_IPV4 -> lookup AF_INET
142 * RTNETADDRTYPE_IPV6 -> lookup AF_INET6
143 * RTNETADDRTYPE_INVALID,NULL,... -> lookup anything
144 */
145RTDECL(int) RTSocketGetAddrInfo(const char *psz, char *pszResult, size_t *resultSize, PRTNETADDRTYPE pAddrType);
146
147/**
148 * Receive data from a socket.
149 *
150 * @returns IPRT status code.
151 * @param hSocket The socket handle.
152 * @param pvBuffer Where to put the data we read.
153 * @param cbBuffer Read buffer size.
154 * @param pcbRead Number of bytes read. If NULL the entire buffer
155 * will be filled upon successful return. If not NULL a
156 * partial read can be done successfully.
157 */
158RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
159
160/**
161 * Receive data from a socket, including sender address. Mainly useful
162 * for datagram sockets.
163 *
164 * @returns IPRT status code.
165 * @param hSocket The socket handle.
166 * @param pvBuffer Where to put the data we read.
167 * @param cbBuffer Read buffer size.
168 * @param pcbRead Number of bytes read. Must be non-NULL.
169 * @param pSrcAddr Pointer to sender address buffer. May be NULL.
170 */
171RTDECL(int) RTSocketReadFrom(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr);
172
173/**
174 * Send data to a socket.
175 *
176 * @returns IPRT status code.
177 * @retval VERR_INTERRUPTED if interrupted before anything was written.
178 *
179 * @param hSocket The socket handle.
180 * @param pvBuffer Buffer to write data to socket.
181 * @param cbBuffer How much to write.
182 */
183RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer);
184
185/**
186 * Send data to a socket, including destination address. Mainly useful
187 * for datagram sockets.
188 *
189 * @returns IPRT status code.
190 * @retval VERR_INTERRUPTED if interrupted before anything was written.
191 *
192 * @param hSocket The socket handle.
193 * @param pvBuffer Buffer to write data to socket.
194 * @param cbBuffer How much to write.
195 * @param pDstAddr Pointer to destination address. May be NULL.
196 */
197RTDECL(int) RTSocketWriteTo(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pDstAddr);
198
199/**
200 * Checks if the socket is ready for reading (for I/O multiplexing).
201 *
202 * @returns IPRT status code.
203 * @param hSocket The socket handle.
204 * @param cMillies Number of milliseconds to wait for the socket. Use
205 * RT_INDEFINITE_WAIT to wait for ever.
206 */
207RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies);
208
209/** @name Select events
210 * @{ */
211/** Readable without blocking. */
212#define RTSOCKET_EVT_READ RT_BIT_32(0)
213/** Writable without blocking. */
214#define RTSOCKET_EVT_WRITE RT_BIT_32(1)
215/** Error condition, hangup, exception or similar. */
216#define RTSOCKET_EVT_ERROR RT_BIT_32(2)
217/** Mask of the valid bits. */
218#define RTSOCKET_EVT_VALID_MASK UINT32_C(0x00000007)
219/** @} */
220
221/**
222 * Socket I/O multiplexing
223 * Checks if the socket is ready for one of the given events.
224 *
225 * @returns iprt status code.
226 * @param Sock Socket descriptor.
227 * @param fEvents Event mask to wait for.
228 * @param pfEvents Where to store the event mask on return.
229 * @param cMillies Number of milliseconds to wait for the socket.
230 * Use RT_INDEFINITE_WAIT to wait for ever.
231 */
232RTR3DECL(int) RTSocketSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
233 RTMSINTERVAL cMillies);
234
235/**
236 * Shuts down one or both directions of communciation.
237 *
238 * @returns IPRT status code.
239 * @param hSocket The socket handle.
240 * @param fRead Whether to shutdown our read direction.
241 * @param fWrite Whether to shutdown our write direction.
242 */
243RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite);
244
245/**
246 * Gets the address of the local side.
247 *
248 * @returns IPRT status code.
249 * @param Sock Socket descriptor.
250 * @param pAddr Where to store the local address on success.
251 */
252RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
253
254/**
255 * Gets the address of the other party.
256 *
257 * @returns IPRT status code.
258 * @param Sock Socket descriptor.
259 * @param pAddr Where to store the peer address on success.
260 */
261RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr);
262
263/**
264 * Send data from a scatter/gather buffer to a socket.
265 *
266 * @returns IPRT status code.
267 * @retval VERR_INTERRUPTED if interrupted before anything was written.
268 *
269 * @param hSocket The socket handle.
270 * @param pSgBuf Scatter/gather buffer to write data to socket.
271 */
272RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf);
273
274/**
275 * Send data from multiple buffers to a socket.
276 *
277 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
278 * for lazy coders. The "L" in the function name is short for "list" just like
279 * in the execl libc API.
280 *
281 * @returns IPRT status code.
282 * @retval VERR_INTERRUPTED if interrupted before anything was written.
283 *
284 * @param hSocket The socket handle.
285 * @param cSegs The number of data segments in the following
286 * ellipsis.
287 * @param ... Pairs of buffer pointers (void const *) and buffer
288 * sizes (size_t). Make 101% sure the pointer is
289 * really size_t.
290 */
291RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...);
292
293/**
294 * Send data from multiple buffers to a socket.
295 *
296 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
297 * for lazy coders. The "L" in the function name is short for "list" just like
298 * in the execl libc API.
299 *
300 * @returns IPRT status code.
301 * @retval VERR_INTERRUPTED if interrupted before anything was written.
302 *
303 * @param hSocket The socket handle.
304 * @param cSegs The number of data segments in the following
305 * argument list.
306 * @param va Pairs of buffer pointers (void const *) and buffer
307 * sizes (size_t). Make 101% sure the pointer is
308 * really size_t.
309 */
310RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va);
311
312/**
313 * Receive data from a socket.
314 *
315 * This version doesn't block if there is no data on the socket.
316 *
317 * @returns IPRT status code.
318 *
319 * @param hSocket The socket handle.
320 * @param pvBuffer Where to put the data we read.
321 * @param cbBuffer Read buffer size.
322 * @param pcbRead Number of bytes read.
323 */
324RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead);
325
326/**
327 * Send data to a socket.
328 *
329 * This version doesn't block if there is not enough room for the message.
330 *
331 * @returns IPRT status code.
332 *
333 * @param hSocket The socket handle.
334 * @param pvBuffer Buffer to write data to socket.
335 * @param cbBuffer How much to write.
336 * @param pcbWritten Number of bytes written.
337 */
338RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten);
339
340/**
341 * Send data from a scatter/gather buffer to a socket.
342 *
343 * This version doesn't block if there is not enough room for the message.
344 *
345 * @returns iprt status code.
346 *
347 * @param Sock Socket descriptor.
348 * @param pSgBuf Scatter/gather buffer to write data to socket.
349 * @param pcbWritten Number of bytes written.
350 */
351RTR3DECL(int) RTSocketSgWriteNB(RTSOCKET Sock, PCRTSGBUF pSgBuf, size_t *pcbWritten);
352
353
354/**
355 * Send data from multiple buffers to a socket.
356 *
357 * This version doesn't block if there is not enough room for the message.
358 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
359 * for lazy coders. The "L" in the function name is short for "list" just like
360 * in the execl libc API.
361 *
362 * @returns IPRT status code.
363 *
364 * @param hSocket The socket handle.
365 * @param cSegs The number of data segments in the following
366 * ellipsis.
367 * @param pcbWritten Number of bytes written.
368 * @param ... Pairs of buffer pointers (void const *) and buffer
369 * sizes (size_t). Make 101% sure the pointer is
370 * really size_t.
371 */
372RTR3DECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...);
373
374/**
375 * Send data from multiple buffers to a socket.
376 *
377 * This version doesn't block if there is not enough room for the message.
378 * This is convenience wrapper around the RTSocketSgWrite and RTSgBufInit calls
379 * for lazy coders. The "L" in the function name is short for "list" just like
380 * in the execl libc API.
381 *
382 * @returns IPRT status code.
383 *
384 * @param hSocket The socket handle.
385 * @param cSegs The number of data segments in the following
386 * argument list.
387 * @param pcbWritten Number of bytes written.
388 * @param va Pairs of buffer pointers (void const *) and buffer
389 * sizes (size_t). Make 101% sure the pointer is
390 * really size_t.
391 */
392RTR3DECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va);
393
394/** @} */
395RT_C_DECLS_END
396
397#endif
398
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