• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum WifiOptions {
106   // Disables background SSID scans.
107   WIFI_OPTIONS_DISABLE_SCAN =  1 << 0,
108   // Enables media streaming mode.
109   WIFI_OPTIONS_MEDIA_STREAMING_MODE = 1 << 1
110 };
111 
112 class NET_EXPORT ScopedWifiOptions {
113  public:
114   ScopedWifiOptions() = default;
115   ScopedWifiOptions(const ScopedWifiOptions&) = delete;
116   ScopedWifiOptions& operator=(const ScopedWifiOptions&) = delete;
117   virtual ~ScopedWifiOptions();
118 };
119 
120 // Set temporary options on all wifi interfaces.
121 // |options| is an ORed bitfield of WifiOptions.
122 // Options are automatically disabled when the scoped pointer
123 // is freed. Currently only available on Windows.
124 NET_EXPORT std::unique_ptr<ScopedWifiOptions> SetWifiOptions(int options);
125 
126 // Returns the hostname of the current system. Returns empty string on failure.
127 NET_EXPORT std::string GetHostName();
128 
129 }  // namespace net
130 
131 #endif  // NET_BASE_NETWORK_INTERFACES_H_
132