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