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