1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_BASE_NETWORK_INTERFACES_H_ 6 #define NET_BASE_NETWORK_INTERFACES_H_ 7 8 #include <stdint.h> 9 10 #include <array> 11 #include <memory> 12 #include <string> 13 #include <vector> 14 15 #include "net/base/ip_address.h" 16 #include "net/base/net_export.h" 17 #include "net/base/network_change_notifier.h" 18 19 #include "third_party/abseil-cpp/absl/types/optional.h" 20 21 namespace net { 22 23 // A subset of IP address attributes which are actionable by the 24 // application layer. Currently unimplemented for all hosts; 25 // IP_ADDRESS_ATTRIBUTE_NONE is always returned. 26 enum IPAddressAttributes { 27 IP_ADDRESS_ATTRIBUTE_NONE = 0, 28 29 // A temporary address is dynamic by nature and will not contain MAC 30 // address. Presence of MAC address in IPv6 addresses can be used to 31 // track an endpoint and cause privacy concern. Please refer to 32 // RFC4941. 33 IP_ADDRESS_ATTRIBUTE_TEMPORARY = 1 << 0, 34 35 // A temporary address could become deprecated once the preferred 36 // lifetime is reached. It is still valid but shouldn't be used to 37 // create new connections. 38 IP_ADDRESS_ATTRIBUTE_DEPRECATED = 1 << 1, 39 40 // Anycast address. 41 IP_ADDRESS_ATTRIBUTE_ANYCAST = 1 << 2, 42 43 // Tentative address. 44 IP_ADDRESS_ATTRIBUTE_TENTATIVE = 1 << 3, 45 46 // DAD detected duplicate. 47 IP_ADDRESS_ATTRIBUTE_DUPLICATED = 1 << 4, 48 49 // May be detached from the link. 50 IP_ADDRESS_ATTRIBUTE_DETACHED = 1 << 5, 51 }; 52 53 using Eui48MacAddress = std::array<uint8_t, 6>; 54 55 // struct that is used by GetNetworkList() to represent a network 56 // interface. 57 struct NET_EXPORT NetworkInterface { 58 NetworkInterface(); 59 NetworkInterface(const std::string& name, 60 const std::string& friendly_name, 61 uint32_t interface_index, 62 NetworkChangeNotifier::ConnectionType type, 63 const IPAddress& address, 64 uint32_t prefix_length, 65 int ip_address_attributes, 66 absl::optional<Eui48MacAddress> mac_address = absl::nullopt); 67 NetworkInterface(const NetworkInterface& other); 68 ~NetworkInterface(); 69 70 std::string name; 71 std::string friendly_name; // Same as |name| on non-Windows. 72 uint32_t interface_index; // Always 0 on Android. 73 NetworkChangeNotifier::ConnectionType type; 74 IPAddress address; 75 uint32_t prefix_length; 76 int ip_address_attributes; // Combination of |IPAddressAttributes|. 77 absl::optional<Eui48MacAddress> mac_address; 78 }; 79 80 typedef std::vector<NetworkInterface> NetworkInterfaceList; 81 82 // Policy settings to include/exclude network interfaces. 83 enum HostAddressSelectionPolicy { 84 INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES = 0x0, 85 EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES = 0x1, 86 }; 87 88 // Returns list of network interfaces except loopback interface. If an 89 // interface has more than one address, a separate entry is added to 90 // the list for each address. 91 // Can be called only on a thread that allows IO. 92 NET_EXPORT bool GetNetworkList(NetworkInterfaceList* networks, 93 int policy); 94 95 // Gets the SSID of the currently associated WiFi access point if there is one, 96 // and it is available. SSID may not be available if the app does not have 97 // permissions to access it. On Android M+, the app accessing SSID needs to have 98 // ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. If there is no WiFi access 99 // point or its SSID is unavailable, an empty string is returned. 100 // Currently only implemented on Linux, ChromeOS, Android and Windows. 101 NET_EXPORT std::string GetWifiSSID(); 102 103 // General category of the IEEE 802.11 (wifi) physical layer operating mode. 104 enum WifiPHYLayerProtocol { 105 // No wifi support or no associated AP. 106 WIFI_PHY_LAYER_PROTOCOL_NONE, 107 // An obsolete modes introduced by the original 802.11, e.g. IR, FHSS. 108 WIFI_PHY_LAYER_PROTOCOL_ANCIENT, 109 // 802.11a, OFDM-based rates. 110 WIFI_PHY_LAYER_PROTOCOL_A, 111 // 802.11b, DSSS or HR DSSS. 112 WIFI_PHY_LAYER_PROTOCOL_B, 113 // 802.11g, same rates as 802.11a but compatible with 802.11b. 114 WIFI_PHY_LAYER_PROTOCOL_G, 115 // 802.11n, HT rates. 116 WIFI_PHY_LAYER_PROTOCOL_N, 117 // Unclassified mode or failure to identify. 118 WIFI_PHY_LAYER_PROTOCOL_UNKNOWN, 119 // 802.11ac 120 WIFI_PHY_LAYER_PROTOCOL_AC, 121 // 802.11ad 122 WIFI_PHY_LAYER_PROTOCOL_AD, 123 // 802.11ax 124 WIFI_PHY_LAYER_PROTOCOL_AX 125 }; 126 127 // Characterize the PHY mode of the currently associated access point. 128 // Currently only available on Windows. 129 NET_EXPORT WifiPHYLayerProtocol GetWifiPHYLayerProtocol(); 130 131 enum WifiOptions { 132 // Disables background SSID scans. 133 WIFI_OPTIONS_DISABLE_SCAN = 1 << 0, 134 // Enables media streaming mode. 135 WIFI_OPTIONS_MEDIA_STREAMING_MODE = 1 << 1 136 }; 137 138 class NET_EXPORT ScopedWifiOptions { 139 public: 140 ScopedWifiOptions() = default; 141 ScopedWifiOptions(const ScopedWifiOptions&) = delete; 142 ScopedWifiOptions& operator=(const ScopedWifiOptions&) = delete; 143 virtual ~ScopedWifiOptions(); 144 }; 145 146 // Set temporary options on all wifi interfaces. 147 // |options| is an ORed bitfield of WifiOptions. 148 // Options are automatically disabled when the scoped pointer 149 // is freed. Currently only available on Windows. 150 NET_EXPORT std::unique_ptr<ScopedWifiOptions> SetWifiOptions(int options); 151 152 // Returns the hostname of the current system. Returns empty string on failure. 153 NET_EXPORT std::string GetHostName(); 154 155 } // namespace net 156 157 #endif // NET_BASE_NETWORK_INTERFACES_H_ 158