| 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> |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 struct ifreq ifr = {}; | 96 struct ifreq ifr = {}; |
| 97 ifr.ifr_data = &ecmd; | 97 ifr.ifr_data = &ecmd; |
| 98 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); | 98 strncpy(ifr.ifr_name, ifname.c_str(), IFNAMSIZ - 1); |
| 99 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) | 99 if (ioctl(s.get(), SIOCETHTOOL, &ifr) != -1) |
| 100 return NetworkChangeNotifier::CONNECTION_ETHERNET; | 100 return NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 101 #endif // !defined(OS_ANDROID) | 101 #endif // !defined(OS_ANDROID) |
| 102 | 102 |
| 103 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 103 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 104 } | 104 } |
| 105 | 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 |
| 106 bool GetNetworkListImpl( | 121 bool GetNetworkListImpl( |
| 107 NetworkInterfaceList* networks, | 122 NetworkInterfaceList* networks, |
| 108 int policy, | 123 int policy, |
| 109 const base::hash_set<int>& online_links, | 124 const base::hash_set<int>& online_links, |
| 110 const internal::AddressTrackerLinux::AddressMap& address_map, | 125 const internal::AddressTrackerLinux::AddressMap& address_map, |
| 111 GetInterfaceNameFunction get_interface_name) { | 126 GetInterfaceNameFunction get_interface_name) { |
| 112 std::map<int, std::string> ifnames; | 127 std::map<int, std::string> ifnames; |
| 113 | 128 |
| 114 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 129 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
| 115 address_map.begin(); | 130 address_map.begin(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 GetInterfaceConnectionType(ifname); | 180 GetInterfaceConnectionType(ifname); |
| 166 | 181 |
| 167 networks->push_back( | 182 networks->push_back( |
| 168 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, | 183 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
| 169 it->second.ifa_prefixlen, ip_attributes)); | 184 it->second.ifa_prefixlen, ip_attributes)); |
| 170 } | 185 } |
| 171 | 186 |
| 172 return true; | 187 return true; |
| 173 } | 188 } |
| 174 | 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 |
| 175 } // namespace internal | 207 } // namespace internal |
| 176 | 208 |
| 177 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 209 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 178 if (networks == NULL) | 210 if (networks == NULL) |
| 179 return false; | 211 return false; |
| 180 | 212 |
| 181 internal::AddressTrackerLinux tracker; | 213 internal::AddressTrackerLinux tracker; |
| 182 tracker.Init(); | 214 tracker.Init(); |
| 183 | 215 |
| 184 return internal::GetNetworkListImpl( | 216 return internal::GetNetworkListImpl( |
| 185 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), | 217 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
| 186 &internal::AddressTrackerLinux::GetInterfaceName); | 218 &internal::AddressTrackerLinux::GetInterfaceName); |
| 187 } | 219 } |
| 188 | 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 |
| 189 } // namespace net | 230 } // namespace net |
| OLD | NEW |