| 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 <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 #include <ifaddrs.h> // NOLINT |
| 15 | 16 |
| 16 #include "bin/fdutils.h" | 17 #include "bin/fdutils.h" |
| 17 #include "bin/file.h" | 18 #include "bin/file.h" |
| 18 #include "bin/log.h" | 19 #include "bin/log.h" |
| 19 #include "bin/socket.h" | 20 #include "bin/socket.h" |
| 20 | 21 |
| 21 | 22 |
| 22 namespace dart { | 23 namespace dart { |
| 23 namespace bin { | 24 namespace bin { |
| 24 | 25 |
| 25 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { | 26 SocketAddress::SocketAddress(struct sockaddr* sockaddr) { |
| 26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 27 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); | 28 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); |
| 28 const char* result = inet_ntop(addrinfo->ai_family, | 29 const char* result = inet_ntop(sockaddr->sa_family, |
| 29 &raw->in.sin_addr, | 30 &raw->in.sin_addr, |
| 30 as_string_, | 31 as_string_, |
| 31 INET6_ADDRSTRLEN); | 32 INET6_ADDRSTRLEN); |
| 32 if (result == NULL) as_string_[0] = 0; | 33 if (result == NULL) as_string_[0] = 0; |
| 33 memmove(reinterpret_cast<void *>(&addr_), | 34 memmove(reinterpret_cast<void *>(&addr_), |
| 34 addrinfo->ai_addr, | 35 sockaddr, |
| 35 addrinfo->ai_addrlen); | 36 GetAddrLength(raw)); |
| 36 } | 37 } |
| 37 | 38 |
| 38 | 39 |
| 39 bool Socket::Initialize() { | 40 bool Socket::Initialize() { |
| 40 // Nothing to do on Android. | 41 // Nothing to do on Android. |
| 41 return true; | 42 return true; |
| 42 } | 43 } |
| 43 | 44 |
| 44 | 45 |
| 45 intptr_t Socket::Create(RawAddr addr) { | 46 intptr_t Socket::Create(RawAddr addr) { |
| 46 intptr_t fd; | 47 intptr_t fd; |
| 47 | 48 |
| 48 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)); |
| 49 if (fd < 0) { | 50 if (fd < 0) { |
| 50 Log::PrintErr("Error Create: %s\n", strerror(errno)); | 51 Log::PrintErr("Error Create: %s\n", strerror(errno)); |
| 51 return -1; | 52 return -1; |
| 52 } | 53 } |
| 53 | 54 |
| 54 FDUtils::SetCloseOnExec(fd); | 55 FDUtils::SetCloseOnExec(fd); |
| 55 return fd; | 56 return fd; |
| 56 } | 57 } |
| 57 | 58 |
| 58 | 59 |
| 59 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { | 60 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { |
| 60 SocketAddress::SetAddrPort(&addr, port); | 61 SocketAddress::SetAddrPort(&addr, port); |
| 61 intptr_t result = TEMP_FAILURE_RETRY( | 62 intptr_t result = TEMP_FAILURE_RETRY( |
| 62 connect(fd, | 63 connect(fd, |
| 63 &addr.addr, | 64 &addr.addr, |
| 64 SocketAddress::GetAddrLength(addr))); | 65 SocketAddress::GetAddrLength(&addr))); |
| 65 if (result == 0 || errno == EINPROGRESS) { | 66 if (result == 0 || errno == EINPROGRESS) { |
| 66 return fd; | 67 return fd; |
| 67 } | 68 } |
| 68 VOID_TEMP_FAILURE_RETRY(close(fd)); | 69 VOID_TEMP_FAILURE_RETRY(close(fd)); |
| 69 return -1; | 70 return -1; |
| 70 } | 71 } |
| 71 | 72 |
| 72 | 73 |
| 73 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { | 74 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { |
| 74 intptr_t fd = Socket::Create(addr); | 75 intptr_t fd = Socket::Create(addr); |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | 215 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 215 addresses->SetAt(i, new SocketAddress(c)); | 216 addresses->SetAt(i, new SocketAddress(c)); |
| 216 i++; | 217 i++; |
| 217 } | 218 } |
| 218 } | 219 } |
| 219 freeaddrinfo(info); | 220 freeaddrinfo(info); |
| 220 return addresses; | 221 return addresses; |
| 221 } | 222 } |
| 222 | 223 |
| 223 | 224 |
| 225 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
| 226 int family = ifa->ifa_addr->sa_family; |
| 227 if (lookup_family == family) return true; |
| 228 if (lookup_family == AF_UNSPEC && |
| 229 (family == AF_INET || family == AF_INET6)) { |
| 230 return true; |
| 231 } |
| 232 return false; |
| 233 } |
| 234 |
| 235 |
| 236 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces( |
| 237 int type, |
| 238 OSError** os_error) { |
| 239 struct ifaddrs* ifaddr; |
| 240 |
| 241 int status = getifaddrs(&ifaddr); |
| 242 if (status != 0) { |
| 243 ASSERT(*os_error == NULL); |
| 244 *os_error = new OSError(status, |
| 245 gai_strerror(status), |
| 246 OSError::kGetAddressInfo); |
| 247 return NULL; |
| 248 } |
| 249 |
| 250 int lookup_family = SocketAddress::FromType(type); |
| 251 |
| 252 intptr_t count = 0; |
| 253 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 254 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++; |
| 255 } |
| 256 |
| 257 AddressList<InterfaceSocketAddress>* addresses = |
| 258 new AddressList<InterfaceSocketAddress>(count); |
| 259 int i = 0; |
| 260 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 261 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) { |
| 262 addresses->SetAt(i, new InterfaceSocketAddress( |
| 263 ifa->ifa_addr, strdup(ifa->ifa_name))); |
| 264 i++; |
| 265 } |
| 266 } |
| 267 freeifaddrs(ifaddr); |
| 268 return addresses; |
| 269 } |
| 270 |
| 271 |
| 224 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 272 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
| 225 intptr_t port, | 273 intptr_t port, |
| 226 intptr_t backlog, | 274 intptr_t backlog, |
| 227 bool v6_only) { | 275 bool v6_only) { |
| 228 intptr_t fd; | 276 intptr_t fd; |
| 229 | 277 |
| 230 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 278 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 231 if (fd < 0) return -1; | 279 if (fd < 0) return -1; |
| 232 | 280 |
| 233 FDUtils::SetCloseOnExec(fd); | 281 FDUtils::SetCloseOnExec(fd); |
| 234 | 282 |
| 235 int optval = 1; | 283 int optval = 1; |
| 236 TEMP_FAILURE_RETRY( | 284 TEMP_FAILURE_RETRY( |
| 237 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 285 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 238 | 286 |
| 239 if (addr.ss.ss_family == AF_INET6) { | 287 if (addr.ss.ss_family == AF_INET6) { |
| 240 optval = v6_only ? 1 : 0; | 288 optval = v6_only ? 1 : 0; |
| 241 TEMP_FAILURE_RETRY( | 289 TEMP_FAILURE_RETRY( |
| 242 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); | 290 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 243 } | 291 } |
| 244 | 292 |
| 245 SocketAddress::SetAddrPort(&addr, port); | 293 SocketAddress::SetAddrPort(&addr, port); |
| 246 if (TEMP_FAILURE_RETRY( | 294 if (TEMP_FAILURE_RETRY( |
| 247 bind(fd, | 295 bind(fd, |
| 248 &addr.addr, | 296 &addr.addr, |
| 249 SocketAddress::GetAddrLength(addr))) < 0) { | 297 SocketAddress::GetAddrLength(&addr))) < 0) { |
| 250 TEMP_FAILURE_RETRY(close(fd)); | 298 TEMP_FAILURE_RETRY(close(fd)); |
| 251 return -1; | 299 return -1; |
| 252 } | 300 } |
| 253 | 301 |
| 254 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 302 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 255 TEMP_FAILURE_RETRY(close(fd)); | 303 TEMP_FAILURE_RETRY(close(fd)); |
| 256 return -1; | 304 return -1; |
| 257 } | 305 } |
| 258 | 306 |
| 259 Socket::SetNonBlocking(fd); | 307 Socket::SetNonBlocking(fd); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 SOL_TCP, | 367 SOL_TCP, |
| 320 TCP_NODELAY, | 368 TCP_NODELAY, |
| 321 reinterpret_cast<char *>(&on), | 369 reinterpret_cast<char *>(&on), |
| 322 sizeof(on))) == 0; | 370 sizeof(on))) == 0; |
| 323 } | 371 } |
| 324 | 372 |
| 325 } // namespace bin | 373 } // namespace bin |
| 326 } // namespace dart | 374 } // namespace dart |
| 327 | 375 |
| 328 #endif // defined(TARGET_OS_ANDROID) | 376 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |