| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifdef HAVE_CONFIG_H | 11 #ifdef HAVE_CONFIG_H |
| 12 #include "config.h" | 12 #include "config.h" |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include "webrtc/base/network.h" | 15 #include "webrtc/base/network.h" |
| 16 | 16 |
| 17 #if defined(WEBRTC_POSIX) | 17 #if defined(WEBRTC_POSIX) |
| 18 // linux/if.h can't be included at the same time as the posix sys/if.h, and | 18 // linux/if.h can't be included at the same time as the posix sys/if.h, and |
| 19 // it's transitively required by linux/route.h, so include that version on | 19 // it's transitively required by linux/route.h, so include that version on |
| 20 // linux instead of the standard posix one. | 20 // linux instead of the standard posix one. |
| 21 #if defined(WEBRTC_LINUX) | 21 #if defined(WEBRTC_LINUX) |
| 22 #include <linux/if.h> | 22 #include <linux/if.h> |
| 23 #include <linux/route.h> | 23 #include <linux/route.h> |
| 24 #elif !defined(__native_client__) | 24 #elif !defined(__native_client__) |
| 25 #include <net/if.h> | 25 #include <net/if.h> |
| 26 #endif | 26 #endif |
| 27 #include <sys/socket.h> | |
| 28 #include <sys/utsname.h> | |
| 29 #include <sys/ioctl.h> | |
| 30 #include <unistd.h> | |
| 31 #include <errno.h> | |
| 32 | |
| 33 #if defined(WEBRTC_ANDROID) | |
| 34 #include "webrtc/base/ifaddrs-android.h" | |
| 35 #elif !defined(__native_client__) | |
| 36 #include <ifaddrs.h> | |
| 37 #endif | |
| 38 | |
| 39 #endif // WEBRTC_POSIX | 27 #endif // WEBRTC_POSIX |
| 40 | 28 |
| 41 #if defined(WEBRTC_WIN) | 29 #if defined(WEBRTC_WIN) |
| 42 #include "webrtc/base/win32.h" | 30 #include "webrtc/base/win32.h" |
| 43 #include <Iphlpapi.h> | 31 #include <Iphlpapi.h> |
| 32 #elif !defined(__native_client__) |
| 33 #include "webrtc/base/ifaddrs_converter.h" |
| 44 #endif | 34 #endif |
| 45 | 35 |
| 46 #include <stdio.h> | 36 #include <stdio.h> |
| 47 | 37 |
| 48 #include <algorithm> | 38 #include <algorithm> |
| 49 | 39 |
| 50 #include "webrtc/base/logging.h" | 40 #include "webrtc/base/logging.h" |
| 51 #include "webrtc/base/networkmonitor.h" | 41 #include "webrtc/base/networkmonitor.h" |
| 52 #include "webrtc/base/scoped_ptr.h" | 42 #include "webrtc/base/scoped_ptr.h" |
| 53 #include "webrtc/base/socket.h" // includes something that makes windows happy | 43 #include "webrtc/base/socket.h" // includes something that makes windows happy |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 case ADAPTER_TYPE_VPN: | 112 case ADAPTER_TYPE_VPN: |
| 123 return "VPN"; | 113 return "VPN"; |
| 124 case ADAPTER_TYPE_LOOPBACK: | 114 case ADAPTER_TYPE_LOOPBACK: |
| 125 return "Loopback"; | 115 return "Loopback"; |
| 126 default: | 116 default: |
| 127 RTC_DCHECK(false) << "Invalid type " << type; | 117 RTC_DCHECK(false) << "Invalid type " << type; |
| 128 return std::string(); | 118 return std::string(); |
| 129 } | 119 } |
| 130 } | 120 } |
| 131 | 121 |
| 132 bool IsIgnoredIPv6(const IPAddress& ip) { | 122 bool IsIgnoredIPv6(const InterfaceAddress& ip) { |
| 133 if (ip.family() != AF_INET6) { | 123 if (ip.family() != AF_INET6) { |
| 134 return false; | 124 return false; |
| 135 } | 125 } |
| 136 | 126 |
| 137 // Link-local addresses require scope id to be bound successfully. | 127 // Link-local addresses require scope id to be bound successfully. |
| 138 // However, our IPAddress structure doesn't carry that so the | 128 // However, our IPAddress structure doesn't carry that so the |
| 139 // information is lost and causes binding failure. | 129 // information is lost and causes binding failure. |
| 140 if (IPIsLinkLocal(ip)) { | 130 if (IPIsLinkLocal(ip)) { |
| 141 return true; | 131 return true; |
| 142 } | 132 } |
| 143 | 133 |
| 144 // Any MAC based IPv6 should be avoided to prevent the MAC tracking. | 134 // Any MAC based IPv6 should be avoided to prevent the MAC tracking. |
| 145 if (IPIsMacBased(ip)) { | 135 if (IPIsMacBased(ip)) { |
| 146 return true; | 136 return true; |
| 147 } | 137 } |
| 148 | 138 |
| 139 // Ignore deprecated IPv6. |
| 140 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) { |
| 141 return true; |
| 142 } |
| 143 |
| 149 return false; | 144 return false; |
| 150 } | 145 } |
| 151 | 146 |
| 152 } // namespace | 147 } // namespace |
| 153 | 148 |
| 154 // These addresses are used as the targets to find out the default local address | 149 // These addresses are used as the targets to find out the default local address |
| 155 // on a multi-homed endpoint. They are actually DNS servers. | 150 // on a multi-homed endpoint. They are actually DNS servers. |
| 156 const char kPublicIPv4Host[] = "8.8.8.8"; | 151 const char kPublicIPv4Host[] = "8.8.8.8"; |
| 157 const char kPublicIPv6Host[] = "2001:4860:4860::8888"; | 152 const char kPublicIPv6Host[] = "2001:4860:4860::8888"; |
| 158 const int kPublicPort = 53; // DNS port. | 153 const int kPublicPort = 53; // DNS port. |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 | 381 |
| 387 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 382 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 388 NetworkList* networks) const { | 383 NetworkList* networks) const { |
| 389 ASSERT(false); | 384 ASSERT(false); |
| 390 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; | 385 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; |
| 391 return false; | 386 return false; |
| 392 } | 387 } |
| 393 | 388 |
| 394 #elif defined(WEBRTC_POSIX) | 389 #elif defined(WEBRTC_POSIX) |
| 395 void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces, | 390 void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces, |
| 391 IfAddrsConverter* ifaddrs_converter, |
| 396 bool include_ignored, | 392 bool include_ignored, |
| 397 NetworkList* networks) const { | 393 NetworkList* networks) const { |
| 398 NetworkMap current_networks; | 394 NetworkMap current_networks; |
| 395 |
| 399 for (struct ifaddrs* cursor = interfaces; | 396 for (struct ifaddrs* cursor = interfaces; |
| 400 cursor != NULL; cursor = cursor->ifa_next) { | 397 cursor != NULL; cursor = cursor->ifa_next) { |
| 401 IPAddress prefix; | 398 IPAddress prefix; |
| 402 IPAddress mask; | 399 IPAddress mask; |
| 403 IPAddress ip; | 400 InterfaceAddress ip; |
| 404 int scope_id = 0; | 401 int scope_id = 0; |
| 405 | 402 |
| 406 // Some interfaces may not have address assigned. | 403 // Some interfaces may not have address assigned. |
| 407 if (!cursor->ifa_addr || !cursor->ifa_netmask) | 404 if (!cursor->ifa_addr || !cursor->ifa_netmask) { |
| 408 continue; | 405 continue; |
| 406 } |
| 407 // Skip ones which are down. |
| 408 if (!(cursor->ifa_flags & IFF_RUNNING)) { |
| 409 continue; |
| 410 } |
| 411 // Skip unknown family. |
| 412 if (cursor->ifa_addr->sa_family != AF_INET && |
| 413 cursor->ifa_addr->sa_family != AF_INET6) { |
| 414 continue; |
| 415 } |
| 416 // Skip IPv6 if not enabled. |
| 417 if (cursor->ifa_addr->sa_family == AF_INET6 && !ipv6_enabled()) { |
| 418 continue; |
| 419 } |
| 420 // Convert to InterfaceAddress. |
| 421 if (!ifaddrs_converter->ConvertIfAddrsToIPAddress(cursor, &ip, &mask)) { |
| 422 continue; |
| 423 } |
| 409 | 424 |
| 410 switch (cursor->ifa_addr->sa_family) { | 425 // Special case for IPv6 address. |
| 411 case AF_INET: { | 426 if (cursor->ifa_addr->sa_family == AF_INET6) { |
| 412 ip = IPAddress( | 427 if (IsIgnoredIPv6(ip)) { |
| 413 reinterpret_cast<sockaddr_in*>(cursor->ifa_addr)->sin_addr); | |
| 414 mask = IPAddress( | |
| 415 reinterpret_cast<sockaddr_in*>(cursor->ifa_netmask)->sin_addr); | |
| 416 break; | |
| 417 } | |
| 418 case AF_INET6: { | |
| 419 if (ipv6_enabled()) { | |
| 420 ip = IPAddress( | |
| 421 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_addr); | |
| 422 | |
| 423 if (IsIgnoredIPv6(ip)) { | |
| 424 continue; | |
| 425 } | |
| 426 | |
| 427 mask = IPAddress( | |
| 428 reinterpret_cast<sockaddr_in6*>(cursor->ifa_netmask)->sin6_addr); | |
| 429 scope_id = | |
| 430 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id; | |
| 431 break; | |
| 432 } else { | |
| 433 continue; | |
| 434 } | |
| 435 } | |
| 436 default: { | |
| 437 continue; | 428 continue; |
| 438 } | 429 } |
| 430 scope_id = |
| 431 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id; |
| 439 } | 432 } |
| 440 | 433 |
| 441 int prefix_length = CountIPMaskBits(mask); | 434 int prefix_length = CountIPMaskBits(mask); |
| 442 prefix = TruncateIP(ip, prefix_length); | 435 prefix = TruncateIP(ip, prefix_length); |
| 443 std::string key = MakeNetworkKey(std::string(cursor->ifa_name), | 436 std::string key = MakeNetworkKey(std::string(cursor->ifa_name), |
| 444 prefix, prefix_length); | 437 prefix, prefix_length); |
| 445 auto existing_network = current_networks.find(key); | 438 auto existing_network = current_networks.find(key); |
| 446 if (existing_network == current_networks.end()) { | 439 if (existing_network == current_networks.end()) { |
| 447 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; | 440 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
| 448 if (cursor->ifa_flags & IFF_LOOPBACK) { | 441 if (cursor->ifa_flags & IFF_LOOPBACK) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 474 | 467 |
| 475 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 468 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 476 NetworkList* networks) const { | 469 NetworkList* networks) const { |
| 477 struct ifaddrs* interfaces; | 470 struct ifaddrs* interfaces; |
| 478 int error = getifaddrs(&interfaces); | 471 int error = getifaddrs(&interfaces); |
| 479 if (error != 0) { | 472 if (error != 0) { |
| 480 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; | 473 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; |
| 481 return false; | 474 return false; |
| 482 } | 475 } |
| 483 | 476 |
| 484 ConvertIfAddrs(interfaces, include_ignored, networks); | 477 rtc::scoped_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter()); |
| 478 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored, |
| 479 networks); |
| 485 | 480 |
| 486 freeifaddrs(interfaces); | 481 freeifaddrs(interfaces); |
| 487 return true; | 482 return true; |
| 488 } | 483 } |
| 489 | 484 |
| 490 #elif defined(WEBRTC_WIN) | 485 #elif defined(WEBRTC_WIN) |
| 491 | 486 |
| 492 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, | 487 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, |
| 493 const IPAddress& ip, IPAddress* prefix) { | 488 const IPAddress& ip, IPAddress* prefix) { |
| 494 IPAddress current_prefix; | 489 IPAddress current_prefix; |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 919 std::stringstream ss; | 914 std::stringstream ss; |
| 920 // Print out the first space-terminated token of the network desc, plus | 915 // Print out the first space-terminated token of the network desc, plus |
| 921 // the IP address. | 916 // the IP address. |
| 922 ss << "Net[" << description_.substr(0, description_.find(' ')) | 917 ss << "Net[" << description_.substr(0, description_.find(' ')) |
| 923 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 918 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
| 924 << ":" << AdapterTypeToString(type_) << "]"; | 919 << ":" << AdapterTypeToString(type_) << "]"; |
| 925 return ss.str(); | 920 return ss.str(); |
| 926 } | 921 } |
| 927 | 922 |
| 928 } // namespace rtc | 923 } // namespace rtc |
| OLD | NEW |