blob: d2e013e0cf1efdfc05d0fc4af7d5455dc462092a [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"
21#include "webrtc/base/messagehandler.h"
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000022#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023#include "webrtc/base/sigslot.h"
24
25#if defined(WEBRTC_POSIX)
26struct ifaddrs;
27#endif // defined(WEBRTC_POSIX)
28
29namespace rtc {
30
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080031extern const char kPublicIPv4Host[];
32extern const char kPublicIPv6Host[];
33
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034class Network;
honghaiz023f3ef2015-10-19 09:39:32 -070035class NetworkMonitorInterface;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036class Thread;
37
38enum AdapterType {
39 // This enum resembles the one in Chromium net::ConnectionType.
40 ADAPTER_TYPE_UNKNOWN = 0,
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000041 ADAPTER_TYPE_ETHERNET = 1 << 0,
42 ADAPTER_TYPE_WIFI = 1 << 1,
43 ADAPTER_TYPE_CELLULAR = 1 << 2,
44 ADAPTER_TYPE_VPN = 1 << 3,
45 ADAPTER_TYPE_LOOPBACK = 1 << 4
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046};
47
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000048// By default, ignore loopback interfaces on the host.
49const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
50
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051// Makes a string key for this network. Used in the network manager's maps.
52// Network objects are keyed on interface name, network prefix and the
53// length of that prefix.
54std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
55 int prefix_length);
56
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080057class DefaultLocalAddressProvider {
58 public:
59 virtual ~DefaultLocalAddressProvider() = default;
60 // The default local address is the local address used in multi-homed endpoint
Guo-wei Shiehe03cab92015-11-11 11:11:19 -080061 // when the any address (0.0.0.0 or ::) is used as the local address. It's
62 // important to check the return value as a IP family may not be enabled.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080063 virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
64};
65
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066// Generic network manager interface. It provides list of local
67// networks.
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080068class NetworkManager : public DefaultLocalAddressProvider {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 public:
70 typedef std::vector<Network*> NetworkList;
71
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070072 // This enum indicates whether adapter enumeration is allowed.
73 enum EnumerationPermission {
guoweisea1012b2015-08-21 09:06:28 -070074 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
75 // from GetNetworks means that there is no network
76 // available.
77 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
78 // GetAnyAddressNetworks() should be used instead.
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070079 };
80
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 NetworkManager();
Guo-wei Shieh9af97f82015-11-10 14:47:39 -080082 ~NetworkManager() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083
84 // Called when network list is updated.
85 sigslot::signal0<> SignalNetworksChanged;
86
87 // Indicates a failure when getting list of network interfaces.
88 sigslot::signal0<> SignalError;
89
90 // Start/Stop monitoring of network interfaces
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000091 // list. SignalNetworksChanged or SignalError is emitted immediately
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092 // after StartUpdating() is called. After that SignalNetworksChanged
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000093 // is emitted whenever list of networks changes.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094 virtual void StartUpdating() = 0;
95 virtual void StopUpdating() = 0;
96
97 // Returns the current list of networks available on this machine.
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070098 // StartUpdating() must be called before this method is called.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099 // It makes sure that repeated calls return the same object for a
100 // given network, so that quality is tracked appropriately. Does not
101 // include ignored networks.
102 virtual void GetNetworks(NetworkList* networks) const = 0;
103
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700104 // return the current permission state of GetNetworks()
Guo-wei Shieh86cb9232015-08-19 10:57:53 -0700105 virtual EnumerationPermission enumeration_permission() const;
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700106
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000107 // "AnyAddressNetwork" is a network which only contains single "any address"
108 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
109 // useful as binding to such interfaces allow default routing behavior like
110 // http traffic.
guoweis@webrtc.org9dfe7aa2015-02-18 20:27:17 +0000111 // TODO(guoweis): remove this body when chromium implements this.
112 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000113
honghaizdb8cf502015-12-21 13:08:46 -0800114 // Dumps the current list of networks in the network manager.
115 virtual void DumpNetworks() {}
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800116 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
117
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000118 struct Stats {
119 int ipv4_network_count;
120 int ipv6_network_count;
121 Stats() {
122 ipv4_network_count = 0;
123 ipv6_network_count = 0;
124 }
125 };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126};
127
128// Base class for NetworkManager implementations.
129class NetworkManagerBase : public NetworkManager {
130 public:
131 NetworkManagerBase();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000132 ~NetworkManagerBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000134 void GetNetworks(std::vector<Network*>* networks) const override;
135 void GetAnyAddressNetworks(NetworkList* networks) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136 bool ipv6_enabled() const { return ipv6_enabled_; }
137 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
138
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000139 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
140 int max_ipv6_networks() { return max_ipv6_networks_; }
141
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700142 EnumerationPermission enumeration_permission() const override;
143
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800144 bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override;
145
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146 protected:
147 typedef std::map<std::string, Network*> NetworkMap;
148 // Updates |networks_| with the networks listed in |list|. If
149 // |network_map_| already has a Network object for a network listed
150 // in the |list| then it is reused. Accept ownership of the Network
151 // objects in the |list|. |changed| will be set to true if there is
152 // any change in the network list.
153 void MergeNetworkList(const NetworkList& list, bool* changed);
154
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000155 // |stats| will be populated even if |*changed| is false.
156 void MergeNetworkList(const NetworkList& list,
157 bool* changed,
158 NetworkManager::Stats* stats);
159
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700160 void set_enumeration_permission(EnumerationPermission state) {
161 enumeration_permission_ = state;
162 }
163
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800164 void set_default_local_addresses(const IPAddress& ipv4,
165 const IPAddress& ipv6);
166
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000167 private:
168 friend class NetworkTest;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700170 EnumerationPermission enumeration_permission_;
171
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 NetworkList networks_;
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000173 int max_ipv6_networks_;
174
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 NetworkMap networks_map_;
176 bool ipv6_enabled_;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000177
178 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_;
179 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_;
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800180
181 IPAddress default_local_ipv4_address_;
182 IPAddress default_local_ipv6_address_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183};
184
185// Basic implementation of the NetworkManager interface that gets list
186// of networks using OS APIs.
187class BasicNetworkManager : public NetworkManagerBase,
honghaiz023f3ef2015-10-19 09:39:32 -0700188 public MessageHandler,
189 public sigslot::has_slots<> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190 public:
191 BasicNetworkManager();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000192 ~BasicNetworkManager() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000194 void StartUpdating() override;
195 void StopUpdating() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000196
honghaizdb8cf502015-12-21 13:08:46 -0800197 void DumpNetworks() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198
199 // MessageHandler interface.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000200 void OnMessage(Message* msg) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000201 bool started() { return start_count_ > 0; }
202
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000203 // Sets the network ignore list, which is empty by default. Any network on the
204 // ignore list will be filtered from network enumeration results.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205 void set_network_ignore_list(const std::vector<std::string>& list) {
206 network_ignore_list_ = list;
207 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000208
209 // Sets the network types to ignore. For instance, calling this with
210 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
211 // loopback interfaces. Set to kDefaultNetworkIgnoreMask by default.
212 void set_network_ignore_mask(int network_ignore_mask) {
213 // TODO(phoglund): implement support for other types than loopback.
214 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
215 // Then remove set_network_ignore_list.
216 network_ignore_mask_ = network_ignore_mask;
217 }
218
219 int network_ignore_mask() const { return network_ignore_mask_; }
220
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221#if defined(WEBRTC_LINUX)
222 // Sets the flag for ignoring non-default routes.
223 void set_ignore_non_default_routes(bool value) {
224 ignore_non_default_routes_ = true;
225 }
226#endif
227
228 protected:
229#if defined(WEBRTC_POSIX)
230 // Separated from CreateNetworks for tests.
231 void ConvertIfAddrs(ifaddrs* interfaces,
232 bool include_ignored,
233 NetworkList* networks) const;
234#endif // defined(WEBRTC_POSIX)
235
236 // Creates a network object for each network available on the machine.
237 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
238
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000239 // Determines if a network should be ignored. This should only be determined
240 // based on the network's property instead of any individual IP.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241 bool IsIgnoredNetwork(const Network& network) const;
242
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800243 // This function connects a UDP socket to a public address and returns the
244 // local address associated it. Since it binds to the "any" address
245 // internally, it returns the default local address on a multi-homed endpoint.
246 IPAddress QueryDefaultLocalAddress(int family) const;
247
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248 private:
249 friend class NetworkTest;
250
honghaiz023f3ef2015-10-19 09:39:32 -0700251 // Creates a network monitor and listens for network updates.
252 void StartNetworkMonitor();
253 // Stops and removes the network monitor.
254 void StopNetworkMonitor();
255 // Called when it receives updates from the network monitor.
256 void OnNetworksChanged();
257
258 // Updates the networks and reschedules the next update.
259 void UpdateNetworksContinually();
260 // Only updates the networks; does not reschedule the next update.
261 void UpdateNetworksOnce();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262
263 Thread* thread_;
264 bool sent_first_update_;
265 int start_count_;
266 std::vector<std::string> network_ignore_list_;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000267 int network_ignore_mask_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000268 bool ignore_non_default_routes_;
honghaiz023f3ef2015-10-19 09:39:32 -0700269 scoped_ptr<NetworkMonitorInterface> network_monitor_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000270};
271
272// Represents a Unix-type network interface, with a name and single address.
273class Network {
274 public:
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800275 Network(const std::string& name,
276 const std::string& description,
277 const IPAddress& prefix,
278 int prefix_length);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800280 Network(const std::string& name,
281 const std::string& description,
282 const IPAddress& prefix,
283 int prefix_length,
284 AdapterType type);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000285 ~Network();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800287 const DefaultLocalAddressProvider* default_local_address_provider() {
288 return default_local_address_provider_;
289 }
290 void set_default_local_address_provider(
291 const DefaultLocalAddressProvider* provider) {
292 default_local_address_provider_ = provider;
293 }
294
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000295 // Returns the name of the interface this network is associated wtih.
296 const std::string& name() const { return name_; }
297
298 // Returns the OS-assigned name for this network. This is useful for
299 // debugging but should not be sent over the wire (for privacy reasons).
300 const std::string& description() const { return description_; }
301
302 // Returns the prefix for this network.
303 const IPAddress& prefix() const { return prefix_; }
304 // Returns the length, in bits, of this network's prefix.
305 int prefix_length() const { return prefix_length_; }
306
307 // |key_| has unique value per network interface. Used in sorting network
308 // interfaces. Key is derived from interface name and it's prefix.
309 std::string key() const { return key_; }
310
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000311 // Returns the Network's current idea of the 'best' IP it has.
312 // Or return an unset IP if this network has no active addresses.
313 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
314 // 1) return all global temporary dynamic and non-deprecrated ones.
315 // 2) if #1 not available, return global ones.
316 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
317 // for unique local address, which is not route-able in open
318 // internet but might be useful for a close WebRTC deployment.
319
320 // TODO(guoweis): rule #3 actually won't happen at current
321 // implementation. The reason being that ULA address starting with
322 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
323 // that is WebRTC will have one extra Network to generate candidates
324 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
325 // ULA should only be tried in a close deployment anyway.
326
327 // Note that when not specifying any flag, it's treated as case global
328 // IPv6 address
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000329 IPAddress GetBestIP() const;
330
331 // Keep the original function here for now.
332 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
333 IPAddress ip() const { return GetBestIP(); }
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000334
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000335 // Adds an active IP address to this network. Does not check for duplicates.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000336 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000337
338 // Sets the network's IP address list. Returns true if new IP addresses were
339 // detected. Passing true to already_changed skips this check.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000340 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341 // Get the list of IP Addresses associated with this network.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000342 const std::vector<InterfaceAddress>& GetIPs() const { return ips_;}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000343 // Clear the network's list of addresses.
344 void ClearIPs() { ips_.clear(); }
345
346 // Returns the scope-id of the network's address.
347 // Should only be relevant for link-local IPv6 addresses.
348 int scope_id() const { return scope_id_; }
349 void set_scope_id(int id) { scope_id_ = id; }
350
351 // Indicates whether this network should be ignored, perhaps because
352 // the IP is 0, or the interface is one we know is invalid.
353 bool ignored() const { return ignored_; }
354 void set_ignored(bool ignored) { ignored_ = ignored; }
355
356 AdapterType type() const { return type_; }
357 int preference() const { return preference_; }
358 void set_preference(int preference) { preference_ = preference; }
359
honghaizdb8cf502015-12-21 13:08:46 -0800360 // When we enumerate networks and find a previously-seen network is missing,
361 // we do not remove it (because it may be used elsewhere). Instead, we mark
362 // it inactive, so that we can detect network changes properly.
363 bool active() const { return active_; }
364 void set_active(bool active) { active_ = active; }
365
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 // Debugging description of this network
367 std::string ToString() const;
368
369 private:
Guo-wei Shieh9af97f82015-11-10 14:47:39 -0800370 const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000371 std::string name_;
372 std::string description_;
373 IPAddress prefix_;
374 int prefix_length_;
375 std::string key_;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000376 std::vector<InterfaceAddress> ips_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000377 int scope_id_;
378 bool ignored_;
379 AdapterType type_;
380 int preference_;
honghaizdb8cf502015-12-21 13:08:46 -0800381 bool active_ = true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000382
383 friend class NetworkManager;
384};
385
386} // namespace rtc
387
388#endif // WEBRTC_BASE_NETWORK_H_