henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/network.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(WEBRTC_POSIX) |
| 14 | // linux/if.h can't be included at the same time as the posix sys/if.h, and |
| 15 | // it's transitively required by linux/route.h, so include that version on |
| 16 | // linux instead of the standard posix one. |
| 17 | #if defined(WEBRTC_LINUX) |
| 18 | #include <linux/if.h> |
| 19 | #include <linux/route.h> |
| 20 | #elif !defined(__native_client__) |
| 21 | #include <net/if.h> |
| 22 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | #endif // WEBRTC_POSIX |
| 24 | |
| 25 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 0191441 | 2018-03-23 16:30:58 +0100 | [diff] [blame] | 26 | #include <iphlpapi.h> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | #include "rtc_base/win32.h" |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 28 | #elif !defined(__native_client__) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/ifaddrs_converter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 34 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 36 | #include "absl/algorithm/container.h" |
| 37 | #include "absl/strings/match.h" |
| 38 | #include "absl/strings/string_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "rtc_base/checks.h" |
| 40 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 41 | #include "rtc_base/network_monitor.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 42 | #include "rtc_base/socket.h" // includes something that makes windows happy |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 43 | #include "rtc_base/string_encode.h" |
| 44 | #include "rtc_base/string_utils.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 45 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 46 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | |
| 48 | namespace rtc { |
| 49 | namespace { |
| 50 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 51 | const uint32_t kUpdateNetworksMessage = 1; |
| 52 | const uint32_t kSignalNetworksMessage = 2; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 53 | |
| 54 | // Fetch list of networks every two seconds. |
| 55 | const int kNetworksUpdateIntervalMs = 2000; |
| 56 | |
| 57 | const int kHighestNetworkPreference = 127; |
| 58 | |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 59 | typedef struct { |
| 60 | Network* net; |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 61 | std::vector<InterfaceAddress> ips; |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 62 | } AddressList; |
| 63 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 64 | bool 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 | |
| 73 | bool 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.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 82 | IPAddress ip_a = a->GetBestIP(); |
| 83 | IPAddress ip_b = b->GetBestIP(); |
| 84 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | // After type, networks are sorted by IP address precedence values |
| 86 | // from RFC 3484-bis |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 87 | if (IPAddressPrecedence(ip_a) != IPAddressPrecedence(ip_b)) { |
| 88 | return IPAddressPrecedence(ip_a) > IPAddressPrecedence(ip_b); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // TODO(mallinath) - Add VPN and Link speed conditions while sorting. |
| 92 | |
| 93 | // Networks are sorted last by key. |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 94 | return a->key() < b->key(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | std::string AdapterTypeToString(AdapterType type) { |
| 98 | switch (type) { |
Qingsi Wang | 9f1de69 | 2018-06-28 15:38:09 -0700 | [diff] [blame] | 99 | case ADAPTER_TYPE_ANY: |
Qingsi Wang | 01560de | 2018-07-26 10:44:02 -0700 | [diff] [blame] | 100 | return "Wildcard"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 101 | 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.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 111 | case ADAPTER_TYPE_LOOPBACK: |
| 112 | return "Loopback"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 114 | RTC_NOTREACHED() << "Invalid type " << type; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | return std::string(); |
| 116 | } |
| 117 | } |
| 118 | |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 119 | uint16_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 Wang | 9f1de69 | 2018-06-28 15:38:09 -0700 | [diff] [blame] | 128 | 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 Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 139 | 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 | |
thakis | 06689a1 | 2015-12-30 15:05:30 -0800 | [diff] [blame] | 148 | #if !defined(__native_client__) |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 149 | bool IsIgnoredIPv6(const InterfaceAddress& ip) { |
guoweis@webrtc.org | b91d0f5 | 2015-03-17 14:43:20 +0000 | [diff] [blame] | 150 | 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 Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 166 | // Ignore deprecated IPv6. |
| 167 | if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) { |
| 168 | return true; |
| 169 | } |
| 170 | |
guoweis@webrtc.org | b91d0f5 | 2015-03-17 14:43:20 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
thakis | 06689a1 | 2015-12-30 15:05:30 -0800 | [diff] [blame] | 173 | #endif // !defined(__native_client__) |
guoweis@webrtc.org | b91d0f5 | 2015-03-17 14:43:20 +0000 | [diff] [blame] | 174 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | } // namespace |
| 176 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 177 | // 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. |
| 179 | const char kPublicIPv4Host[] = "8.8.8.8"; |
| 180 | const char kPublicIPv6Host[] = "2001:4860:4860::8888"; |
| 181 | const int kPublicPort = 53; // DNS port. |
| 182 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 183 | std::string MakeNetworkKey(const std::string& name, |
| 184 | const IPAddress& prefix, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | int prefix_length) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 186 | rtc::StringBuilder ost; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | ost << name << "%" << prefix.ToString() << "/" << prefix_length; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 188 | return ost.Release(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 189 | } |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 190 | // Test if the network name matches the type<number> pattern, e.g. eth0. The |
| 191 | // matching is case-sensitive. |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 192 | bool MatchTypeNameWithIndexPattern(absl::string_view network_name, |
| 193 | absl::string_view type_name) { |
| 194 | if (!absl::StartsWith(network_name, type_name)) { |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 195 | return false; |
| 196 | } |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 197 | return absl::c_none_of(network_name.substr(type_name.size()), |
| 198 | [](char c) { return !isdigit(c); }); |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 199 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 200 | |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 201 | // 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 Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 206 | AdapterType GetAdapterTypeFromName(const char* network_name) { |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 207 | 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 Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 221 | return ADAPTER_TYPE_VPN; |
| 222 | } |
| 223 | #if defined(WEBRTC_IOS) |
| 224 | // Cell networks are pdp_ipN on iOS. |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 225 | if (MatchTypeNameWithIndexPattern(network_name, "pdp_ip")) { |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 226 | return ADAPTER_TYPE_CELLULAR; |
| 227 | } |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 228 | if (MatchTypeNameWithIndexPattern(network_name, "en")) { |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 229 | // 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 Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 236 | if (MatchTypeNameWithIndexPattern(network_name, "rmnet") || |
| 237 | MatchTypeNameWithIndexPattern(network_name, "rmnet_data") || |
| 238 | MatchTypeNameWithIndexPattern(network_name, "v4-rmnet") || |
Jeroen de Borst | 8f096d0 | 2019-02-21 13:34:45 -0800 | [diff] [blame] | 239 | MatchTypeNameWithIndexPattern(network_name, "v4-rmnet_data") || |
| 240 | MatchTypeNameWithIndexPattern(network_name, "clat")) { |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 241 | return ADAPTER_TYPE_CELLULAR; |
| 242 | } |
Qingsi Wang | e53ac04 | 2018-05-08 11:55:07 -0700 | [diff] [blame] | 243 | if (MatchTypeNameWithIndexPattern(network_name, "wlan")) { |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 244 | return ADAPTER_TYPE_WIFI; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | return ADAPTER_TYPE_UNKNOWN; |
| 249 | } |
| 250 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 251 | NetworkManager::NetworkManager() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 252 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 253 | NetworkManager::~NetworkManager() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 254 | |
Guo-wei Shieh | 86cb923 | 2015-08-19 10:57:53 -0700 | [diff] [blame] | 255 | NetworkManager::EnumerationPermission NetworkManager::enumeration_permission() |
| 256 | const { |
guoweis | ea1012b | 2015-08-21 09:06:28 -0700 | [diff] [blame] | 257 | return ENUMERATION_ALLOWED; |
Guo-wei Shieh | 86cb923 | 2015-08-19 10:57:53 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 260 | bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const { |
| 261 | return false; |
| 262 | } |
| 263 | |
Qingsi Wang | 7852d29 | 2018-10-31 11:17:07 -0700 | [diff] [blame] | 264 | webrtc::MdnsResponderInterface* NetworkManager::GetMdnsResponder() const { |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 265 | return nullptr; |
| 266 | } |
| 267 | |
guoweis@webrtc.org | 2444d96 | 2015-01-30 00:09:28 +0000 | [diff] [blame] | 268 | NetworkManagerBase::NetworkManagerBase() |
Mirko Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 269 | : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 270 | |
| 271 | NetworkManagerBase::~NetworkManagerBase() { |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 272 | for (const auto& kv : networks_map_) { |
| 273 | delete kv.second; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 277 | NetworkManager::EnumerationPermission |
| 278 | NetworkManagerBase::enumeration_permission() const { |
| 279 | return enumeration_permission_; |
| 280 | } |
| 281 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 282 | void 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 Wang | 9f1de69 | 2018-06-28 15:38:09 -0700 | [diff] [blame] | 286 | new rtc::Network("any", "any", ipv4_any_address, 0, ADAPTER_TYPE_ANY)); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 287 | ipv4_any_address_network_->set_default_local_address_provider(this); |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 288 | ipv4_any_address_network_->set_mdns_responder_provider(this); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 289 | ipv4_any_address_network_->AddIP(ipv4_any_address); |
| 290 | } |
| 291 | networks->push_back(ipv4_any_address_network_.get()); |
| 292 | |
Mirko Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 293 | 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 Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 298 | ipv6_any_address_network_->set_mdns_responder_provider(this); |
Mirko Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 299 | ipv6_any_address_network_->AddIP(ipv6_any_address); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 300 | } |
Mirko Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 301 | networks->push_back(ipv6_any_address_network_.get()); |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 302 | } |
| 303 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | void NetworkManagerBase::GetNetworks(NetworkList* result) const { |
guoweis@webrtc.org | 2444d96 | 2015-01-30 00:09:28 +0000 | [diff] [blame] | 305 | result->clear(); |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 306 | result->insert(result->begin(), networks_.begin(), networks_.end()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, |
| 310 | bool* changed) { |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 311 | NetworkManager::Stats stats; |
| 312 | MergeNetworkList(new_networks, changed, &stats); |
| 313 | } |
| 314 | |
| 315 | void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, |
| 316 | bool* changed, |
| 317 | NetworkManager::Stats* stats) { |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 318 | *changed = false; |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 319 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 322 | NetworkList list(new_networks); |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 323 | absl::c_sort(list, CompareNetworks); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 324 | // First, build a set of network-keys to the ipaddresses. |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 325 | for (Network* network : list) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 326 | bool might_add_to_merged_list = false; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 327 | std::string key = MakeNetworkKey(network->name(), network->prefix(), |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 328 | network->prefix_length()); |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 329 | if (consolidated_address_list.find(key) == |
| 330 | consolidated_address_list.end()) { |
| 331 | AddressList addrlist; |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 332 | addrlist.net = network; |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 333 | consolidated_address_list[key] = addrlist; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 334 | might_add_to_merged_list = true; |
| 335 | } |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 336 | const std::vector<InterfaceAddress>& addresses = network->GetIPs(); |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 337 | AddressList& current_list = consolidated_address_list[key]; |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 338 | for (const InterfaceAddress& address : addresses) { |
| 339 | current_list.ips.push_back(address); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 340 | } |
| 341 | if (!might_add_to_merged_list) { |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 342 | delete network; |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 343 | } else { |
| 344 | if (current_list.ips[0].family() == AF_INET) { |
| 345 | stats->ipv4_network_count++; |
| 346 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 347 | RTC_DCHECK(current_list.ips[0].family() == AF_INET6); |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 348 | stats->ipv6_network_count++; |
| 349 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | // Next, look for existing network objects to re-use. |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 354 | // Result of Network merge. Element in this list should have unique key. |
| 355 | NetworkList merged_list; |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 356 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 360 | 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; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 364 | net->set_id(next_available_network_id_++); |
guoweis@webrtc.org | 4bbd3c8 | 2014-09-09 13:54:45 +0000 | [diff] [blame] | 365 | // Also, we might have accumulated IPAddresses from the first |
| 366 | // step, set it here. |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 367 | net->SetIPs(kv.second.ips, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 368 | *changed = true; |
| 369 | } else { |
| 370 | // This network exists in the map already. Reset its IP addresses. |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 371 | Network* existing_net = existing->second; |
| 372 | *changed = existing_net->SetIPs(kv.second.ips, *changed); |
| 373 | merged_list.push_back(existing_net); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 374 | if (net->type() != ADAPTER_TYPE_UNKNOWN && |
| 375 | net->type() != existing_net->type()) { |
| 376 | existing_net->set_type(net->type()); |
| 377 | *changed = true; |
| 378 | } |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 379 | // If the existing network was not active, networks have changed. |
| 380 | if (!existing_net->active()) { |
| 381 | *changed = true; |
| 382 | } |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 383 | RTC_DCHECK(net->active()); |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 384 | if (existing_net != net) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 385 | delete net; |
| 386 | } |
| 387 | } |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 388 | networks_map_[key]->set_mdns_responder_provider(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 389 | } |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 390 | // 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 395 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 396 | // If the network list changes, we re-assign |networks_| to the merged list |
| 397 | // and re-sort it. |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 398 | if (*changed) { |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 399 | networks_ = merged_list; |
| 400 | // Reset the active states of all networks. |
| 401 | for (const auto& kv : networks_map_) { |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 402 | Network* network = kv.second; |
| 403 | // If |network| is in the newly generated |networks_|, it is active. |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 404 | bool found = absl::c_linear_search(networks_, network); |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 405 | network->set_active(found); |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 406 | } |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 407 | absl::c_sort(networks_, SortNetworks); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 408 | // 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.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 416 | for (Network* network : networks_) { |
| 417 | network->set_preference(pref); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 418 | if (pref > 0) { |
| 419 | --pref; |
| 420 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 421 | RTC_LOG(LS_ERROR) << "Too many network interfaces to handle!"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 422 | break; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 428 | void 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 | |
| 438 | bool NetworkManagerBase::GetDefaultLocalAddress(int family, |
| 439 | IPAddress* ipaddr) const { |
Guo-wei Shieh | a34c39e | 2015-11-25 13:12:26 -0800 | [diff] [blame] | 440 | if (family == AF_INET && !default_local_ipv4_address_.IsNil()) { |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 441 | *ipaddr = default_local_ipv4_address_; |
| 442 | return true; |
Guo-wei Shieh | a34c39e | 2015-11-25 13:12:26 -0800 | [diff] [blame] | 443 | } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) { |
honghaiz | af83fe6 | 2016-04-18 14:50:44 -0700 | [diff] [blame] | 444 | 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 Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | return false; |
| 456 | } |
| 457 | |
honghaiz | af83fe6 | 2016-04-18 14:50:44 -0700 | [diff] [blame] | 458 | Network* NetworkManagerBase::GetNetworkFromAddress( |
| 459 | const rtc::IPAddress& ip) const { |
| 460 | for (Network* network : networks_) { |
| 461 | const auto& ips = network->GetIPs(); |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 462 | if (absl::c_any_of(ips, [&](const InterfaceAddress& existing_ip) { |
| 463 | return ip == static_cast<rtc::IPAddress>(existing_ip); |
| 464 | })) { |
honghaiz | af83fe6 | 2016-04-18 14:50:44 -0700 | [diff] [blame] | 465 | return network; |
| 466 | } |
| 467 | } |
| 468 | return nullptr; |
| 469 | } |
| 470 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 471 | BasicNetworkManager::BasicNetworkManager() |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 472 | : thread_(nullptr), |
| 473 | sent_first_update_(false), |
| 474 | start_count_(0), |
| 475 | ignore_non_default_routes_(false) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 476 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 477 | BasicNetworkManager::~BasicNetworkManager() {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 478 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 479 | void BasicNetworkManager::OnNetworksChanged() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 480 | RTC_LOG(LS_INFO) << "Network change was observed"; |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 481 | UpdateNetworksOnce(); |
| 482 | } |
| 483 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 484 | #if defined(__native_client__) |
| 485 | |
| 486 | bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 487 | NetworkList* networks) const { |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 488 | RTC_NOTREACHED(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 489 | RTC_LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 490 | return false; |
| 491 | } |
| 492 | |
| 493 | #elif defined(WEBRTC_POSIX) |
| 494 | void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces, |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 495 | IfAddrsConverter* ifaddrs_converter, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 496 | bool include_ignored, |
| 497 | NetworkList* networks) const { |
| 498 | NetworkMap current_networks; |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 499 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 500 | for (struct ifaddrs* cursor = interfaces; cursor != nullptr; |
| 501 | cursor = cursor->ifa_next) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 502 | IPAddress prefix; |
| 503 | IPAddress mask; |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 504 | InterfaceAddress ip; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 505 | int scope_id = 0; |
| 506 | |
| 507 | // Some interfaces may not have address assigned. |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 508 | if (!cursor->ifa_addr || !cursor->ifa_netmask) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 509 | continue; |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 510 | } |
| 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 Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 520 | // Convert to InterfaceAddress. |
| 521 | if (!ifaddrs_converter->ConvertIfAddrsToIPAddress(cursor, &ip, &mask)) { |
| 522 | continue; |
| 523 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 524 | |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 525 | // Special case for IPv6 address. |
| 526 | if (cursor->ifa_addr->sa_family == AF_INET6) { |
| 527 | if (IsIgnoredIPv6(ip)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 528 | continue; |
| 529 | } |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 530 | scope_id = |
| 531 | reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 534 | AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 535 | AdapterType vpn_underlying_adapter_type = ADAPTER_TYPE_UNKNOWN; |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 536 | if (cursor->ifa_flags & IFF_LOOPBACK) { |
| 537 | adapter_type = ADAPTER_TYPE_LOOPBACK; |
| 538 | } else { |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 539 | // 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 Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 547 | } |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 548 | |
| 549 | if (adapter_type == ADAPTER_TYPE_VPN && network_monitor_) { |
| 550 | vpn_underlying_adapter_type = |
| 551 | network_monitor_->GetVpnUnderlyingAdapterType(cursor->ifa_name); |
| 552 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 553 | int prefix_length = CountIPMaskBits(mask); |
| 554 | prefix = TruncateIP(ip, prefix_length); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 555 | std::string key = |
| 556 | MakeNetworkKey(std::string(cursor->ifa_name), prefix, prefix_length); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 557 | auto iter = current_networks.find(key); |
| 558 | if (iter == current_networks.end()) { |
Noah Richards | 2e266e9 | 2015-04-07 11:51:56 -0700 | [diff] [blame] | 559 | // TODO(phoglund): Need to recognize other types as well. |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 560 | std::unique_ptr<Network> network( |
| 561 | new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length, |
| 562 | adapter_type)); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 563 | network->set_default_local_address_provider(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 564 | network->set_scope_id(scope_id); |
| 565 | network->AddIP(ip); |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 566 | network->set_ignored(IsIgnoredNetwork(*network)); |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 567 | network->set_underlying_type_for_vpn(vpn_underlying_adapter_type); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 568 | if (include_ignored || !network->ignored()) { |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 569 | current_networks[key] = network.get(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 570 | networks->push_back(network.release()); |
| 571 | } |
| 572 | } else { |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 573 | 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 Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 577 | existing_network->set_underlying_type_for_vpn( |
| 578 | vpn_underlying_adapter_type); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 579 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | bool BasicNetworkManager::CreateNetworks(bool include_ignored, |
| 585 | NetworkList* networks) const { |
| 586 | struct ifaddrs* interfaces; |
| 587 | int error = getifaddrs(&interfaces); |
| 588 | if (error != 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 589 | RTC_LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " |
| 590 | << error; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 591 | return false; |
| 592 | } |
| 593 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 594 | std::unique_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter()); |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 595 | ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored, |
| 596 | networks); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 597 | |
| 598 | freeifaddrs(interfaces); |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | #elif defined(WEBRTC_WIN) |
| 603 | |
| 604 | unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 605 | const IPAddress& ip, |
| 606 | IPAddress* prefix) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 607 | 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. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 612 | if (prefixlist->Address.lpSockaddr == nullptr || |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 613 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 625 | sockaddr_in6* v6_addr = |
| 626 | reinterpret_cast<sockaddr_in6*>(prefixlist->Address.lpSockaddr); |
| 627 | current_prefix = IPAddress(v6_addr->sin6_addr); |
| 628 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 629 | } |
| 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 | |
| 646 | bool 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; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 651 | std::unique_ptr<char[]> adapter_info(new char[buffer_size]); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 652 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 660 | ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags, 0, adapter_addrs, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 661 | 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; |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 673 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 674 | 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) { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 680 | #if defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 681 | name = rtc::ToString(count); |
| 682 | #endif |
| 683 | |
| 684 | IPAddress ip; |
| 685 | int scope_id = 0; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 686 | std::unique_ptr<Network> network; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 687 | 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 Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 695 | 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.org | b91d0f5 | 2015-03-17 14:43:20 +0000 | [diff] [blame] | 699 | |
Mirko Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 700 | if (IsIgnoredIPv6(InterfaceAddress(ip))) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 701 | continue; |
| 702 | } |
Mirko Bonadei | 44f0f87 | 2019-01-20 18:16:42 +0100 | [diff] [blame] | 703 | |
| 704 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 705 | } |
Jeroen de Borst | 8f096d0 | 2019-02-21 13:34:45 -0800 | [diff] [blame] | 706 | default: { |
| 707 | continue; |
| 708 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | IPAddress prefix; |
| 712 | int prefix_length = GetPrefix(prefixlist, ip, &prefix); |
| 713 | std::string key = MakeNetworkKey(name, prefix, prefix_length); |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 714 | auto existing_network = current_networks.find(key); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 715 | if (existing_network == current_networks.end()) { |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 716 | AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
Yura Yaroshevich | 0ce868c | 2018-05-15 11:15:32 +0300 | [diff] [blame] | 717 | 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.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 740 | } |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 741 | std::unique_ptr<Network> network(new Network( |
| 742 | name, description, prefix, prefix_length, adapter_type)); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 743 | network->set_default_local_address_provider(this); |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 744 | network->set_mdns_responder_provider(this); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 745 | network->set_scope_id(scope_id); |
| 746 | network->AddIP(ip); |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 747 | bool ignored = IsIgnoredNetwork(*network); |
| 748 | network->set_ignored(ignored); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 749 | if (include_ignored || !network->ignored()) { |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 750 | current_networks[key] = network.get(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 751 | 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.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 765 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 766 | |
| 767 | #if defined(WEBRTC_LINUX) |
| 768 | bool IsDefaultRoute(const std::string& network_name) { |
Niels Möller | a2e133d | 2018-10-30 13:04:33 +0100 | [diff] [blame] | 769 | FILE* f = fopen("/proc/net/route", "r"); |
| 770 | if (!f) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 771 | RTC_LOG(LS_WARNING) |
| 772 | << "Couldn't read /proc/net/route, skipping default " |
| 773 | << "route check (assuming everything is a default route)."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 774 | return true; |
Niels Möller | a2e133d | 2018-10-30 13:04:33 +0100 | [diff] [blame] | 775 | } |
| 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
Niels Möller | a2e133d | 2018-10-30 13:04:33 +0100 | [diff] [blame] | 789 | fclose(f); |
| 790 | return is_default_route; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 791 | } |
| 792 | #endif |
| 793 | |
| 794 | bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const { |
| 795 | // Ignore networks on the explicit ignore list. |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 796 | for (const std::string& ignored_name : network_ignore_list_) { |
| 797 | if (network.name() == ignored_name) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 798 | return true; |
| 799 | } |
| 800 | } |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 801 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 802 | #if defined(WEBRTC_POSIX) |
pthatcher@webrtc.org | dfef028 | 2015-01-07 17:20:52 +0000 | [diff] [blame] | 803 | // Filter out VMware/VirtualBox interfaces, typically named vmnet1, |
| 804 | // vmnet8, or vboxnet0. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 805 | if (strncmp(network.name().c_str(), "vmnet", 5) == 0 || |
pthatcher@webrtc.org | dfef028 | 2015-01-07 17:20:52 +0000 | [diff] [blame] | 806 | strncmp(network.name().c_str(), "vnic", 4) == 0 || |
| 807 | strncmp(network.name().c_str(), "vboxnet", 7) == 0) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 808 | 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 |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 821 | if (strstr(network.description().c_str(), "VMnet") != nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 822 | 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.org | bbce5ef | 2015-03-05 04:38:29 +0000 | [diff] [blame] | 830 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 831 | return false; |
| 832 | } |
| 833 | |
| 834 | void 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 Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 841 | thread_->Post(RTC_FROM_HERE, this, kSignalNetworksMessage); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 842 | } else { |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 843 | thread_->Post(RTC_FROM_HERE, this, kUpdateNetworksMessage); |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 844 | StartNetworkMonitor(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 845 | } |
| 846 | ++start_count_; |
| 847 | } |
| 848 | |
| 849 | void BasicNetworkManager::StopUpdating() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 850 | RTC_DCHECK(Thread::Current() == thread_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 851 | if (!start_count_) |
| 852 | return; |
| 853 | |
| 854 | --start_count_; |
| 855 | if (!start_count_) { |
| 856 | thread_->Clear(this); |
| 857 | sent_first_update_ = false; |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 858 | StopNetworkMonitor(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 862 | void BasicNetworkManager::StartNetworkMonitor() { |
| 863 | NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory(); |
| 864 | if (factory == nullptr) { |
| 865 | return; |
| 866 | } |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 867 | if (!network_monitor_) { |
honghaiz | cec0a08 | 2016-01-15 14:49:09 -0800 | [diff] [blame] | 868 | network_monitor_.reset(factory->CreateNetworkMonitor()); |
| 869 | if (!network_monitor_) { |
| 870 | return; |
| 871 | } |
skvlad | 979c268 | 2016-06-10 15:24:13 -0700 | [diff] [blame] | 872 | network_monitor_->SignalNetworksChanged.connect( |
| 873 | this, &BasicNetworkManager::OnNetworksChanged); |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 874 | } |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 875 | network_monitor_->Start(); |
| 876 | } |
| 877 | |
| 878 | void BasicNetworkManager::StopNetworkMonitor() { |
| 879 | if (!network_monitor_) { |
| 880 | return; |
| 881 | } |
| 882 | network_monitor_->Stop(); |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 883 | } |
| 884 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 885 | void BasicNetworkManager::OnMessage(Message* msg) { |
| 886 | switch (msg->message_id) { |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 887 | case kUpdateNetworksMessage: { |
| 888 | UpdateNetworksContinually(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 889 | break; |
| 890 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 891 | case kSignalNetworksMessage: { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 892 | SignalNetworksChanged(); |
| 893 | break; |
| 894 | } |
| 895 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 896 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 900 | IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 901 | RTC_DCHECK(thread_ == Thread::Current()); |
| 902 | RTC_DCHECK(thread_->socketserver() != nullptr); |
| 903 | RTC_DCHECK(family == AF_INET || family == AF_INET6); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 904 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 905 | std::unique_ptr<AsyncSocket> socket( |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 906 | thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); |
| 907 | if (!socket) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 908 | RTC_LOG_ERR(LERROR) << "Socket creation failed"; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 909 | return IPAddress(); |
| 910 | } |
| 911 | |
guoweis | 56271ed | 2016-01-15 14:45:06 -0800 | [diff] [blame] | 912 | if (socket->Connect(SocketAddress( |
| 913 | family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort)) < |
| 914 | 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 915 | if (socket->GetError() != ENETUNREACH && |
| 916 | socket->GetError() != EHOSTUNREACH) { |
skvlad | 7502401 | 2016-09-29 12:59:35 -0700 | [diff] [blame] | 917 | // Ignore the expected case of "host/net unreachable" - which happens if |
| 918 | // the network is V4- or V6-only. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 919 | RTC_LOG(LS_INFO) << "Connect failed with " << socket->GetError(); |
skvlad | 7502401 | 2016-09-29 12:59:35 -0700 | [diff] [blame] | 920 | } |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 921 | return IPAddress(); |
| 922 | } |
| 923 | return socket->GetLocalAddress().ipaddr(); |
| 924 | } |
| 925 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 926 | void BasicNetworkManager::UpdateNetworksOnce() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 927 | if (!start_count_) |
| 928 | return; |
| 929 | |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 930 | RTC_DCHECK(Thread::Current() == thread_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 931 | |
| 932 | NetworkList list; |
| 933 | if (!CreateNetworks(false, &list)) { |
| 934 | SignalError(); |
| 935 | } else { |
| 936 | bool changed; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 937 | NetworkManager::Stats stats; |
| 938 | MergeNetworkList(list, &changed, &stats); |
| 939 | set_default_local_addresses(QueryDefaultLocalAddress(AF_INET), |
| 940 | QueryDefaultLocalAddress(AF_INET6)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 941 | if (changed || !sent_first_update_) { |
| 942 | SignalNetworksChanged(); |
| 943 | sent_first_update_ = true; |
| 944 | } |
| 945 | } |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 946 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 947 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 948 | void BasicNetworkManager::UpdateNetworksContinually() { |
| 949 | UpdateNetworksOnce(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 950 | thread_->PostDelayed(RTC_FROM_HERE, kNetworksUpdateIntervalMs, this, |
| 951 | kUpdateNetworksMessage); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 952 | } |
| 953 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 954 | void BasicNetworkManager::DumpNetworks() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 955 | NetworkList list; |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 956 | GetNetworks(&list); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 957 | RTC_LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:"; |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 958 | for (const Network* network : list) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 959 | RTC_LOG(LS_INFO) << network->ToString() << ": " << network->description() |
| 960 | << ", active ? " << network->active() |
| 961 | << ((network->ignored()) ? ", Ignored" : ""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 962 | } |
| 963 | } |
| 964 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 965 | Network::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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 972 | prefix_length_(prefix_length), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 973 | key_(MakeNetworkKey(name, prefix, prefix_length)), |
| 974 | scope_id_(0), |
| 975 | ignored_(false), |
| 976 | type_(ADAPTER_TYPE_UNKNOWN), |
| 977 | preference_(0) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 978 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 979 | Network::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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 987 | prefix_length_(prefix_length), |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 988 | key_(MakeNetworkKey(name, prefix, prefix_length)), |
| 989 | scope_id_(0), |
| 990 | ignored_(false), |
| 991 | type_(type), |
| 992 | preference_(0) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 993 | |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 994 | Network::Network(const Network&) = default; |
| 995 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 996 | Network::~Network() = default; |
| 997 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 998 | // 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.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 1000 | bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1001 | // Detect changes with a nested loop; n-squared but we expect on the order |
| 1002 | // of 2-3 addresses per network. |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 1003 | changed = changed || ips.size() != ips_.size(); |
| 1004 | if (!changed) { |
| 1005 | for (const InterfaceAddress& ip : ips) { |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame^] | 1006 | if (!absl::c_linear_search(ips_, ip)) { |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 1007 | changed = true; |
| 1008 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1011 | } |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 1012 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | ips_ = ips; |
| 1014 | return changed; |
| 1015 | } |
| 1016 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1017 | // Select the best IP address to use from this Network. |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1018 | IPAddress Network::GetBestIP() const { |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 1019 | if (ips_.size() == 0) { |
| 1020 | return IPAddress(); |
| 1021 | } |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1022 | |
| 1023 | if (prefix_.family() == AF_INET) { |
| 1024 | return static_cast<IPAddress>(ips_.at(0)); |
| 1025 | } |
| 1026 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1027 | InterfaceAddress selected_ip, ula_ip; |
| 1028 | |
pthatcher@webrtc.org | 5950b64 | 2014-12-01 23:18:27 +0000 | [diff] [blame] | 1029 | for (const InterfaceAddress& ip : ips_) { |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1030 | // 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.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1038 | continue; |
| 1039 | } |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1040 | selected_ip = ip; |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1041 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1042 | // Search could stop once a temporary non-deprecated one is found. |
| 1043 | if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY) |
| 1044 | break; |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 1047 | // 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.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 1055 | webrtc::MdnsResponderInterface* Network::GetMdnsResponder() const { |
| 1056 | if (mdns_responder_provider_ == nullptr) { |
| 1057 | return nullptr; |
| 1058 | } |
| 1059 | return mdns_responder_provider_->GetMdnsResponder(); |
| 1060 | } |
| 1061 | |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 1062 | uint16_t Network::GetCost() const { |
| 1063 | AdapterType type = IsVpn() ? underlying_type_for_vpn_ : type_; |
| 1064 | return ComputeNetworkCostByType(type); |
| 1065 | } |
| 1066 | |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1067 | std::string Network::ToString() const { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 1068 | rtc::StringBuilder ss; |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 1069 | // Print out the first space-terminated token of the network desc, plus |
| 1070 | // the IP address. |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 1071 | 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 Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 1077 | ss << ":id=" << id_ << "]"; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 1078 | return ss.Release(); |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1081 | } // namespace rtc |