VirtualBox

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

Last change on this file since 86716 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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