blob: ee0673f40c230f477dec82d03f022683d258b36e [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,
36 ADAPTER_TYPE_ETHERNET = 1,
37 ADAPTER_TYPE_WIFI = 2,
38 ADAPTER_TYPE_CELLULAR = 3,
39 ADAPTER_TYPE_VPN = 4
40};
41
42// Makes a string key for this network. Used in the network manager's maps.
43// Network objects are keyed on interface name, network prefix and the
44// length of that prefix.
45std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix,
46 int prefix_length);
47
48// Generic network manager interface. It provides list of local
49// networks.
50class NetworkManager {
51 public:
52 typedef std::vector<Network*> NetworkList;
53
54 NetworkManager();
55 virtual ~NetworkManager();
56
57 // Called when network list is updated.
58 sigslot::signal0<> SignalNetworksChanged;
59
60 // Indicates a failure when getting list of network interfaces.
61 sigslot::signal0<> SignalError;
62
63 // Start/Stop monitoring of network interfaces
64 // list. SignalNetworksChanged or SignalError is emitted immidiately
65 // after StartUpdating() is called. After that SignalNetworksChanged
66 // is emitted wheneven list of networks changes.
67 virtual void StartUpdating() = 0;
68 virtual void StopUpdating() = 0;
69
70 // Returns the current list of networks available on this machine.
71 // UpdateNetworks() must be called before this method is called.
72 // It makes sure that repeated calls return the same object for a
73 // given network, so that quality is tracked appropriately. Does not
74 // include ignored networks.
75 virtual void GetNetworks(NetworkList* networks) const = 0;
76
77 // Dumps a list of networks available to LS_INFO.
78 virtual void DumpNetworks(bool include_ignored) {}
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +000079
80 struct Stats {
81 int ipv4_network_count;
82 int ipv6_network_count;
83 Stats() {
84 ipv4_network_count = 0;
85 ipv6_network_count = 0;
86 }
87 };
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088};
89
90// Base class for NetworkManager implementations.
91class NetworkManagerBase : public NetworkManager {
92 public:
93 NetworkManagerBase();
94 virtual ~NetworkManagerBase();
95
96 virtual void GetNetworks(std::vector<Network*>* networks) const;
97 bool ipv6_enabled() const { return ipv6_enabled_; }
98 void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; }
99
100 protected:
101 typedef std::map<std::string, Network*> NetworkMap;
102 // Updates |networks_| with the networks listed in |list|. If
103 // |network_map_| already has a Network object for a network listed
104 // in the |list| then it is reused. Accept ownership of the Network
105 // objects in the |list|. |changed| will be set to true if there is
106 // any change in the network list.
107 void MergeNetworkList(const NetworkList& list, bool* changed);
108
guoweis@webrtc.orga094cac2015-01-28 19:34:05 +0000109 // |stats| will be populated even if |*changed| is false.
110 void MergeNetworkList(const NetworkList& list,
111 bool* changed,
112 NetworkManager::Stats* stats);
113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 private:
115 friend class NetworkTest;
116 void DoUpdateNetworks();
117
118 NetworkList networks_;
119 NetworkMap networks_map_;
120 bool ipv6_enabled_;
121};
122
123// Basic implementation of the NetworkManager interface that gets list
124// of networks using OS APIs.
125class BasicNetworkManager : public NetworkManagerBase,
126 public MessageHandler {
127 public:
128 BasicNetworkManager();
129 virtual ~BasicNetworkManager();
130
131 virtual void StartUpdating();
132 virtual void StopUpdating();
133
134 // Logs the available networks.
135 virtual void DumpNetworks(bool include_ignored);
136
137 // MessageHandler interface.
138 virtual void OnMessage(Message* msg);
139 bool started() { return start_count_ > 0; }
140
141 // Sets the network ignore list, which is empty by default. Any network on
142 // the ignore list will be filtered from network enumeration results.
143 void set_network_ignore_list(const std::vector<std::string>& list) {
144 network_ignore_list_ = list;
145 }
146#if defined(WEBRTC_LINUX)
147 // Sets the flag for ignoring non-default routes.
148 void set_ignore_non_default_routes(bool value) {
149 ignore_non_default_routes_ = true;
150 }
151#endif
152
153 protected:
154#if defined(WEBRTC_POSIX)
155 // Separated from CreateNetworks for tests.
156 void ConvertIfAddrs(ifaddrs* interfaces,
157 bool include_ignored,
158 NetworkList* networks) const;
159#endif // defined(WEBRTC_POSIX)
160
161 // Creates a network object for each network available on the machine.
162 bool CreateNetworks(bool include_ignored, NetworkList* networks) const;
163
164 // Determines if a network should be ignored.
165 bool IsIgnoredNetwork(const Network& network) const;
166
167 private:
168 friend class NetworkTest;
169
170 void DoUpdateNetworks();
171
172 Thread* thread_;
173 bool sent_first_update_;
174 int start_count_;
175 std::vector<std::string> network_ignore_list_;
176 bool ignore_non_default_routes_;
177};
178
179// Represents a Unix-type network interface, with a name and single address.
180class Network {
181 public:
182 Network(const std::string& name, const std::string& description,
183 const IPAddress& prefix, int prefix_length);
184
185 Network(const std::string& name, const std::string& description,
186 const IPAddress& prefix, int prefix_length, AdapterType type);
187
188 // Returns the name of the interface this network is associated wtih.
189 const std::string& name() const { return name_; }
190
191 // Returns the OS-assigned name for this network. This is useful for
192 // debugging but should not be sent over the wire (for privacy reasons).
193 const std::string& description() const { return description_; }
194
195 // Returns the prefix for this network.
196 const IPAddress& prefix() const { return prefix_; }
197 // Returns the length, in bits, of this network's prefix.
198 int prefix_length() const { return prefix_length_; }
199
200 // |key_| has unique value per network interface. Used in sorting network
201 // interfaces. Key is derived from interface name and it's prefix.
202 std::string key() const { return key_; }
203
204 // Returns the Network's current idea of the 'best' IP it has.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000205 // Or return an unset IP if this network has no active addresses.
206 // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000207 // 1) return all global temporary dynamic and non-deprecrated ones.
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000208 // 2) if #1 not available, return global ones.
209 // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
210 // for unique local address, which is not route-able in open
211 // internet but might be useful for a close WebRTC deployment.
212
213 // TODO(guoweis): rule #3 actually won't happen at current
214 // implementation. The reason being that ULA address starting with
215 // 0xfc 0r 0xfd will be grouped into its own Network. The result of
216 // that is WebRTC will have one extra Network to generate candidates
217 // but the lack of rule #3 shouldn't prevent turning on IPv6 since
218 // ULA should only be tried in a close deployment anyway.
219
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000220 // Note that when not specifying any flag, it's treated as case global
guoweis@webrtc.org369a6372014-09-17 22:37:29 +0000221 // IPv6 address
222 IPAddress GetBestIP() const;
223
224 // Keep the original function here for now.
225 // TODO(guoweis): Remove this when all callers are migrated to GetBestIP().
226 IPAddress ip() const { return GetBestIP(); }
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000227
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 // Adds an active IP address to this network. Does not check for duplicates.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000229 void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230
231 // Sets the network's IP address list. Returns true if new IP addresses were
232 // detected. Passing true to already_changed skips this check.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000233 bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234 // Get the list of IP Addresses associated with this network.
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000235 const std::vector<InterfaceAddress>& GetIPs() const { return ips_;}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236 // Clear the network's list of addresses.
237 void ClearIPs() { ips_.clear(); }
238
239 // Returns the scope-id of the network's address.
240 // Should only be relevant for link-local IPv6 addresses.
241 int scope_id() const { return scope_id_; }
242 void set_scope_id(int id) { scope_id_ = id; }
243
244 // Indicates whether this network should be ignored, perhaps because
245 // the IP is 0, or the interface is one we know is invalid.
246 bool ignored() const { return ignored_; }
247 void set_ignored(bool ignored) { ignored_ = ignored; }
248
249 AdapterType type() const { return type_; }
250 int preference() const { return preference_; }
251 void set_preference(int preference) { preference_ = preference; }
252
253 // Debugging description of this network
254 std::string ToString() const;
255
256 private:
257 std::string name_;
258 std::string description_;
259 IPAddress prefix_;
260 int prefix_length_;
261 std::string key_;
guoweis@webrtc.orgfa603982014-09-09 23:42:40 +0000262 std::vector<InterfaceAddress> ips_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263 int scope_id_;
264 bool ignored_;
265 AdapterType type_;
266 int preference_;
267
268 friend class NetworkManager;
269};
270
271} // namespace rtc
272
273#endif // WEBRTC_BASE_NETWORK_H_