OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/net_util_linux.h" | 5 #include "net/base/net_util_linux.h" |
6 | 6 |
7 #if !defined(OS_ANDROID) | 7 #if !defined(OS_ANDROID) |
8 #include <linux/ethtool.h> | 8 #include <linux/ethtool.h> |
9 #endif // !defined(OS_ANDROID) | 9 #endif // !defined(OS_ANDROID) |
10 #include <linux/if.h> | 10 #include <linux/if.h> |
11 #include <linux/sockios.h> | 11 #include <linux/sockios.h> |
12 #include <linux/wireless.h> | 12 #include <linux/wireless.h> |
13 #include <set> | 13 #include <set> |
14 #include <sys/ioctl.h> | 14 #include <sys/ioctl.h> |
15 #include <sys/types.h> | 15 #include <sys/types.h> |
16 | 16 |
17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
18 #include "base/files/scoped_file.h" | 18 #include "base/files/scoped_file.h" |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
21 #include "base/stl_util.h" | |
22 #include "base/strings/string_number_conversions.h" | 21 #include "base/strings/string_number_conversions.h" |
23 #include "base/strings/string_tokenizer.h" | 22 #include "base/strings/string_tokenizer.h" |
24 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
25 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
26 #include "net/base/address_tracker_linux.h" | 25 #include "net/base/address_tracker_linux.h" |
27 #include "net/base/escape.h" | 26 #include "net/base/escape.h" |
28 #include "net/base/ip_endpoint.h" | 27 #include "net/base/ip_endpoint.h" |
29 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
30 #include "net/base/net_util_posix.h" | 29 #include "net/base/net_util_posix.h" |
31 #include "url/gurl.h" | 30 #include "url/gurl.h" |
(...skipping 30 matching lines...) Expand all Loading... |
62 } | 61 } |
63 | 62 |
64 return true; | 63 return true; |
65 } | 64 } |
66 | 65 |
67 } // namespace | 66 } // namespace |
68 | 67 |
69 namespace internal { | 68 namespace internal { |
70 | 69 |
71 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { | 70 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { |
72 return vector_as_array(&ip); | 71 #if defined(OS_ANDROID) |
| 72 return ip.begin(); |
| 73 #else |
| 74 return ip.data(); |
| 75 #endif |
73 } | 76 } |
74 | 77 |
75 // Gets the connection type for interface |ifname| by checking for wireless | 78 // Gets the connection type for interface |ifname| by checking for wireless |
76 // or ethtool extensions. | 79 // or ethtool extensions. |
77 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( | 80 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( |
78 const std::string& ifname) { | 81 const std::string& ifname) { |
79 base::ScopedFD s(socket(AF_INET, SOCK_STREAM, 0)); | 82 base::ScopedFD s(socket(AF_INET, SOCK_STREAM, 0)); |
80 if (!s.is_valid()) | 83 if (!s.is_valid()) |
81 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 84 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
82 | 85 |
(...skipping 10 matching lines...) Expand all Loading... |
93 struct ifreq ifr = {}; | 96 struct ifreq ifr = {}; |
94 ifr.ifr_data = &ecmd; | 97 ifr.ifr_data = &ecmd; |
95 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); | 98 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); |
96 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) | 99 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) |
97 return NetworkChangeNotifier::CONNECTION_ETHERNET; | 100 return NetworkChangeNotifier::CONNECTION_ETHERNET; |
98 #endif // !defined(OS_ANDROID) | 101 #endif // !defined(OS_ANDROID) |
99 | 102 |
100 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 103 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
101 } | 104 } |
102 | 105 |
| 106 std::string GetInterfaceSSID(const std::string& ifname) { |
| 107 base::ScopedFD ioctl_socket(socket(AF_INET, SOCK_DGRAM, 0)); |
| 108 if (!ioctl_socket.is_valid()) |
| 109 return ""; |
| 110 struct iwreq wreq = {}; |
| 111 strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1); |
| 112 |
| 113 char ssid[IW_ESSID_MAX_SIZE + 1] = {0}; |
| 114 wreq.u.essid.pointer = ssid; |
| 115 wreq.u.essid.length = IW_ESSID_MAX_SIZE; |
| 116 if (ioctl(ioctl_socket.get(), SIOCGIWESSID, &wreq) != -1) |
| 117 return ssid; |
| 118 return ""; |
| 119 } |
| 120 |
103 bool GetNetworkListImpl( | 121 bool GetNetworkListImpl( |
104 NetworkInterfaceList* networks, | 122 NetworkInterfaceList* networks, |
105 int policy, | 123 int policy, |
106 const base::hash_set<int>& online_links, | 124 const base::hash_set<int>& online_links, |
107 const internal::AddressTrackerLinux::AddressMap& address_map, | 125 const internal::AddressTrackerLinux::AddressMap& address_map, |
108 GetInterfaceNameFunction get_interface_name) { | 126 GetInterfaceNameFunction get_interface_name) { |
109 std::map<int, std::string> ifnames; | 127 std::map<int, std::string> ifnames; |
110 | 128 |
111 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 129 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
112 address_map.begin(); | 130 address_map.begin(); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 GetInterfaceConnectionType(ifname); | 180 GetInterfaceConnectionType(ifname); |
163 | 181 |
164 networks->push_back( | 182 networks->push_back( |
165 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, | 183 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
166 it->second.ifa_prefixlen, ip_attributes)); | 184 it->second.ifa_prefixlen, ip_attributes)); |
167 } | 185 } |
168 | 186 |
169 return true; | 187 return true; |
170 } | 188 } |
171 | 189 |
| 190 std::string GetWifiSSIDFromInterfaceListInternal( |
| 191 const NetworkInterfaceList& interfaces, |
| 192 internal::GetInterfaceSSIDFunction get_interface_ssid) { |
| 193 std::string connected_ssid; |
| 194 for (size_t i = 0; i < interfaces.size(); ++i) { |
| 195 if (interfaces[i].type != NetworkChangeNotifier::CONNECTION_WIFI) |
| 196 return ""; |
| 197 std::string ssid = get_interface_ssid(interfaces[i].name); |
| 198 if (i == 0) { |
| 199 connected_ssid = ssid; |
| 200 } else if (ssid != connected_ssid) { |
| 201 return ""; |
| 202 } |
| 203 } |
| 204 return connected_ssid; |
| 205 } |
| 206 |
172 } // namespace internal | 207 } // namespace internal |
173 | 208 |
174 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 209 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
175 if (networks == NULL) | 210 if (networks == NULL) |
176 return false; | 211 return false; |
177 | 212 |
178 internal::AddressTrackerLinux tracker; | 213 internal::AddressTrackerLinux tracker; |
179 tracker.Init(); | 214 tracker.Init(); |
180 | 215 |
181 return internal::GetNetworkListImpl( | 216 return internal::GetNetworkListImpl( |
182 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), | 217 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
183 &internal::AddressTrackerLinux::GetInterfaceName); | 218 &internal::AddressTrackerLinux::GetInterfaceName); |
184 } | 219 } |
185 | 220 |
| 221 std::string GetWifiSSID() { |
| 222 NetworkInterfaceList networks; |
| 223 if (GetNetworkList(&networks, net::INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)) { |
| 224 return internal::GetWifiSSIDFromInterfaceListInternal( |
| 225 networks, internal::GetInterfaceSSID); |
| 226 } |
| 227 return ""; |
| 228 } |
| 229 |
186 } // namespace net | 230 } // namespace net |
OLD | NEW |