| 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 #define SOCKADDR_STORAGE_GET_PORT(addr) \ | |
| 23 addr.ss_family == AF_INET ? \ | |
| 24 ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \ | |
| 25 ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port) | |
| 26 | |
| 27 | |
| 28 typedef union { | |
| 29 struct sockaddr sa; | |
| 30 struct sockaddr_in sa_in; | |
| 31 struct sockaddr_in6 sa_in6; | |
| 32 struct sockaddr_storage sa_stor; | |
| 33 } address; | |
| 34 | |
| 35 | |
| 36 static void SetPort(sockaddr_storage* addr, int port) { | |
| 37 address* ptr = reinterpret_cast<address*>(addr); | |
| 38 if (ptr->sa_stor.ss_family == AF_INET) { | |
| 39 ptr->sa_in.sin_port = htons(port); | |
| 40 } else { \ | |
| 41 ptr->sa_in6.sin6_port = htons(port); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 | |
| 46 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { | |
| 47 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | |
| 48 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr); | |
| 49 const char* result = inet_ntop(addrinfo->ai_family, | |
| 50 &sockaddr->sin_addr, | |
| 51 as_string_, | |
| 52 INET6_ADDRSTRLEN); | |
| 53 if (result == NULL) as_string_[0] = 0; | |
| 54 memmove(reinterpret_cast<void *>(&addr_), | |
| 55 addrinfo->ai_addr, | |
| 56 addrinfo->ai_addrlen); | |
| 57 } | |
| 58 | |
| 59 | |
| 60 bool Socket::Initialize() { | 22 bool Socket::Initialize() { |
| 61 // Nothing to do on Linux. | 23 // Nothing to do on Linux. |
| 62 return true; | 24 return true; |
| 63 } | 25 } |
| 64 | 26 |
| 65 | 27 |
| 66 intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) { | 28 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| 67 intptr_t fd; | 29 intptr_t fd; |
| 30 struct hostent server; |
| 31 struct sockaddr_in server_address; |
| 68 | 32 |
| 69 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); | 33 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
| 70 if (fd < 0) { | 34 if (fd < 0) { |
| 71 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 35 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 72 return -1; | 36 return -1; |
| 73 } | 37 } |
| 74 | 38 |
| 75 FDUtils::SetCloseOnExec(fd); | 39 FDUtils::SetCloseOnExec(fd); |
| 76 Socket::SetNonBlocking(fd); | 40 Socket::SetNonBlocking(fd); |
| 77 | 41 |
| 78 SetPort(&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)); |
| 79 intptr_t result = TEMP_FAILURE_RETRY( | 57 intptr_t result = TEMP_FAILURE_RETRY( |
| 80 connect(fd, | 58 connect(fd, |
| 81 reinterpret_cast<struct sockaddr*>(&addr), | 59 reinterpret_cast<struct sockaddr *>(&server_address), |
| 82 SocketAddress::GetAddrLength(addr))); | 60 sizeof(server_address))); |
| 83 if (result == 0 || errno == EINPROGRESS) { | 61 if (result == 0 || errno == EINPROGRESS) { |
| 84 return fd; | 62 return fd; |
| 85 } | 63 } |
| 86 TEMP_FAILURE_RETRY(close(fd)); | |
| 87 return -1; | 64 return -1; |
| 88 } | 65 } |
| 89 | 66 |
| 90 | 67 |
| 91 intptr_t Socket::Available(intptr_t fd) { | 68 intptr_t Socket::Available(intptr_t fd) { |
| 92 return FDUtils::AvailableBytes(fd); | 69 return FDUtils::AvailableBytes(fd); |
| 93 } | 70 } |
| 94 | 71 |
| 95 | 72 |
| 96 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... |
| 114 // 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 |
| 115 // the number of bytes written. | 92 // the number of bytes written. |
| 116 written_bytes = 0; | 93 written_bytes = 0; |
| 117 } | 94 } |
| 118 return written_bytes; | 95 return written_bytes; |
| 119 } | 96 } |
| 120 | 97 |
| 121 | 98 |
| 122 intptr_t Socket::GetPort(intptr_t fd) { | 99 intptr_t Socket::GetPort(intptr_t fd) { |
| 123 ASSERT(fd >= 0); | 100 ASSERT(fd >= 0); |
| 124 struct sockaddr_storage socket_address; | 101 struct sockaddr_in socket_address; |
| 125 socklen_t size = sizeof(socket_address); | 102 socklen_t size = sizeof(socket_address); |
| 126 if (TEMP_FAILURE_RETRY( | 103 if (TEMP_FAILURE_RETRY( |
| 127 getsockname(fd, | 104 getsockname(fd, |
| 128 reinterpret_cast<struct sockaddr *>(&socket_address), | 105 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 129 &size))) { | 106 &size))) { |
| 130 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); | 107 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
| 131 return 0; | 108 return 0; |
| 132 } | 109 } |
| 133 return SOCKADDR_STORAGE_GET_PORT(socket_address); | 110 return ntohs(socket_address.sin_port); |
| 134 } | 111 } |
| 135 | 112 |
| 136 | 113 |
| 137 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 114 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 138 ASSERT(fd >= 0); | 115 ASSERT(fd >= 0); |
| 139 struct sockaddr_storage socket_address; | 116 struct sockaddr_in socket_address; |
| 140 socklen_t size = sizeof(socket_address); | 117 socklen_t size = sizeof(socket_address); |
| 141 if (TEMP_FAILURE_RETRY( | 118 if (TEMP_FAILURE_RETRY( |
| 142 getpeername(fd, | 119 getpeername(fd, |
| 143 reinterpret_cast<struct sockaddr *>(&socket_address), | 120 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 144 &size))) { | 121 &size))) { |
| 145 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 122 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 146 return false; | 123 return false; |
| 147 } | 124 } |
| 148 const void* src; | 125 if (inet_ntop(socket_address.sin_family, |
| 149 if (socket_address.ss_family == AF_INET6) { | 126 reinterpret_cast<const void *>(&socket_address.sin_addr), |
| 150 const sockaddr_in6* in6 = | |
| 151 reinterpret_cast<const sockaddr_in6*>(&socket_address); | |
| 152 src = reinterpret_cast<const void*>(&in6->sin6_addr); | |
| 153 } else { | |
| 154 const sockaddr_in* in = | |
| 155 reinterpret_cast<const sockaddr_in*>(&socket_address); | |
| 156 src = reinterpret_cast<const void*>(&in->sin_addr); | |
| 157 } | |
| 158 if (inet_ntop(socket_address.ss_family, | |
| 159 src, | |
| 160 host, | 127 host, |
| 161 INET_ADDRSTRLEN) == NULL) { | 128 INET_ADDRSTRLEN) == NULL) { |
| 162 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); | 129 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
| 163 return false; | 130 return false; |
| 164 } | 131 } |
| 165 *port = SOCKADDR_STORAGE_GET_PORT(socket_address); | 132 *port = ntohs(socket_address.sin_port); |
| 166 return true; | 133 return true; |
| 167 } | 134 } |
| 168 | 135 |
| 169 | 136 |
| 170 void Socket::GetError(intptr_t fd, OSError* os_error) { | 137 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 171 int len = sizeof(errno); | 138 int len = sizeof(errno); |
| 172 getsockopt(fd, | 139 getsockopt(fd, |
| 173 SOL_SOCKET, | 140 SOL_SOCKET, |
| 174 SO_ERROR, | 141 SO_ERROR, |
| 175 &errno, | 142 &errno, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 187 if (S_ISREG(buf.st_mode)) return File::kFile; | 154 if (S_ISREG(buf.st_mode)) return File::kFile; |
| 188 return File::kOther; | 155 return File::kOther; |
| 189 } | 156 } |
| 190 | 157 |
| 191 | 158 |
| 192 intptr_t Socket::GetStdioHandle(int num) { | 159 intptr_t Socket::GetStdioHandle(int num) { |
| 193 return static_cast<intptr_t>(num); | 160 return static_cast<intptr_t>(num); |
| 194 } | 161 } |
| 195 | 162 |
| 196 | 163 |
| 197 SocketAddresses* Socket::LookupAddress(const char* host, | 164 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
| 198 int type, | 165 // Perform a name lookup for an IPv4 address. |
| 199 OSError** os_error) { | |
| 200 // Perform a name lookup for a host name. | |
| 201 struct addrinfo hints; | 166 struct addrinfo hints; |
| 202 memset(&hints, 0, sizeof(hints)); | 167 memset(&hints, 0, sizeof(hints)); |
| 203 hints.ai_family = SocketAddress::FromType(type); | 168 hints.ai_family = AF_INET; |
| 204 hints.ai_socktype = SOCK_STREAM; | 169 hints.ai_socktype = SOCK_STREAM; |
| 205 hints.ai_flags = 0; | |
| 206 hints.ai_protocol = IPPROTO_TCP; | 170 hints.ai_protocol = IPPROTO_TCP; |
| 207 struct addrinfo* info = NULL; | 171 struct addrinfo* info = NULL; |
| 208 int status = getaddrinfo(host, 0, &hints, &info); | 172 int status = getaddrinfo(host, 0, &hints, &info); |
| 209 if (status != 0) { | 173 if (status != 0) { |
| 210 ASSERT(*os_error == NULL); | 174 ASSERT(*os_error == NULL); |
| 211 *os_error = new OSError(status, | 175 *os_error = new OSError(status, |
| 212 gai_strerror(status), | 176 gai_strerror(status), |
| 213 OSError::kGetAddressInfo); | 177 OSError::kGetAddressInfo); |
| 214 return NULL; | 178 return NULL; |
| 215 } | 179 } |
| 216 intptr_t count = 0; | 180 // Convert the address into IPv4 dotted decimal notation. |
| 217 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 181 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); |
| 218 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; |
| 219 } | 190 } |
| 220 SocketAddresses* addresses = new SocketAddresses(count); | 191 ASSERT(result == buffer); |
| 221 intptr_t i = 0; | 192 return buffer; |
| 222 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | |
| 223 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | |
| 224 addresses->SetAt(i, new SocketAddress(c)); | |
| 225 i++; | |
| 226 } | |
| 227 } | |
| 228 freeaddrinfo(info); | |
| 229 return addresses; | |
| 230 } | 193 } |
| 231 | 194 |
| 232 | 195 |
| 233 intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr, | 196 intptr_t ServerSocket::CreateBindListen(const char* host, |
| 234 intptr_t port, | 197 intptr_t port, |
| 235 intptr_t backlog) { | 198 intptr_t backlog) { |
| 236 intptr_t fd; | 199 intptr_t fd; |
| 200 struct sockaddr_in server_address; |
| 237 | 201 |
| 238 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)); |
| 239 if (fd < 0) return -1; | 208 if (fd < 0) return -1; |
| 240 | 209 |
| 241 FDUtils::SetCloseOnExec(fd); | 210 FDUtils::SetCloseOnExec(fd); |
| 242 | 211 |
| 243 int optval = 1; | 212 int optval = 1; |
| 244 TEMP_FAILURE_RETRY( | 213 TEMP_FAILURE_RETRY( |
| 245 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 214 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 246 | 215 |
| 247 if (addr.ss_family == AF_INET6) { | 216 server_address.sin_family = AF_INET; |
| 248 optval = 0; | 217 server_address.sin_port = htons(port); |
| 249 TEMP_FAILURE_RETRY( | 218 server_address.sin_addr.s_addr = s_addr; |
| 250 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 219 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
| 251 } | |
| 252 | 220 |
| 253 SetPort(&addr, port); | |
| 254 if (TEMP_FAILURE_RETRY( | 221 if (TEMP_FAILURE_RETRY( |
| 255 bind(fd, | 222 bind(fd, |
| 256 reinterpret_cast<struct sockaddr*>(&addr), | 223 reinterpret_cast<struct sockaddr *>(&server_address), |
| 257 SocketAddress::GetAddrLength(addr))) < 0) { | 224 sizeof(server_address))) < 0) { |
| 258 TEMP_FAILURE_RETRY(close(fd)); | 225 TEMP_FAILURE_RETRY(close(fd)); |
| 259 return -1; | 226 return -1; |
| 260 } | 227 } |
| 261 | 228 |
| 262 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 229 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 263 TEMP_FAILURE_RETRY(close(fd)); | 230 TEMP_FAILURE_RETRY(close(fd)); |
| 264 return -1; | 231 return -1; |
| 265 } | 232 } |
| 266 | 233 |
| 267 Socket::SetNonBlocking(fd); | 234 Socket::SetNonBlocking(fd); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 291 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 325 int on = enabled ? 1 : 0; | 292 int on = enabled ? 1 : 0; |
| 326 return TEMP_FAILURE_RETRY(setsockopt(fd, | 293 return TEMP_FAILURE_RETRY(setsockopt(fd, |
| 327 SOL_TCP, | 294 SOL_TCP, |
| 328 TCP_NODELAY, | 295 TCP_NODELAY, |
| 329 reinterpret_cast<char *>(&on), | 296 reinterpret_cast<char *>(&on), |
| 330 sizeof(on))) == 0; | 297 sizeof(on))) == 0; |
| 331 } | 298 } |
| 332 | 299 |
| 333 #endif // defined(TARGET_OS_LINUX) | 300 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |