OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
8 #if defined(TARGET_OS_MACOS) | 8 #if defined(TARGET_OS_MACOS) |
9 | 9 |
10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
11 #include "bin/socket_macos.h" | 11 #include "bin/socket_macos.h" |
12 | 12 |
13 #include <errno.h> // NOLINT | 13 #include <errno.h> // NOLINT |
14 #include <ifaddrs.h> // NOLINT | 14 #include <ifaddrs.h> // NOLINT |
15 #include <net/if.h> // NOLINT | 15 #include <net/if.h> // NOLINT |
16 #include <netinet/tcp.h> // NOLINT | 16 #include <netinet/tcp.h> // NOLINT |
17 #include <stdio.h> // NOLINT | 17 #include <stdio.h> // NOLINT |
18 #include <stdlib.h> // NOLINT | 18 #include <stdlib.h> // NOLINT |
19 #include <string.h> // NOLINT | 19 #include <string.h> // NOLINT |
20 #include <sys/stat.h> // NOLINT | 20 #include <sys/stat.h> // NOLINT |
21 #include <unistd.h> // NOLINT | 21 #include <unistd.h> // NOLINT |
22 | 22 |
23 #include "bin/fdutils.h" | 23 #include "bin/fdutils.h" |
24 #include "bin/file.h" | 24 #include "bin/file.h" |
25 #include "platform/signal_blocker.h" | 25 #include "platform/signal_blocker.h" |
26 | 26 |
27 namespace dart { | 27 namespace dart { |
28 namespace bin { | 28 namespace bin { |
29 | 29 |
30 static void SaveErrorAndClose(intptr_t fd) { | 30 static void FDUtils::SaveErrorAndClose(intptr_t fd) { |
31 int err = errno; | 31 int err = errno; |
32 VOID_TEMP_FAILURE_RETRY(close(fd)); | 32 VOID_TEMP_FAILURE_RETRY(close(fd)); |
33 errno = err; | 33 errno = err; |
34 } | 34 } |
35 | 35 |
36 | 36 |
37 SocketAddress::SocketAddress(struct sockaddr* sa) { | 37 SocketAddress::SocketAddress(struct sockaddr* sa) { |
38 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 38 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
39 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, | 39 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, |
40 INET6_ADDRSTRLEN)) { | 40 INET6_ADDRSTRLEN)) { |
(...skipping 17 matching lines...) Expand all Loading... |
58 } | 58 } |
59 | 59 |
60 | 60 |
61 static intptr_t Create(const RawAddr& addr) { | 61 static intptr_t Create(const RawAddr& addr) { |
62 intptr_t fd; | 62 intptr_t fd; |
63 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 63 fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
64 if (fd < 0) { | 64 if (fd < 0) { |
65 return -1; | 65 return -1; |
66 } | 66 } |
67 if (!FDUtils::SetCloseOnExec(fd)) { | 67 if (!FDUtils::SetCloseOnExec(fd)) { |
68 SaveErrorAndClose(fd); | 68 FDUtils::SaveErrorAndClose(fd); |
69 return -1; | 69 return -1; |
70 } | 70 } |
71 if (!FDUtils::SetNonBlocking(fd)) { | 71 if (!FDUtils::SetNonBlocking(fd)) { |
72 SaveErrorAndClose(fd); | 72 FDUtils::SaveErrorAndClose(fd); |
73 return -1; | 73 return -1; |
74 } | 74 } |
75 return fd; | 75 return fd; |
76 } | 76 } |
77 | 77 |
78 | 78 |
79 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 79 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
80 intptr_t result = TEMP_FAILURE_RETRY( | 80 intptr_t result = TEMP_FAILURE_RETRY( |
81 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 81 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
82 if ((result == 0) || (errno == EINPROGRESS)) { | 82 if ((result == 0) || (errno == EINPROGRESS)) { |
83 return fd; | 83 return fd; |
84 } | 84 } |
85 SaveErrorAndClose(fd); | 85 FDUtils::SaveErrorAndClose(fd); |
86 return -1; | 86 return -1; |
87 } | 87 } |
88 | 88 |
89 | 89 |
90 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 90 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
91 intptr_t fd = Create(addr); | 91 intptr_t fd = Create(addr); |
92 if (fd < 0) { | 92 if (fd < 0) { |
93 return fd; | 93 return fd; |
94 } | 94 } |
95 | 95 |
96 return Connect(fd, addr); | 96 return Connect(fd, addr); |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 100 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
101 const RawAddr& source_addr) { | 101 const RawAddr& source_addr) { |
102 intptr_t fd = Create(addr); | 102 intptr_t fd = Create(addr); |
103 if (fd < 0) { | 103 if (fd < 0) { |
104 return fd; | 104 return fd; |
105 } | 105 } |
106 | 106 |
107 intptr_t result = TEMP_FAILURE_RETRY( | 107 intptr_t result = TEMP_FAILURE_RETRY( |
108 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 108 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
109 if ((result != 0) && (errno != EINPROGRESS)) { | 109 if ((result != 0) && (errno != EINPROGRESS)) { |
110 SaveErrorAndClose(fd); | 110 FDUtils::SaveErrorAndClose(fd); |
111 return -1; | 111 return -1; |
112 } | 112 } |
113 | 113 |
114 return Connect(fd, addr); | 114 return Connect(fd, addr); |
115 } | 115 } |
116 | 116 |
117 | 117 |
118 bool Socket::IsBindError(intptr_t error_number) { | 118 bool Socket::IsBindError(intptr_t error_number) { |
119 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 119 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
120 error_number == EINVAL; | 120 error_number == EINVAL; |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 | 311 |
312 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 312 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
313 intptr_t fd; | 313 intptr_t fd; |
314 | 314 |
315 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); | 315 fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
316 if (fd < 0) { | 316 if (fd < 0) { |
317 return -1; | 317 return -1; |
318 } | 318 } |
319 | 319 |
320 if (!FDUtils::SetCloseOnExec(fd)) { | 320 if (!FDUtils::SetCloseOnExec(fd)) { |
321 SaveErrorAndClose(fd); | 321 FDUtils::SaveErrorAndClose(fd); |
322 return -1; | 322 return -1; |
323 } | 323 } |
324 | 324 |
325 if (reuseAddress) { | 325 if (reuseAddress) { |
326 int optval = 1; | 326 int optval = 1; |
327 VOID_NO_RETRY_EXPECTED( | 327 VOID_NO_RETRY_EXPECTED( |
328 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 328 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
329 } | 329 } |
330 | 330 |
331 if (NO_RETRY_EXPECTED( | 331 if (NO_RETRY_EXPECTED( |
332 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 332 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
333 SaveErrorAndClose(fd); | 333 FDUtils::SaveErrorAndClose(fd); |
334 return -1; | 334 return -1; |
335 } | 335 } |
336 | 336 |
337 if (!FDUtils::SetNonBlocking(fd)) { | 337 if (!FDUtils::SetNonBlocking(fd)) { |
338 SaveErrorAndClose(fd); | 338 FDUtils::SaveErrorAndClose(fd); |
339 return -1; | 339 return -1; |
340 } | 340 } |
341 return fd; | 341 return fd; |
342 } | 342 } |
343 | 343 |
344 | 344 |
345 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { | 345 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
346 if (ifa->ifa_addr == NULL) { | 346 if (ifa->ifa_addr == NULL) { |
347 // OpenVPN's virtual device tun0. | 347 // OpenVPN's virtual device tun0. |
348 return false; | 348 return false; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 intptr_t backlog, | 402 intptr_t backlog, |
403 bool v6_only) { | 403 bool v6_only) { |
404 intptr_t fd; | 404 intptr_t fd; |
405 | 405 |
406 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 406 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
407 if (fd < 0) { | 407 if (fd < 0) { |
408 return -1; | 408 return -1; |
409 } | 409 } |
410 | 410 |
411 if (!FDUtils::SetCloseOnExec(fd)) { | 411 if (!FDUtils::SetCloseOnExec(fd)) { |
412 SaveErrorAndClose(fd); | 412 FDUtils::SaveErrorAndClose(fd); |
413 return -1; | 413 return -1; |
414 } | 414 } |
415 | 415 |
416 int optval = 1; | 416 int optval = 1; |
417 VOID_NO_RETRY_EXPECTED( | 417 VOID_NO_RETRY_EXPECTED( |
418 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 418 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
419 | 419 |
420 if (addr.ss.ss_family == AF_INET6) { | 420 if (addr.ss.ss_family == AF_INET6) { |
421 optval = v6_only ? 1 : 0; | 421 optval = v6_only ? 1 : 0; |
422 VOID_NO_RETRY_EXPECTED( | 422 VOID_NO_RETRY_EXPECTED( |
423 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 423 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
424 } | 424 } |
425 | 425 |
426 if (NO_RETRY_EXPECTED( | 426 if (NO_RETRY_EXPECTED( |
427 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 427 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
428 SaveErrorAndClose(fd); | 428 FDUtils::SaveErrorAndClose(fd); |
429 return -1; | 429 return -1; |
430 } | 430 } |
431 | 431 |
432 // Test for invalid socket port 65535 (some browsers disallow it). | 432 // Test for invalid socket port 65535 (some browsers disallow it). |
433 if ((SocketAddress::GetAddrPort(addr) == 0) && | 433 if ((SocketAddress::GetAddrPort(addr) == 0) && |
434 (Socket::GetPort(fd) == 65535)) { | 434 (Socket::GetPort(fd) == 65535)) { |
435 // Don't close the socket until we have created a new socket, ensuring | 435 // Don't close the socket until we have created a new socket, ensuring |
436 // that we do not get the bad port number again. | 436 // that we do not get the bad port number again. |
437 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 437 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
438 SaveErrorAndClose(fd); | 438 FDUtils::SaveErrorAndClose(fd); |
439 return new_fd; | 439 return new_fd; |
440 } | 440 } |
441 | 441 |
442 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 442 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
443 SaveErrorAndClose(fd); | 443 FDUtils::SaveErrorAndClose(fd); |
444 return -1; | 444 return -1; |
445 } | 445 } |
446 | 446 |
447 if (!FDUtils::SetNonBlocking(fd)) { | 447 if (!FDUtils::SetNonBlocking(fd)) { |
448 SaveErrorAndClose(fd); | 448 FDUtils::SaveErrorAndClose(fd); |
449 return -1; | 449 return -1; |
450 } | 450 } |
451 return fd; | 451 return fd; |
452 } | 452 } |
453 | 453 |
454 | 454 |
455 bool ServerSocket::StartAccept(intptr_t fd) { | 455 bool ServerSocket::StartAccept(intptr_t fd) { |
456 USE(fd); | 456 USE(fd); |
457 return true; | 457 return true; |
458 } | 458 } |
459 | 459 |
460 | 460 |
461 intptr_t ServerSocket::Accept(intptr_t fd) { | 461 intptr_t ServerSocket::Accept(intptr_t fd) { |
462 intptr_t socket; | 462 intptr_t socket; |
463 struct sockaddr clientaddr; | 463 struct sockaddr clientaddr; |
464 socklen_t addrlen = sizeof(clientaddr); | 464 socklen_t addrlen = sizeof(clientaddr); |
465 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); | 465 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); |
466 if (socket == -1) { | 466 if (socket == -1) { |
467 if (errno == EAGAIN) { | 467 if (errno == EAGAIN) { |
468 // We need to signal to the caller that this is actually not an | 468 // We need to signal to the caller that this is actually not an |
469 // error. We got woken up from the poll on the listening socket, | 469 // error. We got woken up from the poll on the listening socket, |
470 // but there is no connection ready to be accepted. | 470 // but there is no connection ready to be accepted. |
471 ASSERT(kTemporaryFailure != -1); | 471 ASSERT(kTemporaryFailure != -1); |
472 socket = kTemporaryFailure; | 472 socket = kTemporaryFailure; |
473 } | 473 } |
474 } else { | 474 } else { |
475 if (!FDUtils::SetCloseOnExec(socket)) { | 475 if (!FDUtils::SetCloseOnExec(socket)) { |
476 SaveErrorAndClose(socket); | 476 FDUtils::SaveErrorAndClose(socket); |
477 return -1; | 477 return -1; |
478 } | 478 } |
479 if (!FDUtils::SetNonBlocking(socket)) { | 479 if (!FDUtils::SetNonBlocking(socket)) { |
480 SaveErrorAndClose(socket); | 480 FDUtils::SaveErrorAndClose(socket); |
481 return -1; | 481 return -1; |
482 } | 482 } |
483 } | 483 } |
484 return socket; | 484 return socket; |
485 } | 485 } |
486 | 486 |
487 | 487 |
488 void Socket::Close(intptr_t fd) { | 488 void Socket::Close(intptr_t fd) { |
489 ASSERT(fd >= 0); | 489 ASSERT(fd >= 0); |
490 VOID_TEMP_FAILURE_RETRY(close(fd)); | 490 VOID_TEMP_FAILURE_RETRY(close(fd)); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 int interfaceIndex) { | 631 int interfaceIndex) { |
632 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); | 632 return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false); |
633 } | 633 } |
634 | 634 |
635 } // namespace bin | 635 } // namespace bin |
636 } // namespace dart | 636 } // namespace dart |
637 | 637 |
638 #endif // defined(TARGET_OS_MACOS) | 638 #endif // defined(TARGET_OS_MACOS) |
639 | 639 |
640 #endif // !defined(DART_IO_DISABLED) | 640 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |