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.
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.
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.
17 // The MSDN recommends that one use the WLAN API where available, and WZC
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
24 #include "content/browser/geolocation/wifi_data_provider_win.h"
30 #include "base/metrics/histogram.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "base/win/windows_version.h"
33 #include "content/browser/geolocation/wifi_data_provider_common.h"
34 #include "content/browser/geolocation/wifi_data_provider_common_win.h"
36 // Taken from ndis.h for WinCE.
37 #define NDIS_STATUS_INVALID_LENGTH ((NDIS_STATUS)0xC0010014L)
38 #define NDIS_STATUS_BUFFER_TOO_SHORT ((NDIS_STATUS)0xC0010016L)
42 // The limits on the size of the buffer used for the OID query.
43 const int kInitialBufferSize
= 2 << 12; // Good for about 50 APs.
44 const int kMaximumBufferSize
= 2 << 20; // 2MB
46 // Length for generic string buffers passed to Win32 APIs.
47 const int kStringLength
= 512;
49 // The time periods, in milliseconds, between successive polls of the wifi data.
50 const int kDefaultPollingInterval
= 10000; // 10s
51 const int kNoChangePollingInterval
= 120000; // 2 mins
52 const int kTwoNoChangePollingInterval
= 600000; // 10 mins
53 const int kNoWifiPollingIntervalMilliseconds
= 20 * 1000; // 20s
56 typedef DWORD (WINAPI
* WlanOpenHandleFunction
)(DWORD dwClientVersion
,
58 PDWORD pdwNegotiatedVersion
,
59 PHANDLE phClientHandle
);
62 typedef DWORD (WINAPI
* WlanEnumInterfacesFunction
)(
65 PWLAN_INTERFACE_INFO_LIST
* ppInterfaceList
);
67 // WlanGetNetworkBssList
68 typedef DWORD (WINAPI
* WlanGetNetworkBssListFunction
)(
70 const GUID
* pInterfaceGuid
,
71 const PDOT11_SSID pDot11Ssid
,
72 DOT11_BSS_TYPE dot11BssType
,
73 BOOL bSecurityEnabled
,
75 PWLAN_BSS_LIST
* ppWlanBssList
79 typedef VOID (WINAPI
* WlanFreeMemoryFunction
)(PVOID pMemory
);
82 typedef DWORD (WINAPI
* WlanCloseHandleFunction
)(HANDLE hClientHandle
,
86 // Local classes and functions
87 class WindowsWlanApi
: public WifiDataProviderCommon::WlanApiInterface
{
89 virtual ~WindowsWlanApi();
90 // Factory function. Will return NULL if this API is unavailable.
91 static WindowsWlanApi
* Create();
94 virtual bool GetAccessPointData(WifiData::AccessPointDataSet
* data
);
97 // Takes ownership of the library handle.
98 explicit WindowsWlanApi(HINSTANCE library
);
100 // Loads the required functions from the DLL.
101 void GetWLANFunctions(HINSTANCE wlan_library
);
102 int GetInterfaceDataWLAN(HANDLE wlan_handle
,
103 const GUID
& interface_id
,
104 WifiData::AccessPointDataSet
* data
);
106 // Logs number of detected wlan interfaces.
107 static void LogWlanInterfaceCount(int count
);
109 // Handle to the wlanapi.dll library.
112 // Function pointers for WLAN
113 WlanOpenHandleFunction WlanOpenHandle_function_
;
114 WlanEnumInterfacesFunction WlanEnumInterfaces_function_
;
115 WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_
;
116 WlanFreeMemoryFunction WlanFreeMemory_function_
;
117 WlanCloseHandleFunction WlanCloseHandle_function_
;
120 class WindowsNdisApi
: public WifiDataProviderCommon::WlanApiInterface
{
122 virtual ~WindowsNdisApi();
123 static WindowsNdisApi
* Create();
126 virtual bool GetAccessPointData(WifiData::AccessPointDataSet
* data
);
129 static bool GetInterfacesNDIS(
130 std::vector
<base::string16
>* interface_service_names_out
);
132 // Swaps in content of the vector passed
133 explicit WindowsNdisApi(std::vector
<base::string16
>* interface_service_names
);
135 bool GetInterfaceDataNDIS(HANDLE adapter_handle
,
136 WifiData::AccessPointDataSet
* data
);
138 std::vector
<base::string16
> interface_service_names_
;
140 // Remembers scan result buffer size across calls.
141 int oid_buffer_size_
;
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 Win32 error code.
151 int PerformQuery(HANDLE adapter_handle
,
155 bool ResizeBuffer(int requested_size
,
156 scoped_ptr
<BYTE
, base::FreeDeleter
>* buffer
);
157 // Gets the system directory and appends a trailing slash if not already
159 bool GetSystemDirectory(base::string16
* path
);
162 WifiDataProviderImplBase
* WifiDataProvider::DefaultFactoryFunction() {
163 return new Win32WifiDataProvider();
166 Win32WifiDataProvider::Win32WifiDataProvider() {
169 Win32WifiDataProvider::~Win32WifiDataProvider() {
172 WifiDataProviderCommon::WlanApiInterface
* Win32WifiDataProvider::NewWlanApi() {
173 // Use the WLAN interface if we're on Vista and if it's available. Otherwise,
175 WlanApiInterface
* api
= WindowsWlanApi::Create();
179 return WindowsNdisApi::Create();
182 WifiPollingPolicy
* Win32WifiDataProvider::NewPollingPolicy() {
183 return new GenericWifiPollingPolicy
<kDefaultPollingInterval
,
184 kNoChangePollingInterval
,
185 kTwoNoChangePollingInterval
,
186 kNoWifiPollingIntervalMilliseconds
>;
189 // Local classes and functions
193 WindowsWlanApi::WindowsWlanApi(HINSTANCE library
)
194 : library_(library
) {
195 GetWLANFunctions(library_
);
198 WindowsWlanApi::~WindowsWlanApi() {
199 FreeLibrary(library_
);
202 WindowsWlanApi
* WindowsWlanApi::Create() {
203 if (base::win::GetVersion() < base::win::VERSION_VISTA
)
205 // We use an absolute path to load the DLL to avoid DLL preloading attacks.
206 base::string16 system_directory
;
207 if (!GetSystemDirectory(&system_directory
)) {
210 DCHECK(!system_directory
.empty());
211 base::string16 dll_path
= system_directory
+ L
"wlanapi.dll";
212 HINSTANCE library
= LoadLibraryEx(dll_path
.c_str(),
214 LOAD_WITH_ALTERED_SEARCH_PATH
);
218 return new WindowsWlanApi(library
);
221 void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library
) {
222 DCHECK(wlan_library
);
223 WlanOpenHandle_function_
= reinterpret_cast<WlanOpenHandleFunction
>(
224 GetProcAddress(wlan_library
, "WlanOpenHandle"));
225 WlanEnumInterfaces_function_
= reinterpret_cast<WlanEnumInterfacesFunction
>(
226 GetProcAddress(wlan_library
, "WlanEnumInterfaces"));
227 WlanGetNetworkBssList_function_
=
228 reinterpret_cast<WlanGetNetworkBssListFunction
>(
229 GetProcAddress(wlan_library
, "WlanGetNetworkBssList"));
230 WlanFreeMemory_function_
= reinterpret_cast<WlanFreeMemoryFunction
>(
231 GetProcAddress(wlan_library
, "WlanFreeMemory"));
232 WlanCloseHandle_function_
= reinterpret_cast<WlanCloseHandleFunction
>(
233 GetProcAddress(wlan_library
, "WlanCloseHandle"));
234 DCHECK(WlanOpenHandle_function_
&&
235 WlanEnumInterfaces_function_
&&
236 WlanGetNetworkBssList_function_
&&
237 WlanFreeMemory_function_
&&
238 WlanCloseHandle_function_
);
241 void WindowsWlanApi::LogWlanInterfaceCount(int count
) {
242 UMA_HISTOGRAM_CUSTOM_COUNTS(
243 "Net.Wifi.InterfaceCount",
250 bool WindowsWlanApi::GetAccessPointData(
251 WifiData::AccessPointDataSet
* data
) {
254 // Get the handle to the WLAN API.
255 DWORD negotiated_version
;
256 HANDLE wlan_handle
= NULL
;
257 // We could be executing on either Windows XP or Windows Vista, so use the
258 // lower version of the client WLAN API. It seems that the negotiated version
259 // is the Vista version irrespective of what we pass!
260 static const int kXpWlanClientVersion
= 1;
261 if ((*WlanOpenHandle_function_
)(kXpWlanClientVersion
,
264 &wlan_handle
) != ERROR_SUCCESS
) {
265 LogWlanInterfaceCount(0);
270 // Get the list of interfaces. WlanEnumInterfaces allocates interface_list.
271 WLAN_INTERFACE_INFO_LIST
* interface_list
= NULL
;
272 if ((*WlanEnumInterfaces_function_
)(wlan_handle
, NULL
, &interface_list
) !=
274 LogWlanInterfaceCount(0);
277 DCHECK(interface_list
);
279 LogWlanInterfaceCount(interface_list
->dwNumberOfItems
);
281 // Go through the list of interfaces and get the data for each.
282 for (int i
= 0; i
< static_cast<int>(interface_list
->dwNumberOfItems
); ++i
) {
283 // Skip any interface that is midway through association; the
284 // WlanGetNetworkBssList function call is known to hang indefinitely
285 // when it's in this state. http://crbug.com/39300
286 if (interface_list
->InterfaceInfo
[i
].isState
==
287 wlan_interface_state_associating
) {
288 LOG(WARNING
) << "Skipping wifi scan on adapter " << i
<< " ("
289 << interface_list
->InterfaceInfo
[i
].strInterfaceDescription
290 << ") in 'associating' state. Repeated occurrences "
291 "indicates a non-responding adapter.";
294 GetInterfaceDataWLAN(wlan_handle
,
295 interface_list
->InterfaceInfo
[i
].InterfaceGuid
,
299 // Free interface_list.
300 (*WlanFreeMemory_function_
)(interface_list
);
303 if ((*WlanCloseHandle_function_
)(wlan_handle
, NULL
) != ERROR_SUCCESS
) {
310 // Appends the data for a single interface to the data vector. Returns the
311 // number of access points found, or -1 on error.
312 int WindowsWlanApi::GetInterfaceDataWLAN(
313 const HANDLE wlan_handle
,
314 const GUID
& interface_id
,
315 WifiData::AccessPointDataSet
* data
) {
318 const base::TimeTicks start_time
= base::TimeTicks::Now();
320 // WlanGetNetworkBssList allocates bss_list.
321 WLAN_BSS_LIST
* bss_list
= NULL
;
322 if ((*WlanGetNetworkBssList_function_
)(wlan_handle
,
324 NULL
, // Use all SSIDs.
326 false, // bSecurityEnabled - unused
328 &bss_list
) != ERROR_SUCCESS
) {
331 // According to http://www.attnetclient.com/kb/questions.php?questionid=75
332 // WlanGetNetworkBssList can sometimes return success, but leave the bss
337 const base::TimeDelta duration
= base::TimeTicks::Now() - start_time
;
339 UMA_HISTOGRAM_CUSTOM_TIMES(
340 "Net.Wifi.ScanLatency",
342 base::TimeDelta::FromMilliseconds(1),
343 base::TimeDelta::FromMinutes(1),
348 for (int i
= 0; i
< static_cast<int>(bss_list
->dwNumberOfItems
); ++i
) {
349 AccessPointData access_point_data
;
350 if (GetNetworkData(bss_list
->wlanBssEntries
[i
], &access_point_data
)) {
352 data
->insert(access_point_data
);
356 (*WlanFreeMemory_function_
)(bss_list
);
362 WindowsNdisApi::WindowsNdisApi(
363 std::vector
<base::string16
>* interface_service_names
)
364 : oid_buffer_size_(kInitialBufferSize
) {
365 DCHECK(!interface_service_names
->empty());
366 interface_service_names_
.swap(*interface_service_names
);
369 WindowsNdisApi::~WindowsNdisApi() {
372 WindowsNdisApi
* WindowsNdisApi::Create() {
373 std::vector
<base::string16
> interface_service_names
;
374 if (GetInterfacesNDIS(&interface_service_names
)) {
375 return new WindowsNdisApi(&interface_service_names
);
380 bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet
* data
) {
382 int interfaces_failed
= 0;
383 int interfaces_succeeded
= 0;
385 for (int i
= 0; i
< static_cast<int>(interface_service_names_
.size()); ++i
) {
386 // First, check that we have a DOS device for this adapter.
387 if (!DefineDosDeviceIfNotExists(interface_service_names_
[i
])) {
391 // Get the handle to the device. This will fail if the named device is not
393 HANDLE adapter_handle
= GetFileHandle(interface_service_names_
[i
]);
394 if (adapter_handle
== INVALID_HANDLE_VALUE
) {
399 if (GetInterfaceDataNDIS(adapter_handle
, data
)) {
400 ++interfaces_succeeded
;
406 CloseHandle(adapter_handle
);
407 UndefineDosDevice(interface_service_names_
[i
]);
410 // Return true if at least one interface succeeded, or at the very least none
412 return interfaces_succeeded
> 0 || interfaces_failed
== 0;
415 bool WindowsNdisApi::GetInterfacesNDIS(
416 std::vector
<base::string16
>* interface_service_names_out
) {
417 HKEY network_cards_key
= NULL
;
420 L
"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",
423 &network_cards_key
) != ERROR_SUCCESS
) {
426 DCHECK(network_cards_key
);
428 for (int i
= 0; ; ++i
) {
429 TCHAR name
[kStringLength
];
430 DWORD name_size
= kStringLength
;
432 if (RegEnumKeyEx(network_cards_key
,
439 &time
) != ERROR_SUCCESS
) {
442 HKEY hardware_key
= NULL
;
443 if (RegOpenKeyEx(network_cards_key
, name
, 0, KEY_READ
, &hardware_key
) !=
447 DCHECK(hardware_key
);
449 TCHAR service_name
[kStringLength
];
450 DWORD service_name_size
= kStringLength
;
452 if (RegQueryValueEx(hardware_key
,
456 reinterpret_cast<LPBYTE
>(service_name
),
457 &service_name_size
) == ERROR_SUCCESS
) {
458 interface_service_names_out
->push_back(service_name
);
460 RegCloseKey(hardware_key
);
463 RegCloseKey(network_cards_key
);
468 bool WindowsNdisApi::GetInterfaceDataNDIS(HANDLE adapter_handle
,
469 WifiData::AccessPointDataSet
* data
) {
472 scoped_ptr
<BYTE
, base::FreeDeleter
> buffer(
473 static_cast<BYTE
*>(malloc(oid_buffer_size_
)));
474 if (buffer
== NULL
) {
483 result
= PerformQuery(adapter_handle
, buffer
.get(),
484 oid_buffer_size_
, &bytes_out
);
485 if (result
== ERROR_GEN_FAILURE
|| // Returned by some Intel cards.
486 result
== ERROR_INSUFFICIENT_BUFFER
||
487 result
== ERROR_MORE_DATA
||
488 result
== NDIS_STATUS_INVALID_LENGTH
||
489 result
== NDIS_STATUS_BUFFER_TOO_SHORT
) {
490 // The buffer we supplied is too small, so increase it. bytes_out should
491 // provide the required buffer size, but this is not always the case.
492 if (bytes_out
> static_cast<DWORD
>(oid_buffer_size_
)) {
493 oid_buffer_size_
= bytes_out
;
495 oid_buffer_size_
*= 2;
497 if (!ResizeBuffer(oid_buffer_size_
, &buffer
)) {
498 oid_buffer_size_
= kInitialBufferSize
; // Reset for next time.
502 // The buffer is not too small.
506 DCHECK(buffer
.get());
508 if (result
== ERROR_SUCCESS
) {
509 NDIS_802_11_BSSID_LIST
* bssid_list
=
510 reinterpret_cast<NDIS_802_11_BSSID_LIST
*>(buffer
.get());
511 GetDataFromBssIdList(*bssid_list
, oid_buffer_size_
, data
);
517 bool GetNetworkData(const WLAN_BSS_ENTRY
& bss_entry
,
518 AccessPointData
* access_point_data
) {
519 // Currently we get only MAC address, signal strength and SSID.
520 DCHECK(access_point_data
);
521 access_point_data
->mac_address
= MacAddressAsString16(bss_entry
.dot11Bssid
);
522 access_point_data
->radio_signal_strength
= bss_entry
.lRssi
;
523 // bss_entry.dot11Ssid.ucSSID is not null-terminated.
524 base::UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry
.dot11Ssid
.ucSSID
),
525 static_cast<ULONG
>(bss_entry
.dot11Ssid
.uSSIDLength
),
526 &access_point_data
->ssid
);
527 // TODO(steveblock): Is it possible to get the following?
528 // access_point_data->signal_to_noise
529 // access_point_data->age
530 // access_point_data->channel
534 bool UndefineDosDevice(const base::string16
& device_name
) {
535 // We remove only the mapping we use, that is \Device\<device_name>.
536 base::string16 target_path
= L
"\\Device\\" + device_name
;
537 return DefineDosDevice(
538 DDD_RAW_TARGET_PATH
| DDD_REMOVE_DEFINITION
| DDD_EXACT_MATCH_ON_REMOVE
,
540 target_path
.c_str()) == TRUE
;
543 bool DefineDosDeviceIfNotExists(const base::string16
& device_name
) {
544 // We create a DOS device name for the device at \Device\<device_name>.
545 base::string16 target_path
= L
"\\Device\\" + device_name
;
547 TCHAR target
[kStringLength
];
548 if (QueryDosDevice(device_name
.c_str(), target
, kStringLength
) > 0 &&
549 target_path
.compare(target
) == 0) {
550 // Device already exists.
554 if (GetLastError() != ERROR_FILE_NOT_FOUND
) {
558 if (!DefineDosDevice(DDD_RAW_TARGET_PATH
,
560 target_path
.c_str())) {
564 // Check that the device is really there.
565 return QueryDosDevice(device_name
.c_str(), target
, kStringLength
) > 0 &&
566 target_path
.compare(target
) == 0;
569 HANDLE
GetFileHandle(const base::string16
& device_name
) {
570 // We access a device with DOS path \Device\<device_name> at
571 // \\.\<device_name>.
572 base::string16 formatted_device_name
= L
"\\\\.\\" + device_name
;
574 return CreateFile(formatted_device_name
.c_str(),
576 FILE_SHARE_READ
| FILE_SHARE_WRITE
, // share mode
577 0, // security attributes
579 0, // flags and attributes
580 INVALID_HANDLE_VALUE
);
583 int PerformQuery(HANDLE adapter_handle
,
587 DWORD oid
= OID_802_11_BSSID_LIST
;
588 if (!DeviceIoControl(adapter_handle
,
589 IOCTL_NDIS_QUERY_GLOBAL_STATS
,
596 return GetLastError();
598 return ERROR_SUCCESS
;
601 bool ResizeBuffer(int requested_size
,
602 scoped_ptr
<BYTE
, base::FreeDeleter
>* buffer
) {
603 DCHECK_GT(requested_size
, 0);
605 if (requested_size
> kMaximumBufferSize
) {
610 buffer
->reset(reinterpret_cast<BYTE
*>(
611 realloc(buffer
->release(), requested_size
)));
612 return buffer
!= NULL
;
615 bool GetSystemDirectory(base::string16
* path
) {
617 // Return value includes terminating NULL.
618 int buffer_size
= ::GetSystemDirectory(NULL
, 0);
619 if (buffer_size
== 0) {
622 scoped_ptr
<base::char16
[]> buffer(new base::char16
[buffer_size
]);
624 // Return value excludes terminating NULL.
625 int characters_written
= ::GetSystemDirectory(buffer
.get(), buffer_size
);
626 if (characters_written
== 0) {
629 DCHECK_EQ(buffer_size
- 1, characters_written
);
631 path
->assign(buffer
.get(), characters_written
);
633 if (*path
->rbegin() != L
'\\') {
636 DCHECK_EQ(L
'\\', *path
->rbegin());
641 } // namespace content