1 | /* $Id: socket.cpp 60756 2016-04-29 10:49:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Network Sockets.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 <winsock2.h>
|
---|
33 | # include <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 |
|
---|
74 |
|
---|
75 | /*********************************************************************************************************************************
|
---|
76 | * Defined Constants And Macros *
|
---|
77 | *********************************************************************************************************************************/
|
---|
78 | /* non-standard linux stuff (it seems). */
|
---|
79 | #ifndef MSG_NOSIGNAL
|
---|
80 | # define MSG_NOSIGNAL 0
|
---|
81 | #endif
|
---|
82 |
|
---|
83 | /* Windows has different names for SHUT_XXX. */
|
---|
84 | #ifndef SHUT_RDWR
|
---|
85 | # ifdef SD_BOTH
|
---|
86 | # define SHUT_RDWR SD_BOTH
|
---|
87 | # else
|
---|
88 | # define SHUT_RDWR 2
|
---|
89 | # endif
|
---|
90 | #endif
|
---|
91 | #ifndef SHUT_WR
|
---|
92 | # ifdef SD_SEND
|
---|
93 | # define SHUT_WR SD_SEND
|
---|
94 | # else
|
---|
95 | # define SHUT_WR 1
|
---|
96 | # endif
|
---|
97 | #endif
|
---|
98 | #ifndef SHUT_RD
|
---|
99 | # ifdef SD_RECEIVE
|
---|
100 | # define SHUT_RD SD_RECEIVE
|
---|
101 | # else
|
---|
102 | # define SHUT_RD 0
|
---|
103 | # endif
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /* fixup backlevel OSes. */
|
---|
107 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
108 | # define socklen_t int
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | /** How many pending connection. */
|
---|
112 | #define RTTCP_SERVER_BACKLOG 10
|
---|
113 |
|
---|
114 | /* Limit read and write sizes on Windows and OS/2. */
|
---|
115 | #ifdef RT_OS_WINDOWS
|
---|
116 | # define RTSOCKET_MAX_WRITE (INT_MAX / 2)
|
---|
117 | # define RTSOCKET_MAX_READ (INT_MAX / 2)
|
---|
118 | #elif defined(RT_OS_OS2)
|
---|
119 | # define RTSOCKET_MAX_WRITE 0x10000
|
---|
120 | # define RTSOCKET_MAX_READ 0x10000
|
---|
121 | #endif
|
---|
122 |
|
---|
123 |
|
---|
124 | /*********************************************************************************************************************************
|
---|
125 | * Structures and Typedefs *
|
---|
126 | *********************************************************************************************************************************/
|
---|
127 | /**
|
---|
128 | * Socket handle data.
|
---|
129 | *
|
---|
130 | * This is mainly required for implementing RTPollSet on Windows.
|
---|
131 | */
|
---|
132 | typedef struct RTSOCKETINT
|
---|
133 | {
|
---|
134 | /** Magic number (RTSOCKET_MAGIC). */
|
---|
135 | uint32_t u32Magic;
|
---|
136 | /** Exclusive user count.
|
---|
137 | * This is used to prevent two threads from accessing the handle concurrently.
|
---|
138 | * It can be higher than 1 if this handle is reference multiple times in a
|
---|
139 | * polling set (Windows). */
|
---|
140 | uint32_t volatile cUsers;
|
---|
141 | /** The native socket handle. */
|
---|
142 | RTSOCKETNATIVE hNative;
|
---|
143 | /** Indicates whether the handle has been closed or not. */
|
---|
144 | bool volatile fClosed;
|
---|
145 | /** Indicates whether the socket is operating in blocking or non-blocking mode
|
---|
146 | * currently. */
|
---|
147 | bool fBlocking;
|
---|
148 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
149 | /** The pollset currently polling this socket. This is NIL if no one is
|
---|
150 | * polling. */
|
---|
151 | RTPOLLSET hPollSet;
|
---|
152 | #endif
|
---|
153 | #ifdef RT_OS_WINDOWS
|
---|
154 | /** The event semaphore we've associated with the socket handle.
|
---|
155 | * This is WSA_INVALID_EVENT if not done. */
|
---|
156 | WSAEVENT hEvent;
|
---|
157 | /** The events we're polling for. */
|
---|
158 | uint32_t fPollEvts;
|
---|
159 | /** The events we're currently subscribing to with WSAEventSelect.
|
---|
160 | * This is ZERO if we're currently not subscribing to anything. */
|
---|
161 | uint32_t fSubscribedEvts;
|
---|
162 | /** Saved events which are only posted once. */
|
---|
163 | uint32_t fEventsSaved;
|
---|
164 | #endif /* RT_OS_WINDOWS */
|
---|
165 | } RTSOCKETINT;
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Address union used internally for things like getpeername and getsockname.
|
---|
170 | */
|
---|
171 | typedef union RTSOCKADDRUNION
|
---|
172 | {
|
---|
173 | struct sockaddr Addr;
|
---|
174 | struct sockaddr_in IPv4;
|
---|
175 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
176 | struct sockaddr_in6 IPv6;
|
---|
177 | #endif
|
---|
178 | } RTSOCKADDRUNION;
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Get the last error as an iprt status code.
|
---|
183 | *
|
---|
184 | * @returns IPRT status code.
|
---|
185 | */
|
---|
186 | DECLINLINE(int) rtSocketError(void)
|
---|
187 | {
|
---|
188 | #ifdef RT_OS_WINDOWS
|
---|
189 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
190 | #else
|
---|
191 | return RTErrConvertFromErrno(errno);
|
---|
192 | #endif
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Resets the last error.
|
---|
198 | */
|
---|
199 | DECLINLINE(void) rtSocketErrorReset(void)
|
---|
200 | {
|
---|
201 | #ifdef RT_OS_WINDOWS
|
---|
202 | WSASetLastError(0);
|
---|
203 | #else
|
---|
204 | errno = 0;
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Get the last resolver error as an iprt status code.
|
---|
211 | *
|
---|
212 | * @returns iprt status code.
|
---|
213 | */
|
---|
214 | DECLHIDDEN(int) rtSocketResolverError(void)
|
---|
215 | {
|
---|
216 | #ifdef RT_OS_WINDOWS
|
---|
217 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
218 | #else
|
---|
219 | switch (h_errno)
|
---|
220 | {
|
---|
221 | case HOST_NOT_FOUND:
|
---|
222 | return VERR_NET_HOST_NOT_FOUND;
|
---|
223 | case NO_DATA:
|
---|
224 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
225 | case NO_RECOVERY:
|
---|
226 | return VERR_IO_GEN_FAILURE;
|
---|
227 | case TRY_AGAIN:
|
---|
228 | return VERR_TRY_AGAIN;
|
---|
229 |
|
---|
230 | default:
|
---|
231 | return VERR_UNRESOLVED_ERROR;
|
---|
232 | }
|
---|
233 | #endif
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Converts from a native socket address to a generic IPRT network address.
|
---|
239 | *
|
---|
240 | * @returns IPRT status code.
|
---|
241 | * @param pSrc The source address.
|
---|
242 | * @param cbSrc The size of the source address.
|
---|
243 | * @param pAddr Where to return the generic IPRT network
|
---|
244 | * address.
|
---|
245 | */
|
---|
246 | static int rtSocketNetAddrFromAddr(RTSOCKADDRUNION const *pSrc, size_t cbSrc, PRTNETADDR pAddr)
|
---|
247 | {
|
---|
248 | /*
|
---|
249 | * Convert the address.
|
---|
250 | */
|
---|
251 | if ( cbSrc == sizeof(struct sockaddr_in)
|
---|
252 | && pSrc->Addr.sa_family == AF_INET)
|
---|
253 | {
|
---|
254 | RT_ZERO(*pAddr);
|
---|
255 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
256 | pAddr->uPort = RT_N2H_U16(pSrc->IPv4.sin_port);
|
---|
257 | pAddr->uAddr.IPv4.u = pSrc->IPv4.sin_addr.s_addr;
|
---|
258 | }
|
---|
259 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
260 | else if ( cbSrc == sizeof(struct sockaddr_in6)
|
---|
261 | && pSrc->Addr.sa_family == AF_INET6)
|
---|
262 | {
|
---|
263 | RT_ZERO(*pAddr);
|
---|
264 | pAddr->enmType = RTNETADDRTYPE_IPV6;
|
---|
265 | pAddr->uPort = RT_N2H_U16(pSrc->IPv6.sin6_port);
|
---|
266 | pAddr->uAddr.IPv6.au32[0] = pSrc->IPv6.sin6_addr.s6_addr32[0];
|
---|
267 | pAddr->uAddr.IPv6.au32[1] = pSrc->IPv6.sin6_addr.s6_addr32[1];
|
---|
268 | pAddr->uAddr.IPv6.au32[2] = pSrc->IPv6.sin6_addr.s6_addr32[2];
|
---|
269 | pAddr->uAddr.IPv6.au32[3] = pSrc->IPv6.sin6_addr.s6_addr32[3];
|
---|
270 | }
|
---|
271 | #endif
|
---|
272 | else
|
---|
273 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
274 | return VINF_SUCCESS;
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Converts from a generic IPRT network address to a native socket address.
|
---|
280 | *
|
---|
281 | * @returns IPRT status code.
|
---|
282 | * @param pAddr Pointer to the generic IPRT network address.
|
---|
283 | * @param pDst The source address.
|
---|
284 | * @param cbDst The size of the source address.
|
---|
285 | * @param pcbAddr Where to store the size of the returned address.
|
---|
286 | * Optional
|
---|
287 | */
|
---|
288 | static int rtSocketAddrFromNetAddr(PCRTNETADDR pAddr, RTSOCKADDRUNION *pDst, size_t cbDst, int *pcbAddr)
|
---|
289 | {
|
---|
290 | RT_BZERO(pDst, cbDst);
|
---|
291 | if ( pAddr->enmType == RTNETADDRTYPE_IPV4
|
---|
292 | && cbDst >= sizeof(struct sockaddr_in))
|
---|
293 | {
|
---|
294 | pDst->Addr.sa_family = AF_INET;
|
---|
295 | pDst->IPv4.sin_port = RT_H2N_U16(pAddr->uPort);
|
---|
296 | pDst->IPv4.sin_addr.s_addr = pAddr->uAddr.IPv4.u;
|
---|
297 | if (pcbAddr)
|
---|
298 | *pcbAddr = sizeof(pDst->IPv4);
|
---|
299 | }
|
---|
300 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
301 | else if ( pAddr->enmType == RTNETADDRTYPE_IPV6
|
---|
302 | && cbDst >= sizeof(struct sockaddr_in6))
|
---|
303 | {
|
---|
304 | pDst->Addr.sa_family = AF_INET6;
|
---|
305 | pDst->IPv6.sin6_port = RT_H2N_U16(pAddr->uPort);
|
---|
306 | pSrc->IPv6.sin6_addr.s6_addr32[0] = pAddr->uAddr.IPv6.au32[0];
|
---|
307 | pSrc->IPv6.sin6_addr.s6_addr32[1] = pAddr->uAddr.IPv6.au32[1];
|
---|
308 | pSrc->IPv6.sin6_addr.s6_addr32[2] = pAddr->uAddr.IPv6.au32[2];
|
---|
309 | pSrc->IPv6.sin6_addr.s6_addr32[3] = pAddr->uAddr.IPv6.au32[3];
|
---|
310 | if (pcbAddr)
|
---|
311 | *pcbAddr = sizeof(pDst->IPv6);
|
---|
312 | }
|
---|
313 | #endif
|
---|
314 | else
|
---|
315 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
316 | return VINF_SUCCESS;
|
---|
317 | }
|
---|
318 |
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Tries to lock the socket for exclusive usage by the calling thread.
|
---|
322 | *
|
---|
323 | * Call rtSocketUnlock() to unlock.
|
---|
324 | *
|
---|
325 | * @returns @c true if locked, @c false if not.
|
---|
326 | * @param pThis The socket structure.
|
---|
327 | */
|
---|
328 | DECLINLINE(bool) rtSocketTryLock(RTSOCKETINT *pThis)
|
---|
329 | {
|
---|
330 | return ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0);
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Unlocks the socket.
|
---|
336 | *
|
---|
337 | * @param pThis The socket structure.
|
---|
338 | */
|
---|
339 | DECLINLINE(void) rtSocketUnlock(RTSOCKETINT *pThis)
|
---|
340 | {
|
---|
341 | ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1);
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * The slow path of rtSocketSwitchBlockingMode that does the actual switching.
|
---|
347 | *
|
---|
348 | * @returns IPRT status code.
|
---|
349 | * @param pThis The socket structure.
|
---|
350 | * @param fBlocking The desired mode of operation.
|
---|
351 | * @remarks Do not call directly.
|
---|
352 | */
|
---|
353 | static int rtSocketSwitchBlockingModeSlow(RTSOCKETINT *pThis, bool fBlocking)
|
---|
354 | {
|
---|
355 | #ifdef RT_OS_WINDOWS
|
---|
356 | u_long uBlocking = fBlocking ? 0 : 1;
|
---|
357 | if (ioctlsocket(pThis->hNative, FIONBIO, &uBlocking))
|
---|
358 | return rtSocketError();
|
---|
359 |
|
---|
360 | #else
|
---|
361 | int fFlags = fcntl(pThis->hNative, F_GETFL, 0);
|
---|
362 | if (fFlags == -1)
|
---|
363 | return rtSocketError();
|
---|
364 |
|
---|
365 | if (fBlocking)
|
---|
366 | fFlags &= ~O_NONBLOCK;
|
---|
367 | else
|
---|
368 | fFlags |= O_NONBLOCK;
|
---|
369 | if (fcntl(pThis->hNative, F_SETFL, fFlags) == -1)
|
---|
370 | return rtSocketError();
|
---|
371 | #endif
|
---|
372 |
|
---|
373 | pThis->fBlocking = fBlocking;
|
---|
374 | return VINF_SUCCESS;
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Switches the socket to the desired blocking mode if necessary.
|
---|
380 | *
|
---|
381 | * The socket must be locked.
|
---|
382 | *
|
---|
383 | * @returns IPRT status code.
|
---|
384 | * @param pThis The socket structure.
|
---|
385 | * @param fBlocking The desired mode of operation.
|
---|
386 | */
|
---|
387 | DECLINLINE(int) rtSocketSwitchBlockingMode(RTSOCKETINT *pThis, bool fBlocking)
|
---|
388 | {
|
---|
389 | if (pThis->fBlocking != fBlocking)
|
---|
390 | return rtSocketSwitchBlockingModeSlow(pThis, fBlocking);
|
---|
391 | return VINF_SUCCESS;
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Creates an IPRT socket handle for a native one.
|
---|
397 | *
|
---|
398 | * @returns IPRT status code.
|
---|
399 | * @param ppSocket Where to return the IPRT socket handle.
|
---|
400 | * @param hNative The native handle.
|
---|
401 | */
|
---|
402 | DECLHIDDEN(int) rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative)
|
---|
403 | {
|
---|
404 | RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemPoolAlloc(RTMEMPOOL_DEFAULT, sizeof(*pThis));
|
---|
405 | if (!pThis)
|
---|
406 | return VERR_NO_MEMORY;
|
---|
407 | pThis->u32Magic = RTSOCKET_MAGIC;
|
---|
408 | pThis->cUsers = 0;
|
---|
409 | pThis->hNative = hNative;
|
---|
410 | pThis->fClosed = false;
|
---|
411 | pThis->fBlocking = true;
|
---|
412 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
413 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
414 | #endif
|
---|
415 | #ifdef RT_OS_WINDOWS
|
---|
416 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
417 | pThis->fPollEvts = 0;
|
---|
418 | pThis->fSubscribedEvts = 0;
|
---|
419 | pThis->fEventsSaved = 0;
|
---|
420 | #endif
|
---|
421 | *ppSocket = pThis;
|
---|
422 | return VINF_SUCCESS;
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | RTDECL(int) RTSocketFromNative(PRTSOCKET phSocket, RTHCINTPTR uNative)
|
---|
427 | {
|
---|
428 | AssertReturn(uNative != NIL_RTSOCKETNATIVE, VERR_INVALID_PARAMETER);
|
---|
429 | #ifndef RT_OS_WINDOWS
|
---|
430 | AssertReturn(uNative >= 0, VERR_INVALID_PARAMETER);
|
---|
431 | #endif
|
---|
432 | AssertPtrReturn(phSocket, VERR_INVALID_POINTER);
|
---|
433 | return rtSocketCreateForNative(phSocket, uNative);
|
---|
434 | }
|
---|
435 |
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Wrapper around socket().
|
---|
439 | *
|
---|
440 | * @returns IPRT status code.
|
---|
441 | * @param phSocket Where to store the handle to the socket on
|
---|
442 | * success.
|
---|
443 | * @param iDomain The protocol family (PF_XXX).
|
---|
444 | * @param iType The socket type (SOCK_XXX).
|
---|
445 | * @param iProtocol Socket parameter, usually 0.
|
---|
446 | */
|
---|
447 | DECLHIDDEN(int) rtSocketCreate(PRTSOCKET phSocket, int iDomain, int iType, int iProtocol)
|
---|
448 | {
|
---|
449 | /*
|
---|
450 | * Create the socket.
|
---|
451 | */
|
---|
452 | RTSOCKETNATIVE hNative = socket(iDomain, iType, iProtocol);
|
---|
453 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
454 | return rtSocketError();
|
---|
455 |
|
---|
456 | /*
|
---|
457 | * Wrap it.
|
---|
458 | */
|
---|
459 | int rc = rtSocketCreateForNative(phSocket, hNative);
|
---|
460 | if (RT_FAILURE(rc))
|
---|
461 | {
|
---|
462 | #ifdef RT_OS_WINDOWS
|
---|
463 | closesocket(hNative);
|
---|
464 | #else
|
---|
465 | close(hNative);
|
---|
466 | #endif
|
---|
467 | }
|
---|
468 | return rc;
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 | RTDECL(uint32_t) RTSocketRetain(RTSOCKET hSocket)
|
---|
473 | {
|
---|
474 | RTSOCKETINT *pThis = hSocket;
|
---|
475 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
476 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
477 | return RTMemPoolRetain(pThis);
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * Worker for RTSocketRelease and RTSocketClose.
|
---|
483 | *
|
---|
484 | * @returns IPRT status code.
|
---|
485 | * @param pThis The socket handle instance data.
|
---|
486 | * @param fDestroy Whether we're reaching ref count zero.
|
---|
487 | */
|
---|
488 | static int rtSocketCloseIt(RTSOCKETINT *pThis, bool fDestroy)
|
---|
489 | {
|
---|
490 | /*
|
---|
491 | * Invalidate the handle structure on destroy.
|
---|
492 | */
|
---|
493 | if (fDestroy)
|
---|
494 | {
|
---|
495 | Assert(ASMAtomicReadU32(&pThis->u32Magic) == RTSOCKET_MAGIC);
|
---|
496 | ASMAtomicWriteU32(&pThis->u32Magic, RTSOCKET_MAGIC_DEAD);
|
---|
497 | }
|
---|
498 |
|
---|
499 | int rc = VINF_SUCCESS;
|
---|
500 | if (ASMAtomicCmpXchgBool(&pThis->fClosed, true, false))
|
---|
501 | {
|
---|
502 | /*
|
---|
503 | * Close the native handle.
|
---|
504 | */
|
---|
505 | RTSOCKETNATIVE hNative = pThis->hNative;
|
---|
506 | if (hNative != NIL_RTSOCKETNATIVE)
|
---|
507 | {
|
---|
508 | pThis->hNative = NIL_RTSOCKETNATIVE;
|
---|
509 |
|
---|
510 | #ifdef RT_OS_WINDOWS
|
---|
511 | if (closesocket(hNative))
|
---|
512 | #else
|
---|
513 | if (close(hNative))
|
---|
514 | #endif
|
---|
515 | {
|
---|
516 | rc = rtSocketError();
|
---|
517 | #ifdef RT_OS_WINDOWS
|
---|
518 | AssertMsgFailed(("closesocket(%p) -> %Rrc\n", (uintptr_t)hNative, rc));
|
---|
519 | #else
|
---|
520 | AssertMsgFailed(("close(%d) -> %Rrc\n", hNative, rc));
|
---|
521 | #endif
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | #ifdef RT_OS_WINDOWS
|
---|
526 | /*
|
---|
527 | * Close the event.
|
---|
528 | */
|
---|
529 | WSAEVENT hEvent = pThis->hEvent;
|
---|
530 | if (hEvent == WSA_INVALID_EVENT)
|
---|
531 | {
|
---|
532 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
533 | WSACloseEvent(hEvent);
|
---|
534 | }
|
---|
535 | #endif
|
---|
536 | }
|
---|
537 |
|
---|
538 | return rc;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | RTDECL(uint32_t) RTSocketRelease(RTSOCKET hSocket)
|
---|
543 | {
|
---|
544 | RTSOCKETINT *pThis = hSocket;
|
---|
545 | if (pThis == NIL_RTSOCKET)
|
---|
546 | return 0;
|
---|
547 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
548 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
549 |
|
---|
550 | /* get the refcount without killing it... */
|
---|
551 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
552 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
553 | if (cRefs == 1)
|
---|
554 | rtSocketCloseIt(pThis, true);
|
---|
555 |
|
---|
556 | return RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
557 | }
|
---|
558 |
|
---|
559 |
|
---|
560 | RTDECL(int) RTSocketClose(RTSOCKET hSocket)
|
---|
561 | {
|
---|
562 | RTSOCKETINT *pThis = hSocket;
|
---|
563 | if (pThis == NIL_RTSOCKET)
|
---|
564 | return VINF_SUCCESS;
|
---|
565 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
566 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
567 |
|
---|
568 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
569 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
570 |
|
---|
571 | int rc = rtSocketCloseIt(pThis, cRefs == 1);
|
---|
572 |
|
---|
573 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
574 | return rc;
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 | RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket)
|
---|
579 | {
|
---|
580 | RTSOCKETINT *pThis = hSocket;
|
---|
581 | AssertPtrReturn(pThis, RTHCUINTPTR_MAX);
|
---|
582 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, RTHCUINTPTR_MAX);
|
---|
583 | return (RTHCUINTPTR)pThis->hNative;
|
---|
584 | }
|
---|
585 |
|
---|
586 |
|
---|
587 | RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable)
|
---|
588 | {
|
---|
589 | RTSOCKETINT *pThis = hSocket;
|
---|
590 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
591 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
592 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
593 |
|
---|
594 | int rc = VINF_SUCCESS;
|
---|
595 | #ifdef RT_OS_WINDOWS
|
---|
596 | if (!SetHandleInformation((HANDLE)pThis->hNative, HANDLE_FLAG_INHERIT, fInheritable ? HANDLE_FLAG_INHERIT : 0))
|
---|
597 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
598 | #else
|
---|
599 | if (fcntl(pThis->hNative, F_SETFD, fInheritable ? 0 : FD_CLOEXEC) < 0)
|
---|
600 | rc = RTErrConvertFromErrno(errno);
|
---|
601 | #endif
|
---|
602 |
|
---|
603 | return rc;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | static bool rtSocketIsIPv4Numerical(const char *pszAddress, PRTNETADDRIPV4 pAddr)
|
---|
608 | {
|
---|
609 |
|
---|
610 | /* Empty address resolves to the INADDR_ANY address (good for bind). */
|
---|
611 | if (!pszAddress || !*pszAddress)
|
---|
612 | {
|
---|
613 | pAddr->u = INADDR_ANY;
|
---|
614 | return true;
|
---|
615 | }
|
---|
616 |
|
---|
617 | /* Four quads? */
|
---|
618 | char *psz = (char *)pszAddress;
|
---|
619 | for (int i = 0; i < 4; i++)
|
---|
620 | {
|
---|
621 | uint8_t u8;
|
---|
622 | int rc = RTStrToUInt8Ex(psz, &psz, 0, &u8);
|
---|
623 | if (rc != VINF_SUCCESS && rc != VWRN_TRAILING_CHARS)
|
---|
624 | return false;
|
---|
625 | if (*psz != (i < 3 ? '.' : '\0'))
|
---|
626 | return false;
|
---|
627 | psz++;
|
---|
628 |
|
---|
629 | pAddr->au8[i] = u8; /* big endian */
|
---|
630 | }
|
---|
631 |
|
---|
632 | return true;
|
---|
633 | }
|
---|
634 |
|
---|
635 | RTDECL(int) RTSocketParseInetAddress(const char *pszAddress, unsigned uPort, PRTNETADDR pAddr)
|
---|
636 | {
|
---|
637 | int rc;
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * Validate input.
|
---|
641 | */
|
---|
642 | AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
|
---|
643 | AssertPtrNullReturn(pszAddress, VERR_INVALID_POINTER);
|
---|
644 |
|
---|
645 | #ifdef RT_OS_WINDOWS
|
---|
646 | /*
|
---|
647 | * Initialize WinSock and check version.
|
---|
648 | */
|
---|
649 | WORD wVersionRequested = MAKEWORD(1, 1);
|
---|
650 | WSADATA wsaData;
|
---|
651 | rc = WSAStartup(wVersionRequested, &wsaData);
|
---|
652 | if (wsaData.wVersion != wVersionRequested)
|
---|
653 | {
|
---|
654 | AssertMsgFailed(("Wrong winsock version\n"));
|
---|
655 | return VERR_NOT_SUPPORTED;
|
---|
656 | }
|
---|
657 | #endif
|
---|
658 |
|
---|
659 | /*
|
---|
660 | * Resolve the address. Pretty crude at the moment, but we have to make
|
---|
661 | * sure to not ask the NT 4 gethostbyname about an IPv4 address as it may
|
---|
662 | * give a wrong answer.
|
---|
663 | */
|
---|
664 | /** @todo this only supports IPv4, and IPv6 support needs to be added.
|
---|
665 | * It probably needs to be converted to getaddrinfo(). */
|
---|
666 | RTNETADDRIPV4 IPv4Quad;
|
---|
667 | if (rtSocketIsIPv4Numerical(pszAddress, &IPv4Quad))
|
---|
668 | {
|
---|
669 | Log3(("rtSocketIsIPv4Numerical: %s -> %#x (%RTnaipv4)\n", pszAddress, IPv4Quad.u, IPv4Quad));
|
---|
670 | RT_ZERO(*pAddr);
|
---|
671 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
672 | pAddr->uPort = uPort;
|
---|
673 | pAddr->uAddr.IPv4 = IPv4Quad;
|
---|
674 | return VINF_SUCCESS;
|
---|
675 | }
|
---|
676 |
|
---|
677 | struct hostent *pHostEnt;
|
---|
678 | pHostEnt = gethostbyname(pszAddress);
|
---|
679 | if (!pHostEnt)
|
---|
680 | {
|
---|
681 | rc = rtSocketResolverError();
|
---|
682 | AssertMsgFailed(("Could not resolve '%s', rc=%Rrc\n", pszAddress, rc));
|
---|
683 | return rc;
|
---|
684 | }
|
---|
685 |
|
---|
686 | if (pHostEnt->h_addrtype == AF_INET)
|
---|
687 | {
|
---|
688 | RT_ZERO(*pAddr);
|
---|
689 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
690 | pAddr->uPort = uPort;
|
---|
691 | pAddr->uAddr.IPv4.u = ((struct in_addr *)pHostEnt->h_addr)->s_addr;
|
---|
692 | Log3(("gethostbyname: %s -> %#x (%RTnaipv4)\n", pszAddress, pAddr->uAddr.IPv4.u, pAddr->uAddr.IPv4));
|
---|
693 | }
|
---|
694 | else
|
---|
695 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
696 |
|
---|
697 | return VINF_SUCCESS;
|
---|
698 | }
|
---|
699 |
|
---|
700 |
|
---|
701 | /*
|
---|
702 | * New function to allow both ipv4 and ipv6 addresses to be resolved.
|
---|
703 | * Breaks compatibility with windows before 2000.
|
---|
704 | */
|
---|
705 | RTDECL(int) RTSocketQueryAddressStr(const char *pszHost, char *pszResult, size_t *pcbResult, PRTNETADDRTYPE penmAddrType)
|
---|
706 | {
|
---|
707 | AssertPtrReturn(pszHost, VERR_INVALID_POINTER);
|
---|
708 | AssertPtrReturn(pcbResult, VERR_INVALID_POINTER);
|
---|
709 | AssertPtrNullReturn(penmAddrType, VERR_INVALID_POINTER);
|
---|
710 | AssertPtrNullReturn(pszResult, VERR_INVALID_POINTER);
|
---|
711 |
|
---|
712 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS) /** @todo dynamically resolve the APIs not present in NT4! */
|
---|
713 | return VERR_NOT_SUPPORTED;
|
---|
714 |
|
---|
715 | #else
|
---|
716 | int rc;
|
---|
717 | if (*pcbResult < 16)
|
---|
718 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
719 |
|
---|
720 | /* Setup the hint. */
|
---|
721 | struct addrinfo grHints;
|
---|
722 | RT_ZERO(grHints);
|
---|
723 | grHints.ai_socktype = 0;
|
---|
724 | grHints.ai_flags = 0;
|
---|
725 | grHints.ai_protocol = 0;
|
---|
726 | grHints.ai_family = AF_UNSPEC;
|
---|
727 | if (penmAddrType)
|
---|
728 | {
|
---|
729 | switch (*penmAddrType)
|
---|
730 | {
|
---|
731 | case RTNETADDRTYPE_INVALID:
|
---|
732 | /*grHints.ai_family = AF_UNSPEC;*/
|
---|
733 | break;
|
---|
734 | case RTNETADDRTYPE_IPV4:
|
---|
735 | grHints.ai_family = AF_INET;
|
---|
736 | break;
|
---|
737 | case RTNETADDRTYPE_IPV6:
|
---|
738 | grHints.ai_family = AF_INET6;
|
---|
739 | break;
|
---|
740 | default:
|
---|
741 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
742 | }
|
---|
743 | }
|
---|
744 |
|
---|
745 | # ifdef RT_OS_WINDOWS
|
---|
746 | /*
|
---|
747 | * Winsock2 init
|
---|
748 | */
|
---|
749 | /** @todo someone should check if we really need 2, 2 here */
|
---|
750 | WORD wVersionRequested = MAKEWORD(2, 2);
|
---|
751 | WSADATA wsaData;
|
---|
752 | rc = WSAStartup(wVersionRequested, &wsaData);
|
---|
753 | if (wsaData.wVersion != wVersionRequested)
|
---|
754 | {
|
---|
755 | AssertMsgFailed(("Wrong winsock version\n"));
|
---|
756 | return VERR_NOT_SUPPORTED;
|
---|
757 | }
|
---|
758 | # endif
|
---|
759 |
|
---|
760 | /** @todo r=bird: getaddrinfo and freeaddrinfo breaks the additions on NT4. */
|
---|
761 | struct addrinfo *pgrResults = NULL;
|
---|
762 | rc = getaddrinfo(pszHost, "", &grHints, &pgrResults);
|
---|
763 | if (rc != 0)
|
---|
764 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
765 |
|
---|
766 | // return data
|
---|
767 | // on multiple matches return only the first one
|
---|
768 |
|
---|
769 | if (!pgrResults)
|
---|
770 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
771 |
|
---|
772 | struct addrinfo const *pgrResult = pgrResults->ai_next;
|
---|
773 | if (!pgrResult)
|
---|
774 | {
|
---|
775 | freeaddrinfo(pgrResults);
|
---|
776 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
777 | }
|
---|
778 |
|
---|
779 | uint8_t const *pbDummy;
|
---|
780 | RTNETADDRTYPE enmAddrType = RTNETADDRTYPE_INVALID;
|
---|
781 | size_t cchIpAddress;
|
---|
782 | char szIpAddress[48];
|
---|
783 | if (pgrResult->ai_family == AF_INET)
|
---|
784 | {
|
---|
785 | struct sockaddr_in const *pgrSa = (struct sockaddr_in const *)pgrResult->ai_addr;
|
---|
786 | cchIpAddress = RTStrPrintf(szIpAddress, sizeof(szIpAddress),
|
---|
787 | "%RTnaipv4", pgrSa->sin_addr.s_addr);
|
---|
788 | Assert(cchIpAddress >= 7 && cchIpAddress < sizeof(szIpAddress) - 1);
|
---|
789 | enmAddrType = RTNETADDRTYPE_IPV4;
|
---|
790 | rc = VINF_SUCCESS;
|
---|
791 | }
|
---|
792 | else if (pgrResult->ai_family == AF_INET6)
|
---|
793 | {
|
---|
794 | struct sockaddr_in6 const *pgrSa6 = (struct sockaddr_in6 const *)pgrResult->ai_addr;
|
---|
795 | cchIpAddress = RTStrPrintf(szIpAddress, sizeof(szIpAddress),
|
---|
796 | "%RTnaipv6", (PRTNETADDRIPV6)&pgrSa6->sin6_addr);
|
---|
797 | enmAddrType = RTNETADDRTYPE_IPV6;
|
---|
798 | rc = VINF_SUCCESS;
|
---|
799 | }
|
---|
800 | else
|
---|
801 | {
|
---|
802 | rc = VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
803 | szIpAddress[0] = '\0';
|
---|
804 | cchIpAddress = 0;
|
---|
805 | }
|
---|
806 | freeaddrinfo(pgrResults);
|
---|
807 |
|
---|
808 | /*
|
---|
809 | * Copy out the result.
|
---|
810 | */
|
---|
811 | size_t const cbResult = *pcbResult;
|
---|
812 | *pcbResult = cchIpAddress + 1;
|
---|
813 | if (cchIpAddress < cbResult)
|
---|
814 | memcpy(pszResult, szIpAddress, cchIpAddress + 1);
|
---|
815 | else
|
---|
816 | {
|
---|
817 | RT_BZERO(pszResult, cbResult);
|
---|
818 | if (RT_SUCCESS(rc))
|
---|
819 | rc = VERR_BUFFER_OVERFLOW;
|
---|
820 | }
|
---|
821 | if (penmAddrType && RT_SUCCESS(rc))
|
---|
822 | *penmAddrType = enmAddrType;
|
---|
823 | return rc;
|
---|
824 | #endif /* !RT_OS_OS2 */
|
---|
825 | }
|
---|
826 |
|
---|
827 |
|
---|
828 | RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
829 | {
|
---|
830 | /*
|
---|
831 | * Validate input.
|
---|
832 | */
|
---|
833 | RTSOCKETINT *pThis = hSocket;
|
---|
834 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
835 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
836 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
837 | AssertPtr(pvBuffer);
|
---|
838 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
839 |
|
---|
840 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
841 | if (RT_FAILURE(rc))
|
---|
842 | return rc;
|
---|
843 |
|
---|
844 | /*
|
---|
845 | * Read loop.
|
---|
846 | * If pcbRead is NULL we have to fill the entire buffer!
|
---|
847 | */
|
---|
848 | size_t cbRead = 0;
|
---|
849 | size_t cbToRead = cbBuffer;
|
---|
850 | for (;;)
|
---|
851 | {
|
---|
852 | rtSocketErrorReset();
|
---|
853 | #ifdef RTSOCKET_MAX_READ
|
---|
854 | int cbNow = cbToRead >= RTSOCKET_MAX_READ ? RTSOCKET_MAX_READ : (int)cbToRead;
|
---|
855 | #else
|
---|
856 | size_t cbNow = cbToRead;
|
---|
857 | #endif
|
---|
858 | ssize_t cbBytesRead = recv(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL);
|
---|
859 | if (cbBytesRead <= 0)
|
---|
860 | {
|
---|
861 | rc = rtSocketError();
|
---|
862 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
863 | if (RT_SUCCESS_NP(rc))
|
---|
864 | {
|
---|
865 | if (!pcbRead)
|
---|
866 | rc = VERR_NET_SHUTDOWN;
|
---|
867 | else
|
---|
868 | {
|
---|
869 | *pcbRead = 0;
|
---|
870 | rc = VINF_SUCCESS;
|
---|
871 | }
|
---|
872 | }
|
---|
873 | break;
|
---|
874 | }
|
---|
875 | if (pcbRead)
|
---|
876 | {
|
---|
877 | /* return partial data */
|
---|
878 | *pcbRead = cbBytesRead;
|
---|
879 | break;
|
---|
880 | }
|
---|
881 |
|
---|
882 | /* read more? */
|
---|
883 | cbRead += cbBytesRead;
|
---|
884 | if (cbRead == cbBuffer)
|
---|
885 | break;
|
---|
886 |
|
---|
887 | /* next */
|
---|
888 | cbToRead = cbBuffer - cbRead;
|
---|
889 | }
|
---|
890 |
|
---|
891 | rtSocketUnlock(pThis);
|
---|
892 | return rc;
|
---|
893 | }
|
---|
894 |
|
---|
895 |
|
---|
896 | RTDECL(int) RTSocketReadFrom(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead, PRTNETADDR pSrcAddr)
|
---|
897 | {
|
---|
898 | /*
|
---|
899 | * Validate input.
|
---|
900 | */
|
---|
901 | RTSOCKETINT *pThis = hSocket;
|
---|
902 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
903 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
904 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
905 | AssertPtr(pvBuffer);
|
---|
906 | AssertPtr(pcbRead);
|
---|
907 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
908 |
|
---|
909 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
910 | if (RT_FAILURE(rc))
|
---|
911 | return rc;
|
---|
912 |
|
---|
913 | /*
|
---|
914 | * Read data.
|
---|
915 | */
|
---|
916 | size_t cbRead = 0;
|
---|
917 | size_t cbToRead = cbBuffer;
|
---|
918 | rtSocketErrorReset();
|
---|
919 | RTSOCKADDRUNION u;
|
---|
920 | #ifdef RTSOCKET_MAX_READ
|
---|
921 | int cbNow = cbToRead >= RTSOCKET_MAX_READ ? RTSOCKET_MAX_READ : (int)cbToRead;
|
---|
922 | int cbAddr = sizeof(u);
|
---|
923 | #else
|
---|
924 | size_t cbNow = cbToRead;
|
---|
925 | socklen_t cbAddr = sizeof(u);
|
---|
926 | #endif
|
---|
927 | ssize_t cbBytesRead = recvfrom(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL, &u.Addr, &cbAddr);
|
---|
928 | if (cbBytesRead <= 0)
|
---|
929 | {
|
---|
930 | rc = rtSocketError();
|
---|
931 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
932 | if (RT_SUCCESS_NP(rc))
|
---|
933 | {
|
---|
934 | *pcbRead = 0;
|
---|
935 | rc = VINF_SUCCESS;
|
---|
936 | }
|
---|
937 | }
|
---|
938 | else
|
---|
939 | {
|
---|
940 | if (pSrcAddr)
|
---|
941 | rc = rtSocketNetAddrFromAddr(&u, cbAddr, pSrcAddr);
|
---|
942 | *pcbRead = cbBytesRead;
|
---|
943 | }
|
---|
944 |
|
---|
945 | rtSocketUnlock(pThis);
|
---|
946 | return rc;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer)
|
---|
951 | {
|
---|
952 | /*
|
---|
953 | * Validate input.
|
---|
954 | */
|
---|
955 | RTSOCKETINT *pThis = hSocket;
|
---|
956 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
957 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
958 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
959 |
|
---|
960 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
961 | if (RT_FAILURE(rc))
|
---|
962 | return rc;
|
---|
963 |
|
---|
964 | /*
|
---|
965 | * Try write all at once.
|
---|
966 | */
|
---|
967 | #ifdef RTSOCKET_MAX_WRITE
|
---|
968 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
969 | #else
|
---|
970 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
971 | #endif
|
---|
972 | ssize_t cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
973 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
974 | rc = VINF_SUCCESS;
|
---|
975 | else if (cbWritten < 0)
|
---|
976 | rc = rtSocketError();
|
---|
977 | else
|
---|
978 | {
|
---|
979 | /*
|
---|
980 | * Unfinished business, write the remainder of the request. Must ignore
|
---|
981 | * VERR_INTERRUPTED here if we've managed to send something.
|
---|
982 | */
|
---|
983 | size_t cbSentSoFar = 0;
|
---|
984 | for (;;)
|
---|
985 | {
|
---|
986 | /* advance */
|
---|
987 | cbBuffer -= (size_t)cbWritten;
|
---|
988 | if (!cbBuffer)
|
---|
989 | break;
|
---|
990 | cbSentSoFar += (size_t)cbWritten;
|
---|
991 | pvBuffer = (char const *)pvBuffer + cbWritten;
|
---|
992 |
|
---|
993 | /* send */
|
---|
994 | #ifdef RTSOCKET_MAX_WRITE
|
---|
995 | cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
996 | #else
|
---|
997 | cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
998 | #endif
|
---|
999 | cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1000 | if (cbWritten >= 0)
|
---|
1001 | AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtSocketError()=%d\n",
|
---|
1002 | cbWritten, cbBuffer, rtSocketError()));
|
---|
1003 | else
|
---|
1004 | {
|
---|
1005 | rc = rtSocketError();
|
---|
1006 | if (rc != VERR_INTERNAL_ERROR || cbSentSoFar == 0)
|
---|
1007 | break;
|
---|
1008 | cbWritten = 0;
|
---|
1009 | rc = VINF_SUCCESS;
|
---|
1010 | }
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | rtSocketUnlock(pThis);
|
---|
1015 | return rc;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 |
|
---|
1019 | RTDECL(int) RTSocketWriteTo(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pAddr)
|
---|
1020 | {
|
---|
1021 | /*
|
---|
1022 | * Validate input.
|
---|
1023 | */
|
---|
1024 | RTSOCKETINT *pThis = hSocket;
|
---|
1025 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1026 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1027 |
|
---|
1028 | /* no locking since UDP reads may be done concurrently to writes, and
|
---|
1029 | * this is the normal use case of this code. */
|
---|
1030 |
|
---|
1031 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1032 | if (RT_FAILURE(rc))
|
---|
1033 | return rc;
|
---|
1034 |
|
---|
1035 | /* Figure out destination address. */
|
---|
1036 | struct sockaddr *pSA = NULL;
|
---|
1037 | #ifdef RT_OS_WINDOWS
|
---|
1038 | int cbSA = 0;
|
---|
1039 | #else
|
---|
1040 | socklen_t cbSA = 0;
|
---|
1041 | #endif
|
---|
1042 | RTSOCKADDRUNION u;
|
---|
1043 | if (pAddr)
|
---|
1044 | {
|
---|
1045 | rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), NULL);
|
---|
1046 | if (RT_FAILURE(rc))
|
---|
1047 | return rc;
|
---|
1048 | pSA = &u.Addr;
|
---|
1049 | cbSA = sizeof(u);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | /*
|
---|
1053 | * Must write all at once, otherwise it is a failure.
|
---|
1054 | */
|
---|
1055 | #ifdef RT_OS_WINDOWS
|
---|
1056 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1057 | #else
|
---|
1058 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
1059 | #endif
|
---|
1060 | ssize_t cbWritten = sendto(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL, pSA, cbSA);
|
---|
1061 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
1062 | rc = VINF_SUCCESS;
|
---|
1063 | else if (cbWritten < 0)
|
---|
1064 | rc = rtSocketError();
|
---|
1065 | else
|
---|
1066 | rc = VERR_TOO_MUCH_DATA;
|
---|
1067 |
|
---|
1068 | rtSocketUnlock(pThis);
|
---|
1069 | return rc;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 |
|
---|
1073 | RTDECL(int) RTSocketWriteToNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, PCRTNETADDR pAddr)
|
---|
1074 | {
|
---|
1075 | /*
|
---|
1076 | * Validate input.
|
---|
1077 | */
|
---|
1078 | RTSOCKETINT *pThis = hSocket;
|
---|
1079 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1080 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1081 |
|
---|
1082 | /* no locking since UDP reads may be done concurrently to writes, and
|
---|
1083 | * this is the normal use case of this code. */
|
---|
1084 |
|
---|
1085 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1086 | if (RT_FAILURE(rc))
|
---|
1087 | return rc;
|
---|
1088 |
|
---|
1089 | /* Figure out destination address. */
|
---|
1090 | struct sockaddr *pSA = NULL;
|
---|
1091 | #ifdef RT_OS_WINDOWS
|
---|
1092 | int cbSA = 0;
|
---|
1093 | #else
|
---|
1094 | socklen_t cbSA = 0;
|
---|
1095 | #endif
|
---|
1096 | RTSOCKADDRUNION u;
|
---|
1097 | if (pAddr)
|
---|
1098 | {
|
---|
1099 | rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), NULL);
|
---|
1100 | if (RT_FAILURE(rc))
|
---|
1101 | return rc;
|
---|
1102 | pSA = &u.Addr;
|
---|
1103 | cbSA = sizeof(u);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | /*
|
---|
1107 | * Must write all at once, otherwise it is a failure.
|
---|
1108 | */
|
---|
1109 | #ifdef RT_OS_WINDOWS
|
---|
1110 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1111 | #else
|
---|
1112 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
1113 | #endif
|
---|
1114 | ssize_t cbWritten = sendto(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL, pSA, cbSA);
|
---|
1115 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
1116 | rc = VINF_SUCCESS;
|
---|
1117 | else if (cbWritten < 0)
|
---|
1118 | rc = rtSocketError();
|
---|
1119 | else
|
---|
1120 | rc = VERR_TOO_MUCH_DATA;
|
---|
1121 |
|
---|
1122 | rtSocketUnlock(pThis);
|
---|
1123 | return rc;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 |
|
---|
1127 | RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf)
|
---|
1128 | {
|
---|
1129 | /*
|
---|
1130 | * Validate input.
|
---|
1131 | */
|
---|
1132 | RTSOCKETINT *pThis = hSocket;
|
---|
1133 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1134 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1135 | AssertPtrReturn(pSgBuf, VERR_INVALID_PARAMETER);
|
---|
1136 | AssertReturn(pSgBuf->cSegs > 0, VERR_INVALID_PARAMETER);
|
---|
1137 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1138 |
|
---|
1139 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1140 | if (RT_FAILURE(rc))
|
---|
1141 | return rc;
|
---|
1142 |
|
---|
1143 | /*
|
---|
1144 | * Construct message descriptor (translate pSgBuf) and send it.
|
---|
1145 | */
|
---|
1146 | rc = VERR_NO_TMP_MEMORY;
|
---|
1147 | #ifdef RT_OS_WINDOWS
|
---|
1148 | AssertCompileSize(WSABUF, sizeof(RTSGSEG));
|
---|
1149 | AssertCompileMemberSize(WSABUF, buf, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
1150 |
|
---|
1151 | LPWSABUF paMsg = (LPWSABUF)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(WSABUF));
|
---|
1152 | if (paMsg)
|
---|
1153 | {
|
---|
1154 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
1155 | {
|
---|
1156 | paMsg[i].buf = (char *)pSgBuf->paSegs[i].pvSeg;
|
---|
1157 | paMsg[i].len = (u_long)pSgBuf->paSegs[i].cbSeg;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | DWORD dwSent;
|
---|
1161 | int hrc = WSASend(pThis->hNative, paMsg, pSgBuf->cSegs, &dwSent,
|
---|
1162 | MSG_NOSIGNAL, NULL, NULL);
|
---|
1163 | if (!hrc)
|
---|
1164 | rc = VINF_SUCCESS;
|
---|
1165 | /** @todo check for incomplete writes */
|
---|
1166 | else
|
---|
1167 | rc = rtSocketError();
|
---|
1168 |
|
---|
1169 | RTMemTmpFree(paMsg);
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | #else /* !RT_OS_WINDOWS */
|
---|
1173 | AssertCompileSize(struct iovec, sizeof(RTSGSEG));
|
---|
1174 | AssertCompileMemberSize(struct iovec, iov_base, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
1175 | AssertCompileMemberSize(struct iovec, iov_len, RT_SIZEOFMEMB(RTSGSEG, cbSeg));
|
---|
1176 |
|
---|
1177 | struct iovec *paMsg = (struct iovec *)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(struct iovec));
|
---|
1178 | if (paMsg)
|
---|
1179 | {
|
---|
1180 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
1181 | {
|
---|
1182 | paMsg[i].iov_base = pSgBuf->paSegs[i].pvSeg;
|
---|
1183 | paMsg[i].iov_len = pSgBuf->paSegs[i].cbSeg;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | struct msghdr msgHdr;
|
---|
1187 | RT_ZERO(msgHdr);
|
---|
1188 | msgHdr.msg_iov = paMsg;
|
---|
1189 | msgHdr.msg_iovlen = pSgBuf->cSegs;
|
---|
1190 | ssize_t cbWritten = sendmsg(pThis->hNative, &msgHdr, MSG_NOSIGNAL);
|
---|
1191 | if (RT_LIKELY(cbWritten >= 0))
|
---|
1192 | rc = VINF_SUCCESS;
|
---|
1193 | /** @todo check for incomplete writes */
|
---|
1194 | else
|
---|
1195 | rc = rtSocketError();
|
---|
1196 |
|
---|
1197 | RTMemTmpFree(paMsg);
|
---|
1198 | }
|
---|
1199 | #endif /* !RT_OS_WINDOWS */
|
---|
1200 |
|
---|
1201 | rtSocketUnlock(pThis);
|
---|
1202 | return rc;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 |
|
---|
1206 | RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...)
|
---|
1207 | {
|
---|
1208 | va_list va;
|
---|
1209 | va_start(va, cSegs);
|
---|
1210 | int rc = RTSocketSgWriteLV(hSocket, cSegs, va);
|
---|
1211 | va_end(va);
|
---|
1212 | return rc;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 |
|
---|
1216 | RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va)
|
---|
1217 | {
|
---|
1218 | /*
|
---|
1219 | * Set up a S/G segment array + buffer on the stack and pass it
|
---|
1220 | * on to RTSocketSgWrite.
|
---|
1221 | */
|
---|
1222 | Assert(cSegs <= 16);
|
---|
1223 | PRTSGSEG paSegs = (PRTSGSEG)alloca(cSegs * sizeof(RTSGSEG));
|
---|
1224 | AssertReturn(paSegs, VERR_NO_TMP_MEMORY);
|
---|
1225 | for (size_t i = 0; i < cSegs; i++)
|
---|
1226 | {
|
---|
1227 | paSegs[i].pvSeg = va_arg(va, void *);
|
---|
1228 | paSegs[i].cbSeg = va_arg(va, size_t);
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | RTSGBUF SgBuf;
|
---|
1232 | RTSgBufInit(&SgBuf, paSegs, cSegs);
|
---|
1233 | return RTSocketSgWrite(hSocket, &SgBuf);
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
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 | AssertPtrReturn(pcbRead, VERR_INVALID_PARAMETER);
|
---|
1248 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1249 |
|
---|
1250 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1251 | if (RT_FAILURE(rc))
|
---|
1252 | return rc;
|
---|
1253 |
|
---|
1254 | rtSocketErrorReset();
|
---|
1255 | #ifdef RTSOCKET_MAX_READ
|
---|
1256 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1257 | #else
|
---|
1258 | size_t cbNow = cbBuffer;
|
---|
1259 | #endif
|
---|
1260 |
|
---|
1261 | #ifdef RT_OS_WINDOWS
|
---|
1262 | int cbRead = recv(pThis->hNative, (char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1263 | if (cbRead >= 0)
|
---|
1264 | {
|
---|
1265 | *pcbRead = cbRead;
|
---|
1266 | rc = VINF_SUCCESS;
|
---|
1267 | }
|
---|
1268 | else
|
---|
1269 | rc = rtSocketError();
|
---|
1270 |
|
---|
1271 | if (rc == VERR_TRY_AGAIN)
|
---|
1272 | rc = VINF_TRY_AGAIN;
|
---|
1273 | #else
|
---|
1274 | ssize_t cbRead = recv(pThis->hNative, pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1275 | if (cbRead >= 0)
|
---|
1276 | *pcbRead = cbRead;
|
---|
1277 | else if ( errno == EAGAIN
|
---|
1278 | # ifdef EWOULDBLOCK
|
---|
1279 | # if EWOULDBLOCK != EAGAIN
|
---|
1280 | || errno == EWOULDBLOCK
|
---|
1281 | # endif
|
---|
1282 | # endif
|
---|
1283 | )
|
---|
1284 | {
|
---|
1285 | *pcbRead = 0;
|
---|
1286 | rc = VINF_TRY_AGAIN;
|
---|
1287 | }
|
---|
1288 | else
|
---|
1289 | rc = rtSocketError();
|
---|
1290 | #endif
|
---|
1291 |
|
---|
1292 | rtSocketUnlock(pThis);
|
---|
1293 | return rc;
|
---|
1294 | }
|
---|
1295 |
|
---|
1296 |
|
---|
1297 | RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten)
|
---|
1298 | {
|
---|
1299 | /*
|
---|
1300 | * Validate input.
|
---|
1301 | */
|
---|
1302 | RTSOCKETINT *pThis = hSocket;
|
---|
1303 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1304 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1305 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
1306 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1307 |
|
---|
1308 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1309 | if (RT_FAILURE(rc))
|
---|
1310 | return rc;
|
---|
1311 |
|
---|
1312 | rtSocketErrorReset();
|
---|
1313 | #ifdef RTSOCKET_MAX_WRITE
|
---|
1314 | int cbNow = cbBuffer >= RTSOCKET_MAX_WRITE ? RTSOCKET_MAX_WRITE : (int)cbBuffer;
|
---|
1315 | #else
|
---|
1316 | size_t cbNow = cbBuffer;
|
---|
1317 | #endif
|
---|
1318 |
|
---|
1319 | #ifdef RT_OS_WINDOWS
|
---|
1320 | int cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
1321 | if (cbWritten >= 0)
|
---|
1322 | {
|
---|
1323 | *pcbWritten = cbWritten;
|
---|
1324 | rc = VINF_SUCCESS;
|
---|
1325 | }
|
---|
1326 | else
|
---|
1327 | rc = rtSocketError();
|
---|
1328 |
|
---|
1329 | if (rc == VERR_TRY_AGAIN)
|
---|
1330 | rc = VINF_TRY_AGAIN;
|
---|
1331 | #else
|
---|
1332 | ssize_t cbWritten = send(pThis->hNative, pvBuffer, cbBuffer, MSG_NOSIGNAL);
|
---|
1333 | if (cbWritten >= 0)
|
---|
1334 | *pcbWritten = cbWritten;
|
---|
1335 | else if ( errno == EAGAIN
|
---|
1336 | # ifdef EWOULDBLOCK
|
---|
1337 | # if EWOULDBLOCK != EAGAIN
|
---|
1338 | || errno == EWOULDBLOCK
|
---|
1339 | # endif
|
---|
1340 | # endif
|
---|
1341 | )
|
---|
1342 | {
|
---|
1343 | *pcbWritten = 0;
|
---|
1344 | rc = VINF_TRY_AGAIN;
|
---|
1345 | }
|
---|
1346 | else
|
---|
1347 | rc = rtSocketError();
|
---|
1348 | #endif
|
---|
1349 |
|
---|
1350 | rtSocketUnlock(pThis);
|
---|
1351 | return rc;
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 |
|
---|
1355 | RTDECL(int) RTSocketSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten)
|
---|
1356 | {
|
---|
1357 | /*
|
---|
1358 | * Validate input.
|
---|
1359 | */
|
---|
1360 | RTSOCKETINT *pThis = hSocket;
|
---|
1361 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1362 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1363 | AssertPtrReturn(pSgBuf, VERR_INVALID_PARAMETER);
|
---|
1364 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
1365 | AssertReturn(pSgBuf->cSegs > 0, VERR_INVALID_PARAMETER);
|
---|
1366 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1367 |
|
---|
1368 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1369 | if (RT_FAILURE(rc))
|
---|
1370 | return rc;
|
---|
1371 |
|
---|
1372 | unsigned cSegsToSend = 0;
|
---|
1373 | rc = VERR_NO_TMP_MEMORY;
|
---|
1374 | #ifdef RT_OS_WINDOWS
|
---|
1375 | LPWSABUF paMsg = NULL;
|
---|
1376 |
|
---|
1377 | RTSgBufMapToNative(paMsg, pSgBuf, WSABUF, buf, char *, len, u_long, cSegsToSend);
|
---|
1378 | if (paMsg)
|
---|
1379 | {
|
---|
1380 | DWORD dwSent = 0;
|
---|
1381 | int hrc = WSASend(pThis->hNative, paMsg, cSegsToSend, &dwSent,
|
---|
1382 | MSG_NOSIGNAL, NULL, NULL);
|
---|
1383 | if (!hrc)
|
---|
1384 | rc = VINF_SUCCESS;
|
---|
1385 | else
|
---|
1386 | rc = rtSocketError();
|
---|
1387 |
|
---|
1388 | *pcbWritten = dwSent;
|
---|
1389 |
|
---|
1390 | RTMemTmpFree(paMsg);
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | #else /* !RT_OS_WINDOWS */
|
---|
1394 | struct iovec *paMsg = NULL;
|
---|
1395 |
|
---|
1396 | RTSgBufMapToNative(paMsg, pSgBuf, struct iovec, iov_base, void *, iov_len, size_t, cSegsToSend);
|
---|
1397 | if (paMsg)
|
---|
1398 | {
|
---|
1399 | struct msghdr msgHdr;
|
---|
1400 | RT_ZERO(msgHdr);
|
---|
1401 | msgHdr.msg_iov = paMsg;
|
---|
1402 | msgHdr.msg_iovlen = cSegsToSend;
|
---|
1403 | ssize_t cbWritten = sendmsg(pThis->hNative, &msgHdr, MSG_NOSIGNAL);
|
---|
1404 | if (RT_LIKELY(cbWritten >= 0))
|
---|
1405 | {
|
---|
1406 | rc = VINF_SUCCESS;
|
---|
1407 | *pcbWritten = cbWritten;
|
---|
1408 | }
|
---|
1409 | else
|
---|
1410 | rc = rtSocketError();
|
---|
1411 |
|
---|
1412 | RTMemTmpFree(paMsg);
|
---|
1413 | }
|
---|
1414 | #endif /* !RT_OS_WINDOWS */
|
---|
1415 |
|
---|
1416 | rtSocketUnlock(pThis);
|
---|
1417 | return rc;
|
---|
1418 | }
|
---|
1419 |
|
---|
1420 |
|
---|
1421 | RTDECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...)
|
---|
1422 | {
|
---|
1423 | va_list va;
|
---|
1424 | va_start(va, pcbWritten);
|
---|
1425 | int rc = RTSocketSgWriteLVNB(hSocket, cSegs, pcbWritten, va);
|
---|
1426 | va_end(va);
|
---|
1427 | return rc;
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 |
|
---|
1431 | RTDECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va)
|
---|
1432 | {
|
---|
1433 | /*
|
---|
1434 | * Set up a S/G segment array + buffer on the stack and pass it
|
---|
1435 | * on to RTSocketSgWrite.
|
---|
1436 | */
|
---|
1437 | Assert(cSegs <= 16);
|
---|
1438 | PRTSGSEG paSegs = (PRTSGSEG)alloca(cSegs * sizeof(RTSGSEG));
|
---|
1439 | AssertReturn(paSegs, VERR_NO_TMP_MEMORY);
|
---|
1440 | for (size_t i = 0; i < cSegs; i++)
|
---|
1441 | {
|
---|
1442 | paSegs[i].pvSeg = va_arg(va, void *);
|
---|
1443 | paSegs[i].cbSeg = va_arg(va, size_t);
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | RTSGBUF SgBuf;
|
---|
1447 | RTSgBufInit(&SgBuf, paSegs, cSegs);
|
---|
1448 | return RTSocketSgWriteNB(hSocket, &SgBuf, pcbWritten);
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 |
|
---|
1452 | RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies)
|
---|
1453 | {
|
---|
1454 | /*
|
---|
1455 | * Validate input.
|
---|
1456 | */
|
---|
1457 | RTSOCKETINT *pThis = hSocket;
|
---|
1458 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1459 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1460 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1461 | int const fdMax = (int)pThis->hNative + 1;
|
---|
1462 | AssertReturn(fdMax - 1 == pThis->hNative, VERR_INTERNAL_ERROR_5);
|
---|
1463 |
|
---|
1464 | /*
|
---|
1465 | * Set up the file descriptor sets and do the select.
|
---|
1466 | */
|
---|
1467 | fd_set fdsetR;
|
---|
1468 | FD_ZERO(&fdsetR);
|
---|
1469 | FD_SET(pThis->hNative, &fdsetR);
|
---|
1470 |
|
---|
1471 | fd_set fdsetE = fdsetR;
|
---|
1472 |
|
---|
1473 | int rc;
|
---|
1474 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
1475 | rc = select(fdMax, &fdsetR, NULL, &fdsetE, NULL);
|
---|
1476 | else
|
---|
1477 | {
|
---|
1478 | struct timeval timeout;
|
---|
1479 | timeout.tv_sec = cMillies / 1000;
|
---|
1480 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
1481 | rc = select(fdMax, &fdsetR, NULL, &fdsetE, &timeout);
|
---|
1482 | }
|
---|
1483 | if (rc > 0)
|
---|
1484 | rc = VINF_SUCCESS;
|
---|
1485 | else if (rc == 0)
|
---|
1486 | rc = VERR_TIMEOUT;
|
---|
1487 | else
|
---|
1488 | rc = rtSocketError();
|
---|
1489 |
|
---|
1490 | return rc;
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 |
|
---|
1494 | RTDECL(int) RTSocketSelectOneEx(RTSOCKET hSocket, uint32_t fEvents, uint32_t *pfEvents, RTMSINTERVAL cMillies)
|
---|
1495 | {
|
---|
1496 | /*
|
---|
1497 | * Validate input.
|
---|
1498 | */
|
---|
1499 | RTSOCKETINT *pThis = hSocket;
|
---|
1500 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1501 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1502 | AssertPtrReturn(pfEvents, VERR_INVALID_PARAMETER);
|
---|
1503 | AssertReturn(!(fEvents & ~RTSOCKET_EVT_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
1504 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1505 | int const fdMax = (int)pThis->hNative + 1;
|
---|
1506 | AssertReturn(fdMax - 1 == pThis->hNative, VERR_INTERNAL_ERROR_5);
|
---|
1507 |
|
---|
1508 | *pfEvents = 0;
|
---|
1509 |
|
---|
1510 | /*
|
---|
1511 | * Set up the file descriptor sets and do the select.
|
---|
1512 | */
|
---|
1513 | fd_set fdsetR;
|
---|
1514 | fd_set fdsetW;
|
---|
1515 | fd_set fdsetE;
|
---|
1516 | FD_ZERO(&fdsetR);
|
---|
1517 | FD_ZERO(&fdsetW);
|
---|
1518 | FD_ZERO(&fdsetE);
|
---|
1519 |
|
---|
1520 | if (fEvents & RTSOCKET_EVT_READ)
|
---|
1521 | FD_SET(pThis->hNative, &fdsetR);
|
---|
1522 | if (fEvents & RTSOCKET_EVT_WRITE)
|
---|
1523 | FD_SET(pThis->hNative, &fdsetW);
|
---|
1524 | if (fEvents & RTSOCKET_EVT_ERROR)
|
---|
1525 | FD_SET(pThis->hNative, &fdsetE);
|
---|
1526 |
|
---|
1527 | int rc;
|
---|
1528 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
1529 | rc = select(fdMax, &fdsetR, &fdsetW, &fdsetE, NULL);
|
---|
1530 | else
|
---|
1531 | {
|
---|
1532 | struct timeval timeout;
|
---|
1533 | timeout.tv_sec = cMillies / 1000;
|
---|
1534 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
1535 | rc = select(fdMax, &fdsetR, &fdsetW, &fdsetE, &timeout);
|
---|
1536 | }
|
---|
1537 | if (rc > 0)
|
---|
1538 | {
|
---|
1539 | if (FD_ISSET(pThis->hNative, &fdsetR))
|
---|
1540 | *pfEvents |= RTSOCKET_EVT_READ;
|
---|
1541 | if (FD_ISSET(pThis->hNative, &fdsetW))
|
---|
1542 | *pfEvents |= RTSOCKET_EVT_WRITE;
|
---|
1543 | if (FD_ISSET(pThis->hNative, &fdsetE))
|
---|
1544 | *pfEvents |= RTSOCKET_EVT_ERROR;
|
---|
1545 |
|
---|
1546 | rc = VINF_SUCCESS;
|
---|
1547 | }
|
---|
1548 | else if (rc == 0)
|
---|
1549 | rc = VERR_TIMEOUT;
|
---|
1550 | else
|
---|
1551 | rc = rtSocketError();
|
---|
1552 |
|
---|
1553 | return rc;
|
---|
1554 | }
|
---|
1555 |
|
---|
1556 |
|
---|
1557 | RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite)
|
---|
1558 | {
|
---|
1559 | /*
|
---|
1560 | * Validate input, don't lock it because we might want to interrupt a call
|
---|
1561 | * active on a different thread.
|
---|
1562 | */
|
---|
1563 | RTSOCKETINT *pThis = hSocket;
|
---|
1564 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1565 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1566 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1567 | AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
|
---|
1568 |
|
---|
1569 | /*
|
---|
1570 | * Do the job.
|
---|
1571 | */
|
---|
1572 | int rc = VINF_SUCCESS;
|
---|
1573 | int fHow;
|
---|
1574 | if (fRead && fWrite)
|
---|
1575 | fHow = SHUT_RDWR;
|
---|
1576 | else if (fRead)
|
---|
1577 | fHow = SHUT_RD;
|
---|
1578 | else
|
---|
1579 | fHow = SHUT_WR;
|
---|
1580 | if (shutdown(pThis->hNative, fHow) == -1)
|
---|
1581 | rc = rtSocketError();
|
---|
1582 |
|
---|
1583 | return rc;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 |
|
---|
1587 | RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
1588 | {
|
---|
1589 | /*
|
---|
1590 | * Validate input.
|
---|
1591 | */
|
---|
1592 | RTSOCKETINT *pThis = hSocket;
|
---|
1593 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1594 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1595 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1596 |
|
---|
1597 | /*
|
---|
1598 | * Get the address and convert it.
|
---|
1599 | */
|
---|
1600 | int rc;
|
---|
1601 | RTSOCKADDRUNION u;
|
---|
1602 | #ifdef RT_OS_WINDOWS
|
---|
1603 | int cbAddr = sizeof(u);
|
---|
1604 | #else
|
---|
1605 | socklen_t cbAddr = sizeof(u);
|
---|
1606 | #endif
|
---|
1607 | RT_ZERO(u);
|
---|
1608 | if (getsockname(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
1609 | rc = rtSocketNetAddrFromAddr(&u, cbAddr, pAddr);
|
---|
1610 | else
|
---|
1611 | rc = rtSocketError();
|
---|
1612 |
|
---|
1613 | return rc;
|
---|
1614 | }
|
---|
1615 |
|
---|
1616 |
|
---|
1617 | RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
1618 | {
|
---|
1619 | /*
|
---|
1620 | * Validate input.
|
---|
1621 | */
|
---|
1622 | RTSOCKETINT *pThis = hSocket;
|
---|
1623 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1624 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1625 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1626 |
|
---|
1627 | /*
|
---|
1628 | * Get the address and convert it.
|
---|
1629 | */
|
---|
1630 | int rc;
|
---|
1631 | RTSOCKADDRUNION u;
|
---|
1632 | #ifdef RT_OS_WINDOWS
|
---|
1633 | int cbAddr = sizeof(u);
|
---|
1634 | #else
|
---|
1635 | socklen_t cbAddr = sizeof(u);
|
---|
1636 | #endif
|
---|
1637 | RT_ZERO(u);
|
---|
1638 | if (getpeername(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
1639 | rc = rtSocketNetAddrFromAddr(&u, cbAddr, pAddr);
|
---|
1640 | else
|
---|
1641 | rc = rtSocketError();
|
---|
1642 |
|
---|
1643 | return rc;
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 |
|
---|
1647 |
|
---|
1648 | /**
|
---|
1649 | * Wrapper around bind.
|
---|
1650 | *
|
---|
1651 | * @returns IPRT status code.
|
---|
1652 | * @param hSocket The socket handle.
|
---|
1653 | * @param pAddr The address to bind to.
|
---|
1654 | */
|
---|
1655 | DECLHIDDEN(int) rtSocketBind(RTSOCKET hSocket, PCRTNETADDR pAddr)
|
---|
1656 | {
|
---|
1657 | RTSOCKADDRUNION u;
|
---|
1658 | int cbAddr;
|
---|
1659 | int rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), &cbAddr);
|
---|
1660 | if (RT_SUCCESS(rc))
|
---|
1661 | rc = rtSocketBindRawAddr(hSocket, &u.Addr, cbAddr);
|
---|
1662 | return rc;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 |
|
---|
1666 | /**
|
---|
1667 | * Very thin wrapper around bind.
|
---|
1668 | *
|
---|
1669 | * @returns IPRT status code.
|
---|
1670 | * @param hSocket The socket handle.
|
---|
1671 | * @param pvAddr The address to bind to (struct sockaddr and
|
---|
1672 | * friends).
|
---|
1673 | * @param cbAddr The size of the address.
|
---|
1674 | */
|
---|
1675 | DECLHIDDEN(int) rtSocketBindRawAddr(RTSOCKET hSocket, void const *pvAddr, size_t cbAddr)
|
---|
1676 | {
|
---|
1677 | /*
|
---|
1678 | * Validate input.
|
---|
1679 | */
|
---|
1680 | RTSOCKETINT *pThis = hSocket;
|
---|
1681 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1682 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1683 | AssertPtrReturn(pvAddr, VERR_INVALID_POINTER);
|
---|
1684 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1685 |
|
---|
1686 | int rc;
|
---|
1687 | if (bind(pThis->hNative, (struct sockaddr const *)pvAddr, (int)cbAddr) == 0)
|
---|
1688 | rc = VINF_SUCCESS;
|
---|
1689 | else
|
---|
1690 | rc = rtSocketError();
|
---|
1691 |
|
---|
1692 | rtSocketUnlock(pThis);
|
---|
1693 | return rc;
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 |
|
---|
1697 |
|
---|
1698 | /**
|
---|
1699 | * Wrapper around listen.
|
---|
1700 | *
|
---|
1701 | * @returns IPRT status code.
|
---|
1702 | * @param hSocket The socket handle.
|
---|
1703 | * @param cMaxPending The max number of pending connections.
|
---|
1704 | */
|
---|
1705 | DECLHIDDEN(int) rtSocketListen(RTSOCKET hSocket, int cMaxPending)
|
---|
1706 | {
|
---|
1707 | /*
|
---|
1708 | * Validate input.
|
---|
1709 | */
|
---|
1710 | RTSOCKETINT *pThis = hSocket;
|
---|
1711 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1712 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1713 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1714 |
|
---|
1715 | int rc = VINF_SUCCESS;
|
---|
1716 | if (listen(pThis->hNative, cMaxPending) != 0)
|
---|
1717 | rc = rtSocketError();
|
---|
1718 |
|
---|
1719 | rtSocketUnlock(pThis);
|
---|
1720 | return rc;
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 |
|
---|
1724 | /**
|
---|
1725 | * Wrapper around accept.
|
---|
1726 | *
|
---|
1727 | * @returns IPRT status code.
|
---|
1728 | * @param hSocket The socket handle.
|
---|
1729 | * @param phClient Where to return the client socket handle on
|
---|
1730 | * success.
|
---|
1731 | * @param pAddr Where to return the client address.
|
---|
1732 | * @param pcbAddr On input this gives the size buffer size of what
|
---|
1733 | * @a pAddr point to. On return this contains the
|
---|
1734 | * size of what's stored at @a pAddr.
|
---|
1735 | */
|
---|
1736 | DECLHIDDEN(int) rtSocketAccept(RTSOCKET hSocket, PRTSOCKET phClient, struct sockaddr *pAddr, size_t *pcbAddr)
|
---|
1737 | {
|
---|
1738 | /*
|
---|
1739 | * Validate input.
|
---|
1740 | * Only lock the socket temporarily while we get the native handle, so that
|
---|
1741 | * we can safely shutdown and destroy the socket from a different thread.
|
---|
1742 | */
|
---|
1743 | RTSOCKETINT *pThis = hSocket;
|
---|
1744 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1745 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1746 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1747 |
|
---|
1748 | /*
|
---|
1749 | * Call accept().
|
---|
1750 | */
|
---|
1751 | rtSocketErrorReset();
|
---|
1752 | int rc = VINF_SUCCESS;
|
---|
1753 | #ifdef RT_OS_WINDOWS
|
---|
1754 | int cbAddr = (int)*pcbAddr;
|
---|
1755 | #else
|
---|
1756 | socklen_t cbAddr = *pcbAddr;
|
---|
1757 | #endif
|
---|
1758 | RTSOCKETNATIVE hNativeClient = accept(pThis->hNative, pAddr, &cbAddr);
|
---|
1759 | if (hNativeClient != NIL_RTSOCKETNATIVE)
|
---|
1760 | {
|
---|
1761 | *pcbAddr = cbAddr;
|
---|
1762 |
|
---|
1763 | /*
|
---|
1764 | * Wrap the client socket.
|
---|
1765 | */
|
---|
1766 | rc = rtSocketCreateForNative(phClient, hNativeClient);
|
---|
1767 | if (RT_FAILURE(rc))
|
---|
1768 | {
|
---|
1769 | #ifdef RT_OS_WINDOWS
|
---|
1770 | closesocket(hNativeClient);
|
---|
1771 | #else
|
---|
1772 | close(hNativeClient);
|
---|
1773 | #endif
|
---|
1774 | }
|
---|
1775 | }
|
---|
1776 | else
|
---|
1777 | rc = rtSocketError();
|
---|
1778 |
|
---|
1779 | rtSocketUnlock(pThis);
|
---|
1780 | return rc;
|
---|
1781 | }
|
---|
1782 |
|
---|
1783 |
|
---|
1784 | /**
|
---|
1785 | * Wrapper around connect.
|
---|
1786 | *
|
---|
1787 | * @returns IPRT status code.
|
---|
1788 | * @param hSocket The socket handle.
|
---|
1789 | * @param pAddr The socket address to connect to.
|
---|
1790 | * @param cMillies Number of milliseconds to wait for the connect attempt to complete.
|
---|
1791 | * Use RT_INDEFINITE_WAIT to wait for ever.
|
---|
1792 | * Use RT_TCPCLIENTCONNECT_DEFAULT_WAIT to wait for the default time
|
---|
1793 | * configured on the running system.
|
---|
1794 | */
|
---|
1795 | DECLHIDDEN(int) rtSocketConnect(RTSOCKET hSocket, PCRTNETADDR pAddr, RTMSINTERVAL cMillies)
|
---|
1796 | {
|
---|
1797 | /*
|
---|
1798 | * Validate input.
|
---|
1799 | */
|
---|
1800 | RTSOCKETINT *pThis = hSocket;
|
---|
1801 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1802 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1803 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1804 |
|
---|
1805 | RTSOCKADDRUNION u;
|
---|
1806 | int cbAddr;
|
---|
1807 | int rc = rtSocketAddrFromNetAddr(pAddr, &u, sizeof(u), &cbAddr);
|
---|
1808 | if (RT_SUCCESS(rc))
|
---|
1809 | {
|
---|
1810 | if (cMillies == RT_SOCKETCONNECT_DEFAULT_WAIT)
|
---|
1811 | {
|
---|
1812 | if (connect(pThis->hNative, &u.Addr, cbAddr) != 0)
|
---|
1813 | rc = rtSocketError();
|
---|
1814 | }
|
---|
1815 | else
|
---|
1816 | {
|
---|
1817 | /*
|
---|
1818 | * Switch the socket to nonblocking mode, initiate the connect
|
---|
1819 | * and wait for the socket to become writable or until the timeout
|
---|
1820 | * expires.
|
---|
1821 | */
|
---|
1822 | rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
1823 | if (RT_SUCCESS(rc))
|
---|
1824 | {
|
---|
1825 | if (connect(pThis->hNative, &u.Addr, cbAddr) != 0)
|
---|
1826 | {
|
---|
1827 | rc = rtSocketError();
|
---|
1828 | if (rc == VERR_TRY_AGAIN || rc == VERR_NET_IN_PROGRESS)
|
---|
1829 | {
|
---|
1830 | int rcSock = 0;
|
---|
1831 | fd_set FdSetWriteable;
|
---|
1832 | struct timeval TvTimeout;
|
---|
1833 |
|
---|
1834 | TvTimeout.tv_sec = cMillies / RT_MS_1SEC;
|
---|
1835 | TvTimeout.tv_usec = (cMillies % RT_MS_1SEC) * RT_US_1MS;
|
---|
1836 |
|
---|
1837 | FD_ZERO(&FdSetWriteable);
|
---|
1838 | FD_SET(pThis->hNative, &FdSetWriteable);
|
---|
1839 | do
|
---|
1840 | {
|
---|
1841 | rcSock = select(pThis->hNative + 1, NULL, &FdSetWriteable, NULL,
|
---|
1842 | cMillies == RT_INDEFINITE_WAIT || cMillies >= INT_MAX
|
---|
1843 | ? NULL
|
---|
1844 | : &TvTimeout);
|
---|
1845 | if (rcSock > 0)
|
---|
1846 | {
|
---|
1847 | int iSockError = 0;
|
---|
1848 | socklen_t cbSockOpt = sizeof(iSockError);
|
---|
1849 | rcSock = getsockopt(pThis->hNative, SOL_SOCKET, SO_ERROR, (char *)&iSockError, &cbSockOpt);
|
---|
1850 | if (rcSock == 0)
|
---|
1851 | {
|
---|
1852 | if (iSockError == 0)
|
---|
1853 | rc = VINF_SUCCESS;
|
---|
1854 | else
|
---|
1855 | {
|
---|
1856 | #ifdef RT_OS_WINDOWS
|
---|
1857 | rc = RTErrConvertFromWin32(iSockError);
|
---|
1858 | #else
|
---|
1859 | rc = RTErrConvertFromErrno(iSockError);
|
---|
1860 | #endif
|
---|
1861 | }
|
---|
1862 | }
|
---|
1863 | else
|
---|
1864 | rc = rtSocketError();
|
---|
1865 | }
|
---|
1866 | else if (rcSock == 0)
|
---|
1867 | rc = VERR_TIMEOUT;
|
---|
1868 | else
|
---|
1869 | rc = rtSocketError();
|
---|
1870 | } while (rc == VERR_INTERRUPTED);
|
---|
1871 | }
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
1875 | }
|
---|
1876 | }
|
---|
1877 | }
|
---|
1878 |
|
---|
1879 | rtSocketUnlock(pThis);
|
---|
1880 | return rc;
|
---|
1881 | }
|
---|
1882 |
|
---|
1883 |
|
---|
1884 | /**
|
---|
1885 | * Wrapper around connect, raw address, no timeout.
|
---|
1886 | *
|
---|
1887 | * @returns IPRT status code.
|
---|
1888 | * @param hSocket The socket handle.
|
---|
1889 | * @param pvAddr The raw socket address to connect to.
|
---|
1890 | * @param cbAddr The size of the raw address.
|
---|
1891 | */
|
---|
1892 | DECLHIDDEN(int) rtSocketConnectRaw(RTSOCKET hSocket, void const *pvAddr, size_t cbAddr)
|
---|
1893 | {
|
---|
1894 | /*
|
---|
1895 | * Validate input.
|
---|
1896 | */
|
---|
1897 | RTSOCKETINT *pThis = hSocket;
|
---|
1898 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1899 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1900 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1901 |
|
---|
1902 | int rc;
|
---|
1903 | if (connect(pThis->hNative, (const struct sockaddr *)pvAddr, (int)cbAddr) == 0)
|
---|
1904 | rc = VINF_SUCCESS;
|
---|
1905 | else
|
---|
1906 | rc = rtSocketError();
|
---|
1907 |
|
---|
1908 | rtSocketUnlock(pThis);
|
---|
1909 | return rc;
|
---|
1910 | }
|
---|
1911 |
|
---|
1912 |
|
---|
1913 | /**
|
---|
1914 | * Wrapper around setsockopt.
|
---|
1915 | *
|
---|
1916 | * @returns IPRT status code.
|
---|
1917 | * @param hSocket The socket handle.
|
---|
1918 | * @param iLevel The protocol level, e.g. IPPORTO_TCP.
|
---|
1919 | * @param iOption The option, e.g. TCP_NODELAY.
|
---|
1920 | * @param pvValue The value buffer.
|
---|
1921 | * @param cbValue The size of the value pointed to by pvValue.
|
---|
1922 | */
|
---|
1923 | DECLHIDDEN(int) rtSocketSetOpt(RTSOCKET hSocket, int iLevel, int iOption, void const *pvValue, int cbValue)
|
---|
1924 | {
|
---|
1925 | /*
|
---|
1926 | * Validate input.
|
---|
1927 | */
|
---|
1928 | RTSOCKETINT *pThis = hSocket;
|
---|
1929 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1930 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1931 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1932 |
|
---|
1933 | int rc = VINF_SUCCESS;
|
---|
1934 | if (setsockopt(pThis->hNative, iLevel, iOption, (const char *)pvValue, cbValue) != 0)
|
---|
1935 | rc = rtSocketError();
|
---|
1936 |
|
---|
1937 | rtSocketUnlock(pThis);
|
---|
1938 | return rc;
|
---|
1939 | }
|
---|
1940 |
|
---|
1941 |
|
---|
1942 | /**
|
---|
1943 | * Internal RTPollSetAdd helper that returns the handle that should be added to
|
---|
1944 | * the pollset.
|
---|
1945 | *
|
---|
1946 | * @returns Valid handle on success, INVALID_HANDLE_VALUE on failure.
|
---|
1947 | * @param hSocket The socket handle.
|
---|
1948 | * @param fEvents The events we're polling for.
|
---|
1949 | * @param phNative Where to put the primary handle.
|
---|
1950 | */
|
---|
1951 | DECLHIDDEN(int) rtSocketPollGetHandle(RTSOCKET hSocket, uint32_t fEvents, PRTHCINTPTR phNative)
|
---|
1952 | {
|
---|
1953 | RTSOCKETINT *pThis = hSocket;
|
---|
1954 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1955 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1956 | #ifdef RT_OS_WINDOWS
|
---|
1957 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1958 |
|
---|
1959 | int rc = VINF_SUCCESS;
|
---|
1960 | if (pThis->hEvent != WSA_INVALID_EVENT)
|
---|
1961 | *phNative = (RTHCINTPTR)pThis->hEvent;
|
---|
1962 | else
|
---|
1963 | {
|
---|
1964 | pThis->hEvent = WSACreateEvent();
|
---|
1965 | *phNative = (RTHCINTPTR)pThis->hEvent;
|
---|
1966 | if (pThis->hEvent == WSA_INVALID_EVENT)
|
---|
1967 | rc = rtSocketError();
|
---|
1968 | }
|
---|
1969 |
|
---|
1970 | rtSocketUnlock(pThis);
|
---|
1971 | return rc;
|
---|
1972 |
|
---|
1973 | #else /* !RT_OS_WINDOWS */
|
---|
1974 | *phNative = (RTHCUINTPTR)pThis->hNative;
|
---|
1975 | return VINF_SUCCESS;
|
---|
1976 | #endif /* !RT_OS_WINDOWS */
|
---|
1977 | }
|
---|
1978 |
|
---|
1979 | #ifdef RT_OS_WINDOWS
|
---|
1980 |
|
---|
1981 | /**
|
---|
1982 | * Undos the harm done by WSAEventSelect.
|
---|
1983 | *
|
---|
1984 | * @returns IPRT status code.
|
---|
1985 | * @param pThis The socket handle.
|
---|
1986 | */
|
---|
1987 | static int rtSocketPollClearEventAndRestoreBlocking(RTSOCKETINT *pThis)
|
---|
1988 | {
|
---|
1989 | int rc = VINF_SUCCESS;
|
---|
1990 | if (pThis->fSubscribedEvts)
|
---|
1991 | {
|
---|
1992 | if (WSAEventSelect(pThis->hNative, WSA_INVALID_EVENT, 0) == 0)
|
---|
1993 | {
|
---|
1994 | pThis->fSubscribedEvts = 0;
|
---|
1995 |
|
---|
1996 | /*
|
---|
1997 | * Switch back to blocking mode if that was the state before the
|
---|
1998 | * operation.
|
---|
1999 | */
|
---|
2000 | if (pThis->fBlocking)
|
---|
2001 | {
|
---|
2002 | u_long fNonBlocking = 0;
|
---|
2003 | int rc2 = ioctlsocket(pThis->hNative, FIONBIO, &fNonBlocking);
|
---|
2004 | if (rc2 != 0)
|
---|
2005 | {
|
---|
2006 | rc = rtSocketError();
|
---|
2007 | AssertMsgFailed(("%Rrc; rc2=%d\n", rc, rc2));
|
---|
2008 | }
|
---|
2009 | }
|
---|
2010 | }
|
---|
2011 | else
|
---|
2012 | {
|
---|
2013 | rc = rtSocketError();
|
---|
2014 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
2015 | }
|
---|
2016 | }
|
---|
2017 | return rc;
|
---|
2018 | }
|
---|
2019 |
|
---|
2020 |
|
---|
2021 | /**
|
---|
2022 | * Updates the mask of events we're subscribing to.
|
---|
2023 | *
|
---|
2024 | * @returns IPRT status code.
|
---|
2025 | * @param pThis The socket handle.
|
---|
2026 | * @param fEvents The events we want to subscribe to.
|
---|
2027 | */
|
---|
2028 | static int rtSocketPollUpdateEvents(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
2029 | {
|
---|
2030 | LONG fNetworkEvents = 0;
|
---|
2031 | if (fEvents & RTPOLL_EVT_READ)
|
---|
2032 | fNetworkEvents |= FD_READ;
|
---|
2033 | if (fEvents & RTPOLL_EVT_WRITE)
|
---|
2034 | fNetworkEvents |= FD_WRITE;
|
---|
2035 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
2036 | fNetworkEvents |= FD_CLOSE;
|
---|
2037 | LogFlowFunc(("fNetworkEvents=%#x\n", fNetworkEvents));
|
---|
2038 | if (WSAEventSelect(pThis->hNative, pThis->hEvent, fNetworkEvents) == 0)
|
---|
2039 | {
|
---|
2040 | pThis->fSubscribedEvts = fEvents;
|
---|
2041 | return VINF_SUCCESS;
|
---|
2042 | }
|
---|
2043 |
|
---|
2044 | int rc = rtSocketError();
|
---|
2045 | AssertMsgFailed(("fNetworkEvents=%#x rc=%Rrc\n", fNetworkEvents, rtSocketError()));
|
---|
2046 | return rc;
|
---|
2047 | }
|
---|
2048 |
|
---|
2049 | #endif /* RT_OS_WINDOWS */
|
---|
2050 |
|
---|
2051 |
|
---|
2052 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
2053 |
|
---|
2054 | /**
|
---|
2055 | * Checks for pending events.
|
---|
2056 | *
|
---|
2057 | * @returns Event mask or 0.
|
---|
2058 | * @param pThis The socket handle.
|
---|
2059 | * @param fEvents The desired events.
|
---|
2060 | */
|
---|
2061 | static uint32_t rtSocketPollCheck(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
2062 | {
|
---|
2063 | uint32_t fRetEvents = 0;
|
---|
2064 |
|
---|
2065 | LogFlowFunc(("pThis=%#p fEvents=%#x\n", pThis, fEvents));
|
---|
2066 |
|
---|
2067 | # ifdef RT_OS_WINDOWS
|
---|
2068 | /* Make sure WSAEnumNetworkEvents returns what we want. */
|
---|
2069 | int rc = VINF_SUCCESS;
|
---|
2070 | if ((pThis->fSubscribedEvts & fEvents) != fEvents)
|
---|
2071 | rc = rtSocketPollUpdateEvents(pThis, pThis->fSubscribedEvts | fEvents);
|
---|
2072 |
|
---|
2073 | /* Get the event mask, ASSUMES that WSAEnumNetworkEvents doesn't clear stuff. */
|
---|
2074 | WSANETWORKEVENTS NetEvts;
|
---|
2075 | RT_ZERO(NetEvts);
|
---|
2076 | if (WSAEnumNetworkEvents(pThis->hNative, pThis->hEvent, &NetEvts) == 0)
|
---|
2077 | {
|
---|
2078 | if ( (NetEvts.lNetworkEvents & FD_READ)
|
---|
2079 | && (fEvents & RTPOLL_EVT_READ)
|
---|
2080 | && NetEvts.iErrorCode[FD_READ_BIT] == 0)
|
---|
2081 | fRetEvents |= RTPOLL_EVT_READ;
|
---|
2082 |
|
---|
2083 | if ( (NetEvts.lNetworkEvents & FD_WRITE)
|
---|
2084 | && (fEvents & RTPOLL_EVT_WRITE)
|
---|
2085 | && NetEvts.iErrorCode[FD_WRITE_BIT] == 0)
|
---|
2086 | fRetEvents |= RTPOLL_EVT_WRITE;
|
---|
2087 |
|
---|
2088 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
2089 | {
|
---|
2090 | if (NetEvts.lNetworkEvents & FD_CLOSE)
|
---|
2091 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2092 | else
|
---|
2093 | for (uint32_t i = 0; i < FD_MAX_EVENTS; i++)
|
---|
2094 | if ( (NetEvts.lNetworkEvents & (1L << i))
|
---|
2095 | && NetEvts.iErrorCode[i] != 0)
|
---|
2096 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2097 | }
|
---|
2098 | }
|
---|
2099 | else
|
---|
2100 | rc = rtSocketError();
|
---|
2101 |
|
---|
2102 | /* Fall back on select if we hit an error above. */
|
---|
2103 | if (RT_FAILURE(rc))
|
---|
2104 | {
|
---|
2105 |
|
---|
2106 | }
|
---|
2107 |
|
---|
2108 | #else /* RT_OS_OS2 */
|
---|
2109 | int aFds[4] = { pThis->hNative, pThis->hNative, pThis->hNative, -1 };
|
---|
2110 | int rc = os2_select(aFds, 1, 1, 1, 0);
|
---|
2111 | if (rc > 0)
|
---|
2112 | {
|
---|
2113 | if (aFds[0] == pThis->hNative)
|
---|
2114 | fRetEvents |= RTPOLL_EVT_READ;
|
---|
2115 | if (aFds[1] == pThis->hNative)
|
---|
2116 | fRetEvents |= RTPOLL_EVT_WRITE;
|
---|
2117 | if (aFds[2] == pThis->hNative)
|
---|
2118 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
2119 | fRetEvents &= fEvents;
|
---|
2120 | }
|
---|
2121 | #endif /* RT_OS_OS2 */
|
---|
2122 |
|
---|
2123 | LogFlowFunc(("fRetEvents=%#x\n", fRetEvents));
|
---|
2124 | return fRetEvents;
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 |
|
---|
2128 | /**
|
---|
2129 | * Internal RTPoll helper that polls the socket handle and, if @a fNoWait is
|
---|
2130 | * clear, starts whatever actions we've got running during the poll call.
|
---|
2131 | *
|
---|
2132 | * @returns 0 if no pending events, actions initiated if @a fNoWait is clear.
|
---|
2133 | * Event mask (in @a fEvents) and no actions if the handle is ready
|
---|
2134 | * already.
|
---|
2135 | * UINT32_MAX (asserted) if the socket handle is busy in I/O or a
|
---|
2136 | * different poll set.
|
---|
2137 | *
|
---|
2138 | * @param hSocket The socket handle.
|
---|
2139 | * @param hPollSet The poll set handle (for access checks).
|
---|
2140 | * @param fEvents The events we're polling for.
|
---|
2141 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
2142 | * in this poll set. This can be used for dealing
|
---|
2143 | * with duplicate entries.
|
---|
2144 | * @param fNoWait Set if it's a zero-wait poll call. Clear if
|
---|
2145 | * we'll wait for an event to occur.
|
---|
2146 | *
|
---|
2147 | * @remarks There is a potential race wrt duplicate handles when @a fNoWait is
|
---|
2148 | * @c true, we don't currently care about that oddity...
|
---|
2149 | */
|
---|
2150 | DECLHIDDEN(uint32_t) rtSocketPollStart(RTSOCKET hSocket, RTPOLLSET hPollSet, uint32_t fEvents, bool fFinalEntry, bool fNoWait)
|
---|
2151 | {
|
---|
2152 | RTSOCKETINT *pThis = hSocket;
|
---|
2153 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
2154 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
2155 | /** @todo This isn't quite sane. Replace by critsect and open up concurrent
|
---|
2156 | * reads and writes! */
|
---|
2157 | if (rtSocketTryLock(pThis))
|
---|
2158 | pThis->hPollSet = hPollSet;
|
---|
2159 | else
|
---|
2160 | {
|
---|
2161 | AssertReturn(pThis->hPollSet == hPollSet, UINT32_MAX);
|
---|
2162 | ASMAtomicIncU32(&pThis->cUsers);
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | /* (rtSocketPollCheck will reset the event object). */
|
---|
2166 | # ifdef RT_OS_WINDOWS
|
---|
2167 | uint32_t fRetEvents = pThis->fEventsSaved;
|
---|
2168 | pThis->fEventsSaved = 0; /* Reset */
|
---|
2169 | fRetEvents |= rtSocketPollCheck(pThis, fEvents);
|
---|
2170 |
|
---|
2171 | if ( !fRetEvents
|
---|
2172 | && !fNoWait)
|
---|
2173 | {
|
---|
2174 | pThis->fPollEvts |= fEvents;
|
---|
2175 | if ( fFinalEntry
|
---|
2176 | && pThis->fSubscribedEvts != pThis->fPollEvts)
|
---|
2177 | {
|
---|
2178 | int rc = rtSocketPollUpdateEvents(pThis, pThis->fPollEvts);
|
---|
2179 | if (RT_FAILURE(rc))
|
---|
2180 | {
|
---|
2181 | pThis->fPollEvts = 0;
|
---|
2182 | fRetEvents = UINT32_MAX;
|
---|
2183 | }
|
---|
2184 | }
|
---|
2185 | }
|
---|
2186 | # else
|
---|
2187 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
2188 | # endif
|
---|
2189 |
|
---|
2190 | if (fRetEvents || fNoWait)
|
---|
2191 | {
|
---|
2192 | if (pThis->cUsers == 1)
|
---|
2193 | {
|
---|
2194 | # ifdef RT_OS_WINDOWS
|
---|
2195 | rtSocketPollClearEventAndRestoreBlocking(pThis);
|
---|
2196 | # endif
|
---|
2197 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
2198 | }
|
---|
2199 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
2200 | }
|
---|
2201 |
|
---|
2202 | return fRetEvents;
|
---|
2203 | }
|
---|
2204 |
|
---|
2205 |
|
---|
2206 | /**
|
---|
2207 | * Called after a WaitForMultipleObjects returned in order to check for pending
|
---|
2208 | * events and stop whatever actions that rtSocketPollStart() initiated.
|
---|
2209 | *
|
---|
2210 | * @returns Event mask or 0.
|
---|
2211 | *
|
---|
2212 | * @param hSocket The socket handle.
|
---|
2213 | * @param fEvents The events we're polling for.
|
---|
2214 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
2215 | * in this poll set. This can be used for dealing
|
---|
2216 | * with duplicate entries. Only keep in mind that
|
---|
2217 | * this method is called in reverse order, so the
|
---|
2218 | * first call will have this set (when the entire
|
---|
2219 | * set was processed).
|
---|
2220 | * @param fHarvestEvents Set if we should check for pending events.
|
---|
2221 | */
|
---|
2222 | DECLHIDDEN(uint32_t) rtSocketPollDone(RTSOCKET hSocket, uint32_t fEvents, bool fFinalEntry, bool fHarvestEvents)
|
---|
2223 | {
|
---|
2224 | RTSOCKETINT *pThis = hSocket;
|
---|
2225 | AssertPtrReturn(pThis, 0);
|
---|
2226 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, 0);
|
---|
2227 | Assert(pThis->cUsers > 0);
|
---|
2228 | Assert(pThis->hPollSet != NIL_RTPOLLSET);
|
---|
2229 |
|
---|
2230 | /* Harvest events and clear the event mask for the next round of polling. */
|
---|
2231 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
2232 | # ifdef RT_OS_WINDOWS
|
---|
2233 | pThis->fPollEvts = 0;
|
---|
2234 |
|
---|
2235 | /*
|
---|
2236 | * Save the write event if required.
|
---|
2237 | * It is only posted once and might get lost if the another source in the
|
---|
2238 | * pollset with a higher priority has pending events.
|
---|
2239 | */
|
---|
2240 | if ( !fHarvestEvents
|
---|
2241 | && fRetEvents)
|
---|
2242 | {
|
---|
2243 | pThis->fEventsSaved = fRetEvents;
|
---|
2244 | fRetEvents = 0;
|
---|
2245 | }
|
---|
2246 | # endif
|
---|
2247 |
|
---|
2248 | /* Make the socket blocking again and unlock the handle. */
|
---|
2249 | if (pThis->cUsers == 1)
|
---|
2250 | {
|
---|
2251 | # ifdef RT_OS_WINDOWS
|
---|
2252 | rtSocketPollClearEventAndRestoreBlocking(pThis);
|
---|
2253 | # endif
|
---|
2254 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
2255 | }
|
---|
2256 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
2257 | return fRetEvents;
|
---|
2258 | }
|
---|
2259 |
|
---|
2260 | #endif /* RT_OS_WINDOWS || RT_OS_OS2 */
|
---|
2261 |
|
---|