You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
17 KiB

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