Chromium Code Reviews| 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 int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0); | |
| 108 if (ioctl_socket < 0) | |
| 109 return ""; | |
| 110 struct iwreq wreq; | |
| 111 memset(&wreq, 0, sizeof(wreq)); | |
| 112 strncpy(wreq.ifr_name, ifname.c_str(), IFNAMSIZ - 1); | |
| 113 | |
| 114 char ssid[IW_ESSID_MAX_SIZE + 1] = {0}; | |
| 115 wreq.u.essid.pointer = ssid; | |
| 116 wreq.u.essid.length = IW_ESSID_MAX_SIZE; | |
| 117 int rv = ioctl(ioctl_socket, SIOCGIWESSID, &wreq); | |
| 118 close(ioctl_socket); | |
| 119 return (rv == 0) ? ssid : ""; | |
| 120 } | |
| 121 | |
| 106 bool GetNetworkListImpl( | 122 bool GetNetworkListImpl( |
| 107 NetworkInterfaceList* networks, | 123 NetworkInterfaceList* networks, |
| 108 int policy, | 124 int policy, |
| 109 const base::hash_set<int>& online_links, | 125 const base::hash_set<int>& online_links, |
| 110 const internal::AddressTrackerLinux::AddressMap& address_map, | 126 const internal::AddressTrackerLinux::AddressMap& address_map, |
| 111 GetInterfaceNameFunction get_interface_name) { | 127 GetInterfaceNameFunction get_interface_name) { |
| 112 std::map<int, std::string> ifnames; | 128 std::map<int, std::string> ifnames; |
| 113 | 129 |
| 114 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 130 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
| 115 address_map.begin(); | 131 address_map.begin(); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 GetInterfaceConnectionType(ifname); | 181 GetInterfaceConnectionType(ifname); |
| 166 | 182 |
| 167 networks->push_back( | 183 networks->push_back( |
| 168 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, | 184 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
| 169 it->second.ifa_prefixlen, ip_attributes)); | 185 it->second.ifa_prefixlen, ip_attributes)); |
| 170 } | 186 } |
| 171 | 187 |
| 172 return true; | 188 return true; |
| 173 } | 189 } |
| 174 | 190 |
| 191 std::string GetWifiSSIDFromInterfaceListInternal( | |
| 192 const NetworkInterfaceList& interfaces, | |
| 193 internal::GetInterfaceSSIDFunction get_interface_ssid) { | |
| 194 std::string connected_ssid; | |
| 195 bool first = true; | |
| 196 for (size_t i = 0; i < interfaces.size(); ++i) { | |
| 197 LOG(ERROR) << "Interface " << i << ": " << interfaces[i].name; | |
|
meacer
2015/02/04 01:34:33
Debug cruft, to be removed.
| |
| 198 if (interfaces[i].type != NetworkChangeNotifier::CONNECTION_WIFI) | |
| 199 return ""; | |
| 200 std::string ssid = get_interface_ssid(interfaces[i].name); | |
| 201 if (first) { | |
| 202 first = false; | |
| 203 connected_ssid = ssid; | |
| 204 } else if (ssid != connected_ssid) { | |
| 205 return ""; | |
| 206 } | |
| 207 } | |
| 208 return connected_ssid; | |
| 209 } | |
| 210 | |
| 175 } // namespace internal | 211 } // namespace internal |
| 176 | 212 |
| 177 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 213 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 178 if (networks == NULL) | 214 if (networks == NULL) |
| 179 return false; | 215 return false; |
| 180 | 216 |
| 181 internal::AddressTrackerLinux tracker; | 217 internal::AddressTrackerLinux tracker; |
| 182 tracker.Init(); | 218 tracker.Init(); |
| 183 | 219 |
| 184 return internal::GetNetworkListImpl( | 220 return internal::GetNetworkListImpl( |
| 185 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), | 221 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
| 186 &internal::AddressTrackerLinux::GetInterfaceName); | 222 &internal::AddressTrackerLinux::GetInterfaceName); |
| 187 } | 223 } |
| 188 | 224 |
| 225 std::string GetWifiSSIDFromInterfaceList( | |
| 226 const NetworkInterfaceList& interfaces) { | |
| 227 return internal::GetWifiSSIDFromInterfaceListInternal( | |
| 228 interfaces, internal::GetInterfaceSSID); | |
| 229 } | |
| 230 | |
| 189 } // namespace net | 231 } // namespace net |
| OLD | NEW |