1 | /* $Id: socket.cpp 28535 2010-04-20 20:39:10Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Network Sockets.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #ifdef RT_OS_WINDOWS
|
---|
36 | # include <winsock2.h>
|
---|
37 | #else /* !RT_OS_WINDOWS */
|
---|
38 | # include <errno.h>
|
---|
39 | # include <sys/stat.h>
|
---|
40 | # include <sys/socket.h>
|
---|
41 | # include <netinet/in.h>
|
---|
42 | # include <netinet/tcp.h>
|
---|
43 | # include <arpa/inet.h>
|
---|
44 | # ifdef IPRT_WITH_TCPIP_V6
|
---|
45 | # include <netinet6/in6.h>
|
---|
46 | # endif
|
---|
47 | # include <sys/un.h>
|
---|
48 | # include <netdb.h>
|
---|
49 | # include <unistd.h>
|
---|
50 | # include <fcntl.h>
|
---|
51 | #endif /* !RT_OS_WINDOWS */
|
---|
52 | #include <limits.h>
|
---|
53 |
|
---|
54 | #include "internal/iprt.h"
|
---|
55 | #include <iprt/socket.h>
|
---|
56 |
|
---|
57 | #include <iprt/asm.h>
|
---|
58 | #include <iprt/assert.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 |
|
---|
66 | #include "internal/magics.h"
|
---|
67 | #include "internal/socket.h"
|
---|
68 |
|
---|
69 |
|
---|
70 | /*******************************************************************************
|
---|
71 | * Defined Constants And Macros *
|
---|
72 | *******************************************************************************/
|
---|
73 | /* non-standard linux stuff (it seems). */
|
---|
74 | #ifndef MSG_NOSIGNAL
|
---|
75 | # define MSG_NOSIGNAL 0
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | /* Windows has different names for SHUT_XXX. */
|
---|
79 | #ifndef SHUT_RDWR
|
---|
80 | # ifdef SD_BOTH
|
---|
81 | # define SHUT_RDWR SD_BOTH
|
---|
82 | # else
|
---|
83 | # define SHUT_RDWR 2
|
---|
84 | # endif
|
---|
85 | #endif
|
---|
86 | #ifndef SHUT_WR
|
---|
87 | # ifdef SD_SEND
|
---|
88 | # define SHUT_WR SD_SEND
|
---|
89 | # else
|
---|
90 | # define SHUT_WR 1
|
---|
91 | # endif
|
---|
92 | #endif
|
---|
93 | #ifndef SHUT_RD
|
---|
94 | # ifdef SD_RECEIVE
|
---|
95 | # define SHUT_RD SD_RECEIVE
|
---|
96 | # else
|
---|
97 | # define SHUT_RD 0
|
---|
98 | # endif
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | /* fixup backlevel OSes. */
|
---|
102 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
103 | # define socklen_t int
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | /** How many pending connection. */
|
---|
107 | #define RTTCP_SERVER_BACKLOG 10
|
---|
108 |
|
---|
109 |
|
---|
110 | /*******************************************************************************
|
---|
111 | * Structures and Typedefs *
|
---|
112 | *******************************************************************************/
|
---|
113 | /**
|
---|
114 | * Socket handle data.
|
---|
115 | *
|
---|
116 | * This is mainly required for implementing RTPollSet on Windows.
|
---|
117 | */
|
---|
118 | typedef struct RTSOCKETINT
|
---|
119 | {
|
---|
120 | /** Magic number (RTSOCKET_MAGIC). */
|
---|
121 | uint32_t u32Magic;
|
---|
122 | /** Exclusive user count.
|
---|
123 | * This is used to prevent two threads from accessing the handle concurrently.
|
---|
124 | * It can be higher than 1 if this handle is reference multiple times in a
|
---|
125 | * polling set (Windows). */
|
---|
126 | uint32_t volatile cUsers;
|
---|
127 | /** The native socket handle. */
|
---|
128 | RTSOCKETNATIVE hNative;
|
---|
129 | /** Indicates whether the handle has been closed or not. */
|
---|
130 | bool volatile fClosed;
|
---|
131 | #ifdef RT_OS_WINDOWS
|
---|
132 | /** The event semaphore we've associated with the socket handle.
|
---|
133 | * This is WSA_INVALID_EVENT if not done. */
|
---|
134 | WSAEVENT hEvent;
|
---|
135 | /** The pollset currently polling this socket. This is NIL if no one is
|
---|
136 | * polling. */
|
---|
137 | RTPOLLSET hPollSet;
|
---|
138 | /** The events we're polling for. */
|
---|
139 | uint32_t fPollEvts;
|
---|
140 | /** The events we're currently subscribing to with WSAEventSelect.
|
---|
141 | * This is ZERO if we're currently not subscribing to anything. */
|
---|
142 | uint32_t fSubscribedEvts;
|
---|
143 | #endif /* RT_OS_WINDOWS */
|
---|
144 | } RTSOCKETINT;
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Address union used internally for things like getpeername and getsockname.
|
---|
149 | */
|
---|
150 | typedef union RTSOCKADDRUNION
|
---|
151 | {
|
---|
152 | struct sockaddr Addr;
|
---|
153 | struct sockaddr_in Ipv4;
|
---|
154 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
155 | struct sockaddr_in6 Ipv6;
|
---|
156 | #endif
|
---|
157 | } RTSOCKADDRUNION;
|
---|
158 |
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Get the last error as an iprt status code.
|
---|
162 | *
|
---|
163 | * @returns IPRT status code.
|
---|
164 | */
|
---|
165 | DECLINLINE(int) rtSocketError(void)
|
---|
166 | {
|
---|
167 | #ifdef RT_OS_WINDOWS
|
---|
168 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
169 | #else
|
---|
170 | return RTErrConvertFromErrno(errno);
|
---|
171 | #endif
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Resets the last error.
|
---|
177 | */
|
---|
178 | DECLINLINE(void) rtSocketErrorReset(void)
|
---|
179 | {
|
---|
180 | #ifdef RT_OS_WINDOWS
|
---|
181 | WSASetLastError(0);
|
---|
182 | #else
|
---|
183 | errno = 0;
|
---|
184 | #endif
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Get the last resolver error as an iprt status code.
|
---|
190 | *
|
---|
191 | * @returns iprt status code.
|
---|
192 | */
|
---|
193 | int rtSocketResolverError(void)
|
---|
194 | {
|
---|
195 | #ifdef RT_OS_WINDOWS
|
---|
196 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
197 | #else
|
---|
198 | switch (h_errno)
|
---|
199 | {
|
---|
200 | case HOST_NOT_FOUND:
|
---|
201 | return VERR_NET_HOST_NOT_FOUND;
|
---|
202 | case NO_DATA:
|
---|
203 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
204 | case NO_RECOVERY:
|
---|
205 | return VERR_IO_GEN_FAILURE;
|
---|
206 | case TRY_AGAIN:
|
---|
207 | return VERR_TRY_AGAIN;
|
---|
208 |
|
---|
209 | default:
|
---|
210 | return VERR_UNRESOLVED_ERROR;
|
---|
211 | }
|
---|
212 | #endif
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Tries to lock the socket for exclusive usage by the calling thread.
|
---|
218 | *
|
---|
219 | * Call rtSocketUnlock() to unlock.
|
---|
220 | *
|
---|
221 | * @returns @c true if locked, @c false if not.
|
---|
222 | * @param pThis The socket structure.
|
---|
223 | */
|
---|
224 | DECLINLINE(bool) rtSocketTryLock(RTSOCKETINT *pThis)
|
---|
225 | {
|
---|
226 | return ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0);
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Unlocks the socket.
|
---|
232 | *
|
---|
233 | * @param pThis The socket structure.
|
---|
234 | */
|
---|
235 | DECLINLINE(void) rtSocketUnlock(RTSOCKETINT *pThis)
|
---|
236 | {
|
---|
237 | ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1);
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Creates an IPRT socket handle for a native one.
|
---|
243 | *
|
---|
244 | * @returns IPRT status code.
|
---|
245 | * @param ppSocket Where to return the IPRT socket handle.
|
---|
246 | * @param hNative The native handle.
|
---|
247 | */
|
---|
248 | int rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative)
|
---|
249 | {
|
---|
250 | RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemPoolAlloc(RTMEMPOOL_DEFAULT, sizeof(*pThis));
|
---|
251 | if (!pThis)
|
---|
252 | return VERR_NO_MEMORY;
|
---|
253 | pThis->u32Magic = RTSOCKET_MAGIC;
|
---|
254 | pThis->cUsers = 0;
|
---|
255 | pThis->hNative = hNative;
|
---|
256 | pThis->fClosed = false;
|
---|
257 | #ifdef RT_OS_WINDOWS
|
---|
258 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
259 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
260 | pThis->fPollEvts = 0;
|
---|
261 | pThis->fSubscribedEvts = 0;
|
---|
262 | #endif
|
---|
263 | *ppSocket = pThis;
|
---|
264 | return VINF_SUCCESS;
|
---|
265 | }
|
---|
266 |
|
---|
267 |
|
---|
268 | RTDECL(int) RTSocketFromNative(PRTSOCKET phSocket, RTHCINTPTR uNative)
|
---|
269 | {
|
---|
270 | AssertReturn(uNative != NIL_RTSOCKETNATIVE, VERR_INVALID_PARAMETER);
|
---|
271 | #ifndef RT_OS_WINDOWS
|
---|
272 | AssertReturn(uNative >= 0, VERR_INVALID_PARAMETER);
|
---|
273 | #endif
|
---|
274 | AssertPtrReturn(phSocket, VERR_INVALID_POINTER);
|
---|
275 | return rtSocketCreateForNative(phSocket, uNative);
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | /**
|
---|
280 | * Wrapper around socket().
|
---|
281 | *
|
---|
282 | * @returns IPRT status code.
|
---|
283 | * @param phSocket Where to store the handle to the socket on
|
---|
284 | * success.
|
---|
285 | * @param iDomain The protocol family (PF_XXX).
|
---|
286 | * @param iType The socket type (SOCK_XXX).
|
---|
287 | * @param iProtocol Socket parameter, usually 0.
|
---|
288 | */
|
---|
289 | int rtSocketCreate(PRTSOCKET phSocket, int iDomain, int iType, int iProtocol)
|
---|
290 | {
|
---|
291 | /*
|
---|
292 | * Create the socket.
|
---|
293 | */
|
---|
294 | RTSOCKETNATIVE hNative = socket(iDomain, iType, iProtocol);
|
---|
295 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
296 | return rtSocketError();
|
---|
297 |
|
---|
298 | /*
|
---|
299 | * Wrap it.
|
---|
300 | */
|
---|
301 | int rc = rtSocketCreateForNative(phSocket, hNative);
|
---|
302 | if (RT_FAILURE(rc))
|
---|
303 | {
|
---|
304 | #ifdef RT_OS_WINDOWS
|
---|
305 | closesocket(hNative);
|
---|
306 | #else
|
---|
307 | close(hNative);
|
---|
308 | #endif
|
---|
309 | }
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | RTDECL(uint32_t) RTSocketRetain(RTSOCKET hSocket)
|
---|
315 | {
|
---|
316 | RTSOCKETINT *pThis = hSocket;
|
---|
317 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
318 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
319 | return RTMemPoolRetain(pThis);
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Worker for RTSocketRelease and RTSocketClose.
|
---|
325 | *
|
---|
326 | * @returns IPRT status code.
|
---|
327 | * @param pThis The socket handle instance data.
|
---|
328 | * @param fDestroy Whether we're reaching ref count zero.
|
---|
329 | */
|
---|
330 | static int rtSocketCloseIt(RTSOCKETINT *pThis, bool fDestroy)
|
---|
331 | {
|
---|
332 | /*
|
---|
333 | * Invalidate the handle structure on destroy.
|
---|
334 | */
|
---|
335 | if (fDestroy)
|
---|
336 | {
|
---|
337 | Assert(ASMAtomicReadU32(&pThis->u32Magic) == RTSOCKET_MAGIC);
|
---|
338 | ASMAtomicWriteU32(&pThis->u32Magic, RTSOCKET_MAGIC_DEAD);
|
---|
339 | }
|
---|
340 |
|
---|
341 | int rc = VINF_SUCCESS;
|
---|
342 | if (ASMAtomicCmpXchgBool(&pThis->fClosed, true, false))
|
---|
343 | {
|
---|
344 | /*
|
---|
345 | * Close the native handle.
|
---|
346 | */
|
---|
347 | RTSOCKETNATIVE hNative = pThis->hNative;
|
---|
348 | if (hNative != NIL_RTSOCKETNATIVE)
|
---|
349 | {
|
---|
350 | pThis->hNative = NIL_RTSOCKETNATIVE;
|
---|
351 |
|
---|
352 | #ifdef RT_OS_WINDOWS
|
---|
353 | if (closesocket(hNative))
|
---|
354 | #else
|
---|
355 | if (close(hNative))
|
---|
356 | #endif
|
---|
357 | {
|
---|
358 | rc = rtSocketError();
|
---|
359 | #ifdef RT_OS_WINDOWS
|
---|
360 | AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", (uintptr_t)hNative, rc));
|
---|
361 | #else
|
---|
362 | AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", hNative, rc));
|
---|
363 | #endif
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | #ifdef RT_OS_WINDOWS
|
---|
368 | /*
|
---|
369 | * Close the event.
|
---|
370 | */
|
---|
371 | WSAEVENT hEvent = pThis->hEvent;
|
---|
372 | if (hEvent == WSA_INVALID_EVENT)
|
---|
373 | {
|
---|
374 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
375 | WSACloseEvent(hEvent);
|
---|
376 | }
|
---|
377 | #endif
|
---|
378 | }
|
---|
379 |
|
---|
380 | return rc;
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | RTDECL(uint32_t) RTSocketRelease(RTSOCKET hSocket)
|
---|
385 | {
|
---|
386 | RTSOCKETINT *pThis = hSocket;
|
---|
387 | if (pThis == NIL_RTSOCKET)
|
---|
388 | return 0;
|
---|
389 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
390 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
391 |
|
---|
392 | /* get the refcount without killing it... */
|
---|
393 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
394 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
395 | if (cRefs == 1)
|
---|
396 | rtSocketCloseIt(pThis, true);
|
---|
397 |
|
---|
398 | return RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | RTDECL(int) RTSocketClose(RTSOCKET hSocket)
|
---|
403 | {
|
---|
404 | RTSOCKETINT *pThis = hSocket;
|
---|
405 | if (pThis == NIL_RTSOCKET)
|
---|
406 | return VINF_SUCCESS;
|
---|
407 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
408 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
409 |
|
---|
410 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
411 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
412 |
|
---|
413 | int rc = rtSocketCloseIt(pThis, cRefs == 1);
|
---|
414 |
|
---|
415 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
416 | return rc;
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket)
|
---|
421 | {
|
---|
422 | RTSOCKETINT *pThis = hSocket;
|
---|
423 | AssertPtrReturn(pThis, RTHCUINTPTR_MAX);
|
---|
424 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, RTHCUINTPTR_MAX);
|
---|
425 | return (RTHCUINTPTR)pThis->hNative;
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable)
|
---|
430 | {
|
---|
431 | RTSOCKETINT *pThis = hSocket;
|
---|
432 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
433 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
434 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
435 |
|
---|
436 | int rc = VINF_SUCCESS;
|
---|
437 | #ifdef RT_OS_WINDOWS
|
---|
438 | if (!SetHandleInformation((HANDLE)pThis->hNative, HANDLE_FLAG_INHERIT, fInheritable ? HANDLE_FLAG_INHERIT : 0))
|
---|
439 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
440 | #else
|
---|
441 | if (fcntl(pThis->hNative, F_SETFD, fInheritable ? 0 : FD_CLOEXEC) < 0)
|
---|
442 | rc = RTErrConvertFromErrno(errno);
|
---|
443 | #endif
|
---|
444 |
|
---|
445 | return rc;
|
---|
446 | }
|
---|
447 |
|
---|
448 |
|
---|
449 | RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
450 | {
|
---|
451 | /*
|
---|
452 | * Validate input.
|
---|
453 | */
|
---|
454 | RTSOCKETINT *pThis = hSocket;
|
---|
455 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
456 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
457 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
458 | AssertPtr(pvBuffer);
|
---|
459 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Read loop.
|
---|
463 | * If pcbRead is NULL we have to fill the entire buffer!
|
---|
464 | */
|
---|
465 | int rc = VINF_SUCCESS;
|
---|
466 | size_t cbRead = 0;
|
---|
467 | size_t cbToRead = cbBuffer;
|
---|
468 | for (;;)
|
---|
469 | {
|
---|
470 | rtSocketErrorReset();
|
---|
471 | #ifdef RT_OS_WINDOWS
|
---|
472 | int cbNow = cbToRead >= INT_MAX/2 ? INT_MAX/2 : (int)cbToRead;
|
---|
473 | #else
|
---|
474 | size_t cbNow = cbToRead;
|
---|
475 | #endif
|
---|
476 | ssize_t cbBytesRead = recv(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL);
|
---|
477 | if (cbBytesRead <= 0)
|
---|
478 | {
|
---|
479 | rc = rtSocketError();
|
---|
480 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
481 | if (RT_SUCCESS_NP(rc))
|
---|
482 | {
|
---|
483 | if (!pcbRead)
|
---|
484 | rc = VERR_NET_SHUTDOWN;
|
---|
485 | else
|
---|
486 | {
|
---|
487 | *pcbRead = 0;
|
---|
488 | rc = VINF_SUCCESS;
|
---|
489 | }
|
---|
490 | }
|
---|
491 | break;
|
---|
492 | }
|
---|
493 | if (pcbRead)
|
---|
494 | {
|
---|
495 | /* return partial data */
|
---|
496 | *pcbRead = cbBytesRead;
|
---|
497 | break;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /* read more? */
|
---|
501 | cbRead += cbBytesRead;
|
---|
502 | if (cbRead == cbBuffer)
|
---|
503 | break;
|
---|
504 |
|
---|
505 | /* next */
|
---|
506 | cbToRead = cbBuffer - cbRead;
|
---|
507 | }
|
---|
508 |
|
---|
509 | rtSocketUnlock(pThis);
|
---|
510 | return rc;
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 | RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer)
|
---|
515 | {
|
---|
516 | /*
|
---|
517 | * Validate input.
|
---|
518 | */
|
---|
519 | RTSOCKETINT *pThis = hSocket;
|
---|
520 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
521 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
522 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
523 |
|
---|
524 | /*
|
---|
525 | * Try write all at once.
|
---|
526 | */
|
---|
527 | int rc = VINF_SUCCESS;
|
---|
528 | #ifdef RT_OS_WINDOWS
|
---|
529 | int cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
|
---|
530 | #else
|
---|
531 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
532 | #endif
|
---|
533 | ssize_t cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
534 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
535 | rc = VINF_SUCCESS;
|
---|
536 | else if (cbWritten < 0)
|
---|
537 | rc = rtSocketError();
|
---|
538 | else
|
---|
539 | {
|
---|
540 | /*
|
---|
541 | * Unfinished business, write the remainder of the request. Must ignore
|
---|
542 | * VERR_INTERRUPTED here if we've managed to send something.
|
---|
543 | */
|
---|
544 | size_t cbSentSoFar = 0;
|
---|
545 | for (;;)
|
---|
546 | {
|
---|
547 | /* advance */
|
---|
548 | cbBuffer -= (size_t)cbWritten;
|
---|
549 | if (!cbBuffer)
|
---|
550 | break;
|
---|
551 | cbSentSoFar += (size_t)cbWritten;
|
---|
552 | pvBuffer = (char const *)pvBuffer + cbWritten;
|
---|
553 |
|
---|
554 | /* send */
|
---|
555 | #ifdef RT_OS_WINDOWS
|
---|
556 | cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
|
---|
557 | #else
|
---|
558 | cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
559 | #endif
|
---|
560 | cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
561 | if (cbWritten >= 0)
|
---|
562 | AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtSocketError()=%d\n",
|
---|
563 | cbWritten, cbBuffer, rtSocketError()));
|
---|
564 | else
|
---|
565 | {
|
---|
566 | rc = rtSocketError();
|
---|
567 | if (rc != VERR_INTERNAL_ERROR || cbSentSoFar == 0)
|
---|
568 | break;
|
---|
569 | cbWritten = 0;
|
---|
570 | rc = VINF_SUCCESS;
|
---|
571 | }
|
---|
572 | }
|
---|
573 | }
|
---|
574 |
|
---|
575 | rtSocketUnlock(pThis);
|
---|
576 | return rc;
|
---|
577 | }
|
---|
578 |
|
---|
579 |
|
---|
580 | RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies)
|
---|
581 | {
|
---|
582 | /*
|
---|
583 | * Validate input.
|
---|
584 | */
|
---|
585 | RTSOCKETINT *pThis = hSocket;
|
---|
586 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
587 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
588 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Set up the file descriptor sets and do the select.
|
---|
592 | */
|
---|
593 | fd_set fdsetR;
|
---|
594 | FD_ZERO(&fdsetR);
|
---|
595 | FD_SET(pThis->hNative, &fdsetR);
|
---|
596 |
|
---|
597 | fd_set fdsetE = fdsetR;
|
---|
598 |
|
---|
599 | int rc;
|
---|
600 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
601 | rc = select(pThis->hNative + 1, &fdsetR, NULL, &fdsetE, NULL);
|
---|
602 | else
|
---|
603 | {
|
---|
604 | struct timeval timeout;
|
---|
605 | timeout.tv_sec = cMillies / 1000;
|
---|
606 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
607 | rc = select(pThis->hNative + 1, &fdsetR, NULL, &fdsetE, &timeout);
|
---|
608 | }
|
---|
609 | if (rc > 0)
|
---|
610 | rc = VINF_SUCCESS;
|
---|
611 | else if (rc == 0)
|
---|
612 | rc = VERR_TIMEOUT;
|
---|
613 | else
|
---|
614 | rc = rtSocketError();
|
---|
615 |
|
---|
616 | return rc;
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 | RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite)
|
---|
621 | {
|
---|
622 | /*
|
---|
623 | * Validate input, don't lock it because we might want to interrupt a call
|
---|
624 | * active on a different thread.
|
---|
625 | */
|
---|
626 | RTSOCKETINT *pThis = hSocket;
|
---|
627 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
628 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
629 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
630 | AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Do the job.
|
---|
634 | */
|
---|
635 | int rc = VINF_SUCCESS;
|
---|
636 | int fHow;
|
---|
637 | if (fRead && fWrite)
|
---|
638 | fHow = SHUT_RDWR;
|
---|
639 | else if (fRead)
|
---|
640 | fHow = SHUT_RD;
|
---|
641 | else
|
---|
642 | fHow = SHUT_WR;
|
---|
643 | if (shutdown(pThis->hNative, fHow) == -1)
|
---|
644 | rc = rtSocketError();
|
---|
645 |
|
---|
646 | return rc;
|
---|
647 | }
|
---|
648 |
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * Converts from a native socket address to a generic IPRT network address.
|
---|
652 | *
|
---|
653 | * @returns IPRT status code.
|
---|
654 | * @param pSrc The source address.
|
---|
655 | * @param cbSrc The size of the source address.
|
---|
656 | * @param pAddr Where to return the generic IPRT network
|
---|
657 | * address.
|
---|
658 | */
|
---|
659 | static int rtSocketConvertAddress(RTSOCKADDRUNION const *pSrc, size_t cbSrc, PRTNETADDR pAddr)
|
---|
660 | {
|
---|
661 | /*
|
---|
662 | * Convert the address.
|
---|
663 | */
|
---|
664 | if ( cbSrc == sizeof(struct sockaddr_in)
|
---|
665 | && pSrc->Addr.sa_family == AF_INET)
|
---|
666 | {
|
---|
667 | RT_ZERO(*pAddr);
|
---|
668 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
669 | pAddr->uPort = RT_N2H_U16(pSrc->Ipv4.sin_port);
|
---|
670 | pAddr->uAddr.IPv4.u = pSrc->Ipv4.sin_addr.s_addr;
|
---|
671 | }
|
---|
672 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
673 | else if ( cbSrc == sizeof(struct sockaddr_in6)
|
---|
674 | && pSrc->Addr.sa_family == AF_INET6)
|
---|
675 | {
|
---|
676 | RT_ZERO(*pAddr);
|
---|
677 | pAddr->enmType = RTNETADDRTYPE_IPV6;
|
---|
678 | pAddr->uPort = RT_N2H_U16(pSrc->Ipv6.sin6_port);
|
---|
679 | pAddr->uAddr.IPv6.au32[0] = pSrc->Ipv6.sin6_addr.s6_addr32[0];
|
---|
680 | pAddr->uAddr.IPv6.au32[1] = pSrc->Ipv6.sin6_addr.s6_addr32[1];
|
---|
681 | pAddr->uAddr.IPv6.au32[2] = pSrc->Ipv6.sin6_addr.s6_addr32[2];
|
---|
682 | pAddr->uAddr.IPv6.au32[3] = pSrc->Ipv6.sin6_addr.s6_addr32[3];
|
---|
683 | }
|
---|
684 | #endif
|
---|
685 | else
|
---|
686 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
687 | return VINF_SUCCESS;
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 | RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
692 | {
|
---|
693 | /*
|
---|
694 | * Validate input.
|
---|
695 | */
|
---|
696 | RTSOCKETINT *pThis = hSocket;
|
---|
697 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
698 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
699 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
700 |
|
---|
701 | /*
|
---|
702 | * Get the address and convert it.
|
---|
703 | */
|
---|
704 | int rc;
|
---|
705 | RTSOCKADDRUNION u;
|
---|
706 | #ifdef RT_OS_WINDOWS
|
---|
707 | int cbAddr = sizeof(u);
|
---|
708 | #else
|
---|
709 | socklen_t cbAddr = sizeof(u);
|
---|
710 | #endif
|
---|
711 | RT_ZERO(u);
|
---|
712 | if (getsockname(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
713 | rc = rtSocketConvertAddress(&u, cbAddr, pAddr);
|
---|
714 | else
|
---|
715 | rc = rtSocketError();
|
---|
716 |
|
---|
717 | return rc;
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
722 | {
|
---|
723 | /*
|
---|
724 | * Validate input.
|
---|
725 | */
|
---|
726 | RTSOCKETINT *pThis = hSocket;
|
---|
727 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
728 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
729 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
730 |
|
---|
731 | /*
|
---|
732 | * Get the address and convert it.
|
---|
733 | */
|
---|
734 | int rc;
|
---|
735 | RTSOCKADDRUNION u;
|
---|
736 | #ifdef RT_OS_WINDOWS
|
---|
737 | int cbAddr = sizeof(u);
|
---|
738 | #else
|
---|
739 | socklen_t cbAddr = sizeof(u);
|
---|
740 | #endif
|
---|
741 | RT_ZERO(u);
|
---|
742 | if (getpeername(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
743 | rc = rtSocketConvertAddress(&u, cbAddr, pAddr);
|
---|
744 | else
|
---|
745 | rc = rtSocketError();
|
---|
746 |
|
---|
747 | return rc;
|
---|
748 | }
|
---|
749 |
|
---|
750 |
|
---|
751 |
|
---|
752 | /**
|
---|
753 | * Wrapper around bind.
|
---|
754 | *
|
---|
755 | * @returns IPRT status code.
|
---|
756 | * @param hSocket The socket handle.
|
---|
757 | * @param pAddr The socket address to bind to.
|
---|
758 | * @param cbAddr The size of the address structure @a pAddr
|
---|
759 | * points to.
|
---|
760 | */
|
---|
761 | int rtSocketBind(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr)
|
---|
762 | {
|
---|
763 | /*
|
---|
764 | * Validate input.
|
---|
765 | */
|
---|
766 | RTSOCKETINT *pThis = hSocket;
|
---|
767 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
768 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
769 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
770 |
|
---|
771 | int rc = VINF_SUCCESS;
|
---|
772 | if (bind(pThis->hNative, pAddr, cbAddr) != 0)
|
---|
773 | rc = rtSocketError();
|
---|
774 |
|
---|
775 | rtSocketUnlock(pThis);
|
---|
776 | return rc;
|
---|
777 | }
|
---|
778 |
|
---|
779 |
|
---|
780 | /**
|
---|
781 | * Wrapper around listen.
|
---|
782 | *
|
---|
783 | * @returns IPRT status code.
|
---|
784 | * @param hSocket The socket handle.
|
---|
785 | * @param cMaxPending The max number of pending connections.
|
---|
786 | */
|
---|
787 | int rtSocketListen(RTSOCKET hSocket, int cMaxPending)
|
---|
788 | {
|
---|
789 | /*
|
---|
790 | * Validate input.
|
---|
791 | */
|
---|
792 | RTSOCKETINT *pThis = hSocket;
|
---|
793 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
794 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
795 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
796 |
|
---|
797 | int rc = VINF_SUCCESS;
|
---|
798 | if (listen(pThis->hNative, cMaxPending) != 0)
|
---|
799 | rc = rtSocketError();
|
---|
800 |
|
---|
801 | rtSocketUnlock(pThis);
|
---|
802 | return rc;
|
---|
803 | }
|
---|
804 |
|
---|
805 |
|
---|
806 | /**
|
---|
807 | * Wrapper around accept.
|
---|
808 | *
|
---|
809 | * @returns IPRT status code.
|
---|
810 | * @param hSocket The socket handle.
|
---|
811 | * @param phClient Where to return the client socket handle on
|
---|
812 | * success.
|
---|
813 | * @param pAddr Where to return the client address.
|
---|
814 | * @param pcbAddr On input this gives the size buffer size of what
|
---|
815 | * @a pAddr point to. On return this contains the
|
---|
816 | * size of what's stored at @a pAddr.
|
---|
817 | */
|
---|
818 | int rtSocketAccept(RTSOCKET hSocket, PRTSOCKET phClient, struct sockaddr *pAddr, size_t *pcbAddr)
|
---|
819 | {
|
---|
820 | /*
|
---|
821 | * Validate input.
|
---|
822 | * Only lock the socket temporarily while we get the native handle, so that
|
---|
823 | * we can safely shutdown and destroy the socket from a different thread.
|
---|
824 | */
|
---|
825 | RTSOCKETINT *pThis = hSocket;
|
---|
826 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
827 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
828 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
829 |
|
---|
830 | /*
|
---|
831 | * Call accept().
|
---|
832 | */
|
---|
833 | rtSocketErrorReset();
|
---|
834 | int rc = VINF_SUCCESS;
|
---|
835 | #ifdef RT_OS_WINDOWS
|
---|
836 | int cbAddr = (int)*pcbAddr;
|
---|
837 | #else
|
---|
838 | socklen_t cbAddr = *pcbAddr;
|
---|
839 | #endif
|
---|
840 | RTSOCKETNATIVE hNativeClient = accept(pThis->hNative, pAddr, &cbAddr);
|
---|
841 | if (hNativeClient != NIL_RTSOCKETNATIVE)
|
---|
842 | {
|
---|
843 | *pcbAddr = cbAddr;
|
---|
844 |
|
---|
845 | /*
|
---|
846 | * Wrap the client socket.
|
---|
847 | */
|
---|
848 | rc = rtSocketCreateForNative(phClient, hNativeClient);
|
---|
849 | if (RT_FAILURE(rc))
|
---|
850 | {
|
---|
851 | #ifdef RT_OS_WINDOWS
|
---|
852 | closesocket(hNativeClient);
|
---|
853 | #else
|
---|
854 | close(hNativeClient);
|
---|
855 | #endif
|
---|
856 | }
|
---|
857 | }
|
---|
858 | else
|
---|
859 | rc = rtSocketError();
|
---|
860 |
|
---|
861 | rtSocketUnlock(pThis);
|
---|
862 | return rc;
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 | /**
|
---|
867 | * Wrapper around connect.
|
---|
868 | *
|
---|
869 | * @returns IPRT status code.
|
---|
870 | * @param hSocket The socket handle.
|
---|
871 | * @param pAddr The socket address to connect to.
|
---|
872 | * @param cbAddr The size of the address structure @a pAddr
|
---|
873 | * points to.
|
---|
874 | */
|
---|
875 | int rtSocketConnect(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr)
|
---|
876 | {
|
---|
877 | /*
|
---|
878 | * Validate input.
|
---|
879 | */
|
---|
880 | RTSOCKETINT *pThis = hSocket;
|
---|
881 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
882 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
883 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
884 |
|
---|
885 | int rc = VINF_SUCCESS;
|
---|
886 | if (connect(pThis->hNative, pAddr, cbAddr) != 0)
|
---|
887 | rc = rtSocketError();
|
---|
888 |
|
---|
889 | rtSocketUnlock(pThis);
|
---|
890 | return rc;
|
---|
891 | }
|
---|
892 |
|
---|
893 |
|
---|
894 | /**
|
---|
895 | * Wrapper around setsockopt.
|
---|
896 | *
|
---|
897 | * @returns IPRT status code.
|
---|
898 | * @param hSocket The socket handle.
|
---|
899 | * @param iLevel The protocol level, e.g. IPPORTO_TCP.
|
---|
900 | * @param iOption The option, e.g. TCP_NODELAY.
|
---|
901 | * @param pvValue The value buffer.
|
---|
902 | * @param cbValue The size of the value pointed to by pvValue.
|
---|
903 | */
|
---|
904 | int rtSocketSetOpt(RTSOCKET hSocket, int iLevel, int iOption, void const *pvValue, int cbValue)
|
---|
905 | {
|
---|
906 | /*
|
---|
907 | * Validate input.
|
---|
908 | */
|
---|
909 | RTSOCKETINT *pThis = hSocket;
|
---|
910 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
911 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
912 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
913 |
|
---|
914 | int rc = VINF_SUCCESS;
|
---|
915 | if (setsockopt(pThis->hNative, iLevel, iOption, (const char *)pvValue, cbValue) != 0)
|
---|
916 | rc = rtSocketError();
|
---|
917 |
|
---|
918 | rtSocketUnlock(pThis);
|
---|
919 | return rc;
|
---|
920 | }
|
---|
921 |
|
---|
922 | #ifdef RT_OS_WINDOWS
|
---|
923 |
|
---|
924 | /**
|
---|
925 | * Internal RTPollSetAdd helper that returns the handle that should be added to
|
---|
926 | * the pollset.
|
---|
927 | *
|
---|
928 | * @returns Valid handle on success, INVALID_HANDLE_VALUE on failure.
|
---|
929 | * @param hSocket The socket handle.
|
---|
930 | * @param fEvents The events we're polling for.
|
---|
931 | * @param ph wher to put the primary handle.
|
---|
932 | */
|
---|
933 | int rtSocketPollGetHandle(RTSOCKET hSocket, uint32_t fEvents, PHANDLE ph)
|
---|
934 | {
|
---|
935 | RTSOCKETINT *pThis = hSocket;
|
---|
936 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
937 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
938 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
939 |
|
---|
940 | int rc = VINF_SUCCESS;
|
---|
941 | if (pThis->hEvent != WSA_INVALID_EVENT)
|
---|
942 | *ph = pThis->hEvent;
|
---|
943 | else
|
---|
944 | {
|
---|
945 | *ph = pThis->hEvent = WSACreateEvent();
|
---|
946 | if (pThis->hEvent == WSA_INVALID_EVENT)
|
---|
947 | rc = rtSocketError();
|
---|
948 | }
|
---|
949 |
|
---|
950 | rtSocketUnlock(pThis);
|
---|
951 | return rc;
|
---|
952 | }
|
---|
953 |
|
---|
954 |
|
---|
955 | /**
|
---|
956 | * Undos the harm done by WSAEventSelect.
|
---|
957 | *
|
---|
958 | * @returns IPRT status code.
|
---|
959 | * @param pThis The socket handle.
|
---|
960 | */
|
---|
961 | static int rtSocketPollClearEventAndMakeBlocking(RTSOCKETINT *pThis)
|
---|
962 | {
|
---|
963 | int rc = VINF_SUCCESS;
|
---|
964 | if (pThis->fSubscribedEvts)
|
---|
965 | {
|
---|
966 | if (WSAEventSelect(pThis->hNative, WSA_INVALID_EVENT, 0) == 0)
|
---|
967 | {
|
---|
968 | pThis->fSubscribedEvts = 0;
|
---|
969 |
|
---|
970 | u_long fNonBlocking = 0;
|
---|
971 | int rc2 = ioctlsocket(pThis->hNative, FIONBIO, &fNonBlocking);
|
---|
972 | if (rc2 != 0)
|
---|
973 | {
|
---|
974 | rc = rtSocketError();
|
---|
975 | AssertMsgFailed(("%Rrc; rc2=%d\n", rc, rc2));
|
---|
976 | }
|
---|
977 | }
|
---|
978 | else
|
---|
979 | {
|
---|
980 | rc = rtSocketError();
|
---|
981 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
982 | }
|
---|
983 | }
|
---|
984 | return rc;
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 | /**
|
---|
989 | * Updates the mask of events we're subscribing to.
|
---|
990 | *
|
---|
991 | * @returns IPRT status code.
|
---|
992 | * @param pThis The socket handle.
|
---|
993 | * @param fEvents The events we want to subscribe to.
|
---|
994 | */
|
---|
995 | static int rtSocketPollUpdateEvents(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
996 | {
|
---|
997 | LONG fNetworkEvents = 0;
|
---|
998 | if (fEvents & RTPOLL_EVT_READ)
|
---|
999 | fNetworkEvents |= FD_READ;
|
---|
1000 | if (fEvents & RTPOLL_EVT_WRITE)
|
---|
1001 | fNetworkEvents |= FD_WRITE;
|
---|
1002 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
1003 | fNetworkEvents |= FD_CLOSE;
|
---|
1004 | if (WSAEventSelect(pThis->hNative, pThis->hEvent, fNetworkEvents) == 0)
|
---|
1005 | {
|
---|
1006 | pThis->fSubscribedEvts = fEvents;
|
---|
1007 | return VINF_SUCCESS;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | int rc = rtSocketError();
|
---|
1011 | AssertMsgFailed(("fNetworkEvents=%#x rc=%Rrc\n", fNetworkEvents, rtSocketError()));
|
---|
1012 | return rc;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | /**
|
---|
1017 | * Checks for pending events.
|
---|
1018 | *
|
---|
1019 | * @returns Event mask or 0.
|
---|
1020 | * @param pThis The socket handle.
|
---|
1021 | * @param fEvents The desired events.
|
---|
1022 | */
|
---|
1023 | static uint32_t rtSocketPollCheck(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
1024 | {
|
---|
1025 | int rc = VINF_SUCCESS;
|
---|
1026 | uint32_t fRetEvents = 0;
|
---|
1027 |
|
---|
1028 | /* Make sure WSAEnumNetworkEvents returns what we want. */
|
---|
1029 | if ((pThis->fSubscribedEvts & fEvents) != fEvents)
|
---|
1030 | rc = rtSocketPollUpdateEvents(pThis, pThis->fSubscribedEvts | fEvents);
|
---|
1031 |
|
---|
1032 | /* Get the event mask, ASSUMES that WSAEnumNetworkEvents doesn't clear stuff. */
|
---|
1033 | WSANETWORKEVENTS NetEvts;
|
---|
1034 | RT_ZERO(NetEvts);
|
---|
1035 | if (WSAEnumNetworkEvents(pThis->hNative, pThis->hEvent, &NetEvts) == 0)
|
---|
1036 | {
|
---|
1037 | if ( (NetEvts.lNetworkEvents & FD_READ)
|
---|
1038 | && (fEvents & RTPOLL_EVT_READ)
|
---|
1039 | && NetEvts.iErrorCode[FD_READ_BIT] == 0)
|
---|
1040 | fRetEvents |= RTPOLL_EVT_READ;
|
---|
1041 |
|
---|
1042 | if ( (NetEvts.lNetworkEvents & FD_WRITE)
|
---|
1043 | && (fEvents & RTPOLL_EVT_WRITE)
|
---|
1044 | && NetEvts.iErrorCode[FD_WRITE_BIT] == 0)
|
---|
1045 | fRetEvents |= RTPOLL_EVT_WRITE;
|
---|
1046 |
|
---|
1047 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
1048 | {
|
---|
1049 | if (NetEvts.lNetworkEvents & FD_CLOSE)
|
---|
1050 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
1051 | else
|
---|
1052 | for (uint32_t i = 0; i < FD_MAX_EVENTS; i++)
|
---|
1053 | if ( (NetEvts.lNetworkEvents & (1L << i))
|
---|
1054 | && NetEvts.iErrorCode[i] != 0)
|
---|
1055 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
1056 | }
|
---|
1057 | }
|
---|
1058 | else
|
---|
1059 | rc = rtSocketError();
|
---|
1060 |
|
---|
1061 | /* Fall back on select if we hit an error above. */
|
---|
1062 | if (RT_FAILURE(rc))
|
---|
1063 | {
|
---|
1064 | /** @todo */
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | return fRetEvents;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Internal RTPoll helper that polls the socket handle and, if @a fNoWait is
|
---|
1073 | * clear, starts whatever actions we've got running during the poll call.
|
---|
1074 | *
|
---|
1075 | * @returns 0 if no pending events, actions initiated if @a fNoWait is clear.
|
---|
1076 | * Event mask (in @a fEvents) and no actions if the handle is ready
|
---|
1077 | * already.
|
---|
1078 | * UINT32_MAX (asserted) if the socket handle is busy in I/O or a
|
---|
1079 | * different poll set.
|
---|
1080 | *
|
---|
1081 | * @param hSocket The socket handle.
|
---|
1082 | * @param hPollSet The poll set handle (for access checks).
|
---|
1083 | * @param fEvents The events we're polling for.
|
---|
1084 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
1085 | * in this poll set. This can be used for dealing
|
---|
1086 | * with duplicate entries.
|
---|
1087 | * @param fNoWait Set if it's a zero-wait poll call. Clear if
|
---|
1088 | * we'll wait for an event to occur.
|
---|
1089 | *
|
---|
1090 | * @remarks There is a potential race wrt duplicate handles when @a fNoWait is
|
---|
1091 | * @c true, we don't currently care about that oddity...
|
---|
1092 | */
|
---|
1093 | uint32_t rtSocketPollStart(RTSOCKET hSocket, RTPOLLSET hPollSet, uint32_t fEvents, bool fFinalEntry, bool fNoWait)
|
---|
1094 | {
|
---|
1095 | RTSOCKETINT *pThis = hSocket;
|
---|
1096 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
1097 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
1098 | if (rtSocketTryLock(pThis))
|
---|
1099 | pThis->hPollSet = hPollSet;
|
---|
1100 | else
|
---|
1101 | {
|
---|
1102 | AssertReturn(pThis->hPollSet == hPollSet, UINT32_MAX);
|
---|
1103 | ASMAtomicIncU32(&pThis->cUsers);
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | /* (rtSocketPollCheck will reset the event object). */
|
---|
1107 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
1108 | if ( !fRetEvents
|
---|
1109 | && !fNoWait)
|
---|
1110 | {
|
---|
1111 | pThis->fPollEvts |= fEvents;
|
---|
1112 | if ( fFinalEntry
|
---|
1113 | && pThis->fSubscribedEvts != pThis->fPollEvts)
|
---|
1114 | {
|
---|
1115 | int rc = rtSocketPollUpdateEvents(pThis, pThis->fPollEvts);
|
---|
1116 | if (RT_FAILURE(rc))
|
---|
1117 | {
|
---|
1118 | pThis->fPollEvts = 0;
|
---|
1119 | fRetEvents = UINT32_MAX;
|
---|
1120 | }
|
---|
1121 | }
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | if (fRetEvents || fNoWait)
|
---|
1125 | {
|
---|
1126 | if (pThis->cUsers == 1)
|
---|
1127 | {
|
---|
1128 | rtSocketPollClearEventAndMakeBlocking(pThis);
|
---|
1129 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
1130 | }
|
---|
1131 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | return fRetEvents;
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 |
|
---|
1138 | /**
|
---|
1139 | * Called after a WaitForMultipleObjects returned in order to check for pending
|
---|
1140 | * events and stop whatever actions that rtSocketPollStart() initiated.
|
---|
1141 | *
|
---|
1142 | * @returns Event mask or 0.
|
---|
1143 | *
|
---|
1144 | * @param hSocket The socket handle.
|
---|
1145 | * @param fEvents The events we're polling for.
|
---|
1146 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
1147 | * in this poll set. This can be used for dealing
|
---|
1148 | * with duplicate entries. Only keep in mind that
|
---|
1149 | * this method is called in reverse order, so the
|
---|
1150 | * first call will have this set (when the entire
|
---|
1151 | * set was processed).
|
---|
1152 | */
|
---|
1153 | uint32_t rtSocketPollDone(RTSOCKET hSocket, uint32_t fEvents, bool fFinalEntry)
|
---|
1154 | {
|
---|
1155 | RTSOCKETINT *pThis = hSocket;
|
---|
1156 | AssertPtrReturn(pThis, 0);
|
---|
1157 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, 0);
|
---|
1158 | Assert(pThis->cUsers > 0);
|
---|
1159 | Assert(pThis->hPollSet != NIL_RTPOLLSET);
|
---|
1160 |
|
---|
1161 | /* Harvest events and clear the event mask for the next round of polling. */
|
---|
1162 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
1163 | pThis->fPollEvts = 0;
|
---|
1164 |
|
---|
1165 | /* Make the socket blocking again and unlock the handle. */
|
---|
1166 | if (pThis->cUsers == 1)
|
---|
1167 | {
|
---|
1168 | rtSocketPollClearEventAndMakeBlocking(pThis);
|
---|
1169 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
1170 | }
|
---|
1171 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
1172 | return fRetEvents;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | #endif /* RT_OS_WINDOWS */
|
---|