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_LINUX) | 8 #if defined(TARGET_OS_LINUX) |
9 | 9 |
10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
(...skipping 10 matching lines...) Expand all Loading... |
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 "bin/thread.h" | 25 #include "bin/thread.h" |
26 #include "platform/signal_blocker.h" | 26 #include "platform/signal_blocker.h" |
27 | 27 |
28 namespace dart { | 28 namespace dart { |
29 namespace bin { | 29 namespace bin { |
30 | 30 |
31 static void SaveErrorAndClose(intptr_t fd) { | |
32 int err = errno; | |
33 VOID_TEMP_FAILURE_RETRY(close(fd)); | |
34 errno = err; | |
35 } | |
36 | |
37 | |
38 SocketAddress::SocketAddress(struct sockaddr* sa) { | 31 SocketAddress::SocketAddress(struct sockaddr* sa) { |
39 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 32 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
40 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, | 33 if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, |
41 INET6_ADDRSTRLEN)) { | 34 INET6_ADDRSTRLEN)) { |
42 as_string_[0] = 0; | 35 as_string_[0] = 0; |
43 } | 36 } |
44 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); | 37 socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa)); |
45 memmove(reinterpret_cast<void*>(&addr_), sa, salen); | 38 memmove(reinterpret_cast<void*>(&addr_), sa, salen); |
46 } | 39 } |
47 | 40 |
(...skipping 21 matching lines...) Expand all Loading... |
69 return fd; | 62 return fd; |
70 } | 63 } |
71 | 64 |
72 | 65 |
73 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 66 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
74 intptr_t result = TEMP_FAILURE_RETRY( | 67 intptr_t result = TEMP_FAILURE_RETRY( |
75 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 68 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); |
76 if ((result == 0) || (errno == EINPROGRESS)) { | 69 if ((result == 0) || (errno == EINPROGRESS)) { |
77 return fd; | 70 return fd; |
78 } | 71 } |
79 SaveErrorAndClose(fd); | 72 FDUtils::FDUtils::SaveErrorAndClose(fd); |
80 return -1; | 73 return -1; |
81 } | 74 } |
82 | 75 |
83 | 76 |
84 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 77 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
85 intptr_t fd = Create(addr); | 78 intptr_t fd = Create(addr); |
86 if (fd < 0) { | 79 if (fd < 0) { |
87 return fd; | 80 return fd; |
88 } | 81 } |
89 return Connect(fd, addr); | 82 return Connect(fd, addr); |
90 } | 83 } |
91 | 84 |
92 | 85 |
93 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 86 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
94 const RawAddr& source_addr) { | 87 const RawAddr& source_addr) { |
95 intptr_t fd = Create(addr); | 88 intptr_t fd = Create(addr); |
96 if (fd < 0) { | 89 if (fd < 0) { |
97 return fd; | 90 return fd; |
98 } | 91 } |
99 | 92 |
100 intptr_t result = TEMP_FAILURE_RETRY( | 93 intptr_t result = TEMP_FAILURE_RETRY( |
101 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); | 94 bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
102 if ((result != 0) && (errno != EINPROGRESS)) { | 95 if ((result != 0) && (errno != EINPROGRESS)) { |
103 SaveErrorAndClose(fd); | 96 FDUtils::SaveErrorAndClose(fd); |
104 return -1; | 97 return -1; |
105 } | 98 } |
106 | 99 |
107 return Connect(fd, addr); | 100 return Connect(fd, addr); |
108 } | 101 } |
109 | 102 |
110 | 103 |
111 bool Socket::IsBindError(intptr_t error_number) { | 104 bool Socket::IsBindError(intptr_t error_number) { |
112 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 105 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
113 error_number == EINVAL; | 106 error_number == EINVAL; |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 } | 315 } |
323 | 316 |
324 if (reuseAddress) { | 317 if (reuseAddress) { |
325 int optval = 1; | 318 int optval = 1; |
326 VOID_NO_RETRY_EXPECTED( | 319 VOID_NO_RETRY_EXPECTED( |
327 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 320 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
328 } | 321 } |
329 | 322 |
330 if (NO_RETRY_EXPECTED( | 323 if (NO_RETRY_EXPECTED( |
331 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 324 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
332 SaveErrorAndClose(fd); | 325 FDUtils::SaveErrorAndClose(fd); |
333 return -1; | 326 return -1; |
334 } | 327 } |
335 return fd; | 328 return fd; |
336 } | 329 } |
337 | 330 |
338 | 331 |
339 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { | 332 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
340 if (ifa->ifa_addr == NULL) { | 333 if (ifa->ifa_addr == NULL) { |
341 // OpenVPN's virtual device tun0. | 334 // OpenVPN's virtual device tun0. |
342 return false; | 335 return false; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 401 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
409 | 402 |
410 if (addr.ss.ss_family == AF_INET6) { | 403 if (addr.ss.ss_family == AF_INET6) { |
411 optval = v6_only ? 1 : 0; | 404 optval = v6_only ? 1 : 0; |
412 VOID_NO_RETRY_EXPECTED( | 405 VOID_NO_RETRY_EXPECTED( |
413 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 406 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
414 } | 407 } |
415 | 408 |
416 if (NO_RETRY_EXPECTED( | 409 if (NO_RETRY_EXPECTED( |
417 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 410 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
418 SaveErrorAndClose(fd); | 411 FDUtils::SaveErrorAndClose(fd); |
419 return -1; | 412 return -1; |
420 } | 413 } |
421 | 414 |
422 // Test for invalid socket port 65535 (some browsers disallow it). | 415 // Test for invalid socket port 65535 (some browsers disallow it). |
423 if ((SocketAddress::GetAddrPort(addr) == 0) && | 416 if ((SocketAddress::GetAddrPort(addr) == 0) && |
424 (Socket::GetPort(fd) == 65535)) { | 417 (Socket::GetPort(fd) == 65535)) { |
425 // Don't close the socket until we have created a new socket, ensuring | 418 // Don't close the socket until we have created a new socket, ensuring |
426 // that we do not get the bad port number again. | 419 // that we do not get the bad port number again. |
427 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 420 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
428 SaveErrorAndClose(fd); | 421 FDUtils::SaveErrorAndClose(fd); |
429 return new_fd; | 422 return new_fd; |
430 } | 423 } |
431 | 424 |
432 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 425 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
433 SaveErrorAndClose(fd); | 426 FDUtils::SaveErrorAndClose(fd); |
434 return -1; | 427 return -1; |
435 } | 428 } |
436 | 429 |
437 return fd; | 430 return fd; |
438 } | 431 } |
439 | 432 |
440 | 433 |
441 bool ServerSocket::StartAccept(intptr_t fd) { | 434 bool ServerSocket::StartAccept(intptr_t fd) { |
442 USE(fd); | 435 USE(fd); |
443 return true; | 436 return true; |
(...skipping 18 matching lines...) Expand all Loading... |
462 if (socket == -1) { | 455 if (socket == -1) { |
463 if (IsTemporaryAcceptError(errno)) { | 456 if (IsTemporaryAcceptError(errno)) { |
464 // We need to signal to the caller that this is actually not an | 457 // We need to signal to the caller that this is actually not an |
465 // error. We got woken up from the poll on the listening socket, | 458 // error. We got woken up from the poll on the listening socket, |
466 // but there is no connection ready to be accepted. | 459 // but there is no connection ready to be accepted. |
467 ASSERT(kTemporaryFailure != -1); | 460 ASSERT(kTemporaryFailure != -1); |
468 socket = kTemporaryFailure; | 461 socket = kTemporaryFailure; |
469 } | 462 } |
470 } else { | 463 } else { |
471 if (!FDUtils::SetCloseOnExec(socket)) { | 464 if (!FDUtils::SetCloseOnExec(socket)) { |
472 SaveErrorAndClose(socket); | 465 FDUtils::SaveErrorAndClose(socket); |
473 return -1; | 466 return -1; |
474 } | 467 } |
475 if (!FDUtils::SetNonBlocking(socket)) { | 468 if (!FDUtils::SetNonBlocking(socket)) { |
476 SaveErrorAndClose(socket); | 469 FDUtils::SaveErrorAndClose(socket); |
477 return -1; | 470 return -1; |
478 } | 471 } |
479 } | 472 } |
480 return socket; | 473 return socket; |
481 } | 474 } |
482 | 475 |
483 | 476 |
484 void Socket::Close(intptr_t fd) { | 477 void Socket::Close(intptr_t fd) { |
485 ASSERT(fd >= 0); | 478 ASSERT(fd >= 0); |
486 VOID_TEMP_FAILURE_RETRY(close(fd)); | 479 VOID_TEMP_FAILURE_RETRY(close(fd)); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, | 595 return NO_RETRY_EXPECTED(setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, |
603 sizeof(mreq))) == 0; | 596 sizeof(mreq))) == 0; |
604 } | 597 } |
605 | 598 |
606 } // namespace bin | 599 } // namespace bin |
607 } // namespace dart | 600 } // namespace dart |
608 | 601 |
609 #endif // defined(TARGET_OS_LINUX) | 602 #endif // defined(TARGET_OS_LINUX) |
610 | 603 |
611 #endif // !defined(DART_IO_DISABLED) | 604 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |