-
Notifications
You must be signed in to change notification settings - Fork 83
/
AccessPoint.cs
194 lines (169 loc) · 4.22 KB
/
AccessPoint.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using SimpleWifi.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using SimpleWifi.Win32.Interop;
namespace SimpleWifi
{
public class AccessPoint
{
private WlanInterface _interface;
private WlanAvailableNetwork _network;
internal AccessPoint(WlanInterface interfac, WlanAvailableNetwork network)
{
_interface = interfac;
_network = network;
}
public string Name
{
get
{
return Encoding.ASCII.GetString(_network.dot11Ssid.SSID, 0, (int)_network.dot11Ssid.SSIDLength);
}
}
public uint SignalStrength
{
get
{
return _network.wlanSignalQuality;
}
}
/// <summary>
/// If the computer has a connection profile stored for this access point
/// </summary>
public bool HasProfile
{
get
{
try
{
return _interface.GetProfiles().Where(p => p.profileName == Name).Any();
}
catch
{
return false;
}
}
}
public bool IsSecure
{
get
{
return _network.securityEnabled;
}
}
public bool IsConnected
{
get
{
try
{
var a = _interface.CurrentConnection; // This prop throws exception if not connected, which forces me to this try catch. Refactor plix.
return a.profileName == _network.profileName;
}
catch
{
return false;
}
}
}
/// <summary>
/// Returns the underlying network object.
/// </summary>
internal WlanAvailableNetwork Network
{
get
{
return _network;
}
}
/// <summary>
/// Returns the underlying interface object.
/// </summary>
internal WlanInterface Interface
{
get
{
return _interface;
}
}
/// <summary>
/// Checks that the password format matches this access point's encryption method.
/// </summary>
public bool IsValidPassword(string password)
{
return PasswordHelper.IsValid(password, _network.dot11DefaultCipherAlgorithm);
}
/// <summary>
/// Connect synchronous to the access point.
/// </summary>
public bool Connect(AuthRequest request, bool overwriteProfile = false)
{
// No point to continue with the connect if the password is not valid if overwrite is true or profile is missing.
if (!request.IsPasswordValid && (!HasProfile || overwriteProfile))
return false;
// If we should create or overwrite the profile, do so.
if (!HasProfile || overwriteProfile)
{
if (HasProfile)
_interface.DeleteProfile(Name);
request.Process();
}
// TODO: Auth algorithm: IEEE80211_Open + Cipher algorithm: None throws an error.
// Probably due to connectionmode profile + no profile exist, cant figure out how to solve it though.
return _interface.ConnectSynchronously(WlanConnectionMode.Profile, _network.dot11BssType, Name, 6000);
}
/// <summary>
/// Connect asynchronous to the access point.
/// </summary>
public void ConnectAsync(AuthRequest request, bool overwriteProfile = false, Action<bool> onConnectComplete = null)
{
// TODO: Refactor -> Use async connect in wlaninterface.
ThreadPool.QueueUserWorkItem(new WaitCallback((o) => {
bool success = false;
try
{
success = Connect(request, overwriteProfile);
}
catch (Win32Exception)
{
success = false;
}
if (onConnectComplete != null)
onConnectComplete(success);
}));
}
public string GetProfileXML()
{
if (HasProfile)
return _interface.GetProfileXml(Name);
else
return string.Empty;
}
public void DeleteProfile()
{
try
{
if (HasProfile)
_interface.DeleteProfile(Name);
}
catch { }
}
public override sealed string ToString()
{
StringBuilder info = new StringBuilder();
info.AppendLine("Interface: " + _interface.InterfaceName);
info.AppendLine("Auth algorithm: " + _network.dot11DefaultAuthAlgorithm);
info.AppendLine("Cipher algorithm: " + _network.dot11DefaultCipherAlgorithm);
info.AppendLine("BSS type: " + _network.dot11BssType);
info.AppendLine("Connectable: " + _network.networkConnectable);
if (!_network.networkConnectable)
info.AppendLine("Reason to false: " + _network.wlanNotConnectableReason);
return info.ToString();
}
}
}