blob: a8608d936acaf0a40e3d00b87e704b6197bd069b [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
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-12 22:51:55 -070023#include "rtc_base/mdns_responder_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/message_handler.h"
25#include "rtc_base/network_monitor.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020026#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020027#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028
29#if defined(WEBRTC_POSIX)
30struct ifaddrs;
31#endif // defined(WEBRTC_POSIX)
32
33namespace rtc {
34
35extern const char kPublicIPv4Host[];
36extern const char kPublicIPv6Host[];
37
38class IfAddrsConverter;
39class Network;
40class NetworkMonitorInterface;
41class Thread;
42
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043// By default, ignore loopback interfaces on the host.
44const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
45
46// Makes a string key for this network. Used in the network manager's maps.
47// Network objects are keyed on interface name, network prefix and the
48// length of that prefix.
Yves Gerey665174f2018-06-19 15:03:05 +020049std::string MakeNetworkKey(const std::string& name,
50 const IPAddress& prefix,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051 int prefix_length);
52
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080053// Utility function that attempts to determine an adapter type by an interface
54// name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other
55// mechanisms fail to determine the type.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020056RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name);
Taylor Brandstetter8bac1d92018-01-24 17:38:00 -080057
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058class DefaultLocalAddressProvider {
59 public:
60 virtual ~DefaultLocalAddressProvider() = default;
Qingsi Wang5ae259e2019-02-13 15:46:07 -080061
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062 // The default local address is the local address used in multi-homed endpoint
63 // when the any address (0.0.0.0 or ::) is used as the local address. It's
64 // important to check the return value as a IP family may not be enabled.
65 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
66};
67
Qingsi Wang5ae259e2019-02-13 15:46:07 -080068class MdnsResponderProvider {
69 public:
70 virtual ~MdnsResponderProvider() = default;
71
72 // Returns the mDNS responder that can be used to obfuscate the local IP
73 // addresses of ICE host candidates by mDNS hostnames.
74 //
75 // The provider MUST outlive the mDNS responder.
76 virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
77};
78
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020079// Generic network manager interface. It provides list of local
80// networks.
81//
82// Every method of NetworkManager (including the destructor) must be called on
83// the same thread, except for the constructor which may be called on any
84// thread.
85//
86// This allows constructing a NetworkManager subclass on one thread and
87// passing it into an object that uses it on a different thread.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020088class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider,
89 public MdnsResponderProvider {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020090 public:
91 typedef std::vector<Network*> NetworkList;
92
93 // This enum indicates whether adapter enumeration is allowed.
94 enum EnumerationPermission {
95 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
96 // from GetNetworks means that there is no network
97 // available.
98 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
99 // GetAnyAddressNetworks() should be used instead.
100 };
101
102 NetworkManager();
103 ~NetworkManager() override;
104
105 // Called when network list is updated.
106 sigslot::signal0<> SignalNetworksChanged;
107
108 // Indicates a failure when getting list of network interfaces.
109 sigslot::signal0<> SignalError;
110
111 // This should be called on the NetworkManager's thread before the
112 // NetworkManager is used. Subclasses may override this if necessary.
113 virtual void Initialize() {}
114
115 // Start/Stop monitoring of network interfaces
116 // list. SignalNetworksChanged or SignalError is emitted immediately
117 // after StartUpdating() is called. After that SignalNetworksChanged
118 // is emitted whenever list of networks changes.
119 virtual void StartUpdating() = 0;
120 virtual void StopUpdating() = 0;
121
122 // Returns the current list of networks available on this machine.
123 // StartUpdating() must be called before this method is called.
124 // It makes sure that repeated calls return the same object for a
125 // given network, so that quality is tracked appropriately. Does not
126 // include ignored networks.
127 virtual void GetNetworks(NetworkList* networks) const = 0;
128
Qingsi Wang09619332018-09-12 22:51:55 -0700129 // Returns the current permission state of GetNetworks().
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200130 virtual EnumerationPermission enumeration_permission() const;
131
132 // "AnyAddressNetwork" is a network which only contains single "any address"
133 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
134 // useful as binding to such interfaces allow default routing behavior like
135 // http traffic.
136 //
137 // This method appends the "any address" networks to the list, such that this
138 // can optionally be called after GetNetworks.
139 //
140 // TODO(guoweis): remove this body when chromium implements this.
141 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
142
143 // Dumps the current list of networks in the network manager.
144 virtual void DumpNetworks() {}
145 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
146
147 struct Stats {
148 int ipv4_network_count;
149 int ipv6_network_count;
150 Stats() {
151 ipv4_network_count = 0;
152 ipv6_network_count = 0;
153 }
154 };
Qingsi Wang09619332018-09-12 22:51:55 -0700155
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800156 // MdnsResponderProvider interface.
157 webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158};
159
160// Base class for NetworkManager implementations.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200161class RTC_EXPORT NetworkManagerBase : public NetworkManager {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200162 public:
163 NetworkManagerBase();
164 ~NetworkManagerBase() override;
165
166 void GetNetworks(NetworkList* networks) const override;
167 void GetAnyAddressNetworks(NetworkList* networks) override;
deadbeef3427f532017-07-26 16:09:33 -0700168
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200169 EnumerationPermission enumeration_permission() const override;
170
171 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
172
173 protected:
174 typedef std::map<std::string, Network*> NetworkMap;
175 // Updates |networks_| with the networks listed in |list|. If
176 // |network_map_| already has a Network object for a network listed
177 // in the |list| then it is reused. Accept ownership of the Network
178 // objects in the |list|. |changed| will be set to true if there is
179 // any change in the network list.
180 void MergeNetworkList(const NetworkList& list, bool* changed);
181
182 // |stats| will be populated even if |*changed| is false.
183 void MergeNetworkList(const NetworkList& list,
184 bool* changed,
185 NetworkManager::Stats* stats);
186
187 void set_enumeration_permission(EnumerationPermission state) {
188 enumeration_permission_ = state;
189 }
190
191 void set_default_local_addresses(const IPAddress& ipv4,
192 const IPAddress& ipv6);
193
194 private:
195 friend class NetworkTest;
196
197 Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const;
198
199 EnumerationPermission enumeration_permission_;
200
201 NetworkList networks_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200202
203 NetworkMap networks_map_;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200204
205 std::unique_ptr<rtc::Network> ipv4_any_address_network_;
206 std::unique_ptr<rtc::Network> ipv6_any_address_network_;
207
208 IPAddress default_local_ipv4_address_;
209 IPAddress default_local_ipv6_address_;
210 // We use 16 bits to save the bandwidth consumption when sending the network
211 // id over the Internet. It is OK that the 16-bit integer overflows to get a
212 // network id 0 because we only compare the network ids in the old and the new
213 // best connections in the transport channel.
214 uint16_t next_available_network_id_ = 1;
215};
216
217// Basic implementation of the NetworkManager interface that gets list
218// of networks using OS APIs.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200219class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase,
220 public MessageHandler,
221 public sigslot::has_slots<> {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200222 public:
223 BasicNetworkManager();
224 ~BasicNetworkManager() override;
225
226 void StartUpdating() override;
227 void StopUpdating() override;
228
229 void DumpNetworks() override;
230
231 // MessageHandler interface.
232 void OnMessage(Message* msg) override;
233 bool started() { return start_count_ > 0; }
234
235 // Sets the network ignore list, which is empty by default. Any network on the
236 // ignore list will be filtered from network enumeration results.
237 void set_network_ignore_list(const std::vector<std::string>& list) {
238 network_ignore_list_ = list;
239 }
240
241#if defined(WEBRTC_LINUX)
242 // Sets the flag for ignoring non-default routes.
deadbeefbe7e9c62017-07-11 20:07:37 -0700243 // Defaults to false.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200244 void set_ignore_non_default_routes(bool value) {
deadbeefbe7e9c62017-07-11 20:07:37 -0700245 ignore_non_default_routes_ = value;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200246 }
247#endif
248
249 protected:
250#if defined(WEBRTC_POSIX)
251 // Separated from CreateNetworks for tests.
252 void ConvertIfAddrs(ifaddrs* interfaces,
253 IfAddrsConverter* converter,
254 bool include_ignored,
255 NetworkList* networks) const;
256#endif // defined(WEBRTC_POSIX)
257
258 // Creates a network object for each network available on the machine.
259 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
260
261 // Determines if a network should be ignored. This should only be determined
262 // based on the network's property instead of any individual IP.
263 bool IsIgnoredNetwork(const Network& network) const;
264
265 // This function connects a UDP socket to a public address and returns the
266 // local address associated it. Since it binds to the "any" address
267 // internally, it returns the default local address on a multi-homed endpoint.
268 IPAddress QueryDefaultLocalAddress(int family) const;
269
270 private:
271 friend class NetworkTest;
272
273 // Creates a network monitor and listens for network updates.
274 void StartNetworkMonitor();
275 // Stops and removes the network monitor.
276 void StopNetworkMonitor();
277 // Called when it receives updates from the network monitor.
278 void OnNetworksChanged();
279
280 // Updates the networks and reschedules the next update.
281 void UpdateNetworksContinually();
282 // Only updates the networks; does not reschedule the next update.
283 void UpdateNetworksOnce();
284
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200285 Thread* thread_;
286 bool sent_first_update_;
287 int start_count_;
288 std::vector<std::string> network_ignore_list_;
289 bool ignore_non_default_routes_;
290 std::unique_ptr<NetworkMonitorInterface> network_monitor_;
291};
292
293// Represents a Unix-type network interface, with a name and single address.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200294class RTC_EXPORT Network {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200295 public:
296 Network(const std::string& name,
297 const std::string& description,
298 const IPAddress& prefix,
299 int prefix_length);
300
301 Network(const std::string& name,
302 const std::string& description,
303 const IPAddress& prefix,
304 int prefix_length,
305 AdapterType type);
Steve Anton9de3aac2017-10-24 10:08:26 -0700306 Network(const Network&);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200307 ~Network();
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700308 // This signal is fired whenever type() or underlying_type_for_vpn() changes.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200309 sigslot::signal1<const Network*> SignalTypeChanged;
310
311 const DefaultLocalAddressProvider* default_local_address_provider() {
312 return default_local_address_provider_;
313 }
314 void set_default_local_address_provider(
315 const DefaultLocalAddressProvider* provider) {
316 default_local_address_provider_ = provider;
317 }
318
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800319 void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
320 mdns_responder_provider_ = provider;
321 }
322
323 // Returns the name of the interface this network is associated with.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200324 const std::string& name() const { return name_; }
325
326 // Returns the OS-assigned name for this network. This is useful for
327 // debugging but should not be sent over the wire (for privacy reasons).
328 const std::string& description() const { return description_; }
329
330 // Returns the prefix for this network.
331 const IPAddress& prefix() const { return prefix_; }
332 // Returns the length, in bits, of this network's prefix.
333 int prefix_length() const { return prefix_length_; }
334
335 // |key_| has unique value per network interface. Used in sorting network
336 // interfaces. Key is derived from interface name and it's prefix.
337 std::string key() const { return key_; }
338
339 // Returns the Network's current idea of the 'best' IP it has.
340 // Or return an unset IP if this network has no active addresses.
341 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800342 // 1) return all global temporary dynamic and non-deprecated ones.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200343 // 2) if #1 not available, return global ones.
344 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
345 // for unique local address, which is not route-able in open
346 // internet but might be useful for a close WebRTC deployment.
347
348 // TODO(guoweis): rule #3 actually won't happen at current
349 // implementation. The reason being that ULA address starting with
350 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
351 // that is WebRTC will have one extra Network to generate candidates
352 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
353 // ULA should only be tried in a close deployment anyway.
354
355 // Note that when not specifying any flag, it's treated as case global
356 // IPv6 address
357 IPAddress GetBestIP() const;
358
359 // Keep the original function here for now.
360 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
361 IPAddress ip() const { return GetBestIP(); }
362
363 // Adds an active IP address to this network. Does not check for duplicates.
364 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800365 void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200366
367 // Sets the network's IP address list. Returns true if new IP addresses were
368 // detected. Passing true to already_changed skips this check.
369 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
370 // Get the list of IP Addresses associated with this network.
Yves Gerey665174f2018-06-19 15:03:05 +0200371 const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200372 // Clear the network's list of addresses.
373 void ClearIPs() { ips_.clear(); }
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800374 // Returns the mDNS responder that can be used to obfuscate the local IP
Qingsi Wang09619332018-09-12 22:51:55 -0700375 // addresses of host candidates by mDNS names in ICE gathering. After a
376 // name-address mapping is created by the mDNS responder, queries for the
377 // created name will be resolved by the responder.
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800378 webrtc::MdnsResponderInterface* GetMdnsResponder() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200379
380 // Returns the scope-id of the network's address.
381 // Should only be relevant for link-local IPv6 addresses.
382 int scope_id() const { return scope_id_; }
383 void set_scope_id(int id) { scope_id_ = id; }
384
385 // Indicates whether this network should be ignored, perhaps because
386 // the IP is 0, or the interface is one we know is invalid.
387 bool ignored() const { return ignored_; }
388 void set_ignored(bool ignored) { ignored_ = ignored; }
389
390 AdapterType type() const { return type_; }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700391 // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying
392 // network interface used by the VPN, typically the preferred network type
393 // (see for example, the method setUnderlyingNetworks(android.net.Network[])
394 // on https://developer.android.com/reference/android/net/VpnService.html).
395 // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is
396 // returned.
397 AdapterType underlying_type_for_vpn() const {
398 return underlying_type_for_vpn_;
399 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200400 void set_type(AdapterType type) {
401 if (type_ == type) {
402 return;
403 }
404 type_ = type;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700405 if (type != ADAPTER_TYPE_VPN) {
406 underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
407 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200408 SignalTypeChanged(this);
409 }
410
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700411 void set_underlying_type_for_vpn(AdapterType type) {
412 if (underlying_type_for_vpn_ == type) {
413 return;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200414 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700415 underlying_type_for_vpn_ = type;
416 SignalTypeChanged(this);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200417 }
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700418
419 bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; }
420
421 uint16_t GetCost() const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200422 // A unique id assigned by the network manager, which may be signaled
423 // to the remote side in the candidate.
424 uint16_t id() const { return id_; }
425 void set_id(uint16_t id) { id_ = id; }
426
427 int preference() const { return preference_; }
428 void set_preference(int preference) { preference_ = preference; }
429
430 // When we enumerate networks and find a previously-seen network is missing,
431 // we do not remove it (because it may be used elsewhere). Instead, we mark
432 // it inactive, so that we can detect network changes properly.
433 bool active() const { return active_; }
434 void set_active(bool active) {
435 if (active_ != active) {
436 active_ = active;
437 }
438 }
439
440 // Debugging description of this network
441 std::string ToString() const;
442
443 private:
444 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
Qingsi Wang5ae259e2019-02-13 15:46:07 -0800445 const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200446 std::string name_;
447 std::string description_;
448 IPAddress prefix_;
449 int prefix_length_;
450 std::string key_;
451 std::vector<InterfaceAddress> ips_;
452 int scope_id_;
453 bool ignored_;
454 AdapterType type_;
Qingsi Wangde2ed7d2018-04-27 14:25:37 -0700455 AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200456 int preference_;
457 bool active_ = true;
458 uint16_t id_ = 0;
459
460 friend class NetworkManager;
461};
462
463} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000464
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200465#endif // RTC_BASE_NETWORK_H_