| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
| 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 <unistd.h> // NOLINT | 12 #include <unistd.h> // NOLINT |
| 13 #include <netinet/tcp.h> // NOLINT | 13 #include <netinet/tcp.h> // NOLINT |
| 14 | 14 |
| 15 #include "bin/socket.h" | 15 #include "bin/socket.h" |
| 16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
| 17 #include "bin/log.h" | 17 #include "bin/log.h" |
| 18 | 18 |
| 19 | 19 |
| 20 #define SOCKADDR_STORAGE_SET_PORT(addr, port) \ |
| 21 if (addr.ss_family == AF_INET) { \ |
| 22 reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port = htons(port); \ |
| 23 } else { \ |
| 24 reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port = htons(port); \ |
| 25 } |
| 26 |
| 27 |
| 28 #define SOCKADDR_STORAGE_GET_PORT(addr) \ |
| 29 addr.ss_family == AF_INET ? \ |
| 30 ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \ |
| 31 ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port) |
| 32 |
| 33 |
| 34 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { |
| 35 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 36 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr); |
| 37 const char* result = inet_ntop(addrinfo->ai_family, |
| 38 &sockaddr->sin_addr, |
| 39 as_string_, |
| 40 INET6_ADDRSTRLEN); |
| 41 if (result == NULL) as_string_[0] = 0; |
| 42 memmove(reinterpret_cast<void *>(&addr_), |
| 43 addrinfo->ai_addr, |
| 44 addrinfo->ai_addrlen); |
| 45 } |
| 46 |
| 47 |
| 20 bool Socket::Initialize() { | 48 bool Socket::Initialize() { |
| 21 // Nothing to do on Android. | 49 // Nothing to do on Android. |
| 22 return true; | 50 return true; |
| 23 } | 51 } |
| 24 | 52 |
| 25 | 53 |
| 26 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 54 intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) { |
| 27 intptr_t fd; | 55 intptr_t fd; |
| 28 struct hostent* server; | |
| 29 struct sockaddr_in server_address; | |
| 30 | 56 |
| 31 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 57 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
| 32 if (fd < 0) { | 58 if (fd < 0) { |
| 33 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 59 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 34 return -1; | 60 return -1; |
| 35 } | 61 } |
| 36 | 62 |
| 37 FDUtils::SetCloseOnExec(fd); | 63 FDUtils::SetCloseOnExec(fd); |
| 38 Socket::SetNonBlocking(fd); | 64 Socket::SetNonBlocking(fd); |
| 39 | 65 |
| 40 server = gethostbyname(host); | 66 SOCKADDR_STORAGE_SET_PORT(addr, port); |
| 41 if (server == NULL) { | |
| 42 TEMP_FAILURE_RETRY(close(fd)); | |
| 43 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | |
| 44 return -1; | |
| 45 } | |
| 46 | |
| 47 server_address.sin_family = AF_INET; | |
| 48 server_address.sin_port = htons(port); | |
| 49 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); | |
| 50 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | |
| 51 intptr_t result = TEMP_FAILURE_RETRY( | 67 intptr_t result = TEMP_FAILURE_RETRY( |
| 52 connect(fd, | 68 connect(fd, |
| 53 reinterpret_cast<struct sockaddr *>(&server_address), | 69 reinterpret_cast<struct sockaddr*>(&addr), |
| 54 sizeof(server_address))); | 70 SocketAddress::GetAddrLength(addr))); |
| 55 if (result == 0 || errno == EINPROGRESS) { | 71 if (result == 0 || errno == EINPROGRESS) { |
| 56 return fd; | 72 return fd; |
| 57 } | 73 } |
| 74 TEMP_FAILURE_RETRY(close(fd)); |
| 58 return -1; | 75 return -1; |
| 59 } | 76 } |
| 60 | 77 |
| 61 | 78 |
| 62 intptr_t Socket::Available(intptr_t fd) { | 79 intptr_t Socket::Available(intptr_t fd) { |
| 63 return FDUtils::AvailableBytes(fd); | 80 return FDUtils::AvailableBytes(fd); |
| 64 } | 81 } |
| 65 | 82 |
| 66 | 83 |
| 67 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 84 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 85 // If the would block we need to retry and therefore return 0 as | 102 // If the would block we need to retry and therefore return 0 as |
| 86 // the number of bytes written. | 103 // the number of bytes written. |
| 87 written_bytes = 0; | 104 written_bytes = 0; |
| 88 } | 105 } |
| 89 return written_bytes; | 106 return written_bytes; |
| 90 } | 107 } |
| 91 | 108 |
| 92 | 109 |
| 93 intptr_t Socket::GetPort(intptr_t fd) { | 110 intptr_t Socket::GetPort(intptr_t fd) { |
| 94 ASSERT(fd >= 0); | 111 ASSERT(fd >= 0); |
| 95 struct sockaddr_in socket_address; | 112 struct sockaddr_storage socket_address; |
| 96 socklen_t size = sizeof(socket_address); | 113 socklen_t size = sizeof(socket_address); |
| 97 if (TEMP_FAILURE_RETRY( | 114 if (TEMP_FAILURE_RETRY( |
| 98 getsockname(fd, | 115 getsockname(fd, |
| 99 reinterpret_cast<struct sockaddr *>(&socket_address), | 116 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 100 &size))) { | 117 &size))) { |
| 101 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); | 118 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
| 102 return 0; | 119 return 0; |
| 103 } | 120 } |
| 104 return ntohs(socket_address.sin_port); | 121 return SOCKADDR_STORAGE_GET_PORT(socket_address); |
| 105 } | 122 } |
| 106 | 123 |
| 107 | 124 |
| 108 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 125 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
| 109 ASSERT(fd >= 0); | 126 ASSERT(fd >= 0); |
| 110 struct sockaddr_in socket_address; | 127 struct sockaddr_storage socket_address; |
| 111 socklen_t size = sizeof(socket_address); | 128 socklen_t size = sizeof(socket_address); |
| 112 if (TEMP_FAILURE_RETRY( | 129 if (TEMP_FAILURE_RETRY( |
| 113 getpeername(fd, | 130 getpeername(fd, |
| 114 reinterpret_cast<struct sockaddr *>(&socket_address), | 131 reinterpret_cast<struct sockaddr *>(&socket_address), |
| 115 &size))) { | 132 &size))) { |
| 116 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 133 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
| 117 return false; | 134 return false; |
| 118 } | 135 } |
| 119 if (inet_ntop(socket_address.sin_family, | 136 const void* src; |
| 120 reinterpret_cast<const void *>(&socket_address.sin_addr), | 137 if (socket_address.ss_family == AF_INET6) { |
| 138 const sockaddr_in6* in6 = |
| 139 reinterpret_cast<const sockaddr_in6*>(&socket_address); |
| 140 src = reinterpret_cast<const void*>(&in6->sin6_addr); |
| 141 } else { |
| 142 const sockaddr_in* in = |
| 143 reinterpret_cast<const sockaddr_in*>(&socket_address); |
| 144 src = reinterpret_cast<const void*>(&in->sin_addr); |
| 145 } |
| 146 if (inet_ntop(socket_address.ss_family, |
| 147 src, |
| 121 host, | 148 host, |
| 122 INET_ADDRSTRLEN) == NULL) { | 149 INET_ADDRSTRLEN) == NULL) { |
| 123 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); | 150 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
| 124 return false; | 151 return false; |
| 125 } | 152 } |
| 126 *port = ntohs(socket_address.sin_port); | 153 *port = SOCKADDR_STORAGE_GET_PORT(socket_address); |
| 127 return true; | 154 return true; |
| 128 } | 155 } |
| 129 | 156 |
| 130 | 157 |
| 131 void Socket::GetError(intptr_t fd, OSError* os_error) { | 158 void Socket::GetError(intptr_t fd, OSError* os_error) { |
| 132 int errorNumber; | 159 int errorNumber; |
| 133 socklen_t len = sizeof(errorNumber); | 160 socklen_t len = sizeof(errorNumber); |
| 134 getsockopt(fd, | 161 getsockopt(fd, |
| 135 SOL_SOCKET, | 162 SOL_SOCKET, |
| 136 SO_ERROR, | 163 SO_ERROR, |
| 137 reinterpret_cast<void*>(&errorNumber), | 164 reinterpret_cast<void*>(&errorNumber), |
| 138 &len); | 165 &len); |
| 139 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); | 166 os_error->SetCodeAndMessage(OSError::kSystem, errorNumber); |
| 140 } | 167 } |
| 141 | 168 |
| 142 | 169 |
| 143 intptr_t Socket::GetStdioHandle(int num) { | 170 intptr_t Socket::GetStdioHandle(int num) { |
| 144 return static_cast<intptr_t>(num); | 171 return static_cast<intptr_t>(num); |
| 145 } | 172 } |
| 146 | 173 |
| 147 | 174 |
| 148 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | 175 SocketAddresses* Socket::LookupAddress(const char* host, |
| 149 // Perform a name lookup for an IPv4 address. | 176 int type, |
| 177 OSError** os_error) { |
| 178 // Perform a name lookup for a host name. |
| 150 struct addrinfo hints; | 179 struct addrinfo hints; |
| 151 memset(&hints, 0, sizeof(hints)); | 180 memset(&hints, 0, sizeof(hints)); |
| 152 hints.ai_family = AF_INET; | 181 hints.ai_family = SocketAddress::FromType(type); |
| 153 hints.ai_socktype = SOCK_STREAM; | 182 hints.ai_socktype = SOCK_STREAM; |
| 183 hints.ai_flags = 0; |
| 154 hints.ai_protocol = IPPROTO_TCP; | 184 hints.ai_protocol = IPPROTO_TCP; |
| 155 struct addrinfo* info = NULL; | 185 struct addrinfo* info = NULL; |
| 156 int status = getaddrinfo(host, 0, &hints, &info); | 186 int status = getaddrinfo(host, 0, &hints, &info); |
| 157 if (status != 0) { | 187 if (status != 0) { |
| 158 ASSERT(*os_error == NULL); | 188 ASSERT(*os_error == NULL); |
| 159 *os_error = new OSError(status, | 189 *os_error = new OSError(status, |
| 160 gai_strerror(status), | 190 gai_strerror(status), |
| 161 OSError::kGetAddressInfo); | 191 OSError::kGetAddressInfo); |
| 162 return NULL; | 192 return NULL; |
| 163 } | 193 } |
| 164 // Convert the address into IPv4 dotted decimal notation. | 194 intptr_t count = 0; |
| 165 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | 195 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 166 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | 196 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; |
| 167 const char* result = inet_ntop(AF_INET, | |
| 168 reinterpret_cast<void *>(&sockaddr->sin_addr), | |
| 169 buffer, | |
| 170 INET_ADDRSTRLEN); | |
| 171 if (result == NULL) { | |
| 172 free(buffer); | |
| 173 return NULL; | |
| 174 } | 197 } |
| 175 ASSERT(result == buffer); | 198 SocketAddresses* addresses = new SocketAddresses(count); |
| 176 return buffer; | 199 intptr_t i = 0; |
| 200 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 201 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 202 addresses->SetAt(i, new SocketAddress(c)); |
| 203 i++; |
| 204 } |
| 205 } |
| 206 freeaddrinfo(info); |
| 207 return addresses; |
| 177 } | 208 } |
| 178 | 209 |
| 179 | 210 |
| 180 intptr_t ServerSocket::CreateBindListen(const char* host, | 211 intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr, |
| 181 intptr_t port, | 212 intptr_t port, |
| 182 intptr_t backlog) { | 213 intptr_t backlog) { |
| 183 intptr_t fd; | 214 intptr_t fd; |
| 184 struct sockaddr_in server_address; | |
| 185 | 215 |
| 186 in_addr_t s_addr = inet_addr(host); | 216 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
| 187 if (s_addr == INADDR_NONE) { | 217 if (fd < 0) return -1; |
| 188 return -5; | |
| 189 } | |
| 190 | |
| 191 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | |
| 192 if (fd < 0) { | |
| 193 Log::PrintErr("Error CreateBind: %s\n", strerror(errno)); | |
| 194 return -1; | |
| 195 } | |
| 196 | 218 |
| 197 FDUtils::SetCloseOnExec(fd); | 219 FDUtils::SetCloseOnExec(fd); |
| 198 | 220 |
| 199 int optval = 1; | 221 int optval = 1; |
| 200 TEMP_FAILURE_RETRY( | 222 TEMP_FAILURE_RETRY( |
| 201 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 223 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 202 | 224 |
| 203 server_address.sin_family = AF_INET; | 225 if (addr.ss_family == AF_INET6) { |
| 204 server_address.sin_port = htons(port); | 226 optval = 0; |
| 205 server_address.sin_addr.s_addr = s_addr; | 227 TEMP_FAILURE_RETRY( |
| 206 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 228 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 229 } |
| 207 | 230 |
| 231 SOCKADDR_STORAGE_SET_PORT(addr, port); |
| 208 if (TEMP_FAILURE_RETRY( | 232 if (TEMP_FAILURE_RETRY( |
| 209 bind(fd, | 233 bind(fd, |
| 210 reinterpret_cast<struct sockaddr *>(&server_address), | 234 reinterpret_cast<struct sockaddr*>(&addr), |
| 211 sizeof(server_address))) < 0) { | 235 SocketAddress::GetAddrLength(addr))) < 0) { |
| 212 TEMP_FAILURE_RETRY(close(fd)); | 236 TEMP_FAILURE_RETRY(close(fd)); |
| 213 Log::PrintErr("Error Bind: %s\n", strerror(errno)); | |
| 214 return -1; | 237 return -1; |
| 215 } | 238 } |
| 216 | 239 |
| 217 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { | 240 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 218 Log::PrintErr("Error Listen: %s\n", strerror(errno)); | 241 TEMP_FAILURE_RETRY(close(fd)); |
| 219 return -1; | 242 return -1; |
| 220 } | 243 } |
| 221 | 244 |
| 222 Socket::SetNonBlocking(fd); | 245 Socket::SetNonBlocking(fd); |
| 223 return fd; | 246 return fd; |
| 224 } | 247 } |
| 225 | 248 |
| 226 | 249 |
| 227 static bool IsTemporaryAcceptError(int error) { | 250 static bool IsTemporaryAcceptError(int error) { |
| 228 // On Android a number of protocol errors should be treated as EAGAIN. | 251 // On Android a number of protocol errors should be treated as EAGAIN. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 302 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 280 int on = enabled ? 1 : 0; | 303 int on = enabled ? 1 : 0; |
| 281 return TEMP_FAILURE_RETRY(setsockopt(fd, | 304 return TEMP_FAILURE_RETRY(setsockopt(fd, |
| 282 SOL_TCP, | 305 SOL_TCP, |
| 283 TCP_NODELAY, | 306 TCP_NODELAY, |
| 284 reinterpret_cast<char *>(&on), | 307 reinterpret_cast<char *>(&on), |
| 285 sizeof(on))) == 0; | 308 sizeof(on))) == 0; |
| 286 } | 309 } |
| 287 | 310 |
| 288 #endif // defined(TARGET_OS_ANDROID) | 311 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |