| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include <iphlpapi.h> | 7 #include <iphlpapi.h> |
| 8 #include <wlanapi.h> | 8 #include <wlanapi.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 NetworkChangeNotifier::CONNECTION_UNKNOWN; | 42 NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 43 if (ifType == IF_TYPE_ETHERNET_CSMACD) { | 43 if (ifType == IF_TYPE_ETHERNET_CSMACD) { |
| 44 type = NetworkChangeNotifier::CONNECTION_ETHERNET; | 44 type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 45 } else if (ifType == IF_TYPE_IEEE80211) { | 45 } else if (ifType == IF_TYPE_IEEE80211) { |
| 46 type = NetworkChangeNotifier::CONNECTION_WIFI; | 46 type = NetworkChangeNotifier::CONNECTION_WIFI; |
| 47 } | 47 } |
| 48 // TODO(mallinath) - Cellular? | 48 // TODO(mallinath) - Cellular? |
| 49 return type; | 49 return type; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Returns scoped_ptr to WLAN_CONNECTION_ATTRIBUTES. The scoped_ptr may hold a |
| 53 // NULL pointer if WLAN_CONNECTION_ATTRIBUTES is unavailable. |
| 54 scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter> |
| 55 GetConnectionAttributes() { |
| 56 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); |
| 57 if (!wlanapi.initialized) |
| 58 return scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter>(); |
| 59 |
| 60 internal::WlanHandle client; |
| 61 DWORD cur_version = 0; |
| 62 const DWORD kMaxClientVersion = 2; |
| 63 { |
| 64 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is |
| 65 // fixed. |
| 66 tracked_objects::ScopedTracker tracking_profile( |
| 67 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 OpenHandle()")); |
| 68 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); |
| 69 if (result != ERROR_SUCCESS) |
| 70 return scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter>(); |
| 71 } |
| 72 |
| 73 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; |
| 74 DWORD result = |
| 75 wlanapi.enum_interfaces_func(client.Get(), NULL, &interface_list_ptr); |
| 76 if (result != ERROR_SUCCESS) |
| 77 return scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter>(); |
| 78 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> interface_list( |
| 79 interface_list_ptr); |
| 80 |
| 81 // Assume at most one connected wifi interface. |
| 82 WLAN_INTERFACE_INFO* info = NULL; |
| 83 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { |
| 84 if (interface_list->InterfaceInfo[i].isState == |
| 85 wlan_interface_state_connected) { |
| 86 info = &interface_list->InterfaceInfo[i]; |
| 87 break; |
| 88 } |
| 89 } |
| 90 |
| 91 if (info == NULL) |
| 92 return scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter>(); |
| 93 |
| 94 WLAN_CONNECTION_ATTRIBUTES* conn_info_ptr = nullptr; |
| 95 DWORD conn_info_size = 0; |
| 96 WLAN_OPCODE_VALUE_TYPE op_code; |
| 97 result = wlanapi.query_interface_func( |
| 98 client.Get(), &info->InterfaceGuid, wlan_intf_opcode_current_connection, |
| 99 NULL, &conn_info_size, reinterpret_cast<VOID**>(&conn_info_ptr), |
| 100 &op_code); |
| 101 if (result != ERROR_SUCCESS) |
| 102 return scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter>(); |
| 103 |
| 104 DCHECK(conn_info_ptr); |
| 105 return scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter>( |
| 106 conn_info_ptr); |
| 107 } |
| 108 |
| 52 } // namespace | 109 } // namespace |
| 53 | 110 |
| 54 namespace internal { | 111 namespace internal { |
| 55 | 112 |
| 56 base::LazyInstance<WlanApi>::Leaky lazy_wlanapi = | 113 base::LazyInstance<WlanApi>::Leaky lazy_wlanapi = |
| 57 LAZY_INSTANCE_INITIALIZER; | 114 LAZY_INSTANCE_INITIALIZER; |
| 58 | 115 |
| 59 WlanApi& WlanApi::GetInstance() { | 116 WlanApi& WlanApi::GetInstance() { |
| 60 return lazy_wlanapi.Get(); | 117 return lazy_wlanapi.Get(); |
| 61 } | 118 } |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); | 251 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); |
| 195 if (result != NO_ERROR) { | 252 if (result != NO_ERROR) { |
| 196 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; | 253 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; |
| 197 return false; | 254 return false; |
| 198 } | 255 } |
| 199 | 256 |
| 200 return internal::GetNetworkListImpl(networks, policy, is_xp, adapters); | 257 return internal::GetNetworkListImpl(networks, policy, is_xp, adapters); |
| 201 } | 258 } |
| 202 | 259 |
| 203 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 260 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
| 204 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); | 261 auto conn_info = GetConnectionAttributes(); |
| 205 if (!wlanapi.initialized) | 262 |
| 263 if (!conn_info.get()) |
| 206 return WIFI_PHY_LAYER_PROTOCOL_NONE; | 264 return WIFI_PHY_LAYER_PROTOCOL_NONE; |
| 207 | 265 |
| 208 internal::WlanHandle client; | |
| 209 DWORD cur_version = 0; | |
| 210 const DWORD kMaxClientVersion = 2; | |
| 211 { | |
| 212 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is | |
| 213 // fixed. | |
| 214 tracked_objects::ScopedTracker tracking_profile( | |
| 215 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 OpenHandle()")); | |
| 216 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); | |
| 217 if (result != ERROR_SUCCESS) | |
| 218 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 219 } | |
| 220 | |
| 221 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; | |
| 222 DWORD result = | |
| 223 wlanapi.enum_interfaces_func(client.Get(), NULL, &interface_list_ptr); | |
| 224 if (result != ERROR_SUCCESS) | |
| 225 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 226 scoped_ptr<WLAN_INTERFACE_INFO_LIST, internal::WlanApiDeleter> interface_list( | |
| 227 interface_list_ptr); | |
| 228 | |
| 229 // Assume at most one connected wifi interface. | |
| 230 WLAN_INTERFACE_INFO* info = NULL; | |
| 231 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { | |
| 232 if (interface_list->InterfaceInfo[i].isState == | |
| 233 wlan_interface_state_connected) { | |
| 234 info = &interface_list->InterfaceInfo[i]; | |
| 235 break; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 if (info == NULL) | |
| 240 return WIFI_PHY_LAYER_PROTOCOL_NONE; | |
| 241 | |
| 242 WLAN_CONNECTION_ATTRIBUTES* conn_info_ptr; | |
| 243 DWORD conn_info_size = 0; | |
| 244 WLAN_OPCODE_VALUE_TYPE op_code; | |
| 245 result = wlanapi.query_interface_func( | |
| 246 client.Get(), &info->InterfaceGuid, wlan_intf_opcode_current_connection, | |
| 247 NULL, &conn_info_size, reinterpret_cast<VOID**>(&conn_info_ptr), | |
| 248 &op_code); | |
| 249 if (result != ERROR_SUCCESS) | |
| 250 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | |
| 251 scoped_ptr<WLAN_CONNECTION_ATTRIBUTES, internal::WlanApiDeleter> conn_info( | |
| 252 conn_info_ptr); | |
| 253 | |
| 254 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { | 266 switch (conn_info->wlanAssociationAttributes.dot11PhyType) { |
| 255 case dot11_phy_type_fhss: | 267 case dot11_phy_type_fhss: |
| 256 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT; | 268 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT; |
| 257 case dot11_phy_type_dsss: | 269 case dot11_phy_type_dsss: |
| 258 return WIFI_PHY_LAYER_PROTOCOL_B; | 270 return WIFI_PHY_LAYER_PROTOCOL_B; |
| 259 case dot11_phy_type_irbaseband: | 271 case dot11_phy_type_irbaseband: |
| 260 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT; | 272 return WIFI_PHY_LAYER_PROTOCOL_ANCIENT; |
| 261 case dot11_phy_type_ofdm: | 273 case dot11_phy_type_ofdm: |
| 262 return WIFI_PHY_LAYER_PROTOCOL_A; | 274 return WIFI_PHY_LAYER_PROTOCOL_A; |
| 263 case dot11_phy_type_hrdsss: | 275 case dot11_phy_type_hrdsss: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 333 |
| 322 private: | 334 private: |
| 323 internal::WlanHandle client_; | 335 internal::WlanHandle client_; |
| 324 }; | 336 }; |
| 325 | 337 |
| 326 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { | 338 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { |
| 327 return scoped_ptr<ScopedWifiOptions>(new WifiOptionSetter(options)); | 339 return scoped_ptr<ScopedWifiOptions>(new WifiOptionSetter(options)); |
| 328 } | 340 } |
| 329 | 341 |
| 330 std::string GetWifiSSID() { | 342 std::string GetWifiSSID() { |
| 331 NOTIMPLEMENTED(); | 343 auto conn_info = GetConnectionAttributes(); |
| 332 return ""; | 344 |
| 345 if (!conn_info.get()) |
| 346 return ""; |
| 347 |
| 348 const DOT11_SSID dot11_ssid = conn_info->wlanAssociationAttributes.dot11Ssid; |
| 349 return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID), |
| 350 dot11_ssid.uSSIDLength); |
| 333 } | 351 } |
| 334 | 352 |
| 335 } // namespace net | 353 } // namespace net |
| OLD | NEW |