1 | /* $Id: socket.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Network Sockets.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #ifdef RT_OS_WINDOWS
|
---|
42 | # include <iprt/win/winsock2.h>
|
---|
43 | # include <iprt/win/ws2tcpip.h>
|
---|
44 | #else /* !RT_OS_WINDOWS */
|
---|
45 | # include <errno.h>
|
---|
46 | # include <sys/select.h>
|
---|
47 | # include <sys/stat.h>
|
---|
48 | # include <sys/socket.h>
|
---|
49 | # include <netinet/in.h>
|
---|
50 | # include <netinet/tcp.h>
|
---|
51 | # include <arpa/inet.h>
|
---|
52 | # ifdef IPRT_WITH_TCPIP_V6
|
---|
53 | # include <netinet6/in6.h>
|
---|
54 | # endif
|
---|
55 | # include <sys/un.h>
|
---|
56 | # include <netdb.h>
|
---|
57 | # include <unistd.h>
|
---|
58 | # include <fcntl.h>
|
---|
59 | # include <sys/uio.h>
|
---|
60 | # ifndef AF_LOCAL
|
---|
61 | # define AF_LOCAL AF_UNIX
|
---|
62 | # endif
|
---|
63 | #endif /* !RT_OS_WINDOWS */
|
---|
64 | #include <limits.h>
|
---|
65 |
|
---|
66 | #include "internal/iprt.h"
|
---|
67 | #include <iprt/socket.h>
|
---|
68 |
|
---|
69 | #include <iprt/alloca.h>
|
---|
70 | #include <iprt/asm.h>
|
---|
71 | #include <iprt/assert.h>
|
---|
72 | #include <iprt/ctype.h>
|
---|
73 | #include <iprt/err.h>
|
---|
74 | #include <iprt/mempool.h>
|
---|
75 | #include <iprt/poll.h>
|
---|
76 | #include <iprt/string.h>
|
---|
77 | #include <iprt/thread.h>
|
---|
78 | #include <iprt/time.h>
|
---|
79 | #include <iprt/mem.h>
|
---|
80 | #include <iprt/sg.h>
|
---|
81 | #include <iprt/log.h>
|
---|
82 |
|
---|
83 | #include "internal/magics.h"
|
---|
84 | #include "internal/socket.h"
|
---|
85 | #include "internal/string.h"
|
---|
86 | #ifdef RT_OS_WINDOWS
|
---|
87 | # include "win/internal-r3-win.h"
|
---|
88 | #endif
|
---|
89 |
|
---|
90 |
|
---|
91 | /*********************************************************************************************************************************
|
---|
92 | * Defined Constants And Macros *
|
---|
93 | *********************************************************************************************************************************/
|
---|
94 | /* non-standard linux stuff (it seems). */
|
---|
95 | #ifndef MSG_NOSIGNAL
|
---|
96 | # define MSG_NOSIGNAL 0
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | /* Windows has different names for SHUT_XXX. */
|
---|
100 | #ifndef SHUT_RDWR
|
---|
101 | # ifdef SD_BOTH
|
---|
102 | # define SHUT_RDWR SD_BOTH
|
---|
103 | # else
|
---|
104 | # define SHUT_RDWR 2
|
---|
105 | # endif
|
---|
106 | #endif
|
---|
107 | #ifndef SHUT_WR
|
---|
108 | # ifdef SD_SEND
|
---|
109 | # define SHUT_WR SD_SEND
|
---|
110 | # else
|
---|
111 | # define SHUT_WR 1
|
---|
112 | # endif
|
---|
113 | #endif
|
---|
114 | #ifndef SHUT_RD
|
---|
115 | # ifdef SD_RECEIVE
|
---|
116 | # define SHUT_RD SD_RECEIVE
|
---|
117 | # else
|
---|
118 | # define SHUT_RD 0
|
---|
119 | # endif
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | /* fixup backlevel OSes. */
|
---|
123 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
124 | # define socklen_t int
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | /** How many pending connection. */
|
---|
128 | #define RTTCP_SERVER_BACKLOG 10
|
---|
129 |
|
---|
130 | /* Limit read and write sizes on Windows and OS/2. */
|
---|
131 | #ifdef RT_OS_WINDOWS
|
---|
132 | # define RTSOCKET_MAX_WRITE (INT_MAX / 2)
|
---|
133 | # define RTSOCKET_MAX_READ (INT_MAX / 2)
|
---|
134 | #elif defined(RT_OS_OS2)
|
---|
135 | # define RTSOCKET_MAX_WRITE 0x10000
|
---|
136 | # define RTSOCKET_MAX_READ 0x10000
|
---|
137 | #endif
|
---|
138 |
|
---|
139 |
|
---|
140 | /*********************************************************************************************************************************
|
---|
141 | * Structures and Typedefs *
|
---|
142 | *********************************************************************************************************************************/
|
---|
143 | /**
|
---|
144 | * Socket handle data.
|
---|
145 | *
|
---|
146 | * This is mainly required for implementing RTPollSet on Windows.
|
---|
147 | */
|
---|
148 | typedef struct RTSOCKETINT
|
---|
149 | {
|
---|
150 | /** Magic number (RTSOCKET_MAGIC). */
|
---|
151 | uint32_t u32Magic;
|
---|
152 | /** Exclusive user count.
|
---|
153 | * This is used to prevent two threads from accessing the handle concurrently.
|
---|
154 | * It can be higher than 1 if this handle is reference multiple times in a
|
---|
155 | * polling set (Windows). */
|
---|
156 | uint32_t volatile cUsers;
|
---|
157 | /** The native socket handle. */
|
---|
158 | RTSOCKETNATIVE hNative;
|
---|
159 | /** Indicates whether the handle has been closed or not. */
|
---|
160 | bool volatile fClosed;
|
---|
161 | /** Indicates whether the socket is operating in blocking or non-blocking mode
|
---|
162 | * currently. */
|
---|
163 | bool fBlocking;
|
---|
164 | /** Whether to leave the native socket open rather than closing it (for
|
---|
165 | * RTHandleGetStandard). */
|
---|
166 | bool fLeaveOpen;
|
---|
167 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
168 | /** The pollset currently polling this socket. This is NIL if no one is
|
---|
169 | * polling. */
|
---|
170 | RTPOLLSET hPollSet;
|
---|
171 | #endif
|
---|
172 | #ifdef RT_OS_WINDOWS
|
---|
173 | /** The event semaphore we've associated with the socket handle.
|
---|
174 | * This is WSA_INVALID_EVENT if not done. */
|
---|
175 | WSAEVENT hEvent;
|
---|
176 | /** The events we're polling for. */
|
---|
177 | uint32_t fPollEvts;
|
---|
178 | /** The events we're currently subscribing to with WSAEventSelect.
|
---|
179 | * This is ZERO if we're currently not subscribing to anything. */
|
---|
180 | uint32_t fSubscribedEvts;
|
---|
181 | /** Saved events which are only posted once and events harvested for
|
---|
182 | * sockets entered multiple times into to a poll set. Imagine a scenario where
|
---|
183 | * you have a RTPOLL_EVT_READ entry and RTPOLL_EVT_ERROR entry. The READ
|
---|
184 | * condition can be triggered between checking the READ entry and the ERROR
|
---|
185 | * entry, and we don't want to drop the READ, so we store it here and make sure
|
---|
186 | * the event is signalled.
|
---|
187 | *
|
---|
188 | * The RTPOLL_EVT_ERROR is inconsistenly sticky at the momemnt... */
|
---|
189 | uint32_t fEventsSaved;
|
---|
190 | /** Set if fEventsSaved contains harvested events (used to avoid multiple
|
---|
191 | * calls to rtSocketPollCheck on the same socket during rtSocketPollDone). */
|
---|
192 | bool fHarvestedEvents;
|
---|
193 | /** Set if we're using the polling fallback. */
|
---|
194 | bool fPollFallback;
|
---|
195 | /** Set if the fallback polling is active (event not set). */
|
---|
196 | bool volatile fPollFallbackActive;
|
---|
197 | /** Set to shut down the fallback polling thread. */
|
---|
198 | bool volatile fPollFallbackShutdown;
|
---|
199 | /** Socket use to wake up the select thread. */
|
---|
200 | RTSOCKETNATIVE hPollFallbackNotifyW;
|
---|
201 | /** Socket the select thread always waits on. */
|
---|
202 | RTSOCKETNATIVE hPollFallbackNotifyR;
|
---|
203 | /** The fallback polling thread. */
|
---|
204 | RTTHREAD hPollFallbackThread;
|
---|
205 | #endif /* RT_OS_WINDOWS */
|
---|
206 | } RTSOCKETINT;
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Address union used internally for things like getpeername and getsockname.
|
---|
211 | */
|
---|
212 | typedef union RTSOCKADDRUNION
|
---|
213 | {
|
---|
214 | struct sockaddr Addr;
|
---|
215 | struct sockaddr_in IPv4;
|
---|
216 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
217 | struct sockaddr_in6 IPv6;
|
---|
218 | #endif
|
---|
219 | } RTSOCKADDRUNION;
|
---|
220 |
|
---|
221 |
|
---|
222 | /*********************************************************************************************************************************
|
---|
223 | * Global Variables *
|
---|
224 | *********************************************************************************************************************************/
|
---|
225 | #ifdef RT_OS_WINDOWS
|
---|
226 | /** Indicates that we've successfully initialized winsock. */
|
---|
227 | static uint32_t volatile g_uWinSockInitedVersion = 0;
|
---|
228 | #endif
|
---|
229 |
|
---|
230 |
|
---|
231 | /*********************************************************************************************************************************
|
---|
232 | * Internal Functions *
|
---|
233 | *********************************************************************************************************************************/
|
---|
234 | #ifdef RT_OS_WINDOWS
|
---|
235 | static void rtSocketPokePollFallbackThread(RTSOCKETINT *pThis);
|
---|
236 | #endif
|
---|
237 |
|
---|
238 |
|
---|
239 |
|
---|
240 | #ifdef RT_OS_WINDOWS
|
---|
241 | /**
|
---|
242 | * Initializes winsock for the process.
|
---|
243 | *
|
---|
244 | * @returns IPRT status code.
|
---|
245 | */
|
---|
246 | static int rtSocketInitWinsock(void)
|
---|
247 | {
|
---|
248 | if (g_uWinSockInitedVersion != 0)
|
---|
249 | return VINF_SUCCESS;
|
---|
250 |
|
---|
251 | if ( !g_pfnWSAGetLastError
|
---|
252 | || !g_pfnWSAStartup
|
---|
253 | || !g_pfnsocket
|
---|
254 | || !g_pfnclosesocket)
|
---|
255 | return VERR_NET_INIT_FAILED;
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Initialize winsock. Try with 2.2 and back down till we get something that works.
|
---|
259 | */
|
---|
260 | static const WORD s_awVersions[] =
|
---|
261 | {
|
---|
262 | MAKEWORD(2, 2),
|
---|
263 | MAKEWORD(2, 1),
|
---|
264 | MAKEWORD(2, 0),
|
---|
265 | MAKEWORD(1, 1),
|
---|
266 | MAKEWORD(1, 0),
|
---|
267 | };
|
---|
268 | for (uint32_t i = 0; i < RT_ELEMENTS(s_awVersions); i++)
|
---|
269 | {
|
---|
270 | WSADATA wsaData;
|
---|
271 | RT_ZERO(wsaData);
|
---|
272 | int rcWsa = g_pfnWSAStartup(s_awVersions[i], &wsaData);
|
---|
273 | if (rcWsa == 0)
|
---|
274 | {
|
---|
275 | /* AssertMsg(wsaData.wVersion >= s_awVersions[i]); - triggers with winsock 1.1 */
|
---|
276 | ASMAtomicWriteU32(&g_uWinSockInitedVersion, wsaData.wVersion);
|
---|
277 | return VINF_SUCCESS;
|
---|
278 | }
|
---|
279 | AssertLogRelMsg(rcWsa == WSAVERNOTSUPPORTED, ("rcWsa=%d (winsock version %#x)\n", rcWsa, s_awVersions[i]));
|
---|
280 | }
|
---|
281 | LogRel(("Failed to init winsock!\n"));
|
---|
282 | return VERR_NET_INIT_FAILED;
|
---|
283 | }
|
---|
284 | #endif
|
---|
285 |
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Get the last error as an iprt status code.
|
---|
289 | *
|
---|
290 | * @returns IPRT status code.
|
---|
291 | */
|
---|
292 | DECLINLINE(int) rtSocketError(void)
|
---|
293 | {
|
---|
294 | #ifdef RT_OS_WINDOWS
|
---|
295 | if (g_pfnWSAGetLastError)
|
---|
296 | return RTErrConvertFromWin32(g_pfnWSAGetLastError());
|
---|
297 | return VERR_NET_IO_ERROR;
|
---|
298 | #else
|
---|
299 | return RTErrConvertFromErrno(errno);
|
---|
300 | #endif
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Resets the last error.
|
---|
306 | */
|
---|
307 | DECLINLINE(void) rtSocketErrorReset(void)
|
---|
308 | {
|
---|
309 | #ifdef RT_OS_WINDOWS
|
---|
310 | if (g_pfnWSASetLastError)
|
---|
311 | g_pfnWSASetLastError(0);
|
---|
312 | #else
|
---|
313 | errno = 0;
|
---|
314 | #endif
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Get the last resolver error as an iprt status code.
|
---|
320 | *
|
---|
321 | * @returns iprt status code.
|
---|
322 | */
|
---|
323 | DECLHIDDEN(int) rtSocketResolverError(void)
|
---|
324 | {
|
---|
325 | #ifdef RT_OS_WINDOWS
|
---|
326 | if (g_pfnWSAGetLastError)
|
---|
327 | return RTErrConvertFromWin32(g_pfnWSAGetLastError());
|
---|
328 | return VERR_UNRESOLVED_ERROR;
|
---|
329 | #else
|
---|
330 | switch (h_errno)
|
---|
331 | {
|
---|
332 | case HOST_NOT_FOUND:
|
---|
333 | return VERR_NET_HOST_NOT_FOUND;
|
---|
334 | case NO_DATA:
|
---|
335 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
336 | case NO_RECOVERY:
|
---|
337 | return VERR_IO_GEN_FAILURE;
|
---|
338 | case TRY_AGAIN:
|
---|
339 | return VERR_TRY_AGAIN;
|
---|
340 |
|
---|
341 | default:
|
---|
342 | AssertLogRelMsgFailed(("Unhandled error %u\n", h_errno));
|
---|
343 | return VERR_UNRESOLVED_ERROR;
|
---|
344 | }
|
---|
345 | #endif
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Converts from a native socket address to a generic IPRT network address.
|
---|
351 | *
|
---|
352 | * @returns IPRT status code.
|
---|
353 | * @param pSrc The source address.
|
---|
354 | * @param cbSrc The size of the source address.
|
---|
355 | * @param pAddr Where to return the generic IPRT network
|
---|
356 | * address.
|
---|
357 | */
|
---|
358 | static int rtSocketNetAddrFromAddr(RTSOCKADDRUNION const *pSrc, size_t cbSrc, PRTNETADDR pAddr)
|
---|
359 | {
|
---|
360 | /*
|
---|
361 | * Convert the address.
|
---|
362 | */
|
---|
363 | if ( cbSrc == sizeof(struct sockaddr_in)
|
---|
364 | && pSrc->Addr.sa_family == AF_INET)
|
---|
365 | {
|
---|
366 | RT_ZERO(*pAddr);
|
---|
367 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
368 | pAddr->uPort = RT_N2H_U16(pSrc->IPv4.sin_port);
|
---|
369 | pAddr->uAddr.IPv4.u = pSrc->IPv4.sin_addr.s_addr;
|
---|
370 | }
|
---|
371 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
372 | else if ( cbSrc == sizeof(struct sockaddr_in6)
|
---|
373 | && pSrc->Addr.sa_family == AF_INET6)
|
---|
374 | {
|
---|
375 | RT_ZERO(*pAddr);
|
---|
376 | pAddr->enmType = RTNETADDRTYPE_IPV6;
|
---|
377 | pAddr->uPort = RT_N2H_U16(pSrc->IPv6.sin6_port);
|
---|
378 | pAddr->uAddr.IPv6.au32[0] = pSrc->IPv6.sin6_addr.s6_addr32[0];
|
---|
379 | pAddr->uAddr.IPv6.au32[1] = pSrc->IPv6.sin6_addr.s6_addr32[1];
|
---|
380 | pAddr->uAddr.IPv6.au32[2] = pSrc->IPv6.sin6_addr.s6_addr32[2];
|
---|
381 | pAddr->uAddr.IPv6.au32[3] = pSrc->IPv6.sin6_addr.s6_addr32[3];
|
---|
382 | }
|
---|
383 | #endif
|
---|
384 | else
|
---|
385 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
386 | return VINF_SUCCESS;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | /**
|
---|
391 | * Converts from a generic IPRT network address to a native socket address.
|
---|
392 | *
|
---|
393 | * @returns IPRT status code.
|
---|
394 | * @param pAddr Pointer to the generic IPRT network address.
|
---|
395 | * @param pDst The source address.
|
---|
396 | * @param cbDst The size of the source address.
|
---|
397 | * @param pcbAddr Where to store the size of the returned address.
|
---|
398 | * Optional
|
---|
399 | */
|
---|
400 | static int rtSocketAddrFromNetAddr(PCRTNETADDR pAddr, RTSOCKADDRUNION *pDst, size_t cbDst, int *pcbAddr)
|
---|
401 | {
|
---|
402 | RT_BZERO(pDst, cbDst);
|
---|
403 | if (pAddr->enmType == RTNETADDRTYPE_IPV4)
|
---|
404 | {
|
---|
405 | if (cbDst < sizeof(struct sockaddr_in))
|
---|
406 | return VERR_BUFFER_OVERFLOW;
|
---|
407 |
|
---|
408 | pDst->Addr.sa_family = AF_INET;
|
---|
409 | pDst->IPv4.sin_port = RT_H2N_U16(pAddr->uPort);
|
---|
410 | pDst->IPv4.sin_addr.s_addr = pAddr->uAddr.IPv4.u;
|
---|
411 | if (pcbAddr)
|
---|
412 | *pcbAddr = sizeof(pDst->IPv4);
|
---|
413 | }
|
---|
414 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
415 | else if (pAddr->enmType == RTNETADDRTYPE_IPV6)
|
---|
416 | {
|
---|
417 | if (cbDst < sizeof(struct sockaddr_in6))
|
---|
418 | return VERR_BUFFER_OVERFLOW;
|
---|
419 |
|
---|
420 | pDst->Addr.sa_family = AF_INET6;
|
---|
421 | pDst->IPv6.sin6_port = RT_H2N_U16(pAddr->uPort);
|
---|
422 | pSrc->IPv6.sin6_addr.s6_addr32[0] = pAddr->uAddr.IPv6.au32[0];
|
---|
423 | pSrc->IPv6.sin6_addr.s6_addr32[1] = pAddr->uAddr.IPv6.au32[1];
|
---|
424 | pSrc->IPv6.sin6_addr.s6_addr32[2] = pAddr->uAddr.IPv6.au32[2];
|
---|
425 | pSrc->IPv6.sin6_addr.s6_addr32[3] = pAddr->uAddr.IPv6.au32[3];
|
---|
426 | if (pcbAddr)
|
---|
427 | *pcbAddr = sizeof(pDst->IPv6);
|
---|
428 | }
|
---|
429 | #endif
|
---|
430 | else
|
---|
431 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
432 | return VINF_SUCCESS;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Tries to lock the socket for exclusive usage by the calling thread.
|
---|
438 | *
|
---|
439 | * Call rtSocketUnlock() to unlock.
|
---|
440 | *
|
---|
441 | * @returns @c true if locked, @c false if not.
|
---|
442 | * @param pThis The socket structure.
|
---|
443 | */
|
---|
444 | DECLINLINE(bool) rtSocketTryLock(RTSOCKETINT *pThis)
|
---|
445 | {
|
---|
446 | return ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0);
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Unlocks the socket.
|
---|
452 | *
|
---|
453 | * @param pThis The socket structure.
|
---|
454 | */
|
---|
455 | DECLINLINE(void) rtSocketUnlock(RTSOCKETINT *pThis)
|
---|
456 | {
|
---|
457 | ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1);
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * The slow path of rtSocketSwitchBlockingMode that does the actual switching.
|
---|
463 | *
|
---|
464 | * @returns IPRT status code.
|
---|
465 | * @param pThis The socket structure.
|
---|
466 | * @param fBlocking The desired mode of operation.
|
---|
467 | * @remarks Do not call directly.
|
---|
468 | */
|
---|
469 | static int rtSocketSwitchBlockingModeSlow(RTSOCKETINT *pThis, bool fBlocking)
|
---|
470 | {
|
---|
471 | #ifdef RT_OS_WINDOWS
|
---|
472 | AssertReturn(g_pfnioctlsocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
473 | u_long uBlocking = fBlocking ? 0 : 1;
|
---|
474 | if (g_pfnioctlsocket(pThis->hNative, FIONBIO, &uBlocking))
|
---|
475 | return rtSocketError();
|
---|
476 |
|
---|
477 | #else
|
---|
478 | int fFlags = fcntl(pThis->hNative, F_GETFL, 0);
|
---|
479 | if (fFlags == -1)
|
---|
480 | return rtSocketError();
|
---|
481 |
|
---|
482 | if (fBlocking)
|
---|
483 | fFlags &= ~O_NONBLOCK;
|
---|
484 | else
|
---|
485 | fFlags |= O_NONBLOCK;
|
---|
486 | if (fcntl(pThis->hNative, F_SETFL, fFlags) == -1)
|
---|
487 | return rtSocketError();
|
---|
488 | #endif
|
---|
489 |
|
---|
490 | pThis->fBlocking = fBlocking;
|
---|
491 | return VINF_SUCCESS;
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | /**
|
---|
496 | * Switches the socket to the desired blocking mode if necessary.
|
---|
497 | *
|
---|
498 | * The socket must be locked.
|
---|
499 | *
|
---|
500 | * @returns IPRT status code.
|
---|
501 | * @param pThis The socket structure.
|
---|
502 | * @param fBlocking The desired mode of operation.
|
---|
503 | */
|
---|
504 | DECLINLINE(int) rtSocketSwitchBlockingMode(RTSOCKETINT *pThis, bool fBlocking)
|
---|
505 | {
|
---|
506 | if (pThis->fBlocking != fBlocking)
|
---|
507 | return rtSocketSwitchBlockingModeSlow(pThis, fBlocking);
|
---|
508 | return VINF_SUCCESS;
|
---|
509 | }
|
---|
510 |
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * Creates an IPRT socket handle for a native one.
|
---|
514 | *
|
---|
515 | * @returns IPRT status code.
|
---|
516 | * @param ppSocket Where to return the IPRT socket handle.
|
---|
517 | * @param hNative The native handle.
|
---|
518 | * @param fLeaveOpen Whether to leave the native socket handle open when
|
---|
519 | * closed.
|
---|
520 | */
|
---|
521 | DECLHIDDEN(int) rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative, bool fLeaveOpen)
|
---|
522 | {
|
---|
523 | RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemPoolAlloc(RTMEMPOOL_DEFAULT, sizeof(*pThis));
|
---|
524 | if (!pThis)
|
---|
525 | return VERR_NO_MEMORY;
|
---|
526 | pThis->u32Magic = RTSOCKET_MAGIC;
|
---|
527 | pThis->cUsers = 0;
|
---|
528 | pThis->hNative = hNative;
|
---|
529 | pThis->fClosed = false;
|
---|
530 | pThis->fLeaveOpen = fLeaveOpen;
|
---|
531 | pThis->fBlocking = true;
|
---|
532 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
533 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
534 | #endif
|
---|
535 | #ifdef RT_OS_WINDOWS
|
---|
536 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
537 | pThis->fPollEvts = 0;
|
---|
538 | pThis->fSubscribedEvts = 0;
|
---|
539 | pThis->fEventsSaved = 0;
|
---|
540 | pThis->fHarvestedEvents = false;
|
---|
541 | pThis->fPollFallback = g_uWinSockInitedVersion < MAKEWORD(2, 0)
|
---|
542 | || g_pfnWSACreateEvent == NULL
|
---|
543 | || g_pfnWSACloseEvent == NULL
|
---|
544 | || g_pfnWSAEventSelect == NULL
|
---|
545 | || g_pfnWSAEnumNetworkEvents == NULL;
|
---|
546 | pThis->fPollFallbackActive = false;
|
---|
547 | pThis->fPollFallbackShutdown = false;
|
---|
548 | pThis->hPollFallbackNotifyR = NIL_RTSOCKETNATIVE;
|
---|
549 | pThis->hPollFallbackNotifyW = NIL_RTSOCKETNATIVE;
|
---|
550 | pThis->hPollFallbackThread = NIL_RTTHREAD;
|
---|
551 | #endif
|
---|
552 | *ppSocket = pThis;
|
---|
553 | return VINF_SUCCESS;
|
---|
554 | }
|
---|
555 |
|
---|
556 |
|
---|
557 | RTDECL(int) RTSocketFromNative(PRTSOCKET phSocket, RTHCINTPTR uNative)
|
---|
558 | {
|
---|
559 | AssertReturn(uNative != NIL_RTSOCKETNATIVE, VERR_INVALID_PARAMETER);
|
---|
560 | #ifndef RT_OS_WINDOWS
|
---|
561 | AssertReturn(uNative >= 0, VERR_INVALID_PARAMETER);
|
---|
562 | #endif
|
---|
563 | AssertPtrReturn(phSocket, VERR_INVALID_POINTER);
|
---|
564 | return rtSocketCreateForNative(phSocket, uNative, false /*fLeaveOpen*/);
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * Wrapper around socket().
|
---|
570 | *
|
---|
571 | * @returns IPRT status code.
|
---|
572 | * @param phSocket Where to store the handle to the socket on
|
---|
573 | * success.
|
---|
574 | * @param iDomain The protocol family (PF_XXX).
|
---|
575 | * @param iType The socket type (SOCK_XXX).
|
---|
576 | * @param iProtocol Socket parameter, usually 0.
|
---|
577 | * @param fInheritable Set to true if the socket should be inherted by
|
---|
578 | * child processes, false if not inheritable.
|
---|
579 | */
|
---|
580 | DECLHIDDEN(int) rtSocketCreate(PRTSOCKET phSocket, int iDomain, int iType, int iProtocol, bool fInheritable)
|
---|
581 | {
|
---|
582 | #ifdef RT_OS_WINDOWS
|
---|
583 | AssertReturn(g_pfnsocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
584 | AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
585 |
|
---|
586 | /* Initialize WinSock. */
|
---|
587 | int rc2 = rtSocketInitWinsock();
|
---|
588 | if (RT_FAILURE(rc2))
|
---|
589 | return rc2;
|
---|
590 | #endif
|
---|
591 |
|
---|
592 | /*
|
---|
593 | * Create the socket.
|
---|
594 | *
|
---|
595 | * The RTSocketSetInheritance operation isn't necessarily reliable on windows,
|
---|
596 | * so try use WSA_FLAG_NO_HANDLE_INHERIT with WSASocketW when possible.
|
---|
597 | */
|
---|
598 | #ifdef RT_OS_WINDOWS
|
---|
599 | bool fCallSetInheritance = true;
|
---|
600 | RTSOCKETNATIVE hNative;
|
---|
601 | if (g_pfnWSASocketW)
|
---|
602 | {
|
---|
603 | DWORD fWsaFlags = WSA_FLAG_OVERLAPPED | (!fInheritable ? WSA_FLAG_NO_HANDLE_INHERIT : 0);
|
---|
604 | hNative = g_pfnWSASocketW(iDomain, iType, iProtocol, NULL, 0 /*Group*/, fWsaFlags);
|
---|
605 | if (hNative != NIL_RTSOCKETNATIVE)
|
---|
606 | fCallSetInheritance = false;
|
---|
607 | else
|
---|
608 | {
|
---|
609 | if (!fInheritable)
|
---|
610 | hNative = g_pfnsocket(iDomain, iType, iProtocol);
|
---|
611 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
612 | return rtSocketError();
|
---|
613 | }
|
---|
614 | }
|
---|
615 | else
|
---|
616 | {
|
---|
617 | hNative = g_pfnsocket(iDomain, iType, iProtocol);
|
---|
618 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
619 | return rtSocketError();
|
---|
620 | }
|
---|
621 | #else
|
---|
622 | RTSOCKETNATIVE hNative = socket(iDomain, iType, iProtocol);
|
---|
623 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
624 | return rtSocketError();
|
---|
625 | #endif
|
---|
626 |
|
---|
627 | /*
|
---|
628 | * Wrap it.
|
---|
629 | */
|
---|
630 | int rc = rtSocketCreateForNative(phSocket, hNative, false /*fLeaveOpen*/);
|
---|
631 | if (RT_SUCCESS(rc))
|
---|
632 | {
|
---|
633 | #ifdef RT_OS_WINDOWS
|
---|
634 | if (fCallSetInheritance)
|
---|
635 | #endif
|
---|
636 | RTSocketSetInheritance(*phSocket, fInheritable);
|
---|
637 | }
|
---|
638 | else
|
---|
639 | {
|
---|
640 | #ifdef RT_OS_WINDOWS
|
---|
641 | g_pfnclosesocket(hNative);
|
---|
642 | #else
|
---|
643 | close(hNative);
|
---|
644 | #endif
|
---|
645 | }
|
---|
646 | return rc;
|
---|
647 | }
|
---|
648 |
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * Wrapper around socketpair() for creating a local TCP connection.
|
---|
652 | *
|
---|
653 | * @returns IPRT status code.
|
---|
654 | * @param phServer Where to return the first native socket.
|
---|
655 | * @param phClient Where to return the second native socket.
|
---|
656 | */
|
---|
657 | static int rtSocketCreateNativeTcpPair(RTSOCKETNATIVE *phServer, RTSOCKETNATIVE *phClient)
|
---|
658 | {
|
---|
659 | #ifdef RT_OS_WINDOWS
|
---|
660 | /*
|
---|
661 | * Initialize WinSock and make sure we got the necessary APIs.
|
---|
662 | */
|
---|
663 | int rc = rtSocketInitWinsock();
|
---|
664 | if (RT_FAILURE(rc))
|
---|
665 | return rc;
|
---|
666 | AssertReturn(g_pfnsocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
667 | AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
668 | AssertReturn(g_pfnsetsockopt, VERR_NET_NOT_UNSUPPORTED);
|
---|
669 | AssertReturn(g_pfnbind, VERR_NET_NOT_UNSUPPORTED);
|
---|
670 | AssertReturn(g_pfngetsockname, VERR_NET_NOT_UNSUPPORTED);
|
---|
671 | AssertReturn(g_pfnlisten, VERR_NET_NOT_UNSUPPORTED);
|
---|
672 | AssertReturn(g_pfnaccept, VERR_NET_NOT_UNSUPPORTED);
|
---|
673 | AssertReturn(g_pfnconnect, VERR_NET_NOT_UNSUPPORTED);
|
---|
674 |
|
---|
675 | /*
|
---|
676 | * Create the "server" listen socket and the "client" socket.
|
---|
677 | */
|
---|
678 | RTSOCKETNATIVE hListener = g_pfnsocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
679 | if (hListener == NIL_RTSOCKETNATIVE)
|
---|
680 | return rtSocketError();
|
---|
681 | RTSOCKETNATIVE hClient = g_pfnsocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
682 | if (hClient != NIL_RTSOCKETNATIVE)
|
---|
683 | {
|
---|
684 |
|
---|
685 | /*
|
---|
686 | * We let WinSock choose a port number when we bind.
|
---|
687 | */
|
---|
688 | union
|
---|
689 | {
|
---|
690 | struct sockaddr_in Ip;
|
---|
691 | struct sockaddr Generic;
|
---|
692 | } uAddr;
|
---|
693 | RT_ZERO(uAddr);
|
---|
694 | uAddr.Ip.sin_family = AF_INET;
|
---|
695 | uAddr.Ip.sin_addr.s_addr = RT_H2N_U32_C(INADDR_LOOPBACK);
|
---|
696 | //uAddr.Ip.sin_port = 0;
|
---|
697 | int fReuse = 1;
|
---|
698 | rc = g_pfnsetsockopt(hListener, SOL_SOCKET, SO_REUSEADDR, (const char *)&fReuse, sizeof(fReuse));
|
---|
699 | if (rc == 0)
|
---|
700 | {
|
---|
701 | rc = g_pfnbind(hListener, &uAddr.Generic, sizeof(uAddr.Ip));
|
---|
702 | if (rc == 0)
|
---|
703 | {
|
---|
704 | /*
|
---|
705 | * Get the address the client should connect to. According to the docs,
|
---|
706 | * we cannot assume that getsockname sets the IP and family.
|
---|
707 | */
|
---|
708 | RT_ZERO(uAddr);
|
---|
709 | int cbAddr = sizeof(uAddr.Ip);
|
---|
710 | rc = g_pfngetsockname(hListener, &uAddr.Generic, &cbAddr);
|
---|
711 | if (rc == 0)
|
---|
712 | {
|
---|
713 | uAddr.Ip.sin_family = AF_INET;
|
---|
714 | uAddr.Ip.sin_addr.s_addr = RT_H2N_U32_C(INADDR_LOOPBACK);
|
---|
715 |
|
---|
716 | /*
|
---|
717 | * Listen, connect and accept.
|
---|
718 | */
|
---|
719 | rc = g_pfnlisten(hListener, 1 /*cBacklog*/);
|
---|
720 | if (rc == 0)
|
---|
721 | {
|
---|
722 | rc = g_pfnconnect(hClient, &uAddr.Generic, sizeof(uAddr.Ip));
|
---|
723 | if (rc == 0)
|
---|
724 | {
|
---|
725 | RTSOCKETNATIVE hServer = g_pfnaccept(hListener, NULL, NULL);
|
---|
726 | if (hServer != NIL_RTSOCKETNATIVE)
|
---|
727 | {
|
---|
728 | g_pfnclosesocket(hListener);
|
---|
729 |
|
---|
730 | /*
|
---|
731 | * Done!
|
---|
732 | */
|
---|
733 | *phServer = hServer;
|
---|
734 | *phClient = hClient;
|
---|
735 | return VINF_SUCCESS;
|
---|
736 | }
|
---|
737 | }
|
---|
738 | }
|
---|
739 | }
|
---|
740 | }
|
---|
741 | }
|
---|
742 | rc = rtSocketError();
|
---|
743 | g_pfnclosesocket(hClient);
|
---|
744 | }
|
---|
745 | else
|
---|
746 | rc = rtSocketError();
|
---|
747 | g_pfnclosesocket(hListener);
|
---|
748 | return rc;
|
---|
749 |
|
---|
750 | #else
|
---|
751 | /*
|
---|
752 | * Got socket pair, so use it.
|
---|
753 | * Note! This isn't TCP per se, but it should fool the users.
|
---|
754 | */
|
---|
755 | int aSockets[2] = { -1, -1 };
|
---|
756 | if (socketpair(AF_LOCAL, SOCK_STREAM, 0, aSockets) == 0)
|
---|
757 | {
|
---|
758 | *phServer = aSockets[0];
|
---|
759 | *phClient = aSockets[1];
|
---|
760 | return VINF_SUCCESS;
|
---|
761 | }
|
---|
762 | return rtSocketError();
|
---|
763 | #endif
|
---|
764 | }
|
---|
765 |
|
---|
766 |
|
---|
767 | /**
|
---|
768 | * Worker for RTTcpCreatePair.
|
---|
769 | *
|
---|
770 | * @returns IPRT status code.
|
---|
771 | * @param phServer Where to return the "server" side of the pair.
|
---|
772 | * @param phClient Where to return the "client" side of the pair.
|
---|
773 | * @note There is no server or client side, but we gotta call it something.
|
---|
774 | */
|
---|
775 | DECLHIDDEN(int) rtSocketCreateTcpPair(RTSOCKET *phServer, RTSOCKET *phClient)
|
---|
776 | {
|
---|
777 | RTSOCKETNATIVE hServer = NIL_RTSOCKETNATIVE;
|
---|
778 | RTSOCKETNATIVE hClient = NIL_RTSOCKETNATIVE;
|
---|
779 | int rc = rtSocketCreateNativeTcpPair(&hServer, &hClient);
|
---|
780 | if (RT_SUCCESS(rc))
|
---|
781 | {
|
---|
782 | rc = rtSocketCreateForNative(phServer, hServer, false /*fLeaveOpen*/);
|
---|
783 | if (RT_SUCCESS(rc))
|
---|
784 | {
|
---|
785 | rc = rtSocketCreateForNative(phClient, hClient, false /*fLeaveOpen*/);
|
---|
786 | if (RT_SUCCESS(rc))
|
---|
787 | return VINF_SUCCESS;
|
---|
788 | RTSocketRelease(*phServer);
|
---|
789 | }
|
---|
790 | else
|
---|
791 | {
|
---|
792 | #ifdef RT_OS_WINDOWS
|
---|
793 | g_pfnclosesocket(hServer);
|
---|
794 | #else
|
---|
795 | close(hServer);
|
---|
796 | #endif
|
---|
797 | }
|
---|
798 | #ifdef RT_OS_WINDOWS
|
---|
799 | g_pfnclosesocket(hClient);
|
---|
800 | #else
|
---|
801 | close(hClient);
|
---|
802 | #endif
|
---|
803 | }
|
---|
804 |
|
---|
805 | *phServer = NIL_RTSOCKET;
|
---|
806 | *phClient = NIL_RTSOCKET;
|
---|
807 | return rc;
|
---|
808 | }
|
---|
809 |
|
---|
810 |
|
---|
811 | RTDECL(uint32_t) RTSocketRetain(RTSOCKET hSocket)
|
---|
812 | {
|
---|
813 | RTSOCKETINT *pThis = hSocket;
|
---|
814 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
815 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
816 | return RTMemPoolRetain(pThis);
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * Worker for RTSocketRelease and RTSocketClose.
|
---|
822 | *
|
---|
823 | * @returns IPRT status code.
|
---|
824 | * @param pThis The socket handle instance data.
|
---|
825 | * @param fDestroy Whether we're reaching ref count zero.
|
---|
826 | */
|
---|
827 | static int rtSocketCloseIt(RTSOCKETINT *pThis, bool fDestroy)
|
---|
828 | {
|
---|
829 | /*
|
---|
830 | * Invalidate the handle structure on destroy.
|
---|
831 | */
|
---|
832 | if (fDestroy)
|
---|
833 | {
|
---|
834 | Assert(ASMAtomicReadU32(&pThis->u32Magic) == RTSOCKET_MAGIC);
|
---|
835 | ASMAtomicWriteU32(&pThis->u32Magic, RTSOCKET_MAGIC_DEAD);
|
---|
836 | }
|
---|
837 |
|
---|
838 | int rc = VINF_SUCCESS;
|
---|
839 | if (ASMAtomicCmpXchgBool(&pThis->fClosed, true, false))
|
---|
840 | {
|
---|
841 | #ifdef RT_OS_WINDOWS
|
---|
842 | /*
|
---|
843 | * Poke the polling thread if active and give it a small chance to stop.
|
---|
844 | */
|
---|
845 | if ( pThis->fPollFallback
|
---|
846 | && pThis->hPollFallbackThread != NIL_RTTHREAD)
|
---|
847 | {
|
---|
848 | ASMAtomicWriteBool(&pThis->fPollFallbackShutdown, true);
|
---|
849 | rtSocketPokePollFallbackThread(pThis);
|
---|
850 | int rc2 = RTThreadWait(pThis->hPollFallbackThread, RT_MS_1SEC, NULL);
|
---|
851 | if (RT_SUCCESS(rc2))
|
---|
852 | pThis->hPollFallbackThread = NIL_RTTHREAD;
|
---|
853 | }
|
---|
854 | #endif
|
---|
855 |
|
---|
856 | /*
|
---|
857 | * Close the native handle.
|
---|
858 | */
|
---|
859 | RTSOCKETNATIVE hNative = pThis->hNative;
|
---|
860 | if (hNative != NIL_RTSOCKETNATIVE)
|
---|
861 | {
|
---|
862 | pThis->hNative = NIL_RTSOCKETNATIVE;
|
---|
863 |
|
---|
864 | if (!pThis->fLeaveOpen)
|
---|
865 | {
|
---|
866 | #ifdef RT_OS_WINDOWS
|
---|
867 | AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
868 | if (g_pfnclosesocket(hNative))
|
---|
869 | #else
|
---|
870 | if (close(hNative))
|
---|
871 | #endif
|
---|
872 | {
|
---|
873 | rc = rtSocketError();
|
---|
874 | #ifdef RT_OS_WINDOWS
|
---|
875 | AssertMsgFailed(("closesocket(%p) -> %Rrc\n", (uintptr_t)hNative, rc));
|
---|
876 | #else
|
---|
877 | AssertMsgFailed(("close(%d) -> %Rrc\n", hNative, rc));
|
---|
878 | #endif
|
---|
879 | }
|
---|
880 | }
|
---|
881 | }
|
---|
882 |
|
---|
883 | #ifdef RT_OS_WINDOWS
|
---|
884 | /*
|
---|
885 | * Windows specific polling cleanup.
|
---|
886 | */
|
---|
887 | WSAEVENT hEvent = pThis->hEvent;
|
---|
888 | if (hEvent != WSA_INVALID_EVENT)
|
---|
889 | {
|
---|
890 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
891 | if (!pThis->fPollFallback)
|
---|
892 | {
|
---|
893 | Assert(g_pfnWSACloseEvent);
|
---|
894 | if (g_pfnWSACloseEvent)
|
---|
895 | g_pfnWSACloseEvent(hEvent);
|
---|
896 | }
|
---|
897 | else
|
---|
898 | CloseHandle(hEvent);
|
---|
899 | }
|
---|
900 |
|
---|
901 | if (pThis->fPollFallback)
|
---|
902 | {
|
---|
903 | if (pThis->hPollFallbackNotifyW != NIL_RTSOCKETNATIVE)
|
---|
904 | {
|
---|
905 | g_pfnclosesocket(pThis->hPollFallbackNotifyW);
|
---|
906 | pThis->hPollFallbackNotifyW = NIL_RTSOCKETNATIVE;
|
---|
907 | }
|
---|
908 |
|
---|
909 | if (pThis->hPollFallbackThread != NIL_RTTHREAD)
|
---|
910 | {
|
---|
911 | int rc2 = RTThreadWait(pThis->hPollFallbackThread, RT_MS_1MIN / 2, NULL);
|
---|
912 | AssertRC(rc2);
|
---|
913 | pThis->hPollFallbackThread = NIL_RTTHREAD;
|
---|
914 | }
|
---|
915 |
|
---|
916 | if (pThis->hPollFallbackNotifyR != NIL_RTSOCKETNATIVE)
|
---|
917 | {
|
---|
918 | g_pfnclosesocket(pThis->hPollFallbackNotifyR);
|
---|
919 | pThis->hPollFallbackNotifyR = NIL_RTSOCKETNATIVE;
|
---|
920 | }
|
---|
921 | }
|
---|
922 | #endif
|
---|
923 | }
|
---|
924 |
|
---|
925 | return rc;
|
---|
926 | }
|
---|
927 |
|
---|
928 |
|
---|
929 | RTDECL(uint32_t) RTSocketRelease(RTSOCKET hSocket)
|
---|
930 | {
|
---|
931 | RTSOCKETINT *pThis = hSocket;
|
---|
932 | if (pThis == NIL_RTSOCKET)
|
---|
933 | return 0;
|
---|
934 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
935 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
936 |
|
---|
937 | /* get the refcount without killing it... */
|
---|
938 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
939 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
940 | if (cRefs == 1)
|
---|
941 | rtSocketCloseIt(pThis, true);
|
---|
942 |
|
---|
943 | return RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
944 | }
|
---|
945 |
|
---|
946 |
|
---|
947 | RTDECL(int) RTSocketClose(RTSOCKET hSocket)
|
---|
948 | {
|
---|
949 | RTSOCKETINT *pThis = hSocket;
|
---|
950 | if (pThis == NIL_RTSOCKET)
|
---|
951 | return VINF_SUCCESS;
|
---|
952 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
953 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
954 |
|
---|
955 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
956 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
957 |
|
---|
958 | int rc = rtSocketCloseIt(pThis, cRefs == 1);
|
---|
959 |
|
---|
960 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
961 | return rc;
|
---|
962 | }
|
---|
963 |
|
---|
964 |
|
---|
965 | RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket)
|
---|
966 | {
|
---|
967 | RTSOCKETINT *pThis = hSocket;
|
---|
968 | AssertPtrReturn(pThis, RTHCUINTPTR_MAX);
|
---|
969 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, RTHCUINTPTR_MAX);
|
---|
970 | return (RTHCUINTPTR)pThis->hNative;
|
---|
971 | }
|
---|
972 |
|
---|
973 |
|
---|
974 | RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable)
|
---|
975 | {
|
---|
976 | RTSOCKETINT *pThis = hSocket;
|
---|
977 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
978 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
979 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
980 |
|
---|
981 | #ifndef RT_OS_WINDOWS
|
---|
982 | if (fcntl(pThis->hNative, F_SETFD, fInheritable ? 0 : FD_CLOEXEC) < 0)
|
---|
983 | return RTErrConvertFromErrno(errno);
|
---|
984 | return VINF_SUCCESS;
|
---|
985 | #else
|
---|
986 | /* Windows is more complicated as sockets are complicated wrt inheritance
|
---|
987 | (see stackoverflow for details). In general, though we cannot hope to
|
---|
988 | make a socket really non-inheritable before vista as other layers in
|
---|
989 | the winsock maze may have additional handles associated with the socket. */
|
---|
990 | if (g_pfnGetHandleInformation)
|
---|
991 | {
|
---|
992 | /* Check if the handle is already in what seems to be the right state
|
---|
993 | before we try doing anything. */
|
---|
994 | DWORD fFlags;
|
---|
995 | if (g_pfnGetHandleInformation((HANDLE)pThis->hNative, &fFlags))
|
---|
996 | {
|
---|
997 | if (RT_BOOL(fFlags & HANDLE_FLAG_INHERIT) == fInheritable)
|
---|
998 | return VINF_SUCCESS;
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | if (!g_pfnSetHandleInformation)
|
---|
1003 | return VERR_NET_NOT_UNSUPPORTED;
|
---|
1004 |
|
---|
1005 | if (!g_pfnSetHandleInformation((HANDLE)pThis->hNative, HANDLE_FLAG_INHERIT, fInheritable ? HANDLE_FLAG_INHERIT : 0))
|
---|
1006 | return RTErrConvertFromWin32(GetLastError());
|
---|
1007 | /** @todo Need we do something related to WS_SIO_ASSOCIATE_HANDLE or
|
---|
1008 | * WS_SIO_TRANSLATE_HANDLE? Or what other handles could be associated
|
---|
1009 | * with the socket? that we need to modify? */
|
---|
1010 |
|
---|
1011 | return VINF_SUCCESS;
|
---|
1012 | #endif
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | static bool rtSocketIsIPv4Numerical(const char *pszAddress, PRTNETADDRIPV4 pAddr)
|
---|
1017 | {
|
---|
1018 |
|
---|
1019 | /* Empty address resolves to the INADDR_ANY address (good for bind). */
|
---|
1020 | if (!pszAddress || !*pszAddress)
|
---|
1021 | {
|
---|
1022 | pAddr->u = INADDR_ANY;
|
---|
1023 | return true;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | /* Four quads? */
|
---|
1027 | char *psz = (char *)pszAddress;
|
---|
1028 | for (int i = 0; i < 4; i++)
|
---|
1029 | {
|
---|
1030 | uint8_t u8;
|
---|
1031 | int rc = RTStrToUInt8Ex(psz, &psz, 0, &u8);
|
---|
1032 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
1033 | return false;
|
---|
1034 | if (*psz != (i < 3 ? '.' : '\0'))
|
---|
1035 | return false;
|
---|
1036 | psz++;
|
---|
1037 |
|
---|
1038 | pAddr->au8[i] = u8; /* big endian */
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | return true;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | RTDECL(int) RTSocketParseInetAddress(const char *pszAddress, unsigned uPort, PRTNETADDR pAddr)
|
---|
1045 | {
|
---|
1046 | int rc;
|
---|
1047 |
|
---|
1048 | /*
|
---|
1049 | * Validate input.
|
---|
1050 | */
|
---|
1051 | AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
|
---|
1052 | AssertPtrNullReturn(pszAddress, VERR_INVALID_POINTER);
|
---|
1053 |
|
---|
1054 | /*
|
---|
1055 | * Resolve the address. Pretty crude at the moment, but we have to make
|
---|
1056 | * sure to not ask the NT 4 gethostbyname about an IPv4 address as it may
|
---|
1057 | * give a wrong answer.
|
---|
1058 | */
|
---|
1059 | /** @todo this only supports IPv4, and IPv6 support needs to be added.
|
---|
1060 | * It probably needs to be converted to getaddrinfo(). */
|
---|
1061 | RTNETADDRIPV4 IPv4Quad;
|
---|
1062 | if (rtSocketIsIPv4Numerical(pszAddress, &IPv4Quad))
|
---|
1063 | {
|
---|
1064 | Log3(("rtSocketIsIPv4Numerical: %s -> %#x (%RTnaipv4)\n", pszAddress, IPv4Quad.u, IPv4Quad));
|
---|
1065 | RT_ZERO(*pAddr);
|
---|
1066 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
1067 | pAddr->uPort = uPort;
|
---|
1068 | pAddr->uAddr.IPv4 = IPv4Quad;
|
---|
1069 | return VINF_SUCCESS;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | #ifdef RT_OS_WINDOWS
|
---|
1073 | /* Initialize WinSock and check version before we call gethostbyname. */
|
---|
1074 | if (!g_pfngethostbyname)
|
---|
1075 | return VERR_NET_NOT_UNSUPPORTED;
|
---|
1076 |
|
---|
1077 | int rc2 = rtSocketInitWinsock();
|
---|
1078 | if (RT_FAILURE(rc2))
|
---|
1079 | return rc2;
|
---|
1080 |
|
---|
1081 | # define gethostbyname g_pfngethostbyname
|
---|
1082 | #endif
|
---|
1083 |
|
---|
1084 | struct hostent *pHostEnt;
|
---|
1085 | pHostEnt = gethostbyname(pszAddress);
|
---|
1086 | if (!pHostEnt)
|
---|
1087 | {
|
---|
1088 | rc = rtSocketResolverError();
|
---|
1089 | AssertMsg(rc == VERR_NET_HOST_NOT_FOUND,
|
---|
1090 | ("Could not resolve '%s', rc=%Rrc\n", pszAddress, rc));
|
---|
1091 | return rc;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | if (pHostEnt->h_addrtype == AF_INET)
|
---|
1095 | {
|
---|
1096 | RT_ZERO(*pAddr);
|
---|
1097 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
1098 | pAddr->uPort = uPort;
|
---|
1099 | pAddr->uAddr.IPv4.u = ((struct in_addr *)pHostEnt->h_addr)->s_addr;
|
---|
1100 | Log3(("gethostbyname: %s -> %#x (%RTnaipv4)\n", pszAddress, pAddr->uAddr.IPv4.u, pAddr->uAddr.IPv4));
|
---|
1101 | }
|
---|
1102 | else
|
---|
1103 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
1104 |
|
---|
1105 | #ifdef RT_OS_WINDOWS
|
---|
1106 | # undef gethostbyname
|
---|
1107 | #endif
|
---|
1108 | return VINF_SUCCESS;
|
---|
1109 | }
|
---|
1110 |
|
---|
1111 |
|
---|
1112 | /*
|
---|
1113 | * New function to allow both ipv4 and ipv6 addresses to be resolved.
|
---|
1114 | * Breaks compatibility with windows before 2000.
|
---|
1115 | */
|
---|
1116 | RTDECL(int) RTSocketQueryAddressStr(const char *pszHost, char *pszResult, size_t *pcbResult, PRTNETADDRTYPE penmAddrType)
|
---|
1117 | {
|
---|
1118 | AssertPtrReturn(pszHost, VERR_INVALID_POINTER);
|
---|
1119 | AssertPtrReturn(pcbResult, VERR_INVALID_POINTER);
|
---|
1120 | AssertPtrNullReturn(penmAddrType, VERR_INVALID_POINTER);
|
---|
1121 | AssertPtrNullReturn(pszResult, VERR_INVALID_POINTER);
|
---|
1122 |
|
---|
1123 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS) /** @todo dynamically resolve the APIs not present in NT4! */
|
---|
1124 | return VERR_NOT_SUPPORTED;
|
---|
1125 |
|
---|
1126 | #else
|
---|
1127 | int rc;
|
---|
1128 | if (*pcbResult < 16)
|
---|
1129 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
1130 |
|
---|
1131 | /* Setup the hint. */
|
---|
1132 | struct addrinfo grHints;
|
---|
1133 | RT_ZERO(grHints);
|
---|
1134 | grHints.ai_socktype = 0;
|
---|
1135 | grHints.ai_flags = 0;
|
---|
1136 | grHints.ai_protocol = 0;
|
---|
1137 | grHints.ai_family = AF_UNSPEC;
|
---|
1138 | if (penmAddrType)
|
---|
1139 | {
|
---|
1140 | switch (*penmAddrType)
|
---|
1141 | {
|
---|
1142 | case RTNETADDRTYPE_INVALID:
|
---|
1143 | /*grHints.ai_family = AF_UNSPEC;*/
|
---|
1144 | break;
|
---|
1145 | case RTNETADDRTYPE_IPV4:
|
---|
1146 | grHints.ai_family = AF_INET;
|
---|
1147 | break;
|
---|
1148 | case RTNETADDRTYPE_IPV6:
|
---|
1149 | grHints.ai_family = AF_INET6;
|
---|
1150 | break;
|
---|
1151 | default:
|
---|
1152 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
1153 | }
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | # ifdef RT_OS_WINDOWS
|
---|
1157 | /*
|
---|
1158 | * Winsock2 init
|
---|
1159 | */
|
---|
1160 | if ( !g_pfngetaddrinfo
|
---|
1161 | || !g_pfnfreeaddrinfo)
|
---|
1162 | return VERR_NET_NOT_UNSUPPORTED;
|
---|
1163 |
|
---|
1164 | int rc2 = rtSocketInitWinsock();
|
---|
1165 | if (RT_FAILURE(rc2))
|
---|
1166 | return rc2;
|
---|
1167 |
|
---|
1168 | # define getaddrinfo g_pfngetaddrinfo
|
---|
1169 | # define freeaddrinfo g_pfnfreeaddrinfo
|
---|
1170 | # endif
|
---|
1171 |
|
---|
1172 | /** @todo r=bird: getaddrinfo and freeaddrinfo breaks the additions on NT4. */
|
---|
1173 | struct addrinfo *pgrResults = NULL;
|
---|
1174 | rc = getaddrinfo(pszHost, "", &grHints, &pgrResults);
|
---|
1175 | if (rc != 0)
|
---|
1176 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
1177 |
|
---|
1178 | // return data
|
---|
1179 | // on multiple matches return only the first one
|
---|
1180 |
|
---|
1181 | if (!pgrResults)
|
---|
1182 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
1183 |
|
---|
1184 | struct addrinfo const *pgrResult = pgrResults->ai_next;
|
---|
1185 | if (!pgrResult)
|
---|
1186 | {
|
---|
1187 | freeaddrinfo(pgrResults);
|
---|
1188 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | RTNETADDRTYPE enmAddrType = RTNETADDRTYPE_INVALID;
|
---|
1192 | size_t cchIpAddress;
|
---|
1193 | char szIpAddress[48];
|
---|
1194 | if (pgrResult->ai_family == AF_INET)
|
---|
1195 | {
|
---|
1196 | struct sockaddr_in const *pgrSa = (struct sockaddr_in const *)pgrResult->ai_addr;
|
---|
1197 | cchIpAddress = RTStrPrintf(szIpAddress, sizeof(szIpAddress),
|
---|
1198 | "%RTnaipv4", pgrSa->sin_addr.s_addr);
|
---|
1199 | Assert(cchIpAddress >= 7 && cchIpAddress < sizeof(szIpAddress) - 1);
|
---|
1200 | enmAddrType = RTNETADDRTYPE_IPV4;
|
---|
1201 | rc = VINF_SUCCESS;
|
---|
1202 | }
|
---|
1203 | else if (pgrResult->ai_family == AF_INET6)
|
---|
1204 | {
|
---|
1205 | struct sockaddr_in6 const *pgrSa6 = (struct sockaddr_in6 const *)pgrResult->ai_addr;
|
---|
1206 | cchIpAddress = RTStrPrintf(szIpAddress, sizeof(szIpAddress),
|
---|
1207 | "%RTnaipv6", (PRTNETADDRIPV6)&pgrSa6->sin6_addr);
|
---|
1208 | enmAddrType = RTNETADDRTYPE_IPV6;
|
---|
1209 | rc = VINF_SUCCESS;
|
---|
1210 | }
|
---|
1211 | else
|
---|
1212 | {
|
---|
1213 | rc = VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
1214 | szIpAddress[0] = '\0';
|
---|
1215 | cchIpAddress = 0;
|
---|
1216 | }
|
---|
1217 | freeaddrinfo(pgrResults);
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * Copy out the result.
|
---|
1221 | */
|
---|
1222 | size_t const cbResult = *pcbResult;
|
---|
1223 | *pcbResult = cchIpAddress + 1;
|
---|
1224 | if (cchIpAddress < cbResult)
|
---|
1225 | memcpy(pszResult, szIpAddress, cchIpAddress + 1);
|
---|
1226 | else
|
---|
1227 | {
|
---|
1228 | RT_BZERO(pszResult, cbResult);
|
---|
1229 | if (RT_SUCCESS(rc))
|
---|
1230 | rc = VERR_BUFFER_OVERFLOW;
|
---|
1231 | }
|
---|
1232 | if (penmAddrType && RT_SUCCESS(rc))
|
---|
1233 | *penmAddrType = enmAddrType;
|
---|
1234 | return rc;
|
---|
1235 |
|
---|
1236 | # ifdef RT_OS_WINDOWS
|
---|
1237 | # undef getaddrinfo
|
---|
1238 | # undef freeaddrinfo
|
---|
1239 | # endif
|
---|
1240 | #endif /* !RT_OS_OS2 */
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 |
|
---|
1244 | RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
1245 | {
|
---|
1246 | /*
|
---|
1247 | * Validate input.
|
---|
1248 | */
|
---|
1249 | RTSOCKETINT *pThis = hSocket;
|
---|
1250 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1251 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1252 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
1253 | AssertPtr(pvBuffer);
|
---|
1254 | #ifdef RT_OS_WINDOWS
|
---|
1255 | AssertReturn(g_pfnrecv, VERR_NET_NOT_UNSUPPORTED);
|
---|
1256 | # define recv g_pfnrecv
|
---|
1257 | #endif
|
---|
1258 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1259 |
|
---|
1260 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1261 | if (RT_FAILURE(rc))
|
---|
1262 | return rc;
|
---|
1263 |
|
---|
1264 | /*
|
---|
1265 | * Read loop.
|
---|
1266 | * If pcbRead is NULL we have to fill the entire buffer!
|
---|
1267 | */
|
---|
1268 | size_t cbRead = 0;
|
---|
1269 | size_t cbToRead = cbBuffer;
|
---|
1270 | for (;;)
|
---|
1271 | {
|
---|
1272 | rtSocketErrorReset();
|
---|
1273 | #ifdef RTSOCKET_MAX_READ
|
---|
1274 | int cbNow = cbToRead >= RTSOCKET_MAX_READ ? RTSOCKET_MAX_READ : (int)cbToRead;
|
---|
1275 | #else
|
---|
1276 | size_t cbNow = cbToRead;
|
---|
1277 | #endif
|
---|
1278 | ssize_t cbBytesRead = recv(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL);
|
---|
1279 | if (cbBytesRead <= 0)
|
---|
1280 | {
|
---|
1281 | rc = rtSocketError();
|
---|
1282 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
1283 | if (RT_SUCCESS_NP(rc))
|
---|
1284 | {
|
---|
1285 | if (!pcbRead)
|
---|
1286 | rc = VERR_NET_SHUTDOWN;
|
---|
1287 | else
|
---|
1288 | {
|
---|
1289 | *pcbRead = 0;
|
---|
1290 | rc = VINF_SUCCESS;
|
---|
1291 | }
|
---|
1292 | }
|
---|
1293 | break;
|
---|
1294 | }
|
---|
1295 | if (pcbRead)
|
---|
1296 | {
|
---|
1297 | /* return partial data */
|
---|
1298 | *pcbRead = cbBytesRead;
|
---|
1299 | break;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | /* read more? */
|
---|
1303 | cbRead += cbBytesRead;
|
---|
1304 | if (cbRead == cbBuffer)
|
---|
1305 | break;
|
---|
1306 |
|
---|
1307 | /* next */
|
---|
1308 | cbToRead = cbBuffer - cbRead;
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | rtSocketUnlock(pThis);
|
---|
1312 | #ifdef RT_OS_WINDOWS
|
---|
1313 | # undef recv
|
---|
1314 | #endif
|
---|
1315 | return rc;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 |
|
---|
1319 | RTDECL(int) RTSocketReadFrom(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr)
|
---|
1320 | {
|
---|
1321 | /*
|
---|
1322 | * Validate input.
|
---|
1323 | */
|
---|
1324 | RTSOCKETINT *pThis = hSocket;
|
---|
1325 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1326 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1327 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
1328 | AssertPtr(pvBuffer);
|
---|
1329 | AssertPtr(pcbRead);
|
---|
1330 | #ifdef RT_OS_WINDOWS
|
---|
1331 | AssertReturn(g_pfnrecvfrom, VERR_NET_NOT_UNSUPPORTED);
|
---|
1332 | # define recvfrom g_pfnrecvfrom
|
---|
1333 | #endif
|
---|
1334 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1335 |
|
---|
1336 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1337 | if (RT_FAILURE(rc))
|
---|
1338 | return rc;
|
---|
1339 |
|
---|
1340 | /*
|
---|
1341 | * Read data.
|
---|
1342 | */
|
---|
1343 | size_t cbRead = 0;
|
---|
1344 | size_t cbToRead = cbBuffer;
|
---|
1345 | rtSocketErrorReset();
|
---|
1346 | RTSOCKADDRUNION u;
|
---|
1347 | #ifdef RTSOCKET_MAX_READ
|
---|
1348 | int cbNow = cbToRead >= RTSOCKET_MAX_READ ? RTSOCKET_MAX_READ : (int)cbToRead;
|
---|
1349 | int cbAddr = sizeof(u);
|
---|
1350 | #else
|
---|
1351 | size_t cbNow = cbToRead;
|
---|
1352 | socklen_t cbAddr = sizeof(u);
|
---|
1353 | #endif
|
---|
1354 | ssize_t cbBytesRead = recvfrom(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL, &u.Addr, &cbAddr);
|
---|
1355 | if (cbBytesRead <= 0)
|
---|
1356 | {
|
---|
1357 | rc = rtSocketError();
|
---|
1358 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
1359 | if (RT_SUCCESS_NP(rc))
|
---|
1360 | {
|
---|
1361 | *pcbRead = 0;
|
---|
1362 | rc = VINF_SUCCESS;
|
---|
1363 | }
|
---|
1364 | }
|
---|
1365 | else
|
---|
1366 | {
|
---|
1367 | if (pSrcAddr)
|
---|
1368 | rc = rtSocketNetAddrFromAddr(&u, cbAddr, pSrcAddr);
|
---|
1369 | *pcbRead = cbBytesRead;
|
---|
1370 | }
|
---|
1371 |
|
---|
1372 | rtSocketUnlock(pThis);
|
---|
1373 | #ifdef RT_OS_WINDOWS
|
---|
1374 | # undef recvfrom
|
---|
1375 | #endif
|
---|
1376 | return rc;
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 |
|
---|
1380 | RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer)
|
---|
1381 | {
|
---|
1382 | /*
|
---|
1383 | * Validate input.
|
---|
1384 | */
|
---|
1385 | RTSOCKETINT *pThis = hSocket;
|
---|
1386 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1387 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1388 | #ifdef RT_OS_WINDOWS
|
---|
1389 | AssertReturn(g_pfnsend, VERR_NET_NOT_UNSUPPORTED);
|
---|
1390 | # define send g_pfnsend
|
---|
1391 | #endif
|
---|
1392 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1393 |
|
---|
1394 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1395 | if (RT_FAILURE(rc))
|
---|
1396 | return rc;
|
---|
1397 |
|
---|
1398 | /*
|
---|
1399 | * Try write all at once.
|
---|
1400 | */
|
---|
1401 | #ifdef RTSOCKET_MAX_WRITE
|
---|
1402 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1403 | #else
|
---|
1404 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
1405 | #endif
|
---|
1406 | ssize_t cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1407 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
1408 | rc = VINF_SUCCESS;
|
---|
1409 | else if (cbWritten < 0)
|
---|
1410 | rc = rtSocketError();
|
---|
1411 | else
|
---|
1412 | {
|
---|
1413 | /*
|
---|
1414 | * Unfinished business, write the remainder of the request. Must ignore
|
---|
1415 | * VERR_INTERRUPTED here if we've managed to send something.
|
---|
1416 | */
|
---|
1417 | size_t cbSentSoFar = 0;
|
---|
1418 | for (;;)
|
---|
1419 | {
|
---|
1420 | /* advance */
|
---|
1421 | cbBuffer -= (size_t)cbWritten;
|
---|
1422 | if (!cbBuffer)
|
---|
1423 | break;
|
---|
1424 | cbSentSoFar += (size_t)cbWritten;
|
---|
1425 | pvBuffer = (char const *)pvBuffer + cbWritten;
|
---|
1426 |
|
---|
1427 | /* send */
|
---|
1428 | #ifdef RTSOCKET_MAX_WRITE
|
---|
1429 | cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1430 | #else
|
---|
1431 | cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
1432 | #endif
|
---|
1433 | cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1434 | if (cbWritten >= 0)
|
---|
1435 | AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtSocketError()=%d\n",
|
---|
1436 | cbWritten, cbBuffer, rtSocketError()));
|
---|
1437 | else
|
---|
1438 | {
|
---|
1439 | rc = rtSocketError();
|
---|
1440 | if (rc != VERR_INTERNAL_ERROR || cbSentSoFar == 0)
|
---|
1441 | break;
|
---|
1442 | cbWritten = 0;
|
---|
1443 | rc = VINF_SUCCESS;
|
---|
1444 | }
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | rtSocketUnlock(pThis);
|
---|
1449 | #ifdef RT_OS_WINDOWS
|
---|
1450 | # undef send
|
---|
1451 | #endif
|
---|
1452 | return rc;
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 |
|
---|
1456 | RTDECL(int) RTSocketWriteTo(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pAddr)
|
---|
1457 | {
|
---|
1458 | /*
|
---|
1459 | * Validate input.
|
---|
1460 | */
|
---|
1461 | RTSOCKETINT *pThis = hSocket;
|
---|
1462 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1463 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1464 | #ifdef RT_OS_WINDOWS
|
---|
1465 | AssertReturn(g_pfnsendto, VERR_NET_NOT_UNSUPPORTED);
|
---|
1466 | # define sendto g_pfnsendto
|
---|
1467 | #endif
|
---|
1468 |
|
---|
1469 | /* no locking since UDP reads may be done concurrently to writes, and
|
---|
1470 | * this is the normal use case of this code. */
|
---|
1471 |
|
---|
1472 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1473 | if (RT_FAILURE(rc))
|
---|
1474 | return rc;
|
---|
1475 |
|
---|
1476 | /* Figure out destination address. */
|
---|
1477 | struct sockaddr *pSA = NULL;
|
---|
1478 | #ifdef RT_OS_WINDOWS
|
---|
1479 | int cbSA = 0;
|
---|
1480 | #else
|
---|
1481 | socklen_t cbSA = 0;
|
---|
1482 | #endif
|
---|
1483 | RTSOCKADDRUNION u;
|
---|
1484 | if (pAddr)
|
---|
1485 | {
|
---|
1486 | rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), NULL);
|
---|
1487 | if (RT_FAILURE(rc))
|
---|
1488 | return rc;
|
---|
1489 | pSA = &u.Addr;
|
---|
1490 | cbSA = sizeof(u);
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | /*
|
---|
1494 | * Must write all at once, otherwise it is a failure.
|
---|
1495 | */
|
---|
1496 | #ifdef RT_OS_WINDOWS
|
---|
1497 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1498 | #else
|
---|
1499 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
1500 | #endif
|
---|
1501 | ssize_t cbWritten = sendto(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL, pSA, cbSA);
|
---|
1502 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
1503 | rc = VINF_SUCCESS;
|
---|
1504 | else if (cbWritten < 0)
|
---|
1505 | rc = rtSocketError();
|
---|
1506 | else
|
---|
1507 | rc = VERR_TOO_MUCH_DATA;
|
---|
1508 |
|
---|
1509 | /// @todo rtSocketUnlock(pThis);
|
---|
1510 | #ifdef RT_OS_WINDOWS
|
---|
1511 | # undef sendto
|
---|
1512 | #endif
|
---|
1513 | return rc;
|
---|
1514 | }
|
---|
1515 |
|
---|
1516 |
|
---|
1517 | RTDECL(int) RTSocketWriteToNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pAddr)
|
---|
1518 | {
|
---|
1519 | /*
|
---|
1520 | * Validate input.
|
---|
1521 | */
|
---|
1522 | RTSOCKETINT *pThis = hSocket;
|
---|
1523 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1524 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1525 | #ifdef RT_OS_WINDOWS
|
---|
1526 | AssertReturn(g_pfnsendto, VERR_NET_NOT_UNSUPPORTED);
|
---|
1527 | # define sendto g_pfnsendto
|
---|
1528 | #endif
|
---|
1529 |
|
---|
1530 | /* no locking since UDP reads may be done concurrently to writes, and
|
---|
1531 | * this is the normal use case of this code. */
|
---|
1532 |
|
---|
1533 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1534 | if (RT_FAILURE(rc))
|
---|
1535 | return rc;
|
---|
1536 |
|
---|
1537 | /* Figure out destination address. */
|
---|
1538 | struct sockaddr *pSA = NULL;
|
---|
1539 | #ifdef RT_OS_WINDOWS
|
---|
1540 | int cbSA = 0;
|
---|
1541 | #else
|
---|
1542 | socklen_t cbSA = 0;
|
---|
1543 | #endif
|
---|
1544 | RTSOCKADDRUNION u;
|
---|
1545 | if (pAddr)
|
---|
1546 | {
|
---|
1547 | rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), NULL);
|
---|
1548 | if (RT_FAILURE(rc))
|
---|
1549 | return rc;
|
---|
1550 | pSA = &u.Addr;
|
---|
1551 | cbSA = sizeof(u);
|
---|
1552 | }
|
---|
1553 |
|
---|
1554 | /*
|
---|
1555 | * Must write all at once, otherwise it is a failure.
|
---|
1556 | */
|
---|
1557 | #ifdef RT_OS_WINDOWS
|
---|
1558 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1559 | #else
|
---|
1560 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
1561 | #endif
|
---|
1562 | ssize_t cbWritten = sendto(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL, pSA, cbSA);
|
---|
1563 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
1564 | rc = VINF_SUCCESS;
|
---|
1565 | else if (cbWritten < 0)
|
---|
1566 | rc = rtSocketError();
|
---|
1567 | else
|
---|
1568 | rc = VERR_TOO_MUCH_DATA;
|
---|
1569 |
|
---|
1570 | /// @todo rtSocketUnlock(pThis);
|
---|
1571 | #ifdef RT_OS_WINDOWS
|
---|
1572 | # undef sendto
|
---|
1573 | #endif
|
---|
1574 | return rc;
|
---|
1575 | }
|
---|
1576 |
|
---|
1577 |
|
---|
1578 | RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf)
|
---|
1579 | {
|
---|
1580 | /*
|
---|
1581 | * Validate input.
|
---|
1582 | */
|
---|
1583 | RTSOCKETINT *pThis = hSocket;
|
---|
1584 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1585 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1586 | AssertPtrReturn(pSgBuf, VERR_INVALID_PARAMETER);
|
---|
1587 | AssertReturn(pSgBuf->cSegs > 0, VERR_INVALID_PARAMETER);
|
---|
1588 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1589 |
|
---|
1590 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1591 | if (RT_FAILURE(rc))
|
---|
1592 | return rc;
|
---|
1593 |
|
---|
1594 | /*
|
---|
1595 | * Construct message descriptor (translate pSgBuf) and send it.
|
---|
1596 | */
|
---|
1597 | rc = VERR_NO_TMP_MEMORY;
|
---|
1598 | #ifdef RT_OS_WINDOWS
|
---|
1599 | if (g_pfnWSASend)
|
---|
1600 | {
|
---|
1601 | AssertCompileSize(WSABUF, sizeof(RTSGSEG));
|
---|
1602 | AssertCompileMemberSize(WSABUF, buf, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
1603 |
|
---|
1604 | LPWSABUF paMsg = (LPWSABUF)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(WSABUF));
|
---|
1605 | if (paMsg)
|
---|
1606 | {
|
---|
1607 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
1608 | {
|
---|
1609 | paMsg[i].buf = (char *)pSgBuf->paSegs[i].pvSeg;
|
---|
1610 | paMsg[i].len = (u_long)pSgBuf->paSegs[i].cbSeg;
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | DWORD dwSent;
|
---|
1614 | int hrc = g_pfnWSASend(pThis->hNative, paMsg, pSgBuf->cSegs, &dwSent, MSG_NOSIGNAL, NULL, NULL);
|
---|
1615 | if (!hrc)
|
---|
1616 | rc = VINF_SUCCESS;
|
---|
1617 | /** @todo check for incomplete writes */
|
---|
1618 | else
|
---|
1619 | rc = rtSocketError();
|
---|
1620 |
|
---|
1621 | RTMemTmpFree(paMsg);
|
---|
1622 | }
|
---|
1623 | }
|
---|
1624 | else if (g_pfnsend)
|
---|
1625 | {
|
---|
1626 | rc = VINF_SUCCESS;
|
---|
1627 | for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
|
---|
1628 | {
|
---|
1629 | uint8_t const *pbSeg = (uint8_t const *)pSgBuf->paSegs[iSeg].pvSeg;
|
---|
1630 | size_t cbSeg = pSgBuf->paSegs[iSeg].cbSeg;
|
---|
1631 | int cbNow;
|
---|
1632 | ssize_t cbWritten;
|
---|
1633 | for (;;)
|
---|
1634 | {
|
---|
1635 | cbNow = cbSeg >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbSeg;
|
---|
1636 | cbWritten = g_pfnsend(pThis->hNative, (const char *)pbSeg, cbNow, MSG_NOSIGNAL);
|
---|
1637 | if ((size_t)cbWritten >= cbSeg || cbWritten < 0)
|
---|
1638 | break;
|
---|
1639 | pbSeg += cbWritten;
|
---|
1640 | cbSeg -= cbWritten;
|
---|
1641 | }
|
---|
1642 | if (cbWritten < 0)
|
---|
1643 | {
|
---|
1644 | rc = rtSocketError();
|
---|
1645 | break;
|
---|
1646 | }
|
---|
1647 | }
|
---|
1648 | }
|
---|
1649 | else
|
---|
1650 | rc = VERR_NET_NOT_UNSUPPORTED;
|
---|
1651 |
|
---|
1652 | #else /* !RT_OS_WINDOWS */
|
---|
1653 | AssertCompileSize(struct iovec, sizeof(RTSGSEG));
|
---|
1654 | AssertCompileMemberSize(struct iovec, iov_base, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
1655 | AssertCompileMemberSize(struct iovec, iov_len, RT_SIZEOFMEMB(RTSGSEG, cbSeg));
|
---|
1656 |
|
---|
1657 | struct iovec *paMsg = (struct iovec *)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(struct iovec));
|
---|
1658 | if (paMsg)
|
---|
1659 | {
|
---|
1660 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
1661 | {
|
---|
1662 | paMsg[i].iov_base = pSgBuf->paSegs[i].pvSeg;
|
---|
1663 | paMsg[i].iov_len = pSgBuf->paSegs[i].cbSeg;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 | struct msghdr msgHdr;
|
---|
1667 | RT_ZERO(msgHdr);
|
---|
1668 | msgHdr.msg_iov = paMsg;
|
---|
1669 | msgHdr.msg_iovlen = pSgBuf->cSegs;
|
---|
1670 | ssize_t cbWritten = sendmsg(pThis->hNative, &msgHdr, MSG_NOSIGNAL);
|
---|
1671 | if (RT_LIKELY(cbWritten >= 0))
|
---|
1672 | rc = VINF_SUCCESS;
|
---|
1673 | /** @todo check for incomplete writes */
|
---|
1674 | else
|
---|
1675 | rc = rtSocketError();
|
---|
1676 |
|
---|
1677 | RTMemTmpFree(paMsg);
|
---|
1678 | }
|
---|
1679 | #endif /* !RT_OS_WINDOWS */
|
---|
1680 |
|
---|
1681 | rtSocketUnlock(pThis);
|
---|
1682 | return rc;
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 |
|
---|
1686 | RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...)
|
---|
1687 | {
|
---|
1688 | va_list va;
|
---|
1689 | va_start(va, cSegs);
|
---|
1690 | int rc = RTSocketSgWriteLV(hSocket, cSegs, va);
|
---|
1691 | va_end(va);
|
---|
1692 | return rc;
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 |
|
---|
1696 | RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va)
|
---|
1697 | {
|
---|
1698 | /*
|
---|
1699 | * Set up a S/G segment array + buffer on the stack and pass it
|
---|
1700 | * on to RTSocketSgWrite.
|
---|
1701 | */
|
---|
1702 | Assert(cSegs <= 16);
|
---|
1703 | PRTSGSEG paSegs = (PRTSGSEG)alloca(cSegs * sizeof(RTSGSEG));
|
---|
1704 | AssertReturn(paSegs, VERR_NO_TMP_MEMORY);
|
---|
1705 | for (size_t i = 0; i < cSegs; i++)
|
---|
1706 | {
|
---|
1707 | paSegs[i].pvSeg = va_arg(va, void *);
|
---|
1708 | paSegs[i].cbSeg = va_arg(va, size_t);
|
---|
1709 | }
|
---|
1710 |
|
---|
1711 | RTSGBUF SgBuf;
|
---|
1712 | RTSgBufInit(&SgBuf, paSegs, cSegs);
|
---|
1713 | return RTSocketSgWrite(hSocket, &SgBuf);
|
---|
1714 | }
|
---|
1715 |
|
---|
1716 |
|
---|
1717 | RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
1718 | {
|
---|
1719 | /*
|
---|
1720 | * Validate input.
|
---|
1721 | */
|
---|
1722 | RTSOCKETINT *pThis = hSocket;
|
---|
1723 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1724 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1725 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
1726 | AssertPtr(pvBuffer);
|
---|
1727 | AssertPtrReturn(pcbRead, VERR_INVALID_PARAMETER);
|
---|
1728 | #ifdef RT_OS_WINDOWS
|
---|
1729 | AssertReturn(g_pfnrecv, VERR_NET_NOT_UNSUPPORTED);
|
---|
1730 | #endif
|
---|
1731 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1732 |
|
---|
1733 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1734 | if (RT_FAILURE(rc))
|
---|
1735 | return rc;
|
---|
1736 |
|
---|
1737 | rtSocketErrorReset();
|
---|
1738 | #ifdef RTSOCKET_MAX_READ
|
---|
1739 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1740 | #else
|
---|
1741 | size_t cbNow = cbBuffer;
|
---|
1742 | #endif
|
---|
1743 |
|
---|
1744 | #ifdef RT_OS_WINDOWS
|
---|
1745 | int cbRead = g_pfnrecv(pThis->hNative, (char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1746 | if (cbRead >= 0)
|
---|
1747 | {
|
---|
1748 | *pcbRead = cbRead;
|
---|
1749 | rc = VINF_SUCCESS;
|
---|
1750 | }
|
---|
1751 | else
|
---|
1752 | {
|
---|
1753 | rc = rtSocketError();
|
---|
1754 | if (rc == VERR_TRY_AGAIN)
|
---|
1755 | {
|
---|
1756 | *pcbRead = 0;
|
---|
1757 | rc = VINF_TRY_AGAIN;
|
---|
1758 | }
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | #else
|
---|
1762 | ssize_t cbRead = recv(pThis->hNative, pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1763 | if (cbRead >= 0)
|
---|
1764 | *pcbRead = cbRead;
|
---|
1765 | else if ( errno == EAGAIN
|
---|
1766 | # ifdef EWOULDBLOCK
|
---|
1767 | # if EWOULDBLOCK != EAGAIN
|
---|
1768 | || errno == EWOULDBLOCK
|
---|
1769 | # endif
|
---|
1770 | # endif
|
---|
1771 | )
|
---|
1772 | {
|
---|
1773 | *pcbRead = 0;
|
---|
1774 | rc = VINF_TRY_AGAIN;
|
---|
1775 | }
|
---|
1776 | else
|
---|
1777 | rc = rtSocketError();
|
---|
1778 | #endif
|
---|
1779 |
|
---|
1780 | rtSocketUnlock(pThis);
|
---|
1781 | return rc;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 |
|
---|
1785 | RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten)
|
---|
1786 | {
|
---|
1787 | /*
|
---|
1788 | * Validate input.
|
---|
1789 | */
|
---|
1790 | RTSOCKETINT *pThis = hSocket;
|
---|
1791 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1792 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1793 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
1794 | #ifdef RT_OS_WINDOWS
|
---|
1795 | AssertReturn(g_pfnsend, VERR_NET_NOT_UNSUPPORTED);
|
---|
1796 | #endif
|
---|
1797 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1798 |
|
---|
1799 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1800 | if (RT_FAILURE(rc))
|
---|
1801 | return rc;
|
---|
1802 |
|
---|
1803 | rtSocketErrorReset();
|
---|
1804 | #ifdef RT_OS_WINDOWS
|
---|
1805 | # ifdef RTSOCKET_MAX_WRITE
|
---|
1806 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1807 | # else
|
---|
1808 | size_t cbNow = cbBuffer;
|
---|
1809 | # endif
|
---|
1810 | int cbWritten = g_pfnsend(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1811 | if (cbWritten >= 0)
|
---|
1812 | {
|
---|
1813 | *pcbWritten = cbWritten;
|
---|
1814 | rc = VINF_SUCCESS;
|
---|
1815 | }
|
---|
1816 | else
|
---|
1817 | {
|
---|
1818 | rc = rtSocketError();
|
---|
1819 | if (rc == VERR_TRY_AGAIN)
|
---|
1820 | {
|
---|
1821 | *pcbWritten = 0;
|
---|
1822 | rc = VINF_TRY_AGAIN;
|
---|
1823 | }
|
---|
1824 | }
|
---|
1825 | #else
|
---|
1826 | ssize_t cbWritten = send(pThis->hNative, pvBuffer, cbBuffer, MSG_NOSIGNAL);
|
---|
1827 | if (cbWritten >= 0)
|
---|
1828 | *pcbWritten = cbWritten;
|
---|
1829 | else if ( errno == EAGAIN
|
---|
1830 | # ifdef EWOULDBLOCK
|
---|
1831 | # if EWOULDBLOCK != EAGAIN
|
---|
1832 | || errno == EWOULDBLOCK
|
---|
1833 | # endif
|
---|
1834 | # endif
|
---|
1835 | )
|
---|
1836 | {
|
---|
1837 | *pcbWritten = 0;
|
---|
1838 | rc = VINF_TRY_AGAIN;
|
---|
1839 | }
|
---|
1840 | else
|
---|
1841 | rc = rtSocketError();
|
---|
1842 | #endif
|
---|
1843 |
|
---|
1844 | rtSocketUnlock(pThis);
|
---|
1845 | return rc;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 |
|
---|
1849 | RTDECL(int) RTSocketSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten)
|
---|
1850 | {
|
---|
1851 | /*
|
---|
1852 | * Validate input.
|
---|
1853 | */
|
---|
1854 | RTSOCKETINT *pThis = hSocket;
|
---|
1855 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1856 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1857 | AssertPtrReturn(pSgBuf, VERR_INVALID_PARAMETER);
|
---|
1858 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
1859 | AssertReturn(pSgBuf->cSegs > 0, VERR_INVALID_PARAMETER);
|
---|
1860 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1861 |
|
---|
1862 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1863 | if (RT_FAILURE(rc))
|
---|
1864 | return rc;
|
---|
1865 |
|
---|
1866 | unsigned cSegsToSend = 0;
|
---|
1867 | rc = VERR_NO_TMP_MEMORY;
|
---|
1868 | #ifdef RT_OS_WINDOWS
|
---|
1869 | if (g_pfnWSASend)
|
---|
1870 | {
|
---|
1871 | LPWSABUF paMsg = NULL;
|
---|
1872 | RTSgBufMapToNative(paMsg, pSgBuf, WSABUF, buf, char *, len, u_long, cSegsToSend);
|
---|
1873 | if (paMsg)
|
---|
1874 | {
|
---|
1875 | DWORD dwSent = 0;
|
---|
1876 | int hrc = g_pfnWSASend(pThis->hNative, paMsg, cSegsToSend, &dwSent, MSG_NOSIGNAL, NULL, NULL);
|
---|
1877 | if (!hrc)
|
---|
1878 | rc = VINF_SUCCESS;
|
---|
1879 | else
|
---|
1880 | rc = rtSocketError();
|
---|
1881 |
|
---|
1882 | *pcbWritten = dwSent;
|
---|
1883 |
|
---|
1884 | RTMemTmpFree(paMsg);
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 | else if (g_pfnsend)
|
---|
1888 | {
|
---|
1889 | size_t cbWrittenTotal = 0;
|
---|
1890 | rc = VINF_SUCCESS;
|
---|
1891 | for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
|
---|
1892 | {
|
---|
1893 | uint8_t const *pbSeg = (uint8_t const *)pSgBuf->paSegs[iSeg].pvSeg;
|
---|
1894 | size_t cbSeg = pSgBuf->paSegs[iSeg].cbSeg;
|
---|
1895 | int cbNow;
|
---|
1896 | ssize_t cbWritten;
|
---|
1897 | for (;;)
|
---|
1898 | {
|
---|
1899 | cbNow = cbSeg >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbSeg;
|
---|
1900 | cbWritten = g_pfnsend(pThis->hNative, (const char *)pbSeg, cbNow, MSG_NOSIGNAL);
|
---|
1901 | if ((size_t)cbWritten >= cbSeg || cbWritten < 0)
|
---|
1902 | break;
|
---|
1903 | cbWrittenTotal += cbWrittenTotal;
|
---|
1904 | pbSeg += cbWritten;
|
---|
1905 | cbSeg -= cbWritten;
|
---|
1906 | }
|
---|
1907 | if (cbWritten < 0)
|
---|
1908 | {
|
---|
1909 | rc = rtSocketError();
|
---|
1910 | break;
|
---|
1911 | }
|
---|
1912 | if (cbWritten != cbNow)
|
---|
1913 | break;
|
---|
1914 | }
|
---|
1915 | *pcbWritten = cbWrittenTotal;
|
---|
1916 | }
|
---|
1917 | else
|
---|
1918 | rc = VERR_NET_NOT_UNSUPPORTED;
|
---|
1919 |
|
---|
1920 | #else /* !RT_OS_WINDOWS */
|
---|
1921 | struct iovec *paMsg = NULL;
|
---|
1922 |
|
---|
1923 | RTSgBufMapToNative(paMsg, pSgBuf, struct iovec, iov_base, void *, iov_len, size_t, cSegsToSend);
|
---|
1924 | if (paMsg)
|
---|
1925 | {
|
---|
1926 | struct msghdr msgHdr;
|
---|
1927 | RT_ZERO(msgHdr);
|
---|
1928 | msgHdr.msg_iov = paMsg;
|
---|
1929 | msgHdr.msg_iovlen = cSegsToSend;
|
---|
1930 | ssize_t cbWritten = sendmsg(pThis->hNative, &msgHdr, MSG_NOSIGNAL);
|
---|
1931 | if (RT_LIKELY(cbWritten >= 0))
|
---|
1932 | {
|
---|
1933 | rc = VINF_SUCCESS;
|
---|
1934 | *pcbWritten = cbWritten;
|
---|
1935 | }
|
---|
1936 | else
|
---|
1937 | rc = rtSocketError();
|
---|
1938 |
|
---|
1939 | RTMemTmpFree(paMsg);
|
---|
1940 | }
|
---|
1941 | #endif /* !RT_OS_WINDOWS */
|
---|
1942 |
|
---|
1943 | rtSocketUnlock(pThis);
|
---|
1944 | return rc;
|
---|
1945 | }
|
---|
1946 |
|
---|
1947 |
|
---|
1948 | RTDECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...)
|
---|
1949 | {
|
---|
1950 | va_list va;
|
---|
1951 | va_start(va, pcbWritten);
|
---|
1952 | int rc = RTSocketSgWriteLVNB(hSocket, cSegs, pcbWritten, va);
|
---|
1953 | va_end(va);
|
---|
1954 | return rc;
|
---|
1955 | }
|
---|
1956 |
|
---|
1957 |
|
---|
1958 | RTDECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va)
|
---|
1959 | {
|
---|
1960 | /*
|
---|
1961 | * Set up a S/G segment array + buffer on the stack and pass it
|
---|
1962 | * on to RTSocketSgWrite.
|
---|
1963 | */
|
---|
1964 | Assert(cSegs <= 16);
|
---|
1965 | PRTSGSEG paSegs = (PRTSGSEG)alloca(cSegs * sizeof(RTSGSEG));
|
---|
1966 | AssertReturn(paSegs, VERR_NO_TMP_MEMORY);
|
---|
1967 | for (size_t i = 0; i < cSegs; i++)
|
---|
1968 | {
|
---|
1969 | paSegs[i].pvSeg = va_arg(va, void *);
|
---|
1970 | paSegs[i].cbSeg = va_arg(va, size_t);
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 | RTSGBUF SgBuf;
|
---|
1974 | RTSgBufInit(&SgBuf, paSegs, cSegs);
|
---|
1975 | return RTSocketSgWriteNB(hSocket, &SgBuf, pcbWritten);
|
---|
1976 | }
|
---|
1977 |
|
---|
1978 |
|
---|
1979 | RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies)
|
---|
1980 | {
|
---|
1981 | /*
|
---|
1982 | * Validate input.
|
---|
1983 | */
|
---|
1984 | RTSOCKETINT *pThis = hSocket;
|
---|
1985 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1986 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1987 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1988 | int const fdMax = (int)pThis->hNative + 1;
|
---|
1989 | AssertReturn((RTSOCKETNATIVE)(fdMax - 1) == pThis->hNative, VERR_INTERNAL_ERROR_5);
|
---|
1990 | #ifdef RT_OS_WINDOWS
|
---|
1991 | AssertReturn(g_pfnselect, VERR_NET_NOT_UNSUPPORTED);
|
---|
1992 | # define select g_pfnselect
|
---|
1993 | #endif
|
---|
1994 |
|
---|
1995 | /*
|
---|
1996 | * Set up the file descriptor sets and do the select.
|
---|
1997 | */
|
---|
1998 | fd_set fdsetR;
|
---|
1999 | FD_ZERO(&fdsetR);
|
---|
2000 | FD_SET(pThis->hNative, &fdsetR);
|
---|
2001 |
|
---|
2002 | fd_set fdsetE = fdsetR;
|
---|
2003 |
|
---|
2004 | int rc;
|
---|
2005 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
2006 | rc = select(fdMax, &fdsetR, NULL, &fdsetE, NULL);
|
---|
2007 | else
|
---|
2008 | {
|
---|
2009 | struct timeval timeout;
|
---|
2010 | timeout.tv_sec = cMillies / 1000;
|
---|
2011 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
2012 | rc = select(fdMax, &fdsetR, NULL, &fdsetE, &timeout);
|
---|
2013 | }
|
---|
2014 | if (rc > 0)
|
---|
2015 | rc = VINF_SUCCESS;
|
---|
2016 | else if (rc == 0)
|
---|
2017 | rc = VERR_TIMEOUT;
|
---|
2018 | else
|
---|
2019 | rc = rtSocketError();
|
---|
2020 |
|
---|
2021 | #ifdef RT_OS_WINDOWS
|
---|
2022 | # undef select
|
---|
2023 | #endif
|
---|
2024 | return rc;
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 |
|
---|
2028 | /**
|
---|
2029 | * Internal worker for RTSocketSelectOneEx and rtSocketPollCheck (fallback)
|
---|
2030 | *
|
---|
2031 | * @returns IPRT status code
|
---|
2032 | * @param pThis The socket (valid).
|
---|
2033 | * @param fEvents The events to select for.
|
---|
2034 | * @param pfEvents Where to return the events.
|
---|
2035 | * @param cMillies How long to select for, in milliseconds.
|
---|
2036 | */
|
---|
2037 | static int rtSocketSelectOneEx(RTSOCKET pThis, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies)
|
---|
2038 | {
|
---|
2039 | RTSOCKETNATIVE hNative = pThis->hNative;
|
---|
2040 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
2041 | {
|
---|
2042 | /* Socket is already closed? Possible we raced someone calling rtSocketCloseIt.
|
---|
2043 | Should we return a different status code? */
|
---|
2044 | *pfEvents = RTSOCKET_EVT_ERROR;
|
---|
2045 | return VINF_SUCCESS;
|
---|
2046 | }
|
---|
2047 |
|
---|
2048 | int const fdMax = (int)hNative + 1;
|
---|
2049 | AssertReturn((RTSOCKETNATIVE)(fdMax - 1) == hNative, VERR_INTERNAL_ERROR_5);
|
---|
2050 | #ifdef RT_OS_WINDOWS
|
---|
2051 | AssertReturn(g_pfnselect, VERR_NET_NOT_UNSUPPORTED);
|
---|
2052 | AssertReturn(g_pfn__WSAFDIsSet, VERR_NET_NOT_UNSUPPORTED);
|
---|
2053 | # define select g_pfnselect
|
---|
2054 | # define __WSAFDIsSet g_pfn__WSAFDIsSet
|
---|
2055 | #endif
|
---|
2056 |
|
---|
2057 | *pfEvents = 0;
|
---|
2058 |
|
---|
2059 | /*
|
---|
2060 | * Set up the file descriptor sets and do the select.
|
---|
2061 | */
|
---|
2062 | fd_set fdsetR;
|
---|
2063 | fd_set fdsetW;
|
---|
2064 | fd_set fdsetE;
|
---|
2065 | FD_ZERO(&fdsetR);
|
---|
2066 | FD_ZERO(&fdsetW);
|
---|
2067 | FD_ZERO(&fdsetE);
|
---|
2068 |
|
---|
2069 | if (fEvents & RTSOCKET_EVT_READ)
|
---|
2070 | FD_SET(hNative, &fdsetR);
|
---|
2071 | if (fEvents & RTSOCKET_EVT_WRITE)
|
---|
2072 | FD_SET(hNative, &fdsetW);
|
---|
2073 | if (fEvents & RTSOCKET_EVT_ERROR)
|
---|
2074 | FD_SET(hNative, &fdsetE);
|
---|
2075 |
|
---|
2076 | int rc;
|
---|
2077 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
2078 | rc = select(fdMax, &fdsetR, &fdsetW, &fdsetE, NULL);
|
---|
2079 | else
|
---|
2080 | {
|
---|
2081 | struct timeval timeout;
|
---|
2082 | timeout.tv_sec = cMillies / 1000;
|
---|
2083 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
2084 | rc = select(fdMax, &fdsetR, &fdsetW, &fdsetE, &timeout);
|
---|
2085 | }
|
---|
2086 | if (rc > 0)
|
---|
2087 | {
|
---|
2088 | if (pThis->hNative == hNative)
|
---|
2089 | {
|
---|
2090 | if (FD_ISSET(hNative, &fdsetR))
|
---|
2091 | *pfEvents |= RTSOCKET_EVT_READ;
|
---|
2092 | if (FD_ISSET(hNative, &fdsetW))
|
---|
2093 | *pfEvents |= RTSOCKET_EVT_WRITE;
|
---|
2094 | if (FD_ISSET(hNative, &fdsetE))
|
---|
2095 | *pfEvents |= RTSOCKET_EVT_ERROR;
|
---|
2096 | rc = VINF_SUCCESS;
|
---|
2097 | }
|
---|
2098 | else
|
---|
2099 | {
|
---|
2100 | /* Socket was closed while we waited (rtSocketCloseIt). Different status code? */
|
---|
2101 | *pfEvents = RTSOCKET_EVT_ERROR;
|
---|
2102 | rc = VINF_SUCCESS;
|
---|
2103 | }
|
---|
2104 | }
|
---|
2105 | else if (rc == 0)
|
---|
2106 | rc = VERR_TIMEOUT;
|
---|
2107 | else
|
---|
2108 | rc = rtSocketError();
|
---|
2109 |
|
---|
2110 | #ifdef RT_OS_WINDOWS
|
---|
2111 | # undef select
|
---|
2112 | # undef __WSAFDIsSet
|
---|
2113 | #endif
|
---|
2114 | return rc;
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 |
|
---|
2118 | RTDECL(int) RTSocketSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies)
|
---|
2119 | {
|
---|
2120 | /*
|
---|
2121 | * Validate input.
|
---|
2122 | */
|
---|
2123 | RTSOCKETINT *pThis = hSocket;
|
---|
2124 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2125 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2126 | AssertPtrReturn(pfEvents, VERR_INVALID_PARAMETER);
|
---|
2127 | AssertReturn(!(fEvents & ~RTSOCKET_EVT_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
2128 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
2129 |
|
---|
2130 | return rtSocketSelectOneEx(pThis, fEvents, pfEvents, cMillies);
|
---|
2131 | }
|
---|
2132 |
|
---|
2133 |
|
---|
2134 | RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite)
|
---|
2135 | {
|
---|
2136 | /*
|
---|
2137 | * Validate input, don't lock it because we might want to interrupt a call
|
---|
2138 | * active on a different thread.
|
---|
2139 | */
|
---|
2140 | RTSOCKETINT *pThis = hSocket;
|
---|
2141 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2142 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2143 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
2144 | AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
|
---|
2145 | #ifdef RT_OS_WINDOWS
|
---|
2146 | AssertReturn(g_pfnshutdown, VERR_NET_NOT_UNSUPPORTED);
|
---|
2147 | # define shutdown g_pfnshutdown
|
---|
2148 | #endif
|
---|
2149 |
|
---|
2150 | /*
|
---|
2151 | * Do the job.
|
---|
2152 | */
|
---|
2153 | int rc = VINF_SUCCESS;
|
---|
2154 | int fHow;
|
---|
2155 | if (fRead && fWrite)
|
---|
2156 | fHow = SHUT_RDWR;
|
---|
2157 | else if (fRead)
|
---|
2158 | fHow = SHUT_RD;
|
---|
2159 | else
|
---|
2160 | fHow = SHUT_WR;
|
---|
2161 | if (shutdown(pThis->hNative, fHow) == -1)
|
---|
2162 | rc = rtSocketError();
|
---|
2163 |
|
---|
2164 | #ifdef RT_OS_WINDOWS
|
---|
2165 | # undef shutdown
|
---|
2166 | #endif
|
---|
2167 | return rc;
|
---|
2168 | }
|
---|
2169 |
|
---|
2170 |
|
---|
2171 | RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
2172 | {
|
---|
2173 | /*
|
---|
2174 | * Validate input.
|
---|
2175 | */
|
---|
2176 | RTSOCKETINT *pThis = hSocket;
|
---|
2177 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2178 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2179 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
2180 | #ifdef RT_OS_WINDOWS
|
---|
2181 | AssertReturn(g_pfngetsockname, VERR_NET_NOT_UNSUPPORTED);
|
---|
2182 | # define getsockname g_pfngetsockname
|
---|
2183 | #endif
|
---|
2184 |
|
---|
2185 | /*
|
---|
2186 | * Get the address and convert it.
|
---|
2187 | */
|
---|
2188 | int rc;
|
---|
2189 | RTSOCKADDRUNION u;
|
---|
2190 | #ifdef RT_OS_WINDOWS
|
---|
2191 | int cbAddr = sizeof(u);
|
---|
2192 | #else
|
---|
2193 | socklen_t cbAddr = sizeof(u);
|
---|
2194 | #endif
|
---|
2195 | RT_ZERO(u);
|
---|
2196 | if (getsockname(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
2197 | rc = rtSocketNetAddrFromAddr(&u, cbAddr, pAddr);
|
---|
2198 | else
|
---|
2199 | rc = rtSocketError();
|
---|
2200 |
|
---|
2201 | #ifdef RT_OS_WINDOWS
|
---|
2202 | # undef getsockname
|
---|
2203 | #endif
|
---|
2204 | return rc;
|
---|
2205 | }
|
---|
2206 |
|
---|
2207 |
|
---|
2208 | RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
2209 | {
|
---|
2210 | /*
|
---|
2211 | * Validate input.
|
---|
2212 | */
|
---|
2213 | RTSOCKETINT *pThis = hSocket;
|
---|
2214 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2215 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2216 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
2217 | #ifdef RT_OS_WINDOWS
|
---|
2218 | AssertReturn(g_pfngetpeername, VERR_NET_NOT_UNSUPPORTED);
|
---|
2219 | # define getpeername g_pfngetpeername
|
---|
2220 | #endif
|
---|
2221 |
|
---|
2222 | /*
|
---|
2223 | * Get the address and convert it.
|
---|
2224 | */
|
---|
2225 | int rc;
|
---|
2226 | RTSOCKADDRUNION u;
|
---|
2227 | #ifdef RT_OS_WINDOWS
|
---|
2228 | int cbAddr = sizeof(u);
|
---|
2229 | #else
|
---|
2230 | socklen_t cbAddr = sizeof(u);
|
---|
2231 | #endif
|
---|
2232 | RT_ZERO(u);
|
---|
2233 | if (getpeername(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
2234 | rc = rtSocketNetAddrFromAddr(&u, cbAddr, pAddr);
|
---|
2235 | else
|
---|
2236 | rc = rtSocketError();
|
---|
2237 |
|
---|
2238 | #ifdef RT_OS_WINDOWS
|
---|
2239 | # undef getpeername
|
---|
2240 | #endif
|
---|
2241 | return rc;
|
---|
2242 | }
|
---|
2243 |
|
---|
2244 |
|
---|
2245 |
|
---|
2246 | /**
|
---|
2247 | * Wrapper around bind.
|
---|
2248 | *
|
---|
2249 | * @returns IPRT status code.
|
---|
2250 | * @param hSocket The socket handle.
|
---|
2251 | * @param pAddr The address to bind to.
|
---|
2252 | */
|
---|
2253 | DECLHIDDEN(int) rtSocketBind(RTSOCKET hSocket, PCRTNETADDR pAddr)
|
---|
2254 | {
|
---|
2255 | RTSOCKADDRUNION u;
|
---|
2256 | int cbAddr;
|
---|
2257 | int rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), &cbAddr);
|
---|
2258 | if (RT_SUCCESS(rc))
|
---|
2259 | rc = rtSocketBindRawAddr(hSocket, &u.Addr, cbAddr);
|
---|
2260 | return rc;
|
---|
2261 | }
|
---|
2262 |
|
---|
2263 |
|
---|
2264 | /**
|
---|
2265 | * Very thin wrapper around bind.
|
---|
2266 | *
|
---|
2267 | * @returns IPRT status code.
|
---|
2268 | * @param hSocket The socket handle.
|
---|
2269 | * @param pvAddr The address to bind to (struct sockaddr and
|
---|
2270 | * friends).
|
---|
2271 | * @param cbAddr The size of the address.
|
---|
2272 | */
|
---|
2273 | DECLHIDDEN(int) rtSocketBindRawAddr(RTSOCKET hSocket, void const *pvAddr, size_t cbAddr)
|
---|
2274 | {
|
---|
2275 | /*
|
---|
2276 | * Validate input.
|
---|
2277 | */
|
---|
2278 | RTSOCKETINT *pThis = hSocket;
|
---|
2279 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2280 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2281 | AssertPtrReturn(pvAddr, VERR_INVALID_POINTER);
|
---|
2282 | #ifdef RT_OS_WINDOWS
|
---|
2283 | AssertReturn(g_pfnbind, VERR_NET_NOT_UNSUPPORTED);
|
---|
2284 | # define bind g_pfnbind
|
---|
2285 | #endif
|
---|
2286 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2287 |
|
---|
2288 | int rc;
|
---|
2289 | if (bind(pThis->hNative, (struct sockaddr const *)pvAddr, (int)cbAddr) == 0)
|
---|
2290 | rc = VINF_SUCCESS;
|
---|
2291 | else
|
---|
2292 | rc = rtSocketError();
|
---|
2293 |
|
---|
2294 | rtSocketUnlock(pThis);
|
---|
2295 | #ifdef RT_OS_WINDOWS
|
---|
2296 | # undef bind
|
---|
2297 | #endif
|
---|
2298 | return rc;
|
---|
2299 | }
|
---|
2300 |
|
---|
2301 |
|
---|
2302 |
|
---|
2303 | /**
|
---|
2304 | * Wrapper around listen.
|
---|
2305 | *
|
---|
2306 | * @returns IPRT status code.
|
---|
2307 | * @param hSocket The socket handle.
|
---|
2308 | * @param cMaxPending The max number of pending connections.
|
---|
2309 | */
|
---|
2310 | DECLHIDDEN(int) rtSocketListen(RTSOCKET hSocket, int cMaxPending)
|
---|
2311 | {
|
---|
2312 | /*
|
---|
2313 | * Validate input.
|
---|
2314 | */
|
---|
2315 | RTSOCKETINT *pThis = hSocket;
|
---|
2316 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2317 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2318 | #ifdef RT_OS_WINDOWS
|
---|
2319 | AssertReturn(g_pfnlisten, VERR_NET_NOT_UNSUPPORTED);
|
---|
2320 | # define listen g_pfnlisten
|
---|
2321 | #endif
|
---|
2322 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2323 |
|
---|
2324 | int rc = VINF_SUCCESS;
|
---|
2325 | if (listen(pThis->hNative, cMaxPending) != 0)
|
---|
2326 | rc = rtSocketError();
|
---|
2327 |
|
---|
2328 | rtSocketUnlock(pThis);
|
---|
2329 | #ifdef RT_OS_WINDOWS
|
---|
2330 | # undef listen
|
---|
2331 | #endif
|
---|
2332 | return rc;
|
---|
2333 | }
|
---|
2334 |
|
---|
2335 |
|
---|
2336 | /**
|
---|
2337 | * Wrapper around accept.
|
---|
2338 | *
|
---|
2339 | * @returns IPRT status code.
|
---|
2340 | * @param hSocket The socket handle.
|
---|
2341 | * @param phClient Where to return the client socket handle on
|
---|
2342 | * success.
|
---|
2343 | * @param pAddr Where to return the client address.
|
---|
2344 | * @param pcbAddr On input this gives the size buffer size of what
|
---|
2345 | * @a pAddr point to. On return this contains the
|
---|
2346 | * size of what's stored at @a pAddr.
|
---|
2347 | */
|
---|
2348 | DECLHIDDEN(int) rtSocketAccept(RTSOCKET hSocket, PRTSOCKET phClient, struct sockaddr *pAddr, size_t *pcbAddr)
|
---|
2349 | {
|
---|
2350 | /*
|
---|
2351 | * Validate input.
|
---|
2352 | * Only lock the socket temporarily while we get the native handle, so that
|
---|
2353 | * we can safely shutdown and destroy the socket from a different thread.
|
---|
2354 | */
|
---|
2355 | RTSOCKETINT *pThis = hSocket;
|
---|
2356 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2357 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2358 | #ifdef RT_OS_WINDOWS
|
---|
2359 | AssertReturn(g_pfnaccept, VERR_NET_NOT_UNSUPPORTED);
|
---|
2360 | AssertReturn(g_pfnclosesocket, VERR_NET_NOT_UNSUPPORTED);
|
---|
2361 | # define accept g_pfnaccept
|
---|
2362 | #endif
|
---|
2363 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2364 |
|
---|
2365 | /*
|
---|
2366 | * Call accept().
|
---|
2367 | */
|
---|
2368 | rtSocketErrorReset();
|
---|
2369 | int rc = VINF_SUCCESS;
|
---|
2370 | #ifdef RT_OS_WINDOWS
|
---|
2371 | int cbAddr = (int)*pcbAddr;
|
---|
2372 | #else
|
---|
2373 | socklen_t cbAddr = *pcbAddr;
|
---|
2374 | #endif
|
---|
2375 | RTSOCKETNATIVE hNativeClient = accept(pThis->hNative, pAddr, &cbAddr);
|
---|
2376 | if (hNativeClient != NIL_RTSOCKETNATIVE)
|
---|
2377 | {
|
---|
2378 | *pcbAddr = cbAddr;
|
---|
2379 |
|
---|
2380 | /*
|
---|
2381 | * Wrap the client socket.
|
---|
2382 | */
|
---|
2383 | rc = rtSocketCreateForNative(phClient, hNativeClient, false /*fLeaveOpen*/);
|
---|
2384 | if (RT_FAILURE(rc))
|
---|
2385 | {
|
---|
2386 | #ifdef RT_OS_WINDOWS
|
---|
2387 | g_pfnclosesocket(hNativeClient);
|
---|
2388 | #else
|
---|
2389 | close(hNativeClient);
|
---|
2390 | #endif
|
---|
2391 | }
|
---|
2392 | }
|
---|
2393 | else
|
---|
2394 | rc = rtSocketError();
|
---|
2395 |
|
---|
2396 | rtSocketUnlock(pThis);
|
---|
2397 | #ifdef RT_OS_WINDOWS
|
---|
2398 | # undef accept
|
---|
2399 | #endif
|
---|
2400 | return rc;
|
---|
2401 | }
|
---|
2402 |
|
---|
2403 |
|
---|
2404 | /**
|
---|
2405 | * Wrapper around connect.
|
---|
2406 | *
|
---|
2407 | * @returns IPRT status code.
|
---|
2408 | * @param hSocket The socket handle.
|
---|
2409 | * @param pAddr The socket address to connect to.
|
---|
2410 | * @param cMillies Number of milliseconds to wait for the connect attempt to complete.
|
---|
2411 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
2412 | * Use RT_TCPCLIENTCONNECT_DEFAULT_WAIT to wait for the default time
|
---|
2413 | * configured on the running system.
|
---|
2414 | */
|
---|
2415 | DECLHIDDEN(int) rtSocketConnect(RTSOCKET hSocket, PCRTNETADDR pAddr, RTMSINTERVAL cMillies)
|
---|
2416 | {
|
---|
2417 | /*
|
---|
2418 | * Validate input.
|
---|
2419 | */
|
---|
2420 | RTSOCKETINT *pThis = hSocket;
|
---|
2421 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2422 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2423 | #ifdef RT_OS_WINDOWS
|
---|
2424 | AssertReturn(g_pfnconnect, VERR_NET_NOT_UNSUPPORTED);
|
---|
2425 | AssertReturn(g_pfnselect, VERR_NET_NOT_UNSUPPORTED);
|
---|
2426 | AssertReturn(g_pfngetsockopt, VERR_NET_NOT_UNSUPPORTED);
|
---|
2427 | # define connect g_pfnconnect
|
---|
2428 | # define select g_pfnselect
|
---|
2429 | # define getsockopt g_pfngetsockopt
|
---|
2430 | #endif
|
---|
2431 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2432 |
|
---|
2433 | RTSOCKADDRUNION u;
|
---|
2434 | int cbAddr;
|
---|
2435 | int rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), &cbAddr);
|
---|
2436 | if (RT_SUCCESS(rc))
|
---|
2437 | {
|
---|
2438 | if (cMillies == RT_SOCKETCONNECT_DEFAULT_WAIT)
|
---|
2439 | {
|
---|
2440 | if (connect(pThis->hNative, &u.Addr, cbAddr) != 0)
|
---|
2441 | rc = rtSocketError();
|
---|
2442 | }
|
---|
2443 | else
|
---|
2444 | {
|
---|
2445 | /*
|
---|
2446 | * Switch the socket to nonblocking mode, initiate the connect
|
---|
2447 | * and wait for the socket to become writable or until the timeout
|
---|
2448 | * expires.
|
---|
2449 | */
|
---|
2450 | rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
2451 | if (RT_SUCCESS(rc))
|
---|
2452 | {
|
---|
2453 | if (connect(pThis->hNative, &u.Addr, cbAddr) != 0)
|
---|
2454 | {
|
---|
2455 | rc = rtSocketError();
|
---|
2456 | if (rc == VERR_TRY_AGAIN || rc == VERR_NET_IN_PROGRESS)
|
---|
2457 | {
|
---|
2458 | int rcSock = 0;
|
---|
2459 | fd_set FdSetWriteable;
|
---|
2460 | struct timeval TvTimeout;
|
---|
2461 |
|
---|
2462 | TvTimeout.tv_sec = cMillies / RT_MS_1SEC;
|
---|
2463 | TvTimeout.tv_usec = (cMillies % RT_MS_1SEC) * RT_US_1MS;
|
---|
2464 |
|
---|
2465 | FD_ZERO(&FdSetWriteable);
|
---|
2466 | FD_SET(pThis->hNative, &FdSetWriteable);
|
---|
2467 | do
|
---|
2468 | {
|
---|
2469 | rcSock = select(pThis->hNative + 1, NULL, &FdSetWriteable, NULL,
|
---|
2470 | cMillies == RT_INDEFINITE_WAIT || cMillies >= INT_MAX
|
---|
2471 | ? NULL
|
---|
2472 | : &TvTimeout);
|
---|
2473 | if (rcSock > 0)
|
---|
2474 | {
|
---|
2475 | int iSockError = 0;
|
---|
2476 | socklen_t cbSockOpt = sizeof(iSockError);
|
---|
2477 | rcSock = getsockopt(pThis->hNative, SOL_SOCKET, SO_ERROR, (char *)&iSockError, &cbSockOpt);
|
---|
2478 | if (rcSock == 0)
|
---|
2479 | {
|
---|
2480 | if (iSockError == 0)
|
---|
2481 | rc = VINF_SUCCESS;
|
---|
2482 | else
|
---|
2483 | {
|
---|
2484 | #ifdef RT_OS_WINDOWS
|
---|
2485 | rc = RTErrConvertFromWin32(iSockError);
|
---|
2486 | #else
|
---|
2487 | rc = RTErrConvertFromErrno(iSockError);
|
---|
2488 | #endif
|
---|
2489 | }
|
---|
2490 | }
|
---|
2491 | else
|
---|
2492 | rc = rtSocketError();
|
---|
2493 | }
|
---|
2494 | else if (rcSock == 0)
|
---|
2495 | rc = VERR_TIMEOUT;
|
---|
2496 | else
|
---|
2497 | rc = rtSocketError();
|
---|
2498 | } while (rc == VERR_INTERRUPTED);
|
---|
2499 | }
|
---|
2500 | }
|
---|
2501 |
|
---|
2502 | rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
2503 | }
|
---|
2504 | }
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | rtSocketUnlock(pThis);
|
---|
2508 | #ifdef RT_OS_WINDOWS
|
---|
2509 | # undef connect
|
---|
2510 | # undef select
|
---|
2511 | # undef getsockopt
|
---|
2512 | #endif
|
---|
2513 | return rc;
|
---|
2514 | }
|
---|
2515 |
|
---|
2516 |
|
---|
2517 | /**
|
---|
2518 | * Wrapper around connect, raw address, no timeout.
|
---|
2519 | *
|
---|
2520 | * @returns IPRT status code.
|
---|
2521 | * @param hSocket The socket handle.
|
---|
2522 | * @param pvAddr The raw socket address to connect to.
|
---|
2523 | * @param cbAddr The size of the raw address.
|
---|
2524 | */
|
---|
2525 | DECLHIDDEN(int) rtSocketConnectRaw(RTSOCKET hSocket, void const *pvAddr, size_t cbAddr)
|
---|
2526 | {
|
---|
2527 | /*
|
---|
2528 | * Validate input.
|
---|
2529 | */
|
---|
2530 | RTSOCKETINT *pThis = hSocket;
|
---|
2531 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2532 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2533 | #ifdef RT_OS_WINDOWS
|
---|
2534 | AssertReturn(g_pfnconnect, VERR_NET_NOT_UNSUPPORTED);
|
---|
2535 | # define connect g_pfnconnect
|
---|
2536 | #endif
|
---|
2537 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2538 |
|
---|
2539 | int rc;
|
---|
2540 | if (connect(pThis->hNative, (const struct sockaddr *)pvAddr, (int)cbAddr) == 0)
|
---|
2541 | rc = VINF_SUCCESS;
|
---|
2542 | else
|
---|
2543 | rc = rtSocketError();
|
---|
2544 |
|
---|
2545 | rtSocketUnlock(pThis);
|
---|
2546 | #ifdef RT_OS_WINDOWS
|
---|
2547 | # undef connect
|
---|
2548 | #endif
|
---|
2549 | return rc;
|
---|
2550 | }
|
---|
2551 |
|
---|
2552 |
|
---|
2553 | /**
|
---|
2554 | * Wrapper around setsockopt.
|
---|
2555 | *
|
---|
2556 | * @returns IPRT status code.
|
---|
2557 | * @param hSocket The socket handle.
|
---|
2558 | * @param iLevel The protocol level, e.g. IPPORTO_TCP.
|
---|
2559 | * @param iOption The option, e.g. TCP_NODELAY.
|
---|
2560 | * @param pvValue The value buffer.
|
---|
2561 | * @param cbValue The size of the value pointed to by pvValue.
|
---|
2562 | */
|
---|
2563 | DECLHIDDEN(int) rtSocketSetOpt(RTSOCKET hSocket, int iLevel, int iOption, void const *pvValue, int cbValue)
|
---|
2564 | {
|
---|
2565 | /*
|
---|
2566 | * Validate input.
|
---|
2567 | */
|
---|
2568 | RTSOCKETINT *pThis = hSocket;
|
---|
2569 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2570 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2571 | #ifdef RT_OS_WINDOWS
|
---|
2572 | AssertReturn(g_pfnsetsockopt, VERR_NET_NOT_UNSUPPORTED);
|
---|
2573 | # define setsockopt g_pfnsetsockopt
|
---|
2574 | #endif
|
---|
2575 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2576 |
|
---|
2577 | int rc = VINF_SUCCESS;
|
---|
2578 | if (setsockopt(pThis->hNative, iLevel, iOption, (const char *)pvValue, cbValue) != 0)
|
---|
2579 | rc = rtSocketError();
|
---|
2580 |
|
---|
2581 | rtSocketUnlock(pThis);
|
---|
2582 | #ifdef RT_OS_WINDOWS
|
---|
2583 | # undef setsockopt
|
---|
2584 | #endif
|
---|
2585 | return rc;
|
---|
2586 | }
|
---|
2587 |
|
---|
2588 |
|
---|
2589 | /**
|
---|
2590 | * Internal RTPollSetAdd helper that returns the handle that should be added to
|
---|
2591 | * the pollset.
|
---|
2592 | *
|
---|
2593 | * @returns Valid handle on success, INVALID_HANDLE_VALUE on failure.
|
---|
2594 | * @param hSocket The socket handle.
|
---|
2595 | * @param fEvents The events we're polling for.
|
---|
2596 | * @param phNative Where to put the primary handle.
|
---|
2597 | */
|
---|
2598 | DECLHIDDEN(int) rtSocketPollGetHandle(RTSOCKET hSocket, uint32_t fEvents, PRTHCINTPTR phNative)
|
---|
2599 | {
|
---|
2600 | RTSOCKETINT *pThis = hSocket;
|
---|
2601 | RT_NOREF_PV(fEvents);
|
---|
2602 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
2603 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
2604 | #ifdef RT_OS_WINDOWS
|
---|
2605 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
2606 |
|
---|
2607 | int rc = VINF_SUCCESS;
|
---|
2608 | if (pThis->hEvent != WSA_INVALID_EVENT)
|
---|
2609 | *phNative = (RTHCINTPTR)pThis->hEvent;
|
---|
2610 | else if (g_pfnWSACreateEvent)
|
---|
2611 | {
|
---|
2612 | pThis->hEvent = g_pfnWSACreateEvent();
|
---|
2613 | *phNative = (RTHCINTPTR)pThis->hEvent;
|
---|
2614 | if (pThis->hEvent == WSA_INVALID_EVENT)
|
---|
2615 | rc = rtSocketError();
|
---|
2616 | }
|
---|
2617 | else
|
---|
2618 | {
|
---|
2619 | AssertCompile(WSA_INVALID_EVENT == (WSAEVENT)NULL);
|
---|
2620 | pThis->hEvent = CreateEventW(NULL, TRUE /*fManualReset*/, FALSE /*fInitialState*/, NULL /*pwszName*/);
|
---|
2621 | *phNative = (RTHCINTPTR)pThis->hEvent;
|
---|
2622 | if (pThis->hEvent == WSA_INVALID_EVENT)
|
---|
2623 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 | rtSocketUnlock(pThis);
|
---|
2627 | return rc;
|
---|
2628 |
|
---|
2629 | #else /* !RT_OS_WINDOWS */
|
---|
2630 | *phNative = (RTHCUINTPTR)pThis->hNative;
|
---|
2631 | return VINF_SUCCESS;
|
---|
2632 | #endif /* !RT_OS_WINDOWS */
|
---|
2633 | }
|
---|
2634 |
|
---|
2635 | #ifdef RT_OS_WINDOWS
|
---|
2636 |
|
---|
2637 | /**
|
---|
2638 | * Fallback poller thread.
|
---|
2639 | *
|
---|
2640 | * @returns VINF_SUCCESS.
|
---|
2641 | * @param hSelf The thread handle.
|
---|
2642 | * @param pvUser Socket instance data.
|
---|
2643 | */
|
---|
2644 | static DECLCALLBACK(int) rtSocketPollFallbackThreadProc(RTTHREAD hSelf, void *pvUser)
|
---|
2645 | {
|
---|
2646 | RTSOCKETINT *pThis = (RTSOCKETINT *)pvUser;
|
---|
2647 | RT_NOREF(hSelf);
|
---|
2648 | # define __WSAFDIsSet g_pfn__WSAFDIsSet
|
---|
2649 |
|
---|
2650 | /*
|
---|
2651 | * The execution loop.
|
---|
2652 | */
|
---|
2653 | while (!ASMAtomicReadBool(&pThis->fPollFallbackShutdown))
|
---|
2654 | {
|
---|
2655 | /*
|
---|
2656 | * Do the selecting (with a 15 second timeout because that seems like a good idea).
|
---|
2657 | */
|
---|
2658 | struct fd_set SetRead;
|
---|
2659 | struct fd_set SetWrite;
|
---|
2660 | struct fd_set SetXcpt;
|
---|
2661 |
|
---|
2662 | FD_ZERO(&SetRead);
|
---|
2663 | FD_ZERO(&SetWrite);
|
---|
2664 | FD_ZERO(&SetXcpt);
|
---|
2665 |
|
---|
2666 | FD_SET(pThis->hPollFallbackNotifyR, &SetRead);
|
---|
2667 | FD_SET(pThis->hPollFallbackNotifyR, &SetXcpt);
|
---|
2668 |
|
---|
2669 | bool fActive = ASMAtomicReadBool(&pThis->fPollFallbackActive);
|
---|
2670 | uint32_t fEvents;
|
---|
2671 | if (!fActive)
|
---|
2672 | fEvents = 0;
|
---|
2673 | else
|
---|
2674 | {
|
---|
2675 | fEvents = ASMAtomicReadU32(&pThis->fSubscribedEvts);
|
---|
2676 | if (fEvents & RTPOLL_EVT_READ)
|
---|
2677 | FD_SET(pThis->hNative, &SetRead);
|
---|
2678 | if (fEvents & RTPOLL_EVT_WRITE)
|
---|
2679 | FD_SET(pThis->hNative, &SetWrite);
|
---|
2680 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
2681 | FD_SET(pThis->hNative, &SetXcpt);
|
---|
2682 | }
|
---|
2683 |
|
---|
2684 | struct timeval Timeout;
|
---|
2685 | Timeout.tv_sec = 15;
|
---|
2686 | Timeout.tv_usec = 0;
|
---|
2687 | int rc = g_pfnselect(INT_MAX /*ignored*/, &SetRead, &SetWrite, &SetXcpt, &Timeout);
|
---|
2688 |
|
---|
2689 | /* Stop immediately if told to shut down. */
|
---|
2690 | if (ASMAtomicReadBool(&pThis->fPollFallbackShutdown))
|
---|
2691 | break;
|
---|
2692 |
|
---|
2693 | /*
|
---|
2694 | * Process the result.
|
---|
2695 | */
|
---|
2696 | if (rc > 0)
|
---|
2697 | {
|
---|
2698 | /* First the socket we're listening on. */
|
---|
2699 | if ( fEvents
|
---|
2700 | && ( FD_ISSET(pThis->hNative, &SetRead)
|
---|
2701 | || FD_ISSET(pThis->hNative, &SetWrite)
|
---|
2702 | || FD_ISSET(pThis->hNative, &SetXcpt)) )
|
---|
2703 | {
|
---|
2704 | ASMAtomicWriteBool(&pThis->fPollFallbackActive, false);
|
---|
2705 | SetEvent(pThis->hEvent);
|
---|
2706 | }
|
---|
2707 |
|
---|
2708 | /* Then maintain the notification pipe. (We only read one byte here
|
---|
2709 | because we're overly paranoid wrt socket switching to blocking mode.) */
|
---|
2710 | if (FD_ISSET(pThis->hPollFallbackNotifyR, &SetRead))
|
---|
2711 | {
|
---|
2712 | char chIgnored;
|
---|
2713 | g_pfnrecv(pThis->hPollFallbackNotifyR, &chIgnored, sizeof(chIgnored), MSG_NOSIGNAL);
|
---|
2714 | }
|
---|
2715 | }
|
---|
2716 | else
|
---|
2717 | AssertMsg(rc == 0, ("%Rrc\n", rtSocketError()));
|
---|
2718 | }
|
---|
2719 |
|
---|
2720 | # undef __WSAFDIsSet
|
---|
2721 | return VINF_SUCCESS;
|
---|
2722 | }
|
---|
2723 |
|
---|
2724 |
|
---|
2725 | /**
|
---|
2726 | * Pokes the fallback thread, making sure it gets out of whatever it's stuck in.
|
---|
2727 | *
|
---|
2728 | * @param pThis The socket handle.
|
---|
2729 | */
|
---|
2730 | static void rtSocketPokePollFallbackThread(RTSOCKETINT *pThis)
|
---|
2731 | {
|
---|
2732 | Assert(pThis->fPollFallback);
|
---|
2733 | if (pThis->hPollFallbackThread != NIL_RTTHREAD)
|
---|
2734 | {
|
---|
2735 | int cbWritten = g_pfnsend(pThis->hPollFallbackNotifyW, "!", 1, MSG_NOSIGNAL);
|
---|
2736 | AssertMsg(cbWritten == 1, ("cbWritten=%d err=%Rrc\n", rtSocketError()));
|
---|
2737 | RT_NOREF_PV(cbWritten);
|
---|
2738 | }
|
---|
2739 | }
|
---|
2740 |
|
---|
2741 |
|
---|
2742 | /**
|
---|
2743 | * Called by rtSocketPollStart to make the thread start selecting on the socket.
|
---|
2744 | *
|
---|
2745 | * @returns 0 on success, RTPOLL_EVT_ERROR on failure.
|
---|
2746 | * @param pThis The socket handle.
|
---|
2747 | */
|
---|
2748 | static uint32_t rtSocketPollFallbackStart(RTSOCKETINT *pThis)
|
---|
2749 | {
|
---|
2750 | /*
|
---|
2751 | * Reset the event and tell the thread to start selecting on the socket.
|
---|
2752 | */
|
---|
2753 | ResetEvent(pThis->hEvent);
|
---|
2754 | ASMAtomicWriteBool(&pThis->fPollFallbackActive, true);
|
---|
2755 |
|
---|
2756 | /*
|
---|
2757 | * Wake up the thread the thread.
|
---|
2758 | */
|
---|
2759 | if (pThis->hPollFallbackThread != NIL_RTTHREAD)
|
---|
2760 | rtSocketPokePollFallbackThread(pThis);
|
---|
2761 | else
|
---|
2762 | {
|
---|
2763 | /*
|
---|
2764 | * Not running, need to set it up and start it.
|
---|
2765 | */
|
---|
2766 | AssertLogRelReturn(pThis->hEvent != NULL && pThis->hEvent != INVALID_HANDLE_VALUE, RTPOLL_EVT_ERROR);
|
---|
2767 |
|
---|
2768 | /* Create the notification socket pair. */
|
---|
2769 | int rc;
|
---|
2770 | if (pThis->hPollFallbackNotifyR == NIL_RTSOCKETNATIVE)
|
---|
2771 | {
|
---|
2772 | rc = rtSocketCreateNativeTcpPair(&pThis->hPollFallbackNotifyW, &pThis->hPollFallbackNotifyR);
|
---|
2773 | AssertLogRelRCReturn(rc, RTPOLL_EVT_ERROR);
|
---|
2774 |
|
---|
2775 | /* Make the read end non-blocking (not fatal). */
|
---|
2776 | u_long fNonBlocking = 1;
|
---|
2777 | rc = g_pfnioctlsocket(pThis->hPollFallbackNotifyR, FIONBIO, &fNonBlocking);
|
---|
2778 | AssertLogRelMsg(rc == 0, ("rc=%#x %Rrc\n", rc, rtSocketError()));
|
---|
2779 | }
|
---|
2780 |
|
---|
2781 | /* Finally, start the thread. ASSUME we don't need too much stack. */
|
---|
2782 | rc = RTThreadCreate(&pThis->hPollFallbackThread, rtSocketPollFallbackThreadProc, pThis,
|
---|
2783 | _128K, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "sockpoll");
|
---|
2784 | AssertLogRelRCReturn(rc, RTPOLL_EVT_ERROR);
|
---|
2785 | }
|
---|
2786 | return 0;
|
---|
2787 | }
|
---|
2788 |
|
---|
2789 |
|
---|
2790 | /**
|
---|
2791 | * Undos the harm done by WSAEventSelect.
|
---|
2792 | *
|
---|
2793 | * @returns IPRT status code.
|
---|
2794 | * @param pThis The socket handle.
|
---|
2795 | */
|
---|
2796 | static int rtSocketPollClearEventAndRestoreBlocking(RTSOCKETINT *pThis)
|
---|
2797 | {
|
---|
2798 | int rc = VINF_SUCCESS;
|
---|
2799 | if (pThis->fSubscribedEvts)
|
---|
2800 | {
|
---|
2801 | if (!pThis->fPollFallback)
|
---|
2802 | {
|
---|
2803 | Assert(g_pfnWSAEventSelect && g_pfnioctlsocket);
|
---|
2804 | if (g_pfnWSAEventSelect && g_pfnioctlsocket)
|
---|
2805 | {
|
---|
2806 | if (g_pfnWSAEventSelect(pThis->hNative, WSA_INVALID_EVENT, 0) == 0)
|
---|
2807 | {
|
---|
2808 | pThis->fSubscribedEvts = 0;
|
---|
2809 |
|
---|
2810 | /*
|
---|
2811 | * Switch back to blocking mode if that was the state before the
|
---|
2812 | * operation.
|
---|
2813 | */
|
---|
2814 | if (pThis->fBlocking)
|
---|
2815 | {
|
---|
2816 | u_long fNonBlocking = 0;
|
---|
2817 | int rc2 = g_pfnioctlsocket(pThis->hNative, FIONBIO, &fNonBlocking);
|
---|
2818 | if (rc2 != 0)
|
---|
2819 | {
|
---|
2820 | rc = rtSocketError();
|
---|
2821 | AssertMsgFailed(("%Rrc; rc2=%d\n", rc, rc2));
|
---|
2822 | }
|
---|
2823 | }
|
---|
2824 | }
|
---|
2825 | else
|
---|
2826 | {
|
---|
2827 | rc = rtSocketError();
|
---|
2828 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
2829 | }
|
---|
2830 | }
|
---|
2831 | else
|
---|
2832 | {
|
---|
2833 | Assert(pThis->fPollFallback);
|
---|
2834 | rc = VINF_SUCCESS;
|
---|
2835 | }
|
---|
2836 | }
|
---|
2837 | /*
|
---|
2838 | * Just clear the event mask as we never started waiting if we get here.
|
---|
2839 | */
|
---|
2840 | else
|
---|
2841 | ASMAtomicWriteU32(&pThis->fSubscribedEvts, 0);
|
---|
2842 | }
|
---|
2843 | return rc;
|
---|
2844 | }
|
---|
2845 |
|
---|
2846 |
|
---|
2847 | /**
|
---|
2848 | * Updates the mask of events we're subscribing to.
|
---|
2849 | *
|
---|
2850 | * @returns IPRT status code.
|
---|
2851 | * @param pThis The socket handle.
|
---|
2852 | * @param fEvents The events we want to subscribe to.
|
---|
2853 | */
|
---|
2854 | static int rtSocketPollUpdateEvents(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
2855 | {
|
---|
2856 | if (!pThis->fPollFallback)
|
---|
2857 | {
|
---|
2858 | LONG fNetworkEvents = 0;
|
---|
2859 | if (fEvents & RTPOLL_EVT_READ)
|
---|
2860 | fNetworkEvents |= FD_READ;
|
---|
2861 | if (fEvents & RTPOLL_EVT_WRITE)
|
---|
2862 | fNetworkEvents |= FD_WRITE;
|
---|
2863 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
2864 | fNetworkEvents |= FD_CLOSE;
|
---|
2865 | LogFlowFunc(("fNetworkEvents=%#x\n", fNetworkEvents));
|
---|
2866 |
|
---|
2867 | if (g_pfnWSAEventSelect(pThis->hNative, pThis->hEvent, fNetworkEvents) == 0)
|
---|
2868 | {
|
---|
2869 | pThis->fSubscribedEvts = fEvents;
|
---|
2870 | return VINF_SUCCESS;
|
---|
2871 | }
|
---|
2872 |
|
---|
2873 | int rc = rtSocketError();
|
---|
2874 | AssertMsgFailed(("fNetworkEvents=%#x rc=%Rrc\n", fNetworkEvents, rtSocketError()));
|
---|
2875 | return rc;
|
---|
2876 | }
|
---|
2877 |
|
---|
2878 | /*
|
---|
2879 | * Update the events we're waiting for. Caller will poke/start the thread. later
|
---|
2880 | */
|
---|
2881 | ASMAtomicWriteU32(&pThis->fSubscribedEvts, fEvents);
|
---|
2882 | return VINF_SUCCESS;
|
---|
2883 | }
|
---|
2884 |
|
---|
2885 | #endif /* RT_OS_WINDOWS */
|
---|
2886 |
|
---|
2887 |
|
---|
2888 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
2889 |
|
---|
2890 | /**
|
---|
2891 | * Checks for pending events.
|
---|
2892 | *
|
---|
2893 | * @returns Event mask or 0.
|
---|
2894 | * @param pThis The socket handle.
|
---|
2895 | * @param fEvents The desired events.
|
---|
2896 | */
|
---|
2897 | static uint32_t rtSocketPollCheck(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
2898 | {
|
---|
2899 | uint32_t fRetEvents = 0;
|
---|
2900 |
|
---|
2901 | LogFlowFunc(("pThis=%#p fEvents=%#x\n", pThis, fEvents));
|
---|
2902 |
|
---|
2903 | # ifdef RT_OS_WINDOWS
|
---|
2904 | /* Make sure WSAEnumNetworkEvents returns what we want. */
|
---|
2905 | int rc = VINF_SUCCESS;
|
---|
2906 | if ((pThis->fSubscribedEvts & fEvents) != fEvents)
|
---|
2907 | rc = rtSocketPollUpdateEvents(pThis, pThis->fSubscribedEvts | fEvents);
|
---|
2908 |
|
---|
2909 | if (!pThis->fPollFallback)
|
---|
2910 | {
|
---|
2911 | /* Atomically get pending events and reset the event semaphore. */
|
---|
2912 | Assert(g_pfnWSAEnumNetworkEvents);
|
---|
2913 | WSANETWORKEVENTS NetEvts;
|
---|
2914 | RT_ZERO(NetEvts);
|
---|
2915 | if (g_pfnWSAEnumNetworkEvents(pThis->hNative, pThis->hEvent, &NetEvts) == 0)
|
---|
2916 | {
|
---|
2917 | if ( (NetEvts.lNetworkEvents & FD_READ)
|
---|
2918 | && NetEvts.iErrorCode[FD_READ_BIT] == 0)
|
---|
2919 | fRetEvents |= RTPOLL_EVT_READ;
|
---|
2920 |
|
---|
2921 | if ( (NetEvts.lNetworkEvents & FD_WRITE)
|
---|
2922 | && NetEvts.iErrorCode[FD_WRITE_BIT] == 0)
|
---|
2923 | fRetEvents |= RTPOLL_EVT_WRITE;
|
---|
2924 |
|
---|
2925 | if (NetEvts.lNetworkEvents & FD_CLOSE)
|
---|
2926 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2927 | else
|
---|
2928 | for (uint32_t i = 0; i < FD_MAX_EVENTS; i++)
|
---|
2929 | if ( (NetEvts.lNetworkEvents & (1L << i))
|
---|
2930 | && NetEvts.iErrorCode[i] != 0)
|
---|
2931 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2932 |
|
---|
2933 | pThis->fEventsSaved = fRetEvents |= pThis->fEventsSaved;
|
---|
2934 | fRetEvents &= fEvents | RTPOLL_EVT_ERROR;
|
---|
2935 | }
|
---|
2936 | else
|
---|
2937 | rc = rtSocketError();
|
---|
2938 | }
|
---|
2939 |
|
---|
2940 | /* Fall back on select if we hit an error above or is using fallback polling. */
|
---|
2941 | if (pThis->fPollFallback || RT_FAILURE(rc))
|
---|
2942 | {
|
---|
2943 | rc = rtSocketSelectOneEx(pThis, fEvents & RTPOLL_EVT_ERROR ? fEvents | RTPOLL_EVT_READ : fEvents, &fRetEvents, 0);
|
---|
2944 | if (RT_SUCCESS(rc))
|
---|
2945 | {
|
---|
2946 | /* rtSocketSelectOneEx may return RTPOLL_EVT_READ on disconnect. Use
|
---|
2947 | getpeername to fix this. */
|
---|
2948 | if ((fRetEvents & (RTPOLL_EVT_READ | RTPOLL_EVT_ERROR)) == RTPOLL_EVT_READ)
|
---|
2949 | {
|
---|
2950 | # if 0 /* doens't work */
|
---|
2951 | rtSocketErrorReset();
|
---|
2952 | char chIgn;
|
---|
2953 | rc = g_pfnrecv(pThis->hNative, &chIgn, 0, MSG_NOSIGNAL);
|
---|
2954 | rc = rtSocketError();
|
---|
2955 | if (RT_FAILURE(rc))
|
---|
2956 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2957 |
|
---|
2958 | rc = g_pfnsend(pThis->hNative, &chIgn, 0, MSG_NOSIGNAL);
|
---|
2959 | rc = rtSocketError();
|
---|
2960 | if (RT_FAILURE(rc))
|
---|
2961 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2962 |
|
---|
2963 | RTSOCKADDRUNION u;
|
---|
2964 | int cbAddr = sizeof(u);
|
---|
2965 | if (g_pfngetpeername(pThis->hNative, &u.Addr, &cbAddr) == SOCKET_ERROR)
|
---|
2966 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2967 | # endif
|
---|
2968 | /* If no bytes are available, assume error condition. */
|
---|
2969 | u_long cbAvail = 0;
|
---|
2970 | rc = g_pfnioctlsocket(pThis->hNative, FIONREAD, &cbAvail);
|
---|
2971 | if (rc == 0 && cbAvail == 0)
|
---|
2972 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2973 | }
|
---|
2974 | fRetEvents &= fEvents | RTPOLL_EVT_ERROR;
|
---|
2975 | }
|
---|
2976 | else if (rc == VERR_TIMEOUT)
|
---|
2977 | fRetEvents = 0;
|
---|
2978 | else
|
---|
2979 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2980 | }
|
---|
2981 |
|
---|
2982 | # else /* RT_OS_OS2 */
|
---|
2983 | int aFds[4] = { pThis->hNative, pThis->hNative, pThis->hNative, -1 };
|
---|
2984 | int rc = os2_select(aFds, 1, 1, 1, 0);
|
---|
2985 | if (rc > 0)
|
---|
2986 | {
|
---|
2987 | if (aFds[0] == pThis->hNative)
|
---|
2988 | fRetEvents |= RTPOLL_EVT_READ;
|
---|
2989 | if (aFds[1] == pThis->hNative)
|
---|
2990 | fRetEvents |= RTPOLL_EVT_WRITE;
|
---|
2991 | if (aFds[2] == pThis->hNative)
|
---|
2992 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2993 | fRetEvents &= fEvents;
|
---|
2994 | }
|
---|
2995 | # endif /* RT_OS_OS2 */
|
---|
2996 |
|
---|
2997 | LogFlowFunc(("fRetEvents=%#x\n", fRetEvents));
|
---|
2998 | return fRetEvents;
|
---|
2999 | }
|
---|
3000 |
|
---|
3001 |
|
---|
3002 | /**
|
---|
3003 | * Internal RTPoll helper that polls the socket handle and, if @a fNoWait is
|
---|
3004 | * clear, starts whatever actions we've got running during the poll call.
|
---|
3005 | *
|
---|
3006 | * @returns 0 if no pending events, actions initiated if @a fNoWait is clear.
|
---|
3007 | * Event mask (in @a fEvents) and no actions if the handle is ready
|
---|
3008 | * already.
|
---|
3009 | * UINT32_MAX (asserted) if the socket handle is busy in I/O or a
|
---|
3010 | * different poll set.
|
---|
3011 | *
|
---|
3012 | * @param hSocket The socket handle.
|
---|
3013 | * @param hPollSet The poll set handle (for access checks).
|
---|
3014 | * @param fEvents The events we're polling for.
|
---|
3015 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
3016 | * in this poll set. This can be used for dealing
|
---|
3017 | * with duplicate entries.
|
---|
3018 | * @param fNoWait Set if it's a zero-wait poll call. Clear if
|
---|
3019 | * we'll wait for an event to occur.
|
---|
3020 | *
|
---|
3021 | * @remarks There is a potential race wrt duplicate handles when @a fNoWait is
|
---|
3022 | * @c true, we don't currently care about that oddity...
|
---|
3023 | */
|
---|
3024 | DECLHIDDEN(uint32_t) rtSocketPollStart(RTSOCKET hSocket, RTPOLLSET hPollSet, uint32_t fEvents, bool fFinalEntry, bool fNoWait)
|
---|
3025 | {
|
---|
3026 | RTSOCKETINT *pThis = hSocket;
|
---|
3027 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
3028 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
3029 | /** @todo This isn't quite sane. Replace by critsect and open up concurrent
|
---|
3030 | * reads and writes! */
|
---|
3031 | if (rtSocketTryLock(pThis))
|
---|
3032 | pThis->hPollSet = hPollSet;
|
---|
3033 | else
|
---|
3034 | {
|
---|
3035 | AssertReturn(pThis->hPollSet == hPollSet, UINT32_MAX);
|
---|
3036 | ASMAtomicIncU32(&pThis->cUsers);
|
---|
3037 | }
|
---|
3038 |
|
---|
3039 | /* (rtSocketPollCheck will reset the event object). */
|
---|
3040 | # ifdef RT_OS_WINDOWS
|
---|
3041 | uint32_t fRetEvents = pThis->fEventsSaved;
|
---|
3042 | pThis->fEventsSaved = 0; /* Reset */
|
---|
3043 | fRetEvents |= rtSocketPollCheck(pThis, fEvents);
|
---|
3044 |
|
---|
3045 | if ( !fRetEvents
|
---|
3046 | && !fNoWait)
|
---|
3047 | {
|
---|
3048 | pThis->fPollEvts |= fEvents;
|
---|
3049 | if (fFinalEntry)
|
---|
3050 | {
|
---|
3051 | if (pThis->fSubscribedEvts != pThis->fPollEvts)
|
---|
3052 | {
|
---|
3053 | /** @todo seems like there might be a call to many here and that fPollEvts is
|
---|
3054 | * totally unnecessary... (bird) */
|
---|
3055 | int rc = rtSocketPollUpdateEvents(pThis, pThis->fPollEvts);
|
---|
3056 | if (RT_FAILURE(rc))
|
---|
3057 | {
|
---|
3058 | pThis->fPollEvts = 0;
|
---|
3059 | fRetEvents = UINT32_MAX;
|
---|
3060 | }
|
---|
3061 | }
|
---|
3062 |
|
---|
3063 | /* Make sure we don't block when there are events pending relevant to an earlier poll set entry. */
|
---|
3064 | if (pThis->fEventsSaved && !pThis->fPollFallback && g_pfnWSASetEvent && fRetEvents == 0)
|
---|
3065 | g_pfnWSASetEvent(pThis->hEvent);
|
---|
3066 | }
|
---|
3067 | }
|
---|
3068 | # else
|
---|
3069 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
3070 | # endif
|
---|
3071 |
|
---|
3072 | if (fRetEvents || fNoWait)
|
---|
3073 | {
|
---|
3074 | if (pThis->cUsers == 1)
|
---|
3075 | {
|
---|
3076 | # ifdef RT_OS_WINDOWS
|
---|
3077 | pThis->fEventsSaved &= RTPOLL_EVT_ERROR;
|
---|
3078 | pThis->fHarvestedEvents = false;
|
---|
3079 | rtSocketPollClearEventAndRestoreBlocking(pThis);
|
---|
3080 | # endif
|
---|
3081 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
3082 | }
|
---|
3083 | # ifdef RT_OS_WINDOWS
|
---|
3084 | else
|
---|
3085 | pThis->fHarvestedEvents = true;
|
---|
3086 | # endif
|
---|
3087 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
3088 | }
|
---|
3089 | # ifdef RT_OS_WINDOWS
|
---|
3090 | /*
|
---|
3091 | * Kick the poller thread on if this is the final entry and we're in
|
---|
3092 | * winsock 1.x fallback mode.
|
---|
3093 | */
|
---|
3094 | else if (pThis->fPollFallback && fFinalEntry)
|
---|
3095 | fRetEvents = rtSocketPollFallbackStart(pThis);
|
---|
3096 | # endif
|
---|
3097 |
|
---|
3098 | return fRetEvents;
|
---|
3099 | }
|
---|
3100 |
|
---|
3101 |
|
---|
3102 | /**
|
---|
3103 | * Called after a WaitForMultipleObjects returned in order to check for pending
|
---|
3104 | * events and stop whatever actions that rtSocketPollStart() initiated.
|
---|
3105 | *
|
---|
3106 | * @returns Event mask or 0.
|
---|
3107 | *
|
---|
3108 | * @param hSocket The socket handle.
|
---|
3109 | * @param fEvents The events we're polling for.
|
---|
3110 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
3111 | * in this poll set. This can be used for dealing
|
---|
3112 | * with duplicate entries. Only keep in mind that
|
---|
3113 | * this method is called in reverse order, so the
|
---|
3114 | * first call will have this set (when the entire
|
---|
3115 | * set was processed).
|
---|
3116 | * @param fHarvestEvents Set if we should check for pending events.
|
---|
3117 | */
|
---|
3118 | DECLHIDDEN(uint32_t) rtSocketPollDone(RTSOCKET hSocket, uint32_t fEvents, bool fFinalEntry, bool fHarvestEvents)
|
---|
3119 | {
|
---|
3120 | RTSOCKETINT *pThis = hSocket;
|
---|
3121 | AssertPtrReturn(pThis, 0);
|
---|
3122 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, 0);
|
---|
3123 | Assert(pThis->cUsers > 0);
|
---|
3124 | Assert(pThis->hPollSet != NIL_RTPOLLSET);
|
---|
3125 | RT_NOREF_PV(fFinalEntry);
|
---|
3126 |
|
---|
3127 | # ifdef RT_OS_WINDOWS
|
---|
3128 | /*
|
---|
3129 | * Deactivate the poll thread if we're in winsock 1.x fallback poll mode.
|
---|
3130 | */
|
---|
3131 | if ( pThis->fPollFallback
|
---|
3132 | && pThis->hPollFallbackThread != NIL_RTTHREAD)
|
---|
3133 | {
|
---|
3134 | ASMAtomicWriteU32(&pThis->fSubscribedEvts, 0);
|
---|
3135 | if (ASMAtomicXchgBool(&pThis->fPollFallbackActive, false))
|
---|
3136 | rtSocketPokePollFallbackThread(pThis);
|
---|
3137 | }
|
---|
3138 | # endif
|
---|
3139 |
|
---|
3140 | /*
|
---|
3141 | * Harvest events and clear the event mask for the next round of polling.
|
---|
3142 | */
|
---|
3143 | uint32_t fRetEvents;
|
---|
3144 | # ifdef RT_OS_WINDOWS
|
---|
3145 | if (!pThis->fPollFallback)
|
---|
3146 | {
|
---|
3147 | if (!pThis->fHarvestedEvents)
|
---|
3148 | {
|
---|
3149 | fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
3150 | pThis->fHarvestedEvents = true;
|
---|
3151 | }
|
---|
3152 | else
|
---|
3153 | fRetEvents = pThis->fEventsSaved;
|
---|
3154 | if (fHarvestEvents)
|
---|
3155 | fRetEvents &= fEvents;
|
---|
3156 | else
|
---|
3157 | fRetEvents = 0;
|
---|
3158 | pThis->fPollEvts = 0;
|
---|
3159 | }
|
---|
3160 | else
|
---|
3161 | # endif
|
---|
3162 | {
|
---|
3163 | if (fHarvestEvents)
|
---|
3164 | fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
3165 | else
|
---|
3166 | fRetEvents = 0;
|
---|
3167 | }
|
---|
3168 |
|
---|
3169 | /*
|
---|
3170 | * Make the socket blocking again and unlock the handle.
|
---|
3171 | */
|
---|
3172 | if (pThis->cUsers == 1)
|
---|
3173 | {
|
---|
3174 | # ifdef RT_OS_WINDOWS
|
---|
3175 | pThis->fEventsSaved &= RTPOLL_EVT_ERROR;
|
---|
3176 | pThis->fHarvestedEvents = false;
|
---|
3177 | rtSocketPollClearEventAndRestoreBlocking(pThis);
|
---|
3178 | # endif
|
---|
3179 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
3180 | }
|
---|
3181 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
3182 | return fRetEvents;
|
---|
3183 | }
|
---|
3184 |
|
---|
3185 | #endif /* RT_OS_WINDOWS || RT_OS_OS2 */
|
---|
3186 |
|
---|