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_local_address_provider(this); |
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_local_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_addresses(const IPAddress& ipv4, |
| 337 const IPAddress& ipv6) { |
| 338 if (ipv4.family() == AF_INET) { |
| 339 default_local_ipv4_address_ = ipv4; |
| 340 } |
| 341 if (ipv6.family() == AF_INET6) { |
| 342 default_local_ipv6_address_ = ipv6; |
| 343 } |
| 344 } |
| 345 |
| 346 bool NetworkManagerBase::GetDefaultLocalAddress(int family, |
| 347 IPAddress* ipaddr) const { |
| 348 if (family == AF_INET) { |
| 349 *ipaddr = default_local_ipv4_address_; |
| 350 return true; |
| 351 } else if (family == AF_INET6) { |
| 352 *ipaddr = default_local_ipv6_address_; |
| 353 return true; |
| 354 } |
| 355 return false; |
| 356 } |
| 357 |
324 BasicNetworkManager::BasicNetworkManager() | 358 BasicNetworkManager::BasicNetworkManager() |
325 : thread_(NULL), sent_first_update_(false), start_count_(0), | 359 : thread_(NULL), sent_first_update_(false), start_count_(0), |
326 network_ignore_mask_(kDefaultNetworkIgnoreMask), | 360 network_ignore_mask_(kDefaultNetworkIgnoreMask), |
327 ignore_non_default_routes_(false) { | 361 ignore_non_default_routes_(false) { |
328 } | 362 } |
329 | 363 |
330 BasicNetworkManager::~BasicNetworkManager() { | 364 BasicNetworkManager::~BasicNetworkManager() { |
331 } | 365 } |
332 | 366 |
333 void BasicNetworkManager::OnNetworksChanged() { | 367 void BasicNetworkManager::OnNetworksChanged() { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 adapter_type = ADAPTER_TYPE_LOOPBACK; | 436 adapter_type = ADAPTER_TYPE_LOOPBACK; |
403 } | 437 } |
404 #if defined(WEBRTC_IOS) | 438 #if defined(WEBRTC_IOS) |
405 // Cell networks are pdp_ipN on iOS. | 439 // Cell networks are pdp_ipN on iOS. |
406 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { | 440 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { |
407 adapter_type = ADAPTER_TYPE_CELLULAR; | 441 adapter_type = ADAPTER_TYPE_CELLULAR; |
408 } | 442 } |
409 #endif | 443 #endif |
410 // TODO(phoglund): Need to recognize other types as well. | 444 // TODO(phoglund): Need to recognize other types as well. |
411 scoped_ptr<Network> network(new Network(cursor->ifa_name, | 445 scoped_ptr<Network> network(new Network(cursor->ifa_name, |
412 cursor->ifa_name, | 446 cursor->ifa_name, prefix, |
413 prefix, | 447 prefix_length, adapter_type)); |
414 prefix_length, | 448 network->set_default_local_address_provider(this); |
415 adapter_type)); | |
416 network->set_scope_id(scope_id); | 449 network->set_scope_id(scope_id); |
417 network->AddIP(ip); | 450 network->AddIP(ip); |
418 network->set_ignored(IsIgnoredNetwork(*network)); | 451 network->set_ignored(IsIgnoredNetwork(*network)); |
419 if (include_ignored || !network->ignored()) { | 452 if (include_ignored || !network->ignored()) { |
420 networks->push_back(network.release()); | 453 networks->push_back(network.release()); |
421 } | 454 } |
422 } else { | 455 } else { |
423 (*existing_network).second->AddIP(ip); | 456 (*existing_network).second->AddIP(ip); |
424 } | 457 } |
425 } | 458 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 IPAddress prefix; | 589 IPAddress prefix; |
557 int prefix_length = GetPrefix(prefixlist, ip, &prefix); | 590 int prefix_length = GetPrefix(prefixlist, ip, &prefix); |
558 std::string key = MakeNetworkKey(name, prefix, prefix_length); | 591 std::string key = MakeNetworkKey(name, prefix, prefix_length); |
559 auto existing_network = current_networks.find(key); | 592 auto existing_network = current_networks.find(key); |
560 if (existing_network == current_networks.end()) { | 593 if (existing_network == current_networks.end()) { |
561 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; | 594 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; |
562 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | 595 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { |
563 // TODO(phoglund): Need to recognize other types as well. | 596 // TODO(phoglund): Need to recognize other types as well. |
564 adapter_type = ADAPTER_TYPE_LOOPBACK; | 597 adapter_type = ADAPTER_TYPE_LOOPBACK; |
565 } | 598 } |
566 scoped_ptr<Network> network(new Network(name, | 599 scoped_ptr<Network> network(new Network(name, description, prefix, |
567 description, | 600 prefix_length, adapter_type)); |
568 prefix, | 601 network->set_default_local_address_provider(this); |
569 prefix_length, | |
570 adapter_type)); | |
571 network->set_scope_id(scope_id); | 602 network->set_scope_id(scope_id); |
572 network->AddIP(ip); | 603 network->AddIP(ip); |
573 bool ignored = IsIgnoredNetwork(*network); | 604 bool ignored = IsIgnoredNetwork(*network); |
574 network->set_ignored(ignored); | 605 network->set_ignored(ignored); |
575 if (include_ignored || !network->ignored()) { | 606 if (include_ignored || !network->ignored()) { |
576 networks->push_back(network.release()); | 607 networks->push_back(network.release()); |
577 } | 608 } |
578 } else { | 609 } else { |
579 (*existing_network).second->AddIP(ip); | 610 (*existing_network).second->AddIP(ip); |
580 } | 611 } |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 } | 748 } |
718 case kSignalNetworksMessage: { | 749 case kSignalNetworksMessage: { |
719 SignalNetworksChanged(); | 750 SignalNetworksChanged(); |
720 break; | 751 break; |
721 } | 752 } |
722 default: | 753 default: |
723 ASSERT(false); | 754 ASSERT(false); |
724 } | 755 } |
725 } | 756 } |
726 | 757 |
| 758 IPAddress BasicNetworkManager::QueryDefaultLocalAddress(int family) const { |
| 759 ASSERT(thread_ == Thread::Current()); |
| 760 ASSERT(thread_->socketserver() != nullptr); |
| 761 ASSERT(family == AF_INET || family == AF_INET6); |
| 762 |
| 763 scoped_ptr<AsyncSocket> socket( |
| 764 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM)); |
| 765 if (!socket) { |
| 766 return IPAddress(); |
| 767 } |
| 768 |
| 769 if (!socket->Connect( |
| 770 SocketAddress(family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, |
| 771 kPublicPort))) { |
| 772 return IPAddress(); |
| 773 } |
| 774 return socket->GetLocalAddress().ipaddr(); |
| 775 } |
| 776 |
727 void BasicNetworkManager::UpdateNetworksOnce() { | 777 void BasicNetworkManager::UpdateNetworksOnce() { |
728 if (!start_count_) | 778 if (!start_count_) |
729 return; | 779 return; |
730 | 780 |
731 ASSERT(Thread::Current() == thread_); | 781 ASSERT(Thread::Current() == thread_); |
732 | 782 |
733 NetworkList list; | 783 NetworkList list; |
734 if (!CreateNetworks(false, &list)) { | 784 if (!CreateNetworks(false, &list)) { |
735 SignalError(); | 785 SignalError(); |
736 } else { | 786 } else { |
737 bool changed; | 787 bool changed; |
738 MergeNetworkList(list, &changed); | 788 NetworkManager::Stats stats; |
| 789 MergeNetworkList(list, &changed, &stats); |
| 790 set_default_local_addresses(QueryDefaultLocalAddress(AF_INET), |
| 791 QueryDefaultLocalAddress(AF_INET6)); |
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 |