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