blob: 0b462bdedec9a0dd9e818120973ad48fa5970690 [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
Jonas Orelandac554eb2021-08-27 09:43:38 +020022#include "api/array_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010023#include "api/sequence_checker.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-12 22:51:55 -070025#include "rtc_base/mdns_responder_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "rtc_base/message_handler.h"
27#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"
Artem Titove41c4332018-07-25 15:04:28 +020031#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070032#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033
34#if defined(WEBRTC_POSIX)
35struct ifaddrs;
36#endif // defined(WEBRTC_POSIX)
37
38namespace rtc {
39
40extern const char kPublicIPv4Host[];
41extern const char kPublicIPv6Host[];
42
43class IfAddrsConverter;
44class Network;
45class NetworkMonitorInterface;
46class Thread;
47
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020048// By default, ignore loopback interfaces on the host.
49const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
50
51// Makes a string key for this network. Used in the network manager's maps.
52// Network objects are keyed on interface name, network prefix and the
53// length of that prefix.
Yves Gerey665174f2018-06-19 15:03:05 +020054std::string MakeNetworkKey(const std::string& name,
55 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056 int prefix_length);
57
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080058// Utility function that attempts to determine an adapter type by an interface
59// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
60// mechanisms fail to determine the type.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020061RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name);
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080062
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063class DefaultLocalAddressProvider {
64 public:
65 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 15:46:07 -080066
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020067 // The default local address is the local address used in multi-homed endpoint
68 // when the any address (0.0.0.0 or ::) is used as the local address. It's
69 // important to check the return value as a IP family may not be enabled.
70 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
71};
72
Qingsi Wang5ae259e2019-02-13 15:46:07 -080073class MdnsResponderProvider {
74 public:
75 virtual ~MdnsResponderProvider() = default;
76
77 // Returns the mDNS responder that can be used to obfuscate the local IP
78 // addresses of ICE host candidates by mDNS hostnames.
79 //
80 // The provider MUST outlive the mDNS responder.
81 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
82};
83
Jonas Oreland2ee0e642021-08-25 15:43:02 +020084// Network/mask in CIDR representation.
85class NetworkMask {
86 public:
87 NetworkMask(const IPAddress& addr, int prefix_length)
88 : address_(addr), prefix_length_(prefix_length) {}
89
90 const IPAddress& address() const { return address_; }
91 int prefix_length() const { return prefix_length_; }
92
93 bool operator==(const NetworkMask& o) const {
94 return address_ == o.address_ && prefix_length_ == o.prefix_length_;
95 }
96
97 private:
98 IPAddress address_;
99 // Length of valid bits in address_ (for ipv4 valid range is 0-32)
100 int prefix_length_;
101};
102
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103// Generic network manager interface. It provides list of local
104// networks.
105//
106// Every method of NetworkManager (including the destructor) must be called on
107// the same thread, except for the constructor which may be called on any
108// thread.
109//
110// This allows constructing a NetworkManager subclass on one thread and
111// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200112class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
113 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 public:
115 typedef std::vector<Network*> NetworkList;
116
117 // This enum indicates whether adapter enumeration is allowed.
118 enum EnumerationPermission {
119 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
120 // from GetNetworks means that there is no network
121 // available.
122 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
123 // GetAnyAddressNetworks() should be used instead.
124 };
125
126 NetworkManager();
127 ~NetworkManager() override;
128
129 // Called when network list is updated.
130 sigslot::signal0<> SignalNetworksChanged;
131
132 // Indicates a failure when getting list of network interfaces.
133 sigslot::signal0<> SignalError;
134
135 // This should be called on the NetworkManager's thread before the
136 // NetworkManager is used. Subclasses may override this if necessary.
137 virtual void Initialize() {}
138
139 // Start/Stop monitoring of network interfaces
140 // list. SignalNetworksChanged or SignalError is emitted immediately
141 // after StartUpdating() is called. After that SignalNetworksChanged
142 // is emitted whenever list of networks changes.
143 virtual void StartUpdating() = 0;
144 virtual void StopUpdating() = 0;
145
146 // Returns the current list of networks available on this machine.
147 // StartUpdating() must be called before this method is called.
148 // It makes sure that repeated calls return the same object for a
149 // given network, so that quality is tracked appropriately. Does not
150 // include ignored networks.
151 virtual void GetNetworks(NetworkList* networks) const = 0;
152
Qingsi Wang09619332018-09-12 22:51:55 -0700153 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200154 virtual EnumerationPermission enumeration_permission() const;
155
156 // "AnyAddressNetwork" is a network which only contains single "any address"
157 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
158 // useful as binding to such interfaces allow default routing behavior like
159 // http traffic.
160 //
161 // This method appends the "any address" networks to the list, such that this
162 // can optionally be called after GetNetworks.
163 //
164 // TODO(guoweis): remove this body when chromium implements this.
165 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
166
167 // Dumps the current list of networks in the network manager.
168 virtual void DumpNetworks() {}
169 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
170
171 struct Stats {
172 int ipv4_network_count;
173 int ipv6_network_count;
174 Stats() {
175 ipv4_network_count = 0;
176 ipv6_network_count = 0;
177 }
178 };
Qingsi Wang09619332018-09-12 22:51:55 -0700179
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800180 // MdnsResponderProvider interface.
181 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200182
183 virtual void set_vpn_list(const std::vector<NetworkMask>& vpn) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200184};
185
186// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200187class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200188 public:
189 NetworkManagerBase();
190 ~NetworkManagerBase() override;
191
192 void GetNetworks(NetworkList* networks) const override;
193 void GetAnyAddressNetworks(NetworkList* networks) override;
deadbeef3427f532017-07-26 16:09:33 -0700194
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200195 EnumerationPermission enumeration_permission() const override;
196
197 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
198
Jonas Orelandac554eb2021-08-27 09:43:38 +0200199 // Check if MAC address in |bytes| is one of the pre-defined
200 // MAC addresses for know VPNs.
201 static bool IsVpnMacAddress(rtc::ArrayView<const uint8_t> address);
202
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200203 protected:
204 typedef std::map<std::string, Network*> NetworkMap;
Artem Titov96e3b992021-07-26 16:03:14 +0200205 // Updates `networks_` with the networks listed in `list`. If
206 // `network_map_` already has a Network object for a network listed
207 // in the `list` then it is reused. Accept ownership of the Network
208 // objects in the `list`. `changed` will be set to true if there is
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200209 // any change in the network list.
210 void MergeNetworkList(const NetworkList& list, bool* changed);
211
Artem Titov96e3b992021-07-26 16:03:14 +0200212 // `stats` will be populated even if |*changed| is false.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200213 void MergeNetworkList(const NetworkList& list,
214 bool* changed,
215 NetworkManager::Stats* stats);
216
217 void set_enumeration_permission(EnumerationPermission state) {
218 enumeration_permission_ = state;
219 }
220
221 void set_default_local_addresses(const IPAddress& ipv4,
222 const IPAddress& ipv6);
223
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000224 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
225
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200226 private:
227 friend class NetworkTest;
228
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200229 EnumerationPermission enumeration_permission_;
230
231 NetworkList networks_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200232
233 NetworkMap networks_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200234
235 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
236 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
237
238 IPAddress default_local_ipv4_address_;
239 IPAddress default_local_ipv6_address_;
240 // We use 16 bits to save the bandwidth consumption when sending the network
241 // id over the Internet. It is OK that the 16-bit integer overflows to get a
242 // network id 0 because we only compare the network ids in the old and the new
243 // best connections in the transport channel.
244 uint16_t next_available_network_id_ = 1;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200245
246 // True if calling network_preference() with a changed value
247 // should result in firing the SignalNetworkChanged signal.
248 bool signal_network_preference_change_ = false;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200249};
250
251// Basic implementation of the NetworkManager interface that gets list
252// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200253class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +0200254 public MessageHandlerAutoCleanup,
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000255 public NetworkBinderInterface,
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200256 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200257 public:
Niels Möller539f3e12021-11-26 16:33:19 +0100258 ABSL_DEPRECATED(
259 "Use the version with socket_factory, see bugs.webrtc.org/13145")
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200260 BasicNetworkManager();
Niels Möller539f3e12021-11-26 16:33:19 +0100261 explicit BasicNetworkManager(SocketFactory* socket_factory);
Niels Mölleraa373162021-09-28 16:09:07 +0200262 ABSL_DEPRECATED(
263 "Use the version with socket_factory, see bugs.webrtc.org/13145")
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700264 explicit BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory);
Niels Mölleraa373162021-09-28 16:09:07 +0200265 BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory,
266 SocketFactory* socket_factory);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200267 ~BasicNetworkManager() override;
268
269 void StartUpdating() override;
270 void StopUpdating() override;
271
272 void DumpNetworks() override;
273
274 // MessageHandler interface.
275 void OnMessage(Message* msg) override;
276 bool started() { return start_count_ > 0; }
277
278 // Sets the network ignore list, which is empty by default. Any network on the
279 // ignore list will be filtered from network enumeration results.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700280 // Should be called only before initialization.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200281 void set_network_ignore_list(const std::vector<std::string>& list) {
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700282 RTC_DCHECK(thread_ == nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200283 network_ignore_list_ = list;
284 }
285
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200286 // Set a list of manually configured VPN's.
287 void set_vpn_list(const std::vector<NetworkMask>& vpn) override;
288
289 // Check if |prefix| is configured as VPN.
290 bool IsConfiguredVpn(IPAddress prefix, int prefix_length) const;
291
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000292 // Bind a socket to interface that ip address belong to.
293 // Implementation look up interface name and calls
294 // BindSocketToNetwork on NetworkMonitor.
295 // The interface name is needed as e.g ipv4 over ipv6 addresses
296 // are not exposed using Android functions, but it is possible
297 // bind an ipv4 address to the interface.
298 NetworkBindingResult BindSocketToNetwork(int socket_fd,
299 const IPAddress& address) override;
300
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200301 protected:
302#if defined(WEBRTC_POSIX)
303 // Separated from CreateNetworks for tests.
304 void ConvertIfAddrs(ifaddrs* interfaces,
305 IfAddrsConverter* converter,
306 bool include_ignored,
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700307 NetworkList* networks) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200308#endif // defined(WEBRTC_POSIX)
309
310 // Creates a network object for each network available on the machine.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700311 bool CreateNetworks(bool include_ignored, NetworkList* networks) const
312 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200313
314 // Determines if a network should be ignored. This should only be determined
315 // based on the network's property instead of any individual IP.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700316 bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200317
318 // This function connects a UDP socket to a public address and returns the
319 // local address associated it. Since it binds to the "any" address
320 // internally, it returns the default local address on a multi-homed endpoint.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700321 IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200322
323 private:
324 friend class NetworkTest;
325
326 // Creates a network monitor and listens for network updates.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700327 void StartNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200328 // Stops and removes the network monitor.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700329 void StopNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200330 // Called when it receives updates from the network monitor.
331 void OnNetworksChanged();
332
333 // Updates the networks and reschedules the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700334 void UpdateNetworksContinually() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200335 // Only updates the networks; does not reschedule the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700336 void UpdateNetworksOnce() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200337
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700338 Thread* thread_ = nullptr;
339 bool sent_first_update_ = true;
340 int start_count_ = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200341 std::vector<std::string> network_ignore_list_;
Niels Mölleraa373162021-09-28 16:09:07 +0200342 NetworkMonitorFactory* const network_monitor_factory_;
343 SocketFactory* const socket_factory_;
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700344 std::unique_ptr<NetworkMonitorInterface> network_monitor_
345 RTC_GUARDED_BY(thread_);
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000346 bool allow_mac_based_ipv6_ RTC_GUARDED_BY(thread_) = false;
347 bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200348
349 std::vector<NetworkMask> vpn_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200350};
351
352// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200353class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200354 public:
355 Network(const std::string& name,
356 const std::string& description,
357 const IPAddress& prefix,
358 int prefix_length);
359
360 Network(const std::string& name,
361 const std::string& description,
362 const IPAddress& prefix,
363 int prefix_length,
364 AdapterType type);
Steve Anton9de3aac2017-10-24 10:08:26 -0700365 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200366 ~Network();
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200367
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700368 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200369 sigslot::signal1<const Network*> SignalTypeChanged;
370
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200371 // This signal is fired whenever network preference changes.
372 sigslot::signal1<const Network*> SignalNetworkPreferenceChanged;
373
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200374 const DefaultLocalAddressProvider* default_local_address_provider() {
375 return default_local_address_provider_;
376 }
377 void set_default_local_address_provider(
378 const DefaultLocalAddressProvider* provider) {
379 default_local_address_provider_ = provider;
380 }
381
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800382 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
383 mdns_responder_provider_ = provider;
384 }
385
386 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200387 const std::string& name() const { return name_; }
388
389 // Returns the OS-assigned name for this network. This is useful for
390 // debugging but should not be sent over the wire (for privacy reasons).
391 const std::string& description() const { return description_; }
392
393 // Returns the prefix for this network.
394 const IPAddress& prefix() const { return prefix_; }
395 // Returns the length, in bits, of this network's prefix.
396 int prefix_length() const { return prefix_length_; }
397
Artem Titov96e3b992021-07-26 16:03:14 +0200398 // `key_` has unique value per network interface. Used in sorting network
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200399 // interfaces. Key is derived from interface name and it's prefix.
400 std::string key() const { return key_; }
401
402 // Returns the Network's current idea of the 'best' IP it has.
403 // Or return an unset IP if this network has no active addresses.
404 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800405 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200406 // 2) if #1 not available, return global ones.
407 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
408 // for unique local address, which is not route-able in open
409 // internet but might be useful for a close WebRTC deployment.
410
411 // TODO(guoweis): rule #3 actually won't happen at current
412 // implementation. The reason being that ULA address starting with
413 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
414 // that is WebRTC will have one extra Network to generate candidates
415 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
416 // ULA should only be tried in a close deployment anyway.
417
418 // Note that when not specifying any flag, it's treated as case global
419 // IPv6 address
420 IPAddress GetBestIP() const;
421
422 // Keep the original function here for now.
423 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
424 IPAddress ip() const { return GetBestIP(); }
425
426 // Adds an active IP address to this network. Does not check for duplicates.
427 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800428 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200429
430 // Sets the network's IP address list. Returns true if new IP addresses were
431 // detected. Passing true to already_changed skips this check.
432 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
433 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 15:03:05 +0200434 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200435 // Clear the network's list of addresses.
436 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800437 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-12 22:51:55 -0700438 // addresses of host candidates by mDNS names in ICE gathering. After a
439 // name-address mapping is created by the mDNS responder, queries for the
440 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800441 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200442
443 // Returns the scope-id of the network's address.
444 // Should only be relevant for link-local IPv6 addresses.
445 int scope_id() const { return scope_id_; }
446 void set_scope_id(int id) { scope_id_ = id; }
447
448 // Indicates whether this network should be ignored, perhaps because
449 // the IP is 0, or the interface is one we know is invalid.
450 bool ignored() const { return ignored_; }
451 void set_ignored(bool ignored) { ignored_ = ignored; }
452
453 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700454 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
455 // network interface used by the VPN, typically the preferred network type
456 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
457 // on https://developer.android.com/reference/android/net/VpnService.html).
458 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
459 // returned.
460 AdapterType underlying_type_for_vpn() const {
461 return underlying_type_for_vpn_;
462 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200463 void set_type(AdapterType type) {
464 if (type_ == type) {
465 return;
466 }
467 type_ = type;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700468 if (type != ADAPTER_TYPE_VPN) {
469 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
470 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200471 SignalTypeChanged(this);
472 }
473
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700474 void set_underlying_type_for_vpn(AdapterType type) {
475 if (underlying_type_for_vpn_ == type) {
476 return;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200477 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700478 underlying_type_for_vpn_ = type;
479 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200480 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700481
482 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
483
Jonas Orelandc7ea04a2020-04-03 10:12:28 +0200484 bool IsCellular() const { return IsCellular(type_); }
485
486 static bool IsCellular(AdapterType type) {
487 switch (type) {
Jonas Oreland08d18062020-04-02 07:19:12 +0200488 case ADAPTER_TYPE_CELLULAR:
489 case ADAPTER_TYPE_CELLULAR_2G:
490 case ADAPTER_TYPE_CELLULAR_3G:
491 case ADAPTER_TYPE_CELLULAR_4G:
492 case ADAPTER_TYPE_CELLULAR_5G:
493 return true;
494 default:
495 return false;
496 }
497 }
498
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700499 uint16_t GetCost() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200500 // A unique id assigned by the network manager, which may be signaled
501 // to the remote side in the candidate.
502 uint16_t id() const { return id_; }
503 void set_id(uint16_t id) { id_ = id; }
504
505 int preference() const { return preference_; }
506 void set_preference(int preference) { preference_ = preference; }
507
508 // When we enumerate networks and find a previously-seen network is missing,
509 // we do not remove it (because it may be used elsewhere). Instead, we mark
510 // it inactive, so that we can detect network changes properly.
511 bool active() const { return active_; }
512 void set_active(bool active) {
513 if (active_ != active) {
514 active_ = active;
515 }
516 }
517
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200518 // Property set by operating system/firmware that has information
519 // about connection strength to e.g WIFI router or CELL base towers.
520 NetworkPreference network_preference() const { return network_preference_; }
521 void set_network_preference(NetworkPreference val) {
522 if (network_preference_ == val) {
523 return;
524 }
525 network_preference_ = val;
526 SignalNetworkPreferenceChanged(this);
527 }
528
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200529 // Debugging description of this network
530 std::string ToString() const;
531
532 private:
533 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800534 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200535 std::string name_;
536 std::string description_;
537 IPAddress prefix_;
538 int prefix_length_;
539 std::string key_;
540 std::vector<InterfaceAddress> ips_;
541 int scope_id_;
542 bool ignored_;
543 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700544 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200545 int preference_;
546 bool active_ = true;
547 uint16_t id_ = 0;
Jonas Oreland2105d642020-05-13 10:15:34 +0200548 bool use_differentiated_cellular_costs_ = false;
Jonas Orelandb477fc72021-08-23 12:16:33 +0200549 bool add_network_cost_to_vpn_ = false;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200550 NetworkPreference network_preference_ = NetworkPreference::NEUTRAL;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200551
552 friend class NetworkManager;
553};
554
555} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000556
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200557#endif // RTC_BASE_NETWORK_H_