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