| 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 |
| 27 #endif // WEBRTC_POSIX | 39 #endif // WEBRTC_POSIX |
| 28 | 40 |
| 29 #if defined(WEBRTC_WIN) | 41 #if defined(WEBRTC_WIN) |
| 30 #include "webrtc/base/win32.h" | 42 #include "webrtc/base/win32.h" |
| 31 #include <Iphlpapi.h> | 43 #include <Iphlpapi.h> |
| 32 #elif !defined(__native_client__) | |
| 33 #include "webrtc/base/ifaddrs_converter.h" | |
| 34 #endif | 44 #endif |
| 35 | 45 |
| 36 #include <stdio.h> | 46 #include <stdio.h> |
| 37 | 47 |
| 38 #include <algorithm> | 48 #include <algorithm> |
| 39 | 49 |
| 40 #include "webrtc/base/logging.h" | 50 #include "webrtc/base/logging.h" |
| 41 #include "webrtc/base/networkmonitor.h" | 51 #include "webrtc/base/networkmonitor.h" |
| 42 #include "webrtc/base/scoped_ptr.h" | 52 #include "webrtc/base/scoped_ptr.h" |
| 43 #include "webrtc/base/socket.h" // includes something that makes windows happy | 53 #include "webrtc/base/socket.h" // includes something that makes windows happy |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 case ADAPTER_TYPE_VPN: | 122 case ADAPTER_TYPE_VPN: |
| 113 return "VPN"; | 123 return "VPN"; |
| 114 case ADAPTER_TYPE_LOOPBACK: | 124 case ADAPTER_TYPE_LOOPBACK: |
| 115 return "Loopback"; | 125 return "Loopback"; |
| 116 default: | 126 default: |
| 117 RTC_DCHECK(false) << "Invalid type " << type; | 127 RTC_DCHECK(false) << "Invalid type " << type; |
| 118 return std::string(); | 128 return std::string(); |
| 119 } | 129 } |
| 120 } | 130 } |
| 121 | 131 |
| 122 bool IsIgnoredIPv6(const InterfaceAddress& ip) { | 132 bool IsIgnoredIPv6(const IPAddress& ip) { |
| 123 if (ip.family() != AF_INET6) { | 133 if (ip.family() != AF_INET6) { |
| 124 return false; | 134 return false; |
| 125 } | 135 } |
| 126 | 136 |
| 127 // Link-local addresses require scope id to be bound successfully. | 137 // Link-local addresses require scope id to be bound successfully. |
| 128 // However, our IPAddress structure doesn't carry that so the | 138 // However, our IPAddress structure doesn't carry that so the |
| 129 // information is lost and causes binding failure. | 139 // information is lost and causes binding failure. |
| 130 if (IPIsLinkLocal(ip)) { | 140 if (IPIsLinkLocal(ip)) { |
| 131 return true; | 141 return true; |
| 132 } | 142 } |
| 133 | 143 |
| 134 // Any MAC based IPv6 should be avoided to prevent the MAC tracking. | 144 // Any MAC based IPv6 should be avoided to prevent the MAC tracking. |
| 135 if (IPIsMacBased(ip)) { | 145 if (IPIsMacBased(ip)) { |
| 136 return true; | 146 return true; |
| 137 } | 147 } |
| 138 | 148 |
| 139 // Ignore deprecated IPv6. | |
| 140 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) { | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 return false; | 149 return false; |
| 145 } | 150 } |
| 146 | 151 |
| 147 } // namespace | 152 } // namespace |
| 148 | 153 |
| 149 // These addresses are used as the targets to find out the default local address | 154 // These addresses are used as the targets to find out the default local address |
| 150 // on a multi-homed endpoint. They are actually DNS servers. | 155 // on a multi-homed endpoint. They are actually DNS servers. |
| 151 const char kPublicIPv4Host[] = "8.8.8.8"; | 156 const char kPublicIPv4Host[] = "8.8.8.8"; |
| 152 const char kPublicIPv6Host[] = "2001:4860:4860::8888"; | 157 const char kPublicIPv6Host[] = "2001:4860:4860::8888"; |
| 153 const int kPublicPort = 53; // DNS port. | 158 const int kPublicPort = 53; // DNS port. |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 | 386 |
| 382 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 387 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 383 NetworkList* networks) const { | 388 NetworkList* networks) const { |
| 384 ASSERT(false); | 389 ASSERT(false); |
| 385 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; | 390 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; |
| 386 return false; | 391 return false; |
| 387 } | 392 } |
| 388 | 393 |
| 389 #elif defined(WEBRTC_POSIX) | 394 #elif defined(WEBRTC_POSIX) |
| 390 void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces, | 395 void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces, |
| 391 IfAddrsConverter* ifaddrs_converter, | |
| 392 bool include_ignored, | 396 bool include_ignored, |
| 393 NetworkList* networks) const { | 397 NetworkList* networks) const { |
| 394 NetworkMap current_networks; | 398 NetworkMap current_networks; |
| 395 | |
| 396 for (struct ifaddrs* cursor = interfaces; | 399 for (struct ifaddrs* cursor = interfaces; |
| 397 cursor != NULL; cursor = cursor->ifa_next) { | 400 cursor != NULL; cursor = cursor->ifa_next) { |
| 398 IPAddress prefix; | 401 IPAddress prefix; |
| 399 IPAddress mask; | 402 IPAddress mask; |
| 400 InterfaceAddress ip; | 403 IPAddress ip; |
| 401 int scope_id = 0; | 404 int scope_id = 0; |
| 402 | 405 |
| 403 // Some interfaces may not have address assigned. | 406 // Some interfaces may not have address assigned. |
| 404 if (!cursor->ifa_addr || !cursor->ifa_netmask) { | 407 if (!cursor->ifa_addr || !cursor->ifa_netmask) |
| 405 continue; | 408 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 } | |
| 424 | 409 |
| 425 // Special case for IPv6 address. | 410 switch (cursor->ifa_addr->sa_family) { |
| 426 if (cursor->ifa_addr->sa_family == AF_INET6) { | 411 case AF_INET: { |
| 427 if (IsIgnoredIPv6(ip)) { | 412 ip = IPAddress( |
| 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: { |
| 428 continue; | 437 continue; |
| 429 } | 438 } |
| 430 scope_id = | |
| 431 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id; | |
| 432 } | 439 } |
| 433 | 440 |
| 434 int prefix_length = CountIPMaskBits(mask); | 441 int prefix_length = CountIPMaskBits(mask); |
| 435 prefix = TruncateIP(ip, prefix_length); | 442 prefix = TruncateIP(ip, prefix_length); |
| 436 std::string key = MakeNetworkKey(std::string(cursor->ifa_name), | 443 std::string key = MakeNetworkKey(std::string(cursor->ifa_name), |
| 437 prefix, prefix_length); | 444 prefix, prefix_length); |
| 438 auto existing_network = current_networks.find(key); | 445 auto existing_network = current_networks.find(key); |
| 439 if (existing_network == current_networks.end()) { | 446 if (existing_network == current_networks.end()) { |
| 440 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; | 447 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
| 441 if (cursor->ifa_flags & IFF_LOOPBACK) { | 448 if (cursor->ifa_flags & IFF_LOOPBACK) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 467 | 474 |
| 468 bool BasicNetworkManager::CreateNetworks(bool include_ignored, | 475 bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 469 NetworkList* networks) const { | 476 NetworkList* networks) const { |
| 470 struct ifaddrs* interfaces; | 477 struct ifaddrs* interfaces; |
| 471 int error = getifaddrs(&interfaces); | 478 int error = getifaddrs(&interfaces); |
| 472 if (error != 0) { | 479 if (error != 0) { |
| 473 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; | 480 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error; |
| 474 return false; | 481 return false; |
| 475 } | 482 } |
| 476 | 483 |
| 477 rtc::scoped_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter()); | 484 ConvertIfAddrs(interfaces, include_ignored, networks); |
| 478 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored, | |
| 479 networks); | |
| 480 | 485 |
| 481 freeifaddrs(interfaces); | 486 freeifaddrs(interfaces); |
| 482 return true; | 487 return true; |
| 483 } | 488 } |
| 484 | 489 |
| 485 #elif defined(WEBRTC_WIN) | 490 #elif defined(WEBRTC_WIN) |
| 486 | 491 |
| 487 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, | 492 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, |
| 488 const IPAddress& ip, IPAddress* prefix) { | 493 const IPAddress& ip, IPAddress* prefix) { |
| 489 IPAddress current_prefix; | 494 IPAddress current_prefix; |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 914 std::stringstream ss; | 919 std::stringstream ss; |
| 915 // Print out the first space-terminated token of the network desc, plus | 920 // Print out the first space-terminated token of the network desc, plus |
| 916 // the IP address. | 921 // the IP address. |
| 917 ss << "Net[" << description_.substr(0, description_.find(' ')) | 922 ss << "Net[" << description_.substr(0, description_.find(' ')) |
| 918 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 923 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
| 919 << ":" << AdapterTypeToString(type_) << "]"; | 924 << ":" << AdapterTypeToString(type_) << "]"; |
| 920 return ss.str(); | 925 return ss.str(); |
| 921 } | 926 } |
| 922 | 927 |
| 923 } // namespace rtc | 928 } // namespace rtc |
| OLD | NEW |