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