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