Chromium Code Reviews| 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 |
| 27 static const int kTwoNoChangePollingIntervalMs = 10 * 60 * 1000; // 10 mins | |
| 28 static const int kNoWifiPollingIntervalMs = 20 * 1000; | |
|
robliao
2017/04/26 00:02:40
// 20s
Accidental removal?
mcasas
2017/04/26 00:36:20
Oh yes, well spotted.
| |
| 51 | 29 |
| 52 // The time periods, in milliseconds, between successive polls of the wifi data. | 30 // Extracts data for an access point and converts to AccessPointData. |
| 53 const int kDefaultPollingInterval = 10000; // 10s | 31 AccessPointData GetNetworkData(const WLAN_BSS_ENTRY& bss_entry) { |
| 54 const int kNoChangePollingInterval = 120000; // 2 mins | 32 AccessPointData access_point_data; |
| 55 const int kTwoNoChangePollingInterval = 600000; // 10 mins | 33 // Currently we get only MAC address, signal strength and SSID. |
| 56 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | 34 access_point_data.mac_address = MacAddressAsString16(bss_entry.dot11Bssid); |
| 35 access_point_data.radio_signal_strength = bss_entry.lRssi; | |
| 36 // bss_entry.dot11Ssid.ucSSID is not null-terminated. | |
| 37 base::UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry.dot11Ssid.ucSSID), | |
| 38 static_cast<ULONG>(bss_entry.dot11Ssid.uSSIDLength), | |
| 39 &access_point_data.ssid); | |
| 40 return access_point_data; | |
|
dougt
2017/04/26 00:15:46
I think you should leave steve's comment block. So
mcasas
2017/04/26 00:36:20
Done.
| |
| 41 } | |
| 57 | 42 |
| 58 // WlanOpenHandle | |
| 59 typedef DWORD(WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion, | |
| 60 PVOID pReserved, | |
| 61 PDWORD pdwNegotiatedVersion, | |
| 62 PHANDLE phClientHandle); | |
| 63 | |
| 64 // WlanEnumInterfaces | |
| 65 typedef DWORD(WINAPI* WlanEnumInterfacesFunction)( | |
| 66 HANDLE hClientHandle, | |
| 67 PVOID pReserved, | |
| 68 PWLAN_INTERFACE_INFO_LIST* ppInterfaceList); | |
| 69 | |
| 70 // WlanGetNetworkBssList | |
| 71 typedef DWORD(WINAPI* WlanGetNetworkBssListFunction)( | |
| 72 HANDLE hClientHandle, | |
| 73 const GUID* pInterfaceGuid, | |
| 74 const PDOT11_SSID pDot11Ssid, | |
| 75 DOT11_BSS_TYPE dot11BssType, | |
| 76 BOOL bSecurityEnabled, | |
| 77 PVOID pReserved, | |
| 78 PWLAN_BSS_LIST* ppWlanBssList); | |
| 79 | |
| 80 // WlanFreeMemory | |
| 81 typedef VOID(WINAPI* WlanFreeMemoryFunction)(PVOID pMemory); | |
| 82 | |
| 83 // WlanCloseHandle | |
| 84 typedef DWORD(WINAPI* WlanCloseHandleFunction)(HANDLE hClientHandle, | |
| 85 PVOID pReserved); | |
| 86 | |
| 87 // Local classes and functions | |
| 88 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface { | 43 class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface { |
| 89 public: | 44 public: |
| 90 // Factory function. Will return NULL if this API is unavailable. | 45 WindowsWlanApi(); |
| 91 static std::unique_ptr<WindowsWlanApi> Create(); | |
| 92 | |
| 93 // Takes ownership of the library handle. | |
| 94 explicit WindowsWlanApi(HINSTANCE library); | |
| 95 ~WindowsWlanApi() override; | 46 ~WindowsWlanApi() override; |
| 96 | 47 |
| 97 // WlanApiInterface | 48 // WlanApiInterface imlpementation |
|
robliao
2017/04/26 00:02:40
Nit: implementation
mcasas
2017/04/26 00:36:20
Done.
| |
| 98 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; | 49 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; |
| 99 | 50 |
| 100 private: | 51 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. | 52 // Logs number of detected wlan interfaces. |
| 108 static void LogWlanInterfaceCount(int count); | 53 static void LogWlanInterfaceCount(int count); |
| 109 | 54 |
| 110 // Handle to the wlanapi.dll library. | 55 bool GetInterfaceDataWLAN(HANDLE wlan_handle, |
| 111 HINSTANCE library_; | 56 const GUID& interface_id, |
| 112 | 57 WifiData::AccessPointDataSet* data); |
| 113 // Function pointers for WLAN | |
| 114 WlanOpenHandleFunction WlanOpenHandle_function_; | |
| 115 WlanEnumInterfacesFunction WlanEnumInterfaces_function_; | |
| 116 WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_; | |
| 117 WlanFreeMemoryFunction WlanFreeMemory_function_; | |
| 118 WlanCloseHandleFunction WlanCloseHandle_function_; | |
| 119 }; | 58 }; |
| 120 | 59 |
| 121 class WindowsNdisApi : public WifiDataProviderCommon::WlanApiInterface { | 60 WindowsWlanApi::WindowsWlanApi() = default; |
| 122 public: | |
| 123 static std::unique_ptr<WindowsNdisApi> Create(); | |
| 124 | 61 |
| 125 // Swaps in content of the vector passed | 62 WindowsWlanApi::~WindowsWlanApi() = default; |
| 126 explicit WindowsNdisApi(std::vector<base::string16>* interface_service_names); | |
| 127 ~WindowsNdisApi() override; | |
| 128 | 63 |
| 129 // WlanApiInterface | 64 // static |
| 130 bool GetAccessPointData(WifiData::AccessPointDataSet* data) override; | |
| 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 } | |
| 165 | |
| 166 WifiDataProviderWin::WifiDataProviderWin() {} | |
| 167 | |
| 168 WifiDataProviderWin::~WifiDataProviderWin() {} | |
| 169 | |
| 170 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> | |
| 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 } | |
| 193 | |
| 194 WindowsWlanApi::~WindowsWlanApi() { | |
| 195 FreeLibrary(library_); | |
| 196 } | |
| 197 | |
| 198 std::unique_ptr<WindowsWlanApi> WindowsWlanApi::Create() { | |
| 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) { | 65 void WindowsWlanApi::LogWlanInterfaceCount(int count) { |
| 233 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Wifi.InterfaceCount", count, 1, 5, 6); | 66 UMA_HISTOGRAM_CUSTOM_COUNTS("Net.Wifi.InterfaceCount", count, 1 /* min */, |
| 67 5 /* max */, 6 /* bucket_count */); | |
| 234 } | 68 } |
| 235 | 69 |
| 236 bool WindowsWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { | 70 bool WindowsWlanApi::GetAccessPointData(WifiData::AccessPointDataSet* data) { |
| 237 DCHECK(data); | 71 DCHECK(data); |
| 238 | 72 |
| 239 // Get the handle to the WLAN API. | |
| 240 DWORD negotiated_version; | 73 DWORD negotiated_version; |
| 241 HANDLE wlan_handle = NULL; | 74 HANDLE wlan_handle = nullptr; |
| 242 // We could be executing on either Windows XP or Windows Vista, so use the | 75 // 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 | 76 // that the negotiated version is the Vista version (the highest) irrespective |
| 244 // is the Vista version irrespective of what we pass! | 77 // of what we pass! |
| 245 static const int kXpWlanClientVersion = 1; | 78 static const int kXpWlanClientVersion = 1; |
|
dougt
2017/04/26 00:15:47
This is pretty interesting, I would have thought y
robliao
2017/04/26 00:29:24
This could have subtle behavior changes in the API
| |
| 246 if ((*WlanOpenHandle_function_)(kXpWlanClientVersion, NULL, | 79 if (WlanOpenHandle(kXpWlanClientVersion, NULL, &negotiated_version, |
| 247 &negotiated_version, | 80 &wlan_handle) != ERROR_SUCCESS) { |
| 248 &wlan_handle) != ERROR_SUCCESS) { | |
| 249 LogWlanInterfaceCount(0); | 81 LogWlanInterfaceCount(0); |
| 250 return false; | 82 return false; |
| 251 } | 83 } |
| 252 DCHECK(wlan_handle); | 84 DCHECK(wlan_handle); |
| 253 | 85 |
| 254 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list. | 86 // Get the list of interfaces. WlanEnumInterfaces allocates |interface_list|. |
| 255 WLAN_INTERFACE_INFO_LIST* interface_list = NULL; | 87 WLAN_INTERFACE_INFO_LIST* interface_list = nullptr; |
| 256 if ((*WlanEnumInterfaces_function_)(wlan_handle, NULL, &interface_list) != | 88 if (WlanEnumInterfaces(wlan_handle, NULL, &interface_list) != ERROR_SUCCESS) { |
| 257 ERROR_SUCCESS) { | |
| 258 LogWlanInterfaceCount(0); | 89 LogWlanInterfaceCount(0); |
| 259 return false; | 90 return false; |
| 260 } | 91 } |
| 261 DCHECK(interface_list); | 92 DCHECK(interface_list); |
| 262 | 93 |
| 263 LogWlanInterfaceCount(interface_list->dwNumberOfItems); | 94 LogWlanInterfaceCount(interface_list->dwNumberOfItems); |
| 264 | 95 |
| 265 // Go through the list of interfaces and get the data for each. | 96 // 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) { | 97 for (int i = 0; i < static_cast<int>(interface_list->dwNumberOfItems); ++i) { |
| 267 // Skip any interface that is midway through association; the | 98 // Skip any interface that is midway through association; the |
| 268 // WlanGetNetworkBssList function call is known to hang indefinitely | 99 // WlanGetNetworkBssList function call is known to hang indefinitely |
| 269 // when it's in this state. http://crbug.com/39300 | 100 // when it's in this state. https://crbug.com/39300 |
| 270 if (interface_list->InterfaceInfo[i].isState == | 101 if (interface_list->InterfaceInfo[i].isState == |
| 271 wlan_interface_state_associating) { | 102 wlan_interface_state_associating) { |
| 272 LOG(WARNING) << "Skipping wifi scan on adapter " << i << " (" | 103 DLOG(WARNING) << "Skipping wifi scan on adapter " << i << " (" |
| 273 << interface_list->InterfaceInfo[i].strInterfaceDescription | 104 << interface_list->InterfaceInfo[i].strInterfaceDescription |
| 274 << ") in 'associating' state. Repeated occurrences " | 105 << ") in 'associating' state. Repeated occurrences " |
| 275 "indicates a non-responding adapter."; | 106 "indicates a non-responding adapter."; |
| 276 continue; | 107 continue; |
| 277 } | 108 } |
| 278 GetInterfaceDataWLAN(wlan_handle, | 109 GetInterfaceDataWLAN(wlan_handle, |
| 279 interface_list->InterfaceInfo[i].InterfaceGuid, data); | 110 interface_list->InterfaceInfo[i].InterfaceGuid, data); |
| 280 } | 111 } |
| 281 | 112 |
| 282 // Free interface_list. | 113 WlanFreeMemory(interface_list); |
| 283 (*WlanFreeMemory_function_)(interface_list); | |
| 284 | 114 |
| 285 // Close the handle. | 115 return WlanCloseHandle(wlan_handle, NULL) == ERROR_SUCCESS; |
| 286 if ((*WlanCloseHandle_function_)(wlan_handle, NULL) != ERROR_SUCCESS) { | 116 } |
| 117 | |
| 118 // Appends the data for a single interface to |data|. Returns false for error. | |
| 119 bool WindowsWlanApi::GetInterfaceDataWLAN(const HANDLE wlan_handle, | |
| 120 const GUID& interface_id, | |
| 121 WifiData::AccessPointDataSet* data) { | |
| 122 base::ElapsedTimer wlan_get_network_list_timer; | |
| 123 // WlanGetNetworkBssList allocates bss_list. | |
| 124 WLAN_BSS_LIST* bss_list = nullptr; | |
| 125 if (WlanGetNetworkBssList(wlan_handle, &interface_id, | |
| 126 NULL, // Use all SSIDs. | |
|
dougt
2017/04/26 00:15:47
do you want to convert these NULLs to nullptr's?
mcasas
2017/04/26 00:36:20
I left the NULLs for API calls, I think Win APIs
a
| |
| 127 dot11_BSS_type_any, | |
| 128 false, // bSecurityEnabled - unused | |
| 129 NULL, // reserved | |
| 130 &bss_list) != ERROR_SUCCESS) { | |
| 287 return false; | 131 return false; |
| 288 } | 132 } |
| 133 // According to http://www.attnetclient.com/kb/questions.php?questionid=75 | |
| 134 // WlanGetNetworkBssList can sometimes return success, but leave the bss | |
| 135 // list as nullptr. | |
| 136 if (!bss_list) | |
| 137 return false; | |
| 138 | |
| 139 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Wifi.ScanLatency", | |
| 140 wlan_get_network_list_timer.Elapsed(), | |
| 141 base::TimeDelta::FromMilliseconds(1), | |
| 142 base::TimeDelta::FromMinutes(1), 100); | |
| 143 | |
| 144 for (int i = 0; i < static_cast<int>(bss_list->dwNumberOfItems); ++i) | |
| 145 data->insert(GetNetworkData(bss_list->wlanBssEntries[i])); | |
| 146 | |
| 147 WlanFreeMemory(bss_list); | |
| 289 | 148 |
| 290 return true; | 149 return true; |
| 291 } | 150 } |
| 292 | 151 |
| 293 // Appends the data for a single interface to the data vector. Returns the | 152 } // anonymous namespace |
| 294 // number of access points found, or -1 on error. | |
| 295 int WindowsWlanApi::GetInterfaceDataWLAN(const HANDLE wlan_handle, | |
| 296 const GUID& interface_id, | |
| 297 WifiData::AccessPointDataSet* data) { | |
| 298 DCHECK(data); | |
| 299 | 153 |
| 300 const base::TimeTicks start_time = base::TimeTicks::Now(); | 154 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { |
| 301 | 155 return new WifiDataProviderWin(); |
| 302 // WlanGetNetworkBssList allocates bss_list. | |
| 303 WLAN_BSS_LIST* bss_list = NULL; | |
| 304 if ((*WlanGetNetworkBssList_function_)(wlan_handle, &interface_id, | |
| 305 NULL, // Use all SSIDs. | |
| 306 dot11_BSS_type_any, | |
| 307 false, // bSecurityEnabled - unused | |
| 308 NULL, // reserved | |
| 309 &bss_list) != ERROR_SUCCESS) { | |
| 310 return -1; | |
| 311 } | |
| 312 // According to http://www.attnetclient.com/kb/questions.php?questionid=75 | |
|
dougt
2017/04/26 00:15:47
Dead link. Nothing in web.archive.org. Just kill t
mcasas
2017/04/26 00:36:20
Done.
| |
| 313 // WlanGetNetworkBssList can sometimes return success, but leave the bss | |
| 314 // list as NULL. | |
| 315 if (!bss_list) | |
| 316 return -1; | |
| 317 | |
| 318 const base::TimeDelta duration = base::TimeTicks::Now() - start_time; | |
| 319 | |
| 320 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Wifi.ScanLatency", duration, | |
| 321 base::TimeDelta::FromMilliseconds(1), | |
| 322 base::TimeDelta::FromMinutes(1), 100); | |
| 323 | |
| 324 int found = 0; | |
| 325 for (int i = 0; i < static_cast<int>(bss_list->dwNumberOfItems); ++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 | |
| 333 (*WlanFreeMemory_function_)(bss_list); | |
| 334 | |
| 335 return found; | |
| 336 } | 156 } |
| 337 | 157 |
| 338 // WindowsNdisApi | 158 WifiDataProviderWin::WifiDataProviderWin() = default; |
| 339 WindowsNdisApi::WindowsNdisApi( | 159 |
| 340 std::vector<base::string16>* interface_service_names) | 160 WifiDataProviderWin::~WifiDataProviderWin() = default; |
| 341 : oid_buffer_size_(kInitialBufferSize) { | 161 |
| 342 DCHECK(!interface_service_names->empty()); | 162 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> |
| 343 interface_service_names_.swap(*interface_service_names); | 163 WifiDataProviderWin::CreateWlanApi() { |
| 164 return base::MakeUnique<WindowsWlanApi>(); | |
| 344 } | 165 } |
| 345 | 166 |
| 346 WindowsNdisApi::~WindowsNdisApi() {} | 167 std::unique_ptr<WifiPollingPolicy> WifiDataProviderWin::CreatePollingPolicy() { |
| 347 | 168 return base::MakeUnique<GenericWifiPollingPolicy< |
| 348 std::unique_ptr<WindowsNdisApi> WindowsNdisApi::Create() { | 169 kDefaultPollingIntervalMs, kNoChangePollingIntervalMs, |
| 349 std::vector<base::string16> interface_service_names; | 170 kTwoNoChangePollingIntervalMs, kNoWifiPollingIntervalMs>>(); |
| 350 if (GetInterfacesNDIS(&interface_service_names)) | |
| 351 return base::MakeUnique<WindowsNdisApi>(&interface_service_names); | |
| 352 return nullptr; | |
| 353 } | 171 } |
| 354 | 172 |
| 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; | |
| 473 } | |
| 474 | |
| 475 bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry, | |
| 476 AccessPointData* access_point_data) { | |
| 477 // Currently we get only MAC address, signal strength and SSID. | |
| 478 DCHECK(access_point_data); | |
| 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 } | |
| 491 | |
| 492 bool UndefineDosDevice(const base::string16& device_name) { | |
| 493 // We remove only the mapping we use, that is \Device\<device_name>. | |
| 494 base::string16 target_path = L"\\Device\\" + device_name; | |
| 495 return DefineDosDevice(DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | | |
| 496 DDD_EXACT_MATCH_ON_REMOVE, | |
| 497 device_name.c_str(), target_path.c_str()) == TRUE; | |
| 498 } | |
| 499 | |
| 500 bool DefineDosDeviceIfNotExists(const base::string16& device_name) { | |
| 501 // We create a DOS device name for the device at \Device\<device_name>. | |
| 502 base::string16 target_path = L"\\Device\\" + device_name; | |
| 503 | |
| 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 } | |
| 523 | |
| 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 | 173 } // namespace device |
| OLD | NEW |