blob: ee22d5e573075c24b5842433d9f004400e7099e8 [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
11#ifndef WEBRTC_BASE_NETWORK_H_
12#define WEBRTC_BASE_NETWORK_H_
13
14#include <deque>
15#include <map>
16#include <string>
17#include <vector>
18
19#include "webrtc/base/basictypes.h"
20#include "webrtc/base/ipaddress.h"
honghaiza7ad7c32016-02-02 12:54:14 -080021#include "webrtc/base/networkmonitor.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#include "webrtc/base/messagehandler.h"
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000023#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024#include "webrtc/base/sigslot.h"
25
26#if defined(WEBRTC_POSIX)
27struct ifaddrs;
28#endif // defined(WEBRTC_POSIX)
29
30namespace rtc {
31
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080032extern const char kPublicIPv4Host[];
33extern const char kPublicIPv6Host[];
34
Guo-wei Shieh9faf1542015-12-28 14:06:55 -080035class IfAddrsConverter;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036class Network;
honghaiz023f3ef2015-10-19 09:39:32 -070037class NetworkMonitorInterface;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038class Thread;
39
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000041// By default, ignore loopback interfaces on the host.
42const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
43
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044// Makes a string key for this network. Used in the network manager's maps.
45// Network objects are keyed on interface name, network prefix and the
46// length of that prefix.
47std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
48 int prefix_length);
49
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080050class DefaultLocalAddressProvider {
51 public:
52 virtual ~DefaultLocalAddressProvider() = default;
53 // The default local address is the local address used in multi-homed endpoint
Guo-wei Shiehe03cab92015-11-11 11:11:19 -080054 // when the any address (0.0.0.0 or ::) is used as the local address. It's
55 // important to check the return value as a IP family may not be enabled.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080056 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
57};
58
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059// Generic network manager interface. It provides list of local
60// networks.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080061class NetworkManager : public DefaultLocalAddressProvider {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000062 public:
63 typedef std::vector<Network*> NetworkList;
64
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070065 // This enum indicates whether adapter enumeration is allowed.
66 enum EnumerationPermission {
guoweisea1012b2015-08-21 09:06:28 -070067 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
68 // from GetNetworks means that there is no network
69 // available.
70 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
71 // GetAnyAddressNetworks() should be used instead.
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070072 };
73
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074 NetworkManager();
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080075 ~NetworkManager() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076
77 // Called when network list is updated.
78 sigslot::signal0<> SignalNetworksChanged;
79
80 // Indicates a failure when getting list of network interfaces.
81 sigslot::signal0<> SignalError;
82
83 // Start/Stop monitoring of network interfaces
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000084 // list. SignalNetworksChanged or SignalError is emitted immediately
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085 // after StartUpdating() is called. After that SignalNetworksChanged
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000086 // is emitted whenever list of networks changes.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 virtual void StartUpdating() = 0;
88 virtual void StopUpdating() = 0;
89
90 // Returns the current list of networks available on this machine.
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070091 // StartUpdating() must be called before this method is called.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 // It makes sure that repeated calls return the same object for a
93 // given network, so that quality is tracked appropriately. Does not
94 // include ignored networks.
95 virtual void GetNetworks(NetworkList* networks) const = 0;
96
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070097 // return the current permission state of GetNetworks()
Guo-wei Shieh86cb9232015-08-19 10:57:53 -070098 virtual EnumerationPermission enumeration_permission() const;
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070099
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000100 // "AnyAddressNetwork" is a network which only contains single "any address"
101 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
102 // useful as binding to such interfaces allow default routing behavior like
103 // http traffic.
guoweis@webrtc.org9dfe7aa2015-02-18 20:27:17 +0000104 // TODO(guoweis): remove this body when chromium implements this.
105 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000106
honghaizdb8cf502015-12-21 13:08:46 -0800107 // Dumps the current list of networks in the network manager.
108 virtual void DumpNetworks() {}
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800109 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
110
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000111 struct Stats {
112 int ipv4_network_count;
113 int ipv6_network_count;
114 Stats() {
115 ipv4_network_count = 0;
116 ipv6_network_count = 0;
117 }
118 };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119};
120
121// Base class for NetworkManager implementations.
122class NetworkManagerBase : public NetworkManager {
123 public:
124 NetworkManagerBase();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000125 ~NetworkManagerBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000127 void GetNetworks(std::vector<Network*>* networks) const override;
128 void GetAnyAddressNetworks(NetworkList* networks) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 bool ipv6_enabled() const { return ipv6_enabled_; }
130 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
131
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000132 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
133 int max_ipv6_networks() { return max_ipv6_networks_; }
134
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700135 EnumerationPermission enumeration_permission() const override;
136
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800137 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
138
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 protected:
140 typedef std::map<std::string, Network*> NetworkMap;
141 // Updates |networks_| with the networks listed in |list|. If
142 // |network_map_| already has a Network object for a network listed
143 // in the |list| then it is reused. Accept ownership of the Network
144 // objects in the |list|. |changed| will be set to true if there is
145 // any change in the network list.
146 void MergeNetworkList(const NetworkList& list, bool* changed);
147
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000148 // |stats| will be populated even if |*changed| is false.
149 void MergeNetworkList(const NetworkList& list,
150 bool* changed,
151 NetworkManager::Stats* stats);
152
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700153 void set_enumeration_permission(EnumerationPermission state) {
154 enumeration_permission_ = state;
155 }
156
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800157 void set_default_local_addresses(const IPAddress& ipv4,
158 const IPAddress& ipv6);
159
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000160 private:
161 friend class NetworkTest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700163 EnumerationPermission enumeration_permission_;
164
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000165 NetworkList networks_;
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000166 int max_ipv6_networks_;
167
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 NetworkMap networks_map_;
169 bool ipv6_enabled_;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000170
171 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_;
172 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800173
174 IPAddress default_local_ipv4_address_;
175 IPAddress default_local_ipv6_address_;
honghaiza0c44ea2016-03-23 16:07:48 -0700176 // We use 16 bits to save the bandwidth consumption when sending the network
177 // id over the Internet. It is OK that the 16-bit integer overflows to get a
178 // network id 0 because we only compare the network ids in the old and the new
179 // best connections in the transport channel.
180 uint16_t next_available_network_id_ = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000181};
182
183// Basic implementation of the NetworkManager interface that gets list
184// of networks using OS APIs.
185class BasicNetworkManager : public NetworkManagerBase,
honghaiz023f3ef2015-10-19 09:39:32 -0700186 public MessageHandler,
187 public sigslot::has_slots<> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188 public:
189 BasicNetworkManager();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000190 ~BasicNetworkManager() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000191
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000192 void StartUpdating() override;
193 void StopUpdating() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194
honghaizdb8cf502015-12-21 13:08:46 -0800195 void DumpNetworks() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196
197 // MessageHandler interface.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000198 void OnMessage(Message* msg) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 bool started() { return start_count_ > 0; }
200
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000201 // Sets the network ignore list, which is empty by default. Any network on the
202 // ignore list will be filtered from network enumeration results.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000203 void set_network_ignore_list(const std::vector<std::string>& list) {
204 network_ignore_list_ = list;
205 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000206
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000207#if defined(WEBRTC_LINUX)
208 // Sets the flag for ignoring non-default routes.
209 void set_ignore_non_default_routes(bool value) {
210 ignore_non_default_routes_ = true;
211 }
212#endif
213
214 protected:
215#if defined(WEBRTC_POSIX)
216 // Separated from CreateNetworks for tests.
217 void ConvertIfAddrs(ifaddrs* interfaces,
Guo-wei Shieh9faf1542015-12-28 14:06:55 -0800218 IfAddrsConverter* converter,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000219 bool include_ignored,
220 NetworkList* networks) const;
221#endif // defined(WEBRTC_POSIX)
222
223 // Creates a network object for each network available on the machine.
224 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
225
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000226 // Determines if a network should be ignored. This should only be determined
227 // based on the network's property instead of any individual IP.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 bool IsIgnoredNetwork(const Network& network) const;
229
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800230 // This function connects a UDP socket to a public address and returns the
231 // local address associated it. Since it binds to the "any" address
232 // internally, it returns the default local address on a multi-homed endpoint.
233 IPAddress QueryDefaultLocalAddress(int family) const;
234
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 private:
236 friend class NetworkTest;
237
honghaiz023f3ef2015-10-19 09:39:32 -0700238 // Creates a network monitor and listens for network updates.
239 void StartNetworkMonitor();
240 // Stops and removes the network monitor.
241 void StopNetworkMonitor();
242 // Called when it receives updates from the network monitor.
243 void OnNetworksChanged();
244
245 // Updates the networks and reschedules the next update.
246 void UpdateNetworksContinually();
247 // Only updates the networks; does not reschedule the next update.
248 void UpdateNetworksOnce();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000249
250 Thread* thread_;
251 bool sent_first_update_;
252 int start_count_;
253 std::vector<std::string> network_ignore_list_;
254 bool ignore_non_default_routes_;
honghaiz023f3ef2015-10-19 09:39:32 -0700255 scoped_ptr<NetworkMonitorInterface> network_monitor_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000256};
257
258// Represents a Unix-type network interface, with a name and single address.
259class Network {
260 public:
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800261 Network(const std::string& name,
262 const std::string& description,
263 const IPAddress& prefix,
264 int prefix_length);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800266 Network(const std::string& name,
267 const std::string& description,
268 const IPAddress& prefix,
269 int prefix_length,
270 AdapterType type);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000271 ~Network();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272
honghaize3c6c822016-02-17 13:00:28 -0800273 sigslot::signal1<const Network*> SignalInactive;
274
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800275 const DefaultLocalAddressProvider* default_local_address_provider() {
276 return default_local_address_provider_;
277 }
278 void set_default_local_address_provider(
279 const DefaultLocalAddressProvider* provider) {
280 default_local_address_provider_ = provider;
281 }
282
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000283 // Returns the name of the interface this network is associated wtih.
284 const std::string& name() const { return name_; }
285
286 // Returns the OS-assigned name for this network. This is useful for
287 // debugging but should not be sent over the wire (for privacy reasons).
288 const std::string& description() const { return description_; }
289
290 // Returns the prefix for this network.
291 const IPAddress& prefix() const { return prefix_; }
292 // Returns the length, in bits, of this network's prefix.
293 int prefix_length() const { return prefix_length_; }
294
295 // |key_| has unique value per network interface. Used in sorting network
296 // interfaces. Key is derived from interface name and it's prefix.
297 std::string key() const { return key_; }
298
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000299 // Returns the Network's current idea of the 'best' IP it has.
300 // Or return an unset IP if this network has no active addresses.
301 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
302 // 1) return all global temporary dynamic and non-deprecrated ones.
303 // 2) if #1 not available, return global ones.
304 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
305 // for unique local address, which is not route-able in open
306 // internet but might be useful for a close WebRTC deployment.
307
308 // TODO(guoweis): rule #3 actually won't happen at current
309 // implementation. The reason being that ULA address starting with
310 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
311 // that is WebRTC will have one extra Network to generate candidates
312 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
313 // ULA should only be tried in a close deployment anyway.
314
315 // Note that when not specifying any flag, it's treated as case global
316 // IPv6 address
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000317 IPAddress GetBestIP() const;
318
319 // Keep the original function here for now.
320 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
321 IPAddress ip() const { return GetBestIP(); }
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000322
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323 // Adds an active IP address to this network. Does not check for duplicates.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000324 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325
326 // Sets the network's IP address list. Returns true if new IP addresses were
327 // detected. Passing true to already_changed skips this check.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000328 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000329 // Get the list of IP Addresses associated with this network.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000330 const std::vector<InterfaceAddress>& GetIPs() const { return ips_;}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000331 // Clear the network's list of addresses.
332 void ClearIPs() { ips_.clear(); }
333
334 // Returns the scope-id of the network's address.
335 // Should only be relevant for link-local IPv6 addresses.
336 int scope_id() const { return scope_id_; }
337 void set_scope_id(int id) { scope_id_ = id; }
338
339 // Indicates whether this network should be ignored, perhaps because
340 // the IP is 0, or the interface is one we know is invalid.
341 bool ignored() const { return ignored_; }
342 void set_ignored(bool ignored) { ignored_ = ignored; }
343
344 AdapterType type() const { return type_; }
honghaize2af9ef2016-03-03 08:27:47 -0800345 void set_type(AdapterType type) { type_ = type; }
346
honghaiza0c44ea2016-03-23 16:07:48 -0700347 // A unique id assigned by the network manager, which may be signaled
348 // to the remote side in the candidate.
349 uint16_t id() const { return id_; }
350 void set_id(uint16_t id) { id_ = id; }
351
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000352 int preference() const { return preference_; }
353 void set_preference(int preference) { preference_ = preference; }
354
honghaizdb8cf502015-12-21 13:08:46 -0800355 // When we enumerate networks and find a previously-seen network is missing,
356 // we do not remove it (because it may be used elsewhere). Instead, we mark
357 // it inactive, so that we can detect network changes properly.
358 bool active() const { return active_; }
honghaize3c6c822016-02-17 13:00:28 -0800359 void set_active(bool active) {
360 if (active_ == active) {
361 return;
362 }
363 active_ = active;
364 if (!active) {
365 SignalInactive(this);
366 }
367 }
honghaizdb8cf502015-12-21 13:08:46 -0800368
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000369 // Debugging description of this network
370 std::string ToString() const;
371
372 private:
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800373 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374 std::string name_;
375 std::string description_;
376 IPAddress prefix_;
377 int prefix_length_;
378 std::string key_;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000379 std::vector<InterfaceAddress> ips_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000380 int scope_id_;
381 bool ignored_;
382 AdapterType type_;
383 int preference_;
honghaizdb8cf502015-12-21 13:08:46 -0800384 bool active_ = true;
honghaiza0c44ea2016-03-23 16:07:48 -0700385 uint16_t id_ = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000386
387 friend class NetworkManager;
388};
389
390} // namespace rtc
391
392#endif // WEBRTC_BASE_NETWORK_H_