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