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