Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/p2p/ipc_network_manager.h" | 5 #include "content/renderer/p2p/ipc_network_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
| 14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 15 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
| 16 #include "net/base/ip_address_number.h" | 16 #include "net/base/ip_address_number.h" |
| 17 #include "net/base/net_util.h" | 17 #include "net/base/net_util.h" |
| 18 #include "net/base/network_change_notifier.h" | 18 #include "net/base/network_change_notifier.h" |
| 19 #include "net/base/network_interfaces.h" | 19 #include "net/base/network_interfaces.h" |
| 20 #include "third_party/webrtc/base/socketaddress.h" | |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 rtc::AdapterType ConvertConnectionTypeToAdapterType( | 26 rtc::AdapterType ConvertConnectionTypeToAdapterType( |
| 26 net::NetworkChangeNotifier::ConnectionType type) { | 27 net::NetworkChangeNotifier::ConnectionType type) { |
| 27 switch (type) { | 28 switch (type) { |
| 28 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: | 29 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: |
| 29 return rtc::ADAPTER_TYPE_UNKNOWN; | 30 return rtc::ADAPTER_TYPE_UNKNOWN; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 } | 65 } |
| 65 ++start_count_; | 66 ++start_count_; |
| 66 } | 67 } |
| 67 | 68 |
| 68 void IpcNetworkManager::StopUpdating() { | 69 void IpcNetworkManager::StopUpdating() { |
| 69 DCHECK_GT(start_count_, 0); | 70 DCHECK_GT(start_count_, 0); |
| 70 --start_count_; | 71 --start_count_; |
| 71 } | 72 } |
| 72 | 73 |
| 73 void IpcNetworkManager::OnNetworkListChanged( | 74 void IpcNetworkManager::OnNetworkListChanged( |
| 74 const net::NetworkInterfaceList& list) { | 75 const net::NetworkInterfaceList& list, |
| 75 | 76 const net::IPAddressNumber& default_ipv4_local_address, |
| 77 const net::IPAddressNumber& default_ipv6_local_address) { | |
| 76 // Update flag if network list received for the first time. | 78 // Update flag if network list received for the first time. |
| 77 if (!network_list_received_) | 79 if (!network_list_received_) |
| 78 network_list_received_ = true; | 80 network_list_received_ = true; |
| 79 | 81 |
| 82 // Update the default local interfaces. | |
| 83 rtc::IPAddress ipv4; | |
| 84 if (default_ipv4_local_address.size() == net::kIPv4AddressSize) { | |
| 85 rtc::IPFromString(net::IPAddressToString(default_ipv4_local_address), | |
|
Sergey Ulanov
2015/11/11 18:56:33
Using strings to convert address types may be frag
| |
| 86 &ipv4); | |
| 87 } | |
| 88 rtc::IPAddress ipv6; | |
| 89 if (default_ipv6_local_address.size() == net::kIPv6AddressSize) { | |
| 90 rtc::IPFromString(net::IPAddressToString(default_ipv6_local_address), | |
| 91 &ipv6); | |
| 92 } | |
| 93 set_default_local_addresses(ipv4, ipv6); | |
| 94 | |
| 80 // rtc::Network uses these prefix_length to compare network | 95 // rtc::Network uses these prefix_length to compare network |
| 81 // interfaces discovered. | 96 // interfaces discovered. |
| 82 std::vector<rtc::Network*> networks; | 97 std::vector<rtc::Network*> networks; |
| 83 for (net::NetworkInterfaceList::const_iterator it = list.begin(); | 98 for (net::NetworkInterfaceList::const_iterator it = list.begin(); |
| 84 it != list.end(); it++) { | 99 it != list.end(); it++) { |
| 85 if (it->address.size() == net::kIPv4AddressSize) { | 100 if (it->address.size() == net::kIPv4AddressSize) { |
| 86 uint32 address; | 101 uint32 address; |
| 87 memcpy(&address, &it->address[0], sizeof(uint32)); | 102 memcpy(&address, &it->address[0], sizeof(uint32)); |
| 88 address = rtc::NetworkToHost32(address); | 103 address = rtc::NetworkToHost32(address); |
| 89 rtc::IPAddress prefix = | 104 rtc::IPAddress prefix = |
| 90 rtc::TruncateIP(rtc::IPAddress(address), it->prefix_length); | 105 rtc::TruncateIP(rtc::IPAddress(address), it->prefix_length); |
| 91 rtc::Network* network = | 106 rtc::Network* network = |
| 92 new rtc::Network(it->name, it->name, prefix, it->prefix_length, | 107 new rtc::Network(it->name, it->name, prefix, it->prefix_length, |
| 93 ConvertConnectionTypeToAdapterType(it->type)); | 108 ConvertConnectionTypeToAdapterType(it->type)); |
| 109 network->set_default_local_address_provider(this); | |
| 94 network->AddIP(rtc::IPAddress(address)); | 110 network->AddIP(rtc::IPAddress(address)); |
| 95 networks.push_back(network); | 111 networks.push_back(network); |
| 96 } else if (it->address.size() == net::kIPv6AddressSize) { | 112 } else if (it->address.size() == net::kIPv6AddressSize) { |
| 97 in6_addr address; | 113 in6_addr address; |
| 98 memcpy(&address, &it->address[0], sizeof(in6_addr)); | 114 memcpy(&address, &it->address[0], sizeof(in6_addr)); |
| 99 rtc::InterfaceAddress ip6_addr(address, it->ip_address_attributes); | 115 rtc::InterfaceAddress ip6_addr(address, it->ip_address_attributes); |
| 100 | 116 |
| 101 // Only allow non-deprecated IPv6 addresses which don't contain MAC. | 117 // Only allow non-deprecated IPv6 addresses which don't contain MAC. |
| 102 if (rtc::IPIsMacBased(ip6_addr) || | 118 if (rtc::IPIsMacBased(ip6_addr) || |
| 103 (it->ip_address_attributes & net::IP_ADDRESS_ATTRIBUTE_DEPRECATED)) { | 119 (it->ip_address_attributes & net::IP_ADDRESS_ATTRIBUTE_DEPRECATED)) { |
| 104 continue; | 120 continue; |
| 105 } | 121 } |
| 106 | 122 |
| 107 if (!rtc::IPIsPrivate(ip6_addr)) { | 123 if (!rtc::IPIsPrivate(ip6_addr)) { |
| 108 rtc::IPAddress prefix = | 124 rtc::IPAddress prefix = |
| 109 rtc::TruncateIP(rtc::IPAddress(ip6_addr), it->prefix_length); | 125 rtc::TruncateIP(rtc::IPAddress(ip6_addr), it->prefix_length); |
| 110 rtc::Network* network = | 126 rtc::Network* network = |
| 111 new rtc::Network(it->name, it->name, prefix, it->prefix_length, | 127 new rtc::Network(it->name, it->name, prefix, it->prefix_length, |
| 112 ConvertConnectionTypeToAdapterType(it->type)); | 128 ConvertConnectionTypeToAdapterType(it->type)); |
| 129 network->set_default_local_address_provider(this); | |
| 113 network->AddIP(ip6_addr); | 130 network->AddIP(ip6_addr); |
| 114 networks.push_back(network); | 131 networks.push_back(network); |
| 115 } | 132 } |
| 116 } | 133 } |
| 117 } | 134 } |
| 118 | 135 |
| 119 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 136 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 120 switches::kAllowLoopbackInPeerConnection)) { | 137 switches::kAllowLoopbackInPeerConnection)) { |
| 121 std::string name_v4("loopback_ipv4"); | 138 std::string name_v4("loopback_ipv4"); |
| 122 rtc::IPAddress ip_address_v4(INADDR_LOOPBACK); | 139 rtc::IPAddress ip_address_v4(INADDR_LOOPBACK); |
| 123 rtc::Network* network_v4 = new rtc::Network( | 140 rtc::Network* network_v4 = new rtc::Network( |
| 124 name_v4, name_v4, ip_address_v4, 32, rtc::ADAPTER_TYPE_UNKNOWN); | 141 name_v4, name_v4, ip_address_v4, 32, rtc::ADAPTER_TYPE_UNKNOWN); |
| 142 network_v4->set_default_local_address_provider(this); | |
| 125 network_v4->AddIP(ip_address_v4); | 143 network_v4->AddIP(ip_address_v4); |
| 126 networks.push_back(network_v4); | 144 networks.push_back(network_v4); |
| 127 | 145 |
| 128 std::string name_v6("loopback_ipv6"); | 146 std::string name_v6("loopback_ipv6"); |
| 129 rtc::IPAddress ip_address_v6(in6addr_loopback); | 147 rtc::IPAddress ip_address_v6(in6addr_loopback); |
| 130 rtc::Network* network_v6 = new rtc::Network( | 148 rtc::Network* network_v6 = new rtc::Network( |
| 131 name_v6, name_v6, ip_address_v6, 64, rtc::ADAPTER_TYPE_UNKNOWN); | 149 name_v6, name_v6, ip_address_v6, 64, rtc::ADAPTER_TYPE_UNKNOWN); |
| 150 network_v6->set_default_local_address_provider(this); | |
| 132 network_v6->AddIP(ip_address_v6); | 151 network_v6->AddIP(ip_address_v6); |
| 133 networks.push_back(network_v6); | 152 networks.push_back(network_v6); |
| 134 } | 153 } |
| 135 | 154 |
| 136 bool changed = false; | 155 bool changed = false; |
| 137 NetworkManager::Stats stats; | 156 NetworkManager::Stats stats; |
| 138 MergeNetworkList(networks, &changed, &stats); | 157 MergeNetworkList(networks, &changed, &stats); |
| 139 if (changed) | 158 if (changed) |
| 140 SignalNetworksChanged(); | 159 SignalNetworksChanged(); |
| 141 | 160 |
| 142 // Send interface counts to UMA. | 161 // Send interface counts to UMA. |
| 143 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces", | 162 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces", |
| 144 stats.ipv4_network_count); | 163 stats.ipv4_network_count); |
| 145 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces", | 164 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces", |
| 146 stats.ipv6_network_count); | 165 stats.ipv6_network_count); |
| 147 } | 166 } |
| 148 | 167 |
| 149 void IpcNetworkManager::SendNetworksChangedSignal() { | 168 void IpcNetworkManager::SendNetworksChangedSignal() { |
| 150 SignalNetworksChanged(); | 169 SignalNetworksChanged(); |
| 151 } | 170 } |
| 152 | 171 |
| 153 } // namespace content | 172 } // namespace content |
| OLD | NEW |