blob: f7eec7445f9f24e49d3ea91f4b56e7ef0bb2d54c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_NETWORK_H_
12#define RTC_BASE_NETWORK_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdint.h>
pbosc7c26a02017-01-02 08:42:32 -080015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <deque>
17#include <map>
18#include <memory>
19#include <string>
20#include <vector>
21
Niels Möller22211442022-04-07 11:43:28 +020022#include "absl/base/attributes.h"
Ali Tofigh7fa90572022-03-17 15:47:49 +010023#include "absl/strings/string_view.h"
Jonas Orelandac554eb2021-08-27 09:43:38 +020024#include "api/array_view.h"
Jonas Orelande62c2f22022-03-29 11:04:48 +020025#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010026#include "api/sequence_checker.h"
Artem Titovc374d112022-06-16 21:27:45 +020027#include "api/task_queue/pending_task_safety_flag.h"
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020028#include "api/transport/field_trial_based_config.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-12 22:51:55 -070030#include "rtc_base/mdns_responder_interface.h"
Jonas Orelandc06fe8b2022-03-28 14:58:26 +020031#include "rtc_base/memory/always_valid_pointer.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "rtc_base/network_monitor.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070033#include "rtc_base/network_monitor_factory.h"
Niels Mölleraa373162021-09-28 16:09:07 +020034#include "rtc_base/socket_factory.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020035#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020036#include "rtc_base/third_party/sigslot/sigslot.h"
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -070037#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038
39#if defined(WEBRTC_POSIX)
40struct ifaddrs;
41#endif // defined(WEBRTC_POSIX)
42
43namespace rtc {
44
45extern const char kPublicIPv4Host[];
46extern const char kPublicIPv6Host[];
47
48class IfAddrsConverter;
49class Network;
50class NetworkMonitorInterface;
51class Thread;
52
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053// By default, ignore loopback interfaces on the host.
54const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
55
Mirko Bonadei13f9c622022-04-29 16:38:32 +020056namespace webrtc_network_internal {
57bool CompareNetworks(const std::unique_ptr<Network>& a,
58 const std::unique_ptr<Network>& b);
59} // namespace webrtc_network_internal
60
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020061// Makes a string key for this network. Used in the network manager's maps.
62// Network objects are keyed on interface name, network prefix and the
63// length of that prefix.
Ali Tofigh7fa90572022-03-17 15:47:49 +010064std::string MakeNetworkKey(absl::string_view name,
Yves Gerey665174f2018-06-19 15:03:05 +020065 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020066 int prefix_length);
67
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080068// Utility function that attempts to determine an adapter type by an interface
69// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
70// mechanisms fail to determine the type.
Ali Tofigh2ab914c2022-04-13 12:55:15 +020071RTC_EXPORT AdapterType GetAdapterTypeFromName(absl::string_view network_name);
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080072
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073class DefaultLocalAddressProvider {
74 public:
75 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 15:46:07 -080076
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020077 // The default local address is the local address used in multi-homed endpoint
78 // when the any address (0.0.0.0 or ::) is used as the local address. It's
79 // important to check the return value as a IP family may not be enabled.
80 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
81};
82
Qingsi Wang5ae259e2019-02-13 15:46:07 -080083class MdnsResponderProvider {
84 public:
85 virtual ~MdnsResponderProvider() = default;
86
87 // Returns the mDNS responder that can be used to obfuscate the local IP
88 // addresses of ICE host candidates by mDNS hostnames.
89 //
90 // The provider MUST outlive the mDNS responder.
91 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
92};
93
Jonas Oreland2ee0e642021-08-25 15:43:02 +020094// Network/mask in CIDR representation.
95class NetworkMask {
96 public:
97 NetworkMask(const IPAddress& addr, int prefix_length)
98 : address_(addr), prefix_length_(prefix_length) {}
99
100 const IPAddress& address() const { return address_; }
101 int prefix_length() const { return prefix_length_; }
102
103 bool operator==(const NetworkMask& o) const {
104 return address_ == o.address_ && prefix_length_ == o.prefix_length_;
105 }
106
107 private:
108 IPAddress address_;
109 // Length of valid bits in address_ (for ipv4 valid range is 0-32)
110 int prefix_length_;
111};
112
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113// Generic network manager interface. It provides list of local
114// networks.
115//
116// Every method of NetworkManager (including the destructor) must be called on
117// the same thread, except for the constructor which may be called on any
118// thread.
119//
120// This allows constructing a NetworkManager subclass on one thread and
121// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200122class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
123 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200125 // This enum indicates whether adapter enumeration is allowed.
126 enum EnumerationPermission {
127 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
128 // from GetNetworks means that there is no network
129 // available.
130 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
131 // GetAnyAddressNetworks() should be used instead.
132 };
133
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200134 // Called when network list is updated.
135 sigslot::signal0<> SignalNetworksChanged;
136
137 // Indicates a failure when getting list of network interfaces.
138 sigslot::signal0<> SignalError;
139
140 // This should be called on the NetworkManager's thread before the
141 // NetworkManager is used. Subclasses may override this if necessary.
142 virtual void Initialize() {}
143
144 // Start/Stop monitoring of network interfaces
145 // list. SignalNetworksChanged or SignalError is emitted immediately
146 // after StartUpdating() is called. After that SignalNetworksChanged
147 // is emitted whenever list of networks changes.
148 virtual void StartUpdating() = 0;
149 virtual void StopUpdating() = 0;
150
151 // Returns the current list of networks available on this machine.
152 // StartUpdating() must be called before this method is called.
153 // It makes sure that repeated calls return the same object for a
154 // given network, so that quality is tracked appropriately. Does not
155 // include ignored networks.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200156 // The returned vector of Network* is valid as long as the NetworkManager is
157 // alive.
Niels Möller22211442022-04-07 11:43:28 +0200158 virtual std::vector<const Network*> GetNetworks() const = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159
Qingsi Wang09619332018-09-12 22:51:55 -0700160 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200161 virtual EnumerationPermission enumeration_permission() const;
162
163 // "AnyAddressNetwork" is a network which only contains single "any address"
164 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
165 // useful as binding to such interfaces allow default routing behavior like
166 // http traffic.
167 //
168 // This method appends the "any address" networks to the list, such that this
169 // can optionally be called after GetNetworks.
Niels Möller22211442022-04-07 11:43:28 +0200170 virtual std::vector<const Network*> GetAnyAddressNetworks() = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200171
172 // Dumps the current list of networks in the network manager.
173 virtual void DumpNetworks() {}
174 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
175
176 struct Stats {
177 int ipv4_network_count;
178 int ipv6_network_count;
179 Stats() {
180 ipv4_network_count = 0;
181 ipv6_network_count = 0;
182 }
183 };
Qingsi Wang09619332018-09-12 22:51:55 -0700184
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800185 // MdnsResponderProvider interface.
186 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200187
188 virtual void set_vpn_list(const std::vector<NetworkMask>& vpn) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200189};
190
191// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200192class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200193 public:
Jonas Orelande62c2f22022-03-29 11:04:48 +0200194 NetworkManagerBase(const webrtc::FieldTrialsView* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200195
Niels Möller22211442022-04-07 11:43:28 +0200196 std::vector<const Network*> GetNetworks() const override;
197 std::vector<const Network*> GetAnyAddressNetworks() override;
deadbeef3427f532017-07-26 16:09:33 -0700198
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200199 EnumerationPermission enumeration_permission() const override;
200
201 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
202
Jonas Orelandac554eb2021-08-27 09:43:38 +0200203 // Check if MAC address in |bytes| is one of the pre-defined
204 // MAC addresses for know VPNs.
205 static bool IsVpnMacAddress(rtc::ArrayView<const uint8_t> address);
206
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200207 protected:
Artem Titov96e3b992021-07-26 16:03:14 +0200208 // Updates `networks_` with the networks listed in `list`. If
Niels Möller22211442022-04-07 11:43:28 +0200209 // `networks_map_` already has a Network object for a network listed
Artem Titov96e3b992021-07-26 16:03:14 +0200210 // in the `list` then it is reused. Accept ownership of the Network
211 // objects in the `list`. `changed` will be set to true if there is
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200212 // any change in the network list.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200213 void MergeNetworkList(std::vector<std::unique_ptr<Network>> list,
214 bool* changed);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200215
Artem Titov96e3b992021-07-26 16:03:14 +0200216 // `stats` will be populated even if |*changed| is false.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200217 void MergeNetworkList(std::vector<std::unique_ptr<Network>> list,
218 bool* changed,
219 NetworkManager::Stats* stats);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200220
221 void set_enumeration_permission(EnumerationPermission state) {
222 enumeration_permission_ = state;
223 }
224
225 void set_default_local_addresses(const IPAddress& ipv4,
226 const IPAddress& ipv6);
227
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000228 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
229
Niels Möllerbf4988a2022-04-01 16:38:27 +0200230 // To enable subclasses to get the networks list, without interfering with
231 // refactoring of the interface GetNetworks method.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200232 const std::vector<Network*>& GetNetworksInternal() const { return networks_; }
Niels Möllerbf4988a2022-04-01 16:38:27 +0200233
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200234 private:
235 friend class NetworkTest;
Diep Bui1e589eb2022-08-02 07:37:30 +0000236 const webrtc::FieldTrialsView* field_trials_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200237 EnumerationPermission enumeration_permission_;
238
Niels Möllerd959f3a2022-04-19 11:29:19 +0200239 std::vector<Network*> networks_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200240
Niels Möllerd959f3a2022-04-19 11:29:19 +0200241 std::map<std::string, std::unique_ptr<Network>> networks_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200242
243 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
244 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
245
246 IPAddress default_local_ipv4_address_;
247 IPAddress default_local_ipv6_address_;
248 // We use 16 bits to save the bandwidth consumption when sending the network
249 // id over the Internet. It is OK that the 16-bit integer overflows to get a
250 // network id 0 because we only compare the network ids in the old and the new
251 // best connections in the transport channel.
252 uint16_t next_available_network_id_ = 1;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200253
254 // True if calling network_preference() with a changed value
255 // should result in firing the SignalNetworkChanged signal.
256 bool signal_network_preference_change_ = false;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200257};
258
259// Basic implementation of the NetworkManager interface that gets list
260// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200261class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000262 public NetworkBinderInterface,
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200263 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200264 public:
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200265 // This is used by lots of downstream code.
Jonas Orelande62c2f22022-03-29 11:04:48 +0200266 BasicNetworkManager(SocketFactory* socket_factory,
267 const webrtc::FieldTrialsView* field_trials = nullptr)
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200268 : BasicNetworkManager(/* network_monitor_factory= */ nullptr,
269 socket_factory,
270 field_trials) {}
271
Jonas Orelande62c2f22022-03-29 11:04:48 +0200272 BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory,
273 SocketFactory* socket_factory,
274 const webrtc::FieldTrialsView* field_trials = nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200275 ~BasicNetworkManager() override;
276
277 void StartUpdating() override;
278 void StopUpdating() override;
279
280 void DumpNetworks() override;
281
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200282 bool started() { return start_count_ > 0; }
283
284 // Sets the network ignore list, which is empty by default. Any network on the
285 // ignore list will be filtered from network enumeration results.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700286 // Should be called only before initialization.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200287 void set_network_ignore_list(const std::vector<std::string>& list) {
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700288 RTC_DCHECK(thread_ == nullptr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200289 network_ignore_list_ = list;
290 }
291
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200292 // Set a list of manually configured VPN's.
293 void set_vpn_list(const std::vector<NetworkMask>& vpn) override;
294
295 // Check if |prefix| is configured as VPN.
296 bool IsConfiguredVpn(IPAddress prefix, int prefix_length) const;
297
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000298 // Bind a socket to interface that ip address belong to.
299 // Implementation look up interface name and calls
300 // BindSocketToNetwork on NetworkMonitor.
301 // The interface name is needed as e.g ipv4 over ipv6 addresses
302 // are not exposed using Android functions, but it is possible
303 // bind an ipv4 address to the interface.
304 NetworkBindingResult BindSocketToNetwork(int socket_fd,
305 const IPAddress& address) override;
306
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200307 protected:
308#if defined(WEBRTC_POSIX)
309 // Separated from CreateNetworks for tests.
310 void ConvertIfAddrs(ifaddrs* interfaces,
311 IfAddrsConverter* converter,
312 bool include_ignored,
Niels Möllerd959f3a2022-04-19 11:29:19 +0200313 std::vector<std::unique_ptr<Network>>* networks) const
314 RTC_RUN_ON(thread_);
Jonas Oreland61dbcd12022-05-31 11:34:20 +0200315 NetworkMonitorInterface::InterfaceInfo GetInterfaceInfo(
316 struct ifaddrs* cursor) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200317#endif // defined(WEBRTC_POSIX)
318
319 // Creates a network object for each network available on the machine.
Niels Möllerd959f3a2022-04-19 11:29:19 +0200320 bool CreateNetworks(bool include_ignored,
321 std::vector<std::unique_ptr<Network>>* networks) const
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700322 RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200323
324 // Determines if a network should be ignored. This should only be determined
325 // based on the network's property instead of any individual IP.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700326 bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200327
328 // This function connects a UDP socket to a public address and returns the
329 // local address associated it. Since it binds to the "any" address
330 // internally, it returns the default local address on a multi-homed endpoint.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700331 IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200332
333 private:
334 friend class NetworkTest;
335
336 // Creates a network monitor and listens for network updates.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700337 void StartNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200338 // Stops and removes the network monitor.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700339 void StopNetworkMonitor() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200340 // Called when it receives updates from the network monitor.
341 void OnNetworksChanged();
342
343 // Updates the networks and reschedules the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700344 void UpdateNetworksContinually() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200345 // Only updates the networks; does not reschedule the next update.
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700346 void UpdateNetworksOnce() RTC_RUN_ON(thread_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200347
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700348 Thread* thread_ = nullptr;
349 bool sent_first_update_ = true;
350 int start_count_ = 0;
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200351 // Chromium create BasicNetworkManager() w/o field trials.
Jonas Orelande62c2f22022-03-29 11:04:48 +0200352 webrtc::AlwaysValidPointer<const webrtc::FieldTrialsView,
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200353 webrtc::FieldTrialBasedConfig>
354 field_trials_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200355 std::vector<std::string> network_ignore_list_;
Niels Mölleraa373162021-09-28 16:09:07 +0200356 NetworkMonitorFactory* const network_monitor_factory_;
357 SocketFactory* const socket_factory_;
Taylor Brandstetter239ac8a2020-07-31 16:07:52 -0700358 std::unique_ptr<NetworkMonitorInterface> network_monitor_
359 RTC_GUARDED_BY(thread_);
Jonas Oreland6ca955a2021-03-15 08:27:43 +0000360 bool allow_mac_based_ipv6_ RTC_GUARDED_BY(thread_) = false;
361 bool bind_using_ifname_ RTC_GUARDED_BY(thread_) = false;
Jonas Oreland2ee0e642021-08-25 15:43:02 +0200362
363 std::vector<NetworkMask> vpn_;
Niels Möller27c14522022-02-07 16:44:21 +0100364 rtc::scoped_refptr<webrtc::PendingTaskSafetyFlag> task_safety_flag_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200365};
366
367// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200368class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200369 public:
Ali Tofigh7fa90572022-03-17 15:47:49 +0100370 Network(absl::string_view name,
371 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200372 const IPAddress& prefix,
Diep Bui1e589eb2022-08-02 07:37:30 +0000373 int prefix_length,
374 const webrtc::FieldTrialsView* field_trials = nullptr)
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200375 : Network(name,
376 description,
377 prefix,
378 prefix_length,
Diep Bui1e589eb2022-08-02 07:37:30 +0000379 rtc::ADAPTER_TYPE_UNKNOWN,
380 field_trials) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200381
Ali Tofigh7fa90572022-03-17 15:47:49 +0100382 Network(absl::string_view name,
383 absl::string_view description,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200384 const IPAddress& prefix,
385 int prefix_length,
Diep Bui1e589eb2022-08-02 07:37:30 +0000386 AdapterType type,
387 const webrtc::FieldTrialsView* field_trials = nullptr);
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200388
Steve Anton9de3aac2017-10-24 10:08:26 -0700389 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200390 ~Network();
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200391
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700392 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100393 // Mutable, to support connecting on the const Network passed to cricket::Port
394 // constructor.
395 mutable sigslot::signal1<const Network*> SignalTypeChanged;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200396
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200397 // This signal is fired whenever network preference changes.
398 sigslot::signal1<const Network*> SignalNetworkPreferenceChanged;
399
Niels Möllere0c6bdf2022-03-24 15:18:02 +0100400 const DefaultLocalAddressProvider* default_local_address_provider() const {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200401 return default_local_address_provider_;
402 }
403 void set_default_local_address_provider(
404 const DefaultLocalAddressProvider* provider) {
405 default_local_address_provider_ = provider;
406 }
407
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800408 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
409 mdns_responder_provider_ = provider;
410 }
411
412 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200413 const std::string& name() const { return name_; }
414
415 // Returns the OS-assigned name for this network. This is useful for
416 // debugging but should not be sent over the wire (for privacy reasons).
417 const std::string& description() const { return description_; }
418
419 // Returns the prefix for this network.
420 const IPAddress& prefix() const { return prefix_; }
421 // Returns the length, in bits, of this network's prefix.
422 int prefix_length() const { return prefix_length_; }
423
Sameer Vijaykara75eb432022-08-11 13:47:20 +0200424 // Returns the family for the network prefix.
425 int family() const { return prefix_.family(); }
426
Artem Titov96e3b992021-07-26 16:03:14 +0200427 // `key_` has unique value per network interface. Used in sorting network
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200428 // interfaces. Key is derived from interface name and it's prefix.
429 std::string key() const { return key_; }
430
431 // Returns the Network's current idea of the 'best' IP it has.
432 // Or return an unset IP if this network has no active addresses.
433 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800434 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200435 // 2) if #1 not available, return global ones.
Diep Bui1e589eb2022-08-02 07:37:30 +0000436 // 3) if #2 not available and WebRTC-PreferGlobalIPv6ToLinkLocal enabled,
437 // return local link ones.
438 // 4) if #3 not available, use ULA ipv6 as last resort. (ULA stands for
439 // unique local address, which is not route-able in open internet but might
440 // be useful for a close WebRTC deployment.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200441
442 // TODO(guoweis): rule #3 actually won't happen at current
443 // implementation. The reason being that ULA address starting with
444 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
445 // that is WebRTC will have one extra Network to generate candidates
446 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
447 // ULA should only be tried in a close deployment anyway.
448
449 // Note that when not specifying any flag, it's treated as case global
450 // IPv6 address
451 IPAddress GetBestIP() const;
452
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200453 // Adds an active IP address to this network. Does not check for duplicates.
454 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800455 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200456
457 // Sets the network's IP address list. Returns true if new IP addresses were
458 // detected. Passing true to already_changed skips this check.
459 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
460 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 15:03:05 +0200461 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200462 // Clear the network's list of addresses.
463 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800464 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-12 22:51:55 -0700465 // addresses of host candidates by mDNS names in ICE gathering. After a
466 // name-address mapping is created by the mDNS responder, queries for the
467 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800468 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200469
470 // Returns the scope-id of the network's address.
471 // Should only be relevant for link-local IPv6 addresses.
472 int scope_id() const { return scope_id_; }
473 void set_scope_id(int id) { scope_id_ = id; }
474
475 // Indicates whether this network should be ignored, perhaps because
476 // the IP is 0, or the interface is one we know is invalid.
477 bool ignored() const { return ignored_; }
478 void set_ignored(bool ignored) { ignored_ = ignored; }
479
480 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700481 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
482 // network interface used by the VPN, typically the preferred network type
483 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
484 // on https://developer.android.com/reference/android/net/VpnService.html).
485 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
486 // returned.
487 AdapterType underlying_type_for_vpn() const {
488 return underlying_type_for_vpn_;
489 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200490 void set_type(AdapterType type) {
491 if (type_ == type) {
492 return;
493 }
494 type_ = type;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700495 if (type != ADAPTER_TYPE_VPN) {
496 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
497 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200498 SignalTypeChanged(this);
499 }
500
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700501 void set_underlying_type_for_vpn(AdapterType type) {
502 if (underlying_type_for_vpn_ == type) {
503 return;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200504 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700505 underlying_type_for_vpn_ = type;
506 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200507 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700508
509 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
510
Jonas Orelandc7ea04a2020-04-03 10:12:28 +0200511 bool IsCellular() const { return IsCellular(type_); }
512
513 static bool IsCellular(AdapterType type) {
514 switch (type) {
Jonas Oreland08d18062020-04-02 07:19:12 +0200515 case ADAPTER_TYPE_CELLULAR:
516 case ADAPTER_TYPE_CELLULAR_2G:
517 case ADAPTER_TYPE_CELLULAR_3G:
518 case ADAPTER_TYPE_CELLULAR_4G:
519 case ADAPTER_TYPE_CELLULAR_5G:
520 return true;
521 default:
522 return false;
523 }
524 }
525
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200526 // Note: This function is called "rarely".
527 // Twice per Network in BasicPortAllocator if
528 // PORTALLOCATOR_DISABLE_COSTLY_NETWORKS. Once in Port::Construct() (and when
529 // Port::OnNetworkTypeChanged is called).
530 ABSL_DEPRECATED(
531 "Use the version with field trials, see bugs.webrtc.org/webrtc:10335")
Jonas Orelande62c2f22022-03-29 11:04:48 +0200532 uint16_t GetCost(const webrtc::FieldTrialsView* field_trials = nullptr) const;
533 uint16_t GetCost(const webrtc::FieldTrialsView& field_trials) const;
Jonas Orelandc06fe8b2022-03-28 14:58:26 +0200534
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200535 // A unique id assigned by the network manager, which may be signaled
536 // to the remote side in the candidate.
537 uint16_t id() const { return id_; }
538 void set_id(uint16_t id) { id_ = id; }
539
540 int preference() const { return preference_; }
541 void set_preference(int preference) { preference_ = preference; }
542
543 // When we enumerate networks and find a previously-seen network is missing,
544 // we do not remove it (because it may be used elsewhere). Instead, we mark
545 // it inactive, so that we can detect network changes properly.
546 bool active() const { return active_; }
547 void set_active(bool active) {
548 if (active_ != active) {
549 active_ = active;
550 }
551 }
552
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200553 // Property set by operating system/firmware that has information
554 // about connection strength to e.g WIFI router or CELL base towers.
555 NetworkPreference network_preference() const { return network_preference_; }
556 void set_network_preference(NetworkPreference val) {
557 if (network_preference_ == val) {
558 return;
559 }
560 network_preference_ = val;
561 SignalNetworkPreferenceChanged(this);
562 }
563
Jonas Oreland30019052022-01-28 14:11:44 +0100564 static std::pair<rtc::AdapterType, bool /* vpn */>
565 GuessAdapterFromNetworkCost(int network_cost);
566
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200567 // Debugging description of this network
568 std::string ToString() const;
569
570 private:
Diep Bui1e589eb2022-08-02 07:37:30 +0000571 const webrtc::FieldTrialsView* field_trials_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200572 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800573 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200574 std::string name_;
575 std::string description_;
576 IPAddress prefix_;
577 int prefix_length_;
578 std::string key_;
579 std::vector<InterfaceAddress> ips_;
580 int scope_id_;
581 bool ignored_;
582 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700583 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200584 int preference_;
585 bool active_ = true;
586 uint16_t id_ = 0;
Jonas Orelandf7721fb2020-08-07 11:08:34 +0200587 NetworkPreference network_preference_ = NetworkPreference::NEUTRAL;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200588
589 friend class NetworkManager;
590};
591
592} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000593
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200594#endif // RTC_BASE_NETWORK_H_