| 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 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // Nothing to do on Linux. | 41 // Nothing to do on Linux. |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 | 45 |
| 46 intptr_t Socket::Create(RawAddr addr) { | 46 intptr_t Socket::Create(RawAddr addr) { |
| 47 intptr_t fd; | 47 intptr_t fd; |
| 48 | 48 |
| 49 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 49 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 50 if (fd < 0) { | 50 if (fd < 0) { |
| 51 Log::PrintErr("Error Create: %s\n", strerror(errno)); | 51 const int kBufferSize = 1024; |
| 52 char error_buf[kBufferSize]; |
| 53 Log::PrintErr("Error Create: %s\n", |
| 54 strerror_r(errno, error_buf, kBufferSize)); |
| 52 return -1; | 55 return -1; |
| 53 } | 56 } |
| 54 | 57 |
| 55 FDUtils::SetCloseOnExec(fd); | 58 FDUtils::SetCloseOnExec(fd); |
| 56 return fd; | 59 return fd; |
| 57 } | 60 } |
| 58 | 61 |
| 59 | 62 |
| 60 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 63 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
| 61 SocketAddress::SetAddrPort(&addr, port); | 64 SocketAddress::SetAddrPort(&addr, port); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 118 |
| 116 | 119 |
| 117 intptr_t Socket::GetPort(intptr_t fd) { | 120 intptr_t Socket::GetPort(intptr_t fd) { |
| 118 ASSERT(fd >= 0); | 121 ASSERT(fd >= 0); |
| 119 RawAddr raw; | 122 RawAddr raw; |
| 120 socklen_t size = sizeof(raw); | 123 socklen_t size = sizeof(raw); |
| 121 if (TEMP_FAILURE_RETRY( | 124 if (TEMP_FAILURE_RETRY( |
| 122 getsockname(fd, | 125 getsockname(fd, |
| 123 &raw.addr, | 126 &raw.addr, |
| 124 &size))) { | 127 &size))) { |
| 125 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); | 128 const int kBufferSize = 1024; |
| 129 char error_buf[kBufferSize]; |
| 130 Log::PrintErr("Error getsockname: %s\n", |
| 131 strerror_r(errno, error_buf, kBufferSize)); |
| 126 return 0; | 132 return 0; |
| 127 } | 133 } |
| 128 return SocketAddress::GetAddrPort(&raw); | 134 return SocketAddress::GetAddrPort(&raw); |
| 129 } | 135 } |
| 130 | 136 |
| 131 | 137 |
| 132 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 138 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 133 ASSERT(fd >= 0); | 139 ASSERT(fd >= 0); |
| 134 RawAddr raw; | 140 RawAddr raw; |
| 135 socklen_t size = sizeof(raw); | 141 socklen_t size = sizeof(raw); |
| 136 if (TEMP_FAILURE_RETRY( | 142 if (TEMP_FAILURE_RETRY( |
| 137 getpeername(fd, | 143 getpeername(fd, |
| 138 &raw.addr, | 144 &raw.addr, |
| 139 &size))) { | 145 &size))) { |
| 140 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 146 const int kBufferSize = 1024; |
| 147 char error_buf[kBufferSize]; |
| 148 Log::PrintErr("Error getpeername: %s\n", |
| 149 strerror_r(errno, error_buf, kBufferSize)); |
| 141 return false; | 150 return false; |
| 142 } | 151 } |
| 143 const void* src; | 152 const void* src; |
| 144 if (raw.ss.ss_family == AF_INET6) { | 153 if (raw.ss.ss_family == AF_INET6) { |
| 145 src = reinterpret_cast<const void*>(&raw.in6.sin6_addr); | 154 src = reinterpret_cast<const void*>(&raw.in6.sin6_addr); |
| 146 } else { | 155 } else { |
| 147 src = reinterpret_cast<const void*>(&raw.in.sin_addr); | 156 src = reinterpret_cast<const void*>(&raw.in.sin_addr); |
| 148 } | 157 } |
| 149 if (inet_ntop(raw.ss.ss_family, | 158 if (inet_ntop(raw.ss.ss_family, |
| 150 src, | 159 src, |
| 151 host, | 160 host, |
| 152 INET_ADDRSTRLEN) == NULL) { | 161 INET_ADDRSTRLEN) == NULL) { |
| 153 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); | 162 const int kBufferSize = 1024; |
| 163 char error_buf[kBufferSize]; |
| 164 Log::PrintErr("Error inet_ntop: %s\n", |
| 165 strerror_r(errno, error_buf, kBufferSize)); |
| 154 return false; | 166 return false; |
| 155 } | 167 } |
| 156 *port = SocketAddress::GetAddrPort(&raw); | 168 *port = SocketAddress::GetAddrPort(&raw); |
| 157 return true; | 169 return true; |
| 158 } | 170 } |
| 159 | 171 |
| 160 | 172 |
| 161 void Socket::GetError(intptr_t fd, OSError* os_error) { | 173 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 162 int len = sizeof(errno); | 174 int len = sizeof(errno); |
| 163 getsockopt(fd, | 175 getsockopt(fd, |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 Socket::SetNonBlocking(socket); | 386 Socket::SetNonBlocking(socket); |
| 375 } | 387 } |
| 376 return socket; | 388 return socket; |
| 377 } | 389 } |
| 378 | 390 |
| 379 | 391 |
| 380 void Socket::Close(intptr_t fd) { | 392 void Socket::Close(intptr_t fd) { |
| 381 ASSERT(fd >= 0); | 393 ASSERT(fd >= 0); |
| 382 int err = TEMP_FAILURE_RETRY(close(fd)); | 394 int err = TEMP_FAILURE_RETRY(close(fd)); |
| 383 if (err != 0) { | 395 if (err != 0) { |
| 384 Log::PrintErr("%s\n", strerror(errno)); | 396 const int kBufferSize = 1024; |
| 397 char error_buf[kBufferSize]; |
| 398 Log::PrintErr("%s\n", strerror_r(errno, error_buf, kBufferSize)); |
| 385 } | 399 } |
| 386 } | 400 } |
| 387 | 401 |
| 388 | 402 |
| 389 bool Socket::SetNonBlocking(intptr_t fd) { | 403 bool Socket::SetNonBlocking(intptr_t fd) { |
| 390 return FDUtils::SetNonBlocking(fd); | 404 return FDUtils::SetNonBlocking(fd); |
| 391 } | 405 } |
| 392 | 406 |
| 393 | 407 |
| 394 bool Socket::SetBlocking(intptr_t fd) { | 408 bool Socket::SetBlocking(intptr_t fd) { |
| 395 return FDUtils::SetBlocking(fd); | 409 return FDUtils::SetBlocking(fd); |
| 396 } | 410 } |
| 397 | 411 |
| 398 | 412 |
| 399 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 413 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 400 int on = enabled ? 1 : 0; | 414 int on = enabled ? 1 : 0; |
| 401 return TEMP_FAILURE_RETRY(setsockopt(fd, | 415 return TEMP_FAILURE_RETRY(setsockopt(fd, |
| 402 IPPROTO_TCP, | 416 IPPROTO_TCP, |
| 403 TCP_NODELAY, | 417 TCP_NODELAY, |
| 404 reinterpret_cast<char *>(&on), | 418 reinterpret_cast<char *>(&on), |
| 405 sizeof(on))) == 0; | 419 sizeof(on))) == 0; |
| 406 } | 420 } |
| 407 | 421 |
| 408 } // namespace bin | 422 } // namespace bin |
| 409 } // namespace dart | 423 } // namespace dart |
| 410 | 424 |
| 411 #endif // defined(TARGET_OS_LINUX) | 425 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |