blob: cd013001b9fb09ff8d552a79a32ca145f5fc9c4b [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"
22#include "webrtc/base/sigslot.h"
23
24#if defined(WEBRTC_POSIX)
25struct ifaddrs;
26#endif // defined(WEBRTC_POSIX)
27
28namespace rtc {
29
30class Network;
31class Thread;
32
33enum AdapterType {
34 // This enum resembles the one in Chromium net::ConnectionType.
35 ADAPTER_TYPE_UNKNOWN = 0,
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000036 ADAPTER_TYPE_ETHERNET = 1 << 0,
37 ADAPTER_TYPE_WIFI = 1 << 1,
38 ADAPTER_TYPE_CELLULAR = 1 << 2,
39 ADAPTER_TYPE_VPN = 1 << 3,
40 ADAPTER_TYPE_LOOPBACK = 1 << 4
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041};
42
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000043// By default, ignore loopback interfaces on the host.
44const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK;
45
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046// Makes a string key for this network. Used in the network manager's maps.
47// Network objects are keyed on interface name, network prefix and the
48// length of that prefix.
49std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
50 int prefix_length);
51
52// Generic network manager interface. It provides list of local
53// networks.
54class NetworkManager {
55 public:
56 typedef std::vector<Network*> NetworkList;
57
58 NetworkManager();
59 virtual ~NetworkManager();
60
61 // Called when network list is updated.
62 sigslot::signal0<> SignalNetworksChanged;
63
64 // Indicates a failure when getting list of network interfaces.
65 sigslot::signal0<> SignalError;
66
67 // Start/Stop monitoring of network interfaces
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000068 // list. SignalNetworksChanged or SignalError is emitted immediately
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 // after StartUpdating() is called. After that SignalNetworksChanged
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000070 // is emitted whenever list of networks changes.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 virtual void StartUpdating() = 0;
72 virtual void StopUpdating() = 0;
73
74 // Returns the current list of networks available on this machine.
75 // UpdateNetworks() must be called before this method is called.
76 // It makes sure that repeated calls return the same object for a
77 // given network, so that quality is tracked appropriately. Does not
78 // include ignored networks.
79 virtual void GetNetworks(NetworkList* networks) const = 0;
80
81 // Dumps a list of networks available to LS_INFO.
82 virtual void DumpNetworks(bool include_ignored) {}
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +000083
84 struct Stats {
85 int ipv4_network_count;
86 int ipv6_network_count;
87 Stats() {
88 ipv4_network_count = 0;
89 ipv6_network_count = 0;
90 }
91 };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092};
93
94// Base class for NetworkManager implementations.
95class NetworkManagerBase : public NetworkManager {
96 public:
97 NetworkManagerBase();
98 virtual ~NetworkManagerBase();
99
100 virtual void GetNetworks(std::vector<Network*>* networks) const;
101 bool ipv6_enabled() const { return ipv6_enabled_; }
102 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
103
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000104 void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; }
105 int max_ipv6_networks() { return max_ipv6_networks_; }
106
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000107 protected:
108 typedef std::map<std::string, Network*> NetworkMap;
109 // Updates |networks_| with the networks listed in |list|. If
110 // |network_map_| already has a Network object for a network listed
111 // in the |list| then it is reused. Accept ownership of the Network
112 // objects in the |list|. |changed| will be set to true if there is
113 // any change in the network list.
114 void MergeNetworkList(const NetworkList& list, bool* changed);
115
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000116 // |stats| will be populated even if |*changed| is false.
117 void MergeNetworkList(const NetworkList& list,
118 bool* changed,
119 NetworkManager::Stats* stats);
120
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121 private:
122 friend class NetworkTest;
123 void DoUpdateNetworks();
124
125 NetworkList networks_;
guoweis@webrtc.org2444d962015-01-30 00:09:28 +0000126 int max_ipv6_networks_;
127
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 NetworkMap networks_map_;
129 bool ipv6_enabled_;
130};
131
132// Basic implementation of the NetworkManager interface that gets list
133// of networks using OS APIs.
134class BasicNetworkManager : public NetworkManagerBase,
135 public MessageHandler {
136 public:
137 BasicNetworkManager();
138 virtual ~BasicNetworkManager();
139
140 virtual void StartUpdating();
141 virtual void StopUpdating();
142
143 // Logs the available networks.
144 virtual void DumpNetworks(bool include_ignored);
145
146 // MessageHandler interface.
147 virtual void OnMessage(Message* msg);
148 bool started() { return start_count_ > 0; }
149
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000150 // Sets the network ignore list, which is empty by default. Any network on the
151 // ignore list will be filtered from network enumeration results.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152 void set_network_ignore_list(const std::vector<std::string>& list) {
153 network_ignore_list_ = list;
154 }
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000155
156 // Sets the network types to ignore. For instance, calling this with
157 // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and
158 // loopback interfaces. Set to kDefaultNetworkIgnoreMask by default.
159 void set_network_ignore_mask(int network_ignore_mask) {
160 // TODO(phoglund): implement support for other types than loopback.
161 // See https://code.google.com/p/webrtc/issues/detail?id=4288.
162 // Then remove set_network_ignore_list.
163 network_ignore_mask_ = network_ignore_mask;
164 }
165
166 int network_ignore_mask() const { return network_ignore_mask_; }
167
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168#if defined(WEBRTC_LINUX)
169 // Sets the flag for ignoring non-default routes.
170 void set_ignore_non_default_routes(bool value) {
171 ignore_non_default_routes_ = true;
172 }
173#endif
174
175 protected:
176#if defined(WEBRTC_POSIX)
177 // Separated from CreateNetworks for tests.
178 void ConvertIfAddrs(ifaddrs* interfaces,
179 bool include_ignored,
180 NetworkList* networks) const;
181#endif // defined(WEBRTC_POSIX)
182
183 // Creates a network object for each network available on the machine.
184 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
185
186 // Determines if a network should be ignored.
187 bool IsIgnoredNetwork(const Network& network) const;
188
189 private:
190 friend class NetworkTest;
191
192 void DoUpdateNetworks();
193
194 Thread* thread_;
195 bool sent_first_update_;
196 int start_count_;
197 std::vector<std::string> network_ignore_list_;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000198 int network_ignore_mask_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199 bool ignore_non_default_routes_;
200};
201
202// Represents a Unix-type network interface, with a name and single address.
203class Network {
204 public:
205 Network(const std::string& name, const std::string& description,
206 const IPAddress& prefix, int prefix_length);
207
208 Network(const std::string& name, const std::string& description,
209 const IPAddress& prefix, int prefix_length, AdapterType type);
210
211 // Returns the name of the interface this network is associated wtih.
212 const std::string& name() const { return name_; }
213
214 // Returns the OS-assigned name for this network. This is useful for
215 // debugging but should not be sent over the wire (for privacy reasons).
216 const std::string& description() const { return description_; }
217
218 // Returns the prefix for this network.
219 const IPAddress& prefix() const { return prefix_; }
220 // Returns the length, in bits, of this network's prefix.
221 int prefix_length() const { return prefix_length_; }
222
223 // |key_| has unique value per network interface. Used in sorting network
224 // interfaces. Key is derived from interface name and it's prefix.
225 std::string key() const { return key_; }
226
227 // Returns the Network's current idea of the 'best' IP it has.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000228 // Or return an unset IP if this network has no active addresses.
229 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000230 // 1) return all global temporary dynamic and non-deprecrated ones.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000231 // 2) if #1 not available, return global ones.
232 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
233 // for unique local address, which is not route-able in open
234 // internet but might be useful for a close WebRTC deployment.
235
236 // TODO(guoweis): rule #3 actually won't happen at current
237 // implementation. The reason being that ULA address starting with
238 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
239 // that is WebRTC will have one extra Network to generate candidates
240 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
241 // ULA should only be tried in a close deployment anyway.
242
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000243 // Note that when not specifying any flag, it's treated as case global
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000244 // IPv6 address
245 IPAddress GetBestIP() const;
246
247 // Keep the original function here for now.
248 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
249 IPAddress ip() const { return GetBestIP(); }
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000250
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000251 // Adds an active IP address to this network. Does not check for duplicates.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000252 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253
254 // Sets the network's IP address list. Returns true if new IP addresses were
255 // detected. Passing true to already_changed skips this check.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000256 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000257 // Get the list of IP Addresses associated with this network.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000258 const std::vector<InterfaceAddress>& GetIPs() const { return ips_;}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000259 // Clear the network's list of addresses.
260 void ClearIPs() { ips_.clear(); }
261
262 // Returns the scope-id of the network's address.
263 // Should only be relevant for link-local IPv6 addresses.
264 int scope_id() const { return scope_id_; }
265 void set_scope_id(int id) { scope_id_ = id; }
266
267 // Indicates whether this network should be ignored, perhaps because
268 // the IP is 0, or the interface is one we know is invalid.
269 bool ignored() const { return ignored_; }
270 void set_ignored(bool ignored) { ignored_ = ignored; }
271
272 AdapterType type() const { return type_; }
273 int preference() const { return preference_; }
274 void set_preference(int preference) { preference_ = preference; }
275
276 // Debugging description of this network
277 std::string ToString() const;
278
279 private:
280 std::string name_;
281 std::string description_;
282 IPAddress prefix_;
283 int prefix_length_;
284 std::string key_;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000285 std::vector<InterfaceAddress> ips_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000286 int scope_id_;
287 bool ignored_;
288 AdapterType type_;
289 int preference_;
290
291 friend class NetworkManager;
292};
293
294} // namespace rtc
295
296#endif // WEBRTC_BASE_NETWORK_H_