blob: ba056e4635d0be6de427a6203beb6eae3c590a86 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
henrike@webrtc.orgf0488722014-05-13 18:00:2611#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
henrike@webrtc.orgf0488722014-05-13 18:00:2623#endif // WEBRTC_POSIX
24
25#if defined(WEBRTC_WIN)
26#include "webrtc/base/win32.h"
27#include <Iphlpapi.h>
Guo-wei Shieh9faf1542015-12-28 22:06:5528#elif !defined(__native_client__)
29#include "webrtc/base/ifaddrs_converter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2630#endif
31
32#include <stdio.h>
33
34#include <algorithm>
jbauch555604a2016-04-26 10:13:2235#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:2636
37#include "webrtc/base/logging.h"
honghaiz023f3ef2015-10-19 16:39:3238#include "webrtc/base/networkmonitor.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2639#include "webrtc/base/socket.h" // includes something that makes windows happy
40#include "webrtc/base/stream.h"
41#include "webrtc/base/stringencode.h"
42#include "webrtc/base/thread.h"
43
44namespace rtc {
45namespace {
46
guoweis@webrtc.org2444d962015-01-30 00:09:2847// Turning on IPv6 could make many IPv6 interfaces available for connectivity
48// check and delay the call setup time. kMaxIPv6Networks is the default upper
49// limit of IPv6 networks but could be changed by set_max_ipv6_networks().
50const int kMaxIPv6Networks = 5;
51
Peter Boström0c4e06b2015-10-07 10:23:2152const uint32_t kUpdateNetworksMessage = 1;
53const uint32_t kSignalNetworksMessage = 2;
henrike@webrtc.orgf0488722014-05-13 18:00:2654
55// Fetch list of networks every two seconds.
56const int kNetworksUpdateIntervalMs = 2000;
57
58const int kHighestNetworkPreference = 127;
59
guoweis@webrtc.org4bbd3c82014-09-09 13:54:4560typedef struct {
61 Network* net;
guoweis@webrtc.orgfa603982014-09-09 23:42:4062 std::vector<InterfaceAddress> ips;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:4563} AddressList;
64
henrike@webrtc.orgf0488722014-05-13 18:00:2665bool CompareNetworks(const Network* a, const Network* b) {
66 if (a->prefix_length() == b->prefix_length()) {
67 if (a->name() == b->name()) {
68 return a->prefix() < b->prefix();
69 }
70 }
71 return a->name() < b->name();
72}
73
74bool SortNetworks(const Network* a, const Network* b) {
75 // Network types will be preferred above everything else while sorting
76 // Networks.
77
78 // Networks are sorted first by type.
79 if (a->type() != b->type()) {
80 return a->type() < b->type();
81 }
82
guoweis@webrtc.org369a6372014-09-17 22:37:2983 IPAddress ip_a = a->GetBestIP();
84 IPAddress ip_b = b->GetBestIP();
85
henrike@webrtc.orgf0488722014-05-13 18:00:2686 // After type, networks are sorted by IP address precedence values
87 // from RFC 3484-bis
guoweis@webrtc.org369a6372014-09-17 22:37:2988 if (IPAddressPrecedence(ip_a) != IPAddressPrecedence(ip_b)) {
89 return IPAddressPrecedence(ip_a) > IPAddressPrecedence(ip_b);
henrike@webrtc.orgf0488722014-05-13 18:00:2690 }
91
92 // TODO(mallinath) - Add VPN and Link speed conditions while sorting.
93
94 // Networks are sorted last by key.
95 return a->key() > b->key();
96}
97
98std::string AdapterTypeToString(AdapterType type) {
99 switch (type) {
100 case ADAPTER_TYPE_UNKNOWN:
101 return "Unknown";
102 case ADAPTER_TYPE_ETHERNET:
103 return "Ethernet";
104 case ADAPTER_TYPE_WIFI:
105 return "Wifi";
106 case ADAPTER_TYPE_CELLULAR:
107 return "Cellular";
108 case ADAPTER_TYPE_VPN:
109 return "VPN";
phoglund@webrtc.org006521d2015-02-12 09:23:59110 case ADAPTER_TYPE_LOOPBACK:
111 return "Loopback";
henrike@webrtc.orgf0488722014-05-13 18:00:26112 default:
henrikg91d6ede2015-09-17 07:24:34113 RTC_DCHECK(false) << "Invalid type " << type;
henrike@webrtc.orgf0488722014-05-13 18:00:26114 return std::string();
115 }
116}
117
thakis06689a12015-12-30 23:05:30118#if !defined(__native_client__)
Guo-wei Shieh9faf1542015-12-28 22:06:55119bool IsIgnoredIPv6(const InterfaceAddress& ip) {
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20120 if (ip.family() != AF_INET6) {
121 return false;
122 }
123
124 // Link-local addresses require scope id to be bound successfully.
125 // However, our IPAddress structure doesn't carry that so the
126 // information is lost and causes binding failure.
127 if (IPIsLinkLocal(ip)) {
128 return true;
129 }
130
131 // Any MAC based IPv6 should be avoided to prevent the MAC tracking.
132 if (IPIsMacBased(ip)) {
133 return true;
134 }
135
Guo-wei Shieh9faf1542015-12-28 22:06:55136 // Ignore deprecated IPv6.
137 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED) {
138 return true;
139 }
140
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20141 return false;
142}
thakis06689a12015-12-30 23:05:30143#endif // !defined(__native_client__)
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20144
henrike@webrtc.orgf0488722014-05-13 18:00:26145} // namespace
146
Guo-wei Shieh9af97f82015-11-10 22:47:39147// These addresses are used as the targets to find out the default local address
148// on a multi-homed endpoint. They are actually DNS servers.
149const char kPublicIPv4Host[] = "8.8.8.8";
150const char kPublicIPv6Host[] = "2001:4860:4860::8888";
151const int kPublicPort = 53; // DNS port.
152
henrike@webrtc.orgf0488722014-05-13 18:00:26153std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
154 int prefix_length) {
155 std::ostringstream ost;
156 ost << name << "%" << prefix.ToString() << "/" << prefix_length;
157 return ost.str();
158}
159
160NetworkManager::NetworkManager() {
161}
162
163NetworkManager::~NetworkManager() {
164}
165
Guo-wei Shieh86cb9232015-08-19 17:57:53166NetworkManager::EnumerationPermission NetworkManager::enumeration_permission()
167 const {
guoweisea1012b2015-08-21 16:06:28168 return ENUMERATION_ALLOWED;
Guo-wei Shieh86cb9232015-08-19 17:57:53169}
170
Guo-wei Shieh9af97f82015-11-10 22:47:39171bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const {
172 return false;
173}
174
guoweis@webrtc.org2444d962015-01-30 00:09:28175NetworkManagerBase::NetworkManagerBase()
guoweisea1012b2015-08-21 16:06:28176 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED),
Guo-wei Shieh47872ec2015-08-19 17:32:46177 max_ipv6_networks_(kMaxIPv6Networks),
178 ipv6_enabled_(true) {
henrike@webrtc.orgf0488722014-05-13 18:00:26179}
180
181NetworkManagerBase::~NetworkManagerBase() {
pthatcher@webrtc.org5950b642014-12-01 23:18:27182 for (const auto& kv : networks_map_) {
183 delete kv.second;
henrike@webrtc.orgf0488722014-05-13 18:00:26184 }
185}
186
Guo-wei Shieh47872ec2015-08-19 17:32:46187NetworkManager::EnumerationPermission
188NetworkManagerBase::enumeration_permission() const {
189 return enumeration_permission_;
190}
191
guoweis@webrtc.orgf358aea2015-02-18 18:44:01192void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) {
193 if (!ipv4_any_address_network_) {
194 const rtc::IPAddress ipv4_any_address(INADDR_ANY);
195 ipv4_any_address_network_.reset(
196 new rtc::Network("any", "any", ipv4_any_address, 0));
Guo-wei Shieh9af97f82015-11-10 22:47:39197 ipv4_any_address_network_->set_default_local_address_provider(this);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01198 ipv4_any_address_network_->AddIP(ipv4_any_address);
199 }
200 networks->push_back(ipv4_any_address_network_.get());
201
202 if (ipv6_enabled()) {
203 if (!ipv6_any_address_network_) {
204 const rtc::IPAddress ipv6_any_address(in6addr_any);
205 ipv6_any_address_network_.reset(
206 new rtc::Network("any", "any", ipv6_any_address, 0));
Guo-wei Shieh9af97f82015-11-10 22:47:39207 ipv6_any_address_network_->set_default_local_address_provider(this);
guoweis@webrtc.orgf358aea2015-02-18 18:44:01208 ipv6_any_address_network_->AddIP(ipv6_any_address);
209 }
210 networks->push_back(ipv6_any_address_network_.get());
211 }
212}
213
henrike@webrtc.orgf0488722014-05-13 18:00:26214void NetworkManagerBase::GetNetworks(NetworkList* result) const {
guoweis@webrtc.org2444d962015-01-30 00:09:28215 int ipv6_networks = 0;
216 result->clear();
217 for (Network* network : networks_) {
218 // Keep the number of IPv6 networks under |max_ipv6_networks_|.
219 if (network->prefix().family() == AF_INET6) {
220 if (ipv6_networks >= max_ipv6_networks_) {
221 continue;
222 }
223 ++ipv6_networks;
224 }
225 result->push_back(network);
226 }
henrike@webrtc.orgf0488722014-05-13 18:00:26227}
228
229void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
230 bool* changed) {
guoweis@webrtc.orga094cac2015-01-28 19:34:05231 NetworkManager::Stats stats;
232 MergeNetworkList(new_networks, changed, &stats);
233}
234
235void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks,
236 bool* changed,
237 NetworkManager::Stats* stats) {
honghaizdb8cf502015-12-21 21:08:46238 *changed = false;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45239 // AddressList in this map will track IP addresses for all Networks
240 // with the same key.
241 std::map<std::string, AddressList> consolidated_address_list;
henrike@webrtc.orgf0488722014-05-13 18:00:26242 NetworkList list(new_networks);
henrike@webrtc.orgf0488722014-05-13 18:00:26243 std::sort(list.begin(), list.end(), CompareNetworks);
henrike@webrtc.orgf0488722014-05-13 18:00:26244 // First, build a set of network-keys to the ipaddresses.
pthatcher@webrtc.org5950b642014-12-01 23:18:27245 for (Network* network : list) {
henrike@webrtc.orgf0488722014-05-13 18:00:26246 bool might_add_to_merged_list = false;
pthatcher@webrtc.org5950b642014-12-01 23:18:27247 std::string key = MakeNetworkKey(network->name(),
248 network->prefix(),
249 network->prefix_length());
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45250 if (consolidated_address_list.find(key) ==
251 consolidated_address_list.end()) {
252 AddressList addrlist;
pthatcher@webrtc.org5950b642014-12-01 23:18:27253 addrlist.net = network;
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45254 consolidated_address_list[key] = addrlist;
henrike@webrtc.orgf0488722014-05-13 18:00:26255 might_add_to_merged_list = true;
256 }
pthatcher@webrtc.org5950b642014-12-01 23:18:27257 const std::vector<InterfaceAddress>& addresses = network->GetIPs();
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45258 AddressList& current_list = consolidated_address_list[key];
pthatcher@webrtc.org5950b642014-12-01 23:18:27259 for (const InterfaceAddress& address : addresses) {
260 current_list.ips.push_back(address);
henrike@webrtc.orgf0488722014-05-13 18:00:26261 }
262 if (!might_add_to_merged_list) {
pthatcher@webrtc.org5950b642014-12-01 23:18:27263 delete network;
guoweis@webrtc.orga094cac2015-01-28 19:34:05264 } else {
265 if (current_list.ips[0].family() == AF_INET) {
266 stats->ipv4_network_count++;
267 } else {
268 ASSERT(current_list.ips[0].family() == AF_INET6);
269 stats->ipv6_network_count++;
270 }
henrike@webrtc.orgf0488722014-05-13 18:00:26271 }
272 }
273
274 // Next, look for existing network objects to re-use.
honghaizdb8cf502015-12-21 21:08:46275 // Result of Network merge. Element in this list should have unique key.
276 NetworkList merged_list;
pthatcher@webrtc.org5950b642014-12-01 23:18:27277 for (const auto& kv : consolidated_address_list) {
278 const std::string& key = kv.first;
279 Network* net = kv.second.net;
280 auto existing = networks_map_.find(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26281 if (existing == networks_map_.end()) {
282 // This network is new. Place it in the network map.
283 merged_list.push_back(net);
284 networks_map_[key] = net;
honghaiza0c44ea2016-03-23 23:07:48285 net->set_id(next_available_network_id_++);
guoweis@webrtc.org4bbd3c82014-09-09 13:54:45286 // Also, we might have accumulated IPAddresses from the first
287 // step, set it here.
pthatcher@webrtc.org5950b642014-12-01 23:18:27288 net->SetIPs(kv.second.ips, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26289 *changed = true;
290 } else {
291 // This network exists in the map already. Reset its IP addresses.
honghaizdb8cf502015-12-21 21:08:46292 Network* existing_net = existing->second;
293 *changed = existing_net->SetIPs(kv.second.ips, *changed);
294 merged_list.push_back(existing_net);
Honghai Zhang351d77b2016-05-20 22:08:29295 if (net->type() != ADAPTER_TYPE_UNKNOWN &&
296 net->type() != existing_net->type()) {
297 existing_net->set_type(net->type());
298 *changed = true;
299 }
honghaizdb8cf502015-12-21 21:08:46300 // If the existing network was not active, networks have changed.
301 if (!existing_net->active()) {
302 *changed = true;
303 }
304 ASSERT(net->active());
305 if (existing_net != net) {
henrike@webrtc.orgf0488722014-05-13 18:00:26306 delete net;
307 }
308 }
309 }
honghaizdb8cf502015-12-21 21:08:46310 // It may still happen that the merged list is a subset of |networks_|.
311 // To detect this change, we compare their sizes.
312 if (merged_list.size() != networks_.size()) {
313 *changed = true;
314 }
henrike@webrtc.orgf0488722014-05-13 18:00:26315
honghaizdb8cf502015-12-21 21:08:46316 // If the network list changes, we re-assign |networks_| to the merged list
317 // and re-sort it.
guoweis@webrtc.orga094cac2015-01-28 19:34:05318 if (*changed) {
honghaizdb8cf502015-12-21 21:08:46319 networks_ = merged_list;
320 // Reset the active states of all networks.
321 for (const auto& kv : networks_map_) {
honghaize3c6c822016-02-17 21:00:28322 Network* network = kv.second;
323 // If |network| is in the newly generated |networks_|, it is active.
324 bool found = std::find(networks_.begin(), networks_.end(), network) !=
325 networks_.end();
326 network->set_active(found);
honghaizdb8cf502015-12-21 21:08:46327 }
henrike@webrtc.orgf0488722014-05-13 18:00:26328 std::sort(networks_.begin(), networks_.end(), SortNetworks);
329 // Now network interfaces are sorted, we should set the preference value
330 // for each of the interfaces we are planning to use.
331 // Preference order of network interfaces might have changed from previous
332 // sorting due to addition of higher preference network interface.
333 // Since we have already sorted the network interfaces based on our
334 // requirements, we will just assign a preference value starting with 127,
335 // in decreasing order.
336 int pref = kHighestNetworkPreference;
pthatcher@webrtc.org5950b642014-12-01 23:18:27337 for (Network* network : networks_) {
338 network->set_preference(pref);
henrike@webrtc.orgf0488722014-05-13 18:00:26339 if (pref > 0) {
340 --pref;
341 } else {
342 LOG(LS_ERROR) << "Too many network interfaces to handle!";
343 break;
344 }
345 }
346 }
347}
348
Guo-wei Shieh9af97f82015-11-10 22:47:39349void NetworkManagerBase::set_default_local_addresses(const IPAddress& ipv4,
350 const IPAddress& ipv6) {
351 if (ipv4.family() == AF_INET) {
352 default_local_ipv4_address_ = ipv4;
353 }
354 if (ipv6.family() == AF_INET6) {
355 default_local_ipv6_address_ = ipv6;
356 }
357}
358
359bool NetworkManagerBase::GetDefaultLocalAddress(int family,
360 IPAddress* ipaddr) const {
Guo-wei Shieha34c39e2015-11-25 21:12:26361 if (family == AF_INET && !default_local_ipv4_address_.IsNil()) {
Guo-wei Shieh9af97f82015-11-10 22:47:39362 *ipaddr = default_local_ipv4_address_;
363 return true;
Guo-wei Shieha34c39e2015-11-25 21:12:26364 } else if (family == AF_INET6 && !default_local_ipv6_address_.IsNil()) {
honghaizaf83fe62016-04-18 21:50:44365 Network* ipv6_network = GetNetworkFromAddress(default_local_ipv6_address_);
366 if (ipv6_network) {
367 // If the default ipv6 network's BestIP is different than
368 // default_local_ipv6_address_, use it instead.
369 // This is to prevent potential IP address leakage. See WebRTC bug 5376.
370 *ipaddr = ipv6_network->GetBestIP();
371 } else {
372 *ipaddr = default_local_ipv6_address_;
373 }
Guo-wei Shieh9af97f82015-11-10 22:47:39374 return true;
375 }
376 return false;
377}
378
honghaizaf83fe62016-04-18 21:50:44379Network* NetworkManagerBase::GetNetworkFromAddress(
380 const rtc::IPAddress& ip) const {
381 for (Network* network : networks_) {
382 const auto& ips = network->GetIPs();
383 if (std::find_if(ips.begin(), ips.end(),
384 [ip](const InterfaceAddress& existing_ip) {
385 return ip == static_cast<rtc::IPAddress>(existing_ip);
386 }) != ips.end()) {
387 return network;
388 }
389 }
390 return nullptr;
391}
392
henrike@webrtc.orgf0488722014-05-13 18:00:26393BasicNetworkManager::BasicNetworkManager()
394 : thread_(NULL), sent_first_update_(false), start_count_(0),
395 ignore_non_default_routes_(false) {
396}
397
398BasicNetworkManager::~BasicNetworkManager() {
399}
400
honghaiz023f3ef2015-10-19 16:39:32401void BasicNetworkManager::OnNetworksChanged() {
Honghai Zhang351d77b2016-05-20 22:08:29402 LOG(LS_INFO) << "Network change was observed";
honghaiz023f3ef2015-10-19 16:39:32403 UpdateNetworksOnce();
404}
405
henrike@webrtc.orgf0488722014-05-13 18:00:26406#if defined(__native_client__)
407
408bool BasicNetworkManager::CreateNetworks(bool include_ignored,
409 NetworkList* networks) const {
410 ASSERT(false);
411 LOG(LS_WARNING) << "BasicNetworkManager doesn't work on NaCl yet";
412 return false;
413}
414
415#elif defined(WEBRTC_POSIX)
416void BasicNetworkManager::ConvertIfAddrs(struct ifaddrs* interfaces,
Guo-wei Shieh9faf1542015-12-28 22:06:55417 IfAddrsConverter* ifaddrs_converter,
henrike@webrtc.orgf0488722014-05-13 18:00:26418 bool include_ignored,
419 NetworkList* networks) const {
420 NetworkMap current_networks;
Guo-wei Shieh9faf1542015-12-28 22:06:55421
henrike@webrtc.orgf0488722014-05-13 18:00:26422 for (struct ifaddrs* cursor = interfaces;
423 cursor != NULL; cursor = cursor->ifa_next) {
424 IPAddress prefix;
425 IPAddress mask;
Guo-wei Shieh9faf1542015-12-28 22:06:55426 InterfaceAddress ip;
henrike@webrtc.orgf0488722014-05-13 18:00:26427 int scope_id = 0;
428
429 // Some interfaces may not have address assigned.
Guo-wei Shieh9faf1542015-12-28 22:06:55430 if (!cursor->ifa_addr || !cursor->ifa_netmask) {
henrike@webrtc.orgf0488722014-05-13 18:00:26431 continue;
Guo-wei Shieh9faf1542015-12-28 22:06:55432 }
433 // Skip ones which are down.
434 if (!(cursor->ifa_flags & IFF_RUNNING)) {
435 continue;
436 }
437 // Skip unknown family.
438 if (cursor->ifa_addr->sa_family != AF_INET &&
439 cursor->ifa_addr->sa_family != AF_INET6) {
440 continue;
441 }
442 // Skip IPv6 if not enabled.
443 if (cursor->ifa_addr->sa_family == AF_INET6 && !ipv6_enabled()) {
444 continue;
445 }
446 // Convert to InterfaceAddress.
447 if (!ifaddrs_converter->ConvertIfAddrsToIPAddress(cursor, &ip, &mask)) {
448 continue;
449 }
henrike@webrtc.orgf0488722014-05-13 18:00:26450
Guo-wei Shieh9faf1542015-12-28 22:06:55451 // Special case for IPv6 address.
452 if (cursor->ifa_addr->sa_family == AF_INET6) {
453 if (IsIgnoredIPv6(ip)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26454 continue;
455 }
Guo-wei Shieh9faf1542015-12-28 22:06:55456 scope_id =
457 reinterpret_cast<sockaddr_in6*>(cursor->ifa_addr)->sin6_scope_id;
henrike@webrtc.orgf0488722014-05-13 18:00:26458 }
459
Honghai Zhang351d77b2016-05-20 22:08:29460 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
461 if (cursor->ifa_flags & IFF_LOOPBACK) {
462 adapter_type = ADAPTER_TYPE_LOOPBACK;
463 } else {
464 adapter_type = GetAdapterTypeFromName(cursor->ifa_name);
465 }
henrike@webrtc.orgf0488722014-05-13 18:00:26466 int prefix_length = CountIPMaskBits(mask);
467 prefix = TruncateIP(ip, prefix_length);
468 std::string key = MakeNetworkKey(std::string(cursor->ifa_name),
469 prefix, prefix_length);
Honghai Zhang351d77b2016-05-20 22:08:29470 auto iter = current_networks.find(key);
471 if (iter == current_networks.end()) {
Noah Richards2e266e92015-04-07 18:51:56472 // TODO(phoglund): Need to recognize other types as well.
jbauch555604a2016-04-26 10:13:22473 std::unique_ptr<Network> network(
474 new Network(cursor->ifa_name, cursor->ifa_name, prefix, prefix_length,
475 adapter_type));
Guo-wei Shieh9af97f82015-11-10 22:47:39476 network->set_default_local_address_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26477 network->set_scope_id(scope_id);
478 network->AddIP(ip);
phoglund@webrtc.org006521d2015-02-12 09:23:59479 network->set_ignored(IsIgnoredNetwork(*network));
henrike@webrtc.orgf0488722014-05-13 18:00:26480 if (include_ignored || !network->ignored()) {
honghaizdb8cf502015-12-21 21:08:46481 current_networks[key] = network.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26482 networks->push_back(network.release());
483 }
484 } else {
Honghai Zhang351d77b2016-05-20 22:08:29485 Network* existing_network = iter->second;
486 existing_network->AddIP(ip);
487 if (adapter_type != ADAPTER_TYPE_UNKNOWN) {
488 existing_network->set_type(adapter_type);
489 }
henrike@webrtc.orgf0488722014-05-13 18:00:26490 }
491 }
492}
493
494bool BasicNetworkManager::CreateNetworks(bool include_ignored,
495 NetworkList* networks) const {
496 struct ifaddrs* interfaces;
497 int error = getifaddrs(&interfaces);
498 if (error != 0) {
499 LOG_ERR(LERROR) << "getifaddrs failed to gather interface data: " << error;
500 return false;
501 }
502
jbauch555604a2016-04-26 10:13:22503 std::unique_ptr<IfAddrsConverter> ifaddrs_converter(CreateIfAddrsConverter());
Guo-wei Shieh9faf1542015-12-28 22:06:55504 ConvertIfAddrs(interfaces, ifaddrs_converter.get(), include_ignored,
505 networks);
henrike@webrtc.orgf0488722014-05-13 18:00:26506
507 freeifaddrs(interfaces);
508 return true;
509}
510
511#elif defined(WEBRTC_WIN)
512
513unsigned int GetPrefix(PIP_ADAPTER_PREFIX prefixlist,
514 const IPAddress& ip, IPAddress* prefix) {
515 IPAddress current_prefix;
516 IPAddress best_prefix;
517 unsigned int best_length = 0;
518 while (prefixlist) {
519 // Look for the longest matching prefix in the prefixlist.
520 if (prefixlist->Address.lpSockaddr == NULL ||
521 prefixlist->Address.lpSockaddr->sa_family != ip.family()) {
522 prefixlist = prefixlist->Next;
523 continue;
524 }
525 switch (prefixlist->Address.lpSockaddr->sa_family) {
526 case AF_INET: {
527 sockaddr_in* v4_addr =
528 reinterpret_cast<sockaddr_in*>(prefixlist->Address.lpSockaddr);
529 current_prefix = IPAddress(v4_addr->sin_addr);
530 break;
531 }
532 case AF_INET6: {
533 sockaddr_in6* v6_addr =
534 reinterpret_cast<sockaddr_in6*>(prefixlist->Address.lpSockaddr);
535 current_prefix = IPAddress(v6_addr->sin6_addr);
536 break;
537 }
538 default: {
539 prefixlist = prefixlist->Next;
540 continue;
541 }
542 }
543 if (TruncateIP(ip, prefixlist->PrefixLength) == current_prefix &&
544 prefixlist->PrefixLength > best_length) {
545 best_prefix = current_prefix;
546 best_length = prefixlist->PrefixLength;
547 }
548 prefixlist = prefixlist->Next;
549 }
550 *prefix = best_prefix;
551 return best_length;
552}
553
554bool BasicNetworkManager::CreateNetworks(bool include_ignored,
555 NetworkList* networks) const {
556 NetworkMap current_networks;
557 // MSDN recommends a 15KB buffer for the first try at GetAdaptersAddresses.
558 size_t buffer_size = 16384;
jbauch555604a2016-04-26 10:13:22559 std::unique_ptr<char[]> adapter_info(new char[buffer_size]);
henrike@webrtc.orgf0488722014-05-13 18:00:26560 PIP_ADAPTER_ADDRESSES adapter_addrs =
561 reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
562 int adapter_flags = (GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_ANYCAST |
563 GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_INCLUDE_PREFIX);
564 int ret = 0;
565 do {
566 adapter_info.reset(new char[buffer_size]);
567 adapter_addrs = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(adapter_info.get());
568 ret = GetAdaptersAddresses(AF_UNSPEC, adapter_flags,
569 0, adapter_addrs,
570 reinterpret_cast<PULONG>(&buffer_size));
571 } while (ret == ERROR_BUFFER_OVERFLOW);
572 if (ret != ERROR_SUCCESS) {
573 return false;
574 }
575 int count = 0;
576 while (adapter_addrs) {
577 if (adapter_addrs->OperStatus == IfOperStatusUp) {
578 PIP_ADAPTER_UNICAST_ADDRESS address = adapter_addrs->FirstUnicastAddress;
579 PIP_ADAPTER_PREFIX prefixlist = adapter_addrs->FirstPrefix;
580 std::string name;
581 std::string description;
tfarinaa41ab932015-10-30 23:08:48582#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26583 name = ToUtf8(adapter_addrs->FriendlyName,
584 wcslen(adapter_addrs->FriendlyName));
585#endif
586 description = ToUtf8(adapter_addrs->Description,
587 wcslen(adapter_addrs->Description));
588 for (; address; address = address->Next) {
tfarinaa41ab932015-10-30 23:08:48589#if defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26590 name = rtc::ToString(count);
591#endif
592
593 IPAddress ip;
594 int scope_id = 0;
jbauch555604a2016-04-26 10:13:22595 std::unique_ptr<Network> network;
henrike@webrtc.orgf0488722014-05-13 18:00:26596 switch (address->Address.lpSockaddr->sa_family) {
597 case AF_INET: {
598 sockaddr_in* v4_addr =
599 reinterpret_cast<sockaddr_in*>(address->Address.lpSockaddr);
600 ip = IPAddress(v4_addr->sin_addr);
601 break;
602 }
603 case AF_INET6: {
604 if (ipv6_enabled()) {
605 sockaddr_in6* v6_addr =
606 reinterpret_cast<sockaddr_in6*>(address->Address.lpSockaddr);
607 scope_id = v6_addr->sin6_scope_id;
608 ip = IPAddress(v6_addr->sin6_addr);
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20609
610 if (IsIgnoredIPv6(ip)) {
611 continue;
612 }
613
henrike@webrtc.orgf0488722014-05-13 18:00:26614 break;
615 } else {
616 continue;
617 }
618 }
619 default: {
620 continue;
621 }
622 }
623
624 IPAddress prefix;
625 int prefix_length = GetPrefix(prefixlist, ip, &prefix);
626 std::string key = MakeNetworkKey(name, prefix, prefix_length);
pthatcher@webrtc.org5950b642014-12-01 23:18:27627 auto existing_network = current_networks.find(key);
henrike@webrtc.orgf0488722014-05-13 18:00:26628 if (existing_network == current_networks.end()) {
phoglund@webrtc.org006521d2015-02-12 09:23:59629 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
630 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
631 // TODO(phoglund): Need to recognize other types as well.
632 adapter_type = ADAPTER_TYPE_LOOPBACK;
633 }
jbauch555604a2016-04-26 10:13:22634 std::unique_ptr<Network> network(new Network(
635 name, description, prefix, prefix_length, adapter_type));
Guo-wei Shieh9af97f82015-11-10 22:47:39636 network->set_default_local_address_provider(this);
henrike@webrtc.orgf0488722014-05-13 18:00:26637 network->set_scope_id(scope_id);
638 network->AddIP(ip);
phoglund@webrtc.org006521d2015-02-12 09:23:59639 bool ignored = IsIgnoredNetwork(*network);
640 network->set_ignored(ignored);
henrike@webrtc.orgf0488722014-05-13 18:00:26641 if (include_ignored || !network->ignored()) {
honghaizdb8cf502015-12-21 21:08:46642 current_networks[key] = network.get();
henrike@webrtc.orgf0488722014-05-13 18:00:26643 networks->push_back(network.release());
644 }
645 } else {
646 (*existing_network).second->AddIP(ip);
647 }
648 }
649 // Count is per-adapter - all 'Networks' created from the same
650 // adapter need to have the same name.
651 ++count;
652 }
653 adapter_addrs = adapter_addrs->Next;
654 }
655 return true;
656}
guoweis@webrtc.org369a6372014-09-17 22:37:29657#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26658
659#if defined(WEBRTC_LINUX)
660bool IsDefaultRoute(const std::string& network_name) {
661 FileStream fs;
662 if (!fs.Open("/proc/net/route", "r", NULL)) {
663 LOG(LS_WARNING) << "Couldn't read /proc/net/route, skipping default "
664 << "route check (assuming everything is a default route).";
665 return true;
666 } else {
667 std::string line;
668 while (fs.ReadLine(&line) == SR_SUCCESS) {
669 char iface_name[256];
670 unsigned int iface_ip, iface_gw, iface_mask, iface_flags;
671 if (sscanf(line.c_str(),
672 "%255s %8X %8X %4X %*d %*u %*d %8X",
673 iface_name, &iface_ip, &iface_gw,
674 &iface_flags, &iface_mask) == 5 &&
675 network_name == iface_name &&
676 iface_mask == 0 &&
677 (iface_flags & (RTF_UP | RTF_HOST)) == RTF_UP) {
678 return true;
679 }
680 }
681 }
682 return false;
683}
684#endif
685
686bool BasicNetworkManager::IsIgnoredNetwork(const Network& network) const {
687 // Ignore networks on the explicit ignore list.
pthatcher@webrtc.org5950b642014-12-01 23:18:27688 for (const std::string& ignored_name : network_ignore_list_) {
689 if (network.name() == ignored_name) {
henrike@webrtc.orgf0488722014-05-13 18:00:26690 return true;
691 }
692 }
phoglund@webrtc.org006521d2015-02-12 09:23:59693
henrike@webrtc.orgf0488722014-05-13 18:00:26694#if defined(WEBRTC_POSIX)
pthatcher@webrtc.orgdfef0282015-01-07 17:20:52695 // Filter out VMware/VirtualBox interfaces, typically named vmnet1,
696 // vmnet8, or vboxnet0.
henrike@webrtc.orgf0488722014-05-13 18:00:26697 if (strncmp(network.name().c_str(), "vmnet", 5) == 0 ||
pthatcher@webrtc.orgdfef0282015-01-07 17:20:52698 strncmp(network.name().c_str(), "vnic", 4) == 0 ||
699 strncmp(network.name().c_str(), "vboxnet", 7) == 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26700 return true;
701 }
702#if defined(WEBRTC_LINUX)
703 // Make sure this is a default route, if we're ignoring non-defaults.
704 if (ignore_non_default_routes_ && !IsDefaultRoute(network.name())) {
705 return true;
706 }
707#endif
708#elif defined(WEBRTC_WIN)
709 // Ignore any HOST side vmware adapters with a description like:
710 // VMware Virtual Ethernet Adapter for VMnet1
711 // but don't ignore any GUEST side adapters with a description like:
712 // VMware Accelerated AMD PCNet Adapter #2
713 if (strstr(network.description().c_str(), "VMnet") != NULL) {
714 return true;
715 }
716#endif
717
718 // Ignore any networks with a 0.x.y.z IP
719 if (network.prefix().family() == AF_INET) {
720 return (network.prefix().v4AddressAsHostOrderInteger() < 0x01000000);
721 }
guoweis@webrtc.orgbbce5ef2015-03-05 04:38:29722
henrike@webrtc.orgf0488722014-05-13 18:00:26723 return false;
724}
725
726void BasicNetworkManager::StartUpdating() {
727 thread_ = Thread::Current();
728 if (start_count_) {
729 // If network interfaces are already discovered and signal is sent,
730 // we should trigger network signal immediately for the new clients
731 // to start allocating ports.
732 if (sent_first_update_)
Taylor Brandstetter5d97a9a2016-06-10 21:17:27733 thread_->Post(RTC_FROM_HERE, this, kSignalNetworksMessage);
henrike@webrtc.orgf0488722014-05-13 18:00:26734 } else {
Taylor Brandstetter5d97a9a2016-06-10 21:17:27735 thread_->Post(RTC_FROM_HERE, this, kUpdateNetworksMessage);
honghaiz023f3ef2015-10-19 16:39:32736 StartNetworkMonitor();
henrike@webrtc.orgf0488722014-05-13 18:00:26737 }
738 ++start_count_;
739}
740
741void BasicNetworkManager::StopUpdating() {
742 ASSERT(Thread::Current() == thread_);
743 if (!start_count_)
744 return;
745
746 --start_count_;
747 if (!start_count_) {
748 thread_->Clear(this);
749 sent_first_update_ = false;
honghaiz023f3ef2015-10-19 16:39:32750 StopNetworkMonitor();
henrike@webrtc.orgf0488722014-05-13 18:00:26751 }
752}
753
honghaiz023f3ef2015-10-19 16:39:32754void BasicNetworkManager::StartNetworkMonitor() {
755 NetworkMonitorFactory* factory = NetworkMonitorFactory::GetFactory();
756 if (factory == nullptr) {
757 return;
758 }
honghaiz023f3ef2015-10-19 16:39:32759 if (!network_monitor_) {
honghaizcec0a082016-01-15 22:49:09760 network_monitor_.reset(factory->CreateNetworkMonitor());
761 if (!network_monitor_) {
762 return;
763 }
skvlad979c2682016-06-10 22:24:13764 network_monitor_->SignalNetworksChanged.connect(
765 this, &BasicNetworkManager::OnNetworksChanged);
honghaiz023f3ef2015-10-19 16:39:32766 }
honghaiz023f3ef2015-10-19 16:39:32767 network_monitor_->Start();
768}
769
770void BasicNetworkManager::StopNetworkMonitor() {
771 if (!network_monitor_) {
772 return;
773 }
774 network_monitor_->Stop();
honghaiz023f3ef2015-10-19 16:39:32775}
776
henrike@webrtc.orgf0488722014-05-13 18:00:26777void BasicNetworkManager::OnMessage(Message* msg) {
778 switch (msg->message_id) {
honghaiz023f3ef2015-10-19 16:39:32779 case kUpdateNetworksMessage: {
780 UpdateNetworksContinually();
henrike@webrtc.orgf0488722014-05-13 18:00:26781 break;
782 }
783 case kSignalNetworksMessage: {
784 SignalNetworksChanged();
785 break;
786 }
787 default:
788 ASSERT(false);
789 }
790}
791
Honghai Zhang351d77b2016-05-20 22:08:29792AdapterType BasicNetworkManager::GetAdapterTypeFromName(
793 const char* network_name) const {
794 // If there is a network_monitor, use it to get the adapter type.
795 // Otherwise, get the adapter type based on a few name matching rules.
796 if (network_monitor_) {
797 AdapterType type = network_monitor_->GetAdapterType(network_name);
798 if (type != ADAPTER_TYPE_UNKNOWN) {
799 return type;
800 }
801 }
802#if defined(WEBRTC_IOS)
803 // Cell networks are pdp_ipN on iOS.
804 if (strncmp(network_name, "pdp_ip", 6) == 0) {
805 return ADAPTER_TYPE_CELLULAR;
806 }
Honghai Zhang63ab8102016-05-27 03:30:15807 if (strncmp(network_name, "en", 2) == 0) {
808 // This may not be most accurate because sometimes Ethernet interface
809 // name also starts with "en" but it is better than showing it as
810 // "unknown" type.
811 // TODO(honghaiz): Write a proper IOS network manager.
812 return ADAPTER_TYPE_WIFI;
813 }
Honghai Zhang351d77b2016-05-20 22:08:29814#elif defined(WEBRTC_ANDROID)
815 if (strncmp(network_name, "rmnet", 5) == 0 ||
816 strncmp(network_name, "v4-rmnet", 8) == 0) {
817 return ADAPTER_TYPE_CELLULAR;
818 }
819 if (strncmp(network_name, "wlan", 4) == 0) {
820 return ADAPTER_TYPE_WIFI;
821 }
822#endif
823
824 return ADAPTER_TYPE_UNKNOWN;
825}
826
Guo-wei Shieh9af97f82015-11-10 22:47:39827IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const {
828 ASSERT(thread_ == Thread::Current());
829 ASSERT(thread_->socketserver() != nullptr);
830 ASSERT(family == AF_INET || family == AF_INET6);
831
jbauch555604a2016-04-26 10:13:22832 std::unique_ptr<AsyncSocket> socket(
Guo-wei Shieh9af97f82015-11-10 22:47:39833 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM));
834 if (!socket) {
guoweis56271ed2016-01-15 22:45:06835 LOG_ERR(LERROR) << "Socket creation failed";
Guo-wei Shieh9af97f82015-11-10 22:47:39836 return IPAddress();
837 }
838
guoweis56271ed2016-01-15 22:45:06839 if (socket->Connect(SocketAddress(
840 family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, kPublicPort)) <
841 0) {
skvlad75024012016-09-29 19:59:35842 if (socket->GetError() != ENETUNREACH
843 && socket->GetError() != EHOSTUNREACH) {
844 // Ignore the expected case of "host/net unreachable" - which happens if
845 // the network is V4- or V6-only.
846 LOG(LS_INFO) << "Connect failed with " << socket->GetError();
847 }
Guo-wei Shieh9af97f82015-11-10 22:47:39848 return IPAddress();
849 }
850 return socket->GetLocalAddress().ipaddr();
851}
852
honghaiz023f3ef2015-10-19 16:39:32853void BasicNetworkManager::UpdateNetworksOnce() {
henrike@webrtc.orgf0488722014-05-13 18:00:26854 if (!start_count_)
855 return;
856
857 ASSERT(Thread::Current() == thread_);
858
859 NetworkList list;
860 if (!CreateNetworks(false, &list)) {
861 SignalError();
862 } else {
863 bool changed;
Guo-wei Shieh9af97f82015-11-10 22:47:39864 NetworkManager::Stats stats;
865 MergeNetworkList(list, &changed, &stats);
866 set_default_local_addresses(QueryDefaultLocalAddress(AF_INET),
867 QueryDefaultLocalAddress(AF_INET6));
henrike@webrtc.orgf0488722014-05-13 18:00:26868 if (changed || !sent_first_update_) {
869 SignalNetworksChanged();
870 sent_first_update_ = true;
871 }
872 }
honghaiz023f3ef2015-10-19 16:39:32873}
henrike@webrtc.orgf0488722014-05-13 18:00:26874
honghaiz023f3ef2015-10-19 16:39:32875void BasicNetworkManager::UpdateNetworksContinually() {
876 UpdateNetworksOnce();
Taylor Brandstetter5d97a9a2016-06-10 21:17:27877 thread_->PostDelayed(RTC_FROM_HERE, kNetworksUpdateIntervalMs, this,
878 kUpdateNetworksMessage);
henrike@webrtc.orgf0488722014-05-13 18:00:26879}
880
honghaizdb8cf502015-12-21 21:08:46881void BasicNetworkManager::DumpNetworks() {
henrike@webrtc.orgf0488722014-05-13 18:00:26882 NetworkList list;
honghaizdb8cf502015-12-21 21:08:46883 GetNetworks(&list);
henrike@webrtc.orgf0488722014-05-13 18:00:26884 LOG(LS_INFO) << "NetworkManager detected " << list.size() << " networks:";
pthatcher@webrtc.org5950b642014-12-01 23:18:27885 for (const Network* network : list) {
honghaizdb8cf502015-12-21 21:08:46886 LOG(LS_INFO) << network->ToString() << ": " << network->description()
887 << ", active ? " << network->active()
888 << ((network->ignored()) ? ", Ignored" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26889 }
890}
891
Guo-wei Shieh9af97f82015-11-10 22:47:39892Network::Network(const std::string& name,
893 const std::string& desc,
894 const IPAddress& prefix,
895 int prefix_length)
896 : name_(name),
897 description_(desc),
898 prefix_(prefix),
henrike@webrtc.orgf0488722014-05-13 18:00:26899 prefix_length_(prefix_length),
Guo-wei Shieh9af97f82015-11-10 22:47:39900 key_(MakeNetworkKey(name, prefix, prefix_length)),
901 scope_id_(0),
902 ignored_(false),
903 type_(ADAPTER_TYPE_UNKNOWN),
904 preference_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26905
Guo-wei Shieh9af97f82015-11-10 22:47:39906Network::Network(const std::string& name,
907 const std::string& desc,
908 const IPAddress& prefix,
909 int prefix_length,
910 AdapterType type)
911 : name_(name),
912 description_(desc),
913 prefix_(prefix),
henrike@webrtc.orgf0488722014-05-13 18:00:26914 prefix_length_(prefix_length),
Guo-wei Shieh9af97f82015-11-10 22:47:39915 key_(MakeNetworkKey(name, prefix, prefix_length)),
916 scope_id_(0),
917 ignored_(false),
918 type_(type),
919 preference_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26920
kwiberg@webrtc.org67186fe2015-03-09 22:21:53921Network::~Network() = default;
922
henrike@webrtc.orgf0488722014-05-13 18:00:26923// Sets the addresses of this network. Returns true if the address set changed.
924// Change detection is short circuited if the changed argument is true.
guoweis@webrtc.orgfa603982014-09-09 23:42:40925bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) {
henrike@webrtc.orgf0488722014-05-13 18:00:26926 // Detect changes with a nested loop; n-squared but we expect on the order
927 // of 2-3 addresses per network.
pthatcher@webrtc.org5950b642014-12-01 23:18:27928 changed = changed || ips.size() != ips_.size();
929 if (!changed) {
930 for (const InterfaceAddress& ip : ips) {
931 if (std::find(ips_.begin(), ips_.end(), ip) == ips_.end()) {
932 changed = true;
933 break;
henrike@webrtc.orgf0488722014-05-13 18:00:26934 }
935 }
henrike@webrtc.orgf0488722014-05-13 18:00:26936 }
pthatcher@webrtc.org5950b642014-12-01 23:18:27937
henrike@webrtc.orgf0488722014-05-13 18:00:26938 ips_ = ips;
939 return changed;
940}
941
aluebs@webrtc.org07dcf602015-02-27 18:42:22942// Select the best IP address to use from this Network.
guoweis@webrtc.org369a6372014-09-17 22:37:29943IPAddress Network::GetBestIP() const {
guoweis@webrtc.orgfa603982014-09-09 23:42:40944 if (ips_.size() == 0) {
945 return IPAddress();
946 }
guoweis@webrtc.org369a6372014-09-17 22:37:29947
948 if (prefix_.family() == AF_INET) {
949 return static_cast<IPAddress>(ips_.at(0));
950 }
951
aluebs@webrtc.org07dcf602015-02-27 18:42:22952 InterfaceAddress selected_ip, ula_ip;
953
pthatcher@webrtc.org5950b642014-12-01 23:18:27954 for (const InterfaceAddress& ip : ips_) {
aluebs@webrtc.org07dcf602015-02-27 18:42:22955 // Ignore any address which has been deprecated already.
956 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_DEPRECATED)
957 continue;
958
959 // ULA address should only be returned when we have no other
960 // global IP.
961 if (IPIsULA(static_cast<const IPAddress&>(ip))) {
962 ula_ip = ip;
guoweis@webrtc.org369a6372014-09-17 22:37:29963 continue;
964 }
aluebs@webrtc.org07dcf602015-02-27 18:42:22965 selected_ip = ip;
guoweis@webrtc.org369a6372014-09-17 22:37:29966
aluebs@webrtc.org07dcf602015-02-27 18:42:22967 // Search could stop once a temporary non-deprecated one is found.
968 if (ip.ipv6_flags() & IPV6_ADDRESS_FLAG_TEMPORARY)
969 break;
guoweis@webrtc.org369a6372014-09-17 22:37:29970 }
971
aluebs@webrtc.org07dcf602015-02-27 18:42:22972 // No proper global IPv6 address found, use ULA instead.
973 if (IPIsUnspec(selected_ip) && !IPIsUnspec(ula_ip)) {
974 selected_ip = ula_ip;
975 }
976
977 return static_cast<IPAddress>(selected_ip);
guoweis@webrtc.org369a6372014-09-17 22:37:29978}
979
980std::string Network::ToString() const {
981 std::stringstream ss;
982 // Print out the first space-terminated token of the network desc, plus
983 // the IP address.
984 ss << "Net[" << description_.substr(0, description_.find(' '))
985 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_
986 << ":" << AdapterTypeToString(type_) << "]";
987 return ss.str();
guoweis@webrtc.orgfa603982014-09-09 23:42:40988}
989
henrike@webrtc.orgf0488722014-05-13 18:00:26990} // namespace rtc