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 | |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame^] | 14 | #include <stdint.h> |
| 15 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include <deque> |
| 17 | #include <map> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 18 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | #include "webrtc/base/ipaddress.h" |
honghaiz | a7ad7c3 | 2016-02-02 12:54:14 -0800 | [diff] [blame] | 23 | #include "webrtc/base/networkmonitor.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | #include "webrtc/base/messagehandler.h" |
| 25 | #include "webrtc/base/sigslot.h" |
| 26 | |
| 27 | #if defined(WEBRTC_POSIX) |
| 28 | struct ifaddrs; |
| 29 | #endif // defined(WEBRTC_POSIX) |
| 30 | |
| 31 | namespace rtc { |
| 32 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 33 | extern const char kPublicIPv4Host[]; |
| 34 | extern const char kPublicIPv6Host[]; |
| 35 | |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 36 | class IfAddrsConverter; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | class Network; |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 38 | class NetworkMonitorInterface; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | class Thread; |
| 40 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 41 | static const uint16_t kNetworkCostMax = 999; |
| 42 | static const uint16_t kNetworkCostHigh = 900; |
| 43 | static const uint16_t kNetworkCostUnknown = 50; |
| 44 | static const uint16_t kNetworkCostLow = 10; |
| 45 | static const uint16_t kNetworkCostMin = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 47 | // By default, ignore loopback interfaces on the host. |
| 48 | const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK; |
| 49 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | // Makes a string key for this network. Used in the network manager's maps. |
| 51 | // Network objects are keyed on interface name, network prefix and the |
| 52 | // length of that prefix. |
| 53 | std::string MakeNetworkKey(const std::string& name, const IPAddress& prefix, |
| 54 | int prefix_length); |
| 55 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 56 | class DefaultLocalAddressProvider { |
| 57 | public: |
| 58 | virtual ~DefaultLocalAddressProvider() = default; |
| 59 | // The default local address is the local address used in multi-homed endpoint |
Guo-wei Shieh | e03cab9 | 2015-11-11 11:11:19 -0800 | [diff] [blame] | 60 | // when the any address (0.0.0.0 or ::) is used as the local address. It's |
| 61 | // important to check the return value as a IP family may not be enabled. |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 62 | virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0; |
| 63 | }; |
| 64 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | // Generic network manager interface. It provides list of local |
| 66 | // networks. |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 67 | // |
| 68 | // Every method of NetworkManager (including the destructor) must be called on |
| 69 | // the same thread, except for the constructor which may be called on any |
| 70 | // thread. |
| 71 | // |
| 72 | // This allows constructing a NetworkManager subclass on one thread and |
| 73 | // passing it into an object that uses it on a different thread. |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 74 | class NetworkManager : public DefaultLocalAddressProvider { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 75 | public: |
| 76 | typedef std::vector<Network*> NetworkList; |
| 77 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 78 | // This enum indicates whether adapter enumeration is allowed. |
| 79 | enum EnumerationPermission { |
guoweis | ea1012b | 2015-08-21 09:06:28 -0700 | [diff] [blame] | 80 | ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network |
| 81 | // from GetNetworks means that there is no network |
| 82 | // available. |
| 83 | ENUMERATION_BLOCKED, // Adapter enumeration is disabled. |
| 84 | // GetAnyAddressNetworks() should be used instead. |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 87 | NetworkManager(); |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 88 | ~NetworkManager() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | |
| 90 | // Called when network list is updated. |
| 91 | sigslot::signal0<> SignalNetworksChanged; |
| 92 | |
| 93 | // Indicates a failure when getting list of network interfaces. |
| 94 | sigslot::signal0<> SignalError; |
| 95 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 96 | // This should be called on the NetworkManager's thread before the |
| 97 | // NetworkManager is used. Subclasses may override this if necessary. |
| 98 | virtual void Initialize() {} |
| 99 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | // Start/Stop monitoring of network interfaces |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 101 | // list. SignalNetworksChanged or SignalError is emitted immediately |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | // after StartUpdating() is called. After that SignalNetworksChanged |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 103 | // is emitted whenever list of networks changes. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | virtual void StartUpdating() = 0; |
| 105 | virtual void StopUpdating() = 0; |
| 106 | |
| 107 | // Returns the current list of networks available on this machine. |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 108 | // StartUpdating() must be called before this method is called. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | // It makes sure that repeated calls return the same object for a |
| 110 | // given network, so that quality is tracked appropriately. Does not |
| 111 | // include ignored networks. |
| 112 | virtual void GetNetworks(NetworkList* networks) const = 0; |
| 113 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 114 | // return the current permission state of GetNetworks() |
Guo-wei Shieh | 86cb923 | 2015-08-19 10:57:53 -0700 | [diff] [blame] | 115 | virtual EnumerationPermission enumeration_permission() const; |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 116 | |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 117 | // "AnyAddressNetwork" is a network which only contains single "any address" |
| 118 | // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is |
| 119 | // useful as binding to such interfaces allow default routing behavior like |
| 120 | // http traffic. |
guoweis@webrtc.org | 9dfe7aa | 2015-02-18 20:27:17 +0000 | [diff] [blame] | 121 | // TODO(guoweis): remove this body when chromium implements this. |
| 122 | virtual void GetAnyAddressNetworks(NetworkList* networks) {} |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 123 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 124 | // Dumps the current list of networks in the network manager. |
| 125 | virtual void DumpNetworks() {} |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 126 | bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override; |
| 127 | |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 128 | struct Stats { |
| 129 | int ipv4_network_count; |
| 130 | int ipv6_network_count; |
| 131 | Stats() { |
| 132 | ipv4_network_count = 0; |
| 133 | ipv6_network_count = 0; |
| 134 | } |
| 135 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | // Base class for NetworkManager implementations. |
| 139 | class NetworkManagerBase : public NetworkManager { |
| 140 | public: |
| 141 | NetworkManagerBase(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 142 | ~NetworkManagerBase() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 144 | void GetNetworks(NetworkList* networks) const override; |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 145 | void GetAnyAddressNetworks(NetworkList* networks) override; |
deadbeef | e97389c | 2016-12-23 01:43:45 -0800 | [diff] [blame] | 146 | // Defaults to true. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 147 | bool ipv6_enabled() const { return ipv6_enabled_; } |
| 148 | void set_ipv6_enabled(bool enabled) { ipv6_enabled_ = enabled; } |
| 149 | |
guoweis@webrtc.org | 2444d96 | 2015-01-30 00:09:28 +0000 | [diff] [blame] | 150 | void set_max_ipv6_networks(int networks) { max_ipv6_networks_ = networks; } |
| 151 | int max_ipv6_networks() { return max_ipv6_networks_; } |
| 152 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 153 | EnumerationPermission enumeration_permission() const override; |
| 154 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 155 | bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override; |
| 156 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 157 | protected: |
| 158 | typedef std::map<std::string, Network*> NetworkMap; |
| 159 | // Updates |networks_| with the networks listed in |list|. If |
| 160 | // |network_map_| already has a Network object for a network listed |
| 161 | // in the |list| then it is reused. Accept ownership of the Network |
| 162 | // objects in the |list|. |changed| will be set to true if there is |
| 163 | // any change in the network list. |
| 164 | void MergeNetworkList(const NetworkList& list, bool* changed); |
| 165 | |
guoweis@webrtc.org | a094cac | 2015-01-28 19:34:05 +0000 | [diff] [blame] | 166 | // |stats| will be populated even if |*changed| is false. |
| 167 | void MergeNetworkList(const NetworkList& list, |
| 168 | bool* changed, |
| 169 | NetworkManager::Stats* stats); |
| 170 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 171 | void set_enumeration_permission(EnumerationPermission state) { |
| 172 | enumeration_permission_ = state; |
| 173 | } |
| 174 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 175 | void set_default_local_addresses(const IPAddress& ipv4, |
| 176 | const IPAddress& ipv6); |
| 177 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | private: |
| 179 | friend class NetworkTest; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 180 | |
honghaiz | af83fe6 | 2016-04-18 14:50:44 -0700 | [diff] [blame] | 181 | Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const; |
| 182 | |
Guo-wei Shieh | 47872ec | 2015-08-19 10:32:46 -0700 | [diff] [blame] | 183 | EnumerationPermission enumeration_permission_; |
| 184 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | NetworkList networks_; |
guoweis@webrtc.org | 2444d96 | 2015-01-30 00:09:28 +0000 | [diff] [blame] | 186 | int max_ipv6_networks_; |
| 187 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 188 | NetworkMap networks_map_; |
| 189 | bool ipv6_enabled_; |
guoweis@webrtc.org | f358aea | 2015-02-18 18:44:01 +0000 | [diff] [blame] | 190 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 191 | std::unique_ptr<rtc::Network> ipv4_any_address_network_; |
| 192 | std::unique_ptr<rtc::Network> ipv6_any_address_network_; |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 193 | |
| 194 | IPAddress default_local_ipv4_address_; |
| 195 | IPAddress default_local_ipv6_address_; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 196 | // We use 16 bits to save the bandwidth consumption when sending the network |
| 197 | // id over the Internet. It is OK that the 16-bit integer overflows to get a |
| 198 | // network id 0 because we only compare the network ids in the old and the new |
| 199 | // best connections in the transport channel. |
| 200 | uint16_t next_available_network_id_ = 1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | // Basic implementation of the NetworkManager interface that gets list |
| 204 | // of networks using OS APIs. |
| 205 | class BasicNetworkManager : public NetworkManagerBase, |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 206 | public MessageHandler, |
| 207 | public sigslot::has_slots<> { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | public: |
| 209 | BasicNetworkManager(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 210 | ~BasicNetworkManager() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 211 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 212 | void StartUpdating() override; |
| 213 | void StopUpdating() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 214 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 215 | void DumpNetworks() override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | |
| 217 | // MessageHandler interface. |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 218 | void OnMessage(Message* msg) override; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 219 | bool started() { return start_count_ > 0; } |
| 220 | |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 221 | // Sets the network ignore list, which is empty by default. Any network on the |
| 222 | // ignore list will be filtered from network enumeration results. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 223 | void set_network_ignore_list(const std::vector<std::string>& list) { |
| 224 | network_ignore_list_ = list; |
| 225 | } |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 226 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 227 | #if defined(WEBRTC_LINUX) |
| 228 | // Sets the flag for ignoring non-default routes. |
| 229 | void set_ignore_non_default_routes(bool value) { |
| 230 | ignore_non_default_routes_ = true; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | protected: |
| 235 | #if defined(WEBRTC_POSIX) |
| 236 | // Separated from CreateNetworks for tests. |
| 237 | void ConvertIfAddrs(ifaddrs* interfaces, |
Guo-wei Shieh | 9faf154 | 2015-12-28 14:06:55 -0800 | [diff] [blame] | 238 | IfAddrsConverter* converter, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 239 | bool include_ignored, |
| 240 | NetworkList* networks) const; |
| 241 | #endif // defined(WEBRTC_POSIX) |
| 242 | |
| 243 | // Creates a network object for each network available on the machine. |
| 244 | bool CreateNetworks(bool include_ignored, NetworkList* networks) const; |
| 245 | |
guoweis@webrtc.org | b91d0f5 | 2015-03-17 14:43:20 +0000 | [diff] [blame] | 246 | // Determines if a network should be ignored. This should only be determined |
| 247 | // based on the network's property instead of any individual IP. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | bool IsIgnoredNetwork(const Network& network) const; |
| 249 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 250 | // This function connects a UDP socket to a public address and returns the |
| 251 | // local address associated it. Since it binds to the "any" address |
| 252 | // internally, it returns the default local address on a multi-homed endpoint. |
| 253 | IPAddress QueryDefaultLocalAddress(int family) const; |
| 254 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | private: |
| 256 | friend class NetworkTest; |
| 257 | |
honghaiz | 023f3ef | 2015-10-19 09:39:32 -0700 | [diff] [blame] | 258 | // Creates a network monitor and listens for network updates. |
| 259 | void StartNetworkMonitor(); |
| 260 | // Stops and removes the network monitor. |
| 261 | void StopNetworkMonitor(); |
| 262 | // Called when it receives updates from the network monitor. |
| 263 | void OnNetworksChanged(); |
| 264 | |
| 265 | // Updates the networks and reschedules the next update. |
| 266 | void UpdateNetworksContinually(); |
| 267 | // Only updates the networks; does not reschedule the next update. |
| 268 | void UpdateNetworksOnce(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 269 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 270 | AdapterType GetAdapterTypeFromName(const char* network_name) const; |
| 271 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 272 | Thread* thread_; |
| 273 | bool sent_first_update_; |
| 274 | int start_count_; |
| 275 | std::vector<std::string> network_ignore_list_; |
| 276 | bool ignore_non_default_routes_; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 277 | std::unique_ptr<NetworkMonitorInterface> network_monitor_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | // Represents a Unix-type network interface, with a name and single address. |
| 281 | class Network { |
| 282 | public: |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 283 | Network(const std::string& name, |
| 284 | const std::string& description, |
| 285 | const IPAddress& prefix, |
| 286 | int prefix_length); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 287 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 288 | Network(const std::string& name, |
| 289 | const std::string& description, |
| 290 | const IPAddress& prefix, |
| 291 | int prefix_length, |
| 292 | AdapterType type); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 293 | ~Network(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 294 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 295 | sigslot::signal1<const Network*> SignalTypeChanged; |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 296 | |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 297 | const DefaultLocalAddressProvider* default_local_address_provider() { |
| 298 | return default_local_address_provider_; |
| 299 | } |
| 300 | void set_default_local_address_provider( |
| 301 | const DefaultLocalAddressProvider* provider) { |
| 302 | default_local_address_provider_ = provider; |
| 303 | } |
| 304 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 305 | // Returns the name of the interface this network is associated wtih. |
| 306 | const std::string& name() const { return name_; } |
| 307 | |
| 308 | // Returns the OS-assigned name for this network. This is useful for |
| 309 | // debugging but should not be sent over the wire (for privacy reasons). |
| 310 | const std::string& description() const { return description_; } |
| 311 | |
| 312 | // Returns the prefix for this network. |
| 313 | const IPAddress& prefix() const { return prefix_; } |
| 314 | // Returns the length, in bits, of this network's prefix. |
| 315 | int prefix_length() const { return prefix_length_; } |
| 316 | |
| 317 | // |key_| has unique value per network interface. Used in sorting network |
| 318 | // interfaces. Key is derived from interface name and it's prefix. |
| 319 | std::string key() const { return key_; } |
| 320 | |
aluebs@webrtc.org | 07dcf60 | 2015-02-27 18:42:22 +0000 | [diff] [blame] | 321 | // Returns the Network's current idea of the 'best' IP it has. |
| 322 | // Or return an unset IP if this network has no active addresses. |
| 323 | // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC. |
| 324 | // 1) return all global temporary dynamic and non-deprecrated ones. |
| 325 | // 2) if #1 not available, return global ones. |
| 326 | // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands |
| 327 | // for unique local address, which is not route-able in open |
| 328 | // internet but might be useful for a close WebRTC deployment. |
| 329 | |
| 330 | // TODO(guoweis): rule #3 actually won't happen at current |
| 331 | // implementation. The reason being that ULA address starting with |
| 332 | // 0xfc 0r 0xfd will be grouped into its own Network. The result of |
| 333 | // that is WebRTC will have one extra Network to generate candidates |
| 334 | // but the lack of rule #3 shouldn't prevent turning on IPv6 since |
| 335 | // ULA should only be tried in a close deployment anyway. |
| 336 | |
| 337 | // Note that when not specifying any flag, it's treated as case global |
| 338 | // IPv6 address |
guoweis@webrtc.org | 369a637 | 2014-09-17 22:37:29 +0000 | [diff] [blame] | 339 | IPAddress GetBestIP() const; |
| 340 | |
| 341 | // Keep the original function here for now. |
| 342 | // TODO(guoweis): Remove this when all callers are migrated to GetBestIP(). |
| 343 | IPAddress ip() const { return GetBestIP(); } |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 344 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 345 | // 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] | 346 | void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 347 | |
| 348 | // Sets the network's IP address list. Returns true if new IP addresses were |
| 349 | // detected. Passing true to already_changed skips this check. |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 350 | bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 351 | // Get the list of IP Addresses associated with this network. |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 352 | const std::vector<InterfaceAddress>& GetIPs() const { return ips_;} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 353 | // Clear the network's list of addresses. |
| 354 | void ClearIPs() { ips_.clear(); } |
| 355 | |
| 356 | // Returns the scope-id of the network's address. |
| 357 | // Should only be relevant for link-local IPv6 addresses. |
| 358 | int scope_id() const { return scope_id_; } |
| 359 | void set_scope_id(int id) { scope_id_ = id; } |
| 360 | |
| 361 | // Indicates whether this network should be ignored, perhaps because |
| 362 | // the IP is 0, or the interface is one we know is invalid. |
| 363 | bool ignored() const { return ignored_; } |
| 364 | void set_ignored(bool ignored) { ignored_ = ignored; } |
| 365 | |
| 366 | AdapterType type() const { return type_; } |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 367 | void set_type(AdapterType type) { |
| 368 | if (type_ == type) { |
| 369 | return; |
| 370 | } |
| 371 | type_ = type; |
| 372 | SignalTypeChanged(this); |
| 373 | } |
honghaiz | e2af9ef | 2016-03-03 08:27:47 -0800 | [diff] [blame] | 374 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 375 | uint16_t GetCost() const { |
| 376 | switch (type_) { |
| 377 | case rtc::ADAPTER_TYPE_ETHERNET: |
| 378 | case rtc::ADAPTER_TYPE_LOOPBACK: |
| 379 | return kNetworkCostMin; |
| 380 | case rtc::ADAPTER_TYPE_WIFI: |
| 381 | case rtc::ADAPTER_TYPE_VPN: |
| 382 | return kNetworkCostLow; |
| 383 | case rtc::ADAPTER_TYPE_CELLULAR: |
| 384 | return kNetworkCostHigh; |
| 385 | default: |
| 386 | return kNetworkCostUnknown; |
| 387 | } |
| 388 | } |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 389 | // A unique id assigned by the network manager, which may be signaled |
| 390 | // to the remote side in the candidate. |
| 391 | uint16_t id() const { return id_; } |
| 392 | void set_id(uint16_t id) { id_ = id; } |
| 393 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 394 | int preference() const { return preference_; } |
| 395 | void set_preference(int preference) { preference_ = preference; } |
| 396 | |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 397 | // When we enumerate networks and find a previously-seen network is missing, |
| 398 | // we do not remove it (because it may be used elsewhere). Instead, we mark |
| 399 | // it inactive, so that we can detect network changes properly. |
| 400 | bool active() const { return active_; } |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 401 | void set_active(bool active) { |
Honghai Zhang | 5622c5e | 2016-07-01 13:59:29 -0700 | [diff] [blame] | 402 | if (active_ != active) { |
| 403 | active_ = active; |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 406 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 407 | // Debugging description of this network |
| 408 | std::string ToString() const; |
| 409 | |
| 410 | private: |
Guo-wei Shieh | 9af97f8 | 2015-11-10 14:47:39 -0800 | [diff] [blame] | 411 | const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 412 | std::string name_; |
| 413 | std::string description_; |
| 414 | IPAddress prefix_; |
| 415 | int prefix_length_; |
| 416 | std::string key_; |
guoweis@webrtc.org | fa60398 | 2014-09-09 23:42:40 +0000 | [diff] [blame] | 417 | std::vector<InterfaceAddress> ips_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 418 | int scope_id_; |
| 419 | bool ignored_; |
| 420 | AdapterType type_; |
| 421 | int preference_; |
honghaiz | db8cf50 | 2015-12-21 13:08:46 -0800 | [diff] [blame] | 422 | bool active_ = true; |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 423 | uint16_t id_ = 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 424 | |
| 425 | friend class NetworkManager; |
| 426 | }; |
| 427 | |
| 428 | } // namespace rtc |
| 429 | |
| 430 | #endif // WEBRTC_BASE_NETWORK_H_ |