blob: 063e25596cc99be27efd5383d62b0477ced49ca8 [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"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-12 22:51:55 -070026#include "rtc_base/mdns_responder_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "rtc_base/network_monitor.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070028#include "rtc_base/network_monitor_factory.h"
Niels Mölleraa373162021-09-28 16:09:07 +020029#include "rtc_base/socket_factory.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020030#include "rtc_base/system/rtc_export.h"
Niels Möller27c14522022-02-07 16:44:21 +010031#include "rtc_base/task_utils/pending_task_safety_flag.h"
Artem Titove41c4332018-07-25 15:04:28 +020032#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070033#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034
35#if defined(WEBRTC_POSIX)
36struct ifaddrs;
37#endif // defined(WEBRTC_POSIX)
38
39namespace rtc {
40
41extern const char kPublicIPv4Host[];
42extern const char kPublicIPv6Host[];
43
44class IfAddrsConverter;
45class Network;
46class NetworkMonitorInterface;
47class Thread;
48
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020049// By default, ignore loopback interfaces on the host.
50const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
51
52// Makes a string key for this network. Used in the network manager's maps.
53// Network objects are keyed on interface name, network prefix and the
54// length of that prefix.
Ali Tofigh7fa90572022-03-17 15:47:49 +010055std::string MakeNetworkKey(absl::string_view name,
Yves Gerey665174f2018-06-19 15:03:05 +020056 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057 int prefix_length);
58
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080059// Utility function that attempts to determine an adapter type by an interface
60// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
61// mechanisms fail to determine the type.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020062RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name);
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080063
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064class DefaultLocalAddressProvider {
65 public:
66 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 15:46:07 -080067
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 // The default local address is the local address used in multi-homed endpoint
69 // when the any address (0.0.0.0 or ::) is used as the local address. It's
70 // important to check the return value as a IP family may not be enabled.
71 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
72};
73
Qingsi Wang5ae259e2019-02-13 15:46:07 -080074class MdnsResponderProvider {
75 public:
76 virtual ~MdnsResponderProvider() = default;
77
78 // Returns the mDNS responder that can be used to obfuscate the local IP
79 // addresses of ICE host candidates by mDNS hostnames.
80 //
81 // The provider MUST outlive the mDNS responder.
82 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
83};
84
Jonas Oreland2ee0e642021-08-25 15:43:02 +020085// Network/mask in CIDR representation.
86class NetworkMask {
87 public:
88 NetworkMask(const IPAddress& addr, int prefix_length)
89 : address_(addr), prefix_length_(prefix_length) {}
90
91 const IPAddress& address() const { return address_; }
92 int prefix_length() const { return prefix_length_; }
93
94 bool operator==(const NetworkMask& o) const {
95 return address_ == o.address_ && prefix_length_ == o.prefix_length_;
96 }
97
98 private:
99 IPAddress address_;
100 // Length of valid bits in address_ (for ipv4 valid range is 0-32)
101 int prefix_length_;
102};
103
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200104// Generic network manager interface. It provides list of local
105// networks.
106//
107// Every method of NetworkManager (including the destructor) must be called on
108// the same thread, except for the constructor which may be called on any
109// thread.
110//
111// This allows constructing a NetworkManager subclass on one thread and
112// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200113class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
114 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115 public:
116 typedef std::vector<Network*> NetworkList;
117
118 // This enum indicates whether adapter enumeration is allowed.
119 enum EnumerationPermission {
120 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
121 // from GetNetworks means that there is no network
122 // available.
123 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
124 // GetAnyAddressNetworks() should be used instead.
125 };
126
127 NetworkManager();
128 ~NetworkManager() override;
129
130 // Called when network list is updated.
131 sigslot::signal0<> SignalNetworksChanged;
132
133 // Indicates a failure when getting list of network interfaces.
134 sigslot::signal0<> SignalError;
135
136 // This should be called on the NetworkManager's thread before the
137 // NetworkManager is used. Subclasses may override this if necessary.
138 virtual void Initialize() {}
139
140 // Start/Stop monitoring of network interfaces
141 // list. SignalNetworksChanged or SignalError is emitted immediately
142 // after StartUpdating() is called. After that SignalNetworksChanged
143 // is emitted whenever list of networks changes.
144 virtual void StartUpdating() = 0;
145 virtual void StopUpdating() = 0;
146
147 // Returns the current list of networks available on this machine.
148 // StartUpdating() must be called before this method is called.
149 // It makes sure that repeated calls return the same object for a
150 // given network, so that quality is tracked appropriately. Does not
151 // include ignored networks.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100152 virtual std::vector<const Network*> GetNetworks() const {
153 std::vector<Network*> networks;
154 std::vector<const Network*> const_networks;
155 GetNetworks(&networks);
156 const_networks.insert(const_networks.begin(), networks.begin(),
157 networks.end());
158 return const_networks;
159 }
160 // TODO(bugs.webrtc.org/13869): Delete this overload.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200161 virtual void GetNetworks(NetworkList* networks) const = 0;
162
Qingsi Wang09619332018-09-12 22:51:55 -0700163 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200164 virtual EnumerationPermission enumeration_permission() const;
165
166 // "AnyAddressNetwork" is a network which only contains single "any address"
167 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
168 // useful as binding to such interfaces allow default routing behavior like
169 // http traffic.
170 //
171 // This method appends the "any address" networks to the list, such that this
172 // can optionally be called after GetNetworks.
173 //
174 // TODO(guoweis): remove this body when chromium implements this.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100175 virtual std::vector<const Network*> GetAnyAddressNetworks() {
176 std::vector<Network*> networks;
177 std::vector<const Network*> const_networks;
178 GetAnyAddressNetworks(&networks);
179 const_networks.insert(const_networks.begin(), networks.begin(),
180 networks.end());
181 return const_networks;
182 }
183 // TODO(bugs.webrtc.org/13869): Delete this overload.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200184 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
185
186 // Dumps the current list of networks in the network manager.
187 virtual void DumpNetworks() {}
188 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
189
190 struct Stats {
191 int ipv4_network_count;
192 int ipv6_network_count;
193 Stats() {
194 ipv4_network_count = 0;
195 ipv6_network_count = 0;
196 }
197 };
Qingsi Wang09619332018-09-12 22:51:55 -0700198
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800199 // MdnsResponderProvider interface.
200 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200201
202 virtual void set_vpn_list(const std::vector<NetworkMask>& vpn) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200203};
204
205// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200206class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200207 public:
208 NetworkManagerBase();
209 ~NetworkManagerBase() override;
210
211 void GetNetworks(NetworkList* networks) const override;
212 void GetAnyAddressNetworks(NetworkList* networks) override;
deadbeef3427f532017-07-26 16:09:33 -0700213
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200214 EnumerationPermission enumeration_permission() const override;
215
216 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
217
Jonas Orelandac554eb2021-08-27 09:43:38 +0200218 // Check if MAC address in |bytes| is one of the pre-defined
219 // MAC addresses for know VPNs.
220 static bool IsVpnMacAddress(rtc::ArrayView<const uint8_t> address);
221
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200222 protected:
223 typedef std::map<std::string, Network*> NetworkMap;
Artem Titov96e3b992021-07-26 16:03:14 +0200224 // Updates `networks_` with the networks listed in `list`. If
225 // `network_map_` already has a Network object for a network listed
226 // in the `list` then it is reused. Accept ownership of the Network
227 // objects in the `list`. `changed` will be set to true if there is
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200228 // any change in the network list.
229 void MergeNetworkList(const NetworkList& list, bool* changed);
230
Artem Titov96e3b992021-07-26 16:03:14 +0200231 // `stats` will be populated even if |*changed| is false.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200232 void MergeNetworkList(const NetworkList& list,
233 bool* changed,
234 NetworkManager::Stats* stats);
235
236 void set_enumeration_permission(EnumerationPermission state) {
237 enumeration_permission_ = state;
238 }
239
240 void set_default_local_addresses(const IPAddress& ipv4,
241 const IPAddress& ipv6);
242
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000243 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
244
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200245 private:
246 friend class NetworkTest;
247
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200248 EnumerationPermission enumeration_permission_;
249
250 NetworkList networks_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200251
252 NetworkMap networks_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200253
254 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
255 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
256
257 IPAddress default_local_ipv4_address_;
258 IPAddress default_local_ipv6_address_;
259 // We use 16 bits to save the bandwidth consumption when sending the network
260 // id over the Internet. It is OK that the 16-bit integer overflows to get a
261 // network id 0 because we only compare the network ids in the old and the new
262 // best connections in the transport channel.
263 uint16_t next_available_network_id_ = 1;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200264
265 // True if calling network_preference() with a changed value
266 // should result in firing the SignalNetworkChanged signal.
267 bool signal_network_preference_change_ = false;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200268};
269
270// Basic implementation of the NetworkManager interface that gets list
271// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200272class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000273 public NetworkBinderInterface,
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200274 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200275 public:
Niels Möller539f3e12021-11-26 16:33:19 +0100276 ABSL_DEPRECATED(
277 "Use the version with socket_factory, see bugs.webrtc.org/13145")
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200278 BasicNetworkManager();
Niels Möller539f3e12021-11-26 16:33:19 +0100279 explicit BasicNetworkManager(SocketFactory* socket_factory);
Niels Mölleraa373162021-09-28 16:09:07 +0200280 ABSL_DEPRECATED(
281 "Use the version with socket_factory, see bugs.webrtc.org/13145")
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700282 explicit BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory);
Niels Mölleraa373162021-09-28 16:09:07 +0200283 BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory,
284 SocketFactory* socket_factory);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200285 ~BasicNetworkManager() override;
286
287 void StartUpdating() override;
288 void StopUpdating() override;
289
290 void DumpNetworks() override;
291
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200292 bool started() { return start_count_ > 0; }
293
294 // Sets the network ignore list, which is empty by default. Any network on the
295 // ignore list will be filtered from network enumeration results.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700296 // Should be called only before initialization.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200297 void set_network_ignore_list(const std::vector<std::string>& list) {
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700298 RTC_DCHECK(thread_ == nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200299 network_ignore_list_ = list;
300 }
301
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200302 // Set a list of manually configured VPN's.
303 void set_vpn_list(const std::vector<NetworkMask>& vpn) override;
304
305 // Check if |prefix| is configured as VPN.
306 bool IsConfiguredVpn(IPAddress prefix, int prefix_length) const;
307
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000308 // Bind a socket to interface that ip address belong to.
309 // Implementation look up interface name and calls
310 // BindSocketToNetwork on NetworkMonitor.
311 // The interface name is needed as e.g ipv4 over ipv6 addresses
312 // are not exposed using Android functions, but it is possible
313 // bind an ipv4 address to the interface.
314 NetworkBindingResult BindSocketToNetwork(int socket_fd,
315 const IPAddress& address) override;
316
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200317 protected:
318#if defined(WEBRTC_POSIX)
319 // Separated from CreateNetworks for tests.
320 void ConvertIfAddrs(ifaddrs* interfaces,
321 IfAddrsConverter* converter,
322 bool include_ignored,
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700323 NetworkList* networks) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200324#endif // defined(WEBRTC_POSIX)
325
326 // Creates a network object for each network available on the machine.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700327 bool CreateNetworks(bool include_ignored, NetworkList* networks) const
328 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200329
330 // Determines if a network should be ignored. This should only be determined
331 // based on the network's property instead of any individual IP.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700332 bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200333
334 // This function connects a UDP socket to a public address and returns the
335 // local address associated it. Since it binds to the "any" address
336 // internally, it returns the default local address on a multi-homed endpoint.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700337 IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200338
339 private:
340 friend class NetworkTest;
341
342 // Creates a network monitor and listens for network updates.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700343 void StartNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200344 // Stops and removes the network monitor.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700345 void StopNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200346 // Called when it receives updates from the network monitor.
347 void OnNetworksChanged();
348
349 // Updates the networks and reschedules the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700350 void UpdateNetworksContinually() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200351 // Only updates the networks; does not reschedule the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700352 void UpdateNetworksOnce() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200353
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700354 Thread* thread_ = nullptr;
355 bool sent_first_update_ = true;
356 int start_count_ = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200357 std::vector<std::string> network_ignore_list_;
Niels Mölleraa373162021-09-28 16:09:07 +0200358 NetworkMonitorFactory* const network_monitor_factory_;
359 SocketFactory* const socket_factory_;
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700360 std::unique_ptr<NetworkMonitorInterface> network_monitor_
361 RTC_GUARDED_BY(thread_);
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000362 bool allow_mac_based_ipv6_ RTC_GUARDED_BY(thread_) = false;
363 bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200364
365 std::vector<NetworkMask> vpn_;
Niels Möller27c14522022-02-07 16:44:21 +0100366 rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> task_safety_flag_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200367};
368
369// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200370class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200371 public:
Ali Tofigh7fa90572022-03-17 15:47:49 +0100372 Network(absl::string_view name,
373 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200374 const IPAddress& prefix,
375 int prefix_length);
376
Ali Tofigh7fa90572022-03-17 15:47:49 +0100377 Network(absl::string_view name,
378 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200379 const IPAddress& prefix,
380 int prefix_length,
381 AdapterType type);
Steve Anton9de3aac2017-10-24 10:08:26 -0700382 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200383 ~Network();
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200384
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700385 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100386 // Mutable, to support connecting on the const Network passed to cricket::Port
387 // constructor.
388 mutable sigslot::signal1<const Network*> SignalTypeChanged;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200389
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200390 // This signal is fired whenever network preference changes.
391 sigslot::signal1<const Network*> SignalNetworkPreferenceChanged;
392
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100393 const DefaultLocalAddressProvider* default_local_address_provider() const {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200394 return default_local_address_provider_;
395 }
396 void set_default_local_address_provider(
397 const DefaultLocalAddressProvider* provider) {
398 default_local_address_provider_ = provider;
399 }
400
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800401 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
402 mdns_responder_provider_ = provider;
403 }
404
405 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200406 const std::string& name() const { return name_; }
407
408 // Returns the OS-assigned name for this network. This is useful for
409 // debugging but should not be sent over the wire (for privacy reasons).
410 const std::string& description() const { return description_; }
411
412 // Returns the prefix for this network.
413 const IPAddress& prefix() const { return prefix_; }
414 // Returns the length, in bits, of this network's prefix.
415 int prefix_length() const { return prefix_length_; }
416
Artem Titov96e3b992021-07-26 16:03:14 +0200417 // `key_` has unique value per network interface. Used in sorting network
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200418 // interfaces. Key is derived from interface name and it's prefix.
419 std::string key() const { return key_; }
420
421 // Returns the Network's current idea of the 'best' IP it has.
422 // Or return an unset IP if this network has no active addresses.
423 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800424 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200425 // 2) if #1 not available, return global ones.
426 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
427 // for unique local address, which is not route-able in open
428 // internet but might be useful for a close WebRTC deployment.
429
430 // TODO(guoweis): rule #3 actually won't happen at current
431 // implementation. The reason being that ULA address starting with
432 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
433 // that is WebRTC will have one extra Network to generate candidates
434 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
435 // ULA should only be tried in a close deployment anyway.
436
437 // Note that when not specifying any flag, it's treated as case global
438 // IPv6 address
439 IPAddress GetBestIP() const;
440
441 // Keep the original function here for now.
442 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
443 IPAddress ip() const { return GetBestIP(); }
444
445 // Adds an active IP address to this network. Does not check for duplicates.
446 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800447 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200448
449 // Sets the network's IP address list. Returns true if new IP addresses were
450 // detected. Passing true to already_changed skips this check.
451 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
452 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 15:03:05 +0200453 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200454 // Clear the network's list of addresses.
455 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800456 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-12 22:51:55 -0700457 // addresses of host candidates by mDNS names in ICE gathering. After a
458 // name-address mapping is created by the mDNS responder, queries for the
459 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800460 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200461
462 // Returns the scope-id of the network's address.
463 // Should only be relevant for link-local IPv6 addresses.
464 int scope_id() const { return scope_id_; }
465 void set_scope_id(int id) { scope_id_ = id; }
466
467 // Indicates whether this network should be ignored, perhaps because
468 // the IP is 0, or the interface is one we know is invalid.
469 bool ignored() const { return ignored_; }
470 void set_ignored(bool ignored) { ignored_ = ignored; }
471
472 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700473 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
474 // network interface used by the VPN, typically the preferred network type
475 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
476 // on https://developer.android.com/reference/android/net/VpnService.html).
477 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
478 // returned.
479 AdapterType underlying_type_for_vpn() const {
480 return underlying_type_for_vpn_;
481 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200482 void set_type(AdapterType type) {
483 if (type_ == type) {
484 return;
485 }
486 type_ = type;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700487 if (type != ADAPTER_TYPE_VPN) {
488 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
489 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200490 SignalTypeChanged(this);
491 }
492
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700493 void set_underlying_type_for_vpn(AdapterType type) {
494 if (underlying_type_for_vpn_ == type) {
495 return;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200496 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700497 underlying_type_for_vpn_ = type;
498 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200499 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700500
501 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
502
Jonas Orelandc7ea04a2020-04-03 10:12:28 +0200503 bool IsCellular() const { return IsCellular(type_); }
504
505 static bool IsCellular(AdapterType type) {
506 switch (type) {
Jonas Oreland08d18062020-04-02 07:19:12 +0200507 case ADAPTER_TYPE_CELLULAR:
508 case ADAPTER_TYPE_CELLULAR_2G:
509 case ADAPTER_TYPE_CELLULAR_3G:
510 case ADAPTER_TYPE_CELLULAR_4G:
511 case ADAPTER_TYPE_CELLULAR_5G:
512 return true;
513 default:
514 return false;
515 }
516 }
517
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700518 uint16_t GetCost() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200519 // A unique id assigned by the network manager, which may be signaled
520 // to the remote side in the candidate.
521 uint16_t id() const { return id_; }
522 void set_id(uint16_t id) { id_ = id; }
523
524 int preference() const { return preference_; }
525 void set_preference(int preference) { preference_ = preference; }
526
527 // When we enumerate networks and find a previously-seen network is missing,
528 // we do not remove it (because it may be used elsewhere). Instead, we mark
529 // it inactive, so that we can detect network changes properly.
530 bool active() const { return active_; }
531 void set_active(bool active) {
532 if (active_ != active) {
533 active_ = active;
534 }
535 }
536
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200537 // Property set by operating system/firmware that has information
538 // about connection strength to e.g WIFI router or CELL base towers.
539 NetworkPreference network_preference() const { return network_preference_; }
540 void set_network_preference(NetworkPreference val) {
541 if (network_preference_ == val) {
542 return;
543 }
544 network_preference_ = val;
545 SignalNetworkPreferenceChanged(this);
546 }
547
Jonas Oreland30019052022-01-28 14:11:44 +0100548 static std::pair<rtc::AdapterType, bool /* vpn */>
549 GuessAdapterFromNetworkCost(int network_cost);
550
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200551 // Debugging description of this network
552 std::string ToString() const;
553
554 private:
555 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800556 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200557 std::string name_;
558 std::string description_;
559 IPAddress prefix_;
560 int prefix_length_;
561 std::string key_;
562 std::vector<InterfaceAddress> ips_;
563 int scope_id_;
564 bool ignored_;
565 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700566 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200567 int preference_;
568 bool active_ = true;
569 uint16_t id_ = 0;
Jonas Oreland2105d642020-05-13 10:15:34 +0200570 bool use_differentiated_cellular_costs_ = false;
Jonas Orelandb477fc72021-08-23 12:16:33 +0200571 bool add_network_cost_to_vpn_ = false;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200572 NetworkPreference network_preference_ = NetworkPreference::NEUTRAL;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200573
574 friend class NetworkManager;
575};
576
577} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000578
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200579#endif // RTC_BASE_NETWORK_H_