| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // Windows Vista uses the Native Wifi (WLAN) API for accessing WiFi cards. See | |
| 6 // http://msdn.microsoft.com/en-us/library/ms705945(VS.85).aspx. Windows XP | |
| 7 // Service Pack 3 (and Windows XP Service Pack 2, if upgraded with a hot fix) | |
| 8 // also support a limited version of the WLAN API. See | |
| 9 // http://msdn.microsoft.com/en-us/library/bb204766.aspx. The WLAN API uses | |
| 10 // wlanapi.h, which is not part of the SDK used by Gears, so is replicated | |
| 11 // locally using data from the MSDN. | |
| 12 // | |
| 13 // Windows XP from Service Pack 2 onwards supports the Wireless Zero | |
| 14 // Configuration (WZC) programming interface. See | |
| 15 // http://msdn.microsoft.com/en-us/library/ms706587(VS.85).aspx. | |
| 16 // | |
| 17 // The MSDN recommends that one use the WLAN API where available, and WZC | |
| 18 // otherwise. | |
| 19 // | |
| 20 // However, it seems that WZC fails for some wireless cards. Also, WLAN seems | |
| 21 // not to work on XP SP3. So we use WLAN on Vista, and use NDIS directly | |
| 22 // otherwise. | |
| 23 | |
| 24 #include "device/geolocation/wifi_data_provider_win.h" | 5 #include "device/geolocation/wifi_data_provider_win.h" |
| 25 | 6 |
| 26 #include <windows.h> | 7 #include <windows.h> |
| 27 #include <winioctl.h> | 8 #include <winioctl.h> |
| 28 #include <wlanapi.h> | 9 #include <wlanapi.h> |
| 29 | 10 |
| 30 #include "base/memory/free_deleter.h" | 11 #include "base/memory/free_deleter.h" |
| 31 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 32 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
| 33 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/timer/elapsed_timer.h" |
| 34 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
| 35 #include "device/geolocation/wifi_data_provider_common.h" | 17 #include "device/geolocation/wifi_data_provider_common.h" |
| 36 #include "device/geolocation/wifi_data_provider_common_win.h" | 18 #include "device/geolocation/wifi_data_provider_common_win.h" |
| 37 #include "device/geolocation/wifi_data_provider_manager.h" | 19 #include "device/geolocation/wifi_data_provider_manager.h" |
| 38 | 20 |
| 39 // Taken from ndis.h for WinCE. | 21 namespace device { |
| 40 #define NDIS_STATUS_INVALID_LENGTH ((NDIS_STATUS)0xC0010014L) | |
| 41 #define NDIS_STATUS_BUFFER_TOO_SHORT ((NDIS_STATUS)0xC0010016L) | |
| 42 | 22 |
| 43 namespace device { | |
| 44 namespace { | 23 namespace { |
| 45 // The limits on the size of the buffer used for the OID query. | |
| 46 const int kInitialBufferSize = 2 << 12; // Good for about 50 APs. | |
| 47 const int kMaximumBufferSize = 2 << 20; // 2MB | |
| 48 | 24 |
| 49 // Length for generic string buffers passed to Windows APIs. | 25 static const int kDefaultPollingIntervalMs = 10 * 1000; // 10s |
| 50 const int kStringLength = 512; | 26 static const int kNoChangePollingIntervalMs = 2 * 60 * 1000; // 2 mins |
| 51 | 27 static const int kTwoNoChangePollingIntervalMs = 10 * 60 * 1000; // 10 mins |
| 52 // The time periods, in milliseconds, between successive polls of the wifi data. | 28 static const int kNoWifiPollingIntervalMs = 20 * 1000; // 20s |
| 53 const int kDefaultPollingInterval = 10000; // 10s | |
| 54 const int kNoChangePollingInterval = 120000; // 2 mins | |
| 55 const int kTwoNoChangePollingInterval = 600000; // 10 mins | |
| 56 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | |
| 57 | 29 |
| 58 // WlanOpenHandle | 30 // WlanOpenHandle |
| 59 typedef DWORD(WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion, | 31 typedef DWORD(WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion, |
| 60 PVOID pReserved, | 32 PVOID pReserved, |
| 61 PDWORD pdwNegotiatedVersion, | 33 PDWORD pdwNegotiatedVersion, |
| 62 PHANDLE phClientHandle); | 34 PHANDLE phClientHandle); |
| 63 | 35 |
| 64 // WlanEnumInterfaces | 36 // WlanEnumInterfaces |
| 65 typedef DWORD(WINAPI* WlanEnumInterfacesFunction)( | 37 typedef DWORD(WINAPI* WlanEnumInterfacesFunction)( |
| 66 HANDLE hClientHandle, | 38 HANDLE hClientHandle, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 77 PVOID pReserved, | 49 PVOID pReserved, |
| 78 PWLAN_BSS_LIST* ppWlanBssList); | 50 PWLAN_BSS_LIST* ppWlanBssList); |
| 79 | 51 |
| 80 // WlanFreeMemory | 52 // WlanFreeMemory |
| 81 typedef VOID(WINAPI* WlanFreeMemoryFunction)(PVOID pMemory); | 53 typedef VOID(WINAPI* WlanFreeMemoryFunction)(PVOID pMemory); |
| 82 | 54 |
| 83 // WlanCloseHandle | 55 // WlanCloseHandle |
| 84 typedef DWORD(WINAPI* WlanCloseHandleFunction)(HANDLE hClientHandle, | 56 typedef DWORD(WINAPI* WlanCloseHandleFunction)(HANDLE hClientHandle, |
| 85 PVOID pReserved); | 57 PVOID pReserved); |
| 86 | 58 |
| 87 // Local classes and functions | 59 // Extracts data for an access point and converts to AccessPointData. |
| 60 AccessPointData GetNetworkData(const WLAN_BSS_ENTRY& bss_entry) { |
| 61 AccessPointData access_point_data; |
| 62 // Currently we get only MAC address, signal strength and SSID. |
| 63 access_point_data.mac_address = MacAddressAsString16(bss_entry.dot11Bssid); |
| 64 access_point_data.radio_signal_strength = bss_entry.lRssi; |
| 65 // bss_entry.dot11Ssid.ucSSID is not null-terminated. |
| 66 base::UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry.dot11Ssid.ucSSID), |
| 67 static_cast<ULONG>(bss_entry.dot11Ssid.uSSIDLength), |
| 68 &access_point_data.ssid); |
| 69 |
| 70 // TODO(steveblock): Is it possible to get the following? |
| 71 // access_point_data.signal_to_noise |
| 72 // access_point_data.age |
| 73 // access_point_data.channel |
| 74 return access_point_data; |
| 75 } |
| 76 |
| 77 // This class encapsulates loading and interacting with wlan_api.dll, which can |
| 78 // not be loaded statically because it's not available in Server 2008 R2, where |
| 79 // it must be installed explicitly by the user if and when they wants to use the |
| 80 // Wireless interface. |
| 81 // https://www.bonusbits.com/wiki/KB:Wlanapi.dll_missing_on_Windows_Server_2008_
R2 |
| 88 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface { | 82 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface { |
| 89 public: | 83 public: |
| 90 // Factory function. Will return NULL if this API is unavailable. | |
| 91 static std::unique_ptr<WindowsWlanApi> Create(); | 84 static std::unique_ptr<WindowsWlanApi> Create(); |
| 92 | 85 |
| 93 // Takes ownership of the library handle. | 86 // Takes ownership of the library handle. |
| 94 explicit WindowsWlanApi(HINSTANCE library); | 87 explicit WindowsWlanApi(HINSTANCE library); |
| 95 ~WindowsWlanApi() override; | 88 ~WindowsWlanApi() override; |
| 96 | 89 |
| 97 // WlanApiInterface | 90 // WlanApiInterface implementation |
| 98 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; | 91 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; |
| 99 | 92 |
| 100 private: | 93 private: |
| 101 // Loads the required functions from the DLL. | |
| 102 void GetWLANFunctions(HINSTANCE wlan_library); | |
| 103 int GetInterfaceDataWLAN(HANDLE wlan_handle, | |
| 104 const GUID& interface_id, | |
| 105 WifiData::AccessPointDataSet* data); | |
| 106 | |
| 107 // Logs number of detected wlan interfaces. | 94 // Logs number of detected wlan interfaces. |
| 108 static void LogWlanInterfaceCount(int count); | 95 static void LogWlanInterfaceCount(int count); |
| 109 | 96 |
| 97 bool GetInterfaceDataWLAN(HANDLE wlan_handle, |
| 98 const GUID& interface_id, |
| 99 WifiData::AccessPointDataSet* data); |
| 110 // Handle to the wlanapi.dll library. | 100 // Handle to the wlanapi.dll library. |
| 111 HINSTANCE library_; | 101 HINSTANCE library_; |
| 112 | 102 |
| 113 // Function pointers for WLAN | 103 // Function pointers for WLAN |
| 114 WlanOpenHandleFunction WlanOpenHandle_function_; | 104 WlanOpenHandleFunction WlanOpenHandle_function_; |
| 115 WlanEnumInterfacesFunction WlanEnumInterfaces_function_; | 105 WlanEnumInterfacesFunction WlanEnumInterfaces_function_; |
| 116 WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_; | 106 WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_; |
| 117 WlanFreeMemoryFunction WlanFreeMemory_function_; | 107 WlanFreeMemoryFunction WlanFreeMemory_function_; |
| 118 WlanCloseHandleFunction WlanCloseHandle_function_; | 108 WlanCloseHandleFunction WlanCloseHandle_function_; |
| 119 }; | 109 }; |
| 120 | 110 |
| 121 class WindowsNdisApi : public WifiDataProviderCommon::WlanApiInterface { | 111 // static |
| 122 public: | 112 std::unique_ptr<WindowsWlanApi> WindowsWlanApi::Create() { |
| 123 static std::unique_ptr<WindowsNdisApi> Create(); | 113 // Use an absolute path to load the DLL to avoid DLL preloading attacks. |
| 124 | 114 static const wchar_t* const kDLL = L"%WINDIR%\\system32\\wlanapi.dll"; |
| 125 // Swaps in content of the vector passed | 115 wchar_t path[MAX_PATH] = {0}; |
| 126 explicit WindowsNdisApi(std::vector<base::string16>* interface_service_names); | 116 ExpandEnvironmentStrings(kDLL, path, arraysize(path)); |
| 127 ~WindowsNdisApi() override; | 117 HINSTANCE library = LoadLibraryEx(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); |
| 128 | 118 if (!library) |
| 129 // WlanApiInterface | 119 return nullptr; |
| 130 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; | 120 return base::MakeUnique<WindowsWlanApi>(library); |
| 131 | |
| 132 private: | |
| 133 static bool GetInterfacesNDIS( | |
| 134 std::vector<base::string16>* interface_service_names_out); | |
| 135 bool GetInterfaceDataNDIS(HANDLE adapter_handle, | |
| 136 WifiData::AccessPointDataSet* data); | |
| 137 // NDIS variables. | |
| 138 std::vector<base::string16> interface_service_names_; | |
| 139 | |
| 140 // Remembers scan result buffer size across calls. | |
| 141 int oid_buffer_size_; | |
| 142 }; | |
| 143 | |
| 144 // Extracts data for an access point and converts to Gears format. | |
| 145 bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry, | |
| 146 AccessPointData* access_point_data); | |
| 147 bool UndefineDosDevice(const base::string16& device_name); | |
| 148 bool DefineDosDeviceIfNotExists(const base::string16& device_name); | |
| 149 HANDLE GetFileHandle(const base::string16& device_name); | |
| 150 // Makes the OID query and returns a Windows API error code. | |
| 151 int PerformQuery(HANDLE adapter_handle, | |
| 152 BYTE* buffer, | |
| 153 DWORD buffer_size, | |
| 154 DWORD* bytes_out); | |
| 155 bool ResizeBuffer(int requested_size, | |
| 156 std::unique_ptr<BYTE, base::FreeDeleter>* buffer); | |
| 157 // Gets the system directory and appends a trailing slash if not already | |
| 158 // present. | |
| 159 bool GetSystemDirectory(base::string16* path); | |
| 160 } // anonymous namespace | |
| 161 | |
| 162 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { | |
| 163 return new WifiDataProviderWin(); | |
| 164 } | 121 } |
| 165 | 122 |
| 166 WifiDataProviderWin::WifiDataProviderWin() {} | 123 WindowsWlanApi::WindowsWlanApi(HINSTANCE library) : library_(library) { |
| 124 DCHECK(library_); |
| 125 // Extract all methods from |library_|. |
| 126 WlanOpenHandle_function_ = reinterpret_cast<WlanOpenHandleFunction>( |
| 127 GetProcAddress(library_, "WlanOpenHandle")); |
| 128 WlanEnumInterfaces_function_ = reinterpret_cast<WlanEnumInterfacesFunction>( |
| 129 GetProcAddress(library_, "WlanEnumInterfaces")); |
| 130 WlanGetNetworkBssList_function_ = |
| 131 reinterpret_cast<WlanGetNetworkBssListFunction>( |
| 132 GetProcAddress(library_, "WlanGetNetworkBssList")); |
| 133 WlanFreeMemory_function_ = reinterpret_cast<WlanFreeMemoryFunction>( |
| 134 GetProcAddress(library_, "WlanFreeMemory")); |
| 135 WlanCloseHandle_function_ = reinterpret_cast<WlanCloseHandleFunction>( |
| 136 GetProcAddress(library_, "WlanCloseHandle")); |
| 167 | 137 |
| 168 WifiDataProviderWin::~WifiDataProviderWin() {} | 138 DCHECK(WlanOpenHandle_function_ && WlanEnumInterfaces_function_ && |
| 169 | 139 WlanGetNetworkBssList_function_ && WlanFreeMemory_function_ && |
| 170 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> | 140 WlanCloseHandle_function_); |
| 171 WifiDataProviderWin::CreateWlanApi() { | |
| 172 // Use the WLAN interface if we're on Vista and if it's available. Otherwise, | |
| 173 // use NDIS. | |
| 174 std::unique_ptr<WlanApiInterface> api = WindowsWlanApi::Create(); | |
| 175 if (api) | |
| 176 return api; | |
| 177 return WindowsNdisApi::Create(); | |
| 178 } | |
| 179 | |
| 180 std::unique_ptr<WifiPollingPolicy> WifiDataProviderWin::CreatePollingPolicy() { | |
| 181 return base::MakeUnique<GenericWifiPollingPolicy< | |
| 182 kDefaultPollingInterval, kNoChangePollingInterval, | |
| 183 kTwoNoChangePollingInterval, kNoWifiPollingIntervalMilliseconds>>(); | |
| 184 } | |
| 185 | |
| 186 // Local classes and functions | |
| 187 namespace { | |
| 188 | |
| 189 // WindowsWlanApi | |
| 190 WindowsWlanApi::WindowsWlanApi(HINSTANCE library) : library_(library) { | |
| 191 GetWLANFunctions(library_); | |
| 192 } | 141 } |
| 193 | 142 |
| 194 WindowsWlanApi::~WindowsWlanApi() { | 143 WindowsWlanApi::~WindowsWlanApi() { |
| 195 FreeLibrary(library_); | 144 FreeLibrary(library_); |
| 196 } | 145 } |
| 197 | 146 |
| 198 std::unique_ptr<WindowsWlanApi> WindowsWlanApi::Create() { | 147 // static |
| 199 if (base::win::GetVersion() < base::win::VERSION_VISTA) | |
| 200 return nullptr; | |
| 201 // We use an absolute path to load the DLL to avoid DLL preloading attacks. | |
| 202 base::string16 system_directory; | |
| 203 if (!GetSystemDirectory(&system_directory)) | |
| 204 return nullptr; | |
| 205 DCHECK(!system_directory.empty()); | |
| 206 base::string16 dll_path = system_directory + L"wlanapi.dll"; | |
| 207 HINSTANCE library = | |
| 208 LoadLibraryEx(dll_path.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH); | |
| 209 if (!library) | |
| 210 return nullptr; | |
| 211 return base::MakeUnique<WindowsWlanApi>(library); | |
| 212 } | |
| 213 | |
| 214 void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library) { | |
| 215 DCHECK(wlan_library); | |
| 216 WlanOpenHandle_function_ = reinterpret_cast<WlanOpenHandleFunction>( | |
| 217 GetProcAddress(wlan_library, "WlanOpenHandle")); | |
| 218 WlanEnumInterfaces_function_ = reinterpret_cast<WlanEnumInterfacesFunction>( | |
| 219 GetProcAddress(wlan_library, "WlanEnumInterfaces")); | |
| 220 WlanGetNetworkBssList_function_ = | |
| 221 reinterpret_cast<WlanGetNetworkBssListFunction>( | |
| 222 GetProcAddress(wlan_library, "WlanGetNetworkBssList")); | |
| 223 WlanFreeMemory_function_ = reinterpret_cast<WlanFreeMemoryFunction>( | |
| 224 GetProcAddress(wlan_library, "WlanFreeMemory")); | |
| 225 WlanCloseHandle_function_ = reinterpret_cast<WlanCloseHandleFunction>( | |
| 226 GetProcAddress(wlan_library, "WlanCloseHandle")); | |
| 227 DCHECK(WlanOpenHandle_function_ && WlanEnumInterfaces_function_ && | |
| 228 WlanGetNetworkBssList_function_ && WlanFreeMemory_function_ && | |
| 229 WlanCloseHandle_function_); | |
| 230 } | |
| 231 | |
| 232 void WindowsWlanApi::LogWlanInterfaceCount(int count) { | 148 void WindowsWlanApi::LogWlanInterfaceCount(int count) { |
| 233 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Wifi.InterfaceCount", count, 1, 5, 6); | 149 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Wifi.InterfaceCount", count, 1 /* min */, |
| 150 5 /* max */, 6 /* bucket_count */); |
| 234 } | 151 } |
| 235 | 152 |
| 236 bool WindowsWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { | 153 bool WindowsWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { |
| 237 DCHECK(data); | 154 DCHECK(data); |
| 238 | 155 |
| 239 // Get the handle to the WLAN API. | |
| 240 DWORD negotiated_version; | 156 DWORD negotiated_version; |
| 241 HANDLE wlan_handle = NULL; | 157 HANDLE wlan_handle = nullptr; |
| 242 // We could be executing on either Windows XP or Windows Vista, so use the | 158 // Highest WLAN API version supported by the client; pass the lowest. It seems |
| 243 // lower version of the client WLAN API. It seems that the negotiated version | 159 // that the negotiated version is the Vista version (the highest) irrespective |
| 244 // is the Vista version irrespective of what we pass! | 160 // of what we pass! |
| 245 static const int kXpWlanClientVersion = 1; | 161 static const int kXpWlanClientVersion = 1; |
| 246 if ((*WlanOpenHandle_function_)(kXpWlanClientVersion, NULL, | 162 if ((*WlanOpenHandle_function_)(kXpWlanClientVersion, NULL, |
| 247 &negotiated_version, | 163 &negotiated_version, |
| 248 &wlan_handle) != ERROR_SUCCESS) { | 164 &wlan_handle) != ERROR_SUCCESS) { |
| 249 LogWlanInterfaceCount(0); | 165 LogWlanInterfaceCount(0); |
| 250 return false; | 166 return false; |
| 251 } | 167 } |
| 252 DCHECK(wlan_handle); | 168 DCHECK(wlan_handle); |
| 253 | 169 |
| 254 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list. | 170 // Get the list of interfaces. WlanEnumInterfaces allocates |interface_list|. |
| 255 WLAN_INTERFACE_INFO_LIST* interface_list = NULL; | 171 WLAN_INTERFACE_INFO_LIST* interface_list = nullptr; |
| 256 if ((*WlanEnumInterfaces_function_)(wlan_handle, NULL, &interface_list) != | 172 if ((*WlanEnumInterfaces_function_)(wlan_handle, NULL, &interface_list) != |
| 257 ERROR_SUCCESS) { | 173 ERROR_SUCCESS) { |
| 258 LogWlanInterfaceCount(0); | 174 LogWlanInterfaceCount(0); |
| 259 return false; | 175 return false; |
| 260 } | 176 } |
| 261 DCHECK(interface_list); | 177 DCHECK(interface_list); |
| 262 | 178 |
| 263 LogWlanInterfaceCount(interface_list->dwNumberOfItems); | 179 LogWlanInterfaceCount(interface_list->dwNumberOfItems); |
| 264 | 180 |
| 265 // Go through the list of interfaces and get the data for each. | 181 // Go through the list of interfaces and get the data for each. |
| 266 for (int i = 0; i < static_cast<int>(interface_list->dwNumberOfItems); ++i) { | 182 for (size_t i = 0; i < interface_list->dwNumberOfItems; ++i) { |
| 183 const WLAN_INTERFACE_INFO interface_info = interface_list->InterfaceInfo[i]; |
| 184 |
| 267 // Skip any interface that is midway through association; the | 185 // Skip any interface that is midway through association; the |
| 268 // WlanGetNetworkBssList function call is known to hang indefinitely | 186 // WlanGetNetworkBssList function call is known to hang indefinitely |
| 269 // when it's in this state. http://crbug.com/39300 | 187 // when it's in this state. https://crbug.com/39300 |
| 270 if (interface_list->InterfaceInfo[i].isState == | 188 if (interface_info.isState == wlan_interface_state_associating) { |
| 271 wlan_interface_state_associating) { | 189 DLOG(WARNING) << "Skipping wifi scan on adapter " << i << " (" |
| 272 LOG(WARNING) << "Skipping wifi scan on adapter " << i << " (" | 190 << interface_info.strInterfaceDescription |
| 273 << interface_list->InterfaceInfo[i].strInterfaceDescription | 191 << ") in 'associating' state. Repeated occurrences " |
| 274 << ") in 'associating' state. Repeated occurrences " | 192 "indicates a non-responding adapter."; |
| 275 "indicates a non-responding adapter."; | |
| 276 continue; | 193 continue; |
| 277 } | 194 } |
| 278 GetInterfaceDataWLAN(wlan_handle, | 195 GetInterfaceDataWLAN(wlan_handle, interface_info.InterfaceGuid, data); |
| 279 interface_list->InterfaceInfo[i].InterfaceGuid, data); | |
| 280 } | 196 } |
| 281 | 197 |
| 282 // Free interface_list. | |
| 283 (*WlanFreeMemory_function_)(interface_list); | 198 (*WlanFreeMemory_function_)(interface_list); |
| 284 | 199 |
| 285 // Close the handle. | 200 return (*WlanCloseHandle_function_)(wlan_handle, NULL) == ERROR_SUCCESS; |
| 286 if ((*WlanCloseHandle_function_)(wlan_handle, NULL) != ERROR_SUCCESS) { | |
| 287 return false; | |
| 288 } | |
| 289 | |
| 290 return true; | |
| 291 } | 201 } |
| 292 | 202 |
| 293 // Appends the data for a single interface to the data vector. Returns the | 203 // Appends the data for a single interface to |data|. Returns false for error. |
| 294 // number of access points found, or -1 on error. | 204 bool WindowsWlanApi::GetInterfaceDataWLAN(const HANDLE wlan_handle, |
| 295 int WindowsWlanApi::GetInterfaceDataWLAN(const HANDLE wlan_handle, | 205 const GUID& interface_id, |
| 296 const GUID& interface_id, | 206 WifiData::AccessPointDataSet* data) { |
| 297 WifiData::AccessPointDataSet* data) { | 207 base::ElapsedTimer wlan_get_network_list_timer; |
| 298 DCHECK(data); | 208 // WlanGetNetworkBssList allocates |bss_list|. |
| 299 | 209 WLAN_BSS_LIST* bss_list = nullptr; |
| 300 const base::TimeTicks start_time = base::TimeTicks::Now(); | |
| 301 | |
| 302 // WlanGetNetworkBssList allocates bss_list. | |
| 303 WLAN_BSS_LIST* bss_list = NULL; | |
| 304 if ((*WlanGetNetworkBssList_function_)(wlan_handle, &interface_id, | 210 if ((*WlanGetNetworkBssList_function_)(wlan_handle, &interface_id, |
| 305 NULL, // Use all SSIDs. | 211 NULL, // Use all SSIDs. |
| 306 dot11_BSS_type_any, | 212 dot11_BSS_type_any, |
| 307 false, // bSecurityEnabled - unused | 213 false, // bSecurityEnabled - unused |
| 308 NULL, // reserved | 214 NULL, // reserved |
| 309 &bss_list) != ERROR_SUCCESS) { | 215 &bss_list) != ERROR_SUCCESS) { |
| 310 return -1; | 216 return false; |
| 311 } | 217 } |
| 312 // According to http://www.attnetclient.com/kb/questions.php?questionid=75 | 218 // WlanGetNetworkBssList() can return success without filling |bss_list|. |
| 313 // WlanGetNetworkBssList can sometimes return success, but leave the bss | |
| 314 // list as NULL. | |
| 315 if (!bss_list) | 219 if (!bss_list) |
| 316 return -1; | 220 return false; |
| 317 | 221 |
| 318 const base::TimeDelta duration = base::TimeTicks::Now() - start_time; | 222 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Wifi.ScanLatency", |
| 319 | 223 wlan_get_network_list_timer.Elapsed(), |
| 320 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Wifi.ScanLatency", duration, | |
| 321 base::TimeDelta::FromMilliseconds(1), | 224 base::TimeDelta::FromMilliseconds(1), |
| 322 base::TimeDelta::FromMinutes(1), 100); | 225 base::TimeDelta::FromMinutes(1), 100); |
| 323 | 226 |
| 324 int found = 0; | 227 for (size_t i = 0; i < bss_list->dwNumberOfItems; ++i) |
| 325 for (int i = 0; i < static_cast<int>(bss_list->dwNumberOfItems); ++i) { | 228 data->insert(GetNetworkData(bss_list->wlanBssEntries[i])); |
| 326 AccessPointData access_point_data; | |
| 327 if (GetNetworkData(bss_list->wlanBssEntries[i], &access_point_data)) { | |
| 328 ++found; | |
| 329 data->insert(access_point_data); | |
| 330 } | |
| 331 } | |
| 332 | 229 |
| 333 (*WlanFreeMemory_function_)(bss_list); | 230 (*WlanFreeMemory_function_)(bss_list); |
| 334 | 231 |
| 335 return found; | |
| 336 } | |
| 337 | |
| 338 // WindowsNdisApi | |
| 339 WindowsNdisApi::WindowsNdisApi( | |
| 340 std::vector<base::string16>* interface_service_names) | |
| 341 : oid_buffer_size_(kInitialBufferSize) { | |
| 342 DCHECK(!interface_service_names->empty()); | |
| 343 interface_service_names_.swap(*interface_service_names); | |
| 344 } | |
| 345 | |
| 346 WindowsNdisApi::~WindowsNdisApi() {} | |
| 347 | |
| 348 std::unique_ptr<WindowsNdisApi> WindowsNdisApi::Create() { | |
| 349 std::vector<base::string16> interface_service_names; | |
| 350 if (GetInterfacesNDIS(&interface_service_names)) | |
| 351 return base::MakeUnique<WindowsNdisApi>(&interface_service_names); | |
| 352 return nullptr; | |
| 353 } | |
| 354 | |
| 355 bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { | |
| 356 DCHECK(data); | |
| 357 int interfaces_failed = 0; | |
| 358 int interfaces_succeeded = 0; | |
| 359 | |
| 360 for (int i = 0; i < static_cast<int>(interface_service_names_.size()); ++i) { | |
| 361 // First, check that we have a DOS device for this adapter. | |
| 362 if (!DefineDosDeviceIfNotExists(interface_service_names_[i])) | |
| 363 continue; | |
| 364 | |
| 365 // Get the handle to the device. This will fail if the named device is not | |
| 366 // valid. | |
| 367 HANDLE adapter_handle = GetFileHandle(interface_service_names_[i]); | |
| 368 if (adapter_handle == INVALID_HANDLE_VALUE) | |
| 369 continue; | |
| 370 | |
| 371 // Get the data. | |
| 372 if (GetInterfaceDataNDIS(adapter_handle, data)) | |
| 373 ++interfaces_succeeded; | |
| 374 else | |
| 375 ++interfaces_failed; | |
| 376 | |
| 377 // Clean up. | |
| 378 CloseHandle(adapter_handle); | |
| 379 UndefineDosDevice(interface_service_names_[i]); | |
| 380 } | |
| 381 | |
| 382 // Return true if at least one interface succeeded, or at the very least none | |
| 383 // failed. | |
| 384 return interfaces_succeeded > 0 || interfaces_failed == 0; | |
| 385 } | |
| 386 | |
| 387 bool WindowsNdisApi::GetInterfacesNDIS( | |
| 388 std::vector<base::string16>* interface_service_names_out) { | |
| 389 HKEY network_cards_key = NULL; | |
| 390 if (RegOpenKeyEx( | |
| 391 HKEY_LOCAL_MACHINE, | |
| 392 L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards", 0, | |
| 393 KEY_READ, &network_cards_key) != ERROR_SUCCESS) { | |
| 394 return false; | |
| 395 } | |
| 396 DCHECK(network_cards_key); | |
| 397 | |
| 398 for (int i = 0;; ++i) { | |
| 399 TCHAR name[kStringLength]; | |
| 400 DWORD name_size = kStringLength; | |
| 401 FILETIME time; | |
| 402 if (RegEnumKeyEx(network_cards_key, i, name, &name_size, NULL, NULL, NULL, | |
| 403 &time) != ERROR_SUCCESS) { | |
| 404 break; | |
| 405 } | |
| 406 HKEY hardware_key = NULL; | |
| 407 if (RegOpenKeyEx(network_cards_key, name, 0, KEY_READ, &hardware_key) != | |
| 408 ERROR_SUCCESS) { | |
| 409 break; | |
| 410 } | |
| 411 DCHECK(hardware_key); | |
| 412 | |
| 413 TCHAR service_name[kStringLength]; | |
| 414 DWORD service_name_size = kStringLength; | |
| 415 DWORD type = 0; | |
| 416 if (RegQueryValueEx(hardware_key, L"ServiceName", NULL, &type, | |
| 417 reinterpret_cast<LPBYTE>(service_name), | |
| 418 &service_name_size) == ERROR_SUCCESS) { | |
| 419 interface_service_names_out->push_back(service_name); | |
| 420 } | |
| 421 RegCloseKey(hardware_key); | |
| 422 } | |
| 423 | |
| 424 RegCloseKey(network_cards_key); | |
| 425 return true; | |
| 426 } | |
| 427 | |
| 428 bool WindowsNdisApi::GetInterfaceDataNDIS(HANDLE adapter_handle, | |
| 429 WifiData::AccessPointDataSet* data) { | |
| 430 DCHECK(data); | |
| 431 | |
| 432 std::unique_ptr<BYTE, base::FreeDeleter> buffer( | |
| 433 static_cast<BYTE*>(malloc(oid_buffer_size_))); | |
| 434 if (!buffer) | |
| 435 return false; | |
| 436 | |
| 437 DWORD bytes_out; | |
| 438 int result; | |
| 439 | |
| 440 while (true) { | |
| 441 bytes_out = 0; | |
| 442 result = PerformQuery(adapter_handle, buffer.get(), oid_buffer_size_, | |
| 443 &bytes_out); | |
| 444 if (result == ERROR_GEN_FAILURE || // Returned by some Intel cards. | |
| 445 result == ERROR_INSUFFICIENT_BUFFER || result == ERROR_MORE_DATA || | |
| 446 result == NDIS_STATUS_INVALID_LENGTH || | |
| 447 result == NDIS_STATUS_BUFFER_TOO_SHORT) { | |
| 448 // The buffer we supplied is too small, so increase it. bytes_out should | |
| 449 // provide the required buffer size, but this is not always the case. | |
| 450 if (bytes_out > static_cast<DWORD>(oid_buffer_size_)) | |
| 451 oid_buffer_size_ = bytes_out; | |
| 452 else | |
| 453 oid_buffer_size_ *= 2; | |
| 454 | |
| 455 if (!ResizeBuffer(oid_buffer_size_, &buffer)) { | |
| 456 oid_buffer_size_ = kInitialBufferSize; // Reset for next time. | |
| 457 return false; | |
| 458 } | |
| 459 } else { | |
| 460 // The buffer is not too small. | |
| 461 break; | |
| 462 } | |
| 463 } | |
| 464 DCHECK(buffer.get()); | |
| 465 | |
| 466 if (result == ERROR_SUCCESS) { | |
| 467 NDIS_802_11_BSSID_LIST* bssid_list = | |
| 468 reinterpret_cast<NDIS_802_11_BSSID_LIST*>(buffer.get()); | |
| 469 GetDataFromBssIdList(*bssid_list, oid_buffer_size_, data); | |
| 470 } | |
| 471 | |
| 472 return true; | 232 return true; |
| 473 } | 233 } |
| 474 | 234 |
| 475 bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry, | 235 } // anonymous namespace |
| 476 AccessPointData* access_point_data) { | 236 |
| 477 // Currently we get only MAC address, signal strength and SSID. | 237 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { |
| 478 DCHECK(access_point_data); | 238 return new WifiDataProviderWin(); |
| 479 access_point_data->mac_address = MacAddressAsString16(bss_entry.dot11Bssid); | |
| 480 access_point_data->radio_signal_strength = bss_entry.lRssi; | |
| 481 // bss_entry.dot11Ssid.ucSSID is not null-terminated. | |
| 482 base::UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry.dot11Ssid.ucSSID), | |
| 483 static_cast<ULONG>(bss_entry.dot11Ssid.uSSIDLength), | |
| 484 &access_point_data->ssid); | |
| 485 // TODO(steveblock): Is it possible to get the following? | |
| 486 // access_point_data->signal_to_noise | |
| 487 // access_point_data->age | |
| 488 // access_point_data->channel | |
| 489 return true; | |
| 490 } | 239 } |
| 491 | 240 |
| 492 bool UndefineDosDevice(const base::string16& device_name) { | 241 WifiDataProviderWin::WifiDataProviderWin() = default; |
| 493 // We remove only the mapping we use, that is \Device\<device_name>. | 242 |
| 494 base::string16 target_path = L"\\Device\\" + device_name; | 243 WifiDataProviderWin::~WifiDataProviderWin() = default; |
| 495 return DefineDosDevice(DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | | 244 |
| 496 DDD_EXACT_MATCH_ON_REMOVE, | 245 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> |
| 497 device_name.c_str(), target_path.c_str()) == TRUE; | 246 WifiDataProviderWin::CreateWlanApi() { |
| 247 return WindowsWlanApi::Create(); |
| 498 } | 248 } |
| 499 | 249 |
| 500 bool DefineDosDeviceIfNotExists(const base::string16& device_name) { | 250 std::unique_ptr<WifiPollingPolicy> WifiDataProviderWin::CreatePollingPolicy() { |
| 501 // We create a DOS device name for the device at \Device\<device_name>. | 251 return base::MakeUnique<GenericWifiPollingPolicy< |
| 502 base::string16 target_path = L"\\Device\\" + device_name; | 252 kDefaultPollingIntervalMs, kNoChangePollingIntervalMs, |
| 503 | 253 kTwoNoChangePollingIntervalMs, kNoWifiPollingIntervalMs>>(); |
| 504 TCHAR target[kStringLength]; | |
| 505 if (QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && | |
| 506 target_path.compare(target) == 0) { | |
| 507 // Device already exists. | |
| 508 return true; | |
| 509 } | |
| 510 | |
| 511 if (GetLastError() != ERROR_FILE_NOT_FOUND) | |
| 512 return false; | |
| 513 | |
| 514 if (!DefineDosDevice(DDD_RAW_TARGET_PATH, device_name.c_str(), | |
| 515 target_path.c_str())) { | |
| 516 return false; | |
| 517 } | |
| 518 | |
| 519 // Check that the device is really there. | |
| 520 return QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 && | |
| 521 target_path.compare(target) == 0; | |
| 522 } | 254 } |
| 523 | 255 |
| 524 HANDLE GetFileHandle(const base::string16& device_name) { | |
| 525 // We access a device with DOS path \Device\<device_name> at | |
| 526 // \\.\<device_name>. | |
| 527 base::string16 formatted_device_name = L"\\\\.\\" + device_name; | |
| 528 | |
| 529 return CreateFile(formatted_device_name.c_str(), GENERIC_READ, | |
| 530 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode | |
| 531 0, // security attributes | |
| 532 OPEN_EXISTING, | |
| 533 0, // flags and attributes | |
| 534 INVALID_HANDLE_VALUE); | |
| 535 } | |
| 536 | |
| 537 int PerformQuery(HANDLE adapter_handle, | |
| 538 BYTE* buffer, | |
| 539 DWORD buffer_size, | |
| 540 DWORD* bytes_out) { | |
| 541 DWORD oid = OID_802_11_BSSID_LIST; | |
| 542 if (!DeviceIoControl(adapter_handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, | |
| 543 sizeof(oid), buffer, buffer_size, bytes_out, NULL)) { | |
| 544 return GetLastError(); | |
| 545 } | |
| 546 return ERROR_SUCCESS; | |
| 547 } | |
| 548 | |
| 549 bool ResizeBuffer(int requested_size, | |
| 550 std::unique_ptr<BYTE, base::FreeDeleter>* buffer) { | |
| 551 DCHECK_GT(requested_size, 0); | |
| 552 DCHECK(buffer); | |
| 553 if (requested_size > kMaximumBufferSize) { | |
| 554 buffer->reset(); | |
| 555 return false; | |
| 556 } | |
| 557 | |
| 558 buffer->reset( | |
| 559 reinterpret_cast<BYTE*>(realloc(buffer->release(), requested_size))); | |
| 560 return buffer != NULL; | |
| 561 } | |
| 562 | |
| 563 bool GetSystemDirectory(base::string16* path) { | |
| 564 DCHECK(path); | |
| 565 // Return value includes terminating NULL. | |
| 566 int buffer_size = ::GetSystemDirectory(NULL, 0); | |
| 567 if (buffer_size == 0) | |
| 568 return false; | |
| 569 std::unique_ptr<base::char16[]> buffer(new base::char16[buffer_size]); | |
| 570 | |
| 571 // Return value excludes terminating NULL. | |
| 572 int characters_written = ::GetSystemDirectory(buffer.get(), buffer_size); | |
| 573 if (characters_written == 0) | |
| 574 return false; | |
| 575 DCHECK_EQ(buffer_size - 1, characters_written); | |
| 576 | |
| 577 path->assign(buffer.get(), characters_written); | |
| 578 | |
| 579 if (*path->rbegin() != L'\\') | |
| 580 path->append(L"\\"); | |
| 581 DCHECK_EQ(L'\\', *path->rbegin()); | |
| 582 return true; | |
| 583 } | |
| 584 } // namespace | |
| 585 | |
| 586 } // namespace device | 256 } // namespace device |
| OLD | NEW |