This source file includes following definitions.
- DefaultFactoryFunction
- NewWlanApi
- NewPollingPolicy
- Create
- GetWLANFunctions
- LogWlanInterfaceCount
- GetAccessPointData
- GetInterfaceDataWLAN
- Create
- GetAccessPointData
- GetInterfacesNDIS
- GetInterfaceDataNDIS
- GetNetworkData
- UndefineDosDevice
- DefineDosDeviceIfNotExists
- GetFileHandle
- PerformQuery
- ResizeBuffer
- GetSystemDirectory
#include "content/browser/geolocation/wifi_data_provider_win.h"
#include <windows.h>
#include <winioctl.h>
#include <wlanapi.h>
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/windows_version.h"
#include "content/browser/geolocation/wifi_data_provider_common.h"
#include "content/browser/geolocation/wifi_data_provider_common_win.h"
#define NDIS_STATUS_INVALID_LENGTH ((NDIS_STATUS)0xC0010014L)
#define NDIS_STATUS_BUFFER_TOO_SHORT ((NDIS_STATUS)0xC0010016L)
namespace content {
namespace {
const int kInitialBufferSize = 2 << 12;
const int kMaximumBufferSize = 2 << 20;
const int kStringLength = 512;
const int kDefaultPollingInterval = 10000;
const int kNoChangePollingInterval = 120000;
const int kTwoNoChangePollingInterval = 600000;
const int kNoWifiPollingIntervalMilliseconds = 20 * 1000;
typedef DWORD (WINAPI* WlanOpenHandleFunction)(DWORD dwClientVersion,
PVOID pReserved,
PDWORD pdwNegotiatedVersion,
PHANDLE phClientHandle);
typedef DWORD (WINAPI* WlanEnumInterfacesFunction)(
HANDLE hClientHandle,
PVOID pReserved,
PWLAN_INTERFACE_INFO_LIST* ppInterfaceList);
typedef DWORD (WINAPI* WlanGetNetworkBssListFunction)(
HANDLE hClientHandle,
const GUID* pInterfaceGuid,
const PDOT11_SSID pDot11Ssid,
DOT11_BSS_TYPE dot11BssType,
BOOL bSecurityEnabled,
PVOID pReserved,
PWLAN_BSS_LIST* ppWlanBssList
);
typedef VOID (WINAPI* WlanFreeMemoryFunction)(PVOID pMemory);
typedef DWORD (WINAPI* WlanCloseHandleFunction)(HANDLE hClientHandle,
PVOID pReserved);
class WindowsWlanApi : public WifiDataProviderCommon::WlanApiInterface {
public:
virtual ~WindowsWlanApi();
static WindowsWlanApi* Create();
virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data);
private:
explicit WindowsWlanApi(HINSTANCE library);
void GetWLANFunctions(HINSTANCE wlan_library);
int GetInterfaceDataWLAN(HANDLE wlan_handle,
const GUID& interface_id,
WifiData::AccessPointDataSet* data);
static void LogWlanInterfaceCount(int count);
HINSTANCE library_;
WlanOpenHandleFunction WlanOpenHandle_function_;
WlanEnumInterfacesFunction WlanEnumInterfaces_function_;
WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_;
WlanFreeMemoryFunction WlanFreeMemory_function_;
WlanCloseHandleFunction WlanCloseHandle_function_;
};
class WindowsNdisApi : public WifiDataProviderCommon::WlanApiInterface {
public:
virtual ~WindowsNdisApi();
static WindowsNdisApi* Create();
virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data);
private:
static bool GetInterfacesNDIS(
std::vector<base::string16>* interface_service_names_out);
explicit WindowsNdisApi(std::vector<base::string16>* interface_service_names);
bool GetInterfaceDataNDIS(HANDLE adapter_handle,
WifiData::AccessPointDataSet* data);
std::vector<base::string16> interface_service_names_;
int oid_buffer_size_;
};
bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry,
AccessPointData* access_point_data);
bool UndefineDosDevice(const base::string16& device_name);
bool DefineDosDeviceIfNotExists(const base::string16& device_name);
HANDLE GetFileHandle(const base::string16& device_name);
int PerformQuery(HANDLE adapter_handle,
BYTE* buffer,
DWORD buffer_size,
DWORD* bytes_out);
bool ResizeBuffer(int requested_size,
scoped_ptr<BYTE, base::FreeDeleter>* buffer);
bool GetSystemDirectory(base::string16* path);
}
WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() {
return new Win32WifiDataProvider();
}
Win32WifiDataProvider::Win32WifiDataProvider() {
}
Win32WifiDataProvider::~Win32WifiDataProvider() {
}
WifiDataProviderCommon::WlanApiInterface* Win32WifiDataProvider::NewWlanApi() {
WlanApiInterface* api = WindowsWlanApi::Create();
if (api) {
return api;
}
return WindowsNdisApi::Create();
}
WifiPollingPolicy* Win32WifiDataProvider::NewPollingPolicy() {
return new GenericWifiPollingPolicy<kDefaultPollingInterval,
kNoChangePollingInterval,
kTwoNoChangePollingInterval,
kNoWifiPollingIntervalMilliseconds>;
}
namespace {
WindowsWlanApi::WindowsWlanApi(HINSTANCE library)
: library_(library) {
GetWLANFunctions(library_);
}
WindowsWlanApi::~WindowsWlanApi() {
FreeLibrary(library_);
}
WindowsWlanApi* WindowsWlanApi::Create() {
if (base::win::GetVersion() < base::win::VERSION_VISTA)
return NULL;
base::string16 system_directory;
if (!GetSystemDirectory(&system_directory)) {
return NULL;
}
DCHECK(!system_directory.empty());
base::string16 dll_path = system_directory + L"wlanapi.dll";
HINSTANCE library = LoadLibraryEx(dll_path.c_str(),
NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
if (!library) {
return NULL;
}
return new WindowsWlanApi(library);
}
void WindowsWlanApi::GetWLANFunctions(HINSTANCE wlan_library) {
DCHECK(wlan_library);
WlanOpenHandle_function_ = reinterpret_cast<WlanOpenHandleFunction>(
GetProcAddress(wlan_library, "WlanOpenHandle"));
WlanEnumInterfaces_function_ = reinterpret_cast<WlanEnumInterfacesFunction>(
GetProcAddress(wlan_library, "WlanEnumInterfaces"));
WlanGetNetworkBssList_function_ =
reinterpret_cast<WlanGetNetworkBssListFunction>(
GetProcAddress(wlan_library, "WlanGetNetworkBssList"));
WlanFreeMemory_function_ = reinterpret_cast<WlanFreeMemoryFunction>(
GetProcAddress(wlan_library, "WlanFreeMemory"));
WlanCloseHandle_function_ = reinterpret_cast<WlanCloseHandleFunction>(
GetProcAddress(wlan_library, "WlanCloseHandle"));
DCHECK(WlanOpenHandle_function_ &&
WlanEnumInterfaces_function_ &&
WlanGetNetworkBssList_function_ &&
WlanFreeMemory_function_ &&
WlanCloseHandle_function_);
}
void WindowsWlanApi::LogWlanInterfaceCount(int count) {
UMA_HISTOGRAM_CUSTOM_COUNTS(
"Net.Wifi.InterfaceCount",
count,
1,
5,
5);
}
bool WindowsWlanApi::GetAccessPointData(
WifiData::AccessPointDataSet* data) {
DCHECK(data);
DWORD negotiated_version;
HANDLE wlan_handle = NULL;
static const int kXpWlanClientVersion = 1;
if ((*WlanOpenHandle_function_)(kXpWlanClientVersion,
NULL,
&negotiated_version,
&wlan_handle) != ERROR_SUCCESS) {
LogWlanInterfaceCount(0);
return false;
}
DCHECK(wlan_handle);
WLAN_INTERFACE_INFO_LIST* interface_list = NULL;
if ((*WlanEnumInterfaces_function_)(wlan_handle, NULL, &interface_list) !=
ERROR_SUCCESS) {
LogWlanInterfaceCount(0);
return false;
}
DCHECK(interface_list);
LogWlanInterfaceCount(interface_list->dwNumberOfItems);
for (int i = 0; i < static_cast<int>(interface_list->dwNumberOfItems); ++i) {
if (interface_list->InterfaceInfo[i].isState ==
wlan_interface_state_associating) {
LOG(WARNING) << "Skipping wifi scan on adapter " << i << " ("
<< interface_list->InterfaceInfo[i].strInterfaceDescription
<< ") in 'associating' state. Repeated occurrences "
"indicates a non-responding adapter.";
continue;
}
GetInterfaceDataWLAN(wlan_handle,
interface_list->InterfaceInfo[i].InterfaceGuid,
data);
}
(*WlanFreeMemory_function_)(interface_list);
if ((*WlanCloseHandle_function_)(wlan_handle, NULL) != ERROR_SUCCESS) {
return false;
}
return true;
}
int WindowsWlanApi::GetInterfaceDataWLAN(
const HANDLE wlan_handle,
const GUID& interface_id,
WifiData::AccessPointDataSet* data) {
DCHECK(data);
const base::TimeTicks start_time = base::TimeTicks::Now();
WLAN_BSS_LIST* bss_list = NULL;
if ((*WlanGetNetworkBssList_function_)(wlan_handle,
&interface_id,
NULL,
dot11_BSS_type_any,
false,
NULL,
&bss_list) != ERROR_SUCCESS) {
return -1;
}
if (!bss_list)
return -1;
const base::TimeDelta duration = base::TimeTicks::Now() - start_time;
UMA_HISTOGRAM_CUSTOM_TIMES(
"Net.Wifi.ScanLatency",
duration,
base::TimeDelta::FromMilliseconds(1),
base::TimeDelta::FromMinutes(1),
100);
int found = 0;
for (int i = 0; i < static_cast<int>(bss_list->dwNumberOfItems); ++i) {
AccessPointData access_point_data;
if (GetNetworkData(bss_list->wlanBssEntries[i], &access_point_data)) {
++found;
data->insert(access_point_data);
}
}
(*WlanFreeMemory_function_)(bss_list);
return found;
}
WindowsNdisApi::WindowsNdisApi(
std::vector<base::string16>* interface_service_names)
: oid_buffer_size_(kInitialBufferSize) {
DCHECK(!interface_service_names->empty());
interface_service_names_.swap(*interface_service_names);
}
WindowsNdisApi::~WindowsNdisApi() {
}
WindowsNdisApi* WindowsNdisApi::Create() {
std::vector<base::string16> interface_service_names;
if (GetInterfacesNDIS(&interface_service_names)) {
return new WindowsNdisApi(&interface_service_names);
}
return NULL;
}
bool WindowsNdisApi::GetAccessPointData(WifiData::AccessPointDataSet* data) {
DCHECK(data);
int interfaces_failed = 0;
int interfaces_succeeded = 0;
for (int i = 0; i < static_cast<int>(interface_service_names_.size()); ++i) {
if (!DefineDosDeviceIfNotExists(interface_service_names_[i])) {
continue;
}
HANDLE adapter_handle = GetFileHandle(interface_service_names_[i]);
if (adapter_handle == INVALID_HANDLE_VALUE) {
continue;
}
if (GetInterfaceDataNDIS(adapter_handle, data)) {
++interfaces_succeeded;
} else {
++interfaces_failed;
}
CloseHandle(adapter_handle);
UndefineDosDevice(interface_service_names_[i]);
}
return interfaces_succeeded > 0 || interfaces_failed == 0;
}
bool WindowsNdisApi::GetInterfacesNDIS(
std::vector<base::string16>* interface_service_names_out) {
HKEY network_cards_key = NULL;
if (RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
L"Software\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",
0,
KEY_READ,
&network_cards_key) != ERROR_SUCCESS) {
return false;
}
DCHECK(network_cards_key);
for (int i = 0; ; ++i) {
TCHAR name[kStringLength];
DWORD name_size = kStringLength;
FILETIME time;
if (RegEnumKeyEx(network_cards_key,
i,
name,
&name_size,
NULL,
NULL,
NULL,
&time) != ERROR_SUCCESS) {
break;
}
HKEY hardware_key = NULL;
if (RegOpenKeyEx(network_cards_key, name, 0, KEY_READ, &hardware_key) !=
ERROR_SUCCESS) {
break;
}
DCHECK(hardware_key);
TCHAR service_name[kStringLength];
DWORD service_name_size = kStringLength;
DWORD type = 0;
if (RegQueryValueEx(hardware_key,
L"ServiceName",
NULL,
&type,
reinterpret_cast<LPBYTE>(service_name),
&service_name_size) == ERROR_SUCCESS) {
interface_service_names_out->push_back(service_name);
}
RegCloseKey(hardware_key);
}
RegCloseKey(network_cards_key);
return true;
}
bool WindowsNdisApi::GetInterfaceDataNDIS(HANDLE adapter_handle,
WifiData::AccessPointDataSet* data) {
DCHECK(data);
scoped_ptr<BYTE, base::FreeDeleter> buffer(
static_cast<BYTE*>(malloc(oid_buffer_size_)));
if (buffer == NULL) {
return false;
}
DWORD bytes_out;
int result;
while (true) {
bytes_out = 0;
result = PerformQuery(adapter_handle, buffer.get(),
oid_buffer_size_, &bytes_out);
if (result == ERROR_GEN_FAILURE ||
result == ERROR_INSUFFICIENT_BUFFER ||
result == ERROR_MORE_DATA ||
result == NDIS_STATUS_INVALID_LENGTH ||
result == NDIS_STATUS_BUFFER_TOO_SHORT) {
if (bytes_out > static_cast<DWORD>(oid_buffer_size_)) {
oid_buffer_size_ = bytes_out;
} else {
oid_buffer_size_ *= 2;
}
if (!ResizeBuffer(oid_buffer_size_, &buffer)) {
oid_buffer_size_ = kInitialBufferSize;
return false;
}
} else {
break;
}
}
DCHECK(buffer.get());
if (result == ERROR_SUCCESS) {
NDIS_802_11_BSSID_LIST* bssid_list =
reinterpret_cast<NDIS_802_11_BSSID_LIST*>(buffer.get());
GetDataFromBssIdList(*bssid_list, oid_buffer_size_, data);
}
return true;
}
bool GetNetworkData(const WLAN_BSS_ENTRY& bss_entry,
AccessPointData* access_point_data) {
DCHECK(access_point_data);
access_point_data->mac_address = MacAddressAsString16(bss_entry.dot11Bssid);
access_point_data->radio_signal_strength = bss_entry.lRssi;
base::UTF8ToUTF16(reinterpret_cast<const char*>(bss_entry.dot11Ssid.ucSSID),
static_cast<ULONG>(bss_entry.dot11Ssid.uSSIDLength),
&access_point_data->ssid);
return true;
}
bool UndefineDosDevice(const base::string16& device_name) {
base::string16 target_path = L"\\Device\\" + device_name;
return DefineDosDevice(
DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE,
device_name.c_str(),
target_path.c_str()) == TRUE;
}
bool DefineDosDeviceIfNotExists(const base::string16& device_name) {
base::string16 target_path = L"\\Device\\" + device_name;
TCHAR target[kStringLength];
if (QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 &&
target_path.compare(target) == 0) {
return true;
}
if (GetLastError() != ERROR_FILE_NOT_FOUND) {
return false;
}
if (!DefineDosDevice(DDD_RAW_TARGET_PATH,
device_name.c_str(),
target_path.c_str())) {
return false;
}
return QueryDosDevice(device_name.c_str(), target, kStringLength) > 0 &&
target_path.compare(target) == 0;
}
HANDLE GetFileHandle(const base::string16& device_name) {
base::string16 formatted_device_name = L"\\\\.\\" + device_name;
return CreateFile(formatted_device_name.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
0,
INVALID_HANDLE_VALUE);
}
int PerformQuery(HANDLE adapter_handle,
BYTE* buffer,
DWORD buffer_size,
DWORD* bytes_out) {
DWORD oid = OID_802_11_BSSID_LIST;
if (!DeviceIoControl(adapter_handle,
IOCTL_NDIS_QUERY_GLOBAL_STATS,
&oid,
sizeof(oid),
buffer,
buffer_size,
bytes_out,
NULL)) {
return GetLastError();
}
return ERROR_SUCCESS;
}
bool ResizeBuffer(int requested_size,
scoped_ptr<BYTE, base::FreeDeleter>* buffer) {
DCHECK_GT(requested_size, 0);
DCHECK(buffer);
if (requested_size > kMaximumBufferSize) {
buffer->reset();
return false;
}
buffer->reset(reinterpret_cast<BYTE*>(
realloc(buffer->release(), requested_size)));
return buffer != NULL;
}
bool GetSystemDirectory(base::string16* path) {
DCHECK(path);
int buffer_size = ::GetSystemDirectory(NULL, 0);
if (buffer_size == 0) {
return false;
}
scoped_ptr<base::char16[]> buffer(new base::char16[buffer_size]);
int characters_written = ::GetSystemDirectory(buffer.get(), buffer_size);
if (characters_written == 0) {
return false;
}
DCHECK_EQ(buffer_size - 1, characters_written);
path->assign(buffer.get(), characters_written);
if (*path->rbegin() != L'\\') {
path->append(L"\\");
}
DCHECK_EQ(L'\\', *path->rbegin());
return true;
}
}
}