1 | /* $Id: socket.cpp 31189 2010-07-29 07:23:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Network Sockets.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #ifdef RT_OS_WINDOWS
|
---|
32 | # include <winsock2.h>
|
---|
33 | #else /* !RT_OS_WINDOWS */
|
---|
34 | # include <errno.h>
|
---|
35 | # include <sys/stat.h>
|
---|
36 | # include <sys/socket.h>
|
---|
37 | # include <netinet/in.h>
|
---|
38 | # include <netinet/tcp.h>
|
---|
39 | # include <arpa/inet.h>
|
---|
40 | # ifdef IPRT_WITH_TCPIP_V6
|
---|
41 | # include <netinet6/in6.h>
|
---|
42 | # endif
|
---|
43 | # include <sys/un.h>
|
---|
44 | # include <netdb.h>
|
---|
45 | # include <unistd.h>
|
---|
46 | # include <fcntl.h>
|
---|
47 | # include <sys/uio.h>
|
---|
48 | #endif /* !RT_OS_WINDOWS */
|
---|
49 | #include <limits.h>
|
---|
50 |
|
---|
51 | #include "internal/iprt.h"
|
---|
52 | #include <iprt/socket.h>
|
---|
53 |
|
---|
54 | #include <iprt/alloca.h>
|
---|
55 | #include <iprt/asm.h>
|
---|
56 | #include <iprt/assert.h>
|
---|
57 | #include <iprt/err.h>
|
---|
58 | #include <iprt/mempool.h>
|
---|
59 | #include <iprt/poll.h>
|
---|
60 | #include <iprt/string.h>
|
---|
61 | #include <iprt/thread.h>
|
---|
62 | #include <iprt/time.h>
|
---|
63 | #include <iprt/mem.h>
|
---|
64 | #include <iprt/sg.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 | /** Indicates whether the socket is operating in blocking or non-blocking mode
|
---|
132 | * currently. */
|
---|
133 | bool fBlocking;
|
---|
134 | #ifdef RT_OS_WINDOWS
|
---|
135 | /** The event semaphore we've associated with the socket handle.
|
---|
136 | * This is WSA_INVALID_EVENT if not done. */
|
---|
137 | WSAEVENT hEvent;
|
---|
138 | /** The pollset currently polling this socket. This is NIL if no one is
|
---|
139 | * polling. */
|
---|
140 | RTPOLLSET hPollSet;
|
---|
141 | /** The events we're polling for. */
|
---|
142 | uint32_t fPollEvts;
|
---|
143 | /** The events we're currently subscribing to with WSAEventSelect.
|
---|
144 | * This is ZERO if we're currently not subscribing to anything. */
|
---|
145 | uint32_t fSubscribedEvts;
|
---|
146 | #endif /* RT_OS_WINDOWS */
|
---|
147 | } RTSOCKETINT;
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Address union used internally for things like getpeername and getsockname.
|
---|
152 | */
|
---|
153 | typedef union RTSOCKADDRUNION
|
---|
154 | {
|
---|
155 | struct sockaddr Addr;
|
---|
156 | struct sockaddr_in Ipv4;
|
---|
157 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
158 | struct sockaddr_in6 Ipv6;
|
---|
159 | #endif
|
---|
160 | } RTSOCKADDRUNION;
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Get the last error as an iprt status code.
|
---|
165 | *
|
---|
166 | * @returns IPRT status code.
|
---|
167 | */
|
---|
168 | DECLINLINE(int) rtSocketError(void)
|
---|
169 | {
|
---|
170 | #ifdef RT_OS_WINDOWS
|
---|
171 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
172 | #else
|
---|
173 | return RTErrConvertFromErrno(errno);
|
---|
174 | #endif
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Resets the last error.
|
---|
180 | */
|
---|
181 | DECLINLINE(void) rtSocketErrorReset(void)
|
---|
182 | {
|
---|
183 | #ifdef RT_OS_WINDOWS
|
---|
184 | WSASetLastError(0);
|
---|
185 | #else
|
---|
186 | errno = 0;
|
---|
187 | #endif
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Get the last resolver error as an iprt status code.
|
---|
193 | *
|
---|
194 | * @returns iprt status code.
|
---|
195 | */
|
---|
196 | int rtSocketResolverError(void)
|
---|
197 | {
|
---|
198 | #ifdef RT_OS_WINDOWS
|
---|
199 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
200 | #else
|
---|
201 | switch (h_errno)
|
---|
202 | {
|
---|
203 | case HOST_NOT_FOUND:
|
---|
204 | return VERR_NET_HOST_NOT_FOUND;
|
---|
205 | case NO_DATA:
|
---|
206 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
207 | case NO_RECOVERY:
|
---|
208 | return VERR_IO_GEN_FAILURE;
|
---|
209 | case TRY_AGAIN:
|
---|
210 | return VERR_TRY_AGAIN;
|
---|
211 |
|
---|
212 | default:
|
---|
213 | return VERR_UNRESOLVED_ERROR;
|
---|
214 | }
|
---|
215 | #endif
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Tries to lock the socket for exclusive usage by the calling thread.
|
---|
221 | *
|
---|
222 | * Call rtSocketUnlock() to unlock.
|
---|
223 | *
|
---|
224 | * @returns @c true if locked, @c false if not.
|
---|
225 | * @param pThis The socket structure.
|
---|
226 | */
|
---|
227 | DECLINLINE(bool) rtSocketTryLock(RTSOCKETINT *pThis)
|
---|
228 | {
|
---|
229 | return ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0);
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Unlocks the socket.
|
---|
235 | *
|
---|
236 | * @param pThis The socket structure.
|
---|
237 | */
|
---|
238 | DECLINLINE(void) rtSocketUnlock(RTSOCKETINT *pThis)
|
---|
239 | {
|
---|
240 | ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1);
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Switches the socket to the desired blocking mode if neccessary.
|
---|
246 | *
|
---|
247 | * The socket must be locked.
|
---|
248 | *
|
---|
249 | * @returns IPRT status code.
|
---|
250 | * @param pThis The socket structure.
|
---|
251 | * @param fBlocking The desired mode of operation.
|
---|
252 | */
|
---|
253 | DECLINLINE(int) rtSocketSwitchBlockingMode(RTSOCKETINT *pThis, bool fBlocking)
|
---|
254 | {
|
---|
255 | int rc = VINF_SUCCESS;
|
---|
256 |
|
---|
257 | if (pThis->fBlocking != fBlocking)
|
---|
258 | {
|
---|
259 | #ifdef RT_OS_WINDOWS
|
---|
260 | u_long uBlocking = fBlocking ? 0 : 1;
|
---|
261 | if (ioctlsocket(pThis->hNative, FIONBIO, &uBlocking))
|
---|
262 | rc = rtSocketError();
|
---|
263 | #else
|
---|
264 | int fFlags = fcntl(pThis->hNative, F_GETFL, 0);
|
---|
265 | if (fFlags != -1)
|
---|
266 | {
|
---|
267 | if (fBlocking)
|
---|
268 | fFlags &= ~O_NONBLOCK;
|
---|
269 | else
|
---|
270 | fFlags |= O_NONBLOCK;
|
---|
271 |
|
---|
272 | if (fcntl(pThis->hNative, F_SETFL, fFlags) == -1)
|
---|
273 | rc = rtSocketError();
|
---|
274 | }
|
---|
275 | else
|
---|
276 | rc = rtSocketError();
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | if (RT_SUCCESS(rc))
|
---|
280 | pThis->fBlocking = fBlocking;
|
---|
281 | }
|
---|
282 |
|
---|
283 | return rc;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Creates an IPRT socket handle for a native one.
|
---|
288 | *
|
---|
289 | * @returns IPRT status code.
|
---|
290 | * @param ppSocket Where to return the IPRT socket handle.
|
---|
291 | * @param hNative The native handle.
|
---|
292 | */
|
---|
293 | int rtSocketCreateForNative(RTSOCKETINT **ppSocket, RTSOCKETNATIVE hNative)
|
---|
294 | {
|
---|
295 | RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemPoolAlloc(RTMEMPOOL_DEFAULT, sizeof(*pThis));
|
---|
296 | if (!pThis)
|
---|
297 | return VERR_NO_MEMORY;
|
---|
298 | pThis->u32Magic = RTSOCKET_MAGIC;
|
---|
299 | pThis->cUsers = 0;
|
---|
300 | pThis->hNative = hNative;
|
---|
301 | pThis->fClosed = false;
|
---|
302 | pThis->fBlocking = true;
|
---|
303 | #ifdef RT_OS_WINDOWS
|
---|
304 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
305 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
306 | pThis->fPollEvts = 0;
|
---|
307 | pThis->fSubscribedEvts = 0;
|
---|
308 | #endif
|
---|
309 | *ppSocket = pThis;
|
---|
310 | return VINF_SUCCESS;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | RTDECL(int) RTSocketFromNative(PRTSOCKET phSocket, RTHCINTPTR uNative)
|
---|
315 | {
|
---|
316 | AssertReturn(uNative != NIL_RTSOCKETNATIVE, VERR_INVALID_PARAMETER);
|
---|
317 | #ifndef RT_OS_WINDOWS
|
---|
318 | AssertReturn(uNative >= 0, VERR_INVALID_PARAMETER);
|
---|
319 | #endif
|
---|
320 | AssertPtrReturn(phSocket, VERR_INVALID_POINTER);
|
---|
321 | return rtSocketCreateForNative(phSocket, uNative);
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Wrapper around socket().
|
---|
327 | *
|
---|
328 | * @returns IPRT status code.
|
---|
329 | * @param phSocket Where to store the handle to the socket on
|
---|
330 | * success.
|
---|
331 | * @param iDomain The protocol family (PF_XXX).
|
---|
332 | * @param iType The socket type (SOCK_XXX).
|
---|
333 | * @param iProtocol Socket parameter, usually 0.
|
---|
334 | */
|
---|
335 | int rtSocketCreate(PRTSOCKET phSocket, int iDomain, int iType, int iProtocol)
|
---|
336 | {
|
---|
337 | /*
|
---|
338 | * Create the socket.
|
---|
339 | */
|
---|
340 | RTSOCKETNATIVE hNative = socket(iDomain, iType, iProtocol);
|
---|
341 | if (hNative == NIL_RTSOCKETNATIVE)
|
---|
342 | return rtSocketError();
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Wrap it.
|
---|
346 | */
|
---|
347 | int rc = rtSocketCreateForNative(phSocket, hNative);
|
---|
348 | if (RT_FAILURE(rc))
|
---|
349 | {
|
---|
350 | #ifdef RT_OS_WINDOWS
|
---|
351 | closesocket(hNative);
|
---|
352 | #else
|
---|
353 | close(hNative);
|
---|
354 | #endif
|
---|
355 | }
|
---|
356 | return rc;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | RTDECL(uint32_t) RTSocketRetain(RTSOCKET hSocket)
|
---|
361 | {
|
---|
362 | RTSOCKETINT *pThis = hSocket;
|
---|
363 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
364 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
365 | return RTMemPoolRetain(pThis);
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Worker for RTSocketRelease and RTSocketClose.
|
---|
371 | *
|
---|
372 | * @returns IPRT status code.
|
---|
373 | * @param pThis The socket handle instance data.
|
---|
374 | * @param fDestroy Whether we're reaching ref count zero.
|
---|
375 | */
|
---|
376 | static int rtSocketCloseIt(RTSOCKETINT *pThis, bool fDestroy)
|
---|
377 | {
|
---|
378 | /*
|
---|
379 | * Invalidate the handle structure on destroy.
|
---|
380 | */
|
---|
381 | if (fDestroy)
|
---|
382 | {
|
---|
383 | Assert(ASMAtomicReadU32(&pThis->u32Magic) == RTSOCKET_MAGIC);
|
---|
384 | ASMAtomicWriteU32(&pThis->u32Magic, RTSOCKET_MAGIC_DEAD);
|
---|
385 | }
|
---|
386 |
|
---|
387 | int rc = VINF_SUCCESS;
|
---|
388 | if (ASMAtomicCmpXchgBool(&pThis->fClosed, true, false))
|
---|
389 | {
|
---|
390 | /*
|
---|
391 | * Close the native handle.
|
---|
392 | */
|
---|
393 | RTSOCKETNATIVE hNative = pThis->hNative;
|
---|
394 | if (hNative != NIL_RTSOCKETNATIVE)
|
---|
395 | {
|
---|
396 | pThis->hNative = NIL_RTSOCKETNATIVE;
|
---|
397 |
|
---|
398 | #ifdef RT_OS_WINDOWS
|
---|
399 | if (closesocket(hNative))
|
---|
400 | #else
|
---|
401 | if (close(hNative))
|
---|
402 | #endif
|
---|
403 | {
|
---|
404 | rc = rtSocketError();
|
---|
405 | #ifdef RT_OS_WINDOWS
|
---|
406 | AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", (uintptr_t)hNative, rc));
|
---|
407 | #else
|
---|
408 | AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", hNative, rc));
|
---|
409 | #endif
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | #ifdef RT_OS_WINDOWS
|
---|
414 | /*
|
---|
415 | * Close the event.
|
---|
416 | */
|
---|
417 | WSAEVENT hEvent = pThis->hEvent;
|
---|
418 | if (hEvent == WSA_INVALID_EVENT)
|
---|
419 | {
|
---|
420 | pThis->hEvent = WSA_INVALID_EVENT;
|
---|
421 | WSACloseEvent(hEvent);
|
---|
422 | }
|
---|
423 | #endif
|
---|
424 | }
|
---|
425 |
|
---|
426 | return rc;
|
---|
427 | }
|
---|
428 |
|
---|
429 |
|
---|
430 | RTDECL(uint32_t) RTSocketRelease(RTSOCKET hSocket)
|
---|
431 | {
|
---|
432 | RTSOCKETINT *pThis = hSocket;
|
---|
433 | if (pThis == NIL_RTSOCKET)
|
---|
434 | return 0;
|
---|
435 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
436 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
437 |
|
---|
438 | /* get the refcount without killing it... */
|
---|
439 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
440 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
441 | if (cRefs == 1)
|
---|
442 | rtSocketCloseIt(pThis, true);
|
---|
443 |
|
---|
444 | return RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | RTDECL(int) RTSocketClose(RTSOCKET hSocket)
|
---|
449 | {
|
---|
450 | RTSOCKETINT *pThis = hSocket;
|
---|
451 | if (pThis == NIL_RTSOCKET)
|
---|
452 | return VINF_SUCCESS;
|
---|
453 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
454 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
455 |
|
---|
456 | uint32_t cRefs = RTMemPoolRefCount(pThis);
|
---|
457 | AssertReturn(cRefs != UINT32_MAX, UINT32_MAX);
|
---|
458 |
|
---|
459 | int rc = rtSocketCloseIt(pThis, cRefs == 1);
|
---|
460 |
|
---|
461 | RTMemPoolRelease(RTMEMPOOL_DEFAULT, pThis);
|
---|
462 | return rc;
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 | RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket)
|
---|
467 | {
|
---|
468 | RTSOCKETINT *pThis = hSocket;
|
---|
469 | AssertPtrReturn(pThis, RTHCUINTPTR_MAX);
|
---|
470 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, RTHCUINTPTR_MAX);
|
---|
471 | return (RTHCUINTPTR)pThis->hNative;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable)
|
---|
476 | {
|
---|
477 | RTSOCKETINT *pThis = hSocket;
|
---|
478 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
479 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
480 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
481 |
|
---|
482 | int rc = VINF_SUCCESS;
|
---|
483 | #ifdef RT_OS_WINDOWS
|
---|
484 | if (!SetHandleInformation((HANDLE)pThis->hNative, HANDLE_FLAG_INHERIT, fInheritable ? HANDLE_FLAG_INHERIT : 0))
|
---|
485 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
486 | #else
|
---|
487 | if (fcntl(pThis->hNative, F_SETFD, fInheritable ? 0 : FD_CLOEXEC) < 0)
|
---|
488 | rc = RTErrConvertFromErrno(errno);
|
---|
489 | #endif
|
---|
490 |
|
---|
491 | return rc;
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
496 | {
|
---|
497 | /*
|
---|
498 | * Validate input.
|
---|
499 | */
|
---|
500 | RTSOCKETINT *pThis = hSocket;
|
---|
501 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
502 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
503 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
504 | AssertPtr(pvBuffer);
|
---|
505 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
506 |
|
---|
507 |
|
---|
508 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
509 | if (RT_FAILURE(rc))
|
---|
510 | return rc;
|
---|
511 |
|
---|
512 | /*
|
---|
513 | * Read loop.
|
---|
514 | * If pcbRead is NULL we have to fill the entire buffer!
|
---|
515 | */
|
---|
516 | size_t cbRead = 0;
|
---|
517 | size_t cbToRead = cbBuffer;
|
---|
518 | for (;;)
|
---|
519 | {
|
---|
520 | rtSocketErrorReset();
|
---|
521 | #ifdef RT_OS_WINDOWS
|
---|
522 | int cbNow = cbToRead >= INT_MAX/2 ? INT_MAX/2 : (int)cbToRead;
|
---|
523 | #else
|
---|
524 | size_t cbNow = cbToRead;
|
---|
525 | #endif
|
---|
526 | ssize_t cbBytesRead = recv(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL);
|
---|
527 | if (cbBytesRead <= 0)
|
---|
528 | {
|
---|
529 | rc = rtSocketError();
|
---|
530 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
531 | if (RT_SUCCESS_NP(rc))
|
---|
532 | {
|
---|
533 | if (!pcbRead)
|
---|
534 | rc = VERR_NET_SHUTDOWN;
|
---|
535 | else
|
---|
536 | {
|
---|
537 | *pcbRead = 0;
|
---|
538 | rc = VINF_SUCCESS;
|
---|
539 | }
|
---|
540 | }
|
---|
541 | break;
|
---|
542 | }
|
---|
543 | if (pcbRead)
|
---|
544 | {
|
---|
545 | /* return partial data */
|
---|
546 | *pcbRead = cbBytesRead;
|
---|
547 | break;
|
---|
548 | }
|
---|
549 |
|
---|
550 | /* read more? */
|
---|
551 | cbRead += cbBytesRead;
|
---|
552 | if (cbRead == cbBuffer)
|
---|
553 | break;
|
---|
554 |
|
---|
555 | /* next */
|
---|
556 | cbToRead = cbBuffer - cbRead;
|
---|
557 | }
|
---|
558 |
|
---|
559 | rtSocketUnlock(pThis);
|
---|
560 | return rc;
|
---|
561 | }
|
---|
562 |
|
---|
563 |
|
---|
564 | RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer)
|
---|
565 | {
|
---|
566 | /*
|
---|
567 | * Validate input.
|
---|
568 | */
|
---|
569 | RTSOCKETINT *pThis = hSocket;
|
---|
570 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
571 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
572 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
573 |
|
---|
574 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
575 | if (RT_FAILURE(rc))
|
---|
576 | return rc;
|
---|
577 |
|
---|
578 | /*
|
---|
579 | * Try write all at once.
|
---|
580 | */
|
---|
581 | #ifdef RT_OS_WINDOWS
|
---|
582 | int cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
|
---|
583 | #else
|
---|
584 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
585 | #endif
|
---|
586 | ssize_t cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
587 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
588 | rc = VINF_SUCCESS;
|
---|
589 | else if (cbWritten < 0)
|
---|
590 | rc = rtSocketError();
|
---|
591 | else
|
---|
592 | {
|
---|
593 | /*
|
---|
594 | * Unfinished business, write the remainder of the request. Must ignore
|
---|
595 | * VERR_INTERRUPTED here if we've managed to send something.
|
---|
596 | */
|
---|
597 | size_t cbSentSoFar = 0;
|
---|
598 | for (;;)
|
---|
599 | {
|
---|
600 | /* advance */
|
---|
601 | cbBuffer -= (size_t)cbWritten;
|
---|
602 | if (!cbBuffer)
|
---|
603 | break;
|
---|
604 | cbSentSoFar += (size_t)cbWritten;
|
---|
605 | pvBuffer = (char const *)pvBuffer + cbWritten;
|
---|
606 |
|
---|
607 | /* send */
|
---|
608 | #ifdef RT_OS_WINDOWS
|
---|
609 | cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
|
---|
610 | #else
|
---|
611 | cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
612 | #endif
|
---|
613 | cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
614 | if (cbWritten >= 0)
|
---|
615 | AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtSocketError()=%d\n",
|
---|
616 | cbWritten, cbBuffer, rtSocketError()));
|
---|
617 | else
|
---|
618 | {
|
---|
619 | rc = rtSocketError();
|
---|
620 | if (rc != VERR_INTERNAL_ERROR || cbSentSoFar == 0)
|
---|
621 | break;
|
---|
622 | cbWritten = 0;
|
---|
623 | rc = VINF_SUCCESS;
|
---|
624 | }
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 | rtSocketUnlock(pThis);
|
---|
629 | return rc;
|
---|
630 | }
|
---|
631 |
|
---|
632 |
|
---|
633 | RTDECL(int) RTSocketSgWrite(RTSOCKET hSocket, PCRTSGBUF pSgBuf)
|
---|
634 | {
|
---|
635 | /*
|
---|
636 | * Validate input.
|
---|
637 | */
|
---|
638 | RTSOCKETINT *pThis = hSocket;
|
---|
639 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
640 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
641 | AssertPtrReturn(pSgBuf, VERR_INVALID_PARAMETER);
|
---|
642 | AssertReturn(pSgBuf->cSegs > 0, VERR_INVALID_PARAMETER);
|
---|
643 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
644 |
|
---|
645 | int rc = rtSocketSwitchBlockingMode(pThis, true /* fBlocking */);
|
---|
646 | if (RT_FAILURE(rc))
|
---|
647 | return rc;
|
---|
648 |
|
---|
649 | /*
|
---|
650 | * Construct message descriptor (translate pSgBuf) and send it.
|
---|
651 | */
|
---|
652 | rc = VERR_NO_TMP_MEMORY;
|
---|
653 | #ifdef RT_OS_WINDOWS
|
---|
654 | AssertCompileSize(WSABUF, sizeof(RTSGSEG));
|
---|
655 | AssertCompileMemberSize(WSABUF, buf, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
656 |
|
---|
657 | LPWSABUF paMsg = (LPWSABUF)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(WSABUF));
|
---|
658 | if (paMsg)
|
---|
659 | {
|
---|
660 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
661 | {
|
---|
662 | paMsg[i].buf = (char *)pSgBuf->paSegs[i].pvSeg;
|
---|
663 | paMsg[i].len = (u_long)pSgBuf->paSegs[i].cbSeg;
|
---|
664 | }
|
---|
665 |
|
---|
666 | DWORD dwSent;
|
---|
667 | int hrc = WSASend(pThis->hNative, paMsg, pSgBuf->cSegs, &dwSent,
|
---|
668 | MSG_NOSIGNAL, NULL, NULL);
|
---|
669 | if (!hrc)
|
---|
670 | rc = VINF_SUCCESS;
|
---|
671 | /** @todo check for incomplete writes */
|
---|
672 | else
|
---|
673 | rc = rtSocketError();
|
---|
674 |
|
---|
675 | RTMemTmpFree(paMsg);
|
---|
676 | }
|
---|
677 |
|
---|
678 | #else /* !RT_OS_WINDOWS */
|
---|
679 | AssertCompileSize(struct iovec, sizeof(RTSGSEG));
|
---|
680 | AssertCompileMemberSize(struct iovec, iov_base, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
681 | AssertCompileMemberSize(struct iovec, iov_len, RT_SIZEOFMEMB(RTSGSEG, cbSeg));
|
---|
682 |
|
---|
683 | struct iovec *paMsg = (struct iovec *)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(struct iovec));
|
---|
684 | if (paMsg)
|
---|
685 | {
|
---|
686 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
687 | {
|
---|
688 | paMsg[i].iov_base = pSgBuf->paSegs[i].pvSeg;
|
---|
689 | paMsg[i].iov_len = pSgBuf->paSegs[i].cbSeg;
|
---|
690 | }
|
---|
691 |
|
---|
692 | struct msghdr msgHdr;
|
---|
693 | RT_ZERO(msgHdr);
|
---|
694 | msgHdr.msg_iov = paMsg;
|
---|
695 | msgHdr.msg_iovlen = pSgBuf->cSegs;
|
---|
696 | ssize_t cbWritten = sendmsg(pThis->hNative, &msgHdr, MSG_NOSIGNAL);
|
---|
697 | if (RT_LIKELY(cbWritten >= 0))
|
---|
698 | rc = VINF_SUCCESS;
|
---|
699 | /** @todo check for incomplete writes */
|
---|
700 | else
|
---|
701 | rc = rtSocketError();
|
---|
702 |
|
---|
703 | RTMemTmpFree(paMsg);
|
---|
704 | }
|
---|
705 | #endif /* !RT_OS_WINDOWS */
|
---|
706 |
|
---|
707 | rtSocketUnlock(pThis);
|
---|
708 | return rc;
|
---|
709 | }
|
---|
710 |
|
---|
711 |
|
---|
712 | RTDECL(int) RTSocketSgWriteL(RTSOCKET hSocket, size_t cSegs, ...)
|
---|
713 | {
|
---|
714 | va_list va;
|
---|
715 | va_start(va, cSegs);
|
---|
716 | int rc = RTSocketSgWriteLV(hSocket, cSegs, va);
|
---|
717 | va_end(va);
|
---|
718 | return rc;
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | RTDECL(int) RTSocketSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va)
|
---|
723 | {
|
---|
724 | /*
|
---|
725 | * Set up a S/G segment array + buffer on the stack and pass it
|
---|
726 | * on to RTSocketSgWrite.
|
---|
727 | */
|
---|
728 | Assert(cSegs <= 16);
|
---|
729 | PRTSGSEG paSegs = (PRTSGSEG)alloca(cSegs * sizeof(RTSGSEG));
|
---|
730 | AssertReturn(paSegs, VERR_NO_TMP_MEMORY);
|
---|
731 | for (size_t i = 0; i < cSegs; i++)
|
---|
732 | {
|
---|
733 | paSegs[i].pvSeg = va_arg(va, void *);
|
---|
734 | paSegs[i].cbSeg = va_arg(va, size_t);
|
---|
735 | }
|
---|
736 |
|
---|
737 | RTSGBUF SgBuf;
|
---|
738 | RTSgBufInit(&SgBuf, paSegs, cSegs);
|
---|
739 | return RTSocketSgWrite(hSocket, &SgBuf);
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | RTDECL(int) RTSocketReadNB(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
744 | {
|
---|
745 | /*
|
---|
746 | * Validate input.
|
---|
747 | */
|
---|
748 | RTSOCKETINT *pThis = hSocket;
|
---|
749 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
750 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
751 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
752 | AssertPtr(pvBuffer);
|
---|
753 | AssertPtrReturn(pcbRead, VERR_INVALID_PARAMETER);
|
---|
754 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
755 |
|
---|
756 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
757 | if (RT_FAILURE(rc))
|
---|
758 | return rc;
|
---|
759 |
|
---|
760 | /*
|
---|
761 | * Read loop.
|
---|
762 | */
|
---|
763 | size_t cbRead = 0;
|
---|
764 | size_t cbToRead = cbBuffer;
|
---|
765 | for (;;)
|
---|
766 | {
|
---|
767 | rtSocketErrorReset();
|
---|
768 | #ifdef RT_OS_WINDOWS
|
---|
769 | int cbNow = cbToRead >= INT_MAX/2 ? INT_MAX/2 : (int)cbToRead;
|
---|
770 | #else
|
---|
771 | size_t cbNow = cbToRead;
|
---|
772 | #endif
|
---|
773 | ssize_t cbBytesRead = recv(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL);
|
---|
774 | if (cbBytesRead <= 0)
|
---|
775 | {
|
---|
776 | rc = rtSocketError();
|
---|
777 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
778 | if (RT_SUCCESS_NP(rc))
|
---|
779 | {
|
---|
780 | *pcbRead = 0;
|
---|
781 | rc = VINF_SUCCESS;
|
---|
782 | }
|
---|
783 | else
|
---|
784 | *pcbRead = cbRead;
|
---|
785 | break;
|
---|
786 | }
|
---|
787 |
|
---|
788 | /* read more? */
|
---|
789 | cbRead += cbBytesRead;
|
---|
790 | if (cbRead == cbBuffer)
|
---|
791 | {
|
---|
792 | *pcbRead = cbRead;
|
---|
793 | break;
|
---|
794 | }
|
---|
795 |
|
---|
796 | /* next */
|
---|
797 | cbToRead = cbBuffer - cbRead;
|
---|
798 | }
|
---|
799 |
|
---|
800 | rtSocketUnlock(pThis);
|
---|
801 | return rc;
|
---|
802 | }
|
---|
803 |
|
---|
804 |
|
---|
805 | RTDECL(int) RTSocketWriteNB(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten)
|
---|
806 | {
|
---|
807 | /*
|
---|
808 | * Validate input.
|
---|
809 | */
|
---|
810 | RTSOCKETINT *pThis = hSocket;
|
---|
811 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
812 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
813 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
814 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
815 |
|
---|
816 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
817 | if (RT_FAILURE(rc))
|
---|
818 | return rc;
|
---|
819 |
|
---|
820 | /*
|
---|
821 | * Write loop.
|
---|
822 | */
|
---|
823 | size_t cbWrite = 0;
|
---|
824 | size_t cbToWrite = cbBuffer;
|
---|
825 | for (;;)
|
---|
826 | {
|
---|
827 | rtSocketErrorReset();
|
---|
828 | #ifdef RT_OS_WINDOWS
|
---|
829 | int cbNow = RT_MIN((int)cbToWrite, INT_MAX/2);
|
---|
830 | #else
|
---|
831 | size_t cbNow = cbToWrite;
|
---|
832 | #endif
|
---|
833 | ssize_t cbBytesWritten = send(pThis->hNative, (char *)pvBuffer + cbWrite, cbNow, MSG_NOSIGNAL);
|
---|
834 | if (cbBytesWritten < 0)
|
---|
835 | {
|
---|
836 | rc = rtSocketError();
|
---|
837 | *pcbWritten = cbWrite;
|
---|
838 | break;
|
---|
839 | }
|
---|
840 |
|
---|
841 | /* write more? */
|
---|
842 | cbWrite += cbBytesWritten;
|
---|
843 | if (cbWrite == cbBuffer)
|
---|
844 | {
|
---|
845 | *pcbWritten = cbWrite;
|
---|
846 | break;
|
---|
847 | }
|
---|
848 |
|
---|
849 | /* next */
|
---|
850 | cbToWrite = cbBuffer - cbWrite;
|
---|
851 | }
|
---|
852 |
|
---|
853 | rtSocketUnlock(pThis);
|
---|
854 | return rc;
|
---|
855 | }
|
---|
856 |
|
---|
857 |
|
---|
858 | RTDECL(int) RTSocketSgWriteNB(RTSOCKET hSocket, PCRTSGBUF pSgBuf, size_t *pcbWritten)
|
---|
859 | {
|
---|
860 | /*
|
---|
861 | * Validate input.
|
---|
862 | */
|
---|
863 | RTSOCKETINT *pThis = hSocket;
|
---|
864 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
865 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
866 | AssertPtrReturn(pSgBuf, VERR_INVALID_PARAMETER);
|
---|
867 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
868 | AssertReturn(pSgBuf->cSegs > 0, VERR_INVALID_PARAMETER);
|
---|
869 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
870 |
|
---|
871 | int rc = rtSocketSwitchBlockingMode(pThis, false /* fBlocking */);
|
---|
872 | if (RT_FAILURE(rc))
|
---|
873 | return rc;
|
---|
874 |
|
---|
875 | /*
|
---|
876 | * Construct message descriptor (translate pSgBuf) and send it.
|
---|
877 | */
|
---|
878 | rc = VERR_NO_TMP_MEMORY;
|
---|
879 | #ifdef RT_OS_WINDOWS
|
---|
880 | AssertCompileSize(WSABUF, sizeof(RTSGSEG));
|
---|
881 | AssertCompileMemberSize(WSABUF, buf, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
882 |
|
---|
883 | LPWSABUF paMsg = (LPWSABUF)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(WSABUF));
|
---|
884 | if (paMsg)
|
---|
885 | {
|
---|
886 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
887 | {
|
---|
888 | paMsg[i].buf = (char *)pSgBuf->paSegs[i].pvSeg;
|
---|
889 | paMsg[i].len = (u_long)pSgBuf->paSegs[i].cbSeg;
|
---|
890 | }
|
---|
891 |
|
---|
892 | DWORD dwSent = 0;
|
---|
893 | int hrc = WSASend(pThis->hNative, paMsg, pSgBuf->cSegs, &dwSent,
|
---|
894 | MSG_NOSIGNAL, NULL, NULL);
|
---|
895 | if (!hrc)
|
---|
896 | rc = VINF_SUCCESS;
|
---|
897 | /** @todo check for incomplete writes */
|
---|
898 | else
|
---|
899 | rc = rtSocketError();
|
---|
900 |
|
---|
901 | *pcbWritten = dwSent;
|
---|
902 |
|
---|
903 | RTMemTmpFree(paMsg);
|
---|
904 | }
|
---|
905 |
|
---|
906 | #else /* !RT_OS_WINDOWS */
|
---|
907 | AssertCompileSize(struct iovec, sizeof(RTSGSEG));
|
---|
908 | AssertCompileMemberSize(struct iovec, iov_base, RT_SIZEOFMEMB(RTSGSEG, pvSeg));
|
---|
909 | AssertCompileMemberSize(struct iovec, iov_len, RT_SIZEOFMEMB(RTSGSEG, cbSeg));
|
---|
910 |
|
---|
911 | struct iovec *paMsg = (struct iovec *)RTMemTmpAllocZ(pSgBuf->cSegs * sizeof(struct iovec));
|
---|
912 | if (paMsg)
|
---|
913 | {
|
---|
914 | for (unsigned i = 0; i < pSgBuf->cSegs; i++)
|
---|
915 | {
|
---|
916 | paMsg[i].iov_base = pSgBuf->paSegs[i].pvSeg;
|
---|
917 | paMsg[i].iov_len = pSgBuf->paSegs[i].cbSeg;
|
---|
918 | }
|
---|
919 |
|
---|
920 | struct msghdr msgHdr;
|
---|
921 | RT_ZERO(msgHdr);
|
---|
922 | msgHdr.msg_iov = paMsg;
|
---|
923 | msgHdr.msg_iovlen = pSgBuf->cSegs;
|
---|
924 | ssize_t cbWritten = sendmsg(pThis->hNative, &msgHdr, MSG_NOSIGNAL);
|
---|
925 | if (RT_LIKELY(cbWritten >= 0))
|
---|
926 | rc = VINF_SUCCESS;
|
---|
927 | /** @todo check for incomplete writes */
|
---|
928 | else
|
---|
929 | rc = rtSocketError();
|
---|
930 |
|
---|
931 | *pcbWritten = cbWritten;
|
---|
932 |
|
---|
933 | RTMemTmpFree(paMsg);
|
---|
934 | }
|
---|
935 | #endif /* !RT_OS_WINDOWS */
|
---|
936 |
|
---|
937 | rtSocketUnlock(pThis);
|
---|
938 | return rc;
|
---|
939 | }
|
---|
940 |
|
---|
941 |
|
---|
942 | RTDECL(int) RTSocketSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...)
|
---|
943 | {
|
---|
944 | va_list va;
|
---|
945 | va_start(va, pcbWritten);
|
---|
946 | int rc = RTSocketSgWriteLVNB(hSocket, cSegs, pcbWritten, va);
|
---|
947 | va_end(va);
|
---|
948 | return rc;
|
---|
949 | }
|
---|
950 |
|
---|
951 |
|
---|
952 | RTDECL(int) RTSocketSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va)
|
---|
953 | {
|
---|
954 | /*
|
---|
955 | * Set up a S/G segment array + buffer on the stack and pass it
|
---|
956 | * on to RTSocketSgWrite.
|
---|
957 | */
|
---|
958 | Assert(cSegs <= 16);
|
---|
959 | PRTSGSEG paSegs = (PRTSGSEG)alloca(cSegs * sizeof(RTSGSEG));
|
---|
960 | AssertReturn(paSegs, VERR_NO_TMP_MEMORY);
|
---|
961 | for (size_t i = 0; i < cSegs; i++)
|
---|
962 | {
|
---|
963 | paSegs[i].pvSeg = va_arg(va, void *);
|
---|
964 | paSegs[i].cbSeg = va_arg(va, size_t);
|
---|
965 | }
|
---|
966 |
|
---|
967 | RTSGBUF SgBuf;
|
---|
968 | RTSgBufInit(&SgBuf, paSegs, cSegs);
|
---|
969 | return RTSocketSgWriteNB(hSocket, &SgBuf, pcbWritten);
|
---|
970 | }
|
---|
971 |
|
---|
972 |
|
---|
973 | RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies)
|
---|
974 | {
|
---|
975 | /*
|
---|
976 | * Validate input.
|
---|
977 | */
|
---|
978 | RTSOCKETINT *pThis = hSocket;
|
---|
979 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
980 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
981 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
982 |
|
---|
983 | /*
|
---|
984 | * Set up the file descriptor sets and do the select.
|
---|
985 | */
|
---|
986 | fd_set fdsetR;
|
---|
987 | FD_ZERO(&fdsetR);
|
---|
988 | FD_SET(pThis->hNative, &fdsetR);
|
---|
989 |
|
---|
990 | fd_set fdsetE = fdsetR;
|
---|
991 |
|
---|
992 | int rc;
|
---|
993 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
994 | rc = select(pThis->hNative + 1, &fdsetR, NULL, &fdsetE, NULL);
|
---|
995 | else
|
---|
996 | {
|
---|
997 | struct timeval timeout;
|
---|
998 | timeout.tv_sec = cMillies / 1000;
|
---|
999 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
1000 | rc = select(pThis->hNative + 1, &fdsetR, NULL, &fdsetE, &timeout);
|
---|
1001 | }
|
---|
1002 | if (rc > 0)
|
---|
1003 | rc = VINF_SUCCESS;
|
---|
1004 | else if (rc == 0)
|
---|
1005 | rc = VERR_TIMEOUT;
|
---|
1006 | else
|
---|
1007 | rc = rtSocketError();
|
---|
1008 |
|
---|
1009 | return rc;
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 |
|
---|
1013 | RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite)
|
---|
1014 | {
|
---|
1015 | /*
|
---|
1016 | * Validate input, don't lock it because we might want to interrupt a call
|
---|
1017 | * active on a different thread.
|
---|
1018 | */
|
---|
1019 | RTSOCKETINT *pThis = hSocket;
|
---|
1020 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1021 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1022 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1023 | AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
|
---|
1024 |
|
---|
1025 | /*
|
---|
1026 | * Do the job.
|
---|
1027 | */
|
---|
1028 | int rc = VINF_SUCCESS;
|
---|
1029 | int fHow;
|
---|
1030 | if (fRead && fWrite)
|
---|
1031 | fHow = SHUT_RDWR;
|
---|
1032 | else if (fRead)
|
---|
1033 | fHow = SHUT_RD;
|
---|
1034 | else
|
---|
1035 | fHow = SHUT_WR;
|
---|
1036 | if (shutdown(pThis->hNative, fHow) == -1)
|
---|
1037 | rc = rtSocketError();
|
---|
1038 |
|
---|
1039 | return rc;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 |
|
---|
1043 | /**
|
---|
1044 | * Converts from a native socket address to a generic IPRT network address.
|
---|
1045 | *
|
---|
1046 | * @returns IPRT status code.
|
---|
1047 | * @param pSrc The source address.
|
---|
1048 | * @param cbSrc The size of the source address.
|
---|
1049 | * @param pAddr Where to return the generic IPRT network
|
---|
1050 | * address.
|
---|
1051 | */
|
---|
1052 | static int rtSocketConvertAddress(RTSOCKADDRUNION const *pSrc, size_t cbSrc, PRTNETADDR pAddr)
|
---|
1053 | {
|
---|
1054 | /*
|
---|
1055 | * Convert the address.
|
---|
1056 | */
|
---|
1057 | if ( cbSrc == sizeof(struct sockaddr_in)
|
---|
1058 | && pSrc->Addr.sa_family == AF_INET)
|
---|
1059 | {
|
---|
1060 | RT_ZERO(*pAddr);
|
---|
1061 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
1062 | pAddr->uPort = RT_N2H_U16(pSrc->Ipv4.sin_port);
|
---|
1063 | pAddr->uAddr.IPv4.u = pSrc->Ipv4.sin_addr.s_addr;
|
---|
1064 | }
|
---|
1065 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
1066 | else if ( cbSrc == sizeof(struct sockaddr_in6)
|
---|
1067 | && pSrc->Addr.sa_family == AF_INET6)
|
---|
1068 | {
|
---|
1069 | RT_ZERO(*pAddr);
|
---|
1070 | pAddr->enmType = RTNETADDRTYPE_IPV6;
|
---|
1071 | pAddr->uPort = RT_N2H_U16(pSrc->Ipv6.sin6_port);
|
---|
1072 | pAddr->uAddr.IPv6.au32[0] = pSrc->Ipv6.sin6_addr.s6_addr32[0];
|
---|
1073 | pAddr->uAddr.IPv6.au32[1] = pSrc->Ipv6.sin6_addr.s6_addr32[1];
|
---|
1074 | pAddr->uAddr.IPv6.au32[2] = pSrc->Ipv6.sin6_addr.s6_addr32[2];
|
---|
1075 | pAddr->uAddr.IPv6.au32[3] = pSrc->Ipv6.sin6_addr.s6_addr32[3];
|
---|
1076 | }
|
---|
1077 | #endif
|
---|
1078 | else
|
---|
1079 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
1080 | return VINF_SUCCESS;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 |
|
---|
1084 | RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
1085 | {
|
---|
1086 | /*
|
---|
1087 | * Validate input.
|
---|
1088 | */
|
---|
1089 | RTSOCKETINT *pThis = hSocket;
|
---|
1090 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1091 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1092 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1093 |
|
---|
1094 | /*
|
---|
1095 | * Get the address and convert it.
|
---|
1096 | */
|
---|
1097 | int rc;
|
---|
1098 | RTSOCKADDRUNION u;
|
---|
1099 | #ifdef RT_OS_WINDOWS
|
---|
1100 | int cbAddr = sizeof(u);
|
---|
1101 | #else
|
---|
1102 | socklen_t cbAddr = sizeof(u);
|
---|
1103 | #endif
|
---|
1104 | RT_ZERO(u);
|
---|
1105 | if (getsockname(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
1106 | rc = rtSocketConvertAddress(&u, cbAddr, pAddr);
|
---|
1107 | else
|
---|
1108 | rc = rtSocketError();
|
---|
1109 |
|
---|
1110 | return rc;
|
---|
1111 | }
|
---|
1112 |
|
---|
1113 |
|
---|
1114 | RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
1115 | {
|
---|
1116 | /*
|
---|
1117 | * Validate input.
|
---|
1118 | */
|
---|
1119 | RTSOCKETINT *pThis = hSocket;
|
---|
1120 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1121 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1122 | AssertReturn(RTMemPoolRefCount(pThis) >= (pThis->cUsers ? 2U : 1U), VERR_CALLER_NO_REFERENCE);
|
---|
1123 |
|
---|
1124 | /*
|
---|
1125 | * Get the address and convert it.
|
---|
1126 | */
|
---|
1127 | int rc;
|
---|
1128 | RTSOCKADDRUNION u;
|
---|
1129 | #ifdef RT_OS_WINDOWS
|
---|
1130 | int cbAddr = sizeof(u);
|
---|
1131 | #else
|
---|
1132 | socklen_t cbAddr = sizeof(u);
|
---|
1133 | #endif
|
---|
1134 | RT_ZERO(u);
|
---|
1135 | if (getpeername(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
1136 | rc = rtSocketConvertAddress(&u, cbAddr, pAddr);
|
---|
1137 | else
|
---|
1138 | rc = rtSocketError();
|
---|
1139 |
|
---|
1140 | return rc;
|
---|
1141 | }
|
---|
1142 |
|
---|
1143 |
|
---|
1144 |
|
---|
1145 | /**
|
---|
1146 | * Wrapper around bind.
|
---|
1147 | *
|
---|
1148 | * @returns IPRT status code.
|
---|
1149 | * @param hSocket The socket handle.
|
---|
1150 | * @param pAddr The socket address to bind to.
|
---|
1151 | * @param cbAddr The size of the address structure @a pAddr
|
---|
1152 | * points to.
|
---|
1153 | */
|
---|
1154 | int rtSocketBind(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr)
|
---|
1155 | {
|
---|
1156 | /*
|
---|
1157 | * Validate input.
|
---|
1158 | */
|
---|
1159 | RTSOCKETINT *pThis = hSocket;
|
---|
1160 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1161 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1162 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1163 |
|
---|
1164 | int rc = VINF_SUCCESS;
|
---|
1165 | if (bind(pThis->hNative, pAddr, cbAddr) != 0)
|
---|
1166 | rc = rtSocketError();
|
---|
1167 |
|
---|
1168 | rtSocketUnlock(pThis);
|
---|
1169 | return rc;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 |
|
---|
1173 | /**
|
---|
1174 | * Wrapper around listen.
|
---|
1175 | *
|
---|
1176 | * @returns IPRT status code.
|
---|
1177 | * @param hSocket The socket handle.
|
---|
1178 | * @param cMaxPending The max number of pending connections.
|
---|
1179 | */
|
---|
1180 | int rtSocketListen(RTSOCKET hSocket, int cMaxPending)
|
---|
1181 | {
|
---|
1182 | /*
|
---|
1183 | * Validate input.
|
---|
1184 | */
|
---|
1185 | RTSOCKETINT *pThis = hSocket;
|
---|
1186 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1187 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1188 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1189 |
|
---|
1190 | int rc = VINF_SUCCESS;
|
---|
1191 | if (listen(pThis->hNative, cMaxPending) != 0)
|
---|
1192 | rc = rtSocketError();
|
---|
1193 |
|
---|
1194 | rtSocketUnlock(pThis);
|
---|
1195 | return rc;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 |
|
---|
1199 | /**
|
---|
1200 | * Wrapper around accept.
|
---|
1201 | *
|
---|
1202 | * @returns IPRT status code.
|
---|
1203 | * @param hSocket The socket handle.
|
---|
1204 | * @param phClient Where to return the client socket handle on
|
---|
1205 | * success.
|
---|
1206 | * @param pAddr Where to return the client address.
|
---|
1207 | * @param pcbAddr On input this gives the size buffer size of what
|
---|
1208 | * @a pAddr point to. On return this contains the
|
---|
1209 | * size of what's stored at @a pAddr.
|
---|
1210 | */
|
---|
1211 | int rtSocketAccept(RTSOCKET hSocket, PRTSOCKET phClient, struct sockaddr *pAddr, size_t *pcbAddr)
|
---|
1212 | {
|
---|
1213 | /*
|
---|
1214 | * Validate input.
|
---|
1215 | * Only lock the socket temporarily while we get the native handle, so that
|
---|
1216 | * we can safely shutdown and destroy the socket from a different thread.
|
---|
1217 | */
|
---|
1218 | RTSOCKETINT *pThis = hSocket;
|
---|
1219 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1220 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1221 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1222 |
|
---|
1223 | /*
|
---|
1224 | * Call accept().
|
---|
1225 | */
|
---|
1226 | rtSocketErrorReset();
|
---|
1227 | int rc = VINF_SUCCESS;
|
---|
1228 | #ifdef RT_OS_WINDOWS
|
---|
1229 | int cbAddr = (int)*pcbAddr;
|
---|
1230 | #else
|
---|
1231 | socklen_t cbAddr = *pcbAddr;
|
---|
1232 | #endif
|
---|
1233 | RTSOCKETNATIVE hNativeClient = accept(pThis->hNative, pAddr, &cbAddr);
|
---|
1234 | if (hNativeClient != NIL_RTSOCKETNATIVE)
|
---|
1235 | {
|
---|
1236 | *pcbAddr = cbAddr;
|
---|
1237 |
|
---|
1238 | /*
|
---|
1239 | * Wrap the client socket.
|
---|
1240 | */
|
---|
1241 | rc = rtSocketCreateForNative(phClient, hNativeClient);
|
---|
1242 | if (RT_FAILURE(rc))
|
---|
1243 | {
|
---|
1244 | #ifdef RT_OS_WINDOWS
|
---|
1245 | closesocket(hNativeClient);
|
---|
1246 | #else
|
---|
1247 | close(hNativeClient);
|
---|
1248 | #endif
|
---|
1249 | }
|
---|
1250 | }
|
---|
1251 | else
|
---|
1252 | rc = rtSocketError();
|
---|
1253 |
|
---|
1254 | rtSocketUnlock(pThis);
|
---|
1255 | return rc;
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 |
|
---|
1259 | /**
|
---|
1260 | * Wrapper around connect.
|
---|
1261 | *
|
---|
1262 | * @returns IPRT status code.
|
---|
1263 | * @param hSocket The socket handle.
|
---|
1264 | * @param pAddr The socket address to connect to.
|
---|
1265 | * @param cbAddr The size of the address structure @a pAddr
|
---|
1266 | * points to.
|
---|
1267 | */
|
---|
1268 | int rtSocketConnect(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr)
|
---|
1269 | {
|
---|
1270 | /*
|
---|
1271 | * Validate input.
|
---|
1272 | */
|
---|
1273 | RTSOCKETINT *pThis = hSocket;
|
---|
1274 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1275 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1276 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1277 |
|
---|
1278 | int rc = VINF_SUCCESS;
|
---|
1279 | if (connect(pThis->hNative, pAddr, cbAddr) != 0)
|
---|
1280 | rc = rtSocketError();
|
---|
1281 |
|
---|
1282 | rtSocketUnlock(pThis);
|
---|
1283 | return rc;
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 |
|
---|
1287 | /**
|
---|
1288 | * Wrapper around setsockopt.
|
---|
1289 | *
|
---|
1290 | * @returns IPRT status code.
|
---|
1291 | * @param hSocket The socket handle.
|
---|
1292 | * @param iLevel The protocol level, e.g. IPPORTO_TCP.
|
---|
1293 | * @param iOption The option, e.g. TCP_NODELAY.
|
---|
1294 | * @param pvValue The value buffer.
|
---|
1295 | * @param cbValue The size of the value pointed to by pvValue.
|
---|
1296 | */
|
---|
1297 | int rtSocketSetOpt(RTSOCKET hSocket, int iLevel, int iOption, void const *pvValue, int cbValue)
|
---|
1298 | {
|
---|
1299 | /*
|
---|
1300 | * Validate input.
|
---|
1301 | */
|
---|
1302 | RTSOCKETINT *pThis = hSocket;
|
---|
1303 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1304 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1305 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1306 |
|
---|
1307 | int rc = VINF_SUCCESS;
|
---|
1308 | if (setsockopt(pThis->hNative, iLevel, iOption, (const char *)pvValue, cbValue) != 0)
|
---|
1309 | rc = rtSocketError();
|
---|
1310 |
|
---|
1311 | rtSocketUnlock(pThis);
|
---|
1312 | return rc;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | #ifdef RT_OS_WINDOWS
|
---|
1316 |
|
---|
1317 | /**
|
---|
1318 | * Internal RTPollSetAdd helper that returns the handle that should be added to
|
---|
1319 | * the pollset.
|
---|
1320 | *
|
---|
1321 | * @returns Valid handle on success, INVALID_HANDLE_VALUE on failure.
|
---|
1322 | * @param hSocket The socket handle.
|
---|
1323 | * @param fEvents The events we're polling for.
|
---|
1324 | * @param ph wher to put the primary handle.
|
---|
1325 | */
|
---|
1326 | int rtSocketPollGetHandle(RTSOCKET hSocket, uint32_t fEvents, PHANDLE ph)
|
---|
1327 | {
|
---|
1328 | RTSOCKETINT *pThis = hSocket;
|
---|
1329 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
1330 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
1331 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
1332 |
|
---|
1333 | int rc = VINF_SUCCESS;
|
---|
1334 | if (pThis->hEvent != WSA_INVALID_EVENT)
|
---|
1335 | *ph = pThis->hEvent;
|
---|
1336 | else
|
---|
1337 | {
|
---|
1338 | *ph = pThis->hEvent = WSACreateEvent();
|
---|
1339 | if (pThis->hEvent == WSA_INVALID_EVENT)
|
---|
1340 | rc = rtSocketError();
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | rtSocketUnlock(pThis);
|
---|
1344 | return rc;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 |
|
---|
1348 | /**
|
---|
1349 | * Undos the harm done by WSAEventSelect.
|
---|
1350 | *
|
---|
1351 | * @returns IPRT status code.
|
---|
1352 | * @param pThis The socket handle.
|
---|
1353 | */
|
---|
1354 | static int rtSocketPollClearEventAndMakeBlocking(RTSOCKETINT *pThis)
|
---|
1355 | {
|
---|
1356 | int rc = VINF_SUCCESS;
|
---|
1357 | if (pThis->fSubscribedEvts)
|
---|
1358 | {
|
---|
1359 | if (WSAEventSelect(pThis->hNative, WSA_INVALID_EVENT, 0) == 0)
|
---|
1360 | {
|
---|
1361 | pThis->fSubscribedEvts = 0;
|
---|
1362 |
|
---|
1363 | /*
|
---|
1364 | * Don't switch back to blocking mode if the socket is currently
|
---|
1365 | * operated in non-blocking mode.
|
---|
1366 | */
|
---|
1367 | if (pThis->fBlocking)
|
---|
1368 | {
|
---|
1369 | u_long fNonBlocking = 0;
|
---|
1370 | int rc2 = ioctlsocket(pThis->hNative, FIONBIO, &fNonBlocking);
|
---|
1371 | if (rc2 != 0)
|
---|
1372 | {
|
---|
1373 | rc = rtSocketError();
|
---|
1374 | AssertMsgFailed(("%Rrc; rc2=%d\n", rc, rc2));
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | else
|
---|
1379 | {
|
---|
1380 | rc = rtSocketError();
|
---|
1381 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
1382 | }
|
---|
1383 | }
|
---|
1384 | return rc;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 |
|
---|
1388 | /**
|
---|
1389 | * Updates the mask of events we're subscribing to.
|
---|
1390 | *
|
---|
1391 | * @returns IPRT status code.
|
---|
1392 | * @param pThis The socket handle.
|
---|
1393 | * @param fEvents The events we want to subscribe to.
|
---|
1394 | */
|
---|
1395 | static int rtSocketPollUpdateEvents(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
1396 | {
|
---|
1397 | LONG fNetworkEvents = 0;
|
---|
1398 | if (fEvents & RTPOLL_EVT_READ)
|
---|
1399 | fNetworkEvents |= FD_READ;
|
---|
1400 | if (fEvents & RTPOLL_EVT_WRITE)
|
---|
1401 | fNetworkEvents |= FD_WRITE;
|
---|
1402 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
1403 | fNetworkEvents |= FD_CLOSE;
|
---|
1404 | if (WSAEventSelect(pThis->hNative, pThis->hEvent, fNetworkEvents) == 0)
|
---|
1405 | {
|
---|
1406 | pThis->fSubscribedEvts = fEvents;
|
---|
1407 | return VINF_SUCCESS;
|
---|
1408 | }
|
---|
1409 |
|
---|
1410 | int rc = rtSocketError();
|
---|
1411 | AssertMsgFailed(("fNetworkEvents=%#x rc=%Rrc\n", fNetworkEvents, rtSocketError()));
|
---|
1412 | return rc;
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 |
|
---|
1416 | /**
|
---|
1417 | * Checks for pending events.
|
---|
1418 | *
|
---|
1419 | * @returns Event mask or 0.
|
---|
1420 | * @param pThis The socket handle.
|
---|
1421 | * @param fEvents The desired events.
|
---|
1422 | */
|
---|
1423 | static uint32_t rtSocketPollCheck(RTSOCKETINT *pThis, uint32_t fEvents)
|
---|
1424 | {
|
---|
1425 | int rc = VINF_SUCCESS;
|
---|
1426 | uint32_t fRetEvents = 0;
|
---|
1427 |
|
---|
1428 | /* Make sure WSAEnumNetworkEvents returns what we want. */
|
---|
1429 | if ((pThis->fSubscribedEvts & fEvents) != fEvents)
|
---|
1430 | rc = rtSocketPollUpdateEvents(pThis, pThis->fSubscribedEvts | fEvents);
|
---|
1431 |
|
---|
1432 | /* Get the event mask, ASSUMES that WSAEnumNetworkEvents doesn't clear stuff. */
|
---|
1433 | WSANETWORKEVENTS NetEvts;
|
---|
1434 | RT_ZERO(NetEvts);
|
---|
1435 | if (WSAEnumNetworkEvents(pThis->hNative, pThis->hEvent, &NetEvts) == 0)
|
---|
1436 | {
|
---|
1437 | if ( (NetEvts.lNetworkEvents & FD_READ)
|
---|
1438 | && (fEvents & RTPOLL_EVT_READ)
|
---|
1439 | && NetEvts.iErrorCode[FD_READ_BIT] == 0)
|
---|
1440 | fRetEvents |= RTPOLL_EVT_READ;
|
---|
1441 |
|
---|
1442 | if ( (NetEvts.lNetworkEvents & FD_WRITE)
|
---|
1443 | && (fEvents & RTPOLL_EVT_WRITE)
|
---|
1444 | && NetEvts.iErrorCode[FD_WRITE_BIT] == 0)
|
---|
1445 | fRetEvents |= RTPOLL_EVT_WRITE;
|
---|
1446 |
|
---|
1447 | if (fEvents & RTPOLL_EVT_ERROR)
|
---|
1448 | {
|
---|
1449 | if (NetEvts.lNetworkEvents & FD_CLOSE)
|
---|
1450 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
1451 | else
|
---|
1452 | for (uint32_t i = 0; i < FD_MAX_EVENTS; i++)
|
---|
1453 | if ( (NetEvts.lNetworkEvents & (1L << i))
|
---|
1454 | && NetEvts.iErrorCode[i] != 0)
|
---|
1455 | fRetEvents |= RTPOLL_EVT_ERROR;
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 | else
|
---|
1459 | rc = rtSocketError();
|
---|
1460 |
|
---|
1461 | /* Fall back on select if we hit an error above. */
|
---|
1462 | if (RT_FAILURE(rc))
|
---|
1463 | {
|
---|
1464 | /** @todo */
|
---|
1465 | }
|
---|
1466 |
|
---|
1467 | return fRetEvents;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 |
|
---|
1471 | /**
|
---|
1472 | * Internal RTPoll helper that polls the socket handle and, if @a fNoWait is
|
---|
1473 | * clear, starts whatever actions we've got running during the poll call.
|
---|
1474 | *
|
---|
1475 | * @returns 0 if no pending events, actions initiated if @a fNoWait is clear.
|
---|
1476 | * Event mask (in @a fEvents) and no actions if the handle is ready
|
---|
1477 | * already.
|
---|
1478 | * UINT32_MAX (asserted) if the socket handle is busy in I/O or a
|
---|
1479 | * different poll set.
|
---|
1480 | *
|
---|
1481 | * @param hSocket The socket handle.
|
---|
1482 | * @param hPollSet The poll set handle (for access checks).
|
---|
1483 | * @param fEvents The events we're polling for.
|
---|
1484 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
1485 | * in this poll set. This can be used for dealing
|
---|
1486 | * with duplicate entries.
|
---|
1487 | * @param fNoWait Set if it's a zero-wait poll call. Clear if
|
---|
1488 | * we'll wait for an event to occur.
|
---|
1489 | *
|
---|
1490 | * @remarks There is a potential race wrt duplicate handles when @a fNoWait is
|
---|
1491 | * @c true, we don't currently care about that oddity...
|
---|
1492 | */
|
---|
1493 | uint32_t rtSocketPollStart(RTSOCKET hSocket, RTPOLLSET hPollSet, uint32_t fEvents, bool fFinalEntry, bool fNoWait)
|
---|
1494 | {
|
---|
1495 | RTSOCKETINT *pThis = hSocket;
|
---|
1496 | AssertPtrReturn(pThis, UINT32_MAX);
|
---|
1497 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, UINT32_MAX);
|
---|
1498 | if (rtSocketTryLock(pThis))
|
---|
1499 | pThis->hPollSet = hPollSet;
|
---|
1500 | else
|
---|
1501 | {
|
---|
1502 | AssertReturn(pThis->hPollSet == hPollSet, UINT32_MAX);
|
---|
1503 | ASMAtomicIncU32(&pThis->cUsers);
|
---|
1504 | }
|
---|
1505 |
|
---|
1506 | /* (rtSocketPollCheck will reset the event object). */
|
---|
1507 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
1508 | if ( !fRetEvents
|
---|
1509 | && !fNoWait)
|
---|
1510 | {
|
---|
1511 | pThis->fPollEvts |= fEvents;
|
---|
1512 | if ( fFinalEntry
|
---|
1513 | && pThis->fSubscribedEvts != pThis->fPollEvts)
|
---|
1514 | {
|
---|
1515 | int rc = rtSocketPollUpdateEvents(pThis, pThis->fPollEvts);
|
---|
1516 | if (RT_FAILURE(rc))
|
---|
1517 | {
|
---|
1518 | pThis->fPollEvts = 0;
|
---|
1519 | fRetEvents = UINT32_MAX;
|
---|
1520 | }
|
---|
1521 | }
|
---|
1522 | }
|
---|
1523 |
|
---|
1524 | if (fRetEvents || fNoWait)
|
---|
1525 | {
|
---|
1526 | if (pThis->cUsers == 1)
|
---|
1527 | {
|
---|
1528 | rtSocketPollClearEventAndMakeBlocking(pThis);
|
---|
1529 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
1530 | }
|
---|
1531 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
1532 | }
|
---|
1533 |
|
---|
1534 | return fRetEvents;
|
---|
1535 | }
|
---|
1536 |
|
---|
1537 |
|
---|
1538 | /**
|
---|
1539 | * Called after a WaitForMultipleObjects returned in order to check for pending
|
---|
1540 | * events and stop whatever actions that rtSocketPollStart() initiated.
|
---|
1541 | *
|
---|
1542 | * @returns Event mask or 0.
|
---|
1543 | *
|
---|
1544 | * @param hSocket The socket handle.
|
---|
1545 | * @param fEvents The events we're polling for.
|
---|
1546 | * @param fFinalEntry Set if this is the final entry for this handle
|
---|
1547 | * in this poll set. This can be used for dealing
|
---|
1548 | * with duplicate entries. Only keep in mind that
|
---|
1549 | * this method is called in reverse order, so the
|
---|
1550 | * first call will have this set (when the entire
|
---|
1551 | * set was processed).
|
---|
1552 | */
|
---|
1553 | uint32_t rtSocketPollDone(RTSOCKET hSocket, uint32_t fEvents, bool fFinalEntry)
|
---|
1554 | {
|
---|
1555 | RTSOCKETINT *pThis = hSocket;
|
---|
1556 | AssertPtrReturn(pThis, 0);
|
---|
1557 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, 0);
|
---|
1558 | Assert(pThis->cUsers > 0);
|
---|
1559 | Assert(pThis->hPollSet != NIL_RTPOLLSET);
|
---|
1560 |
|
---|
1561 | /* Harvest events and clear the event mask for the next round of polling. */
|
---|
1562 | uint32_t fRetEvents = rtSocketPollCheck(pThis, fEvents);
|
---|
1563 | pThis->fPollEvts = 0;
|
---|
1564 |
|
---|
1565 | /* Make the socket blocking again and unlock the handle. */
|
---|
1566 | if (pThis->cUsers == 1)
|
---|
1567 | {
|
---|
1568 | rtSocketPollClearEventAndMakeBlocking(pThis);
|
---|
1569 | pThis->hPollSet = NIL_RTPOLLSET;
|
---|
1570 | }
|
---|
1571 | ASMAtomicDecU32(&pThis->cUsers);
|
---|
1572 | return fRetEvents;
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | #endif /* RT_OS_WINDOWS */
|
---|