Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 // Any MAC based IPv6 should be avoided to prevent the MAC tracking. | 144 // Any MAC based IPv6 should be avoided to prevent the MAC tracking. |
| 145 if (IPIsMacBased(ip)) { | 145 if (IPIsMacBased(ip)) { |
| 146 return true; | 146 return true; |
| 147 } | 147 } |
| 148 | 148 |
| 149 return false; | 149 return false; |
| 150 } | 150 } |
| 151 | 151 |
| 152 } // namespace | 152 } // namespace |
| 153 | 153 |
| 154 // These addresses are used as the targets to find out the default local address | |
| 155 // on a multi-homed endpoint. They are actually DNS servers. | |
| 156 const char kPublicIPv4Host[] = "8.8.8.8"; | |
| 157 const char kPublicIPv6Host[] = "2001:4860:4860::8888"; | |
| 158 const int kPublicPort = 53; // DNS port. | |
| 159 | |
| 154 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix, | 160 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix, |
| 155 int prefix_length) { | 161 int prefix_length) { |
| 156 std::ostringstream ost; | 162 std::ostringstream ost; |
| 157 ost << name << "%" << prefix.ToString() << "/" << prefix_length; | 163 ost << name << "%" << prefix.ToString() << "/" << prefix_length; |
| 158 return ost.str(); | 164 return ost.str(); |
| 159 } | 165 } |
| 160 | 166 |
| 161 NetworkManager::NetworkManager() { | 167 NetworkManager::NetworkManager() { |
| 162 } | 168 } |
| 163 | 169 |
| 164 NetworkManager::~NetworkManager() { | 170 NetworkManager::~NetworkManager() { |
| 165 } | 171 } |
| 166 | 172 |
| 167 NetworkManager::EnumerationPermission NetworkManager::enumeration_permission() | 173 NetworkManager::EnumerationPermission NetworkManager::enumeration_permission() |
| 168 const { | 174 const { |
| 169 return ENUMERATION_ALLOWED; | 175 return ENUMERATION_ALLOWED; |
| 170 } | 176 } |
| 171 | 177 |
| 178 bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const { | |
| 179 return false; | |
| 180 } | |
| 181 | |
| 172 NetworkManagerBase::NetworkManagerBase() | 182 NetworkManagerBase::NetworkManagerBase() |
| 173 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED), | 183 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED), |
| 174 max_ipv6_networks_(kMaxIPv6Networks), | 184 max_ipv6_networks_(kMaxIPv6Networks), |
| 175 ipv6_enabled_(true) { | 185 ipv6_enabled_(true) { |
| 176 } | 186 } |
| 177 | 187 |
| 178 NetworkManagerBase::~NetworkManagerBase() { | 188 NetworkManagerBase::~NetworkManagerBase() { |
| 179 for (const auto& kv : networks_map_) { | 189 for (const auto& kv : networks_map_) { |
| 180 delete kv.second; | 190 delete kv.second; |
| 181 } | 191 } |
| 182 } | 192 } |
| 183 | 193 |
| 184 NetworkManager::EnumerationPermission | 194 NetworkManager::EnumerationPermission |
| 185 NetworkManagerBase::enumeration_permission() const { | 195 NetworkManagerBase::enumeration_permission() const { |
| 186 return enumeration_permission_; | 196 return enumeration_permission_; |
| 187 } | 197 } |
| 188 | 198 |
| 189 void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) { | 199 void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) { |
| 190 if (!ipv4_any_address_network_) { | 200 if (!ipv4_any_address_network_) { |
| 191 const rtc::IPAddress ipv4_any_address(INADDR_ANY); | 201 const rtc::IPAddress ipv4_any_address(INADDR_ANY); |
| 192 ipv4_any_address_network_.reset( | 202 ipv4_any_address_network_.reset( |
| 193 new rtc::Network("any", "any", ipv4_any_address, 0)); | 203 new rtc::Network("any", "any", ipv4_any_address, 0)); |
| 204 ipv4_any_address_network_->set_default_address_provider(this); | |
|
pthatcher1
2015/11/10 18:52:34
Why not just set the provider at the very beginnin
guoweis_webrtc
2015/11/10 20:37:29
Because chromium code calls the constructor as wel
| |
| 194 ipv4_any_address_network_->AddIP(ipv4_any_address); | 205 ipv4_any_address_network_->AddIP(ipv4_any_address); |
| 195 } | 206 } |
| 196 networks->push_back(ipv4_any_address_network_.get()); | 207 networks->push_back(ipv4_any_address_network_.get()); |
| 197 | 208 |
| 198 if (ipv6_enabled()) { | 209 if (ipv6_enabled()) { |
| 199 if (!ipv6_any_address_network_) { | 210 if (!ipv6_any_address_network_) { |
| 200 const rtc::IPAddress ipv6_any_address(in6addr_any); | 211 const rtc::IPAddress ipv6_any_address(in6addr_any); |
| 201 ipv6_any_address_network_.reset( | 212 ipv6_any_address_network_.reset( |
| 202 new rtc::Network("any", "any", ipv6_any_address, 0)); | 213 new rtc::Network("any", "any", ipv6_any_address, 0)); |
| 214 ipv6_any_address_network_->set_default_address_provider(this); | |
| 203 ipv6_any_address_network_->AddIP(ipv6_any_address); | 215 ipv6_any_address_network_->AddIP(ipv6_any_address); |
| 204 } | 216 } |
| 205 networks->push_back(ipv6_any_address_network_.get()); | 217 networks->push_back(ipv6_any_address_network_.get()); |
| 206 } | 218 } |
| 207 } | 219 } |
| 208 | 220 |
| 209 void NetworkManagerBase::GetNetworks(NetworkList* result) const { | 221 void NetworkManagerBase::GetNetworks(NetworkList* result) const { |
| 210 int ipv6_networks = 0; | 222 int ipv6_networks = 0; |
| 211 result->clear(); | 223 result->clear(); |
| 212 for (Network* network : networks_) { | 224 for (Network* network : networks_) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 if (pref > 0) { | 326 if (pref > 0) { |
| 315 --pref; | 327 --pref; |
| 316 } else { | 328 } else { |
| 317 LOG(LS_ERROR) << "Too many network interfaces to handle!"; | 329 LOG(LS_ERROR) << "Too many network interfaces to handle!"; |
| 318 break; | 330 break; |
| 319 } | 331 } |
| 320 } | 332 } |
| 321 } | 333 } |
| 322 } | 334 } |
| 323 | 335 |
| 336 void NetworkManagerBase::set_default_local_address(const IPAddress& ip) { | |
| 337 if (ip.family() == AF_INET) { | |
| 338 default_ipv4_address_ = ip; | |
| 339 } else if (ip.family() == AF_INET6) { | |
| 340 default_ipv6_address_ = ip; | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 bool NetworkManagerBase::GetDefaultLocalAddress(int family, | |
| 345 IPAddress* ipaddr) const { | |
| 346 if (family == AF_INET) { | |
| 347 *ipaddr = default_ipv4_address_; | |
| 348 return true; | |
| 349 } else if (family == AF_INET6) { | |
| 350 *ipaddr = default_ipv6_address_; | |
| 351 return true; | |
| 352 } | |
| 353 return false; | |
| 354 } | |
| 355 | |
| 324 BasicNetworkManager::BasicNetworkManager() | 356 BasicNetworkManager::BasicNetworkManager() |
| 325 : thread_(NULL), sent_first_update_(false), start_count_(0), | 357 : thread_(NULL), sent_first_update_(false), start_count_(0), |
| 326 network_ignore_mask_(kDefaultNetworkIgnoreMask), | 358 network_ignore_mask_(kDefaultNetworkIgnoreMask), |
| 327 ignore_non_default_routes_(false) { | 359 ignore_non_default_routes_(false) { |
| 328 } | 360 } |
| 329 | 361 |
| 330 BasicNetworkManager::~BasicNetworkManager() { | 362 BasicNetworkManager::~BasicNetworkManager() { |
| 331 } | 363 } |
| 332 | 364 |
| 333 void BasicNetworkManager::OnNetworksChanged() { | 365 void BasicNetworkManager::OnNetworksChanged() { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 adapter_type = ADAPTER_TYPE_LOOPBACK; | 434 adapter_type = ADAPTER_TYPE_LOOPBACK; |
| 403 } | 435 } |
| 404 #if defined(WEBRTC_IOS) | 436 #if defined(WEBRTC_IOS) |
| 405 // Cell networks are pdp_ipN on iOS. | 437 // Cell networks are pdp_ipN on iOS. |
| 406 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { | 438 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { |
| 407 adapter_type = ADAPTER_TYPE_CELLULAR; | 439 adapter_type = ADAPTER_TYPE_CELLULAR; |
| 408 } | 440 } |
| 409 #endif | 441 #endif |
| 410 // TODO(phoglund): Need to recognize other types as well. | 442 // TODO(phoglund): Need to recognize other types as well. |
| 411 scoped_ptr<Network> network(new Network(cursor->ifa_name, | 443 scoped_ptr<Network> network(new Network(cursor->ifa_name, |
| 412 cursor->ifa_name, | 444 cursor->ifa_name, prefix, |
| 413 prefix, | 445 prefix_length, adapter_type)); |
| 414 prefix_length, | 446 network->set_default_address_provider(this); |
| 415 adapter_type)); | |
| 416 network->set_scope_id(scope_id); | 447 network->set_scope_id(scope_id); |
| 417 network->AddIP(ip); | 448 network->AddIP(ip); |
| 418 network->set_ignored(IsIgnoredNetwork(*network)); | 449 network->set_ignored(IsIgnoredNetwork(*network)); |
| 419 if (include_ignored || !network->ignored()) { | 450 if (include_ignored || !network->ignored()) { |
| 420 networks->push_back(network.release()); | 451 networks->push_back(network.release()); |
| 421 } | 452 } |
| 422 } else { | 453 } else { |
| 423 (*existing_network).second->AddIP(ip); | 454 (*existing_network).second->AddIP(ip); |
| 424 } | 455 } |
| 425 } | 456 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 IPAddress prefix; | 587 IPAddress prefix; |
| 557 int prefix_length = GetPrefix(prefixlist, ip, &prefix); | 588 int prefix_length = GetPrefix(prefixlist, ip, &prefix); |
| 558 std::string key = MakeNetworkKey(name, prefix, prefix_length); | 589 std::string key = MakeNetworkKey(name, prefix, prefix_length); |
| 559 auto existing_network = current_networks.find(key); | 590 auto existing_network = current_networks.find(key); |
| 560 if (existing_network == current_networks.end()) { | 591 if (existing_network == current_networks.end()) { |
| 561 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; | 592 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
| 562 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | 593 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { |
| 563 // TODO(phoglund): Need to recognize other types as well. | 594 // TODO(phoglund): Need to recognize other types as well. |
| 564 adapter_type = ADAPTER_TYPE_LOOPBACK; | 595 adapter_type = ADAPTER_TYPE_LOOPBACK; |
| 565 } | 596 } |
| 566 scoped_ptr<Network> network(new Network(name, | 597 scoped_ptr<Network> network(new Network(name, description, prefix, |
| 567 description, | 598 prefix_length, adapter_type)); |
| 568 prefix, | 599 network->set_default_address_provider(this); |
| 569 prefix_length, | |
| 570 adapter_type)); | |
| 571 network->set_scope_id(scope_id); | 600 network->set_scope_id(scope_id); |
| 572 network->AddIP(ip); | 601 network->AddIP(ip); |
| 573 bool ignored = IsIgnoredNetwork(*network); | 602 bool ignored = IsIgnoredNetwork(*network); |
| 574 network->set_ignored(ignored); | 603 network->set_ignored(ignored); |
| 575 if (include_ignored || !network->ignored()) { | 604 if (include_ignored || !network->ignored()) { |
| 576 networks->push_back(network.release()); | 605 networks->push_back(network.release()); |
| 577 } | 606 } |
| 578 } else { | 607 } else { |
| 579 (*existing_network).second->AddIP(ip); | 608 (*existing_network).second->AddIP(ip); |
| 580 } | 609 } |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 } | 746 } |
| 718 case kSignalNetworksMessage: { | 747 case kSignalNetworksMessage: { |
| 719 SignalNetworksChanged(); | 748 SignalNetworksChanged(); |
| 720 break; | 749 break; |
| 721 } | 750 } |
| 722 default: | 751 default: |
| 723 ASSERT(false); | 752 ASSERT(false); |
| 724 } | 753 } |
| 725 } | 754 } |
| 726 | 755 |
| 756 IPAddress BasicNetworkManager::QueryDefaultAddress(int family) const { | |
| 757 ASSERT(thread_ == Thread::Current()); | |
| 758 ASSERT(thread_->socketserver() != nullptr); | |
| 759 ASSERT(family == AF_INET || family == AF_INET6); | |
| 760 | |
| 761 scoped_ptr<AsyncSocket> socket( | |
| 762 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); | |
| 763 if (socket && | |
| 764 socket->Connect( | |
| 765 SocketAddress(family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, | |
| 766 kPublicPort)) == 0) { | |
| 767 return socket->GetLocalAddress().ipaddr(); | |
| 768 } | |
|
pthatcher1
2015/11/10 18:52:34
It might be more clear as:
if (!socket) {
retur
guoweis_webrtc
2015/11/10 20:37:29
Done.
| |
| 769 | |
| 770 return IPAddress(); | |
| 771 } | |
| 772 | |
| 727 void BasicNetworkManager::UpdateNetworksOnce() { | 773 void BasicNetworkManager::UpdateNetworksOnce() { |
| 728 if (!start_count_) | 774 if (!start_count_) |
| 729 return; | 775 return; |
| 730 | 776 |
| 731 ASSERT(Thread::Current() == thread_); | 777 ASSERT(Thread::Current() == thread_); |
| 732 | 778 |
| 733 NetworkList list; | 779 NetworkList list; |
| 734 if (!CreateNetworks(false, &list)) { | 780 if (!CreateNetworks(false, &list)) { |
| 735 SignalError(); | 781 SignalError(); |
| 736 } else { | 782 } else { |
| 737 bool changed; | 783 bool changed; |
| 738 MergeNetworkList(list, &changed); | 784 NetworkManager::Stats stats; |
| 785 MergeNetworkList(list, &changed, &stats); | |
| 786 if (stats.ipv6_network_count) { | |
| 787 set_default_local_address(QueryDefaultAddress(AF_INET6)); | |
|
pthatcher1
2015/11/10 18:52:34
Why bother checking the stats? Why not just query
guoweis_webrtc
2015/11/10 20:37:29
If there is no ipv6 address at all, there is no po
| |
| 788 } | |
| 789 if (stats.ipv4_network_count) { | |
| 790 set_default_local_address(QueryDefaultAddress(AF_INET)); | |
| 791 } | |
| 739 if (changed || !sent_first_update_) { | 792 if (changed || !sent_first_update_) { |
| 740 SignalNetworksChanged(); | 793 SignalNetworksChanged(); |
| 741 sent_first_update_ = true; | 794 sent_first_update_ = true; |
| 742 } | 795 } |
| 743 } | 796 } |
| 744 } | 797 } |
| 745 | 798 |
| 746 void BasicNetworkManager::UpdateNetworksContinually() { | 799 void BasicNetworkManager::UpdateNetworksContinually() { |
| 747 UpdateNetworksOnce(); | 800 UpdateNetworksOnce(); |
| 748 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage); | 801 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 759 << ((network->ignored()) ? ", Ignored" : ""); | 812 << ((network->ignored()) ? ", Ignored" : ""); |
| 760 } | 813 } |
| 761 } | 814 } |
| 762 // Release the network list created previously. | 815 // Release the network list created previously. |
| 763 // Do this in a seperated for loop for better readability. | 816 // Do this in a seperated for loop for better readability. |
| 764 for (Network* network : list) { | 817 for (Network* network : list) { |
| 765 delete network; | 818 delete network; |
| 766 } | 819 } |
| 767 } | 820 } |
| 768 | 821 |
| 769 Network::Network(const std::string& name, const std::string& desc, | 822 Network::Network(const std::string& name, |
| 770 const IPAddress& prefix, int prefix_length) | 823 const std::string& desc, |
| 771 : name_(name), description_(desc), prefix_(prefix), | 824 const IPAddress& prefix, |
| 825 int prefix_length) | |
| 826 : name_(name), | |
| 827 description_(desc), | |
| 828 prefix_(prefix), | |
| 772 prefix_length_(prefix_length), | 829 prefix_length_(prefix_length), |
| 773 key_(MakeNetworkKey(name, prefix, prefix_length)), scope_id_(0), | 830 key_(MakeNetworkKey(name, prefix, prefix_length)), |
| 774 ignored_(false), type_(ADAPTER_TYPE_UNKNOWN), preference_(0) { | 831 scope_id_(0), |
| 775 } | 832 ignored_(false), |
| 833 type_(ADAPTER_TYPE_UNKNOWN), | |
| 834 preference_(0) {} | |
| 776 | 835 |
| 777 Network::Network(const std::string& name, const std::string& desc, | 836 Network::Network(const std::string& name, |
| 778 const IPAddress& prefix, int prefix_length, AdapterType type) | 837 const std::string& desc, |
| 779 : name_(name), description_(desc), prefix_(prefix), | 838 const IPAddress& prefix, |
| 839 int prefix_length, | |
| 840 AdapterType type) | |
| 841 : name_(name), | |
| 842 description_(desc), | |
| 843 prefix_(prefix), | |
| 780 prefix_length_(prefix_length), | 844 prefix_length_(prefix_length), |
| 781 key_(MakeNetworkKey(name, prefix, prefix_length)), scope_id_(0), | 845 key_(MakeNetworkKey(name, prefix, prefix_length)), |
| 782 ignored_(false), type_(type), preference_(0) { | 846 scope_id_(0), |
| 783 } | 847 ignored_(false), |
| 848 type_(type), | |
| 849 preference_(0) {} | |
| 784 | 850 |
| 785 Network::~Network() = default; | 851 Network::~Network() = default; |
| 786 | 852 |
| 787 // Sets the addresses of this network. Returns true if the address set changed. | 853 // Sets the addresses of this network. Returns true if the address set changed. |
| 788 // Change detection is short circuited if the changed argument is true. | 854 // Change detection is short circuited if the changed argument is true. |
| 789 bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) { | 855 bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) { |
| 790 // Detect changes with a nested loop; n-squared but we expect on the order | 856 // Detect changes with a nested loop; n-squared but we expect on the order |
| 791 // of 2-3 addresses per network. | 857 // of 2-3 addresses per network. |
| 792 changed = changed || ips.size() != ips_.size(); | 858 changed = changed || ips.size() != ips_.size(); |
| 793 if (!changed) { | 859 if (!changed) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 845 std::stringstream ss; | 911 std::stringstream ss; |
| 846 // Print out the first space-terminated token of the network desc, plus | 912 // Print out the first space-terminated token of the network desc, plus |
| 847 // the IP address. | 913 // the IP address. |
| 848 ss << "Net[" << description_.substr(0, description_.find(' ')) | 914 ss << "Net[" << description_.substr(0, description_.find(' ')) |
| 849 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ | 915 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ |
| 850 << ":" << AdapterTypeToString(type_) << "]"; | 916 << ":" << AdapterTypeToString(type_) << "]"; |
| 851 return ss.str(); | 917 return ss.str(); |
| 852 } | 918 } |
| 853 | 919 |
| 854 } // namespace rtc | 920 } // namespace rtc |
| OLD | NEW |