Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
      3  *
      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
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifdef HAVE_CONFIG_H
     12 #include "config.h"
     13 #endif
     14 
     15 #include "webrtc/base/network.h"
     16 
     17 #if defined(WEBRTC_POSIX)
     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
     20 // linux instead of the standard posix one.
     21 #if defined(WEBRTC_LINUX)
     22 #include <linux/if.h>
     23 #include <linux/route.h>
     24 #elif !defined(__native_client__)
     25 #include <net/if.h>
     26 #endif
     27 #endif  // WEBRTC_POSIX
     28 
     29 #if defined(WEBRTC_WIN)
     30 #include "webrtc/base/win32.h"
     31 #include <Iphlpapi.h>
     32 #elif !defined(__native_client__)
     33 #include "webrtc/base/ifaddrs_converter.h"
     34 #endif
     35 
     36 #include <stdio.h>
     37 
     38 #include <algorithm>
     39 
     40 #include "webrtc/base/logging.h"
     41 #include "webrtc/base/networkmonitor.h"
     42 #include "webrtc/base/scoped_ptr.h"
     43 #include "webrtc/base/socket.h"  // includes something that makes windows happy
     44 #include "webrtc/base/stream.h"
     45 #include "webrtc/base/stringencode.h"
     46 #include "webrtc/base/thread.h"
     47 
     48 namespace rtc {
     49 namespace {
     50 
     51 // Turning on IPv6 could make many IPv6 interfaces available for connectivity
     52 // check and delay the call setup time. kMaxIPv6Networks is the default upper
     53 // limit of IPv6 networks but could be changed by set_max_ipv6_networks().
     54 const int kMaxIPv6Networks = 5;
     55 
     56 const uint32_t kUpdateNetworksMessage = 1;
     57 const uint32_t kSignalNetworksMessage = 2;
     58 
     59 // Fetch list of networks every two seconds.
     60 const int kNetworksUpdateIntervalMs = 2000;
     61 
     62 const int kHighestNetworkPreference = 127;
     63 
     64 typedef struct {
     65   Network* net;
     66   std::vector<InterfaceAddress> ips;
     67 } AddressList;
     68 
     69 bool CompareNetworks(const Network* a, const Network* b) {
     70   if (a->prefix_length() == b->prefix_length()) {
     71     if (a->name() == b->name()) {
     72       return a->prefix() < b->prefix();
     73     }
     74   }
     75   return a->name() < b->name();
     76 }
     77 
     78 bool SortNetworks(const Network* a, const Network* b) {
     79   // Network types will be preferred above everything else while sorting
     80   // Networks.
     81 
     82   // Networks are sorted first by type.
     83   if (a->type() != b->type()) {
     84     return a->type() < b->type();
     85   }
     86 
     87   IPAddress ip_a = a->GetBestIP();
     88   IPAddress ip_b = b->GetBestIP();
     89 
     90   // After type, networks are sorted by IP address precedence values
     91   // from RFC 3484-bis
     92   if (IPAddressPrecedence(ip_a) != IPAddressPrecedence(ip_b)) {
     93     return IPAddressPrecedence(ip_a) > IPAddressPrecedence(ip_b);
     94   }
     95 
     96   // TODO(mallinath) - Add VPN and Link speed conditions while sorting.
     97 
     98   // Networks are sorted last by key.
     99   return a->key() > b->key();
    100 }
    101 
    102 std::string AdapterTypeToString(AdapterType type) {
    103   switch (type) {
    104     case ADAPTER_TYPE_UNKNOWN:
    105       return "Unknown";
    106     case ADAPTER_TYPE_ETHERNET:
    107       return "Ethernet";
    108     case ADAPTER_TYPE_WIFI:
    109       return "Wifi";
    110     case ADAPTER_TYPE_CELLULAR:
    111       return "Cellular";
    112     case ADAPTER_TYPE_VPN:
    113       return "VPN";
    114     case ADAPTER_TYPE_LOOPBACK:
    115       return "Loopback";
    116     default:
    117       RTC_DCHECK(false) << "Invalid type " << type;
    118       return std::string();
    119   }
    120 }
    121 
    122 #if !defined(__native_client__)
    123 bool IsIgnoredIPv6(const InterfaceAddress& ip) {
    124   if (ip.family() != AF_INET6) {
    125     return false;
    126   }
    127 
    128   // Link-local addresses require scope id to be bound successfully.
    129   // However, our IPAddress structure doesn't carry that so the
    130   // information is lost and causes binding failure.
    131   if (IPIsLinkLocal(ip)) {
    132     return true;
    133   }
    134 
    135   // Any MAC based IPv6 should be avoided to prevent the MAC tracking.
    136   if (IPIsMacBased(ip)) {
    137     return true;
    138   }
    139 
    140   // Ignore deprecated IPv6.
    141   if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) {
    142     return true;
    143   }
    144 
    145   return false;
    146 }
    147 #endif  // !defined(__native_client__)
    148 
    149 }  // namespace
    150 
    151 // These addresses are used as the targets to find out the default local address
    152 // on a multi-homed endpoint. They are actually DNS servers.
    153 const char kPublicIPv4Host[] = "8.8.8.8";
    154 const char kPublicIPv6Host[] = "2001:4860:4860::8888";
    155 const int kPublicPort = 53;  // DNS port.
    156 
    157 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
    158                            int prefix_length) {
    159   std::ostringstream ost;
    160   ost << name << "%" << prefix.ToString() << "/" << prefix_length;
    161   return ost.str();
    162 }
    163 
    164 NetworkManager::NetworkManager() {
    165 }
    166 
    167 NetworkManager::~NetworkManager() {
    168 }
    169 
    170 NetworkManager::EnumerationPermission NetworkManager::enumeration_permission()
    171     const {
    172   return ENUMERATION_ALLOWED;
    173 }
    174 
    175 bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const {
    176   return false;
    177 }
    178 
    179 NetworkManagerBase::NetworkManagerBase()
    180     : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED),
    181       max_ipv6_networks_(kMaxIPv6Networks),
    182       ipv6_enabled_(true) {
    183 }
    184 
    185 NetworkManagerBase::~NetworkManagerBase() {
    186   for (const auto& kv : networks_map_) {
    187     delete kv.second;
    188   }
    189 }
    190 
    191 NetworkManager::EnumerationPermission
    192 NetworkManagerBase::enumeration_permission() const {
    193   return enumeration_permission_;
    194 }
    195 
    196 void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) {
    197   if (!ipv4_any_address_network_) {
    198     const rtc::IPAddress ipv4_any_address(INADDR_ANY);
    199     ipv4_any_address_network_.reset(
    200         new rtc::Network("any", "any", ipv4_any_address, 0));
    201     ipv4_any_address_network_->set_default_local_address_provider(this);
    202     ipv4_any_address_network_->AddIP(ipv4_any_address);
    203   }
    204   networks->push_back(ipv4_any_address_network_.get());
    205 
    206   if (ipv6_enabled()) {
    207     if (!ipv6_any_address_network_) {
    208       const rtc::IPAddress ipv6_any_address(in6addr_any);
    209       ipv6_any_address_network_.reset(
    210           new rtc::Network("any", "any", ipv6_any_address, 0));
    211       ipv6_any_address_network_->set_default_local_address_provider(this);
    212       ipv6_any_address_network_->AddIP(ipv6_any_address);
    213     }
    214     networks->push_back(ipv6_any_address_network_.get());
    215   }
    216 }
    217 
    218 void NetworkManagerBase::GetNetworks(NetworkList* result) const {
    219   int ipv6_networks = 0;
    220   result->clear();
    221   for (Network* network : networks_) {
    222     // Keep the number of IPv6 networks under |max_ipv6_networks_|.
    223     if (network->prefix().family() == AF_INET6) {
    224       if (ipv6_networks >= max_ipv6_networks_) {
    225         continue;
    226       }
    227       ++ipv6_networks;
    228     }
    229     result->push_back(network);
    230   }
    231 }
    232 
    233 void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
    234                                           bool* changed) {
    235   NetworkManager::Stats stats;
    236   MergeNetworkList(new_networks, changed, &stats);
    237 }
    238 
    239 void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
    240                                           bool* changed,
    241                                           NetworkManager::Stats* stats) {
    242   *changed = false;
    243   // AddressList in this map will track IP addresses for all Networks
    244   // with the same key.
    245   std::map<std::string, AddressList> consolidated_address_list;
    246   NetworkList list(new_networks);
    247   std::sort(list.begin(), list.end(), CompareNetworks);
    248   // First, build a set of network-keys to the ipaddresses.
    249   for (Network* network : list) {
    250     bool might_add_to_merged_list = false;
    251     std::string key = MakeNetworkKey(network->name(),
    252                                      network->prefix(),
    253                                      network->prefix_length());
    254     if (consolidated_address_list.find(key) ==
    255         consolidated_address_list.end()) {
    256       AddressList addrlist;
    257       addrlist.net = network;
    258       consolidated_address_list[key] = addrlist;
    259       might_add_to_merged_list = true;
    260     }
    261     const std::vector<InterfaceAddress>& addresses = network->GetIPs();
    262     AddressList& current_list = consolidated_address_list[key];
    263     for (const InterfaceAddress& address : addresses) {
    264       current_list.ips.push_back(address);
    265     }
    266     if (!might_add_to_merged_list) {
    267       delete network;
    268     } else {
    269       if (current_list.ips[0].family() == AF_INET) {
    270         stats->ipv4_network_count++;
    271       } else {
    272         ASSERT(current_list.ips[0].family() == AF_INET6);
    273         stats->ipv6_network_count++;
    274       }
    275     }
    276   }
    277 
    278   // Next, look for existing network objects to re-use.
    279   // Result of Network merge. Element in this list should have unique key.
    280   NetworkList merged_list;
    281   for (const auto& kv : consolidated_address_list) {
    282     const std::string& key = kv.first;
    283     Network* net = kv.second.net;
    284     auto existing = networks_map_.find(key);
    285     if (existing == networks_map_.end()) {
    286       // This network is new. Place it in the network map.
    287       merged_list.push_back(net);
    288       networks_map_[key] = net;
    289       // Also, we might have accumulated IPAddresses from the first
    290       // step, set it here.
    291       net->SetIPs(kv.second.ips, true);
    292       *changed = true;
    293     } else {
    294       // This network exists in the map already. Reset its IP addresses.
    295       Network* existing_net = existing->second;
    296       *changed = existing_net->SetIPs(kv.second.ips, *changed);
    297       merged_list.push_back(existing_net);
    298       // If the existing network was not active, networks have changed.
    299       if (!existing_net->active()) {
    300         *changed = true;
    301       }
    302       ASSERT(net->active());
    303       if (existing_net != net) {
    304         delete net;
    305       }
    306     }
    307   }
    308   // It may still happen that the merged list is a subset of |networks_|.
    309   // To detect this change, we compare their sizes.
    310   if (merged_list.size() != networks_.size()) {
    311     *changed = true;
    312   }
    313 
    314   // If the network list changes, we re-assign |networks_| to the merged list
    315   // and re-sort it.
    316   if (*changed) {
    317     networks_ = merged_list;
    318     // Reset the active states of all networks.
    319     for (const auto& kv : networks_map_) {
    320       kv.second->set_active(false);
    321     }
    322     for (Network* network : networks_) {
    323       network->set_active(true);
    324     }
    325     std::sort(networks_.begin(), networks_.end(), SortNetworks);
    326     // Now network interfaces are sorted, we should set the preference value
    327     // for each of the interfaces we are planning to use.
    328     // Preference order of network interfaces might have changed from previous
    329     // sorting due to addition of higher preference network interface.
    330     // Since we have already sorted the network interfaces based on our
    331     // requirements, we will just assign a preference value starting with 127,
    332     // in decreasing order.
    333     int pref = kHighestNetworkPreference;
    334     for (Network* network : networks_) {
    335       network->set_preference(pref);
    336       if (pref > 0) {
    337         --pref;
    338       } else {
    339         LOG(LS_ERROR) << "Too many network interfaces to handle!";
    340         break;
    341       }
    342     }
    343   }
    344 }
    345 
    346 void NetworkManagerBase::set_default_local_addresses(const IPAddress& ipv4,
    347                                                      const IPAddress& ipv6) {
    348   if (ipv4.family() == AF_INET) {
    349     default_local_ipv4_address_ = ipv4;
    350   }
    351   if (ipv6.family() == AF_INET6) {
    352     default_local_ipv6_address_ = ipv6;
    353   }
    354 }
    355 
    356 bool NetworkManagerBase::GetDefaultLocalAddress(int family,
    357                                                 IPAddress* ipaddr) const {
    358   if (family == AF_INET && !default_local_ipv4_address_.IsNil()) {
    359     *ipaddr = default_local_ipv4_address_;
    360     return true;
    361   } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) {
    362     *ipaddr = default_local_ipv6_address_;
    363     return true;
    364   }
    365   return false;
    366 }
    367 
    368 BasicNetworkManager::BasicNetworkManager()
    369     : thread_(NULL), sent_first_update_(false), start_count_(0),
    370       ignore_non_default_routes_(false) {
    371 }
    372 
    373 BasicNetworkManager::~BasicNetworkManager() {
    374 }
    375 
    376 void BasicNetworkManager::OnNetworksChanged() {
    377   LOG(LS_VERBOSE) << "Network change was observed at the network manager";
    378   UpdateNetworksOnce();
    379 }
    380 
    381 #if defined(__native_client__)
    382 
    383 bool BasicNetworkManager::CreateNetworks(bool include_ignored,
    384                                          NetworkList* networks) const {
    385   ASSERT(false);
    386   LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet";
    387   return false;
    388 }
    389 
    390 #elif defined(WEBRTC_POSIX)
    391 void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces,
    392                                          IfAddrsConverter* ifaddrs_converter,
    393                                          bool include_ignored,
    394                                          NetworkList* networks) const {
    395   NetworkMap current_networks;
    396 
    397   for (struct ifaddrs* cursor = interfaces;
    398        cursor != NULL; cursor = cursor->ifa_next) {
    399     IPAddress prefix;
    400     IPAddress mask;
    401     InterfaceAddress ip;
    402     int scope_id = 0;
    403 
    404     // Some interfaces may not have address assigned.
    405     if (!cursor->ifa_addr || !cursor->ifa_netmask) {
    406       continue;
    407     }
    408     // Skip ones which are down.
    409     if (!(cursor->ifa_flags & IFF_RUNNING)) {
    410       continue;
    411     }
    412     // Skip unknown family.
    413     if (cursor->ifa_addr->sa_family != AF_INET &&
    414         cursor->ifa_addr->sa_family != AF_INET6) {
    415       continue;
    416     }
    417     // Skip IPv6 if not enabled.
    418     if (cursor->ifa_addr->sa_family == AF_INET6 && !ipv6_enabled()) {
    419       continue;
    420     }
    421     // Convert to InterfaceAddress.
    422     if (!ifaddrs_converter->ConvertIfAddrsToIPAddress(cursor, &ip, &mask)) {
    423       continue;
    424     }
    425 
    426     // Special case for IPv6 address.
    427     if (cursor->ifa_addr->sa_family == AF_INET6) {
    428       if (IsIgnoredIPv6(ip)) {
    429         continue;
    430       }
    431       scope_id =
    432           reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id;
    433     }
    434 
    435     int prefix_length = CountIPMaskBits(mask);
    436     prefix = TruncateIP(ip, prefix_length);
    437     std::string key = MakeNetworkKey(std::string(cursor->ifa_name),
    438                                      prefix, prefix_length);
    439     auto existing_network = current_networks.find(key);
    440     if (existing_network == current_networks.end()) {
    441       AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
    442       if (cursor->ifa_flags & IFF_LOOPBACK) {
    443         adapter_type = ADAPTER_TYPE_LOOPBACK;
    444       }
    445 #if defined(WEBRTC_IOS)
    446       // Cell networks are pdp_ipN on iOS.
    447       if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) {
    448         adapter_type = ADAPTER_TYPE_CELLULAR;
    449       }
    450 #endif
    451       // TODO(phoglund): Need to recognize other types as well.
    452       scoped_ptr<Network> network(new Network(cursor->ifa_name,
    453                                               cursor->ifa_name, prefix,
    454                                               prefix_length, adapter_type));
    455       network->set_default_local_address_provider(this);
    456       network->set_scope_id(scope_id);
    457       network->AddIP(ip);
    458       network->set_ignored(IsIgnoredNetwork(*network));
    459       if (include_ignored || !network->ignored()) {
    460         current_networks[key] = network.get();
    461         networks->push_back(network.release());
    462       }
    463     } else {
    464       (*existing_network).second->AddIP(ip);
    465     }
    466   }
    467 }
    468 
    469 bool BasicNetworkManager::CreateNetworks(bool include_ignored,
    470                                          NetworkList* networks) const {
    471   struct ifaddrs* interfaces;
    472   int error = getifaddrs(&interfaces);
    473   if (error != 0) {
    474     LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error;
    475     return false;
    476   }
    477 
    478   rtc::scoped_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter());
    479   ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored,
    480                  networks);
    481 
    482   freeifaddrs(interfaces);
    483   return true;
    484 }
    485 
    486 #elif defined(WEBRTC_WIN)
    487 
    488 unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist,
    489               const IPAddress& ip, IPAddress* prefix) {
    490   IPAddress current_prefix;
    491   IPAddress best_prefix;
    492   unsigned int best_length = 0;
    493   while (prefixlist) {
    494     // Look for the longest matching prefix in the prefixlist.
    495     if (prefixlist->Address.lpSockaddr == NULL ||
    496         prefixlist->Address.lpSockaddr->sa_family != ip.family()) {
    497       prefixlist = prefixlist->Next;
    498       continue;
    499     }
    500     switch (prefixlist->Address.lpSockaddr->sa_family) {
    501       case AF_INET: {
    502         sockaddr_in* v4_addr =
    503             reinterpret_cast<sockaddr_in*>(prefixlist->Address.lpSockaddr);
    504         current_prefix = IPAddress(v4_addr->sin_addr);
    505         break;
    506       }
    507       case AF_INET6: {
    508           sockaddr_in6* v6_addr =
    509               reinterpret_cast<sockaddr_in6*>(prefixlist->Address.lpSockaddr);
    510           current_prefix = IPAddress(v6_addr->sin6_addr);
    511           break;
    512       }
    513       default: {
    514         prefixlist = prefixlist->Next;
    515         continue;
    516       }
    517     }
    518     if (TruncateIP(ip, prefixlist->PrefixLength) == current_prefix &&
    519         prefixlist->PrefixLength > best_length) {
    520       best_prefix = current_prefix;
    521       best_length = prefixlist->PrefixLength;
    522     }
    523     prefixlist = prefixlist->Next;
    524   }
    525   *prefix = best_prefix;
    526   return best_length;
    527 }
    528 
    529 bool BasicNetworkManager::CreateNetworks(bool include_ignored,
    530                                          NetworkList* networks) const {
    531   NetworkMap current_networks;
    532   // MSDN recommends a 15KB buffer for the first try at GetAdaptersAddresses.
    533   size_t buffer_size = 16384;
    534   scoped_ptr<char[]> adapter_info(new char[buffer_size]);
    535   PIP_ADAPTER_ADDRESSES adapter_addrs =
    536       reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
    537   int adapter_flags = (GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST |
    538                        GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_INCLUDE_PREFIX);
    539   int ret = 0;
    540   do {
    541     adapter_info.reset(new char[buffer_size]);
    542     adapter_addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
    543     ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags,
    544                                0, adapter_addrs,
    545                                reinterpret_cast<PULONG>(&buffer_size));
    546   } while (ret == ERROR_BUFFER_OVERFLOW);
    547   if (ret != ERROR_SUCCESS) {
    548     return false;
    549   }
    550   int count = 0;
    551   while (adapter_addrs) {
    552     if (adapter_addrs->OperStatus == IfOperStatusUp) {
    553       PIP_ADAPTER_UNICAST_ADDRESS address = adapter_addrs->FirstUnicastAddress;
    554       PIP_ADAPTER_PREFIX prefixlist = adapter_addrs->FirstPrefix;
    555       std::string name;
    556       std::string description;
    557 #if !defined(NDEBUG)
    558       name = ToUtf8(adapter_addrs->FriendlyName,
    559                     wcslen(adapter_addrs->FriendlyName));
    560 #endif
    561       description = ToUtf8(adapter_addrs->Description,
    562                            wcslen(adapter_addrs->Description));
    563       for (; address; address = address->Next) {
    564 #if defined(NDEBUG)
    565         name = rtc::ToString(count);
    566 #endif
    567 
    568         IPAddress ip;
    569         int scope_id = 0;
    570         scoped_ptr<Network> network;
    571         switch (address->Address.lpSockaddr->sa_family) {
    572           case AF_INET: {
    573             sockaddr_in* v4_addr =
    574                 reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
    575             ip = IPAddress(v4_addr->sin_addr);
    576             break;
    577           }
    578           case AF_INET6: {
    579             if (ipv6_enabled()) {
    580               sockaddr_in6* v6_addr =
    581                   reinterpret_cast<sockaddr_in6*>(address->Address.lpSockaddr);
    582               scope_id = v6_addr->sin6_scope_id;
    583               ip = IPAddress(v6_addr->sin6_addr);
    584 
    585               if (IsIgnoredIPv6(ip)) {
    586                 continue;
    587               }
    588 
    589               break;
    590             } else {
    591               continue;
    592             }
    593           }
    594           default: {
    595             continue;
    596           }
    597         }
    598 
    599         IPAddress prefix;
    600         int prefix_length = GetPrefix(prefixlist, ip, &prefix);
    601         std::string key = MakeNetworkKey(name, prefix, prefix_length);
    602         auto existing_network = current_networks.find(key);
    603         if (existing_network == current_networks.end()) {
    604           AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
    605           if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
    606             // TODO(phoglund): Need to recognize other types as well.
    607             adapter_type = ADAPTER_TYPE_LOOPBACK;
    608           }
    609           scoped_ptr<Network> network(new Network(name, description, prefix,
    610                                                   prefix_length, adapter_type));
    611           network->set_default_local_address_provider(this);
    612           network->set_scope_id(scope_id);
    613           network->AddIP(ip);
    614           bool ignored = IsIgnoredNetwork(*network);
    615           network->set_ignored(ignored);
    616           if (include_ignored || !network->ignored()) {
    617             current_networks[key] = network.get();
    618             networks->push_back(network.release());
    619           }
    620         } else {
    621           (*existing_network).second->AddIP(ip);
    622         }
    623       }
    624       // Count is per-adapter - all 'Networks' created from the same
    625       // adapter need to have the same name.
    626       ++count;
    627     }
    628     adapter_addrs = adapter_addrs->Next;
    629   }
    630   return true;
    631 }
    632 #endif  // WEBRTC_WIN
    633 
    634 #if defined(WEBRTC_LINUX)
    635 bool IsDefaultRoute(const std::string& network_name) {
    636   FileStream fs;
    637   if (!fs.Open("/proc/net/route", "r", NULL)) {
    638     LOG(LS_WARNING) << "Couldn't read /proc/net/route, skipping default "
    639                     << "route check (assuming everything is a default route).";
    640     return true;
    641   } else {
    642     std::string line;
    643     while (fs.ReadLine(&line) == SR_SUCCESS) {
    644       char iface_name[256];
    645       unsigned int iface_ip, iface_gw, iface_mask, iface_flags;
    646       if (sscanf(line.c_str(),
    647                  "%255s %8X %8X %4X %*d %*u %*d %8X",
    648                  iface_name, &iface_ip, &iface_gw,
    649                  &iface_flags, &iface_mask) == 5 &&
    650           network_name == iface_name &&
    651           iface_mask == 0 &&
    652           (iface_flags & (RTF_UP | RTF_HOST)) == RTF_UP) {
    653         return true;
    654       }
    655     }
    656   }
    657   return false;
    658 }
    659 #endif
    660 
    661 bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const {
    662   // Ignore networks on the explicit ignore list.
    663   for (const std::string& ignored_name : network_ignore_list_) {
    664     if (network.name() == ignored_name) {
    665       return true;
    666     }
    667   }
    668 
    669 #if defined(WEBRTC_POSIX)
    670   // Filter out VMware/VirtualBox interfaces, typically named vmnet1,
    671   // vmnet8, or vboxnet0.
    672   if (strncmp(network.name().c_str(), "vmnet", 5) == 0 ||
    673       strncmp(network.name().c_str(), "vnic", 4) == 0 ||
    674       strncmp(network.name().c_str(), "vboxnet", 7) == 0) {
    675     return true;
    676   }
    677 #if defined(WEBRTC_LINUX)
    678   // Make sure this is a default route, if we're ignoring non-defaults.
    679   if (ignore_non_default_routes_ && !IsDefaultRoute(network.name())) {
    680     return true;
    681   }
    682 #endif
    683 #elif defined(WEBRTC_WIN)
    684   // Ignore any HOST side vmware adapters with a description like:
    685   // VMware Virtual Ethernet Adapter for VMnet1
    686   // but don't ignore any GUEST side adapters with a description like:
    687   // VMware Accelerated AMD PCNet Adapter #2
    688   if (strstr(network.description().c_str(), "VMnet") != NULL) {
    689     return true;
    690   }
    691 #endif
    692 
    693   // Ignore any networks with a 0.x.y.z IP
    694   if (network.prefix().family() == AF_INET) {
    695     return (network.prefix().v4AddressAsHostOrderInteger() < 0x01000000);
    696   }
    697 
    698   return false;
    699 }
    700 
    701 void BasicNetworkManager::StartUpdating() {
    702   thread_ = Thread::Current();
    703   if (start_count_) {
    704     // If network interfaces are already discovered and signal is sent,
    705     // we should trigger network signal immediately for the new clients
    706     // to start allocating ports.
    707     if (sent_first_update_)
    708       thread_->Post(this, kSignalNetworksMessage);
    709   } else {
    710     thread_->Post(this, kUpdateNetworksMessage);
    711     StartNetworkMonitor();
    712   }
    713   ++start_count_;
    714 }
    715 
    716 void BasicNetworkManager::StopUpdating() {
    717   ASSERT(Thread::Current() == thread_);
    718   if (!start_count_)
    719     return;
    720 
    721   --start_count_;
    722   if (!start_count_) {
    723     thread_->Clear(this);
    724     sent_first_update_ = false;
    725     StopNetworkMonitor();
    726   }
    727 }
    728 
    729 void BasicNetworkManager::StartNetworkMonitor() {
    730   NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory();
    731   if (factory == nullptr) {
    732     return;
    733   }
    734   network_monitor_.reset(factory->CreateNetworkMonitor());
    735   if (!network_monitor_) {
    736     return;
    737   }
    738   network_monitor_->SignalNetworksChanged.connect(
    739       this, &BasicNetworkManager::OnNetworksChanged);
    740   network_monitor_->Start();
    741 }
    742 
    743 void BasicNetworkManager::StopNetworkMonitor() {
    744   if (!network_monitor_) {
    745     return;
    746   }
    747   network_monitor_->Stop();
    748   network_monitor_.reset();
    749 }
    750 
    751 void BasicNetworkManager::OnMessage(Message* msg) {
    752   switch (msg->message_id) {
    753     case kUpdateNetworksMessage: {
    754       UpdateNetworksContinually();
    755       break;
    756     }
    757     case kSignalNetworksMessage:  {
    758       SignalNetworksChanged();
    759       break;
    760     }
    761     default:
    762       ASSERT(false);
    763   }
    764 }
    765 
    766 IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const {
    767   ASSERT(thread_ == Thread::Current());
    768   ASSERT(thread_->socketserver() != nullptr);
    769   ASSERT(family == AF_INET || family == AF_INET6);
    770 
    771   scoped_ptr<AsyncSocket> socket(
    772       thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM));
    773   if (!socket) {
    774     return IPAddress();
    775   }
    776 
    777   if (!socket->Connect(
    778           SocketAddress(family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host,
    779                         kPublicPort))) {
    780     return IPAddress();
    781   }
    782   return socket->GetLocalAddress().ipaddr();
    783 }
    784 
    785 void BasicNetworkManager::UpdateNetworksOnce() {
    786   if (!start_count_)
    787     return;
    788 
    789   ASSERT(Thread::Current() == thread_);
    790 
    791   NetworkList list;
    792   if (!CreateNetworks(false, &list)) {
    793     SignalError();
    794   } else {
    795     bool changed;
    796     NetworkManager::Stats stats;
    797     MergeNetworkList(list, &changed, &stats);
    798     set_default_local_addresses(QueryDefaultLocalAddress(AF_INET),
    799                                 QueryDefaultLocalAddress(AF_INET6));
    800     if (changed || !sent_first_update_) {
    801       SignalNetworksChanged();
    802       sent_first_update_ = true;
    803     }
    804   }
    805 }
    806 
    807 void BasicNetworkManager::UpdateNetworksContinually() {
    808   UpdateNetworksOnce();
    809   thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage);
    810 }
    811 
    812 void BasicNetworkManager::DumpNetworks() {
    813   NetworkList list;
    814   GetNetworks(&list);
    815   LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:";
    816   for (const Network* network : list) {
    817     LOG(LS_INFO) << network->ToString() << ": " << network->description()
    818                  << ", active ? " << network->active()
    819                  << ((network->ignored()) ? ", Ignored" : "");
    820   }
    821 }
    822 
    823 Network::Network(const std::string& name,
    824                  const std::string& desc,
    825                  const IPAddress& prefix,
    826                  int prefix_length)
    827     : name_(name),
    828       description_(desc),
    829       prefix_(prefix),
    830       prefix_length_(prefix_length),
    831       key_(MakeNetworkKey(name, prefix, prefix_length)),
    832       scope_id_(0),
    833       ignored_(false),
    834       type_(ADAPTER_TYPE_UNKNOWN),
    835       preference_(0) {}
    836 
    837 Network::Network(const std::string& name,
    838                  const std::string& desc,
    839                  const IPAddress& prefix,
    840                  int prefix_length,
    841                  AdapterType type)
    842     : name_(name),
    843       description_(desc),
    844       prefix_(prefix),
    845       prefix_length_(prefix_length),
    846       key_(MakeNetworkKey(name, prefix, prefix_length)),
    847       scope_id_(0),
    848       ignored_(false),
    849       type_(type),
    850       preference_(0) {}
    851 
    852 Network::~Network() = default;
    853 
    854 // Sets the addresses of this network. Returns true if the address set changed.
    855 // Change detection is short circuited if the changed argument is true.
    856 bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) {
    857   // Detect changes with a nested loop; n-squared but we expect on the order
    858   // of 2-3 addresses per network.
    859   changed = changed || ips.size() != ips_.size();
    860   if (!changed) {
    861     for (const InterfaceAddress& ip : ips) {
    862       if (std::find(ips_.begin(), ips_.end(), ip) == ips_.end()) {
    863         changed = true;
    864         break;
    865       }
    866     }
    867   }
    868 
    869   ips_ = ips;
    870   return changed;
    871 }
    872 
    873 // Select the best IP address to use from this Network.
    874 IPAddress Network::GetBestIP() const {
    875   if (ips_.size() == 0) {
    876     return IPAddress();
    877   }
    878 
    879   if (prefix_.family() == AF_INET) {
    880     return static_cast<IPAddress>(ips_.at(0));
    881   }
    882 
    883   InterfaceAddress selected_ip, ula_ip;
    884 
    885   for (const InterfaceAddress& ip : ips_) {
    886     // Ignore any address which has been deprecated already.
    887     if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED)
    888       continue;
    889 
    890     // ULA address should only be returned when we have no other
    891     // global IP.
    892     if (IPIsULA(static_cast<const IPAddress&>(ip))) {
    893       ula_ip = ip;
    894       continue;
    895     }
    896     selected_ip = ip;
    897 
    898     // Search could stop once a temporary non-deprecated one is found.
    899     if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY)
    900       break;
    901   }
    902 
    903   // No proper global IPv6 address found, use ULA instead.
    904   if (IPIsUnspec(selected_ip) && !IPIsUnspec(ula_ip)) {
    905     selected_ip = ula_ip;
    906   }
    907 
    908   return static_cast<IPAddress>(selected_ip);
    909 }
    910 
    911 std::string Network::ToString() const {
    912   std::stringstream ss;
    913   // Print out the first space-terminated token of the network desc, plus
    914   // the IP address.
    915   ss << "Net[" << description_.substr(0, description_.find(' '))
    916      << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_
    917      << ":" << AdapterTypeToString(type_) << "]";
    918   return ss.str();
    919 }
    920 
    921 }  // namespace rtc
    922