xref: /aosp_15_r20/external/cronet/net/base/network_interfaces.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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   bool operator==(const NetworkInterface& that) const = default;
70   bool operator!=(const NetworkInterface& that) const = default;
71 
72   std::string name;
73   std::string friendly_name;  // Same as |name| on non-Windows.
74   uint32_t interface_index;  // Always 0 on Android.
75   NetworkChangeNotifier::ConnectionType type;
76   IPAddress address;
77   uint32_t prefix_length;
78   int ip_address_attributes;  // Combination of |IPAddressAttributes|.
79   std::optional<Eui48MacAddress> mac_address;
80 };
81 
82 typedef std::vector<NetworkInterface> NetworkInterfaceList;
83 
84 // Policy settings to include/exclude network interfaces.
85 enum HostAddressSelectionPolicy {
86   INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES           = 0x0,
87   EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES           = 0x1,
88 };
89 
90 // Returns list of network interfaces except loopback interface. If an
91 // interface has more than one address, a separate entry is added to
92 // the list for each address.
93 // Can be called only on a thread that allows IO.
94 NET_EXPORT bool GetNetworkList(NetworkInterfaceList* networks,
95                                int policy);
96 
97 // Gets the SSID of the currently associated WiFi access point if there is one,
98 // and it is available. SSID may not be available if the app does not have
99 // permissions to access it. On Android M+, the app accessing SSID needs to have
100 // ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. If there is no WiFi access
101 // point or its SSID is unavailable, an empty string is returned.
102 // Currently only implemented on Linux, ChromeOS, Android and Windows.
103 NET_EXPORT std::string GetWifiSSID();
104 
105 // General category of the IEEE 802.11 (wifi) physical layer operating mode.
106 enum WifiPHYLayerProtocol {
107   // No wifi support or no associated AP.
108   WIFI_PHY_LAYER_PROTOCOL_NONE,
109   // An obsolete modes introduced by the original 802.11, e.g. IR, FHSS.
110   WIFI_PHY_LAYER_PROTOCOL_ANCIENT,
111   // 802.11a, OFDM-based rates.
112   WIFI_PHY_LAYER_PROTOCOL_A,
113   // 802.11b, DSSS or HR DSSS.
114   WIFI_PHY_LAYER_PROTOCOL_B,
115   // 802.11g, same rates as 802.11a but compatible with 802.11b.
116   WIFI_PHY_LAYER_PROTOCOL_G,
117   // 802.11n, HT rates.
118   WIFI_PHY_LAYER_PROTOCOL_N,
119   // Unclassified mode or failure to identify.
120   WIFI_PHY_LAYER_PROTOCOL_UNKNOWN,
121   // 802.11ac
122   WIFI_PHY_LAYER_PROTOCOL_AC,
123   // 802.11ad
124   WIFI_PHY_LAYER_PROTOCOL_AD,
125   // 802.11ax
126   WIFI_PHY_LAYER_PROTOCOL_AX
127 };
128 
129 // Characterize the PHY mode of the currently associated access point.
130 // Currently only available on Windows.
131 NET_EXPORT WifiPHYLayerProtocol GetWifiPHYLayerProtocol();
132 
133 enum WifiOptions {
134   // Disables background SSID scans.
135   WIFI_OPTIONS_DISABLE_SCAN =  1 << 0,
136   // Enables media streaming mode.
137   WIFI_OPTIONS_MEDIA_STREAMING_MODE = 1 << 1
138 };
139 
140 class NET_EXPORT ScopedWifiOptions {
141  public:
142   ScopedWifiOptions() = default;
143   ScopedWifiOptions(const ScopedWifiOptions&) = delete;
144   ScopedWifiOptions& operator=(const ScopedWifiOptions&) = delete;
145   virtual ~ScopedWifiOptions();
146 };
147 
148 // Set temporary options on all wifi interfaces.
149 // |options| is an ORed bitfield of WifiOptions.
150 // Options are automatically disabled when the scoped pointer
151 // is freed. Currently only available on Windows.
152 NET_EXPORT std::unique_ptr<ScopedWifiOptions> SetWifiOptions(int options);
153 
154 // Returns the hostname of the current system. Returns empty string on failure.
155 NET_EXPORT std::string GetHostName();
156 
157 }  // namespace net
158 
159 #endif  // NET_BASE_NETWORK_INTERFACES_H_
160