1 | /* $Id: socket.cpp 27503 2010-03-18 20:52:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Network Sockets.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #ifdef RT_OS_WINDOWS
|
---|
36 | # include <winsock.h>
|
---|
37 | #else /* !RT_OS_WINDOWS */
|
---|
38 | # include <errno.h>
|
---|
39 | # include <sys/stat.h>
|
---|
40 | # include <sys/socket.h>
|
---|
41 | # include <netinet/in.h>
|
---|
42 | # include <netinet/tcp.h>
|
---|
43 | # include <arpa/inet.h>
|
---|
44 | # ifdef IPRT_WITH_TCPIP_V6
|
---|
45 | # include <netinet6/in6.h>
|
---|
46 | # endif
|
---|
47 | # include <sys/un.h>
|
---|
48 | # include <netdb.h>
|
---|
49 | # include <unistd.h>
|
---|
50 | # include <fcntl.h>
|
---|
51 | #endif /* !RT_OS_WINDOWS */
|
---|
52 | #include <limits.h>
|
---|
53 |
|
---|
54 | #include "internal/iprt.h"
|
---|
55 | #include <iprt/socket.h>
|
---|
56 |
|
---|
57 | #include <iprt/asm.h>
|
---|
58 | #include <iprt/assert.h>
|
---|
59 | #include <iprt/err.h>
|
---|
60 | #include <iprt/mem.h>
|
---|
61 | #include <iprt/string.h>
|
---|
62 | #include <iprt/thread.h>
|
---|
63 | #include <iprt/time.h>
|
---|
64 |
|
---|
65 | #include "internal/magics.h"
|
---|
66 | #include "internal/socket.h"
|
---|
67 |
|
---|
68 |
|
---|
69 | /*******************************************************************************
|
---|
70 | * Defined Constants And Macros *
|
---|
71 | *******************************************************************************/
|
---|
72 | /* non-standard linux stuff (it seems). */
|
---|
73 | #ifndef MSG_NOSIGNAL
|
---|
74 | # define MSG_NOSIGNAL 0
|
---|
75 | #endif
|
---|
76 | #ifndef SHUT_RDWR
|
---|
77 | # ifdef SD_BOTH
|
---|
78 | # define SHUT_RDWR SD_BOTH
|
---|
79 | # else
|
---|
80 | # define SHUT_RDWR 2
|
---|
81 | # endif
|
---|
82 | #endif
|
---|
83 | #ifndef SHUT_WR
|
---|
84 | # ifdef SD_SEND
|
---|
85 | # define SHUT_WR SD_SEND
|
---|
86 | # else
|
---|
87 | # define SHUT_WR 1
|
---|
88 | # endif
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | /* fixup backlevel OSes. */
|
---|
92 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
93 | # define socklen_t int
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | /** How many pending connection. */
|
---|
97 | #define RTTCP_SERVER_BACKLOG 10
|
---|
98 |
|
---|
99 |
|
---|
100 | /*******************************************************************************
|
---|
101 | * Structures and Typedefs *
|
---|
102 | *******************************************************************************/
|
---|
103 | /**
|
---|
104 | * Socket handle data.
|
---|
105 | *
|
---|
106 | * This is mainly required for implementing RTPollSet on Windows.
|
---|
107 | */
|
---|
108 | typedef struct RTSOCKETINT
|
---|
109 | {
|
---|
110 | /** Magic number (RTTCPSOCKET_MAGIC). */
|
---|
111 | uint32_t u32Magic;
|
---|
112 | /** Usage count. This is used to prevent two threads from accessing the
|
---|
113 | * handle concurrently. */
|
---|
114 | uint32_t volatile cUsers;
|
---|
115 | #ifdef RT_OS_WINDOWS
|
---|
116 | /** The native socket handle. */
|
---|
117 | SOCKET hNative;
|
---|
118 | /** The event semaphore we've associated with the socket handle.
|
---|
119 | * This is INVALID_HANDLE_VALUE if not done. */
|
---|
120 | WSAEVENT hEvent;
|
---|
121 | /** The pollset currently polling this socket. This is NIL if no one is
|
---|
122 | * polling. */
|
---|
123 | RTPOLLSET hPollSet;
|
---|
124 | /** The events we're polling for. */
|
---|
125 | uint32_t fPollEvts;
|
---|
126 | #else
|
---|
127 | /** The native socket handle. */
|
---|
128 | int hNative;
|
---|
129 | #endif
|
---|
130 | } RTSOCKETINT;
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Address union used internally for things like getpeername and getsockname.
|
---|
135 | */
|
---|
136 | typedef union RTSOCKADDRUNION
|
---|
137 | {
|
---|
138 | struct sockaddr Addr;
|
---|
139 | struct sockaddr_in Ipv4;
|
---|
140 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
141 | struct sockaddr_in6 Ipv6;
|
---|
142 | #endif
|
---|
143 | } RTSOCKADDRUNION;
|
---|
144 |
|
---|
145 |
|
---|
146 | /*******************************************************************************
|
---|
147 | * Internal Functions *
|
---|
148 | *******************************************************************************/
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Get the last error as an iprt status code.
|
---|
153 | *
|
---|
154 | * @returns IPRT status code.
|
---|
155 | */
|
---|
156 | DECLINLINE(int) rtSocketError(void)
|
---|
157 | {
|
---|
158 | #ifdef RT_OS_WINDOWS
|
---|
159 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
160 | #else
|
---|
161 | return RTErrConvertFromErrno(errno);
|
---|
162 | #endif
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Resets the last error.
|
---|
168 | */
|
---|
169 | DECLINLINE(void) rtSocketErrorReset(void)
|
---|
170 | {
|
---|
171 | #ifdef RT_OS_WINDOWS
|
---|
172 | WSASetLastError(0);
|
---|
173 | #else
|
---|
174 | errno = 0;
|
---|
175 | #endif
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Get the last resolver error as an iprt status code.
|
---|
181 | *
|
---|
182 | * @returns iprt status code.
|
---|
183 | */
|
---|
184 | int rtSocketResolverError(void)
|
---|
185 | {
|
---|
186 | #ifdef RT_OS_WINDOWS
|
---|
187 | return RTErrConvertFromWin32(WSAGetLastError());
|
---|
188 | #else
|
---|
189 | switch (h_errno)
|
---|
190 | {
|
---|
191 | case HOST_NOT_FOUND:
|
---|
192 | return VERR_NET_HOST_NOT_FOUND;
|
---|
193 | case NO_DATA:
|
---|
194 | return VERR_NET_ADDRESS_NOT_AVAILABLE;
|
---|
195 | case NO_RECOVERY:
|
---|
196 | return VERR_IO_GEN_FAILURE;
|
---|
197 | case TRY_AGAIN:
|
---|
198 | return VERR_TRY_AGAIN;
|
---|
199 |
|
---|
200 | default:
|
---|
201 | return VERR_UNRESOLVED_ERROR;
|
---|
202 | }
|
---|
203 | #endif
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Tries to lock the socket for exclusive usage by the calling thread.
|
---|
209 | *
|
---|
210 | * Call rtSocketUnlock() to unlock.
|
---|
211 | *
|
---|
212 | * @returns @c true if locked, @c false if not.
|
---|
213 | * @param pThis The socket structure.
|
---|
214 | */
|
---|
215 | DECLINLINE(bool) rtSocketTryLock(RTSOCKETINT *pThis)
|
---|
216 | {
|
---|
217 | return ASMAtomicCmpXchgU32(&pThis->cUsers, 1, 0);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Unlocks the socket.
|
---|
223 | *
|
---|
224 | * @param pThis The socket structure.
|
---|
225 | */
|
---|
226 | DECLINLINE(void) rtSocketUnlock(RTSOCKETINT *pThis)
|
---|
227 | {
|
---|
228 | ASMAtomicCmpXchgU32(&pThis->cUsers, 0, 1);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Creates an IPRT socket handle for a native one.
|
---|
234 | *
|
---|
235 | * @returns IPRT status code.
|
---|
236 | * @param ppSocket Where to return the IPRT socket handle.
|
---|
237 | * @param hNative The native handle.
|
---|
238 | */
|
---|
239 | int rtSocketCreateForNative(RTSOCKETINT **ppSocket,
|
---|
240 | #ifdef RT_OS_WINDOWS
|
---|
241 | SOCKET hNative
|
---|
242 | #else
|
---|
243 | int hNative
|
---|
244 | #endif
|
---|
245 | )
|
---|
246 | {
|
---|
247 | RTSOCKETINT *pThis = (RTSOCKETINT *)RTMemAlloc(sizeof(*pThis));
|
---|
248 | if (!pThis)
|
---|
249 | return VERR_NO_MEMORY;
|
---|
250 | pThis->u32Magic = RTSOCKET_MAGIC;
|
---|
251 | pThis->cUsers = 0;
|
---|
252 | pThis->hNative = hNative;
|
---|
253 | #ifdef RT_OS_WINDOWS
|
---|
254 | pThis->hEvent = INVALID_HANDLE_VALUE;
|
---|
255 | pThis->hPollSet = 0;
|
---|
256 | pThis->fPollEvts = 0;
|
---|
257 | #endif
|
---|
258 | *ppSocket = pThis;
|
---|
259 | return VINF_SUCCESS;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * Wrapper around socket().
|
---|
265 | *
|
---|
266 | * @returns IPRT status code.
|
---|
267 | * @param phSocket Where to store the handle to the socket on
|
---|
268 | * success.
|
---|
269 | * @param iDomain The protocol family (PF_XXX).
|
---|
270 | * @param iType The socket type (SOCK_XXX).
|
---|
271 | * @param iProtocol Socket parameter, usually 0.
|
---|
272 | */
|
---|
273 | int rtSocketCreate(PRTSOCKET phSocket, int iDomain, int iType, int iProtocol)
|
---|
274 | {
|
---|
275 | /*
|
---|
276 | * Create the socket.
|
---|
277 | */
|
---|
278 | #ifdef RT_OS_WINDOWS
|
---|
279 | SOCKET hNative = socket(iDomain, iType, iProtocol);
|
---|
280 | if (hNative == INVALID_SOCKET)
|
---|
281 | return rtSocketError();
|
---|
282 | #else
|
---|
283 | int hNative = socket(iDomain, iType, iProtocol);
|
---|
284 | if (hNative == -1)
|
---|
285 | return rtSocketError();
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * Wrap it.
|
---|
290 | */
|
---|
291 | int rc = rtSocketCreateForNative(phSocket, hNative);
|
---|
292 | if (RT_FAILURE(rc))
|
---|
293 | {
|
---|
294 | #ifdef RT_OS_WINDOWS
|
---|
295 | closesocket(hNative);
|
---|
296 | #else
|
---|
297 | close(hNative);
|
---|
298 | #endif
|
---|
299 | }
|
---|
300 | return rc;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | RTDECL(int) RTSocketDestroy(RTSOCKET hSocket)
|
---|
305 | {
|
---|
306 | RTSOCKETINT *pThis = hSocket;
|
---|
307 | if (pThis == NIL_RTSOCKET)
|
---|
308 | return VINF_SUCCESS;
|
---|
309 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
310 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
311 |
|
---|
312 | Assert(pThis->cUsers == 0);
|
---|
313 | AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, RTSOCKET_MAGIC_DEAD, RTSOCKET_MAGIC), VERR_INVALID_HANDLE);
|
---|
314 |
|
---|
315 | /*
|
---|
316 | * Do the cleanup.
|
---|
317 | */
|
---|
318 | int rc = VINF_SUCCESS;
|
---|
319 | #ifdef RT_OS_WINDOWS
|
---|
320 | if (pThis->hEvent == INVALID_HANDLE_VALUE)
|
---|
321 | {
|
---|
322 | CloseHandle(pThis->hEvent);
|
---|
323 | pThis->hEvent = INVALID_HANDLE_VALUE;
|
---|
324 | }
|
---|
325 |
|
---|
326 | if (pThis->hNative != INVALID_HANDLE_VALUE)
|
---|
327 | {
|
---|
328 | rc = closesocket(pThis->hNative);
|
---|
329 | if (!rc)
|
---|
330 | rc = VINF_SUCCESS;
|
---|
331 | else
|
---|
332 | {
|
---|
333 | rc = rtSocketError();
|
---|
334 | AssertMsgFailed(("\"%s\": closesocket(%p) -> %Rrc\n", pThis->hNative, rc));
|
---|
335 | }
|
---|
336 | pThis->hNative = INVALID_HANDLE_VALUE;
|
---|
337 | }
|
---|
338 |
|
---|
339 | #else
|
---|
340 | if (pThis->hNative != -1)
|
---|
341 | {
|
---|
342 | if (close(pThis->hNative))
|
---|
343 | {
|
---|
344 | rc = rtSocketError();
|
---|
345 | AssertMsgFailed(("\"%s\": close(%d) -> %Rrc\n", pThis->hNative, rc));
|
---|
346 | }
|
---|
347 | pThis->hNative = -1;
|
---|
348 | }
|
---|
349 | #endif
|
---|
350 |
|
---|
351 | return rc;
|
---|
352 | }
|
---|
353 |
|
---|
354 |
|
---|
355 | RTDECL(RTHCUINTPTR) RTSocketToNative(RTSOCKET hSocket)
|
---|
356 | {
|
---|
357 | RTSOCKETINT *pThis = hSocket;
|
---|
358 | AssertPtrReturn(pThis, RTHCUINTPTR_MAX);
|
---|
359 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, RTHCUINTPTR_MAX);
|
---|
360 | return (RTHCUINTPTR)pThis->hNative;
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | RTDECL(int) RTSocketSetInheritance(RTSOCKET hSocket, bool fInheritable)
|
---|
365 | {
|
---|
366 | RTSOCKETINT *pThis = hSocket;
|
---|
367 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
368 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
369 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
370 |
|
---|
371 | int rc = VINF_SUCCESS;
|
---|
372 | #ifdef RT_OS_WINDOWS
|
---|
373 | if (!SetHandleInformation(pThis->hNative, HANDLE_FLAG_INHERIT, fInheritable ? HANDLE_FLAG_INHERIT : 0))
|
---|
374 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
375 | #else
|
---|
376 | if (fcntl(pThis->hNative, F_SETFD, fInheritable ? 0 : FD_CLOEXEC) < 0)
|
---|
377 | rc = RTErrConvertFromErrno(errno);
|
---|
378 | #endif
|
---|
379 | AssertRC(rc); /// @todo remove later.
|
---|
380 |
|
---|
381 | rtSocketUnlock(pThis);
|
---|
382 | return rc;
|
---|
383 | }
|
---|
384 |
|
---|
385 |
|
---|
386 | RTDECL(int) RTSocketRead(RTSOCKET hSocket, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
|
---|
387 | {
|
---|
388 | /*
|
---|
389 | * Validate input.
|
---|
390 | */
|
---|
391 | RTSOCKETINT *pThis = hSocket;
|
---|
392 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
393 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
394 | AssertReturn(cbBuffer > 0, VERR_INVALID_PARAMETER);
|
---|
395 | AssertPtr(pvBuffer);
|
---|
396 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Read loop.
|
---|
400 | * If pcbRead is NULL we have to fill the entire buffer!
|
---|
401 | */
|
---|
402 | int rc = VINF_SUCCESS;
|
---|
403 | size_t cbRead = 0;
|
---|
404 | size_t cbToRead = cbBuffer;
|
---|
405 | for (;;)
|
---|
406 | {
|
---|
407 | rtSocketErrorReset();
|
---|
408 | #ifdef RT_OS_WINDOWS
|
---|
409 | int cbNow = cbToRead >= INT_MAX/2 ? INT_MAX/2 : (int)cbToRead;
|
---|
410 | #else
|
---|
411 | size_t cbNow = cbToRead;
|
---|
412 | #endif
|
---|
413 | ssize_t cbBytesRead = recv(pThis->hNative, (char *)pvBuffer + cbRead, cbNow, MSG_NOSIGNAL);
|
---|
414 | if (cbBytesRead <= 0)
|
---|
415 | {
|
---|
416 | rc = rtSocketError();
|
---|
417 | Assert(RT_FAILURE_NP(rc) || cbBytesRead == 0);
|
---|
418 | if (RT_SUCCESS_NP(rc))
|
---|
419 | {
|
---|
420 | if (!pcbRead)
|
---|
421 | rc = VERR_NET_SHUTDOWN;
|
---|
422 | else
|
---|
423 | {
|
---|
424 | *pcbRead = 0;
|
---|
425 | rc = VINF_SUCCESS;
|
---|
426 | }
|
---|
427 | }
|
---|
428 | break;
|
---|
429 | }
|
---|
430 | if (pcbRead)
|
---|
431 | {
|
---|
432 | /* return partial data */
|
---|
433 | *pcbRead = cbBytesRead;
|
---|
434 | break;
|
---|
435 | }
|
---|
436 |
|
---|
437 | /* read more? */
|
---|
438 | cbRead += cbBytesRead;
|
---|
439 | if (cbRead == cbBuffer)
|
---|
440 | break;
|
---|
441 |
|
---|
442 | /* next */
|
---|
443 | cbToRead = cbBuffer - cbRead;
|
---|
444 | }
|
---|
445 |
|
---|
446 | rtSocketUnlock(pThis);
|
---|
447 | return rc;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | RTDECL(int) RTSocketWrite(RTSOCKET hSocket, const void *pvBuffer, size_t cbBuffer)
|
---|
452 | {
|
---|
453 | /*
|
---|
454 | * Validate input.
|
---|
455 | */
|
---|
456 | RTSOCKETINT *pThis = hSocket;
|
---|
457 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
458 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
459 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Try write all at once.
|
---|
463 | */
|
---|
464 | int rc = VINF_SUCCESS;
|
---|
465 | #ifdef RT_OS_WINDOWS
|
---|
466 | int cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
|
---|
467 | #else
|
---|
468 | size_t cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
469 | #endif
|
---|
470 | ssize_t cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
471 | if (RT_LIKELY((size_t)cbWritten == cbBuffer && cbWritten >= 0))
|
---|
472 | rc = VINF_SUCCESS;
|
---|
473 | else if (cbWritten < 0)
|
---|
474 | rc = rtSocketError();
|
---|
475 | else
|
---|
476 | {
|
---|
477 | /*
|
---|
478 | * Unfinished business, write the remainder of the request. Must ignore
|
---|
479 | * VERR_INTERRUPTED here if we've managed to send something.
|
---|
480 | */
|
---|
481 | size_t cbSentSoFar = 0;
|
---|
482 | for (;;)
|
---|
483 | {
|
---|
484 | /* advance */
|
---|
485 | cbBuffer -= (size_t)cbWritten;
|
---|
486 | if (!cbBuffer)
|
---|
487 | break;
|
---|
488 | cbSentSoFar += (size_t)cbWritten;
|
---|
489 | pvBuffer = (char const *)pvBuffer + cbWritten;
|
---|
490 |
|
---|
491 | /* send */
|
---|
492 | #ifdef RT_OS_WINDOWS
|
---|
493 | cbNow = cbBuffer >= INT_MAX / 2 ? INT_MAX / 2 : (int)cbBuffer;
|
---|
494 | #else
|
---|
495 | cbNow = cbBuffer >= SSIZE_MAX ? SSIZE_MAX : cbBuffer;
|
---|
496 | #endif
|
---|
497 | cbWritten = send(pThis->hNative, (const char *)pvBuffer, cbNow, MSG_NOSIGNAL);
|
---|
498 | if (cbWritten >= 0)
|
---|
499 | AssertMsg(cbBuffer >= (size_t)cbWritten, ("Wrote more than we requested!!! cbWritten=%zu cbBuffer=%zu rtSocketError()=%d\n",
|
---|
500 | cbWritten, cbBuffer, rtSocketError()));
|
---|
501 | else
|
---|
502 | {
|
---|
503 | rc = rtSocketError();
|
---|
504 | if (rc != VERR_INTERNAL_ERROR || cbSentSoFar == 0)
|
---|
505 | break;
|
---|
506 | cbWritten = 0;
|
---|
507 | rc = VINF_SUCCESS;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | rtSocketUnlock(pThis);
|
---|
513 | return rc;
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | RTDECL(int) RTSocketSelectOne(RTSOCKET hSocket, RTMSINTERVAL cMillies)
|
---|
518 | {
|
---|
519 | /*
|
---|
520 | * Validate input.
|
---|
521 | */
|
---|
522 | RTSOCKETINT *pThis = hSocket;
|
---|
523 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
524 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
525 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
526 |
|
---|
527 | /*
|
---|
528 | * Set up the file descriptor sets and do the select.
|
---|
529 | */
|
---|
530 | fd_set fdsetR;
|
---|
531 | FD_ZERO(&fdsetR);
|
---|
532 | FD_SET(pThis->hNative, &fdsetR);
|
---|
533 |
|
---|
534 | fd_set fdsetE = fdsetR;
|
---|
535 |
|
---|
536 | int rc;
|
---|
537 | if (cMillies == RT_INDEFINITE_WAIT)
|
---|
538 | rc = select(pThis->hNative + 1, &fdsetR, NULL, &fdsetE, NULL);
|
---|
539 | else
|
---|
540 | {
|
---|
541 | struct timeval timeout;
|
---|
542 | timeout.tv_sec = cMillies / 1000;
|
---|
543 | timeout.tv_usec = (cMillies % 1000) * 1000;
|
---|
544 | rc = select(pThis->hNative + 1, &fdsetR, NULL, &fdsetE, &timeout);
|
---|
545 | }
|
---|
546 | if (rc > 0)
|
---|
547 | rc = VINF_SUCCESS;
|
---|
548 | else if (rc == 0)
|
---|
549 | rc = VERR_TIMEOUT;
|
---|
550 | else
|
---|
551 | rc = rtSocketError();
|
---|
552 |
|
---|
553 | rtSocketUnlock(pThis);
|
---|
554 | return rc;
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | RTDECL(int) RTSocketShutdown(RTSOCKET hSocket, bool fRead, bool fWrite)
|
---|
559 | {
|
---|
560 | /*
|
---|
561 | * Validate input.
|
---|
562 | */
|
---|
563 | RTSOCKETINT *pThis = hSocket;
|
---|
564 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
565 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
566 | AssertReturn(fRead || fWrite, VERR_INVALID_PARAMETER);
|
---|
567 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
568 |
|
---|
569 | /*
|
---|
570 | * Do the job.
|
---|
571 | */
|
---|
572 | int rc = VINF_SUCCESS;
|
---|
573 | int fHow;
|
---|
574 | if (fRead && fWrite)
|
---|
575 | fHow = SHUT_RDWR;
|
---|
576 | else if (fRead)
|
---|
577 | fHow = SHUT_RD;
|
---|
578 | else
|
---|
579 | fHow = SHUT_WR;
|
---|
580 | if (shutdown(pThis->hNative, fHow) == -1)
|
---|
581 | rc = rtSocketError();
|
---|
582 |
|
---|
583 | rtSocketUnlock(pThis);
|
---|
584 | return rc;
|
---|
585 | }
|
---|
586 |
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * Converts from a native socket address to a generic IPRT network address.
|
---|
590 | *
|
---|
591 | * @returns IPRT status code.
|
---|
592 | * @param pSrc The source address.
|
---|
593 | * @param cbSrc The size of the source address.
|
---|
594 | * @param pAddr Where to return the generic IPRT network
|
---|
595 | * address.
|
---|
596 | */
|
---|
597 | static int rtSocketConvertAddress(RTSOCKADDRUNION const *pSrc, size_t cbSrc, PRTNETADDR pAddr)
|
---|
598 | {
|
---|
599 | /*
|
---|
600 | * Convert the address.
|
---|
601 | */
|
---|
602 | if ( cbSrc == sizeof(struct sockaddr_in)
|
---|
603 | && pSrc->Addr.sa_family == AF_INET)
|
---|
604 | {
|
---|
605 | RT_ZERO(*pAddr);
|
---|
606 | pAddr->enmType = RTNETADDRTYPE_IPV4;
|
---|
607 | pAddr->uPort = RT_N2H_U16(pSrc->Ipv4.sin_port);
|
---|
608 | pAddr->uAddr.IPv4.u = pSrc->Ipv4.sin_addr.s_addr;
|
---|
609 | }
|
---|
610 | #ifdef IPRT_WITH_TCPIP_V6
|
---|
611 | else if ( cbSrc == sizeof(struct sockaddr_in6)
|
---|
612 | && pSrc->Addr.sa_family == AF_INET6)
|
---|
613 | {
|
---|
614 | RT_ZERO(*pAddr);
|
---|
615 | pAddr->enmType = RTNETADDRTYPE_IPV6;
|
---|
616 | pAddr->uPort = RT_N2H_U16(pSrc->Ipv6.sin6_port);
|
---|
617 | pAddr->uAddr.IPv6.au32[0] = pSrc->Ipv6.sin6_addr.s6_addr32[0];
|
---|
618 | pAddr->uAddr.IPv6.au32[1] = pSrc->Ipv6.sin6_addr.s6_addr32[1];
|
---|
619 | pAddr->uAddr.IPv6.au32[2] = pSrc->Ipv6.sin6_addr.s6_addr32[2];
|
---|
620 | pAddr->uAddr.IPv6.au32[3] = pSrc->Ipv6.sin6_addr.s6_addr32[3];
|
---|
621 | }
|
---|
622 | #endif
|
---|
623 | else
|
---|
624 | return VERR_NET_ADDRESS_FAMILY_NOT_SUPPORTED;
|
---|
625 | return VINF_SUCCESS;
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | RTDECL(int) RTSocketGetLocalAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
630 | {
|
---|
631 | /*
|
---|
632 | * Validate input.
|
---|
633 | */
|
---|
634 | RTSOCKETINT *pThis = hSocket;
|
---|
635 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
636 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
637 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * Get the address and convert it.
|
---|
641 | */
|
---|
642 | int rc;
|
---|
643 | RTSOCKADDRUNION u;
|
---|
644 | #ifdef RT_OS_WINDOWS
|
---|
645 | int cbAddr = sizeof(u);
|
---|
646 | #else
|
---|
647 | socklen_t cbAddr = sizeof(u);
|
---|
648 | #endif
|
---|
649 | RT_ZERO(u);
|
---|
650 | if (getsockname(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
651 | rc = rtSocketConvertAddress(&u, cbAddr, pAddr);
|
---|
652 | else
|
---|
653 | rc = rtSocketError();
|
---|
654 |
|
---|
655 | rtSocketUnlock(pThis);
|
---|
656 | return rc;
|
---|
657 | }
|
---|
658 |
|
---|
659 |
|
---|
660 | RTDECL(int) RTSocketGetPeerAddress(RTSOCKET hSocket, PRTNETADDR pAddr)
|
---|
661 | {
|
---|
662 | /*
|
---|
663 | * Validate input.
|
---|
664 | */
|
---|
665 | RTSOCKETINT *pThis = hSocket;
|
---|
666 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
667 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
668 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
669 |
|
---|
670 | /*
|
---|
671 | * Get the address and convert it.
|
---|
672 | */
|
---|
673 | int rc;
|
---|
674 | RTSOCKADDRUNION u;
|
---|
675 | #ifdef RT_OS_WINDOWS
|
---|
676 | int cbAddr = sizeof(u);
|
---|
677 | #else
|
---|
678 | socklen_t cbAddr = sizeof(u);
|
---|
679 | #endif
|
---|
680 | RT_ZERO(u);
|
---|
681 | if (getpeername(pThis->hNative, &u.Addr, &cbAddr) == 0)
|
---|
682 | rc = rtSocketConvertAddress(&u, cbAddr, pAddr);
|
---|
683 | else
|
---|
684 | rc = rtSocketError();
|
---|
685 |
|
---|
686 | rtSocketUnlock(pThis);
|
---|
687 | return rc;
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 |
|
---|
692 | /**
|
---|
693 | * Wrapper around bind.
|
---|
694 | *
|
---|
695 | * @returns IPRT status code.
|
---|
696 | * @param hSocket The socket handle.
|
---|
697 | * @param pAddr The socket address to bind to.
|
---|
698 | * @param cbAddr The size of the address structure @a pAddr
|
---|
699 | * points to.
|
---|
700 | */
|
---|
701 | int rtSocketBind(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr)
|
---|
702 | {
|
---|
703 | /*
|
---|
704 | * Validate input.
|
---|
705 | */
|
---|
706 | RTSOCKETINT *pThis = hSocket;
|
---|
707 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
708 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
709 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
710 |
|
---|
711 | int rc = VINF_SUCCESS;
|
---|
712 | if (bind(pThis->hNative, pAddr, cbAddr) != 0)
|
---|
713 | rc = rtSocketError();
|
---|
714 |
|
---|
715 | rtSocketUnlock(pThis);
|
---|
716 | return rc;
|
---|
717 | }
|
---|
718 |
|
---|
719 |
|
---|
720 | /**
|
---|
721 | * Wrapper around listen.
|
---|
722 | *
|
---|
723 | * @returns IPRT status code.
|
---|
724 | * @param hSocket The socket handle.
|
---|
725 | * @param cMaxPending The max number of pending connections.
|
---|
726 | */
|
---|
727 | int rtSocketListen(RTSOCKET hSocket, int cMaxPending)
|
---|
728 | {
|
---|
729 | /*
|
---|
730 | * Validate input.
|
---|
731 | */
|
---|
732 | RTSOCKETINT *pThis = hSocket;
|
---|
733 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
734 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
735 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
736 |
|
---|
737 | int rc = VINF_SUCCESS;
|
---|
738 | if (listen(pThis->hNative, cMaxPending) != 0)
|
---|
739 | rc = rtSocketError();
|
---|
740 |
|
---|
741 | rtSocketUnlock(pThis);
|
---|
742 | return rc;
|
---|
743 | }
|
---|
744 |
|
---|
745 |
|
---|
746 | /**
|
---|
747 | * Wrapper around accept.
|
---|
748 | *
|
---|
749 | * @returns IPRT status code.
|
---|
750 | * @param hSocket The socket handle.
|
---|
751 | * @param phClient Where to return the client socket handle on
|
---|
752 | * success.
|
---|
753 | * @param pAddr Where to return the client address.
|
---|
754 | * @param pcbAddr On input this gives the size buffer size of what
|
---|
755 | * @a pAddr point to. On return this contains the
|
---|
756 | * size of what's stored at @a pAddr.
|
---|
757 | */
|
---|
758 | int rtSocketAccept(RTSOCKET hSocket, PRTSOCKET phClient, struct sockaddr *pAddr, size_t *pcbAddr)
|
---|
759 | {
|
---|
760 | /*
|
---|
761 | * Validate input.
|
---|
762 | */
|
---|
763 | RTSOCKETINT *pThis = hSocket;
|
---|
764 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
765 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
766 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
767 |
|
---|
768 | /*
|
---|
769 | * Call accept().
|
---|
770 | */
|
---|
771 | rtSocketErrorReset();
|
---|
772 | int rc = VINF_SUCCESS;
|
---|
773 | #ifdef RT_OS_WINDOWS
|
---|
774 | int cbAddr = (int)*pcbAddr;
|
---|
775 | SOCKET hNative = accept(pThis->hNative, pAddr, &cbAddr);
|
---|
776 | if (hNative != INVALID_SOCKET)
|
---|
777 | #else
|
---|
778 | socklen_t cbAddr = *pcbAddr;
|
---|
779 | int hNative = accept(pThis->hNative, pAddr, &cbAddr);
|
---|
780 | if (hNative != -1)
|
---|
781 | #endif
|
---|
782 | {
|
---|
783 | *pcbAddr = cbAddr;
|
---|
784 |
|
---|
785 | /*
|
---|
786 | * Wrap the client socket.
|
---|
787 | */
|
---|
788 | rc = rtSocketCreateForNative(phClient, hNative);
|
---|
789 | if (RT_FAILURE(rc))
|
---|
790 | {
|
---|
791 | #ifdef RT_OS_WINDOWS
|
---|
792 | closesocket(hNative);
|
---|
793 | #else
|
---|
794 | close(hNative);
|
---|
795 | #endif
|
---|
796 | }
|
---|
797 | }
|
---|
798 | else
|
---|
799 | rc = rtSocketError();
|
---|
800 |
|
---|
801 | rtSocketUnlock(pThis);
|
---|
802 | return rc;
|
---|
803 | }
|
---|
804 |
|
---|
805 |
|
---|
806 | /**
|
---|
807 | * Wrapper around connect.
|
---|
808 | *
|
---|
809 | * @returns IPRT status code.
|
---|
810 | * @param hSocket The socket handle.
|
---|
811 | * @param pAddr The socket address to connect to.
|
---|
812 | * @param cbAddr The size of the address structure @a pAddr
|
---|
813 | * points to.
|
---|
814 | */
|
---|
815 | int rtSocketConnect(RTSOCKET hSocket, const struct sockaddr *pAddr, int cbAddr)
|
---|
816 | {
|
---|
817 | /*
|
---|
818 | * Validate input.
|
---|
819 | */
|
---|
820 | RTSOCKETINT *pThis = hSocket;
|
---|
821 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
822 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
823 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
824 |
|
---|
825 | int rc = VINF_SUCCESS;
|
---|
826 | if (connect(pThis->hNative, pAddr, cbAddr) != 0)
|
---|
827 | rc = rtSocketError();
|
---|
828 |
|
---|
829 | rtSocketUnlock(pThis);
|
---|
830 | return rc;
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | /**
|
---|
835 | * Wrapper around setsockopt.
|
---|
836 | *
|
---|
837 | * @returns IPRT status code.
|
---|
838 | * @param hSocket The socket handle.
|
---|
839 | * @param iLevel The protocol level, e.g. IPPORTO_TCP.
|
---|
840 | * @param iOption The option, e.g. TCP_NODELAY.
|
---|
841 | * @param pvValue The value buffer.
|
---|
842 | * @param cbValue The size of the value pointed to by pvValue.
|
---|
843 | */
|
---|
844 | int rtSocketSetOpt(RTSOCKET hSocket, int iLevel, int iOption, void const *pvValue, int cbValue)
|
---|
845 | {
|
---|
846 | /*
|
---|
847 | * Validate input.
|
---|
848 | */
|
---|
849 | RTSOCKETINT *pThis = hSocket;
|
---|
850 | AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
|
---|
851 | AssertReturn(pThis->u32Magic == RTSOCKET_MAGIC, VERR_INVALID_HANDLE);
|
---|
852 | AssertReturn(rtSocketTryLock(pThis), VERR_CONCURRENT_ACCESS);
|
---|
853 |
|
---|
854 | int rc = VINF_SUCCESS;
|
---|
855 | if (setsockopt(pThis->hNative, iLevel, iOption, (const char *)pvValue, cbValue) != 0)
|
---|
856 | rc = rtSocketError();
|
---|
857 |
|
---|
858 | rtSocketUnlock(pThis);
|
---|
859 | return rc;
|
---|
860 | }
|
---|
861 |
|
---|