| 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 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <stdio.h> // NOLINT | 9 #include <stdio.h> // NOLINT |
| 10 #include <stdlib.h> // NOLINT | 10 #include <stdlib.h> // NOLINT |
| 11 #include <string.h> // NOLINT | 11 #include <string.h> // NOLINT |
| 12 #include <sys/stat.h> // NOLINT | 12 #include <sys/stat.h> // NOLINT |
| 13 #include <unistd.h> // NOLINT | 13 #include <unistd.h> // NOLINT |
| 14 #include <net/if.h> // NOLINT | 14 #include <net/if.h> // NOLINT |
| 15 #include <netinet/tcp.h> // NOLINT | 15 #include <netinet/tcp.h> // NOLINT |
| 16 #include <ifaddrs.h> // NOLINT | 16 #include <ifaddrs.h> // NOLINT |
| 17 | 17 |
| 18 #include "bin/fdutils.h" | 18 #include "bin/fdutils.h" |
| 19 #include "bin/file.h" | 19 #include "bin/file.h" |
| 20 #include "bin/log.h" | |
| 21 #include "bin/socket.h" | 20 #include "bin/socket.h" |
| 22 #include "platform/signal_blocker.h" | 21 #include "platform/signal_blocker.h" |
| 23 #include "vm/thread.h" | 22 #include "vm/thread.h" |
| 24 | 23 |
| 25 | 24 |
| 26 namespace dart { | 25 namespace dart { |
| 27 namespace bin { | 26 namespace bin { |
| 28 | 27 |
| 29 SocketAddress::SocketAddress(struct sockaddr* sa) { | 28 SocketAddress::SocketAddress(struct sockaddr* sa) { |
| 30 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 29 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 51 // Nothing to do on Linux. | 50 // Nothing to do on Linux. |
| 52 return true; | 51 return true; |
| 53 } | 52 } |
| 54 | 53 |
| 55 | 54 |
| 56 intptr_t Socket::Create(RawAddr addr) { | 55 intptr_t Socket::Create(RawAddr addr) { |
| 57 intptr_t fd; | 56 intptr_t fd; |
| 58 fd = NO_RETRY_EXPECTED( | 57 fd = NO_RETRY_EXPECTED( |
| 59 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); | 58 socket(addr.ss.ss_family, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0)); |
| 60 if (fd < 0) { | 59 if (fd < 0) { |
| 61 const int kBufferSize = 1024; | |
| 62 char error_buf[kBufferSize]; | |
| 63 Log::PrintErr("Error Create: %s\n", | |
| 64 strerror_r(errno, error_buf, kBufferSize)); | |
| 65 return -1; | 60 return -1; |
| 66 } | 61 } |
| 67 return fd; | 62 return fd; |
| 68 } | 63 } |
| 69 | 64 |
| 70 | 65 |
| 71 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 66 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
| 72 SocketAddress::SetAddrPort(&addr, port); | 67 SocketAddress::SetAddrPort(&addr, port); |
| 73 intptr_t result = TEMP_FAILURE_RETRY( | 68 intptr_t result = TEMP_FAILURE_RETRY( |
| 74 connect(fd, &addr.addr, SocketAddress::GetAddrLength(&addr))); | 69 connect(fd, &addr.addr, SocketAddress::GetAddrLength(&addr))); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 } | 144 } |
| 150 return written_bytes; | 145 return written_bytes; |
| 151 } | 146 } |
| 152 | 147 |
| 153 | 148 |
| 154 intptr_t Socket::GetPort(intptr_t fd) { | 149 intptr_t Socket::GetPort(intptr_t fd) { |
| 155 ASSERT(fd >= 0); | 150 ASSERT(fd >= 0); |
| 156 RawAddr raw; | 151 RawAddr raw; |
| 157 socklen_t size = sizeof(raw); | 152 socklen_t size = sizeof(raw); |
| 158 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 153 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { |
| 159 const int kBufferSize = 1024; | |
| 160 char error_buf[kBufferSize]; | |
| 161 Log::PrintErr("Error getsockname: %s\n", | |
| 162 strerror_r(errno, error_buf, kBufferSize)); | |
| 163 return 0; | 154 return 0; |
| 164 } | 155 } |
| 165 return SocketAddress::GetAddrPort(&raw); | 156 return SocketAddress::GetAddrPort(&raw); |
| 166 } | 157 } |
| 167 | 158 |
| 168 | 159 |
| 169 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { | 160 SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { |
| 170 ASSERT(fd >= 0); | 161 ASSERT(fd >= 0); |
| 171 RawAddr raw; | 162 RawAddr raw; |
| 172 socklen_t size = sizeof(raw); | 163 socklen_t size = sizeof(raw); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 } else { | 429 } else { |
| 439 FDUtils::SetNonBlocking(socket); | 430 FDUtils::SetNonBlocking(socket); |
| 440 FDUtils::SetCloseOnExec(socket); | 431 FDUtils::SetCloseOnExec(socket); |
| 441 } | 432 } |
| 442 return socket; | 433 return socket; |
| 443 } | 434 } |
| 444 | 435 |
| 445 | 436 |
| 446 void Socket::Close(intptr_t fd) { | 437 void Socket::Close(intptr_t fd) { |
| 447 ASSERT(fd >= 0); | 438 ASSERT(fd >= 0); |
| 448 int err = TEMP_FAILURE_RETRY(close(fd)); | 439 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 449 if (err != 0) { | |
| 450 const int kBufferSize = 1024; | |
| 451 char error_buf[kBufferSize]; | |
| 452 Log::PrintErr("%s\n", strerror_r(errno, error_buf, kBufferSize)); | |
| 453 } | |
| 454 } | 440 } |
| 455 | 441 |
| 456 | 442 |
| 457 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { | 443 bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
| 458 int on; | 444 int on; |
| 459 socklen_t len = sizeof(on); | 445 socklen_t len = sizeof(on); |
| 460 int err = NO_RETRY_EXPECTED(getsockopt( | 446 int err = NO_RETRY_EXPECTED(getsockopt( |
| 461 fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<void *>(&on), &len)); | 447 fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<void *>(&on), &len)); |
| 462 if (err == 0) { | 448 if (err == 0) { |
| 463 *enabled = on == 1; | 449 *enabled = on == 1; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 mreq.gr_interface = interfaceIndex; | 552 mreq.gr_interface = interfaceIndex; |
| 567 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); | 553 memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
| 568 return NO_RETRY_EXPECTED( | 554 return NO_RETRY_EXPECTED( |
| 569 setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; | 555 setsockopt(fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; |
| 570 } | 556 } |
| 571 | 557 |
| 572 } // namespace bin | 558 } // namespace bin |
| 573 } // namespace dart | 559 } // namespace dart |
| 574 | 560 |
| 575 #endif // defined(TARGET_OS_LINUX) | 561 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |