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