File: ipc_network_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (239 lines) | stat: -rw-r--r-- 8,913 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/p2p/ipc_network_manager.h"

#include <string>
#include <utility>
#include <vector>

#include "base/location.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "components/webrtc/net_address_utils.h"
#include "net/base/ip_address.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_interfaces.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/webrtc/rtc_base/socket_address.h"

namespace blink {

namespace {

webrtc::AdapterType ConvertConnectionTypeToAdapterType(
    net::NetworkChangeNotifier::ConnectionType type) {
  switch (type) {
    case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
      return webrtc::ADAPTER_TYPE_UNKNOWN;
    case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
      return webrtc::ADAPTER_TYPE_ETHERNET;
    case net::NetworkChangeNotifier::CONNECTION_WIFI:
      return webrtc::ADAPTER_TYPE_WIFI;
    case net::NetworkChangeNotifier::CONNECTION_2G:
    case net::NetworkChangeNotifier::CONNECTION_3G:
    case net::NetworkChangeNotifier::CONNECTION_4G:
    case net::NetworkChangeNotifier::CONNECTION_5G:
      return webrtc::ADAPTER_TYPE_CELLULAR;
    default:
      return webrtc::ADAPTER_TYPE_UNKNOWN;
  }
}

}  // namespace

IpcNetworkManager::IpcNetworkManager(
    blink::NetworkListManager* network_list_manager,
    std::unique_ptr<webrtc::MdnsResponderInterface> mdns_responder)
    : network_list_manager_(network_list_manager),
      mdns_responder_(std::move(mdns_responder)) {
  DETACH_FROM_THREAD(thread_checker_);
  network_list_manager->AddNetworkListObserver(this);
}

IpcNetworkManager::~IpcNetworkManager() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  DCHECK(!network_list_manager_);
}

void IpcNetworkManager::ContextDestroyed() {
  DCHECK(network_list_manager_);
  network_list_manager_->RemoveNetworkListObserver(this);
  network_list_manager_ = nullptr;
}

base::WeakPtr<IpcNetworkManager>
IpcNetworkManager::AsWeakPtrForSignalingThread() {
  return weak_factory_.GetWeakPtr();
}

void IpcNetworkManager::StartUpdating() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  if (network_list_received_) {
    // Post a task to avoid reentrancy.
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, WTF::BindOnce(&IpcNetworkManager::SendNetworksChangedSignal,
                                 weak_factory_.GetWeakPtr()));
  } else {
    VLOG(1) << "IpcNetworkManager::StartUpdating called; still waiting for "
               "network list from browser process.";
  }
  ++start_count_;
}

void IpcNetworkManager::StopUpdating() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  DCHECK_GT(start_count_, 0);
  --start_count_;
}

void IpcNetworkManager::OnNetworkListChanged(
    const net::NetworkInterfaceList& list,
    const net::IPAddress& default_ipv4_local_address,
    const net::IPAddress& default_ipv6_local_address) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  // Update flag if network list received for the first time.
  if (!network_list_received_) {
    VLOG(1) << "IpcNetworkManager received network list from browser process "
               "for the first time.";
    network_list_received_ = true;
  }

  // Default addresses should be set only when they are in the filtered list of
  // network addresses.
  bool use_default_ipv4_address = false;
  bool use_default_ipv6_address = false;

  // webrtc::Network uses these prefix_length to compare network
  // interfaces discovered.
  std::vector<std::unique_ptr<webrtc::Network>> networks;
  for (auto it = list.begin(); it != list.end(); it++) {
    webrtc::IPAddress ip_address =
        webrtc::NetIPAddressToRtcIPAddress(it->address);
    DCHECK(!ip_address.IsNil());

    webrtc::IPAddress prefix =
        webrtc::TruncateIP(ip_address, it->prefix_length);
    webrtc::AdapterType adapter_type =
        ConvertConnectionTypeToAdapterType(it->type);
    // If the adapter type is unknown, try to guess it using WebRTC's string
    // matching rules.
    if (adapter_type == webrtc::ADAPTER_TYPE_UNKNOWN) {
      adapter_type = webrtc::GetAdapterTypeFromName(it->name.c_str());
    }
    webrtc::AdapterType underlying_adapter_type = webrtc::ADAPTER_TYPE_UNKNOWN;
    if (it->mac_address.has_value() && IsVpnMacAddress(*it->mac_address)) {
      adapter_type = webrtc::ADAPTER_TYPE_VPN;
      // With MAC-based detection we do not know the
      // underlying adapter type.
      underlying_adapter_type = webrtc::ADAPTER_TYPE_UNKNOWN;
    }
    auto network = CreateNetwork(it->name, it->name, prefix, it->prefix_length,
                                 adapter_type);
    if (adapter_type == webrtc::ADAPTER_TYPE_VPN) {
      network->set_underlying_type_for_vpn(underlying_adapter_type);
    }
    network->set_default_local_address_provider(this);
    network->set_mdns_responder_provider(this);

    webrtc::InterfaceAddress iface_addr;
    if (it->address.IsIPv4()) {
      use_default_ipv4_address |= (default_ipv4_local_address == it->address);
      iface_addr = webrtc::InterfaceAddress(ip_address);
    } else {
      DCHECK(it->address.IsIPv6());
      iface_addr =
          webrtc::InterfaceAddress(ip_address, it->ip_address_attributes);

      // Only allow non-link-local, non-loopback, non-deprecated IPv6 addresses
      // which don't contain MAC.
      if (webrtc::IPIsMacBased(iface_addr) ||
          (it->ip_address_attributes & net::IP_ADDRESS_ATTRIBUTE_DEPRECATED) ||
          webrtc::IPIsLinkLocal(iface_addr) ||
          webrtc::IPIsLoopback(iface_addr)) {
        continue;
      }

      // On Fuchsia skip private IPv6 addresses as they break some application.
      // TODO(b/350111561): Remove once the applications are updated to handle
      // ULA addresses properly.
#if BUILDFLAG(IS_FUCHSIA)
      if (webrtc::IPIsPrivate(iface_addr)) {
        continue;
      }
#endif  // BUILDFLAG(IS_FUCHSIA)

      use_default_ipv6_address |= (default_ipv6_local_address == it->address);
    }
    network->AddIP(iface_addr);
    networks.push_back(std::move(network));
  }

  // Update the default local addresses.
  webrtc::IPAddress ipv4_default;
  webrtc::IPAddress ipv6_default;
  if (use_default_ipv4_address) {
    ipv4_default =
        webrtc::NetIPAddressToRtcIPAddress(default_ipv4_local_address);
  }
  if (use_default_ipv6_address) {
    ipv6_default =
        webrtc::NetIPAddressToRtcIPAddress(default_ipv6_local_address);
  }
  set_default_local_addresses(ipv4_default, ipv6_default);

  if (Platform::Current()->AllowsLoopbackInPeerConnection()) {
    std::string name_v4("loopback_ipv4");
    webrtc::IPAddress ip_address_v4(INADDR_LOOPBACK);
    auto network_v4 = CreateNetwork(name_v4, name_v4, ip_address_v4, 32,
                                    webrtc::ADAPTER_TYPE_UNKNOWN);
    network_v4->set_default_local_address_provider(this);
    network_v4->set_mdns_responder_provider(this);
    network_v4->AddIP(ip_address_v4);
    networks.push_back(std::move(network_v4));

    webrtc::IPAddress ipv6_default_address;
    // Only add IPv6 loopback if we can get default local address for IPv6. If
    // we can't, it means that we don't have IPv6 enabled on this machine and
    // bind() to the IPv6 loopback address will fail.
    if (GetDefaultLocalAddress(AF_INET6, &ipv6_default_address)) {
      DCHECK(!ipv6_default_address.IsNil());
      std::string name_v6("loopback_ipv6");
      webrtc::IPAddress ip_address_v6(in6addr_loopback);
      auto network_v6 = CreateNetwork(name_v6, name_v6, ip_address_v6, 64,
                                      webrtc::ADAPTER_TYPE_UNKNOWN);
      network_v6->set_default_local_address_provider(this);
      network_v6->set_mdns_responder_provider(this);
      network_v6->AddIP(ip_address_v6);
      networks.push_back(std::move(network_v6));
    }
  }

  bool changed = false;
  NetworkManager::Stats stats;
  MergeNetworkList(std::move(networks), &changed, &stats);
  if (changed)
    SignalNetworksChanged();

  // Send interface counts to UMA.
  UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
                           stats.ipv4_network_count);
  UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
                           stats.ipv6_network_count);
}

webrtc::MdnsResponderInterface* IpcNetworkManager::GetMdnsResponder() const {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  return mdns_responder_.get();
}

void IpcNetworkManager::SendNetworksChangedSignal() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  SignalNetworksChanged();
}

}  // namespace blink