-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathWlanClient.cs
executable file
·259 lines (227 loc) · 9.94 KB
/
WlanClient.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#region Header
//
// Copyright (c) 2007-2010 MetaGeek, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion Header
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using MetaGeek.Diagnostics;
using MetaGeek.Diagnostics.Event;
namespace ManagedWifi
{
public class WlanClient
{
#region Fields
private readonly IntPtr _clientHandle;
private readonly Dictionary<Guid, WlanInterface> _ifaces = new Dictionary<Guid, WlanInterface>();
private uint _negotiatedVersion;
#endregion Fields
#region Properties
public WlanInterface[] Interfaces
{
get
{
IntPtr ptr;
WlanInterface[] interfaceArray2;
Wlan.ThrowIfError(Wlan.WlanEnumInterfaces(_clientHandle, IntPtr.Zero, out ptr));
try
{
Wlan.WlanInterfaceInfoListHeader structure = (Wlan.WlanInterfaceInfoListHeader)Marshal.PtrToStructure(ptr, typeof(Wlan.WlanInterfaceInfoListHeader));
long num = ptr.ToInt64() + Marshal.SizeOf(structure);
WlanInterface[] interfaceArray = new WlanInterface[structure.numberOfItems];
List<Guid> list = new List<Guid>();
for (int i = 0; i < structure.numberOfItems; i++)
{
Wlan.WlanInterfaceInfo info = (Wlan.WlanInterfaceInfo)Marshal.PtrToStructure(new IntPtr(num), typeof(Wlan.WlanInterfaceInfo));
num += Marshal.SizeOf(info);
list.Add(info.interfaceGuid);
WlanInterface interface2 = _ifaces.ContainsKey(info.interfaceGuid) ? _ifaces[info.interfaceGuid] : new WlanInterface(this, info);
interfaceArray[i] = interface2;
_ifaces[info.interfaceGuid] = interface2;
}
Queue<Guid> queue = new Queue<Guid>();
foreach (Guid guid in _ifaces.Keys)
{
if (!list.Contains(guid))
{
queue.Enqueue(guid);
}
}
while (queue.Count != 0)
{
Guid key = queue.Dequeue();
_ifaces.Remove(key);
}
interfaceArray2 = interfaceArray;
}
finally
{
Wlan.WlanFreeMemory(ptr);
}
return interfaceArray2;
}
}
public IntPtr ItsClientHandle
{
get { return _clientHandle; }
}
private Logger ItsLogger
{
get;
set;
}
private Wlan.WlanNotificationCallbackDelegate WlanNotificationCallback
{
get;
set;
}
#endregion Properties
#region Event Fields
public RegisteredEventHandler<InterfaceNotificationEventsArgs> InterfaceArrivedEvent = new RegisteredEventHandler<InterfaceNotificationEventsArgs>();
public RegisteredEventHandler<InterfaceNotificationEventsArgs> InterfaceRemovedEvent = new RegisteredEventHandler<InterfaceNotificationEventsArgs>();
#endregion Event Fields
#region Constructors
public WlanClient()
{
ItsLogger = new Logger(this);
try
{
Wlan.ThrowIfError(Wlan.WlanOpenHandle(1, IntPtr.Zero, out _negotiatedVersion, out _clientHandle));
WlanNotificationCallback = new Wlan.WlanNotificationCallbackDelegate(OnWlanNotification);
Wlan.WlanNotificationSource source;
Wlan.ThrowIfError(Wlan.WlanRegisterNotification(_clientHandle, Wlan.WlanNotificationSource.All, false, WlanNotificationCallback, IntPtr.Zero, IntPtr.Zero, out source));
}
catch (Win32Exception ex)
{
Wlan.WlanCloseHandle(_clientHandle, IntPtr.Zero);
ItsLogger.Warn(ex.Message);
throw;
}
}
~WlanClient()
{
Wlan.WlanCloseHandle(_clientHandle, IntPtr.Zero);
}
#endregion Constructors
#region Public Methods
public string GetStringForReasonCode(Wlan.WlanReasonCode reasonCode)
{
StringBuilder stringBuffer = new StringBuilder(0x400);
Wlan.ThrowIfError(Wlan.WlanReasonCodeToString(reasonCode, stringBuffer.Capacity, stringBuffer, IntPtr.Zero));
return stringBuffer.ToString();
}
#endregion Public Methods
#region Private Methods
private void OnWlanNotification(ref Wlan.WlanNotificationData notifyData, IntPtr context)
{
WlanInterface interface2 = _ifaces.ContainsKey(notifyData.interfaceGuid) ? _ifaces[notifyData.interfaceGuid] : null;
switch (notifyData.notificationSource)
{
case Wlan.WlanNotificationSource.Acm:
switch (notifyData.notificationCode)
{
case 8:
if (notifyData.dataSize >= Marshal.SizeOf(0))
{
Wlan.WlanReasonCode reasonCode = (Wlan.WlanReasonCode)Marshal.ReadInt32(notifyData.dataPtr);
if (interface2 != null)
{
interface2.OnWlanReason(notifyData, reasonCode);
}
}
goto Label_0194;
case 9:
case 10:
case 11:
case 20:
case 0x15:
{
Wlan.WlanConnectionNotificationData? nullable = ParseWlanConnectionNotification(ref notifyData);
if (nullable.HasValue && (interface2 != null))
{
interface2.OnWlanConnection(notifyData, nullable.Value);
}
goto Label_0194;
}
case 12:
case 15:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
goto Label_0194;
case 13:
InterfaceArrivedEvent.Raise(this, new InterfaceNotificationEventsArgs(notifyData.interfaceGuid));
goto Label_0194;
case 14:
InterfaceRemovedEvent.Raise(this, new InterfaceNotificationEventsArgs(notifyData.interfaceGuid));
goto Label_0194;
}
break;
case Wlan.WlanNotificationSource.Msm:
switch (notifyData.notificationCode)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 9:
case 10:
case 11:
case 12:
case 13:
{
Wlan.WlanConnectionNotificationData? nullable2 = ParseWlanConnectionNotification(ref notifyData);
if (nullable2.HasValue && (interface2 != null))
{
interface2.OnWlanConnection(notifyData, nullable2.Value);
}
goto Label_0194;
}
case 7:
case 8:
goto Label_0194;
}
goto Label_0194;
}
Label_0194:
if (interface2 != null)
{
interface2.OnWlanNotification(notifyData);
}
}
private Wlan.WlanConnectionNotificationData? ParseWlanConnectionNotification(ref Wlan.WlanNotificationData notifyData)
{
int num = Marshal.SizeOf(typeof(Wlan.WlanConnectionNotificationData));
if (notifyData.dataSize < num)
{
return null;
}
Wlan.WlanConnectionNotificationData data = (Wlan.WlanConnectionNotificationData)Marshal.PtrToStructure(notifyData.dataPtr, typeof(Wlan.WlanConnectionNotificationData));
if (data.wlanReasonCode == Wlan.WlanReasonCode.Success)
{
IntPtr ptr = new IntPtr(notifyData.dataPtr.ToInt64() + Marshal.OffsetOf(typeof(Wlan.WlanConnectionNotificationData), "profileXml").ToInt64());
data.profileXml = Marshal.PtrToStringUni(ptr);
}
return data;
}
#endregion Private Methods
}
}