This source file includes following definitions.
- RequestConnectedNetworkUpdate
- ProfileNameFromGUID
- GUIDFromSSID
- SSIDFromWLAN
- GUIDFromWLAN
- enable_notify_network_changed_
- Initialize
- UnInitialize
- GetProperties
- GetManagedProperties
- GetState
- SetProperties
- CreateNetwork
- GetVisibleNetworks
- RequestNetworkScan
- StartConnect
- StartDisconnect
- GetKeyFromSystem
- SetEventObservers
- OnWlanNotificationCallback
- OnWlanNotification
- OnNetworkScanCompleteOnMainThread
- WaitForNetworkConnect
- CheckError
- FindNetwork
- SaveCurrentConnectedNetwork
- SortNetworks
- LoadWlanLibrary
- OpenClientHandle
- ResetDHCP
- FindAdapterIndexMapByGUID
- DisableNwCategoryWizard
- RestoreNwCategoryWizard
- EnsureInitialized
- CloseClientHandle
- SSIDFromGUID
- SecurityFromDot11AuthAlg
- NetworkPropertiesFromAvailableNetwork
- GetVisibleNetworkList
- FindConnectedNetwork
- GetConnectedFrequency
- GetFrequencyToConnect
- GetDesiredBssList
- GetNormalizedFrequency
- Connect
- Disconnect
- SaveTempProfile
- GetProfile
- SetProfile
- HaveProfile
- DeleteCreatedProfile
- WpaEncryptionFromEncryptionType
- AuthEncryptionFromSecurity
- CreateProfile
- NotifyNetworkListChanged
- NotifyNetworkChanged
- Create
#include "components/wifi/wifi_service.h"
#include <iphlpapi.h>
#include <objbase.h>
#include <wlanapi.h>
#include <set>
#include "base/base_paths_win.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/string16.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "components/onc/onc_constants.h"
#include "third_party/libxml/chromium/libxml_utils.h"
namespace {
const char kWiFiServiceError[] = "Error.WiFiService";
const char kWiFiServiceErrorNotImplemented[] =
"Error.WiFiService.NotImplemented";
const wchar_t kNwCategoryWizardRegKey[] =
L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Network\\"
L"NwCategoryWizard";
const wchar_t kNwCategoryWizardRegValue[] = L"Show";
const wchar_t kNwCategoryWizardSavedRegValue[] = L"ShowSaved";
const wchar_t kNwCategoryWizardDeleteRegValue[] = L"ShowDelete";
const wchar_t kWlanApiDll[] = L"wlanapi.dll";
const char kProfileXmlKey[] = "xml";
const char kProfileSharedKey[] = "shared";
const char kWlanConnect[] = "WlanConnect";
const char kWlanCloseHandle[] = "WlanCloseHandle";
const char kWlanDeleteProfile[] = "WlanDeleteProfile";
const char kWlanDisconnect[] = "WlanDisconnect";
const char kWlanEnumInterfaces[] = "WlanEnumInterfaces";
const char kWlanFreeMemory[] = "WlanFreeMemory";
const char kWlanGetAvailableNetworkList[] = "WlanGetAvailableNetworkList";
const char kWlanGetNetworkBssList[] = "WlanGetNetworkBssList";
const char kWlanGetProfile[] = "WlanGetProfile";
const char kWlanOpenHandle[] = "WlanOpenHandle";
const char kWlanQueryInterface[] = "WlanQueryInterface";
const char kWlanRegisterNotification[] = "WlanRegisterNotification";
const char kWlanSaveTemporaryProfile[] = "WlanSaveTemporaryProfile";
const char kWlanScan[] = "WlanScan";
const char kWlanSetProfile[] = "WlanSetProfile";
typedef DWORD (WINAPI* WlanConnectFunction)(
HANDLE hClientHandle,
CONST GUID *pInterfaceGuid,
CONST PWLAN_CONNECTION_PARAMETERS pConnectionParameters,
PVOID pReserved);
typedef DWORD (WINAPI* WlanCloseHandleFunction)(
HANDLE hClientHandle,
PVOID pReserved);
typedef DWORD (WINAPI* WlanDeleteProfileFunction)(
HANDLE hClientHandle,
const GUID *pInterfaceGuid,
LPCWSTR strProfileName,
PVOID pReserved);
typedef DWORD (WINAPI* WlanDisconnectFunction)(
HANDLE hClientHandle,
CONST GUID *pInterfaceGuid,
PVOID pReserved);
typedef DWORD (WINAPI* WlanEnumInterfacesFunction)(
HANDLE hClientHandle,
PVOID pReserved,
PWLAN_INTERFACE_INFO_LIST *ppInterfaceList);
typedef VOID (WINAPI* WlanFreeMemoryFunction)(
_In_ PVOID pMemory);
typedef DWORD (WINAPI* WlanGetAvailableNetworkListFunction)(
HANDLE hClientHandle,
CONST GUID *pInterfaceGuid,
DWORD dwFlags,
PVOID pReserved,
PWLAN_AVAILABLE_NETWORK_LIST *ppAvailableNetworkList);
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 DWORD (WINAPI* WlanGetProfileFunction)(
HANDLE hClientHandle,
CONST GUID *pInterfaceGuid,
LPCWSTR strProfileName,
PVOID pReserved,
LPWSTR *pstrProfileXml,
DWORD *pdwFlags,
DWORD *pdwGrantedAccess);
typedef DWORD (WINAPI* WlanOpenHandleFunction)(
DWORD dwClientVersion,
PVOID pReserved,
PDWORD pdwNegotiatedVersion,
PHANDLE phClientHandle);
typedef DWORD (WINAPI* WlanQueryInterfaceFunction)(
HANDLE hClientHandle,
const GUID *pInterfaceGuid,
WLAN_INTF_OPCODE OpCode,
PVOID pReserved,
PDWORD pdwDataSize,
PVOID *ppData,
PWLAN_OPCODE_VALUE_TYPE pWlanOpcodeValueType);
typedef DWORD (WINAPI* WlanRegisterNotificationFunction)(
HANDLE hClientHandle,
DWORD dwNotifSource,
BOOL bIgnoreDuplicate,
WLAN_NOTIFICATION_CALLBACK funcCallback,
PVOID pCallbackContext,
PVOID pReserved,
PDWORD pdwPrevNotifSource);
typedef DWORD (WINAPI* WlanSaveTemporaryProfileFunction)(
HANDLE hClientHandle,
CONST GUID* pInterfaceGuid,
LPCWSTR strProfileName,
LPCWSTR strAllUserProfileSecurity,
DWORD dwFlags,
BOOL bOverWrite,
PVOID pReserved);
typedef DWORD (WINAPI* WlanScanFunction)(
HANDLE hClientHandle,
CONST GUID *pInterfaceGuid,
CONST PDOT11_SSID pDot11Ssid,
CONST PWLAN_RAW_DATA pIeData,
PVOID pReserved);
typedef DWORD (WINAPI* WlanSetProfileFunction)(
HANDLE hClientHandle,
const GUID *pInterfaceGuid,
DWORD dwFlags,
LPCWSTR strProfileXml,
LPCWSTR strAllUserProfileSecurity,
BOOL bOverwrite,
PVOID pReserved,
DWORD* pdwReasonCode);
const char kAuthenticationOpen[] = "open";
const char kAuthenticationWepPsk[] = "WEP";
const char kAuthenticationWpaPsk[] = "WPAPSK";
const char kAuthenticationWpa2Psk[] = "WPA2PSK";
const char kEncryptionAES[] = "AES";
const char kEncryptionNone[] = "none";
const char kEncryptionTKIP[] = "TKIP";
const char kEncryptionWEP[] = "WEP";
const char kKeyTypeNetwork[] = "networkKey";
const char kKeyTypePassphrase[] = "passPhrase";
}
namespace wifi {
class WiFiServiceImpl : public WiFiService {
public:
WiFiServiceImpl();
virtual ~WiFiServiceImpl();
virtual void Initialize(
scoped_refptr<base::SequencedTaskRunner> task_runner) OVERRIDE;
virtual void UnInitialize() OVERRIDE;
virtual void GetProperties(const std::string& network_guid,
base::DictionaryValue* properties,
std::string* error) OVERRIDE;
virtual void GetManagedProperties(const std::string& network_guid,
base::DictionaryValue* managed_properties,
std::string* error) OVERRIDE;
virtual void GetState(const std::string& network_guid,
base::DictionaryValue* properties,
std::string* error) OVERRIDE;
virtual void SetProperties(const std::string& network_guid,
scoped_ptr<base::DictionaryValue> properties,
std::string* error) OVERRIDE;
virtual void CreateNetwork(bool shared,
scoped_ptr<base::DictionaryValue> properties,
std::string* network_guid,
std::string* error) OVERRIDE;
virtual void GetVisibleNetworks(const std::string& network_type,
base::ListValue* network_list) OVERRIDE;
virtual void RequestNetworkScan() OVERRIDE;
virtual void StartConnect(const std::string& network_guid,
std::string* error) OVERRIDE;
virtual void StartDisconnect(const std::string& network_guid,
std::string* error) OVERRIDE;
virtual void GetKeyFromSystem(const std::string& network_guid,
std::string* key_data,
std::string* error) OVERRIDE;
virtual void SetEventObservers(
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
const NetworkGuidListCallback& networks_changed_observer,
const NetworkGuidListCallback& network_list_changed_observer) OVERRIDE;
virtual void RequestConnectedNetworkUpdate() OVERRIDE {}
private:
typedef int32 EncryptionType;
enum EncryptionTypeEnum {
kEncryptionTypeAny = 0,
kEncryptionTypeAES = 1,
kEncryptionTypeTKIP = 2
};
static void __stdcall OnWlanNotificationCallback(
PWLAN_NOTIFICATION_DATA wlan_notification_data,
PVOID context);
void OnWlanNotification(PWLAN_NOTIFICATION_DATA wlan_notification_data);
void OnNetworkScanCompleteOnMainThread();
void WaitForNetworkConnect(const std::string& network_guid, int attempt);
bool CheckError(DWORD error_code,
const std::string& error_name,
std::string* error) const;
NetworkList::iterator FindNetwork(NetworkList& networks,
const std::string& network_guid);
DWORD SaveCurrentConnectedNetwork(std::string* connected_network_guid);
static void SortNetworks(NetworkList* networks);
DWORD OpenClientHandle();
DWORD ResetDHCP();
DWORD FindAdapterIndexMapByGUID(const GUID& interface_guid,
IP_ADAPTER_INDEX_MAP* adapter_index_map);
DWORD DisableNwCategoryWizard();
DWORD RestoreNwCategoryWizard();
DWORD EnsureInitialized();
DWORD CloseClientHandle();
base::string16 ProfileNameFromGUID(const std::string& network_guid) const {
return base::UTF8ToUTF16(network_guid);
}
DOT11_SSID SSIDFromGUID(const std::string& network_guid) const;
std::string GUIDFromSSID(const DOT11_SSID& dot11_ssid) const {
return std::string(reinterpret_cast<const char*>(dot11_ssid.ucSSID),
dot11_ssid.uSSIDLength);
}
std::string SSIDFromWLAN(const WLAN_AVAILABLE_NETWORK& wlan) const {
return GUIDFromSSID(wlan.dot11Ssid);
}
std::string GUIDFromWLAN(const WLAN_AVAILABLE_NETWORK& wlan) const {
return SSIDFromWLAN(wlan);
}
std::string SecurityFromDot11AuthAlg(DOT11_AUTH_ALGORITHM alg) const;
std::string WpaEncryptionFromEncryptionType(
EncryptionType encryption_type) const;
bool AuthEncryptionFromSecurity(const std::string& security,
EncryptionType encryption_type,
std::string* authentication,
std::string* encryption,
std::string* key_type) const;
void NetworkPropertiesFromAvailableNetwork(const WLAN_AVAILABLE_NETWORK& wlan,
const WLAN_BSS_LIST& wlan_bss_list,
NetworkProperties* properties);
DWORD GetVisibleNetworkList(NetworkList* network_list);
DWORD FindConnectedNetwork(std::string* connected_network_guid);
DWORD Connect(const std::string& network_guid, Frequency frequency);
DWORD Disconnect();
Frequency GetConnectedFrequency(const std::string& network_guid);
Frequency GetFrequencyToConnect(const std::string& network_guid) const;
DWORD GetDesiredBssList(DOT11_SSID& ssid,
Frequency frequency,
scoped_ptr<DOT11_BSSID_LIST>* desired_list);
Frequency GetNormalizedFrequency(int frequency_in_mhz) const;
bool CreateProfile(const NetworkProperties& network_properties,
EncryptionType encryption_type,
std::string* profile_xml);
DWORD SaveTempProfile(const std::string& network_guid);
DWORD GetProfile(const std::string& network_guid,
bool get_plaintext_key,
std::string* profile_xml);
DWORD SetProfile(bool shared, const std::string& profile_xml, bool overwrite);
bool HaveProfile(const std::string& network_guid);
DWORD DeleteCreatedProfile(const std::string& network_guid);
void NotifyNetworkListChanged(const NetworkList& networks);
void NotifyNetworkChanged(const std::string& network_guid);
DWORD LoadWlanLibrary();
HINSTANCE wlan_api_library_;
WlanConnectFunction WlanConnect_function_;
WlanCloseHandleFunction WlanCloseHandle_function_;
WlanDeleteProfileFunction WlanDeleteProfile_function_;
WlanDisconnectFunction WlanDisconnect_function_;
WlanEnumInterfacesFunction WlanEnumInterfaces_function_;
WlanFreeMemoryFunction WlanFreeMemory_function_;
WlanGetAvailableNetworkListFunction WlanGetAvailableNetworkList_function_;
WlanGetNetworkBssListFunction WlanGetNetworkBssList_function_;
WlanGetProfileFunction WlanGetProfile_function_;
WlanOpenHandleFunction WlanOpenHandle_function_;
WlanQueryInterfaceFunction WlanQueryInterface_function_;
WlanRegisterNotificationFunction WlanRegisterNotification_function_;
WlanScanFunction WlanScan_function_;
WlanSetProfileFunction WlanSetProfile_function_;
WlanSaveTemporaryProfileFunction WlanSaveTemporaryProfile_function_;
HANDLE client_;
GUID interface_guid_;
base::DictionaryValue connect_properties_;
std::map<std::string, std::string> saved_profiles_xml_;
base::DictionaryValue created_profiles_;
NetworkGuidListCallback networks_changed_observer_;
NetworkGuidListCallback network_list_changed_observer_;
scoped_ptr<DWORD> saved_nw_category_wizard_;
scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
scoped_refptr<base::SequencedTaskRunner> task_runner_;
bool enable_notify_network_changed_;
static const int kMaxAttempts = 100;
static const int kAttemptDelayMs = 100;
DISALLOW_COPY_AND_ASSIGN(WiFiServiceImpl);
};
WiFiServiceImpl::WiFiServiceImpl()
: wlan_api_library_(NULL),
WlanConnect_function_(NULL),
WlanCloseHandle_function_(NULL),
WlanDeleteProfile_function_(NULL),
WlanDisconnect_function_(NULL),
WlanEnumInterfaces_function_(NULL),
WlanFreeMemory_function_(NULL),
WlanGetAvailableNetworkList_function_(NULL),
WlanGetNetworkBssList_function_(NULL),
WlanGetProfile_function_(NULL),
WlanOpenHandle_function_(NULL),
WlanRegisterNotification_function_(NULL),
WlanSaveTemporaryProfile_function_(NULL),
WlanScan_function_(NULL),
WlanSetProfile_function_(NULL),
client_(NULL),
enable_notify_network_changed_(true) {}
WiFiServiceImpl::~WiFiServiceImpl() { UnInitialize(); }
void WiFiServiceImpl::Initialize(
scoped_refptr<base::SequencedTaskRunner> task_runner) {
DCHECK(!client_);
task_runner_.swap(task_runner);
RestoreNwCategoryWizard();
OpenClientHandle();
}
void WiFiServiceImpl::UnInitialize() {
CloseClientHandle();
}
void WiFiServiceImpl::GetProperties(const std::string& network_guid,
base::DictionaryValue* properties,
std::string* error) {
DWORD error_code = EnsureInitialized();
if (error_code == ERROR_SUCCESS) {
NetworkList network_list;
error_code = GetVisibleNetworkList(&network_list);
if (error_code == ERROR_SUCCESS && !network_list.empty()) {
NetworkList::const_iterator it = FindNetwork(network_list, network_guid);
if (it != network_list.end()) {
DVLOG(1) << "Get Properties: " << network_guid << ":"
<< it->connection_state;
properties->Swap(it->ToValue(false).get());
return;
} else {
error_code = ERROR_NOT_FOUND;
}
}
}
CheckError(error_code, kWiFiServiceError, error);
}
void WiFiServiceImpl::GetManagedProperties(
const std::string& network_guid,
base::DictionaryValue* managed_properties,
std::string* error) {
CheckError(ERROR_CALL_NOT_IMPLEMENTED, kWiFiServiceError, error);
}
void WiFiServiceImpl::GetState(const std::string& network_guid,
base::DictionaryValue* properties,
std::string* error) {
CheckError(ERROR_CALL_NOT_IMPLEMENTED, kWiFiServiceError, error);
}
void WiFiServiceImpl::SetProperties(
const std::string& network_guid,
scoped_ptr<base::DictionaryValue> properties,
std::string* error) {
DCHECK(properties.get());
if (!properties->HasKey(onc::network_type::kWiFi)) {
DVLOG(0) << "Missing WiFi properties:" << *properties;
*error = kWiFiServiceError;
return;
}
connect_properties_.SetWithoutPathExpansion(network_guid,
properties.release());
}
void WiFiServiceImpl::CreateNetwork(
bool shared,
scoped_ptr<base::DictionaryValue> properties,
std::string* network_guid,
std::string* error) {
DWORD error_code = EnsureInitialized();
if (CheckError(error_code, kWiFiServiceError, error))
return;
WiFiService::NetworkProperties network_properties;
if (!network_properties.UpdateFromValue(*properties)) {
CheckError(ERROR_INVALID_DATA, kWiFiServiceError, error);
return;
}
network_properties.guid = network_properties.ssid;
std::string profile_xml;
if (!CreateProfile(network_properties, kEncryptionTypeAny, &profile_xml)) {
CheckError(ERROR_INVALID_DATA, kWiFiServiceError, error);
return;
}
error_code = SetProfile(shared, profile_xml, false);
if (CheckError(error_code, kWiFiServiceError, error)) {
DVLOG(0) << profile_xml;
return;
}
std::string tkip_profile_xml;
if (!CreateProfile(network_properties,
kEncryptionTypeTKIP,
&tkip_profile_xml)) {
CheckError(ERROR_INVALID_DATA, kWiFiServiceError, error);
return;
}
if (tkip_profile_xml != profile_xml) {
scoped_ptr<base::DictionaryValue> tkip_profile(new base::DictionaryValue());
tkip_profile->SetString(kProfileXmlKey, tkip_profile_xml);
tkip_profile->SetBoolean(kProfileSharedKey, shared);
created_profiles_.SetWithoutPathExpansion(network_properties.guid,
tkip_profile.release());
}
*network_guid = network_properties.guid;
}
void WiFiServiceImpl::GetVisibleNetworks(const std::string& network_type,
base::ListValue* network_list) {
if (!network_type.empty() &&
network_type != onc::network_type::kAllTypes &&
network_type != onc::network_type::kWiFi) {
return;
}
DWORD error = EnsureInitialized();
if (error == ERROR_SUCCESS) {
NetworkList networks;
error = GetVisibleNetworkList(&networks);
if (error == ERROR_SUCCESS && !networks.empty()) {
SortNetworks(&networks);
for (WiFiService::NetworkList::const_iterator it = networks.begin();
it != networks.end();
++it) {
scoped_ptr<base::DictionaryValue> network(it->ToValue(true));
network_list->Append(network.release());
}
}
}
}
void WiFiServiceImpl::RequestNetworkScan() {
DWORD error = EnsureInitialized();
if (error == ERROR_SUCCESS) {
WlanScan_function_(client_, &interface_guid_, NULL, NULL, NULL);
}
}
void WiFiServiceImpl::StartConnect(const std::string& network_guid,
std::string* error) {
DVLOG(1) << "Start Connect: " << network_guid;
DWORD error_code = EnsureInitialized();
if (error_code == ERROR_SUCCESS) {
std::string connected_network_guid;
error_code = SaveCurrentConnectedNetwork(&connected_network_guid);
if (error_code == ERROR_SUCCESS) {
bool already_connected = (network_guid == connected_network_guid);
Frequency frequency = GetFrequencyToConnect(network_guid);
if (already_connected && frequency != kFrequencyAny) {
Frequency connected_frequency = GetConnectedFrequency(network_guid);
already_connected = (frequency == connected_frequency);
}
if (!already_connected)
error_code = Connect(network_guid, frequency);
if (error_code == ERROR_SUCCESS) {
NotifyNetworkChanged(connected_network_guid);
if (!networks_changed_observer_.is_null()) {
DisableNwCategoryWizard();
enable_notify_network_changed_ = false;
WaitForNetworkConnect(network_guid, 0);
return;
}
}
}
}
CheckError(error_code, kWiFiServiceError, error);
}
void WiFiServiceImpl::StartDisconnect(const std::string& network_guid,
std::string* error) {
DVLOG(1) << "Start Disconnect: " << network_guid;
DWORD error_code = EnsureInitialized();
if (error_code == ERROR_SUCCESS) {
std::string connected_network_guid;
error_code = SaveCurrentConnectedNetwork(&connected_network_guid);
if (error_code == ERROR_SUCCESS && network_guid == connected_network_guid) {
error_code = Disconnect();
if (error_code == ERROR_SUCCESS) {
NotifyNetworkChanged(network_guid);
return;
}
}
}
CheckError(error_code, kWiFiServiceError, error);
}
void WiFiServiceImpl::GetKeyFromSystem(const std::string& network_guid,
std::string* key_data,
std::string* error) {
DWORD error_code = EnsureInitialized();
if (CheckError(error_code, kWiFiServiceError, error))
return;
std::string profile_xml;
error_code = GetProfile(network_guid, true, &profile_xml);
if (CheckError(error_code, kWiFiServiceError, error))
return;
const char kSharedKeyElement[] = "sharedKey";
const char kProtectedElement[] = "protected";
const char kKeyMaterialElement[] = "keyMaterial";
if (profile_xml.find(kSharedKeyElement) == std::string::npos) {
*error = kWiFiServiceError;
return;
}
XmlReader reader;
if (reader.Load(profile_xml)) {
while (reader.Read()) {
reader.SkipToElement();
if (reader.NodeName() == kSharedKeyElement) {
while (reader.Read()) {
reader.SkipToElement();
if (reader.NodeName() == kKeyMaterialElement) {
reader.ReadElementContent(key_data);
} else if (reader.NodeName() == kProtectedElement) {
std::string protected_data;
reader.ReadElementContent(&protected_data);
if (protected_data != "false") {
*error = kWiFiServiceError;
break;
}
}
}
return;
}
}
}
*error = kWiFiServiceError;
}
void WiFiServiceImpl::SetEventObservers(
scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
const NetworkGuidListCallback& networks_changed_observer,
const NetworkGuidListCallback& network_list_changed_observer) {
DWORD error_code = EnsureInitialized();
if (error_code != ERROR_SUCCESS)
return;
message_loop_proxy_.swap(message_loop_proxy);
if (!networks_changed_observer_.is_null() ||
!network_list_changed_observer_.is_null()) {
WlanRegisterNotification_function_(client_,
WLAN_NOTIFICATION_SOURCE_NONE,
FALSE,
OnWlanNotificationCallback,
this,
NULL,
NULL);
}
networks_changed_observer_ = networks_changed_observer;
network_list_changed_observer_ = network_list_changed_observer;
if (!networks_changed_observer_.is_null() ||
!network_list_changed_observer_.is_null()) {
WlanRegisterNotification_function_(client_,
WLAN_NOTIFICATION_SOURCE_ALL,
FALSE,
OnWlanNotificationCallback,
this,
NULL,
NULL);
}
}
void WiFiServiceImpl::OnWlanNotificationCallback(
PWLAN_NOTIFICATION_DATA wlan_notification_data,
PVOID context) {
WiFiServiceImpl* service = reinterpret_cast<WiFiServiceImpl*>(context);
service->OnWlanNotification(wlan_notification_data);
}
void WiFiServiceImpl::OnWlanNotification(
PWLAN_NOTIFICATION_DATA wlan_notification_data) {
if (message_loop_proxy_ == NULL)
return;
switch (wlan_notification_data->NotificationCode) {
case wlan_notification_acm_disconnected:
case wlan_notification_acm_connection_complete:
case wlan_notification_acm_connection_attempt_fail: {
PWLAN_CONNECTION_NOTIFICATION_DATA wlan_connection_data =
reinterpret_cast<PWLAN_CONNECTION_NOTIFICATION_DATA>(
wlan_notification_data->pData);
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&WiFiServiceImpl::NotifyNetworkChanged,
base::Unretained(this),
GUIDFromSSID(wlan_connection_data->dot11Ssid)));
break;
}
case wlan_notification_acm_scan_complete:
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&WiFiServiceImpl::OnNetworkScanCompleteOnMainThread,
base::Unretained(this)));
break;
}
}
void WiFiServiceImpl::OnNetworkScanCompleteOnMainThread() {
NetworkList networks;
DWORD error = GetVisibleNetworkList(&networks);
DCHECK(error == ERROR_SUCCESS);
if (error == ERROR_SUCCESS)
NotifyNetworkListChanged(networks);
}
void WiFiServiceImpl::WaitForNetworkConnect(const std::string& network_guid,
int attempt) {
if (attempt > kMaxAttempts) {
LOG(ERROR) << kMaxAttempts << " attempts exceeded waiting for connect to "
<< network_guid;
base::DictionaryValue* created_profile = NULL;
if (created_profiles_.GetDictionaryWithoutPathExpansion(
network_guid, &created_profile)) {
std::string tkip_profile_xml;
bool shared = false;
if (created_profile->GetString(kProfileXmlKey, &tkip_profile_xml) &&
created_profile->GetBoolean(kProfileSharedKey, &shared)) {
created_profile->Remove(kProfileXmlKey, NULL);
created_profile->Remove(kProfileSharedKey, NULL);
DWORD error_code = SetProfile(shared, tkip_profile_xml, true);
if (error_code == ERROR_SUCCESS) {
error_code = Connect(network_guid,
GetFrequencyToConnect(network_guid));
if (error_code == ERROR_SUCCESS) {
WaitForNetworkConnect(network_guid, 0);
return;
} else {
LOG(ERROR) << "Failed to set created profile for " << network_guid
<< " error=" << error_code;
}
}
} else {
DWORD error_code = DeleteCreatedProfile(network_guid);
if (error_code != ERROR_SUCCESS) {
LOG(ERROR) << "Failed to delete created profile for " << network_guid
<< " error=" << error_code;
}
}
}
enable_notify_network_changed_ = true;
RestoreNwCategoryWizard();
return;
}
std::string connected_network_guid;
DWORD error = FindConnectedNetwork(&connected_network_guid);
if (network_guid == connected_network_guid) {
DVLOG(1) << "WiFi Connected, Reset DHCP: " << network_guid;
error = ResetDHCP();
created_profiles_.RemoveWithoutPathExpansion(network_guid, NULL);
enable_notify_network_changed_ = true;
RestoreNwCategoryWizard();
NotifyNetworkChanged(network_guid);
} else {
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&WiFiServiceImpl::WaitForNetworkConnect,
base::Unretained(this),
network_guid,
++attempt),
base::TimeDelta::FromMilliseconds(kAttemptDelayMs));
}
}
bool WiFiServiceImpl::CheckError(DWORD error_code,
const std::string& error_name,
std::string* error) const {
if (error_code != ERROR_SUCCESS) {
DLOG(ERROR) << "WiFiService Error " << error_code << ": " << error_name;
*error = error_name;
return true;
}
return false;
}
WiFiService::NetworkList::iterator WiFiServiceImpl::FindNetwork(
NetworkList& networks,
const std::string& network_guid) {
for (NetworkList::iterator it = networks.begin(); it != networks.end();
++it) {
if (it->guid == network_guid)
return it;
}
return networks.end();
}
DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork(
std::string* connected_network_guid) {
DWORD error = FindConnectedNetwork(connected_network_guid);
if (error == ERROR_SUCCESS && !connected_network_guid->empty()) {
if (error == ERROR_SUCCESS) {
SaveTempProfile(*connected_network_guid);
std::string profile_xml;
error = GetProfile(*connected_network_guid, false, &profile_xml);
if (error == ERROR_SUCCESS) {
saved_profiles_xml_[*connected_network_guid] = profile_xml;
}
}
}
return error;
}
void WiFiServiceImpl::SortNetworks(NetworkList* networks) {
networks->sort(NetworkProperties::OrderByType);
}
DWORD WiFiServiceImpl::LoadWlanLibrary() {
base::FilePath path;
if (!PathService::Get(base::DIR_SYSTEM, &path)) {
DLOG(ERROR) << "Unable to get system path.";
return ERROR_NOT_FOUND;
}
wlan_api_library_ = ::LoadLibraryEx(path.Append(kWlanApiDll).value().c_str(),
NULL,
LOAD_WITH_ALTERED_SEARCH_PATH);
if (!wlan_api_library_) {
DLOG(ERROR) << "Unable to load WlanApi.dll.";
return ERROR_NOT_FOUND;
}
WlanConnect_function_ =
reinterpret_cast<WlanConnectFunction>(
::GetProcAddress(wlan_api_library_, kWlanConnect));
WlanCloseHandle_function_ =
reinterpret_cast<WlanCloseHandleFunction>(
::GetProcAddress(wlan_api_library_, kWlanCloseHandle));
WlanDeleteProfile_function_ =
reinterpret_cast<WlanDeleteProfileFunction>(
::GetProcAddress(wlan_api_library_, kWlanDeleteProfile));
WlanDisconnect_function_ =
reinterpret_cast<WlanDisconnectFunction>(
::GetProcAddress(wlan_api_library_, kWlanDisconnect));
WlanEnumInterfaces_function_ =
reinterpret_cast<WlanEnumInterfacesFunction>(
::GetProcAddress(wlan_api_library_, kWlanEnumInterfaces));
WlanFreeMemory_function_ =
reinterpret_cast<WlanFreeMemoryFunction>(
::GetProcAddress(wlan_api_library_, kWlanFreeMemory));
WlanGetAvailableNetworkList_function_ =
reinterpret_cast<WlanGetAvailableNetworkListFunction>(
::GetProcAddress(wlan_api_library_, kWlanGetAvailableNetworkList));
WlanGetNetworkBssList_function_ =
reinterpret_cast<WlanGetNetworkBssListFunction>(
::GetProcAddress(wlan_api_library_, kWlanGetNetworkBssList));
WlanGetProfile_function_ =
reinterpret_cast<WlanGetProfileFunction>(
::GetProcAddress(wlan_api_library_, kWlanGetProfile));
WlanOpenHandle_function_ =
reinterpret_cast<WlanOpenHandleFunction>(
::GetProcAddress(wlan_api_library_, kWlanOpenHandle));
WlanQueryInterface_function_ =
reinterpret_cast<WlanQueryInterfaceFunction>(
::GetProcAddress(wlan_api_library_, kWlanQueryInterface));
WlanRegisterNotification_function_ =
reinterpret_cast<WlanRegisterNotificationFunction>(
::GetProcAddress(wlan_api_library_, kWlanRegisterNotification));
WlanSaveTemporaryProfile_function_ =
reinterpret_cast<WlanSaveTemporaryProfileFunction>(
::GetProcAddress(wlan_api_library_, kWlanSaveTemporaryProfile));
WlanScan_function_ =
reinterpret_cast<WlanScanFunction>(
::GetProcAddress(wlan_api_library_, kWlanScan));
WlanSetProfile_function_ =
reinterpret_cast<WlanSetProfileFunction>(
::GetProcAddress(wlan_api_library_, kWlanSetProfile));
if (!WlanConnect_function_ ||
!WlanCloseHandle_function_ ||
!WlanDeleteProfile_function_ ||
!WlanDisconnect_function_ ||
!WlanEnumInterfaces_function_ ||
!WlanFreeMemory_function_ ||
!WlanGetAvailableNetworkList_function_ ||
!WlanGetProfile_function_ ||
!WlanOpenHandle_function_ ||
!WlanQueryInterface_function_ ||
!WlanRegisterNotification_function_ ||
!WlanScan_function_ ||
!WlanSetProfile_function_) {
DLOG(ERROR) << "Unable to find required WlanApi function.";
FreeLibrary(wlan_api_library_);
wlan_api_library_ = NULL;
return ERROR_NOT_FOUND;
}
if (!WlanGetNetworkBssList_function_ ||
!WlanSaveTemporaryProfile_function_) {
DVLOG(1) << "WlanApi function is not be available on XP.";
}
return ERROR_SUCCESS;
}
DWORD WiFiServiceImpl::OpenClientHandle() {
DWORD error = LoadWlanLibrary();
DWORD service_version = 0;
if (error != ERROR_SUCCESS)
return error;
error = WlanOpenHandle_function_(1, NULL, &service_version, &client_);
PWLAN_INTERFACE_INFO_LIST interface_list = NULL;
if (error == ERROR_SUCCESS) {
error = WlanEnumInterfaces_function_(client_, NULL, &interface_list);
if (error == ERROR_SUCCESS) {
if (interface_list != NULL && interface_list->dwNumberOfItems != 0) {
interface_guid_ = interface_list->InterfaceInfo[0].InterfaceGuid;
for (DWORD itf = 0; itf < interface_list->dwNumberOfItems; ++itf) {
if (interface_list->InterfaceInfo[itf].isState ==
wlan_interface_state_connected) {
interface_guid_ = interface_list->InterfaceInfo[itf].InterfaceGuid;
break;
}
}
} else {
error = ERROR_NOINTERFACE;
}
}
if (interface_list != NULL)
WlanFreeMemory_function_(interface_list);
}
return error;
}
DWORD WiFiServiceImpl::ResetDHCP() {
IP_ADAPTER_INDEX_MAP adapter_index_map = {0};
DWORD error = FindAdapterIndexMapByGUID(interface_guid_, &adapter_index_map);
if (error == ERROR_SUCCESS) {
error = ::IpReleaseAddress(&adapter_index_map);
if (error == ERROR_SUCCESS) {
error = ::IpRenewAddress(&adapter_index_map);
}
}
return error;
}
DWORD WiFiServiceImpl::FindAdapterIndexMapByGUID(
const GUID& interface_guid,
IP_ADAPTER_INDEX_MAP* adapter_index_map) {
base::string16 guid_string;
const int kGUIDSize = 39;
::StringFromGUID2(
interface_guid, WriteInto(&guid_string, kGUIDSize), kGUIDSize);
ULONG buffer_length = 0;
DWORD error = ::GetInterfaceInfo(NULL, &buffer_length);
if (error == ERROR_INSUFFICIENT_BUFFER) {
scoped_ptr<unsigned char[]> buffer(new unsigned char[buffer_length]);
IP_INTERFACE_INFO* interface_info =
reinterpret_cast<IP_INTERFACE_INFO*>(buffer.get());
error = GetInterfaceInfo(interface_info, &buffer_length);
if (error == ERROR_SUCCESS) {
for (int adapter = 0; adapter < interface_info->NumAdapters; ++adapter) {
if (EndsWith(
interface_info->Adapter[adapter].Name, guid_string, false)) {
*adapter_index_map = interface_info->Adapter[adapter];
break;
}
}
}
}
return error;
}
DWORD WiFiServiceImpl::DisableNwCategoryWizard() {
base::win::RegKey nw_category_wizard;
DWORD error = nw_category_wizard.Open(HKEY_CURRENT_USER,
kNwCategoryWizardRegKey,
KEY_READ | KEY_SET_VALUE);
if (error == ERROR_SUCCESS) {
if (nw_category_wizard.HasValue(kNwCategoryWizardRegValue)) {
DWORD saved = 0u;
error = nw_category_wizard.ReadValueDW(kNwCategoryWizardRegValue,
&saved);
if (error == ERROR_SUCCESS) {
error = nw_category_wizard.WriteValue(kNwCategoryWizardSavedRegValue,
saved);
}
} else {
error = nw_category_wizard.WriteValue(kNwCategoryWizardDeleteRegValue,
1u);
}
error = nw_category_wizard.WriteValue(kNwCategoryWizardRegValue,
static_cast<DWORD>(0));
}
return error;
}
DWORD WiFiServiceImpl::RestoreNwCategoryWizard() {
base::win::RegKey nw_category_wizard;
DWORD error = nw_category_wizard.Open(HKEY_CURRENT_USER,
kNwCategoryWizardRegKey,
KEY_SET_VALUE);
if (error == ERROR_SUCCESS) {
if (nw_category_wizard.HasValue(kNwCategoryWizardSavedRegValue)) {
DWORD saved = 0u;
error = nw_category_wizard.ReadValueDW(kNwCategoryWizardSavedRegValue,
&saved);
if (error == ERROR_SUCCESS) {
error = nw_category_wizard.WriteValue(kNwCategoryWizardRegValue,
saved);
error = nw_category_wizard.DeleteValue(kNwCategoryWizardSavedRegValue);
}
} else if (nw_category_wizard.HasValue(kNwCategoryWizardDeleteRegValue)) {
error = nw_category_wizard.DeleteValue(kNwCategoryWizardRegValue);
error = nw_category_wizard.DeleteValue(kNwCategoryWizardDeleteRegValue);
}
}
return error;
}
DWORD WiFiServiceImpl::EnsureInitialized() {
if (client_ != NULL)
return ERROR_SUCCESS;
return ERROR_NOINTERFACE;
}
DWORD WiFiServiceImpl::CloseClientHandle() {
DWORD error = ERROR_SUCCESS;
if (client_ != NULL) {
error = WlanCloseHandle_function_(client_, NULL);
client_ = NULL;
}
if (wlan_api_library_ != NULL) {
WlanConnect_function_ = NULL;
WlanCloseHandle_function_ = NULL;
WlanDeleteProfile_function_ = NULL;
WlanDisconnect_function_ = NULL;
WlanEnumInterfaces_function_ = NULL;
WlanFreeMemory_function_ = NULL;
WlanGetAvailableNetworkList_function_ = NULL;
WlanGetNetworkBssList_function_ = NULL;
WlanGetProfile_function_ = NULL;
WlanOpenHandle_function_ = NULL;
WlanRegisterNotification_function_ = NULL;
WlanSaveTemporaryProfile_function_ = NULL;
WlanScan_function_ = NULL;
WlanSetProfile_function_ = NULL;
::FreeLibrary(wlan_api_library_);
wlan_api_library_ = NULL;
}
return error;
}
DOT11_SSID WiFiServiceImpl::SSIDFromGUID(
const std::string& network_guid) const {
DOT11_SSID ssid = {0};
if (network_guid.length() <= DOT11_SSID_MAX_LENGTH) {
ssid.uSSIDLength = static_cast<ULONG>(network_guid.length());
strncpy(reinterpret_cast<char*>(ssid.ucSSID),
network_guid.c_str(),
ssid.uSSIDLength);
} else {
NOTREACHED();
}
return ssid;
}
std::string WiFiServiceImpl::SecurityFromDot11AuthAlg(
DOT11_AUTH_ALGORITHM alg) const {
switch (alg) {
case DOT11_AUTH_ALGO_RSNA:
return onc::wifi::kWPA_EAP;
case DOT11_AUTH_ALGO_RSNA_PSK:
return onc::wifi::kWPA_PSK;
case DOT11_AUTH_ALGO_80211_SHARED_KEY:
return onc::wifi::kWEP_PSK;
case DOT11_AUTH_ALGO_80211_OPEN:
return onc::wifi::kNone;
default:
return onc::wifi::kWPA_EAP;
}
}
void WiFiServiceImpl::NetworkPropertiesFromAvailableNetwork(
const WLAN_AVAILABLE_NETWORK& wlan,
const WLAN_BSS_LIST& wlan_bss_list,
NetworkProperties* properties) {
if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) {
properties->connection_state = onc::connection_state::kConnected;
} else {
properties->connection_state = onc::connection_state::kNotConnected;
}
properties->ssid = SSIDFromWLAN(wlan);
properties->name = properties->ssid;
properties->guid = GUIDFromWLAN(wlan);
properties->type = onc::network_type::kWiFi;
for (size_t bss = 0; bss < wlan_bss_list.dwNumberOfItems; ++bss) {
const WLAN_BSS_ENTRY& bss_entry(wlan_bss_list.wlanBssEntries[bss]);
if (bss_entry.dot11Ssid.uSSIDLength == wlan.dot11Ssid.uSSIDLength &&
0 == memcmp(bss_entry.dot11Ssid.ucSSID,
wlan.dot11Ssid.ucSSID,
bss_entry.dot11Ssid.uSSIDLength)) {
properties->frequency = GetNormalizedFrequency(
bss_entry.ulChCenterFrequency / 1000);
properties->frequency_set.insert(properties->frequency);
properties->bssid = NetworkProperties::MacAddressAsString(
bss_entry.dot11Bssid);
}
}
properties->security =
SecurityFromDot11AuthAlg(wlan.dot11DefaultAuthAlgorithm);
properties->signal_strength = wlan.wlanSignalQuality;
}
DWORD WiFiServiceImpl::GetVisibleNetworkList(NetworkList* network_list) {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
DWORD error = ERROR_SUCCESS;
PWLAN_AVAILABLE_NETWORK_LIST available_network_list = NULL;
PWLAN_BSS_LIST bss_list = NULL;
error = WlanGetAvailableNetworkList_function_(
client_,
&interface_guid_,
WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES,
NULL,
&available_network_list);
std::set<std::string> network_guids;
if (error == ERROR_SUCCESS &&
available_network_list &&
WlanGetNetworkBssList_function_) {
error = WlanGetNetworkBssList_function_(client_,
&interface_guid_,
NULL,
dot11_BSS_type_any,
FALSE,
NULL,
&bss_list);
if (error == ERROR_SUCCESS && NULL != bss_list) {
for (DWORD i = 0; i < available_network_list->dwNumberOfItems; ++i) {
NetworkProperties network_properties;
NetworkPropertiesFromAvailableNetwork(
available_network_list->Network[i],
*bss_list,
&network_properties);
if (network_guids.count(network_properties.guid)) {
if (network_properties.connection_state ==
onc::connection_state::kConnected) {
NetworkList::iterator previous_network_properties =
FindNetwork(*network_list, network_properties.guid);
DCHECK(previous_network_properties != network_list->end());
previous_network_properties->connection_state =
network_properties.connection_state;
}
} else {
network_list->push_back(network_properties);
}
network_guids.insert(network_properties.guid);
}
}
}
if (available_network_list != NULL) {
WlanFreeMemory_function_(available_network_list);
}
if (bss_list != NULL) {
WlanFreeMemory_function_(bss_list);
}
return error;
}
DWORD WiFiServiceImpl::FindConnectedNetwork(
std::string* connected_network_guid) {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
DWORD error = ERROR_SUCCESS;
PWLAN_AVAILABLE_NETWORK_LIST available_network_list = NULL;
error = WlanGetAvailableNetworkList_function_(
client_, &interface_guid_, 0, NULL, &available_network_list);
if (error == ERROR_SUCCESS && NULL != available_network_list) {
for (DWORD i = 0; i < available_network_list->dwNumberOfItems; ++i) {
const WLAN_AVAILABLE_NETWORK& wlan = available_network_list->Network[i];
if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) {
*connected_network_guid = GUIDFromWLAN(wlan);
break;
}
}
}
if (available_network_list != NULL) {
WlanFreeMemory_function_(available_network_list);
}
return error;
}
WiFiService::Frequency WiFiServiceImpl::GetConnectedFrequency(
const std::string& network_guid) {
if (client_ == NULL) {
NOTREACHED();
return kFrequencyUnknown;
}
if (WlanGetNetworkBssList_function_ == NULL)
return kFrequencyUnknown;
Frequency frequency = kFrequencyUnknown;
DWORD error = ERROR_SUCCESS;
DWORD data_size = 0;
PWLAN_CONNECTION_ATTRIBUTES wlan_connection_attributes = NULL;
PWLAN_BSS_LIST bss_list = NULL;
error = WlanQueryInterface_function_(
client_,
&interface_guid_,
wlan_intf_opcode_current_connection,
NULL,
&data_size,
reinterpret_cast<PVOID*>(&wlan_connection_attributes),
NULL);
if (error == ERROR_SUCCESS &&
wlan_connection_attributes != NULL &&
wlan_connection_attributes->isState == wlan_interface_state_connected) {
WLAN_ASSOCIATION_ATTRIBUTES& connected_wlan =
wlan_connection_attributes->wlanAssociationAttributes;
if (GUIDFromSSID(connected_wlan.dot11Ssid) == network_guid &&
WlanGetNetworkBssList_function_ != NULL) {
error = WlanGetNetworkBssList_function_(client_,
&interface_guid_,
&connected_wlan.dot11Ssid,
connected_wlan.dot11BssType,
FALSE,
NULL,
&bss_list);
if (error == ERROR_SUCCESS && NULL != bss_list) {
for (size_t bss = 0; bss < bss_list->dwNumberOfItems; ++bss) {
const WLAN_BSS_ENTRY& bss_entry(bss_list->wlanBssEntries[bss]);
if (0 == memcmp(bss_entry.dot11Bssid,
connected_wlan.dot11Bssid,
sizeof(bss_entry.dot11Bssid))) {
frequency = GetNormalizedFrequency(
bss_entry.ulChCenterFrequency / 1000);
break;
}
}
}
}
}
if (wlan_connection_attributes != NULL) {
WlanFreeMemory_function_(wlan_connection_attributes);
}
if (bss_list != NULL) {
WlanFreeMemory_function_(bss_list);
}
return frequency;
}
WiFiService::Frequency WiFiServiceImpl::GetFrequencyToConnect(
const std::string& network_guid) const {
const base::DictionaryValue* properties;
const base::DictionaryValue* wifi;
int frequency;
if (connect_properties_.GetDictionaryWithoutPathExpansion(
network_guid, &properties) &&
properties->GetDictionary(onc::network_type::kWiFi, &wifi) &&
wifi->GetInteger(onc::wifi::kFrequency, &frequency)) {
return GetNormalizedFrequency(frequency);
}
return kFrequencyAny;
}
DWORD WiFiServiceImpl::GetDesiredBssList(
DOT11_SSID& ssid,
Frequency frequency,
scoped_ptr<DOT11_BSSID_LIST>* desired_list) {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
desired_list->reset();
if (frequency == kFrequencyAny)
return ERROR_SUCCESS;
if (!WlanGetNetworkBssList_function_)
return ERROR_NOT_SUPPORTED;
DWORD error = ERROR_SUCCESS;
PWLAN_BSS_LIST bss_list = NULL;
error = WlanGetNetworkBssList_function_(client_,
&interface_guid_,
&ssid,
dot11_BSS_type_infrastructure,
FALSE,
NULL,
&bss_list);
if (error == ERROR_SUCCESS && NULL != bss_list) {
unsigned int best_quality = 0u;
size_t best_index = 0;
Frequency bss_frequency;
for (size_t bss = 0; bss < bss_list->dwNumberOfItems; ++bss) {
const WLAN_BSS_ENTRY& bss_entry(bss_list->wlanBssEntries[bss]);
if (bss_entry.dot11Ssid.uSSIDLength != ssid.uSSIDLength ||
0 != memcmp(bss_entry.dot11Ssid.ucSSID,
ssid.ucSSID,
bss_entry.dot11Ssid.uSSIDLength))
continue;
bss_frequency = GetNormalizedFrequency(
bss_entry.ulChCenterFrequency / 1000);
if (bss_frequency == frequency &&
bss_entry.uLinkQuality > best_quality) {
best_quality = bss_entry.uLinkQuality;
best_index = bss;
}
}
if (best_quality > 0) {
const WLAN_BSS_ENTRY& bss_entry(bss_list->wlanBssEntries[best_index]);
scoped_ptr<DOT11_BSSID_LIST> selected_list(new DOT11_BSSID_LIST);
selected_list->Header.Revision = DOT11_BSSID_LIST_REVISION_1;
selected_list->Header.Size = sizeof(DOT11_BSSID_LIST);
selected_list->Header.Type = NDIS_OBJECT_TYPE_DEFAULT;
selected_list->uNumOfEntries = 1;
selected_list->uTotalNumOfEntries = 1;
std::copy(bss_entry.dot11Bssid,
bss_entry.dot11Bssid+sizeof(bss_entry.dot11Bssid),
selected_list->BSSIDs[0]);
desired_list->swap(selected_list);
DVLOG(1) << "Quality: " << best_quality << " BSS: "
<< NetworkProperties::MacAddressAsString(bss_entry.dot11Bssid);
} else {
error = ERROR_NOT_FOUND;
}
}
if (bss_list != NULL) {
WlanFreeMemory_function_(bss_list);
}
return error;
}
WiFiService::Frequency WiFiServiceImpl::GetNormalizedFrequency(
int frequency_in_mhz) const {
if (frequency_in_mhz == 0)
return kFrequencyAny;
if (frequency_in_mhz < 3000)
return kFrequency2400;
return kFrequency5000;
}
DWORD WiFiServiceImpl::Connect(const std::string& network_guid,
Frequency frequency) {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
DWORD error = ERROR_SUCCESS;
DOT11_SSID ssid = SSIDFromGUID(network_guid);
scoped_ptr<DOT11_BSSID_LIST> desired_bss_list;
error = GetDesiredBssList(ssid, frequency, &desired_bss_list);
if (error == ERROR_SUCCESS) {
if (HaveProfile(network_guid)) {
base::string16 profile_name = ProfileNameFromGUID(network_guid);
WLAN_CONNECTION_PARAMETERS wlan_params = {
wlan_connection_mode_profile,
profile_name.c_str(),
NULL,
desired_bss_list.get(),
dot11_BSS_type_any,
0};
error = WlanConnect_function_(
client_, &interface_guid_, &wlan_params, NULL);
} else {
WLAN_CONNECTION_PARAMETERS wlan_params = {
wlan_connection_mode_discovery_unsecure,
NULL,
&ssid,
desired_bss_list.get(),
dot11_BSS_type_infrastructure,
0};
error = WlanConnect_function_(
client_, &interface_guid_, &wlan_params, NULL);
}
}
return error;
}
DWORD WiFiServiceImpl::Disconnect() {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
DWORD error = ERROR_SUCCESS;
error = WlanDisconnect_function_(client_, &interface_guid_, NULL);
return error;
}
DWORD WiFiServiceImpl::SaveTempProfile(const std::string& network_guid) {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
DWORD error = ERROR_SUCCESS;
base::string16 profile_name = ProfileNameFromGUID(network_guid);
if (WlanSaveTemporaryProfile_function_) {
error = WlanSaveTemporaryProfile_function_(client_,
&interface_guid_,
profile_name.c_str(),
NULL,
WLAN_PROFILE_USER,
true,
NULL);
} else {
error = ERROR_NOT_SUPPORTED;
}
return error;
}
DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid,
bool get_plaintext_key,
std::string* profile_xml) {
if (client_ == NULL) {
NOTREACHED();
return ERROR_NOINTERFACE;
}
DWORD error = ERROR_SUCCESS;
base::string16 profile_name = ProfileNameFromGUID(network_guid);
DWORD flags = get_plaintext_key ? WLAN_PROFILE_GET_PLAINTEXT_KEY : 0;
LPWSTR str_profile_xml = NULL;
error = WlanGetProfile_function_(client_,
&interface_guid_,
profile_name.c_str(),
NULL,
&str_profile_xml,
&flags,
NULL);
if (error == ERROR_SUCCESS && str_profile_xml != NULL) {
*profile_xml = base::UTF16ToUTF8(str_profile_xml);
}
if (str_profile_xml != NULL) {
WlanFreeMemory_function_(str_profile_xml);
}
return error;
}
DWORD WiFiServiceImpl::SetProfile(bool shared,
const std::string& profile_xml,
bool overwrite) {
DWORD error_code = ERROR_SUCCESS;
base::string16 profile_xml16(base::UTF8ToUTF16(profile_xml));
DWORD reason_code = 0u;
error_code = WlanSetProfile_function_(client_,
&interface_guid_,
shared ? 0 : WLAN_PROFILE_USER,
profile_xml16.c_str(),
NULL,
overwrite,
NULL,
&reason_code);
return error_code;
}
bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) {
DWORD error = ERROR_SUCCESS;
std::string profile_xml;
return GetProfile(network_guid, false, &profile_xml) == ERROR_SUCCESS;
}
DWORD WiFiServiceImpl::DeleteCreatedProfile(const std::string& network_guid) {
base::DictionaryValue* created_profile = NULL;
DWORD error_code = ERROR_SUCCESS;
if (created_profiles_.GetDictionaryWithoutPathExpansion(
network_guid, &created_profile)) {
base::string16 profile_name = ProfileNameFromGUID(network_guid);
error_code = WlanDeleteProfile_function_(client_,
&interface_guid_,
profile_name.c_str(),
NULL);
created_profiles_.RemoveWithoutPathExpansion(network_guid, NULL);
}
return error_code;
}
std::string WiFiServiceImpl::WpaEncryptionFromEncryptionType(
EncryptionType encryption_type) const {
if (encryption_type == kEncryptionTypeTKIP)
return kEncryptionTKIP;
return kEncryptionAES;
}
bool WiFiServiceImpl::AuthEncryptionFromSecurity(
const std::string& security,
EncryptionType encryption_type,
std::string* authentication,
std::string* encryption,
std::string* key_type) const {
if (security == onc::wifi::kNone) {
*authentication = kAuthenticationOpen;
*encryption = kEncryptionNone;
} else if (security == onc::wifi::kWEP_PSK) {
*authentication = kAuthenticationOpen;
*encryption = kEncryptionWEP;
*key_type = kKeyTypeNetwork;
} else if (security == onc::wifi::kWPA_PSK) {
*authentication = kAuthenticationWpaPsk;
*encryption = WpaEncryptionFromEncryptionType(encryption_type);
*key_type = kKeyTypePassphrase;
} else if (security == onc::wifi::kWPA2_PSK) {
*authentication = kAuthenticationWpa2Psk;
*encryption = WpaEncryptionFromEncryptionType(encryption_type);
*key_type = kKeyTypePassphrase;
} else {
return false;
}
return true;
}
bool WiFiServiceImpl::CreateProfile(
const NetworkProperties& network_properties,
EncryptionType encryption_type,
std::string* profile_xml) {
std::string authentication;
std::string encryption;
std::string key_type;
bool valid = AuthEncryptionFromSecurity(network_properties.security,
encryption_type,
&authentication,
&encryption,
&key_type);
if (!valid)
return valid;
XmlWriter xml_writer;
xml_writer.StartWriting();
xml_writer.StartElement("WLANProfile");
xml_writer.AddAttribute(
"xmlns",
"http://www.microsoft.com/networking/WLAN/profile/v1");
xml_writer.WriteElement("name", network_properties.guid);
xml_writer.StartElement("SSIDConfig");
xml_writer.StartElement("SSID");
xml_writer.WriteElement("name", network_properties.ssid);
xml_writer.EndElement();
xml_writer.EndElement();
xml_writer.WriteElement("connectionType", "ESS");
xml_writer.WriteElement("connectionMode", "manual");
xml_writer.StartElement("MSM");
xml_writer.StartElement("security");
xml_writer.StartElement("authEncryption");
xml_writer.WriteElement("authentication", authentication);
xml_writer.WriteElement("encryption", encryption);
xml_writer.WriteElement("useOneX", "false");
xml_writer.EndElement();
if (!key_type.empty()) {
xml_writer.StartElement("sharedKey");
xml_writer.WriteElement("keyType", key_type);
xml_writer.WriteElement("protected", "false");
xml_writer.WriteElement("keyMaterial", network_properties.password);
xml_writer.EndElement();
}
xml_writer.EndElement();
xml_writer.EndElement();
xml_writer.EndElement();
xml_writer.StopWriting();
*profile_xml = xml_writer.GetWrittenString();
return true;
}
void WiFiServiceImpl::NotifyNetworkListChanged(const NetworkList& networks) {
if (network_list_changed_observer_.is_null())
return;
NetworkGuidList current_networks;
for (NetworkList::const_iterator it = networks.begin();
it != networks.end();
++it) {
current_networks.push_back(it->guid);
}
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(network_list_changed_observer_, current_networks));
}
void WiFiServiceImpl::NotifyNetworkChanged(const std::string& network_guid) {
if (enable_notify_network_changed_ && !networks_changed_observer_.is_null()) {
DVLOG(1) << "NotifyNetworkChanged: " << network_guid;
NetworkGuidList changed_networks(1, network_guid);
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(networks_changed_observer_, changed_networks));
}
}
WiFiService* WiFiService::Create() { return new WiFiServiceImpl(); }
}