henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 22 | #include "webrtc/base/scoped_ptr.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | #include "webrtc/base/sigslot.h" |
| 24 | |
| 25 | #if defined(WEBRTC_POSIX) |
| 26 | struct ifaddrs; |
| 27 | #endif // defined(WEBRTC_POSIX) |
| 28 | |
| 29 | namespace rtc { |
| 30 | |
| 31 | class Network; |
| 32 | class Thread; |
| 33 | |
| 34 | enum AdapterType { |
| 35 | // This enum resembles the one in Chromium net::ConnectionType. |
| 36 | ADAPTER_TYPE_UNKNOWN = 0, |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 37 | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 44 | // By default, ignore loopback interfaces on the host. |
| 45 | const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK; |
| 46 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | // 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. |
| 50 | std::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. |
| 55 | class NetworkManager { |
| 56 | public: |
| 57 | typedef std::vector<Network*> NetworkList; |
| 58 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame^] | 59 | // This enum indicates whether adapter enumeration is allowed. |
| 60 | enum EnumerationPermission { |
| 61 | kEnumerationAllowed, // Adapter enumeration is allowed. Getting 0 |
| 62 | // network from GetNetworks means that there is no |
| 63 | // network available. |
| 64 | kEnumerationDisallowed, // Adapter enumeration is |
| 65 | // disabled. GetAnyAddressNetworks() should be used |
| 66 | // instead. |
| 67 | }; |
| 68 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | NetworkManager(); |
| 70 | virtual ~NetworkManager(); |
| 71 | |
| 72 | // Called when network list is updated. |
| 73 | sigslot::signal0<> SignalNetworksChanged; |
| 74 | |
| 75 | // Indicates a failure when getting list of network interfaces. |
| 76 | sigslot::signal0<> SignalError; |
| 77 | |
| 78 | // Start/Stop monitoring of network interfaces |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 79 | // list. SignalNetworksChanged or SignalError is emitted immediately |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | // after StartUpdating() is called. After that SignalNetworksChanged |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 81 | // is emitted whenever list of networks changes. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | virtual void StartUpdating() = 0; |
| 83 | virtual void StopUpdating() = 0; |
| 84 | |
| 85 | // Returns the current list of networks available on this machine. |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame^] | 86 | // StartUpdating() must be called before this method is called. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | // It makes sure that repeated calls return the same object for a |
| 88 | // given network, so that quality is tracked appropriately. Does not |
| 89 | // include ignored networks. |
| 90 | virtual void GetNetworks(NetworkList* networks) const = 0; |
| 91 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame^] | 92 | // return the current permission state of GetNetworks() |
| 93 | virtual EnumerationPermission enumeration_permission() const { |
| 94 | return kEnumerationAllowed; |
| 95 | } |
| 96 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 97 | // "AnyAddressNetwork" is a network which only contains single "any address" |
| 98 | // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is |
| 99 | // useful as binding to such interfaces allow default routing behavior like |
| 100 | // http traffic. |
guoweis@webrtc.org | 9dfe7aa | 2015-02-18 20:27:17 +0000 | [diff] [blame] | 101 | // TODO(guoweis): remove this body when chromium implements this. |
| 102 | virtual void GetAnyAddressNetworks(NetworkList* networks) {} |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 103 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | // Dumps a list of networks available to LS_INFO. |
| 105 | virtual void DumpNetworks(bool include_ignored) {} |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 106 | |
| 107 | struct Stats { |
| 108 | int ipv4_network_count; |
| 109 | int ipv6_network_count; |
| 110 | Stats() { |
| 111 | ipv4_network_count = 0; |
| 112 | ipv6_network_count = 0; |
| 113 | } |
| 114 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | // Base class for NetworkManager implementations. |
| 118 | class NetworkManagerBase : public NetworkManager { |
| 119 | public: |
| 120 | NetworkManagerBase(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 121 | ~NetworkManagerBase() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 123 | void GetNetworks(std::vector<Network*>* networks) const override; |
| 124 | void GetAnyAddressNetworks(NetworkList* networks) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | bool ipv6_enabled() const { return ipv6_enabled_; } |
| 126 | void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; } |
| 127 | |
guoweis@webrtc.org | 2444d96 | 2015-01-30 00:09:28 +0000 | [diff] [blame] | 128 | void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } |
| 129 | int max_ipv6_networks() { return max_ipv6_networks_; } |
| 130 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame^] | 131 | EnumerationPermission enumeration_permission() const override; |
| 132 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | protected: |
| 134 | typedef std::map<std::string, Network*> NetworkMap; |
| 135 | // Updates |networks_| with the networks listed in |list|. If |
| 136 | // |network_map_| already has a Network object for a network listed |
| 137 | // in the |list| then it is reused. Accept ownership of the Network |
| 138 | // objects in the |list|. |changed| will be set to true if there is |
| 139 | // any change in the network list. |
| 140 | void MergeNetworkList(const NetworkList& list, bool* changed); |
| 141 | |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 142 | // |stats| will be populated even if |*changed| is false. |
| 143 | void MergeNetworkList(const NetworkList& list, |
| 144 | bool* changed, |
| 145 | NetworkManager::Stats* stats); |
| 146 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame^] | 147 | void set_enumeration_permission(EnumerationPermission state) { |
| 148 | enumeration_permission_ = state; |
| 149 | } |
| 150 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | private: |
| 152 | friend class NetworkTest; |
| 153 | void DoUpdateNetworks(); |
| 154 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame^] | 155 | EnumerationPermission enumeration_permission_; |
| 156 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 157 | NetworkList networks_; |
guoweis@webrtc.org | 2444d96 | 2015-01-30 00:09:28 +0000 | [diff] [blame] | 158 | int max_ipv6_networks_; |
| 159 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | NetworkMap networks_map_; |
| 161 | bool ipv6_enabled_; |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 162 | |
| 163 | rtc::scoped_ptr<rtc::Network> ipv4_any_address_network_; |
| 164 | rtc::scoped_ptr<rtc::Network> ipv6_any_address_network_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | // Basic implementation of the NetworkManager interface that gets list |
| 168 | // of networks using OS APIs. |
| 169 | class BasicNetworkManager : public NetworkManagerBase, |
| 170 | public MessageHandler { |
| 171 | public: |
| 172 | BasicNetworkManager(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 173 | ~BasicNetworkManager() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 174 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 175 | void StartUpdating() override; |
| 176 | void StopUpdating() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | |
| 178 | // Logs the available networks. |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 179 | void DumpNetworks(bool include_ignored) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | |
| 181 | // MessageHandler interface. |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 182 | void OnMessage(Message* msg) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | bool started() { return start_count_ > 0; } |
| 184 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 185 | // Sets the network ignore list, which is empty by default. Any network on the |
| 186 | // ignore list will be filtered from network enumeration results. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | void set_network_ignore_list(const std::vector<std::string>& list) { |
| 188 | network_ignore_list_ = list; |
| 189 | } |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 190 | |
| 191 | // Sets the network types to ignore. For instance, calling this with |
| 192 | // ADAPTER_TYPE_ETHERNET | ADAPTER_TYPE_LOOPBACK will ignore Ethernet and |
| 193 | // loopback interfaces. Set to kDefaultNetworkIgnoreMask by default. |
| 194 | void set_network_ignore_mask(int network_ignore_mask) { |
| 195 | // TODO(phoglund): implement support for other types than loopback. |
| 196 | // See https://code.google.com/p/webrtc/issues/detail?id=4288. |
| 197 | // Then remove set_network_ignore_list. |
| 198 | network_ignore_mask_ = network_ignore_mask; |
| 199 | } |
| 200 | |
| 201 | int network_ignore_mask() const { return network_ignore_mask_; } |
| 202 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 203 | #if defined(WEBRTC_LINUX) |
| 204 | // Sets the flag for ignoring non-default routes. |
| 205 | void set_ignore_non_default_routes(bool value) { |
| 206 | ignore_non_default_routes_ = true; |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | protected: |
| 211 | #if defined(WEBRTC_POSIX) |
| 212 | // Separated from CreateNetworks for tests. |
| 213 | void ConvertIfAddrs(ifaddrs* interfaces, |
| 214 | bool include_ignored, |
| 215 | NetworkList* networks) const; |
| 216 | #endif // defined(WEBRTC_POSIX) |
| 217 | |
| 218 | // Creates a network object for each network available on the machine. |
| 219 | bool CreateNetworks(bool include_ignored, NetworkList* networks) const; |
| 220 | |
guoweis@webrtc.org | b91d0f5 | 2015-03-17 14:43:20 +0000 | [diff] [blame] | 221 | // Determines if a network should be ignored. This should only be determined |
| 222 | // based on the network's property instead of any individual IP. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 223 | bool IsIgnoredNetwork(const Network& network) const; |
| 224 | |
| 225 | private: |
| 226 | friend class NetworkTest; |
| 227 | |
| 228 | void DoUpdateNetworks(); |
| 229 | |
| 230 | Thread* thread_; |
| 231 | bool sent_first_update_; |
| 232 | int start_count_; |
| 233 | std::vector<std::string> network_ignore_list_; |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 234 | int network_ignore_mask_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | bool ignore_non_default_routes_; |
| 236 | }; |
| 237 | |
| 238 | // Represents a Unix-type network interface, with a name and single address. |
| 239 | class Network { |
| 240 | public: |
| 241 | Network(const std::string& name, const std::string& description, |
| 242 | const IPAddress& prefix, int prefix_length); |
| 243 | |
| 244 | Network(const std::string& name, const std::string& description, |
| 245 | const IPAddress& prefix, int prefix_length, AdapterType type); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 246 | ~Network(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 247 | |
| 248 | // Returns the name of the interface this network is associated wtih. |
| 249 | const std::string& name() const { return name_; } |
| 250 | |
| 251 | // Returns the OS-assigned name for this network. This is useful for |
| 252 | // debugging but should not be sent over the wire (for privacy reasons). |
| 253 | const std::string& description() const { return description_; } |
| 254 | |
| 255 | // Returns the prefix for this network. |
| 256 | const IPAddress& prefix() const { return prefix_; } |
| 257 | // Returns the length, in bits, of this network's prefix. |
| 258 | int prefix_length() const { return prefix_length_; } |
| 259 | |
| 260 | // |key_| has unique value per network interface. Used in sorting network |
| 261 | // interfaces. Key is derived from interface name and it's prefix. |
| 262 | std::string key() const { return key_; } |
| 263 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 264 | // Returns the Network's current idea of the 'best' IP it has. |
| 265 | // Or return an unset IP if this network has no active addresses. |
| 266 | // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC. |
| 267 | // 1) return all global temporary dynamic and non-deprecrated ones. |
| 268 | // 2) if #1 not available, return global ones. |
| 269 | // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands |
| 270 | // for unique local address, which is not route-able in open |
| 271 | // internet but might be useful for a close WebRTC deployment. |
| 272 | |
| 273 | // TODO(guoweis): rule #3 actually won't happen at current |
| 274 | // implementation. The reason being that ULA address starting with |
| 275 | // 0xfc 0r 0xfd will be grouped into its own Network. The result of |
| 276 | // that is WebRTC will have one extra Network to generate candidates |
| 277 | // but the lack of rule #3 shouldn't prevent turning on IPv6 since |
| 278 | // ULA should only be tried in a close deployment anyway. |
| 279 | |
| 280 | // Note that when not specifying any flag, it's treated as case global |
| 281 | // IPv6 address |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 282 | IPAddress GetBestIP() const; |
| 283 | |
| 284 | // Keep the original function here for now. |
| 285 | // TODO(guoweis): Remove this when all callers are migrated to GetBestIP(). |
| 286 | IPAddress ip() const { return GetBestIP(); } |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 287 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 288 | // Adds an active IP address to this network. Does not check for duplicates. |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 289 | void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 290 | |
| 291 | // Sets the network's IP address list. Returns true if new IP addresses were |
| 292 | // detected. Passing true to already_changed skips this check. |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 293 | bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 294 | // Get the list of IP Addresses associated with this network. |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 295 | const std::vector<InterfaceAddress>& GetIPs() const { return ips_;} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 296 | // Clear the network's list of addresses. |
| 297 | void ClearIPs() { ips_.clear(); } |
| 298 | |
| 299 | // Returns the scope-id of the network's address. |
| 300 | // Should only be relevant for link-local IPv6 addresses. |
| 301 | int scope_id() const { return scope_id_; } |
| 302 | void set_scope_id(int id) { scope_id_ = id; } |
| 303 | |
| 304 | // Indicates whether this network should be ignored, perhaps because |
| 305 | // the IP is 0, or the interface is one we know is invalid. |
| 306 | bool ignored() const { return ignored_; } |
| 307 | void set_ignored(bool ignored) { ignored_ = ignored; } |
| 308 | |
| 309 | AdapterType type() const { return type_; } |
| 310 | int preference() const { return preference_; } |
| 311 | void set_preference(int preference) { preference_ = preference; } |
| 312 | |
| 313 | // Debugging description of this network |
| 314 | std::string ToString() const; |
| 315 | |
| 316 | private: |
| 317 | std::string name_; |
| 318 | std::string description_; |
| 319 | IPAddress prefix_; |
| 320 | int prefix_length_; |
| 321 | std::string key_; |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 322 | std::vector<InterfaceAddress> ips_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 323 | int scope_id_; |
| 324 | bool ignored_; |
| 325 | AdapterType type_; |
| 326 | int preference_; |
| 327 | |
| 328 | friend class NetworkManager; |
| 329 | }; |
| 330 | |
| 331 | } // namespace rtc |
| 332 | |
| 333 | #endif // WEBRTC_BASE_NETWORK_H_ |