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