Chromium Code Reviews| 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> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 20 #include "base/win/scoped_handle.h" | 20 #include "base/win/scoped_handle.h" |
| 21 #include "base/win/windows_version.h" | 21 #include "base/win/windows_version.h" |
| 22 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
| 23 #include "net/base/ip_endpoint.h" | 23 #include "net/base/ip_endpoint.h" |
| 24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 25 #include "net/base/net_util_win.h" | |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 | 27 |
| 28 using namespace net::internal; | |
|
pauljensen
2014/09/23 17:59:03
ditto
hubbe
2014/09/23 18:06:16
Done.
| |
| 29 | |
| 27 namespace net { | 30 namespace net { |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 31 struct WlanApi { | |
| 32 typedef DWORD (WINAPI *WlanOpenHandleFunc)( | |
| 33 DWORD, VOID*, DWORD*, HANDLE*); | |
| 34 typedef DWORD (WINAPI *WlanEnumInterfacesFunc)( | |
| 35 HANDLE, VOID*, WLAN_INTERFACE_INFO_LIST**); | |
| 36 typedef DWORD (WINAPI *WlanQueryInterfaceFunc)( | |
| 37 HANDLE, const GUID*, WLAN_INTF_OPCODE, VOID*, DWORD*, VOID**, | |
| 38 WLAN_OPCODE_VALUE_TYPE*); | |
| 39 typedef VOID (WINAPI *WlanFreeMemoryFunc)(VOID*); | |
| 40 typedef DWORD (WINAPI *WlanCloseHandleFunc)(HANDLE, VOID*); | |
| 41 | |
| 42 WlanApi() : initialized(false) { | |
| 43 // Use an absolute path to load the DLL to avoid DLL preloading attacks. | |
| 44 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll"; | |
| 45 wchar_t path[MAX_PATH] = {0}; | |
| 46 ExpandEnvironmentStrings(kDLL, path, arraysize(path)); | |
| 47 module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
| 48 if (!module) | |
| 49 return; | |
| 50 | |
| 51 open_handle_func = reinterpret_cast<WlanOpenHandleFunc>( | |
| 52 ::GetProcAddress(module, "WlanOpenHandle")); | |
| 53 enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>( | |
| 54 ::GetProcAddress(module, "WlanEnumInterfaces")); | |
| 55 query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>( | |
| 56 ::GetProcAddress(module, "WlanQueryInterface")); | |
| 57 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>( | |
| 58 ::GetProcAddress(module, "WlanFreeMemory")); | |
| 59 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( | |
| 60 ::GetProcAddress(module, "WlanCloseHandle")); | |
| 61 initialized = open_handle_func && enum_interfaces_func && | |
| 62 query_interface_func && free_memory_func && | |
| 63 close_handle_func; | |
| 64 } | |
| 65 | |
| 66 template <typename T> | |
| 67 DWORD OpenHandle(DWORD client_version, DWORD* cur_version, T* handle) const { | |
| 68 HANDLE temp_handle; | |
| 69 DWORD result = open_handle_func(client_version, NULL, cur_version, | |
| 70 &temp_handle); | |
| 71 if (result != ERROR_SUCCESS) | |
| 72 return result; | |
| 73 handle->Set(temp_handle); | |
| 74 return ERROR_SUCCESS; | |
| 75 } | |
| 76 | |
| 77 HMODULE module; | |
| 78 WlanOpenHandleFunc open_handle_func; | |
| 79 WlanEnumInterfacesFunc enum_interfaces_func; | |
| 80 WlanQueryInterfaceFunc query_interface_func; | |
| 81 WlanFreeMemoryFunc free_memory_func; | |
| 82 WlanCloseHandleFunc close_handle_func; | |
| 83 bool initialized; | |
| 84 }; | |
| 85 | |
| 86 // Converts Windows defined types to NetworkInterfaceType. | 34 // Converts Windows defined types to NetworkInterfaceType. |
| 87 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { | 35 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(DWORD ifType) { |
| 88 // Bail out for pre-Vista versions of Windows which are documented to give | 36 // Bail out for pre-Vista versions of Windows which are documented to give |
| 89 // inaccurate results like returning Ethernet for WiFi. | 37 // inaccurate results like returning Ethernet for WiFi. |
| 90 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366058.aspx | 38 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366058.aspx |
| 91 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 39 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 92 return NetworkChangeNotifier::CONNECTION_UNKNOWN; | 40 return NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 93 | 41 |
| 94 NetworkChangeNotifier::ConnectionType type = | 42 NetworkChangeNotifier::ConnectionType type = |
| 95 NetworkChangeNotifier::CONNECTION_UNKNOWN; | 43 NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 96 if (ifType == IF_TYPE_ETHERNET_CSMACD) { | 44 if (ifType == IF_TYPE_ETHERNET_CSMACD) { |
| 97 type = NetworkChangeNotifier::CONNECTION_ETHERNET; | 45 type = NetworkChangeNotifier::CONNECTION_ETHERNET; |
| 98 } else if (ifType == IF_TYPE_IEEE80211) { | 46 } else if (ifType == IF_TYPE_IEEE80211) { |
| 99 type = NetworkChangeNotifier::CONNECTION_WIFI; | 47 type = NetworkChangeNotifier::CONNECTION_WIFI; |
| 100 } | 48 } |
| 101 // TODO(mallinath) - Cellular? | 49 // TODO(mallinath) - Cellular? |
| 102 return type; | 50 return type; |
| 103 } | 51 } |
| 104 | 52 |
| 105 } // namespace | 53 } // namespace |
| 106 | 54 |
| 55 namespace internal { | |
| 56 | |
| 57 base::LazyInstance<WlanApi>::Leaky lazy_wlanapi = | |
| 58 LAZY_INSTANCE_INITIALIZER; | |
| 59 | |
| 60 WlanApi& WlanApi::GetInstance() { | |
| 61 return lazy_wlanapi.Get(); | |
| 62 } | |
| 63 | |
| 64 WlanApi::WlanApi() : initialized(false) { | |
| 65 // Use an absolute path to load the DLL to avoid DLL preloading attacks. | |
| 66 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll"; | |
| 67 wchar_t path[MAX_PATH] = {0}; | |
| 68 ExpandEnvironmentStrings(kDLL, path, arraysize(path)); | |
| 69 module = ::LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
| 70 if (!module) | |
| 71 return; | |
| 72 | |
| 73 open_handle_func = reinterpret_cast<WlanOpenHandleFunc>( | |
| 74 ::GetProcAddress(module, "WlanOpenHandle")); | |
| 75 enum_interfaces_func = reinterpret_cast<WlanEnumInterfacesFunc>( | |
| 76 ::GetProcAddress(module, "WlanEnumInterfaces")); | |
| 77 query_interface_func = reinterpret_cast<WlanQueryInterfaceFunc>( | |
| 78 ::GetProcAddress(module, "WlanQueryInterface")); | |
| 79 set_interface_func = reinterpret_cast<WlanSetInterfaceFunc>( | |
| 80 ::GetProcAddress(module, "WlanSetInterface")); | |
| 81 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>( | |
| 82 ::GetProcAddress(module, "WlanFreeMemory")); | |
| 83 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( | |
| 84 ::GetProcAddress(module, "WlanCloseHandle")); | |
| 85 initialized = open_handle_func && enum_interfaces_func && | |
| 86 query_interface_func && set_interface_func && | |
| 87 free_memory_func && close_handle_func; | |
| 88 } | |
| 89 | |
| 90 } // namespace internal | |
| 91 | |
| 107 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 92 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 108 // GetAdaptersAddresses() may require IO operations. | 93 // GetAdaptersAddresses() may require IO operations. |
| 109 base::ThreadRestrictions::AssertIOAllowed(); | 94 base::ThreadRestrictions::AssertIOAllowed(); |
| 110 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; | 95 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; |
| 111 ULONG len = 0; | 96 ULONG len = 0; |
| 112 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; | 97 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; |
| 113 // First get number of networks. | 98 // First get number of networks. |
| 114 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); | 99 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); |
| 115 if (result != ERROR_BUFFER_OVERFLOW) { | 100 if (result != ERROR_BUFFER_OVERFLOW) { |
| 116 // There are 0 networks. | 101 // There are 0 networks. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 } | 194 } |
| 210 } | 195 } |
| 211 | 196 |
| 212 if (ipv6_address.get()) { | 197 if (ipv6_address.get()) { |
| 213 networks->push_back(*(ipv6_address.get())); | 198 networks->push_back(*(ipv6_address.get())); |
| 214 } | 199 } |
| 215 return true; | 200 return true; |
| 216 } | 201 } |
| 217 | 202 |
| 218 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 203 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
| 219 static base::LazyInstance<WlanApi>::Leaky lazy_wlanapi = | 204 const WlanApi& wlanapi = WlanApi::GetInstance(); |
| 220 LAZY_INSTANCE_INITIALIZER; | |
| 221 | |
| 222 struct WlanApiHandleTraits { | |
| 223 typedef HANDLE Handle; | |
| 224 | |
| 225 static bool CloseHandle(HANDLE handle) { | |
| 226 return lazy_wlanapi.Get().close_handle_func(handle, NULL) == | |
| 227 ERROR_SUCCESS; | |
| 228 } | |
| 229 static bool IsHandleValid(HANDLE handle) { | |
| 230 return base::win::HandleTraits::IsHandleValid(handle); | |
| 231 } | |
| 232 static HANDLE NullHandle() { | |
| 233 return base::win::HandleTraits::NullHandle(); | |
| 234 } | |
| 235 }; | |
| 236 | |
| 237 typedef base::win::GenericScopedHandle< | |
| 238 WlanApiHandleTraits, | |
| 239 base::win::DummyVerifierTraits> WlanHandle; | |
| 240 | |
| 241 struct WlanApiDeleter { | |
| 242 inline void operator()(void* ptr) const { | |
| 243 lazy_wlanapi.Get().free_memory_func(ptr); | |
| 244 } | |
| 245 }; | |
| 246 | |
| 247 const WlanApi& wlanapi = lazy_wlanapi.Get(); | |
| 248 if (!wlanapi.initialized) | 205 if (!wlanapi.initialized) |
| 249 return WIFI_PHY_LAYER_PROTOCOL_NONE; | 206 return WIFI_PHY_LAYER_PROTOCOL_NONE; |
| 250 | 207 |
| 251 WlanHandle client; | 208 WlanHandle client; |
| 252 DWORD cur_version = 0; | 209 DWORD cur_version = 0; |
| 253 const DWORD kMaxClientVersion = 2; | 210 const DWORD kMaxClientVersion = 2; |
| 254 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); | 211 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); |
| 255 if (result != ERROR_SUCCESS) | 212 if (result != ERROR_SUCCESS) |
| 256 return WIFI_PHY_LAYER_PROTOCOL_NONE; | 213 return WIFI_PHY_LAYER_PROTOCOL_NONE; |
| 257 | 214 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 return WIFI_PHY_LAYER_PROTOCOL_B; | 256 return WIFI_PHY_LAYER_PROTOCOL_B; |
| 300 case dot11_phy_type_erp: | 257 case dot11_phy_type_erp: |
| 301 return WIFI_PHY_LAYER_PROTOCOL_G; | 258 return WIFI_PHY_LAYER_PROTOCOL_G; |
| 302 case dot11_phy_type_ht: | 259 case dot11_phy_type_ht: |
| 303 return WIFI_PHY_LAYER_PROTOCOL_N; | 260 return WIFI_PHY_LAYER_PROTOCOL_N; |
| 304 default: | 261 default: |
| 305 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; | 262 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
| 306 } | 263 } |
| 307 } | 264 } |
| 308 | 265 |
| 266 // Note: There is no need to explicitly set the options back | |
| 267 // as the OS will automatically set them back when the WlanHandle | |
| 268 // is closed. | |
| 269 class WifiOptionSetter : public ScopedWifiOptions { | |
| 270 public: | |
| 271 WifiOptionSetter(int options) { | |
| 272 const WlanApi& wlanapi = WlanApi::GetInstance(); | |
| 273 if (!wlanapi.initialized) | |
| 274 return; | |
| 275 | |
| 276 DWORD cur_version = 0; | |
| 277 const DWORD kMaxClientVersion = 2; | |
| 278 DWORD result = wlanapi.OpenHandle( | |
| 279 kMaxClientVersion, &cur_version, &client_); | |
| 280 if (result != ERROR_SUCCESS) | |
| 281 return; | |
| 282 | |
| 283 WLAN_INTERFACE_INFO_LIST* interface_list_ptr = NULL; | |
| 284 result = wlanapi.enum_interfaces_func(client_, NULL, &interface_list_ptr); | |
| 285 if (result != ERROR_SUCCESS) | |
| 286 return; | |
| 287 scoped_ptr<WLAN_INTERFACE_INFO_LIST, WlanApiDeleter> interface_list( | |
| 288 interface_list_ptr); | |
| 289 | |
| 290 for (unsigned i = 0; i < interface_list->dwNumberOfItems; ++i) { | |
| 291 WLAN_INTERFACE_INFO* info = &interface_list->InterfaceInfo[i]; | |
| 292 if (options & WIFI_OPTIONS_DISABLE_SCAN) { | |
| 293 BOOL data = false; | |
| 294 wlanapi.set_interface_func(client_, | |
| 295 &info->InterfaceGuid, | |
| 296 wlan_intf_opcode_background_scan_enabled, | |
| 297 sizeof(data), | |
| 298 &data, | |
| 299 NULL); | |
| 300 } | |
| 301 if (options & WIFI_OPTIONS_MEDIA_STREAMING_MODE) { | |
| 302 BOOL data = true; | |
| 303 wlanapi.set_interface_func(client_, | |
| 304 &info->InterfaceGuid, | |
| 305 wlan_intf_opcode_media_streaming_mode, | |
| 306 sizeof(data), | |
| 307 &data, | |
| 308 NULL); | |
| 309 } | |
| 310 } | |
| 311 } | |
| 312 | |
| 313 private: | |
| 314 WlanHandle client_; | |
| 315 }; | |
| 316 | |
| 317 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { | |
| 318 return scoped_ptr<ScopedWifiOptions>(new WifiOptionSetter(options)); | |
| 319 } | |
| 320 | |
| 309 } // namespace net | 321 } // namespace net |
| OLD | NEW |