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