VirtualBox

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

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