blob: d01a4188600e5b90f162601f9985d6f41ffde0e5 [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
Ali Tofigh7fa90572022-03-17 15:47:49 +010022#include "absl/strings/string_view.h"
Jonas Orelandac554eb2021-08-27 09:43:38 +020023#include "api/array_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010024#include "api/sequence_checker.h"
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020025#include "api/transport/field_trial_based_config.h"
26#include "api/webrtc_key_value_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-12 22:51:55 -070028#include "rtc_base/mdns_responder_interface.h"
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020029#include "rtc_base/memory/always_valid_pointer.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/network_monitor.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070031#include "rtc_base/network_monitor_factory.h"
Niels Mölleraa373162021-09-28 16:09:07 +020032#include "rtc_base/socket_factory.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020033#include "rtc_base/system/rtc_export.h"
Niels Möller27c14522022-02-07 16:44:21 +010034#include "rtc_base/task_utils/pending_task_safety_flag.h"
Artem Titove41c4332018-07-25 15:04:28 +020035#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070036#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
38#if defined(WEBRTC_POSIX)
39struct ifaddrs;
40#endif // defined(WEBRTC_POSIX)
41
42namespace rtc {
43
44extern const char kPublicIPv4Host[];
45extern const char kPublicIPv6Host[];
46
47class IfAddrsConverter;
48class Network;
49class NetworkMonitorInterface;
50class Thread;
51
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052// By default, ignore loopback interfaces on the host.
53const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
54
55// Makes a string key for this network. Used in the network manager's maps.
56// Network objects are keyed on interface name, network prefix and the
57// length of that prefix.
Ali Tofigh7fa90572022-03-17 15:47:49 +010058std::string MakeNetworkKey(absl::string_view name,
Yves Gerey665174f2018-06-19 15:03:05 +020059 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 int prefix_length);
61
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080062// Utility function that attempts to determine an adapter type by an interface
63// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
64// mechanisms fail to determine the type.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020065RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name);
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080066
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067class DefaultLocalAddressProvider {
68 public:
69 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 15:46:07 -080070
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071 // The default local address is the local address used in multi-homed endpoint
72 // when the any address (0.0.0.0 or ::) is used as the local address. It's
73 // important to check the return value as a IP family may not be enabled.
74 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
75};
76
Qingsi Wang5ae259e2019-02-13 15:46:07 -080077class MdnsResponderProvider {
78 public:
79 virtual ~MdnsResponderProvider() = default;
80
81 // Returns the mDNS responder that can be used to obfuscate the local IP
82 // addresses of ICE host candidates by mDNS hostnames.
83 //
84 // The provider MUST outlive the mDNS responder.
85 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
86};
87
Jonas Oreland2ee0e642021-08-25 15:43:02 +020088// Network/mask in CIDR representation.
89class NetworkMask {
90 public:
91 NetworkMask(const IPAddress& addr, int prefix_length)
92 : address_(addr), prefix_length_(prefix_length) {}
93
94 const IPAddress& address() const { return address_; }
95 int prefix_length() const { return prefix_length_; }
96
97 bool operator==(const NetworkMask& o) const {
98 return address_ == o.address_ && prefix_length_ == o.prefix_length_;
99 }
100
101 private:
102 IPAddress address_;
103 // Length of valid bits in address_ (for ipv4 valid range is 0-32)
104 int prefix_length_;
105};
106
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107// Generic network manager interface. It provides list of local
108// networks.
109//
110// Every method of NetworkManager (including the destructor) must be called on
111// the same thread, except for the constructor which may be called on any
112// thread.
113//
114// This allows constructing a NetworkManager subclass on one thread and
115// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200116class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
117 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 public:
119 typedef std::vector<Network*> NetworkList;
120
121 // This enum indicates whether adapter enumeration is allowed.
122 enum EnumerationPermission {
123 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
124 // from GetNetworks means that there is no network
125 // available.
126 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
127 // GetAnyAddressNetworks() should be used instead.
128 };
129
130 NetworkManager();
131 ~NetworkManager() override;
132
133 // Called when network list is updated.
134 sigslot::signal0<> SignalNetworksChanged;
135
136 // Indicates a failure when getting list of network interfaces.
137 sigslot::signal0<> SignalError;
138
139 // This should be called on the NetworkManager's thread before the
140 // NetworkManager is used. Subclasses may override this if necessary.
141 virtual void Initialize() {}
142
143 // Start/Stop monitoring of network interfaces
144 // list. SignalNetworksChanged or SignalError is emitted immediately
145 // after StartUpdating() is called. After that SignalNetworksChanged
146 // is emitted whenever list of networks changes.
147 virtual void StartUpdating() = 0;
148 virtual void StopUpdating() = 0;
149
150 // Returns the current list of networks available on this machine.
151 // StartUpdating() must be called before this method is called.
152 // It makes sure that repeated calls return the same object for a
153 // given network, so that quality is tracked appropriately. Does not
154 // include ignored networks.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100155 virtual std::vector<const Network*> GetNetworks() const {
156 std::vector<Network*> networks;
157 std::vector<const Network*> const_networks;
158 GetNetworks(&networks);
159 const_networks.insert(const_networks.begin(), networks.begin(),
160 networks.end());
161 return const_networks;
162 }
163 // TODO(bugs.webrtc.org/13869): Delete this overload.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200164 virtual void GetNetworks(NetworkList* networks) const = 0;
165
Qingsi Wang09619332018-09-12 22:51:55 -0700166 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200167 virtual EnumerationPermission enumeration_permission() const;
168
169 // "AnyAddressNetwork" is a network which only contains single "any address"
170 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
171 // useful as binding to such interfaces allow default routing behavior like
172 // http traffic.
173 //
174 // This method appends the "any address" networks to the list, such that this
175 // can optionally be called after GetNetworks.
176 //
177 // TODO(guoweis): remove this body when chromium implements this.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100178 virtual std::vector<const Network*> GetAnyAddressNetworks() {
179 std::vector<Network*> networks;
180 std::vector<const Network*> const_networks;
181 GetAnyAddressNetworks(&networks);
182 const_networks.insert(const_networks.begin(), networks.begin(),
183 networks.end());
184 return const_networks;
185 }
186 // TODO(bugs.webrtc.org/13869): Delete this overload.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200187 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
188
189 // Dumps the current list of networks in the network manager.
190 virtual void DumpNetworks() {}
191 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
192
193 struct Stats {
194 int ipv4_network_count;
195 int ipv6_network_count;
196 Stats() {
197 ipv4_network_count = 0;
198 ipv6_network_count = 0;
199 }
200 };
Qingsi Wang09619332018-09-12 22:51:55 -0700201
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800202 // MdnsResponderProvider interface.
203 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200204
205 virtual void set_vpn_list(const std::vector<NetworkMask>& vpn) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200206};
207
208// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200209class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200210 public:
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200211 NetworkManagerBase(
212 const webrtc::WebRtcKeyValueConfig* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200213 ~NetworkManagerBase() override;
214
215 void GetNetworks(NetworkList* networks) const override;
216 void GetAnyAddressNetworks(NetworkList* networks) override;
deadbeef3427f532017-07-26 16:09:33 -0700217
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200218 EnumerationPermission enumeration_permission() const override;
219
220 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
221
Jonas Orelandac554eb2021-08-27 09:43:38 +0200222 // Check if MAC address in |bytes| is one of the pre-defined
223 // MAC addresses for know VPNs.
224 static bool IsVpnMacAddress(rtc::ArrayView<const uint8_t> address);
225
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200226 protected:
227 typedef std::map<std::string, Network*> NetworkMap;
Artem Titov96e3b992021-07-26 16:03:14 +0200228 // Updates `networks_` with the networks listed in `list`. If
229 // `network_map_` already has a Network object for a network listed
230 // in the `list` then it is reused. Accept ownership of the Network
231 // objects in the `list`. `changed` will be set to true if there is
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200232 // any change in the network list.
233 void MergeNetworkList(const NetworkList& list, bool* changed);
234
Artem Titov96e3b992021-07-26 16:03:14 +0200235 // `stats` will be populated even if |*changed| is false.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200236 void MergeNetworkList(const NetworkList& list,
237 bool* changed,
238 NetworkManager::Stats* stats);
239
240 void set_enumeration_permission(EnumerationPermission state) {
241 enumeration_permission_ = state;
242 }
243
244 void set_default_local_addresses(const IPAddress& ipv4,
245 const IPAddress& ipv6);
246
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000247 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
248
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200249 private:
250 friend class NetworkTest;
251
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200252 EnumerationPermission enumeration_permission_;
253
254 NetworkList networks_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200255
256 NetworkMap networks_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200257
258 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
259 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
260
261 IPAddress default_local_ipv4_address_;
262 IPAddress default_local_ipv6_address_;
263 // We use 16 bits to save the bandwidth consumption when sending the network
264 // id over the Internet. It is OK that the 16-bit integer overflows to get a
265 // network id 0 because we only compare the network ids in the old and the new
266 // best connections in the transport channel.
267 uint16_t next_available_network_id_ = 1;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200268
269 // True if calling network_preference() with a changed value
270 // should result in firing the SignalNetworkChanged signal.
271 bool signal_network_preference_change_ = false;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200272};
273
274// Basic implementation of the NetworkManager interface that gets list
275// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200276class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000277 public NetworkBinderInterface,
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200278 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200279 public:
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200280 // This version is used by chromium.
Niels Möller539f3e12021-11-26 16:33:19 +0100281 ABSL_DEPRECATED(
282 "Use the version with socket_factory, see bugs.webrtc.org/13145")
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200283 explicit BasicNetworkManager(
284 const webrtc::WebRtcKeyValueConfig* field_trials = nullptr)
285 : BasicNetworkManager(
286 /* network_monitor_factory= */ nullptr,
287 /* socket_factory= */ nullptr,
288 field_trials) {}
289
290 // This is used by lots of downstream code.
291 BasicNetworkManager(
292 SocketFactory* socket_factory,
293 const webrtc::WebRtcKeyValueConfig* field_trials = nullptr)
294 : BasicNetworkManager(/* network_monitor_factory= */ nullptr,
295 socket_factory,
296 field_trials) {}
297
298 BasicNetworkManager(
299 NetworkMonitorFactory* network_monitor_factory,
300 SocketFactory* socket_factory,
301 const webrtc::WebRtcKeyValueConfig* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200302 ~BasicNetworkManager() override;
303
304 void StartUpdating() override;
305 void StopUpdating() override;
306
307 void DumpNetworks() override;
308
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200309 bool started() { return start_count_ > 0; }
310
311 // Sets the network ignore list, which is empty by default. Any network on the
312 // ignore list will be filtered from network enumeration results.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700313 // Should be called only before initialization.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200314 void set_network_ignore_list(const std::vector<std::string>& list) {
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700315 RTC_DCHECK(thread_ == nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200316 network_ignore_list_ = list;
317 }
318
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200319 // Set a list of manually configured VPN's.
320 void set_vpn_list(const std::vector<NetworkMask>& vpn) override;
321
322 // Check if |prefix| is configured as VPN.
323 bool IsConfiguredVpn(IPAddress prefix, int prefix_length) const;
324
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000325 // Bind a socket to interface that ip address belong to.
326 // Implementation look up interface name and calls
327 // BindSocketToNetwork on NetworkMonitor.
328 // The interface name is needed as e.g ipv4 over ipv6 addresses
329 // are not exposed using Android functions, but it is possible
330 // bind an ipv4 address to the interface.
331 NetworkBindingResult BindSocketToNetwork(int socket_fd,
332 const IPAddress& address) override;
333
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200334 protected:
335#if defined(WEBRTC_POSIX)
336 // Separated from CreateNetworks for tests.
337 void ConvertIfAddrs(ifaddrs* interfaces,
338 IfAddrsConverter* converter,
339 bool include_ignored,
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700340 NetworkList* networks) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200341#endif // defined(WEBRTC_POSIX)
342
343 // Creates a network object for each network available on the machine.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700344 bool CreateNetworks(bool include_ignored, NetworkList* networks) const
345 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200346
347 // Determines if a network should be ignored. This should only be determined
348 // based on the network's property instead of any individual IP.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700349 bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200350
351 // This function connects a UDP socket to a public address and returns the
352 // local address associated it. Since it binds to the "any" address
353 // internally, it returns the default local address on a multi-homed endpoint.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700354 IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200355
356 private:
357 friend class NetworkTest;
358
359 // Creates a network monitor and listens for network updates.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700360 void StartNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200361 // Stops and removes the network monitor.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700362 void StopNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200363 // Called when it receives updates from the network monitor.
364 void OnNetworksChanged();
365
366 // Updates the networks and reschedules the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700367 void UpdateNetworksContinually() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200368 // Only updates the networks; does not reschedule the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700369 void UpdateNetworksOnce() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200370
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700371 Thread* thread_ = nullptr;
372 bool sent_first_update_ = true;
373 int start_count_ = 0;
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200374 // Chromium create BasicNetworkManager() w/o field trials.
375 webrtc::AlwaysValidPointer<const webrtc::WebRtcKeyValueConfig,
376 webrtc::FieldTrialBasedConfig>
377 field_trials_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200378 std::vector<std::string> network_ignore_list_;
Niels Mölleraa373162021-09-28 16:09:07 +0200379 NetworkMonitorFactory* const network_monitor_factory_;
380 SocketFactory* const socket_factory_;
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700381 std::unique_ptr<NetworkMonitorInterface> network_monitor_
382 RTC_GUARDED_BY(thread_);
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000383 bool allow_mac_based_ipv6_ RTC_GUARDED_BY(thread_) = false;
384 bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200385
386 std::vector<NetworkMask> vpn_;
Niels Möller27c14522022-02-07 16:44:21 +0100387 rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> task_safety_flag_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200388};
389
390// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200391class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200392 public:
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,
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200396 int prefix_length)
397 : Network(name,
398 description,
399 prefix,
400 prefix_length,
401 rtc::ADAPTER_TYPE_UNKNOWN) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200402
Ali Tofigh7fa90572022-03-17 15:47:49 +0100403 Network(absl::string_view name,
404 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200405 const IPAddress& prefix,
406 int prefix_length,
407 AdapterType type);
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200408
Steve Anton9de3aac2017-10-24 10:08:26 -0700409 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200410 ~Network();
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200411
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700412 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100413 // Mutable, to support connecting on the const Network passed to cricket::Port
414 // constructor.
415 mutable sigslot::signal1<const Network*> SignalTypeChanged;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200416
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200417 // This signal is fired whenever network preference changes.
418 sigslot::signal1<const Network*> SignalNetworkPreferenceChanged;
419
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100420 const DefaultLocalAddressProvider* default_local_address_provider() const {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200421 return default_local_address_provider_;
422 }
423 void set_default_local_address_provider(
424 const DefaultLocalAddressProvider* provider) {
425 default_local_address_provider_ = provider;
426 }
427
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800428 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
429 mdns_responder_provider_ = provider;
430 }
431
432 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200433 const std::string& name() const { return name_; }
434
435 // Returns the OS-assigned name for this network. This is useful for
436 // debugging but should not be sent over the wire (for privacy reasons).
437 const std::string& description() const { return description_; }
438
439 // Returns the prefix for this network.
440 const IPAddress& prefix() const { return prefix_; }
441 // Returns the length, in bits, of this network's prefix.
442 int prefix_length() const { return prefix_length_; }
443
Artem Titov96e3b992021-07-26 16:03:14 +0200444 // `key_` has unique value per network interface. Used in sorting network
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200445 // interfaces. Key is derived from interface name and it's prefix.
446 std::string key() const { return key_; }
447
448 // Returns the Network's current idea of the 'best' IP it has.
449 // Or return an unset IP if this network has no active addresses.
450 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800451 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200452 // 2) if #1 not available, return global ones.
453 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
454 // for unique local address, which is not route-able in open
455 // internet but might be useful for a close WebRTC deployment.
456
457 // TODO(guoweis): rule #3 actually won't happen at current
458 // implementation. The reason being that ULA address starting with
459 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
460 // that is WebRTC will have one extra Network to generate candidates
461 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
462 // ULA should only be tried in a close deployment anyway.
463
464 // Note that when not specifying any flag, it's treated as case global
465 // IPv6 address
466 IPAddress GetBestIP() const;
467
468 // Keep the original function here for now.
469 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
470 IPAddress ip() const { return GetBestIP(); }
471
472 // Adds an active IP address to this network. Does not check for duplicates.
473 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800474 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200475
476 // Sets the network's IP address list. Returns true if new IP addresses were
477 // detected. Passing true to already_changed skips this check.
478 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
479 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 15:03:05 +0200480 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200481 // Clear the network's list of addresses.
482 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800483 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-12 22:51:55 -0700484 // addresses of host candidates by mDNS names in ICE gathering. After a
485 // name-address mapping is created by the mDNS responder, queries for the
486 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800487 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200488
489 // Returns the scope-id of the network's address.
490 // Should only be relevant for link-local IPv6 addresses.
491 int scope_id() const { return scope_id_; }
492 void set_scope_id(int id) { scope_id_ = id; }
493
494 // Indicates whether this network should be ignored, perhaps because
495 // the IP is 0, or the interface is one we know is invalid.
496 bool ignored() const { return ignored_; }
497 void set_ignored(bool ignored) { ignored_ = ignored; }
498
499 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700500 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
501 // network interface used by the VPN, typically the preferred network type
502 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
503 // on https://developer.android.com/reference/android/net/VpnService.html).
504 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
505 // returned.
506 AdapterType underlying_type_for_vpn() const {
507 return underlying_type_for_vpn_;
508 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200509 void set_type(AdapterType type) {
510 if (type_ == type) {
511 return;
512 }
513 type_ = type;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700514 if (type != ADAPTER_TYPE_VPN) {
515 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
516 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200517 SignalTypeChanged(this);
518 }
519
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700520 void set_underlying_type_for_vpn(AdapterType type) {
521 if (underlying_type_for_vpn_ == type) {
522 return;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200523 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700524 underlying_type_for_vpn_ = type;
525 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200526 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700527
528 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
529
Jonas Orelandc7ea04a2020-04-03 10:12:28 +0200530 bool IsCellular() const { return IsCellular(type_); }
531
532 static bool IsCellular(AdapterType type) {
533 switch (type) {
Jonas Oreland08d18062020-04-02 07:19:12 +0200534 case ADAPTER_TYPE_CELLULAR:
535 case ADAPTER_TYPE_CELLULAR_2G:
536 case ADAPTER_TYPE_CELLULAR_3G:
537 case ADAPTER_TYPE_CELLULAR_4G:
538 case ADAPTER_TYPE_CELLULAR_5G:
539 return true;
540 default:
541 return false;
542 }
543 }
544
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200545 // Note: This function is called "rarely".
546 // Twice per Network in BasicPortAllocator if
547 // PORTALLOCATOR_DISABLE_COSTLY_NETWORKS. Once in Port::Construct() (and when
548 // Port::OnNetworkTypeChanged is called).
549 ABSL_DEPRECATED(
550 "Use the version with field trials, see bugs.webrtc.org/webrtc:10335")
551 uint16_t GetCost(
552 const webrtc::WebRtcKeyValueConfig* field_trials = nullptr) const;
553 uint16_t GetCost(const webrtc::WebRtcKeyValueConfig& field_trials) const;
554
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200555 // A unique id assigned by the network manager, which may be signaled
556 // to the remote side in the candidate.
557 uint16_t id() const { return id_; }
558 void set_id(uint16_t id) { id_ = id; }
559
560 int preference() const { return preference_; }
561 void set_preference(int preference) { preference_ = preference; }
562
563 // When we enumerate networks and find a previously-seen network is missing,
564 // we do not remove it (because it may be used elsewhere). Instead, we mark
565 // it inactive, so that we can detect network changes properly.
566 bool active() const { return active_; }
567 void set_active(bool active) {
568 if (active_ != active) {
569 active_ = active;
570 }
571 }
572
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200573 // Property set by operating system/firmware that has information
574 // about connection strength to e.g WIFI router or CELL base towers.
575 NetworkPreference network_preference() const { return network_preference_; }
576 void set_network_preference(NetworkPreference val) {
577 if (network_preference_ == val) {
578 return;
579 }
580 network_preference_ = val;
581 SignalNetworkPreferenceChanged(this);
582 }
583
Jonas Oreland30019052022-01-28 14:11:44 +0100584 static std::pair<rtc::AdapterType, bool /* vpn */>
585 GuessAdapterFromNetworkCost(int network_cost);
586
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200587 // Debugging description of this network
588 std::string ToString() const;
589
590 private:
591 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800592 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200593 std::string name_;
594 std::string description_;
595 IPAddress prefix_;
596 int prefix_length_;
597 std::string key_;
598 std::vector<InterfaceAddress> ips_;
599 int scope_id_;
600 bool ignored_;
601 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700602 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200603 int preference_;
604 bool active_ = true;
605 uint16_t id_ = 0;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200606 NetworkPreference network_preference_ = NetworkPreference::NEUTRAL;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200607
608 friend class NetworkManager;
609};
610
611} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000612
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200613#endif // RTC_BASE_NETWORK_H_