| 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 <netinet/tcp.h> // NOLINT | 14 #include <netinet/tcp.h> // NOLINT |
| 15 | 15 |
| 16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
| 17 #include "bin/file.h" | 17 #include "bin/file.h" |
| 18 #include "bin/log.h" | 18 #include "bin/log.h" |
| 19 #include "bin/socket.h" | 19 #include "bin/socket.h" |
| 20 | 20 |
| 21 | 21 |
| 22 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { | |
| 23 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | |
| 24 sockaddr_in *sockaddr = SocketAddress::GetAsSockAddrIn(addrinfo->ai_addr); | |
| 25 const char* result = inet_ntop(addrinfo->ai_family, | |
| 26 &sockaddr->sin_addr, | |
| 27 as_string_, | |
| 28 INET6_ADDRSTRLEN); | |
| 29 if (result == NULL) as_string_[0] = 0; | |
| 30 memmove(reinterpret_cast<void *>(&addr_), | |
| 31 addrinfo->ai_addr, | |
| 32 addrinfo->ai_addrlen); | |
| 33 } | |
| 34 | |
| 35 | |
| 36 bool Socket::Initialize() { | 22 bool Socket::Initialize() { |
| 37 // Nothing to do on Linux. | 23 // Nothing to do on Linux. |
| 38 return true; | 24 return true; |
| 39 } | 25 } |
| 40 | 26 |
| 41 | 27 |
| 42 intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) { | 28 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 43 intptr_t fd; | 29 intptr_t fd; |
| 30 struct hostent server; |
| 31 struct sockaddr_in server_address; |
| 44 | 32 |
| 45 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); | 33 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 46 if (fd < 0) { | 34 if (fd < 0) { |
| 47 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 35 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 48 return -1; | 36 return -1; |
| 49 } | 37 } |
| 50 | 38 |
| 51 FDUtils::SetCloseOnExec(fd); | 39 FDUtils::SetCloseOnExec(fd); |
| 52 Socket::SetNonBlocking(fd); | 40 Socket::SetNonBlocking(fd); |
| 53 | 41 |
| 54 SocketAddress::SetAddrPort(&addr, port); | 42 static const size_t kTempBufSize = 1024; |
| 43 char temp_buf[kTempBufSize]; |
| 44 struct hostent *unused; |
| 45 int err; |
| 46 if (gethostbyname_r( |
| 47 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { |
| 48 TEMP_FAILURE_RETRY(close(fd)); |
| 49 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 50 return -1; |
| 51 } |
| 52 |
| 53 server_address.sin_family = AF_INET; |
| 54 server_address.sin_port = htons(port); |
| 55 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length); |
| 56 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 55 intptr_t result = TEMP_FAILURE_RETRY( | 57 intptr_t result = TEMP_FAILURE_RETRY( |
| 56 connect(fd, | 58 connect(fd, |
| 57 SocketAddress::GetAsSockAddr(&addr), | 59 reinterpret_cast<struct sockaddr *>(&server_address), |
| 58 SocketAddress::GetAddrLength(addr))); | 60 sizeof(server_address))); |
| 59 if (result == 0 || errno == EINPROGRESS) { | 61 if (result == 0 || errno == EINPROGRESS) { |
| 60 return fd; | 62 return fd; |
| 61 } | 63 } |
| 62 TEMP_FAILURE_RETRY(close(fd)); | |
| 63 return -1; | 64 return -1; |
| 64 } | 65 } |
| 65 | 66 |
| 66 | 67 |
| 67 intptr_t Socket::Available(intptr_t fd) { | 68 intptr_t Socket::Available(intptr_t fd) { |
| 68 return FDUtils::AvailableBytes(fd); | 69 return FDUtils::AvailableBytes(fd); |
| 69 } | 70 } |
| 70 | 71 |
| 71 | 72 |
| 72 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 73 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 90 // If the would block we need to retry and therefore return 0 as | 91 // If the would block we need to retry and therefore return 0 as |
| 91 // the number of bytes written. | 92 // the number of bytes written. |
| 92 written_bytes = 0; | 93 written_bytes = 0; |
| 93 } | 94 } |
| 94 return written_bytes; | 95 return written_bytes; |
| 95 } | 96 } |
| 96 | 97 |
| 97 | 98 |
| 98 intptr_t Socket::GetPort(intptr_t fd) { | 99 intptr_t Socket::GetPort(intptr_t fd) { |
| 99 ASSERT(fd >= 0); | 100 ASSERT(fd >= 0); |
| 100 struct sockaddr_storage socket_address; | 101 struct sockaddr_in socket_address; |
| 101 socklen_t size = sizeof(socket_address); | 102 socklen_t size = sizeof(socket_address); |
| 102 if (TEMP_FAILURE_RETRY( | 103 if (TEMP_FAILURE_RETRY( |
| 103 getsockname(fd, | 104 getsockname(fd, |
| 104 SocketAddress::GetAsSockAddr(&socket_address), | 105 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 105 &size))) { | 106 &size))) { |
| 106 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); | 107 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
| 107 return 0; | 108 return 0; |
| 108 } | 109 } |
| 109 return SocketAddress::GetAddrPort(&socket_address); | 110 return ntohs(socket_address.sin_port); |
| 110 } | 111 } |
| 111 | 112 |
| 112 | 113 |
| 113 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 114 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 114 ASSERT(fd >= 0); | 115 ASSERT(fd >= 0); |
| 115 struct sockaddr_storage socket_address; | 116 struct sockaddr_in socket_address; |
| 116 socklen_t size = sizeof(socket_address); | 117 socklen_t size = sizeof(socket_address); |
| 117 if (TEMP_FAILURE_RETRY( | 118 if (TEMP_FAILURE_RETRY( |
| 118 getpeername(fd, | 119 getpeername(fd, |
| 119 SocketAddress::GetAsSockAddr(&socket_address), | 120 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 120 &size))) { | 121 &size))) { |
| 121 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 122 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 122 return false; | 123 return false; |
| 123 } | 124 } |
| 124 struct sockaddr* as_addr = SocketAddress::GetAsSockAddr(&socket_address); | 125 if (inet_ntop(socket_address.sin_family, |
| 125 const void* src; | 126 reinterpret_cast<const void *>(&socket_address.sin_addr), |
| 126 if (socket_address.ss_family == AF_INET6) { | |
| 127 src = reinterpret_cast<const void*>( | |
| 128 &SocketAddress::GetAsSockAddrIn6(as_addr)->sin6_addr); | |
| 129 } else { | |
| 130 src = reinterpret_cast<const void*>( | |
| 131 &SocketAddress::GetAsSockAddrIn(as_addr)->sin_addr); | |
| 132 } | |
| 133 if (inet_ntop(socket_address.ss_family, | |
| 134 src, | |
| 135 host, | 127 host, |
| 136 INET_ADDRSTRLEN) == NULL) { | 128 INET_ADDRSTRLEN) == NULL) { |
| 137 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); | 129 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
| 138 return false; | 130 return false; |
| 139 } | 131 } |
| 140 *port = SocketAddress::GetAddrPort(&socket_address); | 132 *port = ntohs(socket_address.sin_port); |
| 141 return true; | 133 return true; |
| 142 } | 134 } |
| 143 | 135 |
| 144 | 136 |
| 145 void Socket::GetError(intptr_t fd, OSError* os_error) { | 137 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 146 int len = sizeof(errno); | 138 int len = sizeof(errno); |
| 147 getsockopt(fd, | 139 getsockopt(fd, |
| 148 SOL_SOCKET, | 140 SOL_SOCKET, |
| 149 SO_ERROR, | 141 SO_ERROR, |
| 150 &errno, | 142 &errno, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 162 if (S_ISREG(buf.st_mode)) return File::kFile; | 154 if (S_ISREG(buf.st_mode)) return File::kFile; |
| 163 return File::kOther; | 155 return File::kOther; |
| 164 } | 156 } |
| 165 | 157 |
| 166 | 158 |
| 167 intptr_t Socket::GetStdioHandle(int num) { | 159 intptr_t Socket::GetStdioHandle(int num) { |
| 168 return static_cast<intptr_t>(num); | 160 return static_cast<intptr_t>(num); |
| 169 } | 161 } |
| 170 | 162 |
| 171 | 163 |
| 172 SocketAddresses* Socket::LookupAddress(const char* host, | 164 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
| 173 int type, | 165 // Perform a name lookup for an IPv4 address. |
| 174 OSError** os_error) { | |
| 175 // Perform a name lookup for a host name. | |
| 176 struct addrinfo hints; | 166 struct addrinfo hints; |
| 177 memset(&hints, 0, sizeof(hints)); | 167 memset(&hints, 0, sizeof(hints)); |
| 178 hints.ai_family = SocketAddress::FromType(type); | 168 hints.ai_family = AF_INET; |
| 179 hints.ai_socktype = SOCK_STREAM; | 169 hints.ai_socktype = SOCK_STREAM; |
| 180 hints.ai_flags = 0; | |
| 181 hints.ai_protocol = IPPROTO_TCP; | 170 hints.ai_protocol = IPPROTO_TCP; |
| 182 struct addrinfo* info = NULL; | 171 struct addrinfo* info = NULL; |
| 183 int status = getaddrinfo(host, 0, &hints, &info); | 172 int status = getaddrinfo(host, 0, &hints, &info); |
| 184 if (status != 0) { | 173 if (status != 0) { |
| 185 ASSERT(*os_error == NULL); | 174 ASSERT(*os_error == NULL); |
| 186 *os_error = new OSError(status, | 175 *os_error = new OSError(status, |
| 187 gai_strerror(status), | 176 gai_strerror(status), |
| 188 OSError::kGetAddressInfo); | 177 OSError::kGetAddressInfo); |
| 189 return NULL; | 178 return NULL; |
| 190 } | 179 } |
| 191 intptr_t count = 0; | 180 // Convert the address into IPv4 dotted decimal notation. |
| 192 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 181 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); |
| 193 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; | 182 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); |
| 183 const char* result = inet_ntop(AF_INET, |
| 184 reinterpret_cast<void *>(&sockaddr->sin_addr), |
| 185 buffer, |
| 186 INET_ADDRSTRLEN); |
| 187 if (result == NULL) { |
| 188 free(buffer); |
| 189 return NULL; |
| 194 } | 190 } |
| 195 SocketAddresses* addresses = new SocketAddresses(count); | 191 ASSERT(result == buffer); |
| 196 intptr_t i = 0; | 192 return buffer; |
| 197 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | |
| 198 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | |
| 199 addresses->SetAt(i, new SocketAddress(c)); | |
| 200 i++; | |
| 201 } | |
| 202 } | |
| 203 freeaddrinfo(info); | |
| 204 return addresses; | |
| 205 } | 193 } |
| 206 | 194 |
| 207 | 195 |
| 208 intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr, | 196 intptr_t ServerSocket::CreateBindListen(const char* host, |
| 209 intptr_t port, | 197 intptr_t port, |
| 210 intptr_t backlog) { | 198 intptr_t backlog) { |
| 211 intptr_t fd; | 199 intptr_t fd; |
| 200 struct sockaddr_in server_address; |
| 212 | 201 |
| 213 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); | 202 in_addr_t s_addr = inet_addr(host); |
| 203 if (s_addr == INADDR_NONE) { |
| 204 return -5; |
| 205 } |
| 206 |
| 207 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 214 if (fd < 0) return -1; | 208 if (fd < 0) return -1; |
| 215 | 209 |
| 216 FDUtils::SetCloseOnExec(fd); | 210 FDUtils::SetCloseOnExec(fd); |
| 217 | 211 |
| 218 int optval = 1; | 212 int optval = 1; |
| 219 TEMP_FAILURE_RETRY( | 213 TEMP_FAILURE_RETRY( |
| 220 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 214 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 221 | 215 |
| 222 if (addr.ss_family == AF_INET6) { | 216 server_address.sin_family = AF_INET; |
| 223 optval = 0; | 217 server_address.sin_port = htons(port); |
| 224 TEMP_FAILURE_RETRY( | 218 server_address.sin_addr.s_addr = s_addr; |
| 225 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 219 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 226 } | |
| 227 | 220 |
| 228 SocketAddress::SetAddrPort(&addr, port); | |
| 229 if (TEMP_FAILURE_RETRY( | 221 if (TEMP_FAILURE_RETRY( |
| 230 bind(fd, | 222 bind(fd, |
| 231 SocketAddress::GetAsSockAddr(&addr), | 223 reinterpret_cast<struct sockaddr *>(&server_address), |
| 232 SocketAddress::GetAddrLength(addr))) < 0) { | 224 sizeof(server_address))) < 0) { |
| 233 TEMP_FAILURE_RETRY(close(fd)); | 225 TEMP_FAILURE_RETRY(close(fd)); |
| 234 return -1; | 226 return -1; |
| 235 } | 227 } |
| 236 | 228 |
| 237 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 229 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 238 TEMP_FAILURE_RETRY(close(fd)); | 230 TEMP_FAILURE_RETRY(close(fd)); |
| 239 return -1; | 231 return -1; |
| 240 } | 232 } |
| 241 | 233 |
| 242 Socket::SetNonBlocking(fd); | 234 Socket::SetNonBlocking(fd); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 291 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 300 int on = enabled ? 1 : 0; | 292 int on = enabled ? 1 : 0; |
| 301 return TEMP_FAILURE_RETRY(setsockopt(fd, | 293 return TEMP_FAILURE_RETRY(setsockopt(fd, |
| 302 SOL_TCP, | 294 SOL_TCP, |
| 303 TCP_NODELAY, | 295 TCP_NODELAY, |
| 304 reinterpret_cast<char *>(&on), | 296 reinterpret_cast<char *>(&on), |
| 305 sizeof(on))) == 0; | 297 sizeof(on))) == 0; |
| 306 } | 298 } |
| 307 | 299 |
| 308 #endif // defined(TARGET_OS_LINUX) | 300 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |