blob: 4906184b5dcfafd9422fee5e689d71c6a7e0d767 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/network.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#if defined(WEBRTC_POSIX)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000014#include <net/if.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#endif // WEBRTC_POSIX
16
17#if defined(WEBRTC_WIN)
Mirko Bonadei01914412018-03-23 16:30:58 +010018#include <iphlpapi.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020019
Yves Gerey665174f2018-06-19 15:03:05 +020020#include "rtc_base/win32.h"
Guo-wei Shieh9faf1542015-12-28 14:06:55 -080021#elif !defined(__native_client__)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/ifaddrs_converter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023#endif
24
jbauch555604a2016-04-26 03:13:22 -070025#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
Steve Anton2acd1632019-03-25 13:48:30 -070027#include "absl/algorithm/container.h"
28#include "absl/strings/match.h"
29#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/checks.h"
31#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/network_monitor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/socket.h" // includes something that makes windows happy
Steve Anton10542f22019-01-11 09:11:00 -080034#include "rtc_base/string_encode.h"
35#include "rtc_base/string_utils.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020036#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038
39namespace rtc {
40namespace {
41
Peter Boström0c4e06b2015-10-07 12:23:21 +020042const uint32_t kUpdateNetworksMessage = 1;
43const uint32_t kSignalNetworksMessage = 2;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044
45// Fetch list of networks every two seconds.
46const int kNetworksUpdateIntervalMs = 2000;
47
48const int kHighestNetworkPreference = 127;
49
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +000050typedef struct {
51 Network* net;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +000052 std::vector<InterfaceAddress> ips;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +000053} AddressList;
54
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055bool CompareNetworks(const Network* a, const Network* b) {
56 if (a->prefix_length() == b->prefix_length()) {
57 if (a->name() == b->name()) {
58 return a->prefix() < b->prefix();
59 }
60 }
61 return a->name() < b->name();
62}
63
64bool SortNetworks(const Network* a, const Network* b) {
65 // Network types will be preferred above everything else while sorting
66 // Networks.
67
68 // Networks are sorted first by type.
69 if (a->type() != b->type()) {
70 return a->type() < b->type();
71 }
72
guoweis@webrtc.org369a6372014-09-17 22:37:29 +000073 IPAddress ip_a = a->GetBestIP();
74 IPAddress ip_b = b->GetBestIP();
75
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 // After type, networks are sorted by IP address precedence values
77 // from RFC 3484-bis
guoweis@webrtc.org369a6372014-09-17 22:37:29 +000078 if (IPAddressPrecedence(ip_a) != IPAddressPrecedence(ip_b)) {
79 return IPAddressPrecedence(ip_a) > IPAddressPrecedence(ip_b);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 }
81
82 // TODO(mallinath) - Add VPN and Link speed conditions while sorting.
83
84 // Networks are sorted last by key.
deadbeef3427f532017-07-26 16:09:33 -070085 return a->key() < b->key();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086}
87
88std::string AdapterTypeToString(AdapterType type) {
89 switch (type) {
Qingsi Wang9f1de692018-06-28 15:38:09 -070090 case ADAPTER_TYPE_ANY:
Qingsi Wang01560de2018-07-26 10:44:02 -070091 return "Wildcard";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 case ADAPTER_TYPE_UNKNOWN:
93 return "Unknown";
94 case ADAPTER_TYPE_ETHERNET:
95 return "Ethernet";
96 case ADAPTER_TYPE_WIFI:
97 return "Wifi";
98 case ADAPTER_TYPE_CELLULAR:
99 return "Cellular";
100 case ADAPTER_TYPE_VPN:
101 return "VPN";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000102 case ADAPTER_TYPE_LOOPBACK:
103 return "Loopback";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800105 RTC_NOTREACHED() << "Invalid type " << type;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106 return std::string();
107 }
108}
109
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700110uint16_t ComputeNetworkCostByType(int type) {
111 switch (type) {
112 case rtc::ADAPTER_TYPE_ETHERNET:
113 case rtc::ADAPTER_TYPE_LOOPBACK:
114 return kNetworkCostMin;
115 case rtc::ADAPTER_TYPE_WIFI:
116 return kNetworkCostLow;
117 case rtc::ADAPTER_TYPE_CELLULAR:
118 return kNetworkCostHigh;
Qingsi Wang9f1de692018-06-28 15:38:09 -0700119 case rtc::ADAPTER_TYPE_ANY:
120 // Candidates gathered from the any-address/wildcard ports, as backups,
121 // are given the maximum cost so that if there are other candidates with
122 // known interface types, we would not select candidate pairs using these
123 // backup candidates if other selection criteria with higher precedence
124 // (network conditions over the route) are the same. Note that setting the
125 // cost to kNetworkCostUnknown would be problematic since
126 // ADAPTER_TYPE_CELLULAR would then have a higher cost. See
127 // P2PTransportChannel::SortConnectionsAndUpdateState for how we rank and
128 // select candidate pairs, where the network cost is among the criteria.
129 return kNetworkCostMax;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700130 case rtc::ADAPTER_TYPE_VPN:
131 // The cost of a VPN should be computed using its underlying network type.
132 RTC_NOTREACHED();
133 return kNetworkCostUnknown;
134 default:
135 return kNetworkCostUnknown;
136 }
137}
138
thakis06689a12015-12-30 15:05:30 -0800139#if !defined(__native_client__)
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800140bool IsIgnoredIPv6(const InterfaceAddress& ip) {
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000141 if (ip.family() != AF_INET6) {
142 return false;
143 }
144
145 // Link-local addresses require scope id to be bound successfully.
146 // However, our IPAddress structure doesn't carry that so the
147 // information is lost and causes binding failure.
148 if (IPIsLinkLocal(ip)) {
149 return true;
150 }
151
152 // Any MAC based IPv6 should be avoided to prevent the MAC tracking.
153 if (IPIsMacBased(ip)) {
154 return true;
155 }
156
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800157 // Ignore deprecated IPv6.
158 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) {
159 return true;
160 }
161
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000162 return false;
163}
thakis06689a12015-12-30 15:05:30 -0800164#endif // !defined(__native_client__)
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000165
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000166} // namespace
167
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800168// These addresses are used as the targets to find out the default local address
169// on a multi-homed endpoint. They are actually DNS servers.
170const char kPublicIPv4Host[] = "8.8.8.8";
171const char kPublicIPv6Host[] = "2001:4860:4860::8888";
172const int kPublicPort = 53; // DNS port.
173
Yves Gerey665174f2018-06-19 15:03:05 +0200174std::string MakeNetworkKey(const std::string& name,
175 const IPAddress& prefix,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 int prefix_length) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200177 rtc::StringBuilder ost;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178 ost << name << "%" << prefix.ToString() << "/" << prefix_length;
Jonas Olsson84df1c72018-09-14 16:59:32 +0200179 return ost.Release();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180}
Qingsi Wange53ac042018-05-08 11:55:07 -0700181// Test if the network name matches the type<number> pattern, e.g. eth0. The
182// matching is case-sensitive.
Steve Anton2acd1632019-03-25 13:48:30 -0700183bool MatchTypeNameWithIndexPattern(absl::string_view network_name,
184 absl::string_view type_name) {
185 if (!absl::StartsWith(network_name, type_name)) {
Qingsi Wange53ac042018-05-08 11:55:07 -0700186 return false;
187 }
Steve Anton2acd1632019-03-25 13:48:30 -0700188 return absl::c_none_of(network_name.substr(type_name.size()),
189 [](char c) { return !isdigit(c); });
Qingsi Wange53ac042018-05-08 11:55:07 -0700190}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191
Qingsi Wange53ac042018-05-08 11:55:07 -0700192// A cautious note that this method may not provide an accurate adapter type
193// based on the string matching. Incorrect type of adapters can affect the
194// result of the downstream network filtering, see e.g.
195// BasicPortAllocatorSession::GetNetworks when
196// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is turned on.
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800197AdapterType GetAdapterTypeFromName(const char* network_name) {
Qingsi Wange53ac042018-05-08 11:55:07 -0700198 if (MatchTypeNameWithIndexPattern(network_name, "lo")) {
199 // Note that we have a more robust way to determine if a network interface
200 // is a loopback interface by checking the flag IFF_LOOPBACK in ifa_flags of
201 // an ifaddr struct. See ConvertIfAddrs in this file.
202 return ADAPTER_TYPE_LOOPBACK;
203 }
Qingsi Wangc5bc9d62019-09-25 15:03:19 -0700204
Qingsi Wange53ac042018-05-08 11:55:07 -0700205 if (MatchTypeNameWithIndexPattern(network_name, "eth")) {
206 return ADAPTER_TYPE_ETHERNET;
207 }
208
Qingsi Wangc5bc9d62019-09-25 15:03:19 -0700209 if (MatchTypeNameWithIndexPattern(network_name, "wlan")) {
210 return ADAPTER_TYPE_WIFI;
211 }
212
Qingsi Wange53ac042018-05-08 11:55:07 -0700213 if (MatchTypeNameWithIndexPattern(network_name, "ipsec") ||
214 MatchTypeNameWithIndexPattern(network_name, "tun") ||
215 MatchTypeNameWithIndexPattern(network_name, "utun") ||
216 MatchTypeNameWithIndexPattern(network_name, "tap")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800217 return ADAPTER_TYPE_VPN;
218 }
219#if defined(WEBRTC_IOS)
220 // Cell networks are pdp_ipN on iOS.
Qingsi Wange53ac042018-05-08 11:55:07 -0700221 if (MatchTypeNameWithIndexPattern(network_name, "pdp_ip")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800222 return ADAPTER_TYPE_CELLULAR;
223 }
Qingsi Wange53ac042018-05-08 11:55:07 -0700224 if (MatchTypeNameWithIndexPattern(network_name, "en")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800225 // This may not be most accurate because sometimes Ethernet interface
226 // name also starts with "en" but it is better than showing it as
227 // "unknown" type.
228 // TODO(honghaiz): Write a proper IOS network manager.
229 return ADAPTER_TYPE_WIFI;
230 }
231#elif defined(WEBRTC_ANDROID)
Qingsi Wange53ac042018-05-08 11:55:07 -0700232 if (MatchTypeNameWithIndexPattern(network_name, "rmnet") ||
233 MatchTypeNameWithIndexPattern(network_name, "rmnet_data") ||
234 MatchTypeNameWithIndexPattern(network_name, "v4-rmnet") ||
Jeroen de Borst8f096d02019-02-21 13:34:45 -0800235 MatchTypeNameWithIndexPattern(network_name, "v4-rmnet_data") ||
236 MatchTypeNameWithIndexPattern(network_name, "clat")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800237 return ADAPTER_TYPE_CELLULAR;
238 }
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800239#endif
240
241 return ADAPTER_TYPE_UNKNOWN;
242}
243
Yves Gerey665174f2018-06-19 15:03:05 +0200244NetworkManager::NetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000245
Yves Gerey665174f2018-06-19 15:03:05 +0200246NetworkManager::~NetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247
Guo-wei Shieh86cb9232015-08-19 10:57:53 -0700248NetworkManager::EnumerationPermission NetworkManager::enumeration_permission()
249 const {
guoweisea1012b2015-08-21 09:06:28 -0700250 return ENUMERATION_ALLOWED;
Guo-wei Shieh86cb9232015-08-19 10:57:53 -0700251}
252
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800253bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const {
254 return false;
255}
256
Qingsi Wang7852d292018-10-31 11:17:07 -0700257webrtc::MdnsResponderInterface* NetworkManager::GetMdnsResponder() const {
Qingsi Wang09619332018-09-12 22:51:55 -0700258 return nullptr;
259}
260
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000261NetworkManagerBase::NetworkManagerBase()
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100262 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263
264NetworkManagerBase::~NetworkManagerBase() {
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000265 for (const auto& kv : networks_map_) {
266 delete kv.second;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000267 }
268}
269
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700270NetworkManager::EnumerationPermission
271NetworkManagerBase::enumeration_permission() const {
272 return enumeration_permission_;
273}
274
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000275void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) {
276 if (!ipv4_any_address_network_) {
277 const rtc::IPAddress ipv4_any_address(INADDR_ANY);
278 ipv4_any_address_network_.reset(
Qingsi Wang9f1de692018-06-28 15:38:09 -0700279 new rtc::Network("any", "any", ipv4_any_address, 0, ADAPTER_TYPE_ANY));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800280 ipv4_any_address_network_->set_default_local_address_provider(this);
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800281 ipv4_any_address_network_->set_mdns_responder_provider(this);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000282 ipv4_any_address_network_->AddIP(ipv4_any_address);
283 }
284 networks->push_back(ipv4_any_address_network_.get());
285
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100286 if (!ipv6_any_address_network_) {
287 const rtc::IPAddress ipv6_any_address(in6addr_any);
288 ipv6_any_address_network_.reset(
289 new rtc::Network("any", "any", ipv6_any_address, 0, ADAPTER_TYPE_ANY));
290 ipv6_any_address_network_->set_default_local_address_provider(this);
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800291 ipv6_any_address_network_->set_mdns_responder_provider(this);
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100292 ipv6_any_address_network_->AddIP(ipv6_any_address);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000293 }
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100294 networks->push_back(ipv6_any_address_network_.get());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000295}
296
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297void NetworkManagerBase::GetNetworks(NetworkList* result) const {
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000298 result->clear();
deadbeef3427f532017-07-26 16:09:33 -0700299 result->insert(result->begin(), networks_.begin(), networks_.end());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300}
301
302void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
303 bool* changed) {
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000304 NetworkManager::Stats stats;
305 MergeNetworkList(new_networks, changed, &stats);
306}
307
308void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
309 bool* changed,
310 NetworkManager::Stats* stats) {
honghaizdb8cf502015-12-21 13:08:46 -0800311 *changed = false;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000312 // AddressList in this map will track IP addresses for all Networks
313 // with the same key.
314 std::map<std::string, AddressList> consolidated_address_list;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315 NetworkList list(new_networks);
Steve Anton2acd1632019-03-25 13:48:30 -0700316 absl::c_sort(list, CompareNetworks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000317 // First, build a set of network-keys to the ipaddresses.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000318 for (Network* network : list) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000319 bool might_add_to_merged_list = false;
Yves Gerey665174f2018-06-19 15:03:05 +0200320 std::string key = MakeNetworkKey(network->name(), network->prefix(),
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000321 network->prefix_length());
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000322 if (consolidated_address_list.find(key) ==
323 consolidated_address_list.end()) {
324 AddressList addrlist;
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000325 addrlist.net = network;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000326 consolidated_address_list[key] = addrlist;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000327 might_add_to_merged_list = true;
328 }
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000329 const std::vector<InterfaceAddress>& addresses = network->GetIPs();
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000330 AddressList& current_list = consolidated_address_list[key];
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000331 for (const InterfaceAddress& address : addresses) {
332 current_list.ips.push_back(address);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000333 }
334 if (!might_add_to_merged_list) {
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000335 delete network;
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000336 } else {
337 if (current_list.ips[0].family() == AF_INET) {
338 stats->ipv4_network_count++;
339 } else {
nisseede5da42017-01-12 05:15:36 -0800340 RTC_DCHECK(current_list.ips[0].family() == AF_INET6);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000341 stats->ipv6_network_count++;
342 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 }
344 }
345
346 // Next, look for existing network objects to re-use.
honghaizdb8cf502015-12-21 13:08:46 -0800347 // Result of Network merge. Element in this list should have unique key.
348 NetworkList merged_list;
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000349 for (const auto& kv : consolidated_address_list) {
350 const std::string& key = kv.first;
351 Network* net = kv.second.net;
352 auto existing = networks_map_.find(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000353 if (existing == networks_map_.end()) {
354 // This network is new. Place it in the network map.
355 merged_list.push_back(net);
356 networks_map_[key] = net;
honghaiza0c44ea2016-03-23 16:07:48 -0700357 net->set_id(next_available_network_id_++);
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000358 // Also, we might have accumulated IPAddresses from the first
359 // step, set it here.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000360 net->SetIPs(kv.second.ips, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000361 *changed = true;
362 } else {
363 // This network exists in the map already. Reset its IP addresses.
honghaizdb8cf502015-12-21 13:08:46 -0800364 Network* existing_net = existing->second;
365 *changed = existing_net->SetIPs(kv.second.ips, *changed);
366 merged_list.push_back(existing_net);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700367 if (net->type() != ADAPTER_TYPE_UNKNOWN &&
368 net->type() != existing_net->type()) {
369 existing_net->set_type(net->type());
370 *changed = true;
371 }
honghaizdb8cf502015-12-21 13:08:46 -0800372 // If the existing network was not active, networks have changed.
373 if (!existing_net->active()) {
374 *changed = true;
375 }
nisseede5da42017-01-12 05:15:36 -0800376 RTC_DCHECK(net->active());
honghaizdb8cf502015-12-21 13:08:46 -0800377 if (existing_net != net) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000378 delete net;
379 }
380 }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800381 networks_map_[key]->set_mdns_responder_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000382 }
honghaizdb8cf502015-12-21 13:08:46 -0800383 // It may still happen that the merged list is a subset of |networks_|.
384 // To detect this change, we compare their sizes.
385 if (merged_list.size() != networks_.size()) {
386 *changed = true;
387 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000388
honghaizdb8cf502015-12-21 13:08:46 -0800389 // If the network list changes, we re-assign |networks_| to the merged list
390 // and re-sort it.
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000391 if (*changed) {
honghaizdb8cf502015-12-21 13:08:46 -0800392 networks_ = merged_list;
393 // Reset the active states of all networks.
394 for (const auto& kv : networks_map_) {
honghaize3c6c822016-02-17 13:00:28 -0800395 Network* network = kv.second;
396 // If |network| is in the newly generated |networks_|, it is active.
Steve Anton2acd1632019-03-25 13:48:30 -0700397 bool found = absl::c_linear_search(networks_, network);
honghaize3c6c822016-02-17 13:00:28 -0800398 network->set_active(found);
honghaizdb8cf502015-12-21 13:08:46 -0800399 }
Steve Anton2acd1632019-03-25 13:48:30 -0700400 absl::c_sort(networks_, SortNetworks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000401 // Now network interfaces are sorted, we should set the preference value
402 // for each of the interfaces we are planning to use.
403 // Preference order of network interfaces might have changed from previous
404 // sorting due to addition of higher preference network interface.
405 // Since we have already sorted the network interfaces based on our
406 // requirements, we will just assign a preference value starting with 127,
407 // in decreasing order.
408 int pref = kHighestNetworkPreference;
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000409 for (Network* network : networks_) {
410 network->set_preference(pref);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000411 if (pref > 0) {
412 --pref;
413 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100414 RTC_LOG(LS_ERROR) << "Too many network interfaces to handle!";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000415 break;
416 }
417 }
418 }
419}
420
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800421void NetworkManagerBase::set_default_local_addresses(const IPAddress& ipv4,
422 const IPAddress& ipv6) {
423 if (ipv4.family() == AF_INET) {
424 default_local_ipv4_address_ = ipv4;
425 }
426 if (ipv6.family() == AF_INET6) {
427 default_local_ipv6_address_ = ipv6;
428 }
429}
430
431bool NetworkManagerBase::GetDefaultLocalAddress(int family,
432 IPAddress* ipaddr) const {
Guo-wei Shieha34c39e2015-11-25 13:12:26 -0800433 if (family == AF_INET && !default_local_ipv4_address_.IsNil()) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800434 *ipaddr = default_local_ipv4_address_;
435 return true;
Guo-wei Shieha34c39e2015-11-25 13:12:26 -0800436 } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) {
honghaizaf83fe62016-04-18 14:50:44 -0700437 Network* ipv6_network = GetNetworkFromAddress(default_local_ipv6_address_);
438 if (ipv6_network) {
439 // If the default ipv6 network's BestIP is different than
440 // default_local_ipv6_address_, use it instead.
441 // This is to prevent potential IP address leakage. See WebRTC bug 5376.
442 *ipaddr = ipv6_network->GetBestIP();
443 } else {
444 *ipaddr = default_local_ipv6_address_;
445 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800446 return true;
447 }
448 return false;
449}
450
honghaizaf83fe62016-04-18 14:50:44 -0700451Network* NetworkManagerBase::GetNetworkFromAddress(
452 const rtc::IPAddress& ip) const {
453 for (Network* network : networks_) {
454 const auto& ips = network->GetIPs();
Steve Anton2acd1632019-03-25 13:48:30 -0700455 if (absl::c_any_of(ips, [&](const InterfaceAddress& existing_ip) {
456 return ip == static_cast<rtc::IPAddress>(existing_ip);
457 })) {
honghaizaf83fe62016-04-18 14:50:44 -0700458 return network;
459 }
460 }
461 return nullptr;
462}
463
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000464BasicNetworkManager::BasicNetworkManager()
Xavier Lepaulf1cf89b2020-02-10 13:42:26 +0100465 : thread_(nullptr), sent_first_update_(false), start_count_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000466
Yves Gerey665174f2018-06-19 15:03:05 +0200467BasicNetworkManager::~BasicNetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000468
honghaiz023f3ef2015-10-19 09:39:32 -0700469void BasicNetworkManager::OnNetworksChanged() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100470 RTC_LOG(LS_INFO) << "Network change was observed";
honghaiz023f3ef2015-10-19 09:39:32 -0700471 UpdateNetworksOnce();
472}
473
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000474#if defined(__native_client__)
475
476bool BasicNetworkManager::CreateNetworks(bool include_ignored,
477 NetworkList* networks) const {
nissec80e7412017-01-11 05:56:46 -0800478 RTC_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100479 RTC_LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000480 return false;
481}
482
483#elif defined(WEBRTC_POSIX)
484void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces,
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800485 IfAddrsConverter* ifaddrs_converter,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000486 bool include_ignored,
487 NetworkList* networks) const {
488 NetworkMap current_networks;
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800489
deadbeef37f5ecf2017-02-27 14:06:41 -0800490 for (struct ifaddrs* cursor = interfaces; cursor != nullptr;
491 cursor = cursor->ifa_next) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000492 IPAddress prefix;
493 IPAddress mask;
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800494 InterfaceAddress ip;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000495 int scope_id = 0;
496
497 // Some interfaces may not have address assigned.
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800498 if (!cursor->ifa_addr || !cursor->ifa_netmask) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000499 continue;
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800500 }
501 // Skip ones which are down.
502 if (!(cursor->ifa_flags & IFF_RUNNING)) {
503 continue;
504 }
505 // Skip unknown family.
506 if (cursor->ifa_addr->sa_family != AF_INET &&
507 cursor->ifa_addr->sa_family != AF_INET6) {
508 continue;
509 }
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800510 // Convert to InterfaceAddress.
511 if (!ifaddrs_converter->ConvertIfAddrsToIPAddress(cursor, &ip, &mask)) {
512 continue;
513 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000514
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800515 // Special case for IPv6 address.
516 if (cursor->ifa_addr->sa_family == AF_INET6) {
517 if (IsIgnoredIPv6(ip)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000518 continue;
519 }
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800520 scope_id =
521 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000522 }
523
Honghai Zhang351d77b2016-05-20 15:08:29 -0700524 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700525 AdapterType vpn_underlying_adapter_type = ADAPTER_TYPE_UNKNOWN;
Honghai Zhang351d77b2016-05-20 15:08:29 -0700526 if (cursor->ifa_flags & IFF_LOOPBACK) {
527 adapter_type = ADAPTER_TYPE_LOOPBACK;
528 } else {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800529 // If there is a network_monitor, use it to get the adapter type.
530 // Otherwise, get the adapter type based on a few name matching rules.
531 if (network_monitor_) {
532 adapter_type = network_monitor_->GetAdapterType(cursor->ifa_name);
533 }
534 if (adapter_type == ADAPTER_TYPE_UNKNOWN) {
535 adapter_type = GetAdapterTypeFromName(cursor->ifa_name);
536 }
Honghai Zhang351d77b2016-05-20 15:08:29 -0700537 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700538
539 if (adapter_type == ADAPTER_TYPE_VPN && network_monitor_) {
540 vpn_underlying_adapter_type =
541 network_monitor_->GetVpnUnderlyingAdapterType(cursor->ifa_name);
542 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543 int prefix_length = CountIPMaskBits(mask);
544 prefix = TruncateIP(ip, prefix_length);
Yves Gerey665174f2018-06-19 15:03:05 +0200545 std::string key =
546 MakeNetworkKey(std::string(cursor->ifa_name), prefix, prefix_length);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700547 auto iter = current_networks.find(key);
548 if (iter == current_networks.end()) {
Noah Richards2e266e92015-04-07 11:51:56 -0700549 // TODO(phoglund): Need to recognize other types as well.
jbauch555604a2016-04-26 03:13:22 -0700550 std::unique_ptr<Network> network(
551 new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length,
552 adapter_type));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800553 network->set_default_local_address_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000554 network->set_scope_id(scope_id);
555 network->AddIP(ip);
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000556 network->set_ignored(IsIgnoredNetwork(*network));
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700557 network->set_underlying_type_for_vpn(vpn_underlying_adapter_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558 if (include_ignored || !network->ignored()) {
honghaizdb8cf502015-12-21 13:08:46 -0800559 current_networks[key] = network.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000560 networks->push_back(network.release());
561 }
562 } else {
Honghai Zhang351d77b2016-05-20 15:08:29 -0700563 Network* existing_network = iter->second;
564 existing_network->AddIP(ip);
565 if (adapter_type != ADAPTER_TYPE_UNKNOWN) {
566 existing_network->set_type(adapter_type);
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700567 existing_network->set_underlying_type_for_vpn(
568 vpn_underlying_adapter_type);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700569 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000570 }
571 }
572}
573
574bool BasicNetworkManager::CreateNetworks(bool include_ignored,
575 NetworkList* networks) const {
576 struct ifaddrs* interfaces;
577 int error = getifaddrs(&interfaces);
578 if (error != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100579 RTC_LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: "
580 << error;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000581 return false;
582 }
583
jbauch555604a2016-04-26 03:13:22 -0700584 std::unique_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter());
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800585 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored,
586 networks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000587
588 freeifaddrs(interfaces);
589 return true;
590}
591
592#elif defined(WEBRTC_WIN)
593
594unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist,
Yves Gerey665174f2018-06-19 15:03:05 +0200595 const IPAddress& ip,
596 IPAddress* prefix) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000597 IPAddress current_prefix;
598 IPAddress best_prefix;
599 unsigned int best_length = 0;
600 while (prefixlist) {
601 // Look for the longest matching prefix in the prefixlist.
deadbeef37f5ecf2017-02-27 14:06:41 -0800602 if (prefixlist->Address.lpSockaddr == nullptr ||
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000603 prefixlist->Address.lpSockaddr->sa_family != ip.family()) {
604 prefixlist = prefixlist->Next;
605 continue;
606 }
607 switch (prefixlist->Address.lpSockaddr->sa_family) {
608 case AF_INET: {
609 sockaddr_in* v4_addr =
610 reinterpret_cast<sockaddr_in*>(prefixlist->Address.lpSockaddr);
611 current_prefix = IPAddress(v4_addr->sin_addr);
612 break;
613 }
614 case AF_INET6: {
Yves Gerey665174f2018-06-19 15:03:05 +0200615 sockaddr_in6* v6_addr =
616 reinterpret_cast<sockaddr_in6*>(prefixlist->Address.lpSockaddr);
617 current_prefix = IPAddress(v6_addr->sin6_addr);
618 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000619 }
620 default: {
621 prefixlist = prefixlist->Next;
622 continue;
623 }
624 }
625 if (TruncateIP(ip, prefixlist->PrefixLength) == current_prefix &&
626 prefixlist->PrefixLength > best_length) {
627 best_prefix = current_prefix;
628 best_length = prefixlist->PrefixLength;
629 }
630 prefixlist = prefixlist->Next;
631 }
632 *prefix = best_prefix;
633 return best_length;
634}
635
636bool BasicNetworkManager::CreateNetworks(bool include_ignored,
637 NetworkList* networks) const {
638 NetworkMap current_networks;
639 // MSDN recommends a 15KB buffer for the first try at GetAdaptersAddresses.
640 size_t buffer_size = 16384;
jbauch555604a2016-04-26 03:13:22 -0700641 std::unique_ptr<char[]> adapter_info(new char[buffer_size]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000642 PIP_ADAPTER_ADDRESSES adapter_addrs =
643 reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
644 int adapter_flags = (GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST |
645 GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_INCLUDE_PREFIX);
646 int ret = 0;
647 do {
648 adapter_info.reset(new char[buffer_size]);
649 adapter_addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
Yves Gerey665174f2018-06-19 15:03:05 +0200650 ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags, 0, adapter_addrs,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000651 reinterpret_cast<PULONG>(&buffer_size));
652 } while (ret == ERROR_BUFFER_OVERFLOW);
653 if (ret != ERROR_SUCCESS) {
654 return false;
655 }
656 int count = 0;
657 while (adapter_addrs) {
658 if (adapter_addrs->OperStatus == IfOperStatusUp) {
659 PIP_ADAPTER_UNICAST_ADDRESS address = adapter_addrs->FirstUnicastAddress;
660 PIP_ADAPTER_PREFIX prefixlist = adapter_addrs->FirstPrefix;
661 std::string name;
662 std::string description;
tfarinaa41ab932015-10-30 16:08:48 -0700663#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000664 name = ToUtf8(adapter_addrs->FriendlyName,
665 wcslen(adapter_addrs->FriendlyName));
666#endif
667 description = ToUtf8(adapter_addrs->Description,
668 wcslen(adapter_addrs->Description));
669 for (; address; address = address->Next) {
tfarinaa41ab932015-10-30 16:08:48 -0700670#if defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000671 name = rtc::ToString(count);
672#endif
673
674 IPAddress ip;
675 int scope_id = 0;
jbauch555604a2016-04-26 03:13:22 -0700676 std::unique_ptr<Network> network;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000677 switch (address->Address.lpSockaddr->sa_family) {
678 case AF_INET: {
679 sockaddr_in* v4_addr =
680 reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
681 ip = IPAddress(v4_addr->sin_addr);
682 break;
683 }
684 case AF_INET6: {
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100685 sockaddr_in6* v6_addr =
686 reinterpret_cast<sockaddr_in6*>(address->Address.lpSockaddr);
687 scope_id = v6_addr->sin6_scope_id;
688 ip = IPAddress(v6_addr->sin6_addr);
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000689
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100690 if (IsIgnoredIPv6(InterfaceAddress(ip))) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000691 continue;
692 }
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100693
694 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000695 }
Jeroen de Borst8f096d02019-02-21 13:34:45 -0800696 default: {
697 continue;
698 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000699 }
700
701 IPAddress prefix;
702 int prefix_length = GetPrefix(prefixlist, ip, &prefix);
703 std::string key = MakeNetworkKey(name, prefix, prefix_length);
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000704 auto existing_network = current_networks.find(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000705 if (existing_network == current_networks.end()) {
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000706 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
Yura Yaroshevich0ce868c2018-05-15 11:15:32 +0300707 switch (adapter_addrs->IfType) {
708 case IF_TYPE_SOFTWARE_LOOPBACK:
709 adapter_type = ADAPTER_TYPE_LOOPBACK;
710 break;
711 case IF_TYPE_ETHERNET_CSMACD:
712 case IF_TYPE_ETHERNET_3MBIT:
713 case IF_TYPE_IEEE80212:
714 case IF_TYPE_FASTETHER:
715 case IF_TYPE_FASTETHER_FX:
716 case IF_TYPE_GIGABITETHERNET:
717 adapter_type = ADAPTER_TYPE_ETHERNET;
718 break;
719 case IF_TYPE_IEEE80211:
720 adapter_type = ADAPTER_TYPE_WIFI;
721 break;
722 case IF_TYPE_WWANPP:
723 case IF_TYPE_WWANPP2:
724 adapter_type = ADAPTER_TYPE_CELLULAR;
725 break;
726 default:
727 // TODO(phoglund): Need to recognize other types as well.
728 adapter_type = ADAPTER_TYPE_UNKNOWN;
729 break;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000730 }
jbauch555604a2016-04-26 03:13:22 -0700731 std::unique_ptr<Network> network(new Network(
732 name, description, prefix, prefix_length, adapter_type));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800733 network->set_default_local_address_provider(this);
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800734 network->set_mdns_responder_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000735 network->set_scope_id(scope_id);
736 network->AddIP(ip);
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000737 bool ignored = IsIgnoredNetwork(*network);
738 network->set_ignored(ignored);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000739 if (include_ignored || !network->ignored()) {
honghaizdb8cf502015-12-21 13:08:46 -0800740 current_networks[key] = network.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000741 networks->push_back(network.release());
742 }
743 } else {
744 (*existing_network).second->AddIP(ip);
745 }
746 }
747 // Count is per-adapter - all 'Networks' created from the same
748 // adapter need to have the same name.
749 ++count;
750 }
751 adapter_addrs = adapter_addrs->Next;
752 }
753 return true;
754}
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000755#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000756
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000757bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const {
758 // Ignore networks on the explicit ignore list.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000759 for (const std::string& ignored_name : network_ignore_list_) {
760 if (network.name() == ignored_name) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000761 return true;
762 }
763 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000764
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000765#if defined(WEBRTC_POSIX)
pthatcher@webrtc.orgdfef0282015-01-07 17:20:52 +0000766 // Filter out VMware/VirtualBox interfaces, typically named vmnet1,
767 // vmnet8, or vboxnet0.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000768 if (strncmp(network.name().c_str(), "vmnet", 5) == 0 ||
pthatcher@webrtc.orgdfef0282015-01-07 17:20:52 +0000769 strncmp(network.name().c_str(), "vnic", 4) == 0 ||
770 strncmp(network.name().c_str(), "vboxnet", 7) == 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000771 return true;
772 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000773#elif defined(WEBRTC_WIN)
774 // Ignore any HOST side vmware adapters with a description like:
775 // VMware Virtual Ethernet Adapter for VMnet1
776 // but don't ignore any GUEST side adapters with a description like:
777 // VMware Accelerated AMD PCNet Adapter #2
deadbeef37f5ecf2017-02-27 14:06:41 -0800778 if (strstr(network.description().c_str(), "VMnet") != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000779 return true;
780 }
781#endif
782
783 // Ignore any networks with a 0.x.y.z IP
784 if (network.prefix().family() == AF_INET) {
785 return (network.prefix().v4AddressAsHostOrderInteger() < 0x01000000);
786 }
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000787
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000788 return false;
789}
790
791void BasicNetworkManager::StartUpdating() {
792 thread_ = Thread::Current();
793 if (start_count_) {
794 // If network interfaces are already discovered and signal is sent,
795 // we should trigger network signal immediately for the new clients
796 // to start allocating ports.
797 if (sent_first_update_)
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700798 thread_->Post(RTC_FROM_HERE, this, kSignalNetworksMessage);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000799 } else {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700800 thread_->Post(RTC_FROM_HERE, this, kUpdateNetworksMessage);
honghaiz023f3ef2015-10-19 09:39:32 -0700801 StartNetworkMonitor();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000802 }
803 ++start_count_;
804}
805
806void BasicNetworkManager::StopUpdating() {
nisseede5da42017-01-12 05:15:36 -0800807 RTC_DCHECK(Thread::Current() == thread_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000808 if (!start_count_)
809 return;
810
811 --start_count_;
812 if (!start_count_) {
813 thread_->Clear(this);
814 sent_first_update_ = false;
honghaiz023f3ef2015-10-19 09:39:32 -0700815 StopNetworkMonitor();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000816 }
817}
818
honghaiz023f3ef2015-10-19 09:39:32 -0700819void BasicNetworkManager::StartNetworkMonitor() {
820 NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory();
821 if (factory == nullptr) {
822 return;
823 }
honghaiz023f3ef2015-10-19 09:39:32 -0700824 if (!network_monitor_) {
honghaizcec0a082016-01-15 14:49:09 -0800825 network_monitor_.reset(factory->CreateNetworkMonitor());
826 if (!network_monitor_) {
827 return;
828 }
skvlad979c2682016-06-10 15:24:13 -0700829 network_monitor_->SignalNetworksChanged.connect(
830 this, &BasicNetworkManager::OnNetworksChanged);
honghaiz023f3ef2015-10-19 09:39:32 -0700831 }
honghaiz023f3ef2015-10-19 09:39:32 -0700832 network_monitor_->Start();
833}
834
835void BasicNetworkManager::StopNetworkMonitor() {
836 if (!network_monitor_) {
837 return;
838 }
839 network_monitor_->Stop();
honghaiz023f3ef2015-10-19 09:39:32 -0700840}
841
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000842void BasicNetworkManager::OnMessage(Message* msg) {
843 switch (msg->message_id) {
honghaiz023f3ef2015-10-19 09:39:32 -0700844 case kUpdateNetworksMessage: {
845 UpdateNetworksContinually();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000846 break;
847 }
Yves Gerey665174f2018-06-19 15:03:05 +0200848 case kSignalNetworksMessage: {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000849 SignalNetworksChanged();
850 break;
851 }
852 default:
nissec80e7412017-01-11 05:56:46 -0800853 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000854 }
855}
856
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800857IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const {
nisseede5da42017-01-12 05:15:36 -0800858 RTC_DCHECK(thread_ == Thread::Current());
859 RTC_DCHECK(thread_->socketserver() != nullptr);
860 RTC_DCHECK(family == AF_INET || family == AF_INET6);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800861
jbauch555604a2016-04-26 03:13:22 -0700862 std::unique_ptr<AsyncSocket> socket(
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800863 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM));
864 if (!socket) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100865 RTC_LOG_ERR(LERROR) << "Socket creation failed";
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800866 return IPAddress();
867 }
868
guoweis56271ed2016-01-15 14:45:06 -0800869 if (socket->Connect(SocketAddress(
870 family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort)) <
871 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200872 if (socket->GetError() != ENETUNREACH &&
873 socket->GetError() != EHOSTUNREACH) {
skvlad75024012016-09-29 12:59:35 -0700874 // Ignore the expected case of "host/net unreachable" - which happens if
875 // the network is V4- or V6-only.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100876 RTC_LOG(LS_INFO) << "Connect failed with " << socket->GetError();
skvlad75024012016-09-29 12:59:35 -0700877 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800878 return IPAddress();
879 }
880 return socket->GetLocalAddress().ipaddr();
881}
882
honghaiz023f3ef2015-10-19 09:39:32 -0700883void BasicNetworkManager::UpdateNetworksOnce() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000884 if (!start_count_)
885 return;
886
nisseede5da42017-01-12 05:15:36 -0800887 RTC_DCHECK(Thread::Current() == thread_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000888
889 NetworkList list;
890 if (!CreateNetworks(false, &list)) {
891 SignalError();
892 } else {
893 bool changed;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800894 NetworkManager::Stats stats;
895 MergeNetworkList(list, &changed, &stats);
896 set_default_local_addresses(QueryDefaultLocalAddress(AF_INET),
897 QueryDefaultLocalAddress(AF_INET6));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000898 if (changed || !sent_first_update_) {
899 SignalNetworksChanged();
900 sent_first_update_ = true;
901 }
902 }
honghaiz023f3ef2015-10-19 09:39:32 -0700903}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000904
honghaiz023f3ef2015-10-19 09:39:32 -0700905void BasicNetworkManager::UpdateNetworksContinually() {
906 UpdateNetworksOnce();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700907 thread_->PostDelayed(RTC_FROM_HERE, kNetworksUpdateIntervalMs, this,
908 kUpdateNetworksMessage);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000909}
910
honghaizdb8cf502015-12-21 13:08:46 -0800911void BasicNetworkManager::DumpNetworks() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000912 NetworkList list;
honghaizdb8cf502015-12-21 13:08:46 -0800913 GetNetworks(&list);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100914 RTC_LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:";
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000915 for (const Network* network : list) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100916 RTC_LOG(LS_INFO) << network->ToString() << ": " << network->description()
917 << ", active ? " << network->active()
918 << ((network->ignored()) ? ", Ignored" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000919 }
920}
921
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800922Network::Network(const std::string& name,
923 const std::string& desc,
924 const IPAddress& prefix,
925 int prefix_length)
926 : name_(name),
927 description_(desc),
928 prefix_(prefix),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000929 prefix_length_(prefix_length),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800930 key_(MakeNetworkKey(name, prefix, prefix_length)),
931 scope_id_(0),
932 ignored_(false),
933 type_(ADAPTER_TYPE_UNKNOWN),
934 preference_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000935
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800936Network::Network(const std::string& name,
937 const std::string& desc,
938 const IPAddress& prefix,
939 int prefix_length,
940 AdapterType type)
941 : name_(name),
942 description_(desc),
943 prefix_(prefix),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000944 prefix_length_(prefix_length),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800945 key_(MakeNetworkKey(name, prefix, prefix_length)),
946 scope_id_(0),
947 ignored_(false),
948 type_(type),
949 preference_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000950
Steve Anton9de3aac2017-10-24 10:08:26 -0700951Network::Network(const Network&) = default;
952
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000953Network::~Network() = default;
954
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000955// Sets the addresses of this network. Returns true if the address set changed.
956// Change detection is short circuited if the changed argument is true.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000957bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000958 // Detect changes with a nested loop; n-squared but we expect on the order
959 // of 2-3 addresses per network.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000960 changed = changed || ips.size() != ips_.size();
961 if (!changed) {
962 for (const InterfaceAddress& ip : ips) {
Steve Anton2acd1632019-03-25 13:48:30 -0700963 if (!absl::c_linear_search(ips_, ip)) {
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000964 changed = true;
965 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000966 }
967 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000968 }
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000969
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000970 ips_ = ips;
971 return changed;
972}
973
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000974// Select the best IP address to use from this Network.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000975IPAddress Network::GetBestIP() const {
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000976 if (ips_.size() == 0) {
977 return IPAddress();
978 }
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000979
980 if (prefix_.family() == AF_INET) {
981 return static_cast<IPAddress>(ips_.at(0));
982 }
983
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000984 InterfaceAddress selected_ip, ula_ip;
985
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000986 for (const InterfaceAddress& ip : ips_) {
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000987 // Ignore any address which has been deprecated already.
988 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED)
989 continue;
990
991 // ULA address should only be returned when we have no other
992 // global IP.
993 if (IPIsULA(static_cast<const IPAddress&>(ip))) {
994 ula_ip = ip;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000995 continue;
996 }
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000997 selected_ip = ip;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000998
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000999 // Search could stop once a temporary non-deprecated one is found.
1000 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY)
1001 break;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001002 }
1003
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001004 // No proper global IPv6 address found, use ULA instead.
1005 if (IPIsUnspec(selected_ip) && !IPIsUnspec(ula_ip)) {
1006 selected_ip = ula_ip;
1007 }
1008
1009 return static_cast<IPAddress>(selected_ip);
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001010}
1011
Qingsi Wang5ae259e2019-02-13 15:46:07 -08001012webrtc::MdnsResponderInterface* Network::GetMdnsResponder() const {
1013 if (mdns_responder_provider_ == nullptr) {
1014 return nullptr;
1015 }
1016 return mdns_responder_provider_->GetMdnsResponder();
1017}
1018
Qingsi Wangde2ed7d2018-04-27 14:25:37 -07001019uint16_t Network::GetCost() const {
1020 AdapterType type = IsVpn() ? underlying_type_for_vpn_ : type_;
1021 return ComputeNetworkCostByType(type);
1022}
1023
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001024std::string Network::ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001025 rtc::StringBuilder ss;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001026 // Print out the first space-terminated token of the network desc, plus
1027 // the IP address.
Qingsi Wangde2ed7d2018-04-27 14:25:37 -07001028 ss << "Net[" << description_.substr(0, description_.find(' ')) << ":"
1029 << prefix_.ToSensitiveString() << "/" << prefix_length_ << ":"
1030 << AdapterTypeToString(type_);
1031 if (IsVpn()) {
1032 ss << "/" << AdapterTypeToString(underlying_type_for_vpn_);
1033 }
Qingsi Wang10a0e512018-05-16 13:37:03 -07001034 ss << ":id=" << id_ << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +02001035 return ss.Release();
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +00001036}
1037
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001038} // namespace rtc