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