blob: 6dddbc0caaeba72f4d166f97679de23d2814f6f6 [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)
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.orgf0488722014-05-13 18:00:26 +000023#endif // WEBRTC_POSIX
24
25#if defined(WEBRTC_WIN)
Mirko Bonadei01914412018-03-23 16:30:58 +010026#include <iphlpapi.h>
Yves Gerey665174f2018-06-19 15:03:05 +020027#include "rtc_base/win32.h"
Guo-wei Shieh9faf1542015-12-28 14:06:55 -080028#elif !defined(__native_client__)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/ifaddrs_converter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030#endif
31
32#include <stdio.h>
33
jbauch555604a2016-04-26 03:13:22 -070034#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
Steve Anton2acd1632019-03-25 13:48:30 -070036#include "absl/algorithm/container.h"
37#include "absl/strings/match.h"
38#include "absl/strings/string_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "rtc_base/checks.h"
40#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080041#include "rtc_base/network_monitor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "rtc_base/socket.h" // includes something that makes windows happy
Steve Anton10542f22019-01-11 09:11:00 -080043#include "rtc_base/string_encode.h"
44#include "rtc_base/string_utils.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020045#include "rtc_base/strings/string_builder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047
48namespace rtc {
49namespace {
50
Peter Boström0c4e06b2015-10-07 12:23:21 +020051const uint32_t kUpdateNetworksMessage = 1;
52const uint32_t kSignalNetworksMessage = 2;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053
54// Fetch list of networks every two seconds.
55const int kNetworksUpdateIntervalMs = 2000;
56
57const int kHighestNetworkPreference = 127;
58
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +000059typedef struct {
60 Network* net;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +000061 std::vector<InterfaceAddress> ips;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +000062} AddressList;
63
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064bool CompareNetworks(const Network* a, const Network* b) {
65 if (a->prefix_length() == b->prefix_length()) {
66 if (a->name() == b->name()) {
67 return a->prefix() < b->prefix();
68 }
69 }
70 return a->name() < b->name();
71}
72
73bool SortNetworks(const Network* a, const Network* b) {
74 // Network types will be preferred above everything else while sorting
75 // Networks.
76
77 // Networks are sorted first by type.
78 if (a->type() != b->type()) {
79 return a->type() < b->type();
80 }
81
guoweis@webrtc.org369a6372014-09-17 22:37:29 +000082 IPAddress ip_a = a->GetBestIP();
83 IPAddress ip_b = b->GetBestIP();
84
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 // After type, networks are sorted by IP address precedence values
86 // from RFC 3484-bis
guoweis@webrtc.org369a6372014-09-17 22:37:29 +000087 if (IPAddressPrecedence(ip_a) != IPAddressPrecedence(ip_b)) {
88 return IPAddressPrecedence(ip_a) > IPAddressPrecedence(ip_b);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 }
90
91 // TODO(mallinath) - Add VPN and Link speed conditions while sorting.
92
93 // Networks are sorted last by key.
deadbeef3427f532017-07-26 16:09:33 -070094 return a->key() < b->key();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095}
96
97std::string AdapterTypeToString(AdapterType type) {
98 switch (type) {
Qingsi Wang9f1de692018-06-28 15:38:09 -070099 case ADAPTER_TYPE_ANY:
Qingsi Wang01560de2018-07-26 10:44:02 -0700100 return "Wildcard";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 case ADAPTER_TYPE_UNKNOWN:
102 return "Unknown";
103 case ADAPTER_TYPE_ETHERNET:
104 return "Ethernet";
105 case ADAPTER_TYPE_WIFI:
106 return "Wifi";
107 case ADAPTER_TYPE_CELLULAR:
108 return "Cellular";
109 case ADAPTER_TYPE_VPN:
110 return "VPN";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000111 case ADAPTER_TYPE_LOOPBACK:
112 return "Loopback";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800114 RTC_NOTREACHED() << "Invalid type " << type;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 return std::string();
116 }
117}
118
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700119uint16_t ComputeNetworkCostByType(int type) {
120 switch (type) {
121 case rtc::ADAPTER_TYPE_ETHERNET:
122 case rtc::ADAPTER_TYPE_LOOPBACK:
123 return kNetworkCostMin;
124 case rtc::ADAPTER_TYPE_WIFI:
125 return kNetworkCostLow;
126 case rtc::ADAPTER_TYPE_CELLULAR:
127 return kNetworkCostHigh;
Qingsi Wang9f1de692018-06-28 15:38:09 -0700128 case rtc::ADAPTER_TYPE_ANY:
129 // Candidates gathered from the any-address/wildcard ports, as backups,
130 // are given the maximum cost so that if there are other candidates with
131 // known interface types, we would not select candidate pairs using these
132 // backup candidates if other selection criteria with higher precedence
133 // (network conditions over the route) are the same. Note that setting the
134 // cost to kNetworkCostUnknown would be problematic since
135 // ADAPTER_TYPE_CELLULAR would then have a higher cost. See
136 // P2PTransportChannel::SortConnectionsAndUpdateState for how we rank and
137 // select candidate pairs, where the network cost is among the criteria.
138 return kNetworkCostMax;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700139 case rtc::ADAPTER_TYPE_VPN:
140 // The cost of a VPN should be computed using its underlying network type.
141 RTC_NOTREACHED();
142 return kNetworkCostUnknown;
143 default:
144 return kNetworkCostUnknown;
145 }
146}
147
thakis06689a12015-12-30 15:05:30 -0800148#if !defined(__native_client__)
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800149bool IsIgnoredIPv6(const InterfaceAddress& ip) {
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000150 if (ip.family() != AF_INET6) {
151 return false;
152 }
153
154 // Link-local addresses require scope id to be bound successfully.
155 // However, our IPAddress structure doesn't carry that so the
156 // information is lost and causes binding failure.
157 if (IPIsLinkLocal(ip)) {
158 return true;
159 }
160
161 // Any MAC based IPv6 should be avoided to prevent the MAC tracking.
162 if (IPIsMacBased(ip)) {
163 return true;
164 }
165
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800166 // Ignore deprecated IPv6.
167 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) {
168 return true;
169 }
170
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000171 return false;
172}
thakis06689a12015-12-30 15:05:30 -0800173#endif // !defined(__native_client__)
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000174
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175} // namespace
176
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800177// These addresses are used as the targets to find out the default local address
178// on a multi-homed endpoint. They are actually DNS servers.
179const char kPublicIPv4Host[] = "8.8.8.8";
180const char kPublicIPv6Host[] = "2001:4860:4860::8888";
181const int kPublicPort = 53; // DNS port.
182
Yves Gerey665174f2018-06-19 15:03:05 +0200183std::string MakeNetworkKey(const std::string& name,
184 const IPAddress& prefix,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185 int prefix_length) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200186 rtc::StringBuilder ost;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187 ost << name << "%" << prefix.ToString() << "/" << prefix_length;
Jonas Olsson84df1c72018-09-14 16:59:32 +0200188 return ost.Release();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000189}
Qingsi Wange53ac042018-05-08 11:55:07 -0700190// Test if the network name matches the type<number> pattern, e.g. eth0. The
191// matching is case-sensitive.
Steve Anton2acd1632019-03-25 13:48:30 -0700192bool MatchTypeNameWithIndexPattern(absl::string_view network_name,
193 absl::string_view type_name) {
194 if (!absl::StartsWith(network_name, type_name)) {
Qingsi Wange53ac042018-05-08 11:55:07 -0700195 return false;
196 }
Steve Anton2acd1632019-03-25 13:48:30 -0700197 return absl::c_none_of(network_name.substr(type_name.size()),
198 [](char c) { return !isdigit(c); });
Qingsi Wange53ac042018-05-08 11:55:07 -0700199}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200
Qingsi Wange53ac042018-05-08 11:55:07 -0700201// A cautious note that this method may not provide an accurate adapter type
202// based on the string matching. Incorrect type of adapters can affect the
203// result of the downstream network filtering, see e.g.
204// BasicPortAllocatorSession::GetNetworks when
205// PORTALLOCATOR_DISABLE_COSTLY_NETWORKS is turned on.
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800206AdapterType GetAdapterTypeFromName(const char* network_name) {
Qingsi Wange53ac042018-05-08 11:55:07 -0700207 if (MatchTypeNameWithIndexPattern(network_name, "lo")) {
208 // Note that we have a more robust way to determine if a network interface
209 // is a loopback interface by checking the flag IFF_LOOPBACK in ifa_flags of
210 // an ifaddr struct. See ConvertIfAddrs in this file.
211 return ADAPTER_TYPE_LOOPBACK;
212 }
213 if (MatchTypeNameWithIndexPattern(network_name, "eth")) {
214 return ADAPTER_TYPE_ETHERNET;
215 }
216
217 if (MatchTypeNameWithIndexPattern(network_name, "ipsec") ||
218 MatchTypeNameWithIndexPattern(network_name, "tun") ||
219 MatchTypeNameWithIndexPattern(network_name, "utun") ||
220 MatchTypeNameWithIndexPattern(network_name, "tap")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800221 return ADAPTER_TYPE_VPN;
222 }
223#if defined(WEBRTC_IOS)
224 // Cell networks are pdp_ipN on iOS.
Qingsi Wange53ac042018-05-08 11:55:07 -0700225 if (MatchTypeNameWithIndexPattern(network_name, "pdp_ip")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800226 return ADAPTER_TYPE_CELLULAR;
227 }
Qingsi Wange53ac042018-05-08 11:55:07 -0700228 if (MatchTypeNameWithIndexPattern(network_name, "en")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800229 // This may not be most accurate because sometimes Ethernet interface
230 // name also starts with "en" but it is better than showing it as
231 // "unknown" type.
232 // TODO(honghaiz): Write a proper IOS network manager.
233 return ADAPTER_TYPE_WIFI;
234 }
235#elif defined(WEBRTC_ANDROID)
Qingsi Wange53ac042018-05-08 11:55:07 -0700236 if (MatchTypeNameWithIndexPattern(network_name, "rmnet") ||
237 MatchTypeNameWithIndexPattern(network_name, "rmnet_data") ||
238 MatchTypeNameWithIndexPattern(network_name, "v4-rmnet") ||
Jeroen de Borst8f096d02019-02-21 13:34:45 -0800239 MatchTypeNameWithIndexPattern(network_name, "v4-rmnet_data") ||
240 MatchTypeNameWithIndexPattern(network_name, "clat")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800241 return ADAPTER_TYPE_CELLULAR;
242 }
Qingsi Wange53ac042018-05-08 11:55:07 -0700243 if (MatchTypeNameWithIndexPattern(network_name, "wlan")) {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800244 return ADAPTER_TYPE_WIFI;
245 }
246#endif
247
248 return ADAPTER_TYPE_UNKNOWN;
249}
250
Yves Gerey665174f2018-06-19 15:03:05 +0200251NetworkManager::NetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000252
Yves Gerey665174f2018-06-19 15:03:05 +0200253NetworkManager::~NetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254
Guo-wei Shieh86cb9232015-08-19 10:57:53 -0700255NetworkManager::EnumerationPermission NetworkManager::enumeration_permission()
256 const {
guoweisea1012b2015-08-21 09:06:28 -0700257 return ENUMERATION_ALLOWED;
Guo-wei Shieh86cb9232015-08-19 10:57:53 -0700258}
259
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800260bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const {
261 return false;
262}
263
Qingsi Wang7852d292018-10-31 11:17:07 -0700264webrtc::MdnsResponderInterface* NetworkManager::GetMdnsResponder() const {
Qingsi Wang09619332018-09-12 22:51:55 -0700265 return nullptr;
266}
267
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000268NetworkManagerBase::NetworkManagerBase()
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100269 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000270
271NetworkManagerBase::~NetworkManagerBase() {
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000272 for (const auto& kv : networks_map_) {
273 delete kv.second;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000274 }
275}
276
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700277NetworkManager::EnumerationPermission
278NetworkManagerBase::enumeration_permission() const {
279 return enumeration_permission_;
280}
281
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000282void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) {
283 if (!ipv4_any_address_network_) {
284 const rtc::IPAddress ipv4_any_address(INADDR_ANY);
285 ipv4_any_address_network_.reset(
Qingsi Wang9f1de692018-06-28 15:38:09 -0700286 new rtc::Network("any", "any", ipv4_any_address, 0, ADAPTER_TYPE_ANY));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800287 ipv4_any_address_network_->set_default_local_address_provider(this);
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800288 ipv4_any_address_network_->set_mdns_responder_provider(this);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000289 ipv4_any_address_network_->AddIP(ipv4_any_address);
290 }
291 networks->push_back(ipv4_any_address_network_.get());
292
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100293 if (!ipv6_any_address_network_) {
294 const rtc::IPAddress ipv6_any_address(in6addr_any);
295 ipv6_any_address_network_.reset(
296 new rtc::Network("any", "any", ipv6_any_address, 0, ADAPTER_TYPE_ANY));
297 ipv6_any_address_network_->set_default_local_address_provider(this);
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800298 ipv6_any_address_network_->set_mdns_responder_provider(this);
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100299 ipv6_any_address_network_->AddIP(ipv6_any_address);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000300 }
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100301 networks->push_back(ipv6_any_address_network_.get());
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000302}
303
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304void NetworkManagerBase::GetNetworks(NetworkList* result) const {
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000305 result->clear();
deadbeef3427f532017-07-26 16:09:33 -0700306 result->insert(result->begin(), networks_.begin(), networks_.end());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000307}
308
309void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
310 bool* changed) {
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000311 NetworkManager::Stats stats;
312 MergeNetworkList(new_networks, changed, &stats);
313}
314
315void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
316 bool* changed,
317 NetworkManager::Stats* stats) {
honghaizdb8cf502015-12-21 13:08:46 -0800318 *changed = false;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000319 // AddressList in this map will track IP addresses for all Networks
320 // with the same key.
321 std::map<std::string, AddressList> consolidated_address_list;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000322 NetworkList list(new_networks);
Steve Anton2acd1632019-03-25 13:48:30 -0700323 absl::c_sort(list, CompareNetworks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000324 // First, build a set of network-keys to the ipaddresses.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000325 for (Network* network : list) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000326 bool might_add_to_merged_list = false;
Yves Gerey665174f2018-06-19 15:03:05 +0200327 std::string key = MakeNetworkKey(network->name(), network->prefix(),
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000328 network->prefix_length());
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000329 if (consolidated_address_list.find(key) ==
330 consolidated_address_list.end()) {
331 AddressList addrlist;
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000332 addrlist.net = network;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000333 consolidated_address_list[key] = addrlist;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000334 might_add_to_merged_list = true;
335 }
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000336 const std::vector<InterfaceAddress>& addresses = network->GetIPs();
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000337 AddressList& current_list = consolidated_address_list[key];
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000338 for (const InterfaceAddress& address : addresses) {
339 current_list.ips.push_back(address);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340 }
341 if (!might_add_to_merged_list) {
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000342 delete network;
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000343 } else {
344 if (current_list.ips[0].family() == AF_INET) {
345 stats->ipv4_network_count++;
346 } else {
nisseede5da42017-01-12 05:15:36 -0800347 RTC_DCHECK(current_list.ips[0].family() == AF_INET6);
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000348 stats->ipv6_network_count++;
349 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000350 }
351 }
352
353 // Next, look for existing network objects to re-use.
honghaizdb8cf502015-12-21 13:08:46 -0800354 // Result of Network merge. Element in this list should have unique key.
355 NetworkList merged_list;
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000356 for (const auto& kv : consolidated_address_list) {
357 const std::string& key = kv.first;
358 Network* net = kv.second.net;
359 auto existing = networks_map_.find(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000360 if (existing == networks_map_.end()) {
361 // This network is new. Place it in the network map.
362 merged_list.push_back(net);
363 networks_map_[key] = net;
honghaiza0c44ea2016-03-23 16:07:48 -0700364 net->set_id(next_available_network_id_++);
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45 +0000365 // Also, we might have accumulated IPAddresses from the first
366 // step, set it here.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000367 net->SetIPs(kv.second.ips, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368 *changed = true;
369 } else {
370 // This network exists in the map already. Reset its IP addresses.
honghaizdb8cf502015-12-21 13:08:46 -0800371 Network* existing_net = existing->second;
372 *changed = existing_net->SetIPs(kv.second.ips, *changed);
373 merged_list.push_back(existing_net);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700374 if (net->type() != ADAPTER_TYPE_UNKNOWN &&
375 net->type() != existing_net->type()) {
376 existing_net->set_type(net->type());
377 *changed = true;
378 }
honghaizdb8cf502015-12-21 13:08:46 -0800379 // If the existing network was not active, networks have changed.
380 if (!existing_net->active()) {
381 *changed = true;
382 }
nisseede5da42017-01-12 05:15:36 -0800383 RTC_DCHECK(net->active());
honghaizdb8cf502015-12-21 13:08:46 -0800384 if (existing_net != net) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000385 delete net;
386 }
387 }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800388 networks_map_[key]->set_mdns_responder_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000389 }
honghaizdb8cf502015-12-21 13:08:46 -0800390 // It may still happen that the merged list is a subset of |networks_|.
391 // To detect this change, we compare their sizes.
392 if (merged_list.size() != networks_.size()) {
393 *changed = true;
394 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395
honghaizdb8cf502015-12-21 13:08:46 -0800396 // If the network list changes, we re-assign |networks_| to the merged list
397 // and re-sort it.
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000398 if (*changed) {
honghaizdb8cf502015-12-21 13:08:46 -0800399 networks_ = merged_list;
400 // Reset the active states of all networks.
401 for (const auto& kv : networks_map_) {
honghaize3c6c822016-02-17 13:00:28 -0800402 Network* network = kv.second;
403 // If |network| is in the newly generated |networks_|, it is active.
Steve Anton2acd1632019-03-25 13:48:30 -0700404 bool found = absl::c_linear_search(networks_, network);
honghaize3c6c822016-02-17 13:00:28 -0800405 network->set_active(found);
honghaizdb8cf502015-12-21 13:08:46 -0800406 }
Steve Anton2acd1632019-03-25 13:48:30 -0700407 absl::c_sort(networks_, SortNetworks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000408 // Now network interfaces are sorted, we should set the preference value
409 // for each of the interfaces we are planning to use.
410 // Preference order of network interfaces might have changed from previous
411 // sorting due to addition of higher preference network interface.
412 // Since we have already sorted the network interfaces based on our
413 // requirements, we will just assign a preference value starting with 127,
414 // in decreasing order.
415 int pref = kHighestNetworkPreference;
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000416 for (Network* network : networks_) {
417 network->set_preference(pref);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 if (pref > 0) {
419 --pref;
420 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100421 RTC_LOG(LS_ERROR) << "Too many network interfaces to handle!";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000422 break;
423 }
424 }
425 }
426}
427
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800428void NetworkManagerBase::set_default_local_addresses(const IPAddress& ipv4,
429 const IPAddress& ipv6) {
430 if (ipv4.family() == AF_INET) {
431 default_local_ipv4_address_ = ipv4;
432 }
433 if (ipv6.family() == AF_INET6) {
434 default_local_ipv6_address_ = ipv6;
435 }
436}
437
438bool NetworkManagerBase::GetDefaultLocalAddress(int family,
439 IPAddress* ipaddr) const {
Guo-wei Shieha34c39e2015-11-25 13:12:26 -0800440 if (family == AF_INET && !default_local_ipv4_address_.IsNil()) {
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800441 *ipaddr = default_local_ipv4_address_;
442 return true;
Guo-wei Shieha34c39e2015-11-25 13:12:26 -0800443 } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) {
honghaizaf83fe62016-04-18 14:50:44 -0700444 Network* ipv6_network = GetNetworkFromAddress(default_local_ipv6_address_);
445 if (ipv6_network) {
446 // If the default ipv6 network's BestIP is different than
447 // default_local_ipv6_address_, use it instead.
448 // This is to prevent potential IP address leakage. See WebRTC bug 5376.
449 *ipaddr = ipv6_network->GetBestIP();
450 } else {
451 *ipaddr = default_local_ipv6_address_;
452 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800453 return true;
454 }
455 return false;
456}
457
honghaizaf83fe62016-04-18 14:50:44 -0700458Network* NetworkManagerBase::GetNetworkFromAddress(
459 const rtc::IPAddress& ip) const {
460 for (Network* network : networks_) {
461 const auto& ips = network->GetIPs();
Steve Anton2acd1632019-03-25 13:48:30 -0700462 if (absl::c_any_of(ips, [&](const InterfaceAddress& existing_ip) {
463 return ip == static_cast<rtc::IPAddress>(existing_ip);
464 })) {
honghaizaf83fe62016-04-18 14:50:44 -0700465 return network;
466 }
467 }
468 return nullptr;
469}
470
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000471BasicNetworkManager::BasicNetworkManager()
deadbeef37f5ecf2017-02-27 14:06:41 -0800472 : thread_(nullptr),
473 sent_first_update_(false),
474 start_count_(0),
475 ignore_non_default_routes_(false) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000476
Yves Gerey665174f2018-06-19 15:03:05 +0200477BasicNetworkManager::~BasicNetworkManager() {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000478
honghaiz023f3ef2015-10-19 09:39:32 -0700479void BasicNetworkManager::OnNetworksChanged() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100480 RTC_LOG(LS_INFO) << "Network change was observed";
honghaiz023f3ef2015-10-19 09:39:32 -0700481 UpdateNetworksOnce();
482}
483
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000484#if defined(__native_client__)
485
486bool BasicNetworkManager::CreateNetworks(bool include_ignored,
487 NetworkList* networks) const {
nissec80e7412017-01-11 05:56:46 -0800488 RTC_NOTREACHED();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100489 RTC_LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490 return false;
491}
492
493#elif defined(WEBRTC_POSIX)
494void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces,
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800495 IfAddrsConverter* ifaddrs_converter,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000496 bool include_ignored,
497 NetworkList* networks) const {
498 NetworkMap current_networks;
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800499
deadbeef37f5ecf2017-02-27 14:06:41 -0800500 for (struct ifaddrs* cursor = interfaces; cursor != nullptr;
501 cursor = cursor->ifa_next) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000502 IPAddress prefix;
503 IPAddress mask;
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800504 InterfaceAddress ip;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000505 int scope_id = 0;
506
507 // Some interfaces may not have address assigned.
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800508 if (!cursor->ifa_addr || !cursor->ifa_netmask) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000509 continue;
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800510 }
511 // Skip ones which are down.
512 if (!(cursor->ifa_flags & IFF_RUNNING)) {
513 continue;
514 }
515 // Skip unknown family.
516 if (cursor->ifa_addr->sa_family != AF_INET &&
517 cursor->ifa_addr->sa_family != AF_INET6) {
518 continue;
519 }
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800520 // Convert to InterfaceAddress.
521 if (!ifaddrs_converter->ConvertIfAddrsToIPAddress(cursor, &ip, &mask)) {
522 continue;
523 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000524
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800525 // Special case for IPv6 address.
526 if (cursor->ifa_addr->sa_family == AF_INET6) {
527 if (IsIgnoredIPv6(ip)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000528 continue;
529 }
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800530 scope_id =
531 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000532 }
533
Honghai Zhang351d77b2016-05-20 15:08:29 -0700534 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700535 AdapterType vpn_underlying_adapter_type = ADAPTER_TYPE_UNKNOWN;
Honghai Zhang351d77b2016-05-20 15:08:29 -0700536 if (cursor->ifa_flags & IFF_LOOPBACK) {
537 adapter_type = ADAPTER_TYPE_LOOPBACK;
538 } else {
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -0800539 // If there is a network_monitor, use it to get the adapter type.
540 // Otherwise, get the adapter type based on a few name matching rules.
541 if (network_monitor_) {
542 adapter_type = network_monitor_->GetAdapterType(cursor->ifa_name);
543 }
544 if (adapter_type == ADAPTER_TYPE_UNKNOWN) {
545 adapter_type = GetAdapterTypeFromName(cursor->ifa_name);
546 }
Honghai Zhang351d77b2016-05-20 15:08:29 -0700547 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700548
549 if (adapter_type == ADAPTER_TYPE_VPN && network_monitor_) {
550 vpn_underlying_adapter_type =
551 network_monitor_->GetVpnUnderlyingAdapterType(cursor->ifa_name);
552 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000553 int prefix_length = CountIPMaskBits(mask);
554 prefix = TruncateIP(ip, prefix_length);
Yves Gerey665174f2018-06-19 15:03:05 +0200555 std::string key =
556 MakeNetworkKey(std::string(cursor->ifa_name), prefix, prefix_length);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700557 auto iter = current_networks.find(key);
558 if (iter == current_networks.end()) {
Noah Richards2e266e92015-04-07 11:51:56 -0700559 // TODO(phoglund): Need to recognize other types as well.
jbauch555604a2016-04-26 03:13:22 -0700560 std::unique_ptr<Network> network(
561 new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length,
562 adapter_type));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800563 network->set_default_local_address_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000564 network->set_scope_id(scope_id);
565 network->AddIP(ip);
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000566 network->set_ignored(IsIgnoredNetwork(*network));
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700567 network->set_underlying_type_for_vpn(vpn_underlying_adapter_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000568 if (include_ignored || !network->ignored()) {
honghaizdb8cf502015-12-21 13:08:46 -0800569 current_networks[key] = network.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000570 networks->push_back(network.release());
571 }
572 } else {
Honghai Zhang351d77b2016-05-20 15:08:29 -0700573 Network* existing_network = iter->second;
574 existing_network->AddIP(ip);
575 if (adapter_type != ADAPTER_TYPE_UNKNOWN) {
576 existing_network->set_type(adapter_type);
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700577 existing_network->set_underlying_type_for_vpn(
578 vpn_underlying_adapter_type);
Honghai Zhang351d77b2016-05-20 15:08:29 -0700579 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000580 }
581 }
582}
583
584bool BasicNetworkManager::CreateNetworks(bool include_ignored,
585 NetworkList* networks) const {
586 struct ifaddrs* interfaces;
587 int error = getifaddrs(&interfaces);
588 if (error != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100589 RTC_LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: "
590 << error;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000591 return false;
592 }
593
jbauch555604a2016-04-26 03:13:22 -0700594 std::unique_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter());
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800595 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored,
596 networks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000597
598 freeifaddrs(interfaces);
599 return true;
600}
601
602#elif defined(WEBRTC_WIN)
603
604unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist,
Yves Gerey665174f2018-06-19 15:03:05 +0200605 const IPAddress& ip,
606 IPAddress* prefix) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000607 IPAddress current_prefix;
608 IPAddress best_prefix;
609 unsigned int best_length = 0;
610 while (prefixlist) {
611 // Look for the longest matching prefix in the prefixlist.
deadbeef37f5ecf2017-02-27 14:06:41 -0800612 if (prefixlist->Address.lpSockaddr == nullptr ||
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000613 prefixlist->Address.lpSockaddr->sa_family != ip.family()) {
614 prefixlist = prefixlist->Next;
615 continue;
616 }
617 switch (prefixlist->Address.lpSockaddr->sa_family) {
618 case AF_INET: {
619 sockaddr_in* v4_addr =
620 reinterpret_cast<sockaddr_in*>(prefixlist->Address.lpSockaddr);
621 current_prefix = IPAddress(v4_addr->sin_addr);
622 break;
623 }
624 case AF_INET6: {
Yves Gerey665174f2018-06-19 15:03:05 +0200625 sockaddr_in6* v6_addr =
626 reinterpret_cast<sockaddr_in6*>(prefixlist->Address.lpSockaddr);
627 current_prefix = IPAddress(v6_addr->sin6_addr);
628 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000629 }
630 default: {
631 prefixlist = prefixlist->Next;
632 continue;
633 }
634 }
635 if (TruncateIP(ip, prefixlist->PrefixLength) == current_prefix &&
636 prefixlist->PrefixLength > best_length) {
637 best_prefix = current_prefix;
638 best_length = prefixlist->PrefixLength;
639 }
640 prefixlist = prefixlist->Next;
641 }
642 *prefix = best_prefix;
643 return best_length;
644}
645
646bool BasicNetworkManager::CreateNetworks(bool include_ignored,
647 NetworkList* networks) const {
648 NetworkMap current_networks;
649 // MSDN recommends a 15KB buffer for the first try at GetAdaptersAddresses.
650 size_t buffer_size = 16384;
jbauch555604a2016-04-26 03:13:22 -0700651 std::unique_ptr<char[]> adapter_info(new char[buffer_size]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000652 PIP_ADAPTER_ADDRESSES adapter_addrs =
653 reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
654 int adapter_flags = (GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST |
655 GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_INCLUDE_PREFIX);
656 int ret = 0;
657 do {
658 adapter_info.reset(new char[buffer_size]);
659 adapter_addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
Yves Gerey665174f2018-06-19 15:03:05 +0200660 ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags, 0, adapter_addrs,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000661 reinterpret_cast<PULONG>(&buffer_size));
662 } while (ret == ERROR_BUFFER_OVERFLOW);
663 if (ret != ERROR_SUCCESS) {
664 return false;
665 }
666 int count = 0;
667 while (adapter_addrs) {
668 if (adapter_addrs->OperStatus == IfOperStatusUp) {
669 PIP_ADAPTER_UNICAST_ADDRESS address = adapter_addrs->FirstUnicastAddress;
670 PIP_ADAPTER_PREFIX prefixlist = adapter_addrs->FirstPrefix;
671 std::string name;
672 std::string description;
tfarinaa41ab932015-10-30 16:08:48 -0700673#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000674 name = ToUtf8(adapter_addrs->FriendlyName,
675 wcslen(adapter_addrs->FriendlyName));
676#endif
677 description = ToUtf8(adapter_addrs->Description,
678 wcslen(adapter_addrs->Description));
679 for (; address; address = address->Next) {
tfarinaa41ab932015-10-30 16:08:48 -0700680#if defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000681 name = rtc::ToString(count);
682#endif
683
684 IPAddress ip;
685 int scope_id = 0;
jbauch555604a2016-04-26 03:13:22 -0700686 std::unique_ptr<Network> network;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000687 switch (address->Address.lpSockaddr->sa_family) {
688 case AF_INET: {
689 sockaddr_in* v4_addr =
690 reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
691 ip = IPAddress(v4_addr->sin_addr);
692 break;
693 }
694 case AF_INET6: {
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100695 sockaddr_in6* v6_addr =
696 reinterpret_cast<sockaddr_in6*>(address->Address.lpSockaddr);
697 scope_id = v6_addr->sin6_scope_id;
698 ip = IPAddress(v6_addr->sin6_addr);
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000699
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100700 if (IsIgnoredIPv6(InterfaceAddress(ip))) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000701 continue;
702 }
Mirko Bonadei44f0f872019-01-20 18:16:42 +0100703
704 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000705 }
Jeroen de Borst8f096d02019-02-21 13:34:45 -0800706 default: {
707 continue;
708 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000709 }
710
711 IPAddress prefix;
712 int prefix_length = GetPrefix(prefixlist, ip, &prefix);
713 std::string key = MakeNetworkKey(name, prefix, prefix_length);
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000714 auto existing_network = current_networks.find(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000715 if (existing_network == current_networks.end()) {
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000716 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
Yura Yaroshevich0ce868c2018-05-15 11:15:32 +0300717 switch (adapter_addrs->IfType) {
718 case IF_TYPE_SOFTWARE_LOOPBACK:
719 adapter_type = ADAPTER_TYPE_LOOPBACK;
720 break;
721 case IF_TYPE_ETHERNET_CSMACD:
722 case IF_TYPE_ETHERNET_3MBIT:
723 case IF_TYPE_IEEE80212:
724 case IF_TYPE_FASTETHER:
725 case IF_TYPE_FASTETHER_FX:
726 case IF_TYPE_GIGABITETHERNET:
727 adapter_type = ADAPTER_TYPE_ETHERNET;
728 break;
729 case IF_TYPE_IEEE80211:
730 adapter_type = ADAPTER_TYPE_WIFI;
731 break;
732 case IF_TYPE_WWANPP:
733 case IF_TYPE_WWANPP2:
734 adapter_type = ADAPTER_TYPE_CELLULAR;
735 break;
736 default:
737 // TODO(phoglund): Need to recognize other types as well.
738 adapter_type = ADAPTER_TYPE_UNKNOWN;
739 break;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000740 }
jbauch555604a2016-04-26 03:13:22 -0700741 std::unique_ptr<Network> network(new Network(
742 name, description, prefix, prefix_length, adapter_type));
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800743 network->set_default_local_address_provider(this);
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800744 network->set_mdns_responder_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000745 network->set_scope_id(scope_id);
746 network->AddIP(ip);
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000747 bool ignored = IsIgnoredNetwork(*network);
748 network->set_ignored(ignored);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000749 if (include_ignored || !network->ignored()) {
honghaizdb8cf502015-12-21 13:08:46 -0800750 current_networks[key] = network.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000751 networks->push_back(network.release());
752 }
753 } else {
754 (*existing_network).second->AddIP(ip);
755 }
756 }
757 // Count is per-adapter - all 'Networks' created from the same
758 // adapter need to have the same name.
759 ++count;
760 }
761 adapter_addrs = adapter_addrs->Next;
762 }
763 return true;
764}
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000765#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000766
767#if defined(WEBRTC_LINUX)
768bool IsDefaultRoute(const std::string& network_name) {
Niels Möllera2e133d2018-10-30 13:04:33 +0100769 FILE* f = fopen("/proc/net/route", "r");
770 if (!f) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100771 RTC_LOG(LS_WARNING)
772 << "Couldn't read /proc/net/route, skipping default "
773 << "route check (assuming everything is a default route).";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000774 return true;
Niels Möllera2e133d2018-10-30 13:04:33 +0100775 }
776 bool is_default_route = false;
777 char line[500];
778 while (fgets(line, sizeof(line), f)) {
779 char iface_name[256];
780 unsigned int iface_ip, iface_gw, iface_mask, iface_flags;
781 if (sscanf(line, "%255s %8X %8X %4X %*d %*u %*d %8X", iface_name, &iface_ip,
782 &iface_gw, &iface_flags, &iface_mask) == 5 &&
783 network_name == iface_name && iface_mask == 0 &&
784 (iface_flags & (RTF_UP | RTF_HOST)) == RTF_UP) {
785 is_default_route = true;
786 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000787 }
788 }
Niels Möllera2e133d2018-10-30 13:04:33 +0100789 fclose(f);
790 return is_default_route;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000791}
792#endif
793
794bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const {
795 // Ignore networks on the explicit ignore list.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000796 for (const std::string& ignored_name : network_ignore_list_) {
797 if (network.name() == ignored_name) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000798 return true;
799 }
800 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000801
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000802#if defined(WEBRTC_POSIX)
pthatcher@webrtc.orgdfef0282015-01-07 17:20:52 +0000803 // Filter out VMware/VirtualBox interfaces, typically named vmnet1,
804 // vmnet8, or vboxnet0.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000805 if (strncmp(network.name().c_str(), "vmnet", 5) == 0 ||
pthatcher@webrtc.orgdfef0282015-01-07 17:20:52 +0000806 strncmp(network.name().c_str(), "vnic", 4) == 0 ||
807 strncmp(network.name().c_str(), "vboxnet", 7) == 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000808 return true;
809 }
810#if defined(WEBRTC_LINUX)
811 // Make sure this is a default route, if we're ignoring non-defaults.
812 if (ignore_non_default_routes_ && !IsDefaultRoute(network.name())) {
813 return true;
814 }
815#endif
816#elif defined(WEBRTC_WIN)
817 // Ignore any HOST side vmware adapters with a description like:
818 // VMware Virtual Ethernet Adapter for VMnet1
819 // but don't ignore any GUEST side adapters with a description like:
820 // VMware Accelerated AMD PCNet Adapter #2
deadbeef37f5ecf2017-02-27 14:06:41 -0800821 if (strstr(network.description().c_str(), "VMnet") != nullptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000822 return true;
823 }
824#endif
825
826 // Ignore any networks with a 0.x.y.z IP
827 if (network.prefix().family() == AF_INET) {
828 return (network.prefix().v4AddressAsHostOrderInteger() < 0x01000000);
829 }
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29 +0000830
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000831 return false;
832}
833
834void BasicNetworkManager::StartUpdating() {
835 thread_ = Thread::Current();
836 if (start_count_) {
837 // If network interfaces are already discovered and signal is sent,
838 // we should trigger network signal immediately for the new clients
839 // to start allocating ports.
840 if (sent_first_update_)
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700841 thread_->Post(RTC_FROM_HERE, this, kSignalNetworksMessage);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000842 } else {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700843 thread_->Post(RTC_FROM_HERE, this, kUpdateNetworksMessage);
honghaiz023f3ef2015-10-19 09:39:32 -0700844 StartNetworkMonitor();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000845 }
846 ++start_count_;
847}
848
849void BasicNetworkManager::StopUpdating() {
nisseede5da42017-01-12 05:15:36 -0800850 RTC_DCHECK(Thread::Current() == thread_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000851 if (!start_count_)
852 return;
853
854 --start_count_;
855 if (!start_count_) {
856 thread_->Clear(this);
857 sent_first_update_ = false;
honghaiz023f3ef2015-10-19 09:39:32 -0700858 StopNetworkMonitor();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000859 }
860}
861
honghaiz023f3ef2015-10-19 09:39:32 -0700862void BasicNetworkManager::StartNetworkMonitor() {
863 NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory();
864 if (factory == nullptr) {
865 return;
866 }
honghaiz023f3ef2015-10-19 09:39:32 -0700867 if (!network_monitor_) {
honghaizcec0a082016-01-15 14:49:09 -0800868 network_monitor_.reset(factory->CreateNetworkMonitor());
869 if (!network_monitor_) {
870 return;
871 }
skvlad979c2682016-06-10 15:24:13 -0700872 network_monitor_->SignalNetworksChanged.connect(
873 this, &BasicNetworkManager::OnNetworksChanged);
honghaiz023f3ef2015-10-19 09:39:32 -0700874 }
honghaiz023f3ef2015-10-19 09:39:32 -0700875 network_monitor_->Start();
876}
877
878void BasicNetworkManager::StopNetworkMonitor() {
879 if (!network_monitor_) {
880 return;
881 }
882 network_monitor_->Stop();
honghaiz023f3ef2015-10-19 09:39:32 -0700883}
884
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000885void BasicNetworkManager::OnMessage(Message* msg) {
886 switch (msg->message_id) {
honghaiz023f3ef2015-10-19 09:39:32 -0700887 case kUpdateNetworksMessage: {
888 UpdateNetworksContinually();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000889 break;
890 }
Yves Gerey665174f2018-06-19 15:03:05 +0200891 case kSignalNetworksMessage: {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000892 SignalNetworksChanged();
893 break;
894 }
895 default:
nissec80e7412017-01-11 05:56:46 -0800896 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000897 }
898}
899
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800900IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const {
nisseede5da42017-01-12 05:15:36 -0800901 RTC_DCHECK(thread_ == Thread::Current());
902 RTC_DCHECK(thread_->socketserver() != nullptr);
903 RTC_DCHECK(family == AF_INET || family == AF_INET6);
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800904
jbauch555604a2016-04-26 03:13:22 -0700905 std::unique_ptr<AsyncSocket> socket(
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800906 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM));
907 if (!socket) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100908 RTC_LOG_ERR(LERROR) << "Socket creation failed";
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800909 return IPAddress();
910 }
911
guoweis56271ed2016-01-15 14:45:06 -0800912 if (socket->Connect(SocketAddress(
913 family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort)) <
914 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200915 if (socket->GetError() != ENETUNREACH &&
916 socket->GetError() != EHOSTUNREACH) {
skvlad75024012016-09-29 12:59:35 -0700917 // Ignore the expected case of "host/net unreachable" - which happens if
918 // the network is V4- or V6-only.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100919 RTC_LOG(LS_INFO) << "Connect failed with " << socket->GetError();
skvlad75024012016-09-29 12:59:35 -0700920 }
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800921 return IPAddress();
922 }
923 return socket->GetLocalAddress().ipaddr();
924}
925
honghaiz023f3ef2015-10-19 09:39:32 -0700926void BasicNetworkManager::UpdateNetworksOnce() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000927 if (!start_count_)
928 return;
929
nisseede5da42017-01-12 05:15:36 -0800930 RTC_DCHECK(Thread::Current() == thread_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000931
932 NetworkList list;
933 if (!CreateNetworks(false, &list)) {
934 SignalError();
935 } else {
936 bool changed;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800937 NetworkManager::Stats stats;
938 MergeNetworkList(list, &changed, &stats);
939 set_default_local_addresses(QueryDefaultLocalAddress(AF_INET),
940 QueryDefaultLocalAddress(AF_INET6));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000941 if (changed || !sent_first_update_) {
942 SignalNetworksChanged();
943 sent_first_update_ = true;
944 }
945 }
honghaiz023f3ef2015-10-19 09:39:32 -0700946}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000947
honghaiz023f3ef2015-10-19 09:39:32 -0700948void BasicNetworkManager::UpdateNetworksContinually() {
949 UpdateNetworksOnce();
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700950 thread_->PostDelayed(RTC_FROM_HERE, kNetworksUpdateIntervalMs, this,
951 kUpdateNetworksMessage);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000952}
953
honghaizdb8cf502015-12-21 13:08:46 -0800954void BasicNetworkManager::DumpNetworks() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000955 NetworkList list;
honghaizdb8cf502015-12-21 13:08:46 -0800956 GetNetworks(&list);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100957 RTC_LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:";
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +0000958 for (const Network* network : list) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100959 RTC_LOG(LS_INFO) << network->ToString() << ": " << network->description()
960 << ", active ? " << network->active()
961 << ((network->ignored()) ? ", Ignored" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000962 }
963}
964
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800965Network::Network(const std::string& name,
966 const std::string& desc,
967 const IPAddress& prefix,
968 int prefix_length)
969 : name_(name),
970 description_(desc),
971 prefix_(prefix),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000972 prefix_length_(prefix_length),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800973 key_(MakeNetworkKey(name, prefix, prefix_length)),
974 scope_id_(0),
975 ignored_(false),
976 type_(ADAPTER_TYPE_UNKNOWN),
977 preference_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000978
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800979Network::Network(const std::string& name,
980 const std::string& desc,
981 const IPAddress& prefix,
982 int prefix_length,
983 AdapterType type)
984 : name_(name),
985 description_(desc),
986 prefix_(prefix),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000987 prefix_length_(prefix_length),
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800988 key_(MakeNetworkKey(name, prefix, prefix_length)),
989 scope_id_(0),
990 ignored_(false),
991 type_(type),
992 preference_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000993
Steve Anton9de3aac2017-10-24 10:08:26 -0700994Network::Network(const Network&) = default;
995
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000996Network::~Network() = default;
997
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000998// Sets the addresses of this network. Returns true if the address set changed.
999// Change detection is short circuited if the changed argument is true.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +00001000bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001001 // Detect changes with a nested loop; n-squared but we expect on the order
1002 // of 2-3 addresses per network.
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +00001003 changed = changed || ips.size() != ips_.size();
1004 if (!changed) {
1005 for (const InterfaceAddress& ip : ips) {
Steve Anton2acd1632019-03-25 13:48:30 -07001006 if (!absl::c_linear_search(ips_, ip)) {
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +00001007 changed = true;
1008 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001009 }
1010 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001011 }
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +00001012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001013 ips_ = ips;
1014 return changed;
1015}
1016
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001017// Select the best IP address to use from this Network.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001018IPAddress Network::GetBestIP() const {
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +00001019 if (ips_.size() == 0) {
1020 return IPAddress();
1021 }
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001022
1023 if (prefix_.family() == AF_INET) {
1024 return static_cast<IPAddress>(ips_.at(0));
1025 }
1026
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001027 InterfaceAddress selected_ip, ula_ip;
1028
pthatcher@webrtc.org5950b642014-12-01 23:18:27 +00001029 for (const InterfaceAddress& ip : ips_) {
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001030 // Ignore any address which has been deprecated already.
1031 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED)
1032 continue;
1033
1034 // ULA address should only be returned when we have no other
1035 // global IP.
1036 if (IPIsULA(static_cast<const IPAddress&>(ip))) {
1037 ula_ip = ip;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001038 continue;
1039 }
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001040 selected_ip = ip;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001041
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001042 // Search could stop once a temporary non-deprecated one is found.
1043 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY)
1044 break;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001045 }
1046
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +00001047 // No proper global IPv6 address found, use ULA instead.
1048 if (IPIsUnspec(selected_ip) && !IPIsUnspec(ula_ip)) {
1049 selected_ip = ula_ip;
1050 }
1051
1052 return static_cast<IPAddress>(selected_ip);
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001053}
1054
Qingsi Wang5ae259e2019-02-13 15:46:07 -08001055webrtc::MdnsResponderInterface* Network::GetMdnsResponder() const {
1056 if (mdns_responder_provider_ == nullptr) {
1057 return nullptr;
1058 }
1059 return mdns_responder_provider_->GetMdnsResponder();
1060}
1061
Qingsi Wangde2ed7d2018-04-27 14:25:37 -07001062uint16_t Network::GetCost() const {
1063 AdapterType type = IsVpn() ? underlying_type_for_vpn_ : type_;
1064 return ComputeNetworkCostByType(type);
1065}
1066
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001067std::string Network::ToString() const {
Jonas Olsson366a50c2018-09-06 13:41:30 +02001068 rtc::StringBuilder ss;
guoweis@webrtc.org369a6372014-09-17 22:37:29 +00001069 // Print out the first space-terminated token of the network desc, plus
1070 // the IP address.
Qingsi Wangde2ed7d2018-04-27 14:25:37 -07001071 ss << "Net[" << description_.substr(0, description_.find(' ')) << ":"
1072 << prefix_.ToSensitiveString() << "/" << prefix_length_ << ":"
1073 << AdapterTypeToString(type_);
1074 if (IsVpn()) {
1075 ss << "/" << AdapterTypeToString(underlying_type_for_vpn_);
1076 }
Qingsi Wang10a0e512018-05-16 13:37:03 -07001077 ss << ":id=" << id_ << "]";
Jonas Olsson84df1c72018-09-14 16:59:32 +02001078 return ss.Release();
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +00001079}
1080
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001081} // namespace rtc