blob: f365a7877262ed30c149daa15bfcf63bf07eac0a [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
31class Network;
32class Thread;
33
34enum AdapterType {
35 // This enum resembles the one in Chromium net::ConnectionType.
36 ADAPTER_TYPE_UNKNOWN = 0,
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000037 ADAPTER_TYPE_ETHERNET = 1 << 0,
38 ADAPTER_TYPE_WIFI = 1 << 1,
39 ADAPTER_TYPE_CELLULAR = 1 << 2,
40 ADAPTER_TYPE_VPN = 1 << 3,
41 ADAPTER_TYPE_LOOPBACK = 1 << 4
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042};
43
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000044// By default, ignore loopback interfaces on the host.
45const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
46
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047// Makes a string key for this network. Used in the network manager's maps.
48// Network objects are keyed on interface name, network prefix and the
49// length of that prefix.
50std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
51 int prefix_length);
52
53// Generic network manager interface. It provides list of local
54// networks.
55class NetworkManager {
56 public:
57 typedef std::vector<Network*> NetworkList;
58
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070059 // This enum indicates whether adapter enumeration is allowed.
60 enum EnumerationPermission {
guoweisea1012b2015-08-21 09:06:28 -070061 ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network
62 // from GetNetworks means that there is no network
63 // available.
64 ENUMERATION_BLOCKED, // Adapter enumeration is disabled.
65 // GetAnyAddressNetworks() should be used instead.
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070066 };
67
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 NetworkManager();
69 virtual ~NetworkManager();
70
71 // Called when network list is updated.
72 sigslot::signal0<> SignalNetworksChanged;
73
74 // Indicates a failure when getting list of network interfaces.
75 sigslot::signal0<> SignalError;
76
77 // Start/Stop monitoring of network interfaces
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000078 // list. SignalNetworksChanged or SignalError is emitted immediately
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 // after StartUpdating() is called. After that SignalNetworksChanged
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000080 // is emitted whenever list of networks changes.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 virtual void StartUpdating() = 0;
82 virtual void StopUpdating() = 0;
83
84 // Returns the current list of networks available on this machine.
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070085 // StartUpdating() must be called before this method is called.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 // It makes sure that repeated calls return the same object for a
87 // given network, so that quality is tracked appropriately. Does not
88 // include ignored networks.
89 virtual void GetNetworks(NetworkList* networks) const = 0;
90
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070091 // return the current permission state of GetNetworks()
Guo-wei Shieh86cb9232015-08-19 10:57:53 -070092 virtual EnumerationPermission enumeration_permission() const;
Guo-wei Shieh47872ec2015-08-19 10:32:46 -070093
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000094 // "AnyAddressNetwork" is a network which only contains single "any address"
95 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
96 // useful as binding to such interfaces allow default routing behavior like
97 // http traffic.
guoweis@webrtc.org9dfe7aa2015-02-18 20:27:17 +000098 // TODO(guoweis): remove this body when chromium implements this.
99 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000100
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 // Dumps a list of networks available to LS_INFO.
102 virtual void DumpNetworks(bool include_ignored) {}
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000103
104 struct Stats {
105 int ipv4_network_count;
106 int ipv6_network_count;
107 Stats() {
108 ipv4_network_count = 0;
109 ipv6_network_count = 0;
110 }
111 };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112};
113
114// Base class for NetworkManager implementations.
115class NetworkManagerBase : public NetworkManager {
116 public:
117 NetworkManagerBase();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000118 ~NetworkManagerBase() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000120 void GetNetworks(std::vector<Network*>* networks) const override;
121 void GetAnyAddressNetworks(NetworkList* networks) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000122 bool ipv6_enabled() const { return ipv6_enabled_; }
123 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
124
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000125 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
126 int max_ipv6_networks() { return max_ipv6_networks_; }
127
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700128 EnumerationPermission enumeration_permission() const override;
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 protected:
131 typedef std::map<std::string, Network*> NetworkMap;
132 // Updates |networks_| with the networks listed in |list|. If
133 // |network_map_| already has a Network object for a network listed
134 // in the |list| then it is reused. Accept ownership of the Network
135 // objects in the |list|. |changed| will be set to true if there is
136 // any change in the network list.
137 void MergeNetworkList(const NetworkList& list, bool* changed);
138
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000139 // |stats| will be populated even if |*changed| is false.
140 void MergeNetworkList(const NetworkList& list,
141 bool* changed,
142 NetworkManager::Stats* stats);
143
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700144 void set_enumeration_permission(EnumerationPermission state) {
145 enumeration_permission_ = state;
146 }
147
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148 private:
149 friend class NetworkTest;
150 void DoUpdateNetworks();
151
Guo-wei Shieh47872ec2015-08-19 10:32:46 -0700152 EnumerationPermission enumeration_permission_;
153
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 NetworkList networks_;
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000155 int max_ipv6_networks_;
156
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157 NetworkMap networks_map_;
158 bool ipv6_enabled_;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000159
160 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_;
161 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000162};
163
164// Basic implementation of the NetworkManager interface that gets list
165// of networks using OS APIs.
166class BasicNetworkManager : public NetworkManagerBase,
167 public MessageHandler {
168 public:
169 BasicNetworkManager();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000170 ~BasicNetworkManager() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000172 void StartUpdating() override;
173 void StopUpdating() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174
175 // Logs the available networks.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000176 void DumpNetworks(bool include_ignored) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177
178 // MessageHandler interface.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000179 void OnMessage(Message* msg) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180 bool started() { return start_count_ > 0; }
181
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000182 // Sets the network ignore list, which is empty by default. Any network on the
183 // ignore list will be filtered from network enumeration results.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 void set_network_ignore_list(const std::vector<std::string>& list) {
185 network_ignore_list_ = list;
186 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000187
188 // Sets the network types to ignore. For instance, calling this with
189 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
190 // loopback interfaces. Set to kDefaultNetworkIgnoreMask by default.
191 void set_network_ignore_mask(int network_ignore_mask) {
192 // TODO(phoglund): implement support for other types than loopback.
193 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
194 // Then remove set_network_ignore_list.
195 network_ignore_mask_ = network_ignore_mask;
196 }
197
198 int network_ignore_mask() const { return network_ignore_mask_; }
199
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200#if defined(WEBRTC_LINUX)
201 // Sets the flag for ignoring non-default routes.
202 void set_ignore_non_default_routes(bool value) {
203 ignore_non_default_routes_ = true;
204 }
205#endif
206
207 protected:
208#if defined(WEBRTC_POSIX)
209 // Separated from CreateNetworks for tests.
210 void ConvertIfAddrs(ifaddrs* interfaces,
211 bool include_ignored,
212 NetworkList* networks) const;
213#endif // defined(WEBRTC_POSIX)
214
215 // Creates a network object for each network available on the machine.
216 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
217
guoweis@webrtc.orgb91d0f52015-03-17 14:43:20 +0000218 // Determines if a network should be ignored. This should only be determined
219 // based on the network's property instead of any individual IP.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 bool IsIgnoredNetwork(const Network& network) const;
221
222 private:
223 friend class NetworkTest;
224
225 void DoUpdateNetworks();
226
227 Thread* thread_;
228 bool sent_first_update_;
229 int start_count_;
230 std::vector<std::string> network_ignore_list_;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000231 int network_ignore_mask_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232 bool ignore_non_default_routes_;
233};
234
235// Represents a Unix-type network interface, with a name and single address.
236class Network {
237 public:
238 Network(const std::string& name, const std::string& description,
239 const IPAddress& prefix, int prefix_length);
240
241 Network(const std::string& name, const std::string& description,
242 const IPAddress& prefix, int prefix_length, AdapterType type);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000243 ~Network();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000244
245 // Returns the name of the interface this network is associated wtih.
246 const std::string& name() const { return name_; }
247
248 // Returns the OS-assigned name for this network. This is useful for
249 // debugging but should not be sent over the wire (for privacy reasons).
250 const std::string& description() const { return description_; }
251
252 // Returns the prefix for this network.
253 const IPAddress& prefix() const { return prefix_; }
254 // Returns the length, in bits, of this network's prefix.
255 int prefix_length() const { return prefix_length_; }
256
257 // |key_| has unique value per network interface. Used in sorting network
258 // interfaces. Key is derived from interface name and it's prefix.
259 std::string key() const { return key_; }
260
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000261 // Returns the Network's current idea of the 'best' IP it has.
262 // Or return an unset IP if this network has no active addresses.
263 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
264 // 1) return all global temporary dynamic and non-deprecrated ones.
265 // 2) if #1 not available, return global ones.
266 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
267 // for unique local address, which is not route-able in open
268 // internet but might be useful for a close WebRTC deployment.
269
270 // TODO(guoweis): rule #3 actually won't happen at current
271 // implementation. The reason being that ULA address starting with
272 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
273 // that is WebRTC will have one extra Network to generate candidates
274 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
275 // ULA should only be tried in a close deployment anyway.
276
277 // Note that when not specifying any flag, it's treated as case global
278 // IPv6 address
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000279 IPAddress GetBestIP() const;
280
281 // Keep the original function here for now.
282 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
283 IPAddress ip() const { return GetBestIP(); }
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000284
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 // Adds an active IP address to this network. Does not check for duplicates.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000286 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000287
288 // Sets the network's IP address list. Returns true if new IP addresses were
289 // detected. Passing true to already_changed skips this check.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000290 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291 // Get the list of IP Addresses associated with this network.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000292 const std::vector<InterfaceAddress>& GetIPs() const { return ips_;}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000293 // Clear the network's list of addresses.
294 void ClearIPs() { ips_.clear(); }
295
296 // Returns the scope-id of the network's address.
297 // Should only be relevant for link-local IPv6 addresses.
298 int scope_id() const { return scope_id_; }
299 void set_scope_id(int id) { scope_id_ = id; }
300
301 // Indicates whether this network should be ignored, perhaps because
302 // the IP is 0, or the interface is one we know is invalid.
303 bool ignored() const { return ignored_; }
304 void set_ignored(bool ignored) { ignored_ = ignored; }
305
306 AdapterType type() const { return type_; }
307 int preference() const { return preference_; }
308 void set_preference(int preference) { preference_ = preference; }
309
310 // Debugging description of this network
311 std::string ToString() const;
312
313 private:
314 std::string name_;
315 std::string description_;
316 IPAddress prefix_;
317 int prefix_length_;
318 std::string key_;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000319 std::vector<InterfaceAddress> ips_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000320 int scope_id_;
321 bool ignored_;
322 AdapterType type_;
323 int preference_;
324
325 friend class NetworkManager;
326};
327
328} // namespace rtc
329
330#endif // WEBRTC_BASE_NETWORK_H_