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