| 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 #include <ifaddrs.h> // NOLINT |
| 16 #include <net/if.h> // NOLINT |
| 15 | 17 |
| 16 #include "bin/fdutils.h" | 18 #include "bin/fdutils.h" |
| 17 #include "bin/file.h" | 19 #include "bin/file.h" |
| 18 #include "bin/log.h" | 20 #include "bin/log.h" |
| 19 #include "bin/socket.h" | 21 #include "bin/socket.h" |
| 20 | 22 |
| 21 | 23 |
| 22 namespace dart { | 24 namespace dart { |
| 23 namespace bin { | 25 namespace bin { |
| 24 | 26 |
| 25 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { | 27 SocketAddress::SocketAddress(struct sockaddr* sockaddr) { |
| 26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 28 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 27 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); | 29 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr); |
| 28 const char* result = inet_ntop(addrinfo->ai_family, | 30 const char* result = inet_ntop(sockaddr->sa_family, |
| 29 &raw->in.sin_addr, | 31 &raw->in.sin_addr, |
| 30 as_string_, | 32 as_string_, |
| 31 INET6_ADDRSTRLEN); | 33 INET6_ADDRSTRLEN); |
| 32 if (result == NULL) as_string_[0] = 0; | 34 if (result == NULL) as_string_[0] = 0; |
| 33 memmove(reinterpret_cast<void *>(&addr_), | 35 memmove(reinterpret_cast<void *>(&addr_), |
| 34 addrinfo->ai_addr, | 36 sockaddr, |
| 35 addrinfo->ai_addrlen); | 37 GetAddrLength(*raw)); |
| 36 } | 38 } |
| 37 | 39 |
| 38 | 40 |
| 39 bool Socket::Initialize() { | 41 bool Socket::Initialize() { |
| 40 // Nothing to do on Linux. | 42 // Nothing to do on Linux. |
| 41 return true; | 43 return true; |
| 42 } | 44 } |
| 43 | 45 |
| 44 | 46 |
| 45 intptr_t Socket::Create(RawAddr addr) { | 47 intptr_t Socket::Create(RawAddr addr) { |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 *os_error = new OSError(status, | 203 *os_error = new OSError(status, |
| 202 gai_strerror(status), | 204 gai_strerror(status), |
| 203 OSError::kGetAddressInfo); | 205 OSError::kGetAddressInfo); |
| 204 return NULL; | 206 return NULL; |
| 205 } | 207 } |
| 206 intptr_t count = 0; | 208 intptr_t count = 0; |
| 207 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 209 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 208 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; | 210 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; |
| 209 } | 211 } |
| 210 SocketAddresses* addresses = new SocketAddresses(count); | 212 SocketAddresses* addresses = new SocketAddresses(count); |
| 211 intptr_t i = 0; | |
| 212 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | 213 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | 214 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 214 addresses->SetAt(i, new SocketAddress(c)); | 215 addresses->SetAt(i, new SocketAddress(c->ai_addr)); |
| 215 i++; | |
| 216 } | 216 } |
| 217 } | 217 } |
| 218 freeaddrinfo(info); | 218 freeaddrinfo(info); |
| 219 return addresses; | 219 return addresses; |
| 220 } | 220 } |
| 221 | 221 |
| 222 | 222 |
| 223 SocketAddresses* Socket::ListAdaptors(bool include_loopback, |
| 224 int type, |
| 225 OSError** os_error) { |
| 226 struct ifaddrs* ifaddr; |
| 227 |
| 228 int status = getifaddrs(&ifaddr); |
| 229 if (status != 0) { |
| 230 ASSERT(*os_error == NULL); |
| 231 *os_error = new OSError(status, |
| 232 gai_strerror(status), |
| 233 OSError::kGetAddressInfo); |
| 234 return NULL; |
| 235 } |
| 236 |
| 237 int lookup_family = SocketAddress::FromType(type); |
| 238 |
| 239 intptr_t count = 0; |
| 240 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 241 int family = ifa->ifa_addr->sa_family; |
| 242 if ((lookup_family == family || |
| 243 (lookup_family == AF_UNSPEC && |
| 244 (family == AF_INET || family == AF_INET6))) && |
| 245 (include_loopback || (ifa->ifa_flags & IFF_LOOPBACK) == 0)) { |
| 246 count++; |
| 247 } |
| 248 } |
| 249 |
| 250 SocketAddresses* addresses = new SocketAddresses(count); |
| 251 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| 252 int family = ifa->ifa_addr->sa_family; |
| 253 if ((lookup_family == family || |
| 254 (lookup_family == AF_UNSPEC && |
| 255 (family == AF_INET || family == AF_INET6))) && |
| 256 (include_loopback || (ifa->ifa_flags & IFF_LOOPBACK) == 0)) { |
| 257 addresses->SetAt(i, new AdaptorSocketAddress( |
| 258 ifa->ifa_addr, strdup(ifa->ifa_name))); |
| 259 } |
| 260 } |
| 261 freeifaddrs(ifaddr); |
| 262 return addresses; |
| 263 } |
| 264 |
| 265 |
| 223 intptr_t ServerSocket::CreateBindListen(RawAddr addr, | 266 intptr_t ServerSocket::CreateBindListen(RawAddr addr, |
| 224 intptr_t port, | 267 intptr_t port, |
| 225 intptr_t backlog, | 268 intptr_t backlog, |
| 226 bool v6_only) { | 269 bool v6_only) { |
| 227 intptr_t fd; | 270 intptr_t fd; |
| 228 | 271 |
| 229 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 272 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
| 230 if (fd < 0) return -1; | 273 if (fd < 0) return -1; |
| 231 | 274 |
| 232 FDUtils::SetCloseOnExec(fd); | 275 FDUtils::SetCloseOnExec(fd); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 SOL_TCP, | 361 SOL_TCP, |
| 319 TCP_NODELAY, | 362 TCP_NODELAY, |
| 320 reinterpret_cast<char *>(&on), | 363 reinterpret_cast<char *>(&on), |
| 321 sizeof(on))) == 0; | 364 sizeof(on))) == 0; |
| 322 } | 365 } |
| 323 | 366 |
| 324 } // namespace bin | 367 } // namespace bin |
| 325 } // namespace dart | 368 } // namespace dart |
| 326 | 369 |
| 327 #endif // defined(TARGET_OS_LINUX) | 370 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |