blob: 089d86bbdf41747e6d4d1c35dc177b53d37cb964 [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
59 NetworkManager();
60 virtual ~NetworkManager();
61
62 // Called when network list is updated.
63 sigslot::signal0<> SignalNetworksChanged;
64
65 // Indicates a failure when getting list of network interfaces.
66 sigslot::signal0<> SignalError;
67
68 // Start/Stop monitoring of network interfaces
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000069 // list. SignalNetworksChanged or SignalError is emitted immediately
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000070 // after StartUpdating() is called. After that SignalNetworksChanged
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000071 // is emitted whenever list of networks changes.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 virtual void StartUpdating() = 0;
73 virtual void StopUpdating() = 0;
74
75 // Returns the current list of networks available on this machine.
76 // UpdateNetworks() must be called before this method is called.
77 // It makes sure that repeated calls return the same object for a
78 // given network, so that quality is tracked appropriately. Does not
79 // include ignored networks.
80 virtual void GetNetworks(NetworkList* networks) const = 0;
81
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000082 // "AnyAddressNetwork" is a network which only contains single "any address"
83 // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is
84 // useful as binding to such interfaces allow default routing behavior like
85 // http traffic.
guoweis@webrtc.org9dfe7aa2015-02-18 20:27:17 +000086 // TODO(guoweis): remove this body when chromium implements this.
87 virtual void GetAnyAddressNetworks(NetworkList* networks) {}
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +000088
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 // Dumps a list of networks available to LS_INFO.
90 virtual void DumpNetworks(bool include_ignored) {}
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +000091
92 struct Stats {
93 int ipv4_network_count;
94 int ipv6_network_count;
95 Stats() {
96 ipv4_network_count = 0;
97 ipv6_network_count = 0;
98 }
99 };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100};
101
102// Base class for NetworkManager implementations.
103class NetworkManagerBase : public NetworkManager {
104 public:
105 NetworkManagerBase();
106 virtual ~NetworkManagerBase();
107
108 virtual void GetNetworks(std::vector<Network*>* networks) const;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000109 virtual void GetAnyAddressNetworks(NetworkList* networks);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 bool ipv6_enabled() const { return ipv6_enabled_; }
111 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
112
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000113 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
114 int max_ipv6_networks() { return max_ipv6_networks_; }
115
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 protected:
117 typedef std::map<std::string, Network*> NetworkMap;
118 // Updates |networks_| with the networks listed in |list|. If
119 // |network_map_| already has a Network object for a network listed
120 // in the |list| then it is reused. Accept ownership of the Network
121 // objects in the |list|. |changed| will be set to true if there is
122 // any change in the network list.
123 void MergeNetworkList(const NetworkList& list, bool* changed);
124
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000125 // |stats| will be populated even if |*changed| is false.
126 void MergeNetworkList(const NetworkList& list,
127 bool* changed,
128 NetworkManager::Stats* stats);
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 private:
131 friend class NetworkTest;
132 void DoUpdateNetworks();
133
134 NetworkList networks_;
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000135 int max_ipv6_networks_;
136
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 NetworkMap networks_map_;
138 bool ipv6_enabled_;
guoweis@webrtc.orgf358aea2015-02-18 18:44:01 +0000139
140 rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_;
141 rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142};
143
144// Basic implementation of the NetworkManager interface that gets list
145// of networks using OS APIs.
146class BasicNetworkManager : public NetworkManagerBase,
147 public MessageHandler {
148 public:
149 BasicNetworkManager();
150 virtual ~BasicNetworkManager();
151
152 virtual void StartUpdating();
153 virtual void StopUpdating();
154
155 // Logs the available networks.
156 virtual void DumpNetworks(bool include_ignored);
157
158 // MessageHandler interface.
159 virtual void OnMessage(Message* msg);
160 bool started() { return start_count_ > 0; }
161
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000162 // Sets the network ignore list, which is empty by default. Any network on the
163 // ignore list will be filtered from network enumeration results.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164 void set_network_ignore_list(const std::vector<std::string>& list) {
165 network_ignore_list_ = list;
166 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000167
168 // Sets the network types to ignore. For instance, calling this with
169 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
170 // loopback interfaces. Set to kDefaultNetworkIgnoreMask by default.
171 void set_network_ignore_mask(int network_ignore_mask) {
172 // TODO(phoglund): implement support for other types than loopback.
173 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
174 // Then remove set_network_ignore_list.
175 network_ignore_mask_ = network_ignore_mask;
176 }
177
178 int network_ignore_mask() const { return network_ignore_mask_; }
179
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180#if defined(WEBRTC_LINUX)
181 // Sets the flag for ignoring non-default routes.
182 void set_ignore_non_default_routes(bool value) {
183 ignore_non_default_routes_ = true;
184 }
185#endif
186
187 protected:
188#if defined(WEBRTC_POSIX)
189 // Separated from CreateNetworks for tests.
190 void ConvertIfAddrs(ifaddrs* interfaces,
191 bool include_ignored,
192 NetworkList* networks) const;
193#endif // defined(WEBRTC_POSIX)
194
195 // Creates a network object for each network available on the machine.
196 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
197
198 // Determines if a network should be ignored.
199 bool IsIgnoredNetwork(const Network& network) const;
200
201 private:
202 friend class NetworkTest;
203
204 void DoUpdateNetworks();
205
206 Thread* thread_;
207 bool sent_first_update_;
208 int start_count_;
209 std::vector<std::string> network_ignore_list_;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000210 int network_ignore_mask_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 bool ignore_non_default_routes_;
212};
213
214// Represents a Unix-type network interface, with a name and single address.
215class Network {
216 public:
217 Network(const std::string& name, const std::string& description,
218 const IPAddress& prefix, int prefix_length);
219
220 Network(const std::string& name, const std::string& description,
221 const IPAddress& prefix, int prefix_length, AdapterType type);
222
223 // Returns the name of the interface this network is associated wtih.
224 const std::string& name() const { return name_; }
225
226 // Returns the OS-assigned name for this network. This is useful for
227 // debugging but should not be sent over the wire (for privacy reasons).
228 const std::string& description() const { return description_; }
229
230 // Returns the prefix for this network.
231 const IPAddress& prefix() const { return prefix_; }
232 // Returns the length, in bits, of this network's prefix.
233 int prefix_length() const { return prefix_length_; }
234
235 // |key_| has unique value per network interface. Used in sorting network
236 // interfaces. Key is derived from interface name and it's prefix.
237 std::string key() const { return key_; }
238
aluebs@webrtc.org07dcf602015-02-27 18:42:22 +0000239 // Returns the Network's current idea of the 'best' IP it has.
240 // Or return an unset IP if this network has no active addresses.
241 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
242 // 1) return all global temporary dynamic and non-deprecrated ones.
243 // 2) if #1 not available, return global ones.
244 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
245 // for unique local address, which is not route-able in open
246 // internet but might be useful for a close WebRTC deployment.
247
248 // TODO(guoweis): rule #3 actually won't happen at current
249 // implementation. The reason being that ULA address starting with
250 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
251 // that is WebRTC will have one extra Network to generate candidates
252 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
253 // ULA should only be tried in a close deployment anyway.
254
255 // Note that when not specifying any flag, it's treated as case global
256 // IPv6 address
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000257 IPAddress GetBestIP() const;
258
259 // Keep the original function here for now.
260 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
261 IPAddress ip() const { return GetBestIP(); }
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000262
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 // Adds an active IP address to this network. Does not check for duplicates.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000264 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265
266 // Sets the network's IP address list. Returns true if new IP addresses were
267 // detected. Passing true to already_changed skips this check.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000268 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000269 // Get the list of IP Addresses associated with this network.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000270 const std::vector<InterfaceAddress>& GetIPs() const { return ips_;}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000271 // Clear the network's list of addresses.
272 void ClearIPs() { ips_.clear(); }
273
274 // Returns the scope-id of the network's address.
275 // Should only be relevant for link-local IPv6 addresses.
276 int scope_id() const { return scope_id_; }
277 void set_scope_id(int id) { scope_id_ = id; }
278
279 // Indicates whether this network should be ignored, perhaps because
280 // the IP is 0, or the interface is one we know is invalid.
281 bool ignored() const { return ignored_; }
282 void set_ignored(bool ignored) { ignored_ = ignored; }
283
284 AdapterType type() const { return type_; }
285 int preference() const { return preference_; }
286 void set_preference(int preference) { preference_ = preference; }
287
288 // Debugging description of this network
289 std::string ToString() const;
290
291 private:
292 std::string name_;
293 std::string description_;
294 IPAddress prefix_;
295 int prefix_length_;
296 std::string key_;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000297 std::vector<InterfaceAddress> ips_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 int scope_id_;
299 bool ignored_;
300 AdapterType type_;
301 int preference_;
302
303 friend class NetworkManager;
304};
305
306} // namespace rtc
307
308#endif // WEBRTC_BASE_NETWORK_H_