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