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