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
3 // found in the LICENSE file.
5 #include "content/renderer/p2p/ipc_network_manager.h"
8 #include "base/command_line.h"
9 #include "base/metrics/histogram.h"
10 #include "base/sys_byteorder.h"
11 #include "content/public/common/content_switches.h"
12 #include "net/base/net_util.h"
18 rtc::AdapterType
ConvertConnectionTypeToAdapterType(
19 net::NetworkChangeNotifier::ConnectionType type
) {
21 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN
:
22 return rtc::ADAPTER_TYPE_UNKNOWN
;
23 case net::NetworkChangeNotifier::CONNECTION_ETHERNET
:
24 return rtc::ADAPTER_TYPE_ETHERNET
;
25 case net::NetworkChangeNotifier::CONNECTION_WIFI
:
26 return rtc::ADAPTER_TYPE_WIFI
;
27 case net::NetworkChangeNotifier::CONNECTION_2G
:
28 case net::NetworkChangeNotifier::CONNECTION_3G
:
29 case net::NetworkChangeNotifier::CONNECTION_4G
:
30 return rtc::ADAPTER_TYPE_CELLULAR
;
32 return rtc::ADAPTER_TYPE_UNKNOWN
;
38 IpcNetworkManager::IpcNetworkManager(P2PSocketDispatcher
* socket_dispatcher
)
39 : socket_dispatcher_(socket_dispatcher
),
41 network_list_received_(false),
43 socket_dispatcher_
->AddNetworkListObserver(this);
46 IpcNetworkManager::~IpcNetworkManager() {
47 DCHECK(!start_count_
);
48 socket_dispatcher_
->RemoveNetworkListObserver(this);
51 void IpcNetworkManager::StartUpdating() {
52 if (network_list_received_
) {
53 // Post a task to avoid reentrancy.
54 base::MessageLoop::current()->PostTask(
56 base::Bind(&IpcNetworkManager::SendNetworksChangedSignal
,
57 weak_factory_
.GetWeakPtr()));
62 void IpcNetworkManager::StopUpdating() {
63 DCHECK_GT(start_count_
, 0);
67 void IpcNetworkManager::OnNetworkListChanged(
68 const net::NetworkInterfaceList
& list
) {
70 // Update flag if network list received for the first time.
71 if (!network_list_received_
)
72 network_list_received_
= true;
74 // Note: 32 and 64 are the arbitrary(kind of) prefix length used to
75 // differentiate IPv4 and IPv6 addresses.
76 // rtc::Network uses these prefix_length to compare network
77 // interfaces discovered.
78 std::vector
<rtc::Network
*> networks
;
79 int ipv4_interfaces
= 0;
80 int ipv6_interfaces
= 0;
81 for (net::NetworkInterfaceList::const_iterator it
= list
.begin();
82 it
!= list
.end(); it
++) {
83 if (it
->address
.size() == net::kIPv4AddressSize
) {
85 memcpy(&address
, &it
->address
[0], sizeof(uint32
));
86 address
= rtc::NetworkToHost32(address
);
87 rtc::Network
* network
= new rtc::Network(
88 it
->name
, it
->name
, rtc::IPAddress(address
), 32,
89 ConvertConnectionTypeToAdapterType(it
->type
));
90 network
->AddIP(rtc::IPAddress(address
));
91 networks
.push_back(network
);
93 } else if (it
->address
.size() == net::kIPv6AddressSize
) {
95 memcpy(&address
, &it
->address
[0], sizeof(in6_addr
));
96 rtc::IPAddress
ip6_addr(address
);
97 if (!rtc::IPIsPrivate(ip6_addr
)) {
98 rtc::Network
* network
= new rtc::Network(
99 it
->name
, it
->name
, ip6_addr
, 64,
100 ConvertConnectionTypeToAdapterType(it
->type
));
101 network
->AddIP(ip6_addr
);
102 networks
.push_back(network
);
109 // Send interface counts to UMA.
110 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
112 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
115 if (CommandLine::ForCurrentProcess()->HasSwitch(
116 switches::kAllowLoopbackInPeerConnection
)) {
117 std::string
name_v4("loopback_ipv4");
118 rtc::IPAddress
ip_address_v4(INADDR_LOOPBACK
);
119 rtc::Network
* network_v4
= new rtc::Network(
120 name_v4
, name_v4
, ip_address_v4
, 32, rtc::ADAPTER_TYPE_UNKNOWN
);
121 network_v4
->AddIP(ip_address_v4
);
122 networks
.push_back(network_v4
);
124 std::string
name_v6("loopback_ipv6");
125 rtc::IPAddress
ip_address_v6(in6addr_loopback
);
126 rtc::Network
* network_v6
= new rtc::Network(
127 name_v6
, name_v6
, ip_address_v6
, 64, rtc::ADAPTER_TYPE_UNKNOWN
);
128 network_v6
->AddIP(ip_address_v6
);
129 networks
.push_back(network_v6
);
132 bool changed
= false;
133 MergeNetworkList(networks
, &changed
);
135 SignalNetworksChanged();
138 void IpcNetworkManager::SendNetworksChangedSignal() {
139 SignalNetworksChanged();
142 } // namespace content