blob: 94350e0e4da54467aac1921fe872da1724b8f513 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#ifndef RTC_BASE_NETWORK_H_
12#define RTC_BASE_NETWORK_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <stdint.h>
pbosc7c26a02017-01-02 16:42:3215
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <deque>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
Artem Titovd15a5752021-02-10 13:31:2422#include "api/sequence_checker.h"
Steve Anton10542f22019-01-11 17:11:0023#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-13 05:51:5524#include "rtc_base/mdns_responder_interface.h"
Steve Anton10542f22019-01-11 17:11:0025#include "rtc_base/message_handler.h"
26#include "rtc_base/network_monitor.h"
Taylor Brandstetter239ac8a2020-07-31 23:07:5227#include "rtc_base/network_monitor_factory.h"
Mirko Bonadei35214fc2019-09-23 12:54:2828#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 13:04:2829#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetter239ac8a2020-07-31 23:07:5230#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5031
32#if defined(WEBRTC_POSIX)
33struct ifaddrs;
34#endif // defined(WEBRTC_POSIX)
35
36namespace rtc {
37
38extern const char kPublicIPv4Host[];
39extern const char kPublicIPv6Host[];
40
41class IfAddrsConverter;
42class Network;
43class NetworkMonitorInterface;
44class Thread;
45
Henrik Kjellanderec78f1c2017-06-29 05:52:5046// By default, ignore loopback interfaces on the host.
47const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
48
49// Makes a string key for this network. Used in the network manager's maps.
50// Network objects are keyed on interface name, network prefix and the
51// length of that prefix.
Yves Gerey665174f2018-06-19 13:03:0552std::string MakeNetworkKey(const std::string& name,
53 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 05:52:5054 int prefix_length);
55
Taylor Brandstetter8bac1d92018-01-25 01:38:0056// Utility function that attempts to determine an adapter type by an interface
57// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
58// mechanisms fail to determine the type.
Mirko Bonadei35214fc2019-09-23 12:54:2859RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name);
Taylor Brandstetter8bac1d92018-01-25 01:38:0060
Henrik Kjellanderec78f1c2017-06-29 05:52:5061class DefaultLocalAddressProvider {
62 public:
63 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 23:46:0764
Henrik Kjellanderec78f1c2017-06-29 05:52:5065 // The default local address is the local address used in multi-homed endpoint
66 // when the any address (0.0.0.0 or ::) is used as the local address. It's
67 // important to check the return value as a IP family may not be enabled.
68 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
69};
70
Qingsi Wang5ae259e2019-02-13 23:46:0771class MdnsResponderProvider {
72 public:
73 virtual ~MdnsResponderProvider() = default;
74
75 // Returns the mDNS responder that can be used to obfuscate the local IP
76 // addresses of ICE host candidates by mDNS hostnames.
77 //
78 // The provider MUST outlive the mDNS responder.
79 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
80};
81
Jonas Oreland2ee0e642021-08-25 13:43:0282// Network/mask in CIDR representation.
83class NetworkMask {
84 public:
85 NetworkMask(const IPAddress& addr, int prefix_length)
86 : address_(addr), prefix_length_(prefix_length) {}
87
88 const IPAddress& address() const { return address_; }
89 int prefix_length() const { return prefix_length_; }
90
91 bool operator==(const NetworkMask& o) const {
92 return address_ == o.address_ && prefix_length_ == o.prefix_length_;
93 }
94
95 private:
96 IPAddress address_;
97 // Length of valid bits in address_ (for ipv4 valid range is 0-32)
98 int prefix_length_;
99};
100
Henrik Kjellanderec78f1c2017-06-29 05:52:50101// Generic network manager interface. It provides list of local
102// networks.
103//
104// Every method of NetworkManager (including the destructor) must be called on
105// the same thread, except for the constructor which may be called on any
106// thread.
107//
108// This allows constructing a NetworkManager subclass on one thread and
109// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 12:54:28110class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
111 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 05:52:50112 public:
113 typedef std::vector<Network*> NetworkList;
114
115 // This enum indicates whether adapter enumeration is allowed.
116 enum EnumerationPermission {
117 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
118 // from GetNetworks means that there is no network
119 // available.
120 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
121 // GetAnyAddressNetworks() should be used instead.
122 };
123
124 NetworkManager();
125 ~NetworkManager() override;
126
127 // Called when network list is updated.
128 sigslot::signal0<> SignalNetworksChanged;
129
130 // Indicates a failure when getting list of network interfaces.
131 sigslot::signal0<> SignalError;
132
133 // This should be called on the NetworkManager's thread before the
134 // NetworkManager is used. Subclasses may override this if necessary.
135 virtual void Initialize() {}
136
137 // Start/Stop monitoring of network interfaces
138 // list. SignalNetworksChanged or SignalError is emitted immediately
139 // after StartUpdating() is called. After that SignalNetworksChanged
140 // is emitted whenever list of networks changes.
141 virtual void StartUpdating() = 0;
142 virtual void StopUpdating() = 0;
143
144 // Returns the current list of networks available on this machine.
145 // StartUpdating() must be called before this method is called.
146 // It makes sure that repeated calls return the same object for a
147 // given network, so that quality is tracked appropriately. Does not
148 // include ignored networks.
149 virtual void GetNetworks(NetworkList* networks) const = 0;
150
Qingsi Wang09619332018-09-13 05:51:55151 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 05:52:50152 virtual EnumerationPermission enumeration_permission() const;
153
154 // "AnyAddressNetwork" is a network which only contains single "any address"
155 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
156 // useful as binding to such interfaces allow default routing behavior like
157 // http traffic.
158 //
159 // This method appends the "any address" networks to the list, such that this
160 // can optionally be called after GetNetworks.
161 //
162 // TODO(guoweis): remove this body when chromium implements this.
163 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
164
165 // Dumps the current list of networks in the network manager.
166 virtual void DumpNetworks() {}
167 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
168
169 struct Stats {
170 int ipv4_network_count;
171 int ipv6_network_count;
172 Stats() {
173 ipv4_network_count = 0;
174 ipv6_network_count = 0;
175 }
176 };
Qingsi Wang09619332018-09-13 05:51:55177
Qingsi Wang5ae259e2019-02-13 23:46:07178 // MdnsResponderProvider interface.
179 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Jonas Oreland2ee0e642021-08-25 13:43:02180
181 virtual void set_vpn_list(const std::vector<NetworkMask>& vpn) {}
Henrik Kjellanderec78f1c2017-06-29 05:52:50182};
183
184// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 12:54:28185class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 05:52:50186 public:
187 NetworkManagerBase();
188 ~NetworkManagerBase() override;
189
190 void GetNetworks(NetworkList* networks) const override;
191 void GetAnyAddressNetworks(NetworkList* networks) override;
deadbeef3427f532017-07-26 23:09:33192
Henrik Kjellanderec78f1c2017-06-29 05:52:50193 EnumerationPermission enumeration_permission() const override;
194
195 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
196
197 protected:
198 typedef std::map<std::string, Network*> NetworkMap;
Artem Titov96e3b992021-07-26 14:03:14199 // Updates `networks_` with the networks listed in `list`. If
200 // `network_map_` already has a Network object for a network listed
201 // in the `list` then it is reused. Accept ownership of the Network
202 // objects in the `list`. `changed` will be set to true if there is
Henrik Kjellanderec78f1c2017-06-29 05:52:50203 // any change in the network list.
204 void MergeNetworkList(const NetworkList& list, bool* changed);
205
Artem Titov96e3b992021-07-26 14:03:14206 // `stats` will be populated even if |*changed| is false.
Henrik Kjellanderec78f1c2017-06-29 05:52:50207 void MergeNetworkList(const NetworkList& list,
208 bool* changed,
209 NetworkManager::Stats* stats);
210
211 void set_enumeration_permission(EnumerationPermission state) {
212 enumeration_permission_ = state;
213 }
214
215 void set_default_local_addresses(const IPAddress& ipv4,
216 const IPAddress& ipv6);
217
Jonas Oreland6ca955a2021-03-15 08:27:43218 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
219
Henrik Kjellanderec78f1c2017-06-29 05:52:50220 private:
221 friend class NetworkTest;
222
Henrik Kjellanderec78f1c2017-06-29 05:52:50223 EnumerationPermission enumeration_permission_;
224
225 NetworkList networks_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50226
227 NetworkMap networks_map_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50228
229 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
230 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
231
232 IPAddress default_local_ipv4_address_;
233 IPAddress default_local_ipv6_address_;
234 // We use 16 bits to save the bandwidth consumption when sending the network
235 // id over the Internet. It is OK that the 16-bit integer overflows to get a
236 // network id 0 because we only compare the network ids in the old and the new
237 // best connections in the transport channel.
238 uint16_t next_available_network_id_ = 1;
Jonas Orelandf7721fb2020-08-07 09:08:34239
240 // True if calling network_preference() with a changed value
241 // should result in firing the SignalNetworkChanged signal.
242 bool signal_network_preference_change_ = false;
Henrik Kjellanderec78f1c2017-06-29 05:52:50243};
244
245// Basic implementation of the NetworkManager interface that gets list
246// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 12:54:28247class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
Tomas Gunnarssonabdb4702020-09-05 16:43:36248 public MessageHandlerAutoCleanup,
Jonas Oreland6ca955a2021-03-15 08:27:43249 public NetworkBinderInterface,
Mirko Bonadei35214fc2019-09-23 12:54:28250 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 05:52:50251 public:
252 BasicNetworkManager();
Taylor Brandstetter239ac8a2020-07-31 23:07:52253 explicit BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory);
Henrik Kjellanderec78f1c2017-06-29 05:52:50254 ~BasicNetworkManager() override;
255
256 void StartUpdating() override;
257 void StopUpdating() override;
258
259 void DumpNetworks() override;
260
261 // MessageHandler interface.
262 void OnMessage(Message* msg) override;
263 bool started() { return start_count_ > 0; }
264
265 // Sets the network ignore list, which is empty by default. Any network on the
266 // ignore list will be filtered from network enumeration results.
Taylor Brandstetter239ac8a2020-07-31 23:07:52267 // Should be called only before initialization.
Henrik Kjellanderec78f1c2017-06-29 05:52:50268 void set_network_ignore_list(const std::vector<std::string>& list) {
Taylor Brandstetter239ac8a2020-07-31 23:07:52269 RTC_DCHECK(thread_ == nullptr);
Henrik Kjellanderec78f1c2017-06-29 05:52:50270 network_ignore_list_ = list;
271 }
272
Jonas Oreland2ee0e642021-08-25 13:43:02273 // Set a list of manually configured VPN's.
274 void set_vpn_list(const std::vector<NetworkMask>& vpn) override;
275
276 // Check if |prefix| is configured as VPN.
277 bool IsConfiguredVpn(IPAddress prefix, int prefix_length) const;
278
Jonas Oreland6ca955a2021-03-15 08:27:43279 // Bind a socket to interface that ip address belong to.
280 // Implementation look up interface name and calls
281 // BindSocketToNetwork on NetworkMonitor.
282 // The interface name is needed as e.g ipv4 over ipv6 addresses
283 // are not exposed using Android functions, but it is possible
284 // bind an ipv4 address to the interface.
285 NetworkBindingResult BindSocketToNetwork(int socket_fd,
286 const IPAddress& address) override;
287
Henrik Kjellanderec78f1c2017-06-29 05:52:50288 protected:
289#if defined(WEBRTC_POSIX)
290 // Separated from CreateNetworks for tests.
291 void ConvertIfAddrs(ifaddrs* interfaces,
292 IfAddrsConverter* converter,
293 bool include_ignored,
Taylor Brandstetter239ac8a2020-07-31 23:07:52294 NetworkList* networks) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50295#endif // defined(WEBRTC_POSIX)
296
297 // Creates a network object for each network available on the machine.
Taylor Brandstetter239ac8a2020-07-31 23:07:52298 bool CreateNetworks(bool include_ignored, NetworkList* networks) const
299 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50300
301 // Determines if a network should be ignored. This should only be determined
302 // based on the network's property instead of any individual IP.
Taylor Brandstetter239ac8a2020-07-31 23:07:52303 bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50304
305 // This function connects a UDP socket to a public address and returns the
306 // local address associated it. Since it binds to the "any" address
307 // internally, it returns the default local address on a multi-homed endpoint.
Taylor Brandstetter239ac8a2020-07-31 23:07:52308 IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50309
310 private:
311 friend class NetworkTest;
312
313 // Creates a network monitor and listens for network updates.
Taylor Brandstetter239ac8a2020-07-31 23:07:52314 void StartNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50315 // Stops and removes the network monitor.
Taylor Brandstetter239ac8a2020-07-31 23:07:52316 void StopNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50317 // Called when it receives updates from the network monitor.
318 void OnNetworksChanged();
319
320 // Updates the networks and reschedules the next update.
Taylor Brandstetter239ac8a2020-07-31 23:07:52321 void UpdateNetworksContinually() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50322 // Only updates the networks; does not reschedule the next update.
Taylor Brandstetter239ac8a2020-07-31 23:07:52323 void UpdateNetworksOnce() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 05:52:50324
Taylor Brandstetter239ac8a2020-07-31 23:07:52325 Thread* thread_ = nullptr;
326 bool sent_first_update_ = true;
327 int start_count_ = 0;
Henrik Kjellanderec78f1c2017-06-29 05:52:50328 std::vector<std::string> network_ignore_list_;
Taylor Brandstetter239ac8a2020-07-31 23:07:52329 NetworkMonitorFactory* network_monitor_factory_ RTC_GUARDED_BY(thread_) =
330 nullptr;
331 std::unique_ptr<NetworkMonitorInterface> network_monitor_
332 RTC_GUARDED_BY(thread_);
Jonas Oreland6ca955a2021-03-15 08:27:43333 bool allow_mac_based_ipv6_ RTC_GUARDED_BY(thread_) = false;
334 bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
Jonas Oreland2ee0e642021-08-25 13:43:02335
336 std::vector<NetworkMask> vpn_;
Henrik Kjellanderec78f1c2017-06-29 05:52:50337};
338
339// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 12:54:28340class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 05:52:50341 public:
342 Network(const std::string& name,
343 const std::string& description,
344 const IPAddress& prefix,
345 int prefix_length);
346
347 Network(const std::string& name,
348 const std::string& description,
349 const IPAddress& prefix,
350 int prefix_length,
351 AdapterType type);
Steve Anton9de3aac2017-10-24 17:08:26352 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 05:52:50353 ~Network();
Jonas Orelandf7721fb2020-08-07 09:08:34354
Qingsi Wangde2ed7d2018-04-27 21:25:37355 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Henrik Kjellanderec78f1c2017-06-29 05:52:50356 sigslot::signal1<const Network*> SignalTypeChanged;
357
Jonas Orelandf7721fb2020-08-07 09:08:34358 // This signal is fired whenever network preference changes.
359 sigslot::signal1<const Network*> SignalNetworkPreferenceChanged;
360
Henrik Kjellanderec78f1c2017-06-29 05:52:50361 const DefaultLocalAddressProvider* default_local_address_provider() {
362 return default_local_address_provider_;
363 }
364 void set_default_local_address_provider(
365 const DefaultLocalAddressProvider* provider) {
366 default_local_address_provider_ = provider;
367 }
368
Qingsi Wang5ae259e2019-02-13 23:46:07369 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
370 mdns_responder_provider_ = provider;
371 }
372
373 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 05:52:50374 const std::string& name() const { return name_; }
375
376 // Returns the OS-assigned name for this network. This is useful for
377 // debugging but should not be sent over the wire (for privacy reasons).
378 const std::string& description() const { return description_; }
379
380 // Returns the prefix for this network.
381 const IPAddress& prefix() const { return prefix_; }
382 // Returns the length, in bits, of this network's prefix.
383 int prefix_length() const { return prefix_length_; }
384
Artem Titov96e3b992021-07-26 14:03:14385 // `key_` has unique value per network interface. Used in sorting network
Henrik Kjellanderec78f1c2017-06-29 05:52:50386 // interfaces. Key is derived from interface name and it's prefix.
387 std::string key() const { return key_; }
388
389 // Returns the Network's current idea of the 'best' IP it has.
390 // Or return an unset IP if this network has no active addresses.
391 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 23:46:07392 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 05:52:50393 // 2) if #1 not available, return global ones.
394 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
395 // for unique local address, which is not route-able in open
396 // internet but might be useful for a close WebRTC deployment.
397
398 // TODO(guoweis): rule #3 actually won't happen at current
399 // implementation. The reason being that ULA address starting with
400 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
401 // that is WebRTC will have one extra Network to generate candidates
402 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
403 // ULA should only be tried in a close deployment anyway.
404
405 // Note that when not specifying any flag, it's treated as case global
406 // IPv6 address
407 IPAddress GetBestIP() const;
408
409 // Keep the original function here for now.
410 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
411 IPAddress ip() const { return GetBestIP(); }
412
413 // Adds an active IP address to this network. Does not check for duplicates.
414 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 23:49:32415 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 05:52:50416
417 // Sets the network's IP address list. Returns true if new IP addresses were
418 // detected. Passing true to already_changed skips this check.
419 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
420 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 13:03:05421 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 05:52:50422 // Clear the network's list of addresses.
423 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 23:46:07424 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-13 05:51:55425 // addresses of host candidates by mDNS names in ICE gathering. After a
426 // name-address mapping is created by the mDNS responder, queries for the
427 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 23:46:07428 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:50429
430 // Returns the scope-id of the network's address.
431 // Should only be relevant for link-local IPv6 addresses.
432 int scope_id() const { return scope_id_; }
433 void set_scope_id(int id) { scope_id_ = id; }
434
435 // Indicates whether this network should be ignored, perhaps because
436 // the IP is 0, or the interface is one we know is invalid.
437 bool ignored() const { return ignored_; }
438 void set_ignored(bool ignored) { ignored_ = ignored; }
439
440 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 21:25:37441 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
442 // network interface used by the VPN, typically the preferred network type
443 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
444 // on https://developer.android.com/reference/android/net/VpnService.html).
445 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
446 // returned.
447 AdapterType underlying_type_for_vpn() const {
448 return underlying_type_for_vpn_;
449 }
Henrik Kjellanderec78f1c2017-06-29 05:52:50450 void set_type(AdapterType type) {
451 if (type_ == type) {
452 return;
453 }
454 type_ = type;
Qingsi Wangde2ed7d2018-04-27 21:25:37455 if (type != ADAPTER_TYPE_VPN) {
456 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
457 }
Henrik Kjellanderec78f1c2017-06-29 05:52:50458 SignalTypeChanged(this);
459 }
460
Qingsi Wangde2ed7d2018-04-27 21:25:37461 void set_underlying_type_for_vpn(AdapterType type) {
462 if (underlying_type_for_vpn_ == type) {
463 return;
Henrik Kjellanderec78f1c2017-06-29 05:52:50464 }
Qingsi Wangde2ed7d2018-04-27 21:25:37465 underlying_type_for_vpn_ = type;
466 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 05:52:50467 }
Qingsi Wangde2ed7d2018-04-27 21:25:37468
469 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
470
Jonas Orelandc7ea04a2020-04-03 08:12:28471 bool IsCellular() const { return IsCellular(type_); }
472
473 static bool IsCellular(AdapterType type) {
474 switch (type) {
Jonas Oreland08d18062020-04-02 05:19:12475 case ADAPTER_TYPE_CELLULAR:
476 case ADAPTER_TYPE_CELLULAR_2G:
477 case ADAPTER_TYPE_CELLULAR_3G:
478 case ADAPTER_TYPE_CELLULAR_4G:
479 case ADAPTER_TYPE_CELLULAR_5G:
480 return true;
481 default:
482 return false;
483 }
484 }
485
Qingsi Wangde2ed7d2018-04-27 21:25:37486 uint16_t GetCost() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:50487 // A unique id assigned by the network manager, which may be signaled
488 // to the remote side in the candidate.
489 uint16_t id() const { return id_; }
490 void set_id(uint16_t id) { id_ = id; }
491
492 int preference() const { return preference_; }
493 void set_preference(int preference) { preference_ = preference; }
494
495 // When we enumerate networks and find a previously-seen network is missing,
496 // we do not remove it (because it may be used elsewhere). Instead, we mark
497 // it inactive, so that we can detect network changes properly.
498 bool active() const { return active_; }
499 void set_active(bool active) {
500 if (active_ != active) {
501 active_ = active;
502 }
503 }
504
Jonas Orelandf7721fb2020-08-07 09:08:34505 // Property set by operating system/firmware that has information
506 // about connection strength to e.g WIFI router or CELL base towers.
507 NetworkPreference network_preference() const { return network_preference_; }
508 void set_network_preference(NetworkPreference val) {
509 if (network_preference_ == val) {
510 return;
511 }
512 network_preference_ = val;
513 SignalNetworkPreferenceChanged(this);
514 }
515
Henrik Kjellanderec78f1c2017-06-29 05:52:50516 // Debugging description of this network
517 std::string ToString() const;
518
519 private:
520 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 23:46:07521 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 05:52:50522 std::string name_;
523 std::string description_;
524 IPAddress prefix_;
525 int prefix_length_;
526 std::string key_;
527 std::vector<InterfaceAddress> ips_;
528 int scope_id_;
529 bool ignored_;
530 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 21:25:37531 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 05:52:50532 int preference_;
533 bool active_ = true;
534 uint16_t id_ = 0;
Jonas Oreland2105d642020-05-13 08:15:34535 bool use_differentiated_cellular_costs_ = false;
Jonas Orelandb477fc72021-08-23 10:16:33536 bool add_network_cost_to_vpn_ = false;
Jonas Orelandf7721fb2020-08-07 09:08:34537 NetworkPreference network_preference_ = NetworkPreference::NEUTRAL;
Henrik Kjellanderec78f1c2017-06-29 05:52:50538
539 friend class NetworkManager;
540};
541
542} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26543
Mirko Bonadei92ea95e2017-09-15 04:47:31544#endif // RTC_BASE_NETWORK_H_