Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: webrtc/base/network.cc

Issue 1411253008: WebRTC should generate default private address even when adapter enumeration is disabled. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: change the signature of GetDefaultPrivateAddress Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 char kPublicIPv4Host[] = "8.8.8.8";
juberti 2015/10/28 23:57:40 const char. This also needs explanatory comments.
guoweis_webrtc 2015/10/29 03:41:00 Done.
155 char kPublicIPv6Host[] = "2001:4860:4860::8888";
156
154 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix, 157 std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
155 int prefix_length) { 158 int prefix_length) {
156 std::ostringstream ost; 159 std::ostringstream ost;
157 ost << name << "%" << prefix.ToString() << "/" << prefix_length; 160 ost << name << "%" << prefix.ToString() << "/" << prefix_length;
158 return ost.str(); 161 return ost.str();
159 } 162 }
160 163
161 NetworkManager::NetworkManager() { 164 NetworkManager::NetworkManager() {
162 } 165 }
163 166
164 NetworkManager::~NetworkManager() { 167 NetworkManager::~NetworkManager() {
165 } 168 }
166 169
167 NetworkManager::EnumerationPermission NetworkManager::enumeration_permission() 170 NetworkManager::EnumerationPermission NetworkManager::enumeration_permission()
168 const { 171 const {
169 return ENUMERATION_ALLOWED; 172 return ENUMERATION_ALLOWED;
170 } 173 }
171 174
175 bool NetworkManager::GetDefaultPrivateAddress(int family,
176 IPAddress* addr) const {
177 return false;
178 }
179
172 NetworkManagerBase::NetworkManagerBase() 180 NetworkManagerBase::NetworkManagerBase()
173 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED), 181 : enumeration_permission_(NetworkManager::ENUMERATION_ALLOWED),
174 max_ipv6_networks_(kMaxIPv6Networks), 182 max_ipv6_networks_(kMaxIPv6Networks),
175 ipv6_enabled_(true) { 183 ipv6_enabled_(true) {
176 } 184 }
177 185
178 NetworkManagerBase::~NetworkManagerBase() { 186 NetworkManagerBase::~NetworkManagerBase() {
179 for (const auto& kv : networks_map_) { 187 for (const auto& kv : networks_map_) {
180 delete kv.second; 188 delete kv.second;
181 } 189 }
182 } 190 }
183 191
184 NetworkManager::EnumerationPermission 192 NetworkManager::EnumerationPermission
185 NetworkManagerBase::enumeration_permission() const { 193 NetworkManagerBase::enumeration_permission() const {
186 return enumeration_permission_; 194 return enumeration_permission_;
187 } 195 }
188 196
189 void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) { 197 void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) {
190 if (!ipv4_any_address_network_) { 198 if (!ipv4_any_address_network_) {
191 const rtc::IPAddress ipv4_any_address(INADDR_ANY); 199 const rtc::IPAddress ipv4_any_address(INADDR_ANY);
192 ipv4_any_address_network_.reset( 200 ipv4_any_address_network_.reset(
193 new rtc::Network("any", "any", ipv4_any_address, 0)); 201 new rtc::Network("any", "any", ipv4_any_address, 0));
202 ipv4_any_address_network_->set_default_address_provider(this);
194 ipv4_any_address_network_->AddIP(ipv4_any_address); 203 ipv4_any_address_network_->AddIP(ipv4_any_address);
195 } 204 }
196 networks->push_back(ipv4_any_address_network_.get()); 205 networks->push_back(ipv4_any_address_network_.get());
197 206
198 if (ipv6_enabled()) { 207 if (ipv6_enabled()) {
199 if (!ipv6_any_address_network_) { 208 if (!ipv6_any_address_network_) {
200 const rtc::IPAddress ipv6_any_address(in6addr_any); 209 const rtc::IPAddress ipv6_any_address(in6addr_any);
201 ipv6_any_address_network_.reset( 210 ipv6_any_address_network_.reset(
202 new rtc::Network("any", "any", ipv6_any_address, 0)); 211 new rtc::Network("any", "any", ipv6_any_address, 0));
212 ipv6_any_address_network_->set_default_address_provider(this);
203 ipv6_any_address_network_->AddIP(ipv6_any_address); 213 ipv6_any_address_network_->AddIP(ipv6_any_address);
204 } 214 }
205 networks->push_back(ipv6_any_address_network_.get()); 215 networks->push_back(ipv6_any_address_network_.get());
206 } 216 }
207 } 217 }
208 218
209 void NetworkManagerBase::GetNetworks(NetworkList* result) const { 219 void NetworkManagerBase::GetNetworks(NetworkList* result) const {
210 int ipv6_networks = 0; 220 int ipv6_networks = 0;
211 result->clear(); 221 result->clear();
212 for (Network* network : networks_) { 222 for (Network* network : networks_) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 if (pref > 0) { 324 if (pref > 0) {
315 --pref; 325 --pref;
316 } else { 326 } else {
317 LOG(LS_ERROR) << "Too many network interfaces to handle!"; 327 LOG(LS_ERROR) << "Too many network interfaces to handle!";
318 break; 328 break;
319 } 329 }
320 } 330 }
321 } 331 }
322 } 332 }
323 333
334 void NetworkManagerBase::set_default_private_address(const IPAddress& ip) {
335 if (ip.family() == AF_INET) {
336 default_ipv4_address_ = ip;
337 } else if (ip.family() == AF_INET6) {
338 default_ipv6_address_ = ip;
339 }
340 }
341
342 bool NetworkManagerBase::GetDefaultPrivateAddress(int family,
343 IPAddress* ipaddr) const {
344 if (family == AF_INET) {
345 *ipaddr = default_ipv4_address_;
346 return true;
347 } else if (family == AF_INET6) {
348 *ipaddr = default_ipv6_address_;
349 return true;
350 }
351 return false;
352 }
353
324 BasicNetworkManager::BasicNetworkManager() 354 BasicNetworkManager::BasicNetworkManager()
325 : thread_(NULL), sent_first_update_(false), start_count_(0), 355 : thread_(NULL), sent_first_update_(false), start_count_(0),
326 network_ignore_mask_(kDefaultNetworkIgnoreMask), 356 network_ignore_mask_(kDefaultNetworkIgnoreMask),
327 ignore_non_default_routes_(false) { 357 ignore_non_default_routes_(false) {
328 } 358 }
329 359
330 BasicNetworkManager::~BasicNetworkManager() { 360 BasicNetworkManager::~BasicNetworkManager() {
331 } 361 }
332 362
333 void BasicNetworkManager::OnNetworksChanged() { 363 void BasicNetworkManager::OnNetworksChanged() {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 adapter_type = ADAPTER_TYPE_LOOPBACK; 432 adapter_type = ADAPTER_TYPE_LOOPBACK;
403 } 433 }
404 #if defined(WEBRTC_IOS) 434 #if defined(WEBRTC_IOS)
405 // Cell networks are pdp_ipN on iOS. 435 // Cell networks are pdp_ipN on iOS.
406 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) { 436 if (strncmp(cursor->ifa_name, "pdp_ip", 6) == 0) {
407 adapter_type = ADAPTER_TYPE_CELLULAR; 437 adapter_type = ADAPTER_TYPE_CELLULAR;
408 } 438 }
409 #endif 439 #endif
410 // TODO(phoglund): Need to recognize other types as well. 440 // TODO(phoglund): Need to recognize other types as well.
411 scoped_ptr<Network> network(new Network(cursor->ifa_name, 441 scoped_ptr<Network> network(new Network(cursor->ifa_name,
412 cursor->ifa_name, 442 cursor->ifa_name, prefix,
413 prefix, 443 prefix_length, adapter_type));
414 prefix_length, 444 network->set_default_address_provider(this);
415 adapter_type));
416 network->set_scope_id(scope_id); 445 network->set_scope_id(scope_id);
417 network->AddIP(ip); 446 network->AddIP(ip);
418 network->set_ignored(IsIgnoredNetwork(*network)); 447 network->set_ignored(IsIgnoredNetwork(*network));
419 if (include_ignored || !network->ignored()) { 448 if (include_ignored || !network->ignored()) {
420 networks->push_back(network.release()); 449 networks->push_back(network.release());
421 } 450 }
422 } else { 451 } else {
423 (*existing_network).second->AddIP(ip); 452 (*existing_network).second->AddIP(ip);
424 } 453 }
425 } 454 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 IPAddress prefix; 585 IPAddress prefix;
557 int prefix_length = GetPrefix(prefixlist, ip, &prefix); 586 int prefix_length = GetPrefix(prefixlist, ip, &prefix);
558 std::string key = MakeNetworkKey(name, prefix, prefix_length); 587 std::string key = MakeNetworkKey(name, prefix, prefix_length);
559 auto existing_network = current_networks.find(key); 588 auto existing_network = current_networks.find(key);
560 if (existing_network == current_networks.end()) { 589 if (existing_network == current_networks.end()) {
561 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN; 590 AdapterType adapter_type = ADAPTER_TYPE_UNKNOWN;
562 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { 591 if (adapter_addrs->IfType == IF_TYPE_SOFTWARE_LOOPBACK) {
563 // TODO(phoglund): Need to recognize other types as well. 592 // TODO(phoglund): Need to recognize other types as well.
564 adapter_type = ADAPTER_TYPE_LOOPBACK; 593 adapter_type = ADAPTER_TYPE_LOOPBACK;
565 } 594 }
566 scoped_ptr<Network> network(new Network(name, 595 scoped_ptr<Network> network(new Network(name, description, prefix,
567 description, 596 prefix_length, adapter_type));
568 prefix, 597 network->set_default_address_provider(this);
569 prefix_length,
570 adapter_type));
571 network->set_scope_id(scope_id); 598 network->set_scope_id(scope_id);
572 network->AddIP(ip); 599 network->AddIP(ip);
573 bool ignored = IsIgnoredNetwork(*network); 600 bool ignored = IsIgnoredNetwork(*network);
574 network->set_ignored(ignored); 601 network->set_ignored(ignored);
575 if (include_ignored || !network->ignored()) { 602 if (include_ignored || !network->ignored()) {
576 networks->push_back(network.release()); 603 networks->push_back(network.release());
577 } 604 }
578 } else { 605 } else {
579 (*existing_network).second->AddIP(ip); 606 (*existing_network).second->AddIP(ip);
580 } 607 }
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 } 744 }
718 case kSignalNetworksMessage: { 745 case kSignalNetworksMessage: {
719 SignalNetworksChanged(); 746 SignalNetworksChanged();
720 break; 747 break;
721 } 748 }
722 default: 749 default:
723 ASSERT(false); 750 ASSERT(false);
724 } 751 }
725 } 752 }
726 753
754 IPAddress BasicNetworkManager::UpdateDefaultPrivateAddress(int family) {
juberti 2015/10/28 23:57:40 This isn't really an Update, since it doesn't chan
guoweis_webrtc 2015/10/29 03:41:00 renamed as QueryDefaultAddress
755 ASSERT(thread_ == Thread::Current());
756 ASSERT(thread_->socketserver() != nullptr);
757 ASSERT(family == AF_INET || family == AF_INET6);
758
759 scoped_ptr<AsyncSocket> socket(
760 thread_->socketserver()->CreateAsyncSocket(family, SOCK_DGRAM));
761 if (socket && socket->Bind(SocketAddress(GetWildcardIP(family), 0)) == 0 &&
762 socket->Connect(SocketAddress(
763 family == AF_INET ? kPublicIPv4Host : kPublicIPv6Host, 3478)) == 0) {
juberti 2015/10/28 23:57:40 3478 should be a constant as well. I would pick th
guoweis_webrtc 2015/10/29 03:41:00 Done.
764 return socket->GetLocalAddress().ipaddr();
juberti 2015/10/28 23:57:40 Not sure this will work in Chromium, where GetLoca
guoweis_webrtc 2015/10/29 03:41:00 Not an issue since IpcNetworkManager will be used
765 }
766
767 return IPAddress();
768 }
769
727 void BasicNetworkManager::UpdateNetworksOnce() { 770 void BasicNetworkManager::UpdateNetworksOnce() {
728 if (!start_count_) 771 if (!start_count_)
729 return; 772 return;
730 773
731 ASSERT(Thread::Current() == thread_); 774 ASSERT(Thread::Current() == thread_);
732 775
733 NetworkList list; 776 NetworkList list;
734 if (!CreateNetworks(false, &list)) { 777 if (!CreateNetworks(false, &list)) {
735 SignalError(); 778 SignalError();
736 } else { 779 } else {
737 bool changed; 780 bool changed;
738 MergeNetworkList(list, &changed); 781 NetworkManager::Stats stats;
782 MergeNetworkList(list, &changed, &stats);
783 if (stats.ipv6_network_count) {
784 set_default_private_address(UpdateDefaultPrivateAddress(AF_INET6));
785 }
786 if (stats.ipv4_network_count) {
787 set_default_private_address(UpdateDefaultPrivateAddress(AF_INET));
788 }
739 if (changed || !sent_first_update_) { 789 if (changed || !sent_first_update_) {
740 SignalNetworksChanged(); 790 SignalNetworksChanged();
741 sent_first_update_ = true; 791 sent_first_update_ = true;
742 } 792 }
743 } 793 }
744 } 794 }
745 795
746 void BasicNetworkManager::UpdateNetworksContinually() { 796 void BasicNetworkManager::UpdateNetworksContinually() {
747 UpdateNetworksOnce(); 797 UpdateNetworksOnce();
748 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage); 798 thread_->PostDelayed(kNetworksUpdateIntervalMs, this, kUpdateNetworksMessage);
(...skipping 10 matching lines...) Expand all
759 << ((network->ignored()) ? ", Ignored" : ""); 809 << ((network->ignored()) ? ", Ignored" : "");
760 } 810 }
761 } 811 }
762 // Release the network list created previously. 812 // Release the network list created previously.
763 // Do this in a seperated for loop for better readability. 813 // Do this in a seperated for loop for better readability.
764 for (Network* network : list) { 814 for (Network* network : list) {
765 delete network; 815 delete network;
766 } 816 }
767 } 817 }
768 818
769 Network::Network(const std::string& name, const std::string& desc, 819 Network::Network(const std::string& name,
770 const IPAddress& prefix, int prefix_length) 820 const std::string& desc,
771 : name_(name), description_(desc), prefix_(prefix), 821 const IPAddress& prefix,
822 int prefix_length)
823 : name_(name),
824 description_(desc),
825 prefix_(prefix),
772 prefix_length_(prefix_length), 826 prefix_length_(prefix_length),
773 key_(MakeNetworkKey(name, prefix, prefix_length)), scope_id_(0), 827 key_(MakeNetworkKey(name, prefix, prefix_length)),
774 ignored_(false), type_(ADAPTER_TYPE_UNKNOWN), preference_(0) { 828 scope_id_(0),
775 } 829 ignored_(false),
830 type_(ADAPTER_TYPE_UNKNOWN),
831 preference_(0) {}
776 832
777 Network::Network(const std::string& name, const std::string& desc, 833 Network::Network(const std::string& name,
778 const IPAddress& prefix, int prefix_length, AdapterType type) 834 const std::string& desc,
779 : name_(name), description_(desc), prefix_(prefix), 835 const IPAddress& prefix,
836 int prefix_length,
837 AdapterType type)
838 : name_(name),
839 description_(desc),
840 prefix_(prefix),
780 prefix_length_(prefix_length), 841 prefix_length_(prefix_length),
781 key_(MakeNetworkKey(name, prefix, prefix_length)), scope_id_(0), 842 key_(MakeNetworkKey(name, prefix, prefix_length)),
782 ignored_(false), type_(type), preference_(0) { 843 scope_id_(0),
783 } 844 ignored_(false),
845 type_(type),
846 preference_(0) {}
784 847
785 Network::~Network() = default; 848 Network::~Network() = default;
786 849
787 // Sets the addresses of this network. Returns true if the address set changed. 850 // 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. 851 // Change detection is short circuited if the changed argument is true.
789 bool Network::SetIPs(const std::vector<InterfaceAddress>& ips, bool changed) { 852 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 853 // Detect changes with a nested loop; n-squared but we expect on the order
791 // of 2-3 addresses per network. 854 // of 2-3 addresses per network.
792 changed = changed || ips.size() != ips_.size(); 855 changed = changed || ips.size() != ips_.size();
793 if (!changed) { 856 if (!changed) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
845 std::stringstream ss; 908 std::stringstream ss;
846 // Print out the first space-terminated token of the network desc, plus 909 // Print out the first space-terminated token of the network desc, plus
847 // the IP address. 910 // the IP address.
848 ss << "Net[" << description_.substr(0, description_.find(' ')) 911 ss << "Net[" << description_.substr(0, description_.find(' '))
849 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_ 912 << ":" << prefix_.ToSensitiveString() << "/" << prefix_length_
850 << ":" << AdapterTypeToString(type_) << "]"; 913 << ":" << AdapterTypeToString(type_) << "]";
851 return ss.str(); 914 return ss.str();
852 } 915 }
853 916
854 } // namespace rtc 917 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698