blob: 4c8aba96e77948fc998b1a6188d795e4c2af8c1a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_NETWORK_H_
12#define RTC_BASE_NETWORK_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
pbosc7c26a02017-01-02 08:42:32 -080015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <deque>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
Niels Möller22211442022-04-07 11:43:28 +020022#include "absl/base/attributes.h"
Ali Tofigh7fa90572022-03-17 15:47:49 +010023#include "absl/strings/string_view.h"
Jonas Orelandac554eb2021-08-27 09:43:38 +020024#include "api/array_view.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020025#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010026#include "api/sequence_checker.h"
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020027#include "api/transport/field_trial_based_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-12 22:51:55 -070029#include "rtc_base/mdns_responder_interface.h"
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020030#include "rtc_base/memory/always_valid_pointer.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/network_monitor.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070032#include "rtc_base/network_monitor_factory.h"
Niels Mölleraa373162021-09-28 16:09:07 +020033#include "rtc_base/socket_factory.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020034#include "rtc_base/system/rtc_export.h"
Niels Möller27c14522022-02-07 16:44:21 +010035#include "rtc_base/task_utils/pending_task_safety_flag.h"
Artem Titove41c4332018-07-25 15:04:28 +020036#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070037#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038
39#if defined(WEBRTC_POSIX)
40struct ifaddrs;
41#endif // defined(WEBRTC_POSIX)
42
43namespace rtc {
44
45extern const char kPublicIPv4Host[];
46extern const char kPublicIPv6Host[];
47
48class IfAddrsConverter;
49class Network;
50class NetworkMonitorInterface;
51class Thread;
52
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053// By default, ignore loopback interfaces on the host.
54const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
55
56// Makes a string key for this network. Used in the network manager's maps.
57// Network objects are keyed on interface name, network prefix and the
58// length of that prefix.
Ali Tofigh7fa90572022-03-17 15:47:49 +010059std::string MakeNetworkKey(absl::string_view name,
Yves Gerey665174f2018-06-19 15:03:05 +020060 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061 int prefix_length);
62
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080063// Utility function that attempts to determine an adapter type by an interface
64// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
65// mechanisms fail to determine the type.
Ali Tofigh2ab914c2022-04-13 12:55:15 +020066RTC_EXPORT AdapterType GetAdapterTypeFromName(absl::string_view network_name);
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080067
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068class DefaultLocalAddressProvider {
69 public:
70 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 15:46:07 -080071
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 // The default local address is the local address used in multi-homed endpoint
73 // when the any address (0.0.0.0 or ::) is used as the local address. It's
74 // important to check the return value as a IP family may not be enabled.
75 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
76};
77
Qingsi Wang5ae259e2019-02-13 15:46:07 -080078class MdnsResponderProvider {
79 public:
80 virtual ~MdnsResponderProvider() = default;
81
82 // Returns the mDNS responder that can be used to obfuscate the local IP
83 // addresses of ICE host candidates by mDNS hostnames.
84 //
85 // The provider MUST outlive the mDNS responder.
86 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
87};
88
Jonas Oreland2ee0e642021-08-25 15:43:02 +020089// Network/mask in CIDR representation.
90class NetworkMask {
91 public:
92 NetworkMask(const IPAddress& addr, int prefix_length)
93 : address_(addr), prefix_length_(prefix_length) {}
94
95 const IPAddress& address() const { return address_; }
96 int prefix_length() const { return prefix_length_; }
97
98 bool operator==(const NetworkMask& o) const {
99 return address_ == o.address_ && prefix_length_ == o.prefix_length_;
100 }
101
102 private:
103 IPAddress address_;
104 // Length of valid bits in address_ (for ipv4 valid range is 0-32)
105 int prefix_length_;
106};
107
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200108// Generic network manager interface. It provides list of local
109// networks.
110//
111// Every method of NetworkManager (including the destructor) must be called on
112// the same thread, except for the constructor which may be called on any
113// thread.
114//
115// This allows constructing a NetworkManager subclass on one thread and
116// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200117class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
118 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 public:
Niels Möllerd959f3a2022-04-19 11:29:19 +0200120 using NetworkList ABSL_DEPRECATED("bugs.webrtc.org/13869") =
121 std::vector<Network*>;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122
123 // This enum indicates whether adapter enumeration is allowed.
124 enum EnumerationPermission {
125 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
126 // from GetNetworks means that there is no network
127 // available.
128 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
129 // GetAnyAddressNetworks() should be used instead.
130 };
131
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132 // Called when network list is updated.
133 sigslot::signal0<> SignalNetworksChanged;
134
135 // Indicates a failure when getting list of network interfaces.
136 sigslot::signal0<> SignalError;
137
138 // This should be called on the NetworkManager's thread before the
139 // NetworkManager is used. Subclasses may override this if necessary.
140 virtual void Initialize() {}
141
142 // Start/Stop monitoring of network interfaces
143 // list. SignalNetworksChanged or SignalError is emitted immediately
144 // after StartUpdating() is called. After that SignalNetworksChanged
145 // is emitted whenever list of networks changes.
146 virtual void StartUpdating() = 0;
147 virtual void StopUpdating() = 0;
148
149 // Returns the current list of networks available on this machine.
150 // StartUpdating() must be called before this method is called.
151 // It makes sure that repeated calls return the same object for a
152 // given network, so that quality is tracked appropriately. Does not
153 // include ignored networks.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200154 // The returned vector of Network* is valid as long as the NetworkManager is
155 // alive.
Niels Möller22211442022-04-07 11:43:28 +0200156 virtual std::vector<const Network*> GetNetworks() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200157
Qingsi Wang09619332018-09-12 22:51:55 -0700158 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159 virtual EnumerationPermission enumeration_permission() const;
160
161 // "AnyAddressNetwork" is a network which only contains single "any address"
162 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
163 // useful as binding to such interfaces allow default routing behavior like
164 // http traffic.
165 //
166 // This method appends the "any address" networks to the list, such that this
167 // can optionally be called after GetNetworks.
Niels Möller22211442022-04-07 11:43:28 +0200168 virtual std::vector<const Network*> GetAnyAddressNetworks() = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200169
170 // Dumps the current list of networks in the network manager.
171 virtual void DumpNetworks() {}
172 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
173
174 struct Stats {
175 int ipv4_network_count;
176 int ipv6_network_count;
177 Stats() {
178 ipv4_network_count = 0;
179 ipv6_network_count = 0;
180 }
181 };
Qingsi Wang09619332018-09-12 22:51:55 -0700182
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800183 // MdnsResponderProvider interface.
184 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200185
186 virtual void set_vpn_list(const std::vector<NetworkMask>& vpn) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200187};
188
189// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200190class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200191 public:
Jonas Orelande62c2f22022-03-29 11:04:48 +0200192 NetworkManagerBase(const webrtc::FieldTrialsView* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200193
Niels Möller22211442022-04-07 11:43:28 +0200194 std::vector<const Network*> GetNetworks() const override;
195 std::vector<const Network*> GetAnyAddressNetworks() override;
deadbeef3427f532017-07-26 16:09:33 -0700196
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200197 EnumerationPermission enumeration_permission() const override;
198
199 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
200
Jonas Orelandac554eb2021-08-27 09:43:38 +0200201 // Check if MAC address in |bytes| is one of the pre-defined
202 // MAC addresses for know VPNs.
203 static bool IsVpnMacAddress(rtc::ArrayView<const uint8_t> address);
204
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200205 protected:
Artem Titov96e3b992021-07-26 16:03:14 +0200206 // Updates `networks_` with the networks listed in `list`. If
Niels Möller22211442022-04-07 11:43:28 +0200207 // `networks_map_` already has a Network object for a network listed
Artem Titov96e3b992021-07-26 16:03:14 +0200208 // in the `list` then it is reused. Accept ownership of the Network
209 // objects in the `list`. `changed` will be set to true if there is
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200210 // any change in the network list.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200211 void MergeNetworkList(std::vector<std::unique_ptr<Network>> list,
212 bool* changed);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200213
Artem Titov96e3b992021-07-26 16:03:14 +0200214 // `stats` will be populated even if |*changed| is false.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200215 void MergeNetworkList(std::vector<std::unique_ptr<Network>> list,
216 bool* changed,
217 NetworkManager::Stats* stats);
218 ABSL_DEPRECATED("bugs.webrtc.org/13869")
219 void MergeNetworkList(std::vector<Network*> list, bool* changed);
220
221 ABSL_DEPRECATED("bugs.webrtc.org/13869")
222 void MergeNetworkList(std::vector<Network*> list,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200223 bool* changed,
224 NetworkManager::Stats* stats);
225
226 void set_enumeration_permission(EnumerationPermission state) {
227 enumeration_permission_ = state;
228 }
229
230 void set_default_local_addresses(const IPAddress& ipv4,
231 const IPAddress& ipv6);
232
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000233 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
234
Niels Möllerbf4988a2022-04-01 16:38:27 +0200235 // To enable subclasses to get the networks list, without interfering with
236 // refactoring of the interface GetNetworks method.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200237 const std::vector<Network*>& GetNetworksInternal() const { return networks_; }
Niels Möllerbf4988a2022-04-01 16:38:27 +0200238
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200239 private:
240 friend class NetworkTest;
241
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200242 EnumerationPermission enumeration_permission_;
243
Niels Möllerd959f3a2022-04-19 11:29:19 +0200244 std::vector<Network*> networks_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200245
Niels Möllerd959f3a2022-04-19 11:29:19 +0200246 std::map<std::string, std::unique_ptr<Network>> networks_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200247
248 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
249 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
250
251 IPAddress default_local_ipv4_address_;
252 IPAddress default_local_ipv6_address_;
253 // We use 16 bits to save the bandwidth consumption when sending the network
254 // id over the Internet. It is OK that the 16-bit integer overflows to get a
255 // network id 0 because we only compare the network ids in the old and the new
256 // best connections in the transport channel.
257 uint16_t next_available_network_id_ = 1;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200258
259 // True if calling network_preference() with a changed value
260 // should result in firing the SignalNetworkChanged signal.
261 bool signal_network_preference_change_ = false;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200262};
263
264// Basic implementation of the NetworkManager interface that gets list
265// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200266class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000267 public NetworkBinderInterface,
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200268 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200269 public:
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200270 // This version is used by chromium.
Niels Möller539f3e12021-11-26 16:33:19 +0100271 ABSL_DEPRECATED(
272 "Use the version with socket_factory, see bugs.webrtc.org/13145")
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200273 explicit BasicNetworkManager(
Jonas Orelande62c2f22022-03-29 11:04:48 +0200274 const webrtc::FieldTrialsView* field_trials = nullptr)
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200275 : BasicNetworkManager(
276 /* network_monitor_factory= */ nullptr,
277 /* socket_factory= */ nullptr,
278 field_trials) {}
279
280 // This is used by lots of downstream code.
Jonas Orelande62c2f22022-03-29 11:04:48 +0200281 BasicNetworkManager(SocketFactory* socket_factory,
282 const webrtc::FieldTrialsView* field_trials = nullptr)
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200283 : BasicNetworkManager(/* network_monitor_factory= */ nullptr,
284 socket_factory,
285 field_trials) {}
286
Jonas Orelande62c2f22022-03-29 11:04:48 +0200287 BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory,
288 SocketFactory* socket_factory,
289 const webrtc::FieldTrialsView* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200290 ~BasicNetworkManager() override;
291
292 void StartUpdating() override;
293 void StopUpdating() override;
294
295 void DumpNetworks() override;
296
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200297 bool started() { return start_count_ > 0; }
298
299 // Sets the network ignore list, which is empty by default. Any network on the
300 // ignore list will be filtered from network enumeration results.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700301 // Should be called only before initialization.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200302 void set_network_ignore_list(const std::vector<std::string>& list) {
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700303 RTC_DCHECK(thread_ == nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200304 network_ignore_list_ = list;
305 }
306
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200307 // Set a list of manually configured VPN's.
308 void set_vpn_list(const std::vector<NetworkMask>& vpn) override;
309
310 // Check if |prefix| is configured as VPN.
311 bool IsConfiguredVpn(IPAddress prefix, int prefix_length) const;
312
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000313 // Bind a socket to interface that ip address belong to.
314 // Implementation look up interface name and calls
315 // BindSocketToNetwork on NetworkMonitor.
316 // The interface name is needed as e.g ipv4 over ipv6 addresses
317 // are not exposed using Android functions, but it is possible
318 // bind an ipv4 address to the interface.
319 NetworkBindingResult BindSocketToNetwork(int socket_fd,
320 const IPAddress& address) override;
321
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200322 protected:
323#if defined(WEBRTC_POSIX)
324 // Separated from CreateNetworks for tests.
325 void ConvertIfAddrs(ifaddrs* interfaces,
326 IfAddrsConverter* converter,
327 bool include_ignored,
Niels Möllerd959f3a2022-04-19 11:29:19 +0200328 std::vector<std::unique_ptr<Network>>* networks) const
329 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200330#endif // defined(WEBRTC_POSIX)
331
332 // Creates a network object for each network available on the machine.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200333 bool CreateNetworks(bool include_ignored,
334 std::vector<std::unique_ptr<Network>>* networks) const
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700335 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200336
337 // Determines if a network should be ignored. This should only be determined
338 // based on the network's property instead of any individual IP.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700339 bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200340
341 // This function connects a UDP socket to a public address and returns the
342 // local address associated it. Since it binds to the "any" address
343 // internally, it returns the default local address on a multi-homed endpoint.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700344 IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200345
346 private:
347 friend class NetworkTest;
348
349 // Creates a network monitor and listens for network updates.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700350 void StartNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200351 // Stops and removes the network monitor.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700352 void StopNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200353 // Called when it receives updates from the network monitor.
354 void OnNetworksChanged();
355
356 // Updates the networks and reschedules the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700357 void UpdateNetworksContinually() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200358 // Only updates the networks; does not reschedule the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700359 void UpdateNetworksOnce() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200360
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700361 Thread* thread_ = nullptr;
362 bool sent_first_update_ = true;
363 int start_count_ = 0;
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200364 // Chromium create BasicNetworkManager() w/o field trials.
Jonas Orelande62c2f22022-03-29 11:04:48 +0200365 webrtc::AlwaysValidPointer<const webrtc::FieldTrialsView,
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200366 webrtc::FieldTrialBasedConfig>
367 field_trials_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200368 std::vector<std::string> network_ignore_list_;
Niels Mölleraa373162021-09-28 16:09:07 +0200369 NetworkMonitorFactory* const network_monitor_factory_;
370 SocketFactory* const socket_factory_;
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700371 std::unique_ptr<NetworkMonitorInterface> network_monitor_
372 RTC_GUARDED_BY(thread_);
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000373 bool allow_mac_based_ipv6_ RTC_GUARDED_BY(thread_) = false;
374 bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200375
376 std::vector<NetworkMask> vpn_;
Niels Möller27c14522022-02-07 16:44:21 +0100377 rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> task_safety_flag_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200378};
379
380// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200381class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200382 public:
Ali Tofigh7fa90572022-03-17 15:47:49 +0100383 Network(absl::string_view name,
384 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200385 const IPAddress& prefix,
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200386 int prefix_length)
387 : Network(name,
388 description,
389 prefix,
390 prefix_length,
391 rtc::ADAPTER_TYPE_UNKNOWN) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200392
Ali Tofigh7fa90572022-03-17 15:47:49 +0100393 Network(absl::string_view name,
394 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200395 const IPAddress& prefix,
396 int prefix_length,
397 AdapterType type);
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200398
Steve Anton9de3aac2017-10-24 10:08:26 -0700399 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200400 ~Network();
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200401
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700402 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100403 // Mutable, to support connecting on the const Network passed to cricket::Port
404 // constructor.
405 mutable sigslot::signal1<const Network*> SignalTypeChanged;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200406
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200407 // This signal is fired whenever network preference changes.
408 sigslot::signal1<const Network*> SignalNetworkPreferenceChanged;
409
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100410 const DefaultLocalAddressProvider* default_local_address_provider() const {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200411 return default_local_address_provider_;
412 }
413 void set_default_local_address_provider(
414 const DefaultLocalAddressProvider* provider) {
415 default_local_address_provider_ = provider;
416 }
417
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800418 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
419 mdns_responder_provider_ = provider;
420 }
421
422 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200423 const std::string& name() const { return name_; }
424
425 // Returns the OS-assigned name for this network. This is useful for
426 // debugging but should not be sent over the wire (for privacy reasons).
427 const std::string& description() const { return description_; }
428
429 // Returns the prefix for this network.
430 const IPAddress& prefix() const { return prefix_; }
431 // Returns the length, in bits, of this network's prefix.
432 int prefix_length() const { return prefix_length_; }
433
Artem Titov96e3b992021-07-26 16:03:14 +0200434 // `key_` has unique value per network interface. Used in sorting network
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200435 // interfaces. Key is derived from interface name and it's prefix.
436 std::string key() const { return key_; }
437
438 // Returns the Network's current idea of the 'best' IP it has.
439 // Or return an unset IP if this network has no active addresses.
440 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800441 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200442 // 2) if #1 not available, return global ones.
443 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
444 // for unique local address, which is not route-able in open
445 // internet but might be useful for a close WebRTC deployment.
446
447 // TODO(guoweis): rule #3 actually won't happen at current
448 // implementation. The reason being that ULA address starting with
449 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
450 // that is WebRTC will have one extra Network to generate candidates
451 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
452 // ULA should only be tried in a close deployment anyway.
453
454 // Note that when not specifying any flag, it's treated as case global
455 // IPv6 address
456 IPAddress GetBestIP() const;
457
458 // Keep the original function here for now.
459 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
460 IPAddress ip() const { return GetBestIP(); }
461
462 // Adds an active IP address to this network. Does not check for duplicates.
463 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800464 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200465
466 // Sets the network's IP address list. Returns true if new IP addresses were
467 // detected. Passing true to already_changed skips this check.
468 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
469 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 15:03:05 +0200470 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200471 // Clear the network's list of addresses.
472 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800473 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-12 22:51:55 -0700474 // addresses of host candidates by mDNS names in ICE gathering. After a
475 // name-address mapping is created by the mDNS responder, queries for the
476 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800477 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200478
479 // Returns the scope-id of the network's address.
480 // Should only be relevant for link-local IPv6 addresses.
481 int scope_id() const { return scope_id_; }
482 void set_scope_id(int id) { scope_id_ = id; }
483
484 // Indicates whether this network should be ignored, perhaps because
485 // the IP is 0, or the interface is one we know is invalid.
486 bool ignored() const { return ignored_; }
487 void set_ignored(bool ignored) { ignored_ = ignored; }
488
489 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700490 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
491 // network interface used by the VPN, typically the preferred network type
492 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
493 // on https://developer.android.com/reference/android/net/VpnService.html).
494 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
495 // returned.
496 AdapterType underlying_type_for_vpn() const {
497 return underlying_type_for_vpn_;
498 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200499 void set_type(AdapterType type) {
500 if (type_ == type) {
501 return;
502 }
503 type_ = type;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700504 if (type != ADAPTER_TYPE_VPN) {
505 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
506 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200507 SignalTypeChanged(this);
508 }
509
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700510 void set_underlying_type_for_vpn(AdapterType type) {
511 if (underlying_type_for_vpn_ == type) {
512 return;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200513 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700514 underlying_type_for_vpn_ = type;
515 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200516 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700517
518 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
519
Jonas Orelandc7ea04a2020-04-03 10:12:28 +0200520 bool IsCellular() const { return IsCellular(type_); }
521
522 static bool IsCellular(AdapterType type) {
523 switch (type) {
Jonas Oreland08d18062020-04-02 07:19:12 +0200524 case ADAPTER_TYPE_CELLULAR:
525 case ADAPTER_TYPE_CELLULAR_2G:
526 case ADAPTER_TYPE_CELLULAR_3G:
527 case ADAPTER_TYPE_CELLULAR_4G:
528 case ADAPTER_TYPE_CELLULAR_5G:
529 return true;
530 default:
531 return false;
532 }
533 }
534
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200535 // Note: This function is called "rarely".
536 // Twice per Network in BasicPortAllocator if
537 // PORTALLOCATOR_DISABLE_COSTLY_NETWORKS. Once in Port::Construct() (and when
538 // Port::OnNetworkTypeChanged is called).
539 ABSL_DEPRECATED(
540 "Use the version with field trials, see bugs.webrtc.org/webrtc:10335")
Jonas Orelande62c2f22022-03-29 11:04:48 +0200541 uint16_t GetCost(const webrtc::FieldTrialsView* field_trials = nullptr) const;
542 uint16_t GetCost(const webrtc::FieldTrialsView& field_trials) const;
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200543
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200544 // A unique id assigned by the network manager, which may be signaled
545 // to the remote side in the candidate.
546 uint16_t id() const { return id_; }
547 void set_id(uint16_t id) { id_ = id; }
548
549 int preference() const { return preference_; }
550 void set_preference(int preference) { preference_ = preference; }
551
552 // When we enumerate networks and find a previously-seen network is missing,
553 // we do not remove it (because it may be used elsewhere). Instead, we mark
554 // it inactive, so that we can detect network changes properly.
555 bool active() const { return active_; }
556 void set_active(bool active) {
557 if (active_ != active) {
558 active_ = active;
559 }
560 }
561
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200562 // Property set by operating system/firmware that has information
563 // about connection strength to e.g WIFI router or CELL base towers.
564 NetworkPreference network_preference() const { return network_preference_; }
565 void set_network_preference(NetworkPreference val) {
566 if (network_preference_ == val) {
567 return;
568 }
569 network_preference_ = val;
570 SignalNetworkPreferenceChanged(this);
571 }
572
Jonas Oreland30019052022-01-28 14:11:44 +0100573 static std::pair<rtc::AdapterType, bool /* vpn */>
574 GuessAdapterFromNetworkCost(int network_cost);
575
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200576 // Debugging description of this network
577 std::string ToString() const;
578
579 private:
580 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800581 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200582 std::string name_;
583 std::string description_;
584 IPAddress prefix_;
585 int prefix_length_;
586 std::string key_;
587 std::vector<InterfaceAddress> ips_;
588 int scope_id_;
589 bool ignored_;
590 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700591 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200592 int preference_;
593 bool active_ = true;
594 uint16_t id_ = 0;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200595 NetworkPreference network_preference_ = NetworkPreference::NEUTRAL;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200596
597 friend class NetworkManager;
598};
599
600} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000601
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200602#endif // RTC_BASE_NETWORK_H_