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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_NETWORK_H_ |
| 12 | #define RTC_BASE_NETWORK_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
pbos | c7c26a0 | 2017-01-02 08:42:32 -0800 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <deque> |
| 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 22 | #include "rtc_base/ip_address.h" |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 23 | #include "rtc_base/mdns_responder_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/message_handler.h" |
| 25 | #include "rtc_base/network_monitor.h" |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 26 | #include "rtc_base/network_monitor_factory.h" |
| 27 | #include "rtc_base/synchronization/sequence_checker.h" |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 28 | #include "rtc_base/system/rtc_export.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 29 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 30 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 31 | |
| 32 | #if defined(WEBRTC_POSIX) |
| 33 | struct ifaddrs; |
| 34 | #endif // defined(WEBRTC_POSIX) |
| 35 | |
| 36 | namespace rtc { |
| 37 | |
| 38 | extern const char kPublicIPv4Host[]; |
| 39 | extern const char kPublicIPv6Host[]; |
| 40 | |
| 41 | class IfAddrsConverter; |
| 42 | class Network; |
| 43 | class NetworkMonitorInterface; |
| 44 | class Thread; |
| 45 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 46 | // By default, ignore loopback interfaces on the host. |
| 47 | const int kDefaultNetworkIgnoreMask = ADAPTER_TYPE_LOOPBACK; |
| 48 | |
| 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | std::string MakeNetworkKey(const std::string& name, |
| 53 | const IPAddress& prefix, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 54 | int prefix_length); |
| 55 | |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 56 | // Utility function that attempts to determine an adapter type by an interface |
| 57 | // name (e.g., "wlan0"). Can be used by NetworkManager subclasses when other |
| 58 | // mechanisms fail to determine the type. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 59 | RTC_EXPORT AdapterType GetAdapterTypeFromName(const char* network_name); |
Taylor Brandstetter | 8bac1d9 | 2018-01-24 17:38:00 -0800 | [diff] [blame] | 60 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | class DefaultLocalAddressProvider { |
| 62 | public: |
| 63 | virtual ~DefaultLocalAddressProvider() = default; |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 64 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 65 | // The default local address is the local address used in multi-homed endpoint |
| 66 | // when the any address (0.0.0.0 or ::) is used as the local address. It's |
| 67 | // important to check the return value as a IP family may not be enabled. |
| 68 | virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0; |
| 69 | }; |
| 70 | |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 71 | class MdnsResponderProvider { |
| 72 | public: |
| 73 | virtual ~MdnsResponderProvider() = default; |
| 74 | |
| 75 | // Returns the mDNS responder that can be used to obfuscate the local IP |
| 76 | // addresses of ICE host candidates by mDNS hostnames. |
| 77 | // |
| 78 | // The provider MUST outlive the mDNS responder. |
| 79 | virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0; |
| 80 | }; |
| 81 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 82 | // Generic network manager interface. It provides list of local |
| 83 | // networks. |
| 84 | // |
| 85 | // Every method of NetworkManager (including the destructor) must be called on |
| 86 | // the same thread, except for the constructor which may be called on any |
| 87 | // thread. |
| 88 | // |
| 89 | // This allows constructing a NetworkManager subclass on one thread and |
| 90 | // passing it into an object that uses it on a different thread. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 91 | class RTC_EXPORT NetworkManager : public DefaultLocalAddressProvider, |
| 92 | public MdnsResponderProvider { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | public: |
| 94 | typedef std::vector<Network*> NetworkList; |
| 95 | |
| 96 | // This enum indicates whether adapter enumeration is allowed. |
| 97 | enum EnumerationPermission { |
| 98 | ENUMERATION_ALLOWED, // Adapter enumeration is allowed. Getting 0 network |
| 99 | // from GetNetworks means that there is no network |
| 100 | // available. |
| 101 | ENUMERATION_BLOCKED, // Adapter enumeration is disabled. |
| 102 | // GetAnyAddressNetworks() should be used instead. |
| 103 | }; |
| 104 | |
| 105 | NetworkManager(); |
| 106 | ~NetworkManager() override; |
| 107 | |
| 108 | // Called when network list is updated. |
| 109 | sigslot::signal0<> SignalNetworksChanged; |
| 110 | |
| 111 | // Indicates a failure when getting list of network interfaces. |
| 112 | sigslot::signal0<> SignalError; |
| 113 | |
| 114 | // This should be called on the NetworkManager's thread before the |
| 115 | // NetworkManager is used. Subclasses may override this if necessary. |
| 116 | virtual void Initialize() {} |
| 117 | |
| 118 | // Start/Stop monitoring of network interfaces |
| 119 | // list. SignalNetworksChanged or SignalError is emitted immediately |
| 120 | // after StartUpdating() is called. After that SignalNetworksChanged |
| 121 | // is emitted whenever list of networks changes. |
| 122 | virtual void StartUpdating() = 0; |
| 123 | virtual void StopUpdating() = 0; |
| 124 | |
| 125 | // Returns the current list of networks available on this machine. |
| 126 | // StartUpdating() must be called before this method is called. |
| 127 | // It makes sure that repeated calls return the same object for a |
| 128 | // given network, so that quality is tracked appropriately. Does not |
| 129 | // include ignored networks. |
| 130 | virtual void GetNetworks(NetworkList* networks) const = 0; |
| 131 | |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 132 | // Returns the current permission state of GetNetworks(). |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 133 | virtual EnumerationPermission enumeration_permission() const; |
| 134 | |
| 135 | // "AnyAddressNetwork" is a network which only contains single "any address" |
| 136 | // IP address. (i.e. INADDR_ANY for IPv4 or in6addr_any for IPv6). This is |
| 137 | // useful as binding to such interfaces allow default routing behavior like |
| 138 | // http traffic. |
| 139 | // |
| 140 | // This method appends the "any address" networks to the list, such that this |
| 141 | // can optionally be called after GetNetworks. |
| 142 | // |
| 143 | // TODO(guoweis): remove this body when chromium implements this. |
| 144 | virtual void GetAnyAddressNetworks(NetworkList* networks) {} |
| 145 | |
| 146 | // Dumps the current list of networks in the network manager. |
| 147 | virtual void DumpNetworks() {} |
| 148 | bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override; |
| 149 | |
| 150 | struct Stats { |
| 151 | int ipv4_network_count; |
| 152 | int ipv6_network_count; |
| 153 | Stats() { |
| 154 | ipv4_network_count = 0; |
| 155 | ipv6_network_count = 0; |
| 156 | } |
| 157 | }; |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 158 | |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 159 | // MdnsResponderProvider interface. |
| 160 | webrtc::MdnsResponderInterface* GetMdnsResponder() const override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | // Base class for NetworkManager implementations. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 164 | class RTC_EXPORT NetworkManagerBase : public NetworkManager { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 165 | public: |
| 166 | NetworkManagerBase(); |
| 167 | ~NetworkManagerBase() override; |
| 168 | |
| 169 | void GetNetworks(NetworkList* networks) const override; |
| 170 | void GetAnyAddressNetworks(NetworkList* networks) override; |
deadbeef | 3427f53 | 2017-07-26 16:09:33 -0700 | [diff] [blame] | 171 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 172 | EnumerationPermission enumeration_permission() const override; |
| 173 | |
| 174 | bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const override; |
| 175 | |
| 176 | protected: |
| 177 | typedef std::map<std::string, Network*> NetworkMap; |
| 178 | // Updates |networks_| with the networks listed in |list|. If |
| 179 | // |network_map_| already has a Network object for a network listed |
| 180 | // in the |list| then it is reused. Accept ownership of the Network |
| 181 | // objects in the |list|. |changed| will be set to true if there is |
| 182 | // any change in the network list. |
| 183 | void MergeNetworkList(const NetworkList& list, bool* changed); |
| 184 | |
| 185 | // |stats| will be populated even if |*changed| is false. |
| 186 | void MergeNetworkList(const NetworkList& list, |
| 187 | bool* changed, |
| 188 | NetworkManager::Stats* stats); |
| 189 | |
| 190 | void set_enumeration_permission(EnumerationPermission state) { |
| 191 | enumeration_permission_ = state; |
| 192 | } |
| 193 | |
| 194 | void set_default_local_addresses(const IPAddress& ipv4, |
| 195 | const IPAddress& ipv6); |
| 196 | |
| 197 | private: |
| 198 | friend class NetworkTest; |
| 199 | |
| 200 | Network* GetNetworkFromAddress(const rtc::IPAddress& ip) const; |
| 201 | |
| 202 | EnumerationPermission enumeration_permission_; |
| 203 | |
| 204 | NetworkList networks_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 205 | |
| 206 | NetworkMap networks_map_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 207 | |
| 208 | std::unique_ptr<rtc::Network> ipv4_any_address_network_; |
| 209 | std::unique_ptr<rtc::Network> ipv6_any_address_network_; |
| 210 | |
| 211 | IPAddress default_local_ipv4_address_; |
| 212 | IPAddress default_local_ipv6_address_; |
| 213 | // We use 16 bits to save the bandwidth consumption when sending the network |
| 214 | // id over the Internet. It is OK that the 16-bit integer overflows to get a |
| 215 | // network id 0 because we only compare the network ids in the old and the new |
| 216 | // best connections in the transport channel. |
| 217 | uint16_t next_available_network_id_ = 1; |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 218 | |
| 219 | // True if calling network_preference() with a changed value |
| 220 | // should result in firing the SignalNetworkChanged signal. |
| 221 | bool signal_network_preference_change_ = false; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | // Basic implementation of the NetworkManager interface that gets list |
| 225 | // of networks using OS APIs. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 226 | class RTC_EXPORT BasicNetworkManager : public NetworkManagerBase, |
Tomas Gunnarsson | abdb470 | 2020-09-05 18:43:36 +0200 | [diff] [blame] | 227 | public MessageHandlerAutoCleanup, |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 228 | public sigslot::has_slots<> { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 229 | public: |
| 230 | BasicNetworkManager(); |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 231 | explicit BasicNetworkManager(NetworkMonitorFactory* network_monitor_factory); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 232 | ~BasicNetworkManager() override; |
| 233 | |
| 234 | void StartUpdating() override; |
| 235 | void StopUpdating() override; |
| 236 | |
| 237 | void DumpNetworks() override; |
| 238 | |
| 239 | // MessageHandler interface. |
| 240 | void OnMessage(Message* msg) override; |
| 241 | bool started() { return start_count_ > 0; } |
| 242 | |
| 243 | // Sets the network ignore list, which is empty by default. Any network on the |
| 244 | // ignore list will be filtered from network enumeration results. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 245 | // Should be called only before initialization. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 246 | void set_network_ignore_list(const std::vector<std::string>& list) { |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 247 | RTC_DCHECK(thread_ == nullptr); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 248 | network_ignore_list_ = list; |
| 249 | } |
| 250 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 251 | protected: |
| 252 | #if defined(WEBRTC_POSIX) |
| 253 | // Separated from CreateNetworks for tests. |
| 254 | void ConvertIfAddrs(ifaddrs* interfaces, |
| 255 | IfAddrsConverter* converter, |
| 256 | bool include_ignored, |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 257 | NetworkList* networks) const RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 258 | #endif // defined(WEBRTC_POSIX) |
| 259 | |
| 260 | // Creates a network object for each network available on the machine. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 261 | bool CreateNetworks(bool include_ignored, NetworkList* networks) const |
| 262 | RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 263 | |
| 264 | // Determines if a network should be ignored. This should only be determined |
| 265 | // based on the network's property instead of any individual IP. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 266 | bool IsIgnoredNetwork(const Network& network) const RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 267 | |
| 268 | // This function connects a UDP socket to a public address and returns the |
| 269 | // local address associated it. Since it binds to the "any" address |
| 270 | // internally, it returns the default local address on a multi-homed endpoint. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 271 | IPAddress QueryDefaultLocalAddress(int family) const RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 272 | |
| 273 | private: |
| 274 | friend class NetworkTest; |
| 275 | |
| 276 | // Creates a network monitor and listens for network updates. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 277 | void StartNetworkMonitor() RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 278 | // Stops and removes the network monitor. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 279 | void StopNetworkMonitor() RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 280 | // Called when it receives updates from the network monitor. |
| 281 | void OnNetworksChanged(); |
| 282 | |
| 283 | // Updates the networks and reschedules the next update. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 284 | void UpdateNetworksContinually() RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 285 | // Only updates the networks; does not reschedule the next update. |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 286 | void UpdateNetworksOnce() RTC_RUN_ON(thread_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 287 | |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 288 | Thread* thread_ = nullptr; |
| 289 | bool sent_first_update_ = true; |
| 290 | int start_count_ = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 291 | std::vector<std::string> network_ignore_list_; |
Taylor Brandstetter | 239ac8a | 2020-07-31 16:07:52 -0700 | [diff] [blame] | 292 | NetworkMonitorFactory* network_monitor_factory_ RTC_GUARDED_BY(thread_) = |
| 293 | nullptr; |
| 294 | std::unique_ptr<NetworkMonitorInterface> network_monitor_ |
| 295 | RTC_GUARDED_BY(thread_); |
Jonas Oreland | 47fa08f | 2020-12-05 18:09:13 +0100 | [diff] [blame] | 296 | bool allow_mac_based_ipv6_ = false; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | // Represents a Unix-type network interface, with a name and single address. |
Mirko Bonadei | 35214fc | 2019-09-23 14:54:28 +0200 | [diff] [blame] | 300 | class RTC_EXPORT Network { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 301 | public: |
| 302 | Network(const std::string& name, |
| 303 | const std::string& description, |
| 304 | const IPAddress& prefix, |
| 305 | int prefix_length); |
| 306 | |
| 307 | Network(const std::string& name, |
| 308 | const std::string& description, |
| 309 | const IPAddress& prefix, |
| 310 | int prefix_length, |
| 311 | AdapterType type); |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 312 | Network(const Network&); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 313 | ~Network(); |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 314 | |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 315 | // This signal is fired whenever type() or underlying_type_for_vpn() changes. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 316 | sigslot::signal1<const Network*> SignalTypeChanged; |
| 317 | |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 318 | // This signal is fired whenever network preference changes. |
| 319 | sigslot::signal1<const Network*> SignalNetworkPreferenceChanged; |
| 320 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 321 | const DefaultLocalAddressProvider* default_local_address_provider() { |
| 322 | return default_local_address_provider_; |
| 323 | } |
| 324 | void set_default_local_address_provider( |
| 325 | const DefaultLocalAddressProvider* provider) { |
| 326 | default_local_address_provider_ = provider; |
| 327 | } |
| 328 | |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 329 | void set_mdns_responder_provider(const MdnsResponderProvider* provider) { |
| 330 | mdns_responder_provider_ = provider; |
| 331 | } |
| 332 | |
| 333 | // Returns the name of the interface this network is associated with. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 334 | const std::string& name() const { return name_; } |
| 335 | |
| 336 | // Returns the OS-assigned name for this network. This is useful for |
| 337 | // debugging but should not be sent over the wire (for privacy reasons). |
| 338 | const std::string& description() const { return description_; } |
| 339 | |
| 340 | // Returns the prefix for this network. |
| 341 | const IPAddress& prefix() const { return prefix_; } |
| 342 | // Returns the length, in bits, of this network's prefix. |
| 343 | int prefix_length() const { return prefix_length_; } |
| 344 | |
| 345 | // |key_| has unique value per network interface. Used in sorting network |
| 346 | // interfaces. Key is derived from interface name and it's prefix. |
| 347 | std::string key() const { return key_; } |
| 348 | |
| 349 | // Returns the Network's current idea of the 'best' IP it has. |
| 350 | // Or return an unset IP if this network has no active addresses. |
| 351 | // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC. |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 352 | // 1) return all global temporary dynamic and non-deprecated ones. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 353 | // 2) if #1 not available, return global ones. |
| 354 | // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands |
| 355 | // for unique local address, which is not route-able in open |
| 356 | // internet but might be useful for a close WebRTC deployment. |
| 357 | |
| 358 | // TODO(guoweis): rule #3 actually won't happen at current |
| 359 | // implementation. The reason being that ULA address starting with |
| 360 | // 0xfc 0r 0xfd will be grouped into its own Network. The result of |
| 361 | // that is WebRTC will have one extra Network to generate candidates |
| 362 | // but the lack of rule #3 shouldn't prevent turning on IPv6 since |
| 363 | // ULA should only be tried in a close deployment anyway. |
| 364 | |
| 365 | // Note that when not specifying any flag, it's treated as case global |
| 366 | // IPv6 address |
| 367 | IPAddress GetBestIP() const; |
| 368 | |
| 369 | // Keep the original function here for now. |
| 370 | // TODO(guoweis): Remove this when all callers are migrated to GetBestIP(). |
| 371 | IPAddress ip() const { return GetBestIP(); } |
| 372 | |
| 373 | // Adds an active IP address to this network. Does not check for duplicates. |
| 374 | void AddIP(const InterfaceAddress& ip) { ips_.push_back(ip); } |
Taylor Brandstetter | 01cb5f2 | 2018-03-07 15:49:32 -0800 | [diff] [blame] | 375 | void AddIP(const IPAddress& ip) { ips_.push_back(rtc::InterfaceAddress(ip)); } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 376 | |
| 377 | // Sets the network's IP address list. Returns true if new IP addresses were |
| 378 | // detected. Passing true to already_changed skips this check. |
| 379 | bool SetIPs(const std::vector<InterfaceAddress>& ips, bool already_changed); |
| 380 | // Get the list of IP Addresses associated with this network. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 381 | const std::vector<InterfaceAddress>& GetIPs() const { return ips_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 382 | // Clear the network's list of addresses. |
| 383 | void ClearIPs() { ips_.clear(); } |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 384 | // Returns the mDNS responder that can be used to obfuscate the local IP |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 385 | // addresses of host candidates by mDNS names in ICE gathering. After a |
| 386 | // name-address mapping is created by the mDNS responder, queries for the |
| 387 | // created name will be resolved by the responder. |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 388 | webrtc::MdnsResponderInterface* GetMdnsResponder() const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 389 | |
| 390 | // Returns the scope-id of the network's address. |
| 391 | // Should only be relevant for link-local IPv6 addresses. |
| 392 | int scope_id() const { return scope_id_; } |
| 393 | void set_scope_id(int id) { scope_id_ = id; } |
| 394 | |
| 395 | // Indicates whether this network should be ignored, perhaps because |
| 396 | // the IP is 0, or the interface is one we know is invalid. |
| 397 | bool ignored() const { return ignored_; } |
| 398 | void set_ignored(bool ignored) { ignored_ = ignored; } |
| 399 | |
| 400 | AdapterType type() const { return type_; } |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 401 | // When type() is ADAPTER_TYPE_VPN, this returns the type of the underlying |
| 402 | // network interface used by the VPN, typically the preferred network type |
| 403 | // (see for example, the method setUnderlyingNetworks(android.net.Network[]) |
| 404 | // on https://developer.android.com/reference/android/net/VpnService.html). |
| 405 | // When this information is unavailable from the OS, ADAPTER_TYPE_UNKNOWN is |
| 406 | // returned. |
| 407 | AdapterType underlying_type_for_vpn() const { |
| 408 | return underlying_type_for_vpn_; |
| 409 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 410 | void set_type(AdapterType type) { |
| 411 | if (type_ == type) { |
| 412 | return; |
| 413 | } |
| 414 | type_ = type; |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 415 | if (type != ADAPTER_TYPE_VPN) { |
| 416 | underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN; |
| 417 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 418 | SignalTypeChanged(this); |
| 419 | } |
| 420 | |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 421 | void set_underlying_type_for_vpn(AdapterType type) { |
| 422 | if (underlying_type_for_vpn_ == type) { |
| 423 | return; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 424 | } |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 425 | underlying_type_for_vpn_ = type; |
| 426 | SignalTypeChanged(this); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 427 | } |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 428 | |
| 429 | bool IsVpn() const { return type_ == ADAPTER_TYPE_VPN; } |
| 430 | |
Jonas Oreland | c7ea04a | 2020-04-03 10:12:28 +0200 | [diff] [blame] | 431 | bool IsCellular() const { return IsCellular(type_); } |
| 432 | |
| 433 | static bool IsCellular(AdapterType type) { |
| 434 | switch (type) { |
Jonas Oreland | 08d1806 | 2020-04-02 07:19:12 +0200 | [diff] [blame] | 435 | case ADAPTER_TYPE_CELLULAR: |
| 436 | case ADAPTER_TYPE_CELLULAR_2G: |
| 437 | case ADAPTER_TYPE_CELLULAR_3G: |
| 438 | case ADAPTER_TYPE_CELLULAR_4G: |
| 439 | case ADAPTER_TYPE_CELLULAR_5G: |
| 440 | return true; |
| 441 | default: |
| 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 446 | uint16_t GetCost() const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 447 | // A unique id assigned by the network manager, which may be signaled |
| 448 | // to the remote side in the candidate. |
| 449 | uint16_t id() const { return id_; } |
| 450 | void set_id(uint16_t id) { id_ = id; } |
| 451 | |
| 452 | int preference() const { return preference_; } |
| 453 | void set_preference(int preference) { preference_ = preference; } |
| 454 | |
| 455 | // When we enumerate networks and find a previously-seen network is missing, |
| 456 | // we do not remove it (because it may be used elsewhere). Instead, we mark |
| 457 | // it inactive, so that we can detect network changes properly. |
| 458 | bool active() const { return active_; } |
| 459 | void set_active(bool active) { |
| 460 | if (active_ != active) { |
| 461 | active_ = active; |
| 462 | } |
| 463 | } |
| 464 | |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 465 | // Property set by operating system/firmware that has information |
| 466 | // about connection strength to e.g WIFI router or CELL base towers. |
| 467 | NetworkPreference network_preference() const { return network_preference_; } |
| 468 | void set_network_preference(NetworkPreference val) { |
| 469 | if (network_preference_ == val) { |
| 470 | return; |
| 471 | } |
| 472 | network_preference_ = val; |
| 473 | SignalNetworkPreferenceChanged(this); |
| 474 | } |
| 475 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 476 | // Debugging description of this network |
| 477 | std::string ToString() const; |
| 478 | |
| 479 | private: |
| 480 | const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr; |
Qingsi Wang | 5ae259e | 2019-02-13 15:46:07 -0800 | [diff] [blame] | 481 | const MdnsResponderProvider* mdns_responder_provider_ = nullptr; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 482 | std::string name_; |
| 483 | std::string description_; |
| 484 | IPAddress prefix_; |
| 485 | int prefix_length_; |
| 486 | std::string key_; |
| 487 | std::vector<InterfaceAddress> ips_; |
| 488 | int scope_id_; |
| 489 | bool ignored_; |
| 490 | AdapterType type_; |
Qingsi Wang | de2ed7d | 2018-04-27 14:25:37 -0700 | [diff] [blame] | 491 | AdapterType underlying_type_for_vpn_ = ADAPTER_TYPE_UNKNOWN; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 492 | int preference_; |
| 493 | bool active_ = true; |
| 494 | uint16_t id_ = 0; |
Jonas Oreland | 2105d64 | 2020-05-13 10:15:34 +0200 | [diff] [blame] | 495 | bool use_differentiated_cellular_costs_ = false; |
Jonas Oreland | f7721fb | 2020-08-07 11:08:34 +0200 | [diff] [blame] | 496 | NetworkPreference network_preference_ = NetworkPreference::NEUTRAL; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 497 | |
| 498 | friend class NetworkManager; |
| 499 | }; |
| 500 | |
| 501 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 502 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 503 | #endif // RTC_BASE_NETWORK_H_ |