forked from Vivek-Ramachandran/wi-door
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wlan.cpp
109 lines (85 loc) · 2.89 KB
/
Wlan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "stdafx.h"
#include "Wlan.h"
Wlan::Wlan()
{
}
Wlan::~Wlan()
{
CloseHandle();
}
bool Wlan::IsNetAvailable( const char * ssid )
{
bool result = false;
if (OpenHandle()) {
PWLAN_INTERFACE_INFO_LIST wlanInterfaces;
if (WlanEnumInterfaces( m_handle, NULL, &wlanInterfaces ) == ERROR_SUCCESS) {
for (int i = 0; i < wlanInterfaces->dwNumberOfItems; i++) {
if (wlanInterfaces->InterfaceInfo[i].isState == wlan_interface_state_connected) {
PWLAN_AVAILABLE_NETWORK_LIST wlanNetworks;
if (WlanGetAvailableNetworkList( m_handle, &wlanInterfaces->InterfaceInfo[i].InterfaceGuid, 0, NULL, &wlanNetworks ) == ERROR_SUCCESS) {
for (int j = 0; j < wlanNetworks->dwNumberOfItems; j++) {
if (_strnicmp( (const char *) wlanNetworks->Network[j].dot11Ssid.ucSSID, ssid, strlen(ssid) ) == 0) {
result = true;
break;
}
}
WlanFreeMemory( wlanNetworks );
}
}
}
WlanFreeMemory( wlanInterfaces );
}
}
return result;
}
bool Wlan::StartHostedNet( const char * ssid, const char * key, int maxPeers )
{
bool result = false;
if (OpenHandle()) {
WLAN_HOSTED_NETWORK_REASON reason = {};
WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS settings = {};
settings.dwMaxNumberOfPeers = maxPeers;
size_t ssidLen = strlen( ssid );
settings.hostedNetworkSSID.uSSIDLength = ssidLen;
memcpy( settings.hostedNetworkSSID.ucSSID, (void*) ssid, ssidLen );
if (WlanHostedNetworkSetProperty(
m_handle,
WLAN_HOSTED_NETWORK_OPCODE::wlan_hosted_network_opcode_connection_settings,
sizeof( WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS ),
&settings,
&reason,
NULL ) == ERROR_SUCCESS) {
size_t keyLength = strlen( key ) + 1;
if (WlanHostedNetworkSetSecondaryKey( m_handle, keyLength, (PUCHAR) key, TRUE, TRUE, &reason, NULL ) == ERROR_SUCCESS) {
result = (WlanHostedNetworkStartUsing( m_handle, &reason, NULL ) == ERROR_SUCCESS);
}
}
}
return result;
}
void Wlan::StopHostedNet()
{
if (OpenHandle()) {
WLAN_HOSTED_NETWORK_REASON reason = {};
WlanHostedNetworkStopUsing( m_handle, &reason, NULL );
}
}
bool Wlan::OpenHandle()
{
bool result = true;
DWORD version;
if (m_handle == NULL) {
if (WlanOpenHandle( 2, NULL, &version, &m_handle ) != ERROR_SUCCESS) {
m_handle = NULL;
result = false;
}
}
return result;
}
void Wlan::CloseHandle()
{
if (m_handle != NULL) {
WlanCloseHandle( m_handle, NULL );
m_handle = NULL;
}
}