-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWlanClient.cs
More file actions
1183 lines (1059 loc) · 40.7 KB
/
Copy pathWlanClient.cs
File metadata and controls
1183 lines (1059 loc) · 40.7 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// A SafeHandle-based wrapper for native memory allocated via <see cref="Marshal.AllocHGlobal(int)"/>.
/// Ensures proper cleanup even during exceptions.
/// </summary>
/// <remarks>
/// <para>
/// This class inherits from SafeHandle to leverage the CLR's critical finalization guarantees,
/// ensuring native memory is freed even if the finalizer thread is aborted.
/// </para>
/// <para>
/// <b>Important:</b> Only use this class for memory allocated with <see cref="Marshal.AllocHGlobal(int)"/>.
/// Do NOT use for memory returned by Windows APIs (e.g., WlanFreeMemory) as those require different deallocation.
/// </para>
/// </remarks>
internal sealed class SafeNativeMemory : SafeHandle
{
public SafeNativeMemory() : base(IntPtr.Zero, ownsHandle: true)
{
}
public SafeNativeMemory(int size) : base(IntPtr.Zero, ownsHandle: true)
{
if (size > 0)
{
SetHandle(Marshal.AllocHGlobal(size));
}
}
public override bool IsInvalid => handle == IntPtr.Zero;
public IntPtr Pointer => handle;
/// <summary>
/// Takes ownership of memory allocated via <see cref="Marshal.AllocHGlobal(int)"/>.
/// Frees any previously held memory before taking the new pointer.
/// </summary>
/// <param name="ptr">Pointer to memory allocated with <see cref="Marshal.AllocHGlobal(int)"/>. Must not be from other allocators.</param>
public void TakeOwnership(IntPtr ptr)
{
if (!IsInvalid)
{
Marshal.FreeHGlobal(handle);
}
SetHandle(ptr);
}
protected override bool ReleaseHandle()
{
if (!IsInvalid)
{
Marshal.FreeHGlobal(handle);
SetHandle(IntPtr.Zero);
}
return true;
}
}
/// <summary>
/// Provides a managed wrapper for Windows WLAN API operations.
/// </summary>
/// <remarks>
/// This class manages native WLAN resources and must be disposed when no longer needed.
/// It supports Wi-Fi interface enumeration, connection management, and network scanning.
/// </remarks>
internal sealed partial class WlanClient : IDisposable
{
private const uint ClientVersion = 2;
private const int ERROR_INVALID_STATE = 5023;
private static readonly TimeSpan ConnectTimeout = TimeSpan.FromSeconds(20);
private static readonly TimeSpan ScanTimeout = TimeSpan.FromSeconds(15);
private readonly IntPtr _clientHandle;
// Must keep a reference to the callback delegate to prevent GC from collecting it while native code holds a pointer.
private readonly WlanNotificationCallback _notificationCallback;
private readonly EventHandler _processExitHandler;
private readonly UnhandledExceptionEventHandler _unhandledExceptionHandler;
private TaskCompletionSource<bool>? _connectTcs;
private TaskCompletionSource<bool>? _scanTcs;
private int _disposed; // 0 = not disposed, 1 = disposed
public WlanClient()
{
ThrowOnError(WlanOpenHandle(ClientVersion, IntPtr.Zero, out var negotiatedVersion, out _clientHandle), "WlanOpenHandle");
try
{
_notificationCallback = OnNotification;
ThrowOnError(WlanRegisterNotification(_clientHandle, WLAN_NOTIFICATION_SOURCE.ACM, false, _notificationCallback, IntPtr.Zero, IntPtr.Zero, out _), "WlanRegisterNotification");
_processExitHandler = (_, __) => Dispose();
_unhandledExceptionHandler = (_, __) => Dispose();
AppDomain.CurrentDomain.ProcessExit += _processExitHandler;
AppDomain.CurrentDomain.UnhandledException += _unhandledExceptionHandler;
}
catch
{
WlanCloseHandle(_clientHandle, IntPtr.Zero);
throw;
}
}
~WlanClient()
{
Dispose();
}
/// <summary>
/// Enumerates all available Wi-Fi interfaces on the system.
/// </summary>
/// <returns>A read-only list of available Wi-Fi interfaces.</returns>
/// <exception cref="ObjectDisposedException">Thrown when the client has been disposed.</exception>
/// <exception cref="Win32Exception">Thrown when the native API call fails.</exception>
public IReadOnlyList<WLAN_INTERFACE_INFO> GetInterfaces()
{
EnsureNotDisposed();
ThrowOnError(WlanEnumInterfaces(_clientHandle, IntPtr.Zero, out var listPtr), "WlanEnumInterfaces");
try
{
var result = new List<WLAN_INTERFACE_INFO>();
var header = Marshal.PtrToStructure<WLAN_INTERFACE_INFO_LIST_HEADER>(listPtr);
var itemPtr = IntPtr.Add(listPtr, Marshal.SizeOf<WLAN_INTERFACE_INFO_LIST_HEADER>());
for (var i = 0; i < header.dwNumberOfItems; i++)
{
var info = Marshal.PtrToStructure<WLAN_INTERFACE_INFO>(itemPtr);
result.Add(info);
itemPtr = IntPtr.Add(itemPtr, Marshal.SizeOf<WLAN_INTERFACE_INFO>());
}
return result;
}
finally
{
WlanFreeMemory(listPtr);
}
}
/// <summary>
/// Asynchronously connects to a Wi-Fi network using the specified SSID and optional BSSID.
/// </summary>
/// <param name="interfaceId">The GUID of the Wi-Fi interface to use for the connection.</param>
/// <param name="ssid">The SSID of the network to connect to.</param>
/// <param name="bssid">The optional BSSID (MAC address) of the specific access point to connect to.</param>
/// <param name="cancellationToken">A token to cancel the connection attempt.</param>
/// <returns>A task that completes when the connection is established or fails.</returns>
/// <exception cref="ArgumentException">Thrown when the SSID is null or empty.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the client has been disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown when the connection attempt fails.</exception>
/// <exception cref="OperationCanceledException">Thrown when the operation is canceled or times out.</exception>
public async Task ConnectAsync(Guid interfaceId, string ssid, string? bssid, CancellationToken cancellationToken)
{
EnsureNotDisposed();
if (string.IsNullOrWhiteSpace(ssid))
{
throw new ArgumentException("SSID cannot be empty.", nameof(ssid));
}
var ssidStruct = WlanNative.CreateSsid(ssid);
using var ssidMem = new SafeNativeMemory(Marshal.SizeOf<DOT11_SSID>());
using var bssidMem = new SafeNativeMemory();
// Note: profileMem takes ownership of memory from Marshal.StringToHGlobalUni,
// which internally uses AllocHGlobal, so SafeNativeMemory can safely free it.
using var profileMem = new SafeNativeMemory();
TaskCompletionSource<bool>? tcs = null;
try
{
Marshal.StructureToPtr(ssidStruct, ssidMem.Pointer, false);
if (!string.IsNullOrWhiteSpace(bssid))
{
bssidMem.TakeOwnership(WlanNative.CreateBssidList(bssid!));
}
profileMem.TakeOwnership(Marshal.StringToHGlobalUni(ssid));
var parameters = new WLAN_CONNECTION_PARAMETERS_NATIVE(
WLAN_CONNECTION_MODE.Profile,
profileMem.Pointer,
ssidMem.Pointer,
bssidMem.Pointer,
DOT11_BSS_TYPE.Any,
0);
tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
// Set TCS before calling API to avoid race condition where notification arrives before TCS is set
var oldTcs = Interlocked.Exchange(ref _connectTcs, tcs);
oldTcs?.TrySetCanceled();
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeoutCts.CancelAfter(ConnectTimeout);
using var reg = timeoutCts.Token.Register(() => tcs.TrySetCanceled(timeoutCts.Token));
ThrowOnError(WlanConnect(_clientHandle, ref interfaceId, ref parameters, IntPtr.Zero), "WlanConnect");
var completed = await tcs.Task.ConfigureAwait(false);
if (!completed)
{
throw new InvalidOperationException("Connection attempt failed.");
}
}
finally
{
// Only clear if we set it (avoids clearing a newer TCS set by another call)
if (tcs != null)
{
Interlocked.CompareExchange(ref _connectTcs, null, tcs);
}
// Native memory is automatically freed by SafeNativeMemory.Dispose()
}
}
/// <summary>
/// Gets information about the current Wi-Fi connection on the specified interface.
/// </summary>
/// <param name="interfaceId">The GUID of the Wi-Fi interface to query.</param>
/// <returns>
/// A <see cref="CurrentConnectionInfo"/> containing connection details.
/// If not connected, <see cref="CurrentConnectionInfo.IsConnected"/> will be <c>false</c>.
/// </returns>
/// <exception cref="ObjectDisposedException">Thrown when the client has been disposed.</exception>
/// <exception cref="Win32Exception">Thrown when the native API call fails.</exception>
public CurrentConnectionInfo GetCurrentConnection(Guid interfaceId)
{
EnsureNotDisposed();
var result = WlanQueryInterface(
_clientHandle,
ref interfaceId,
WLAN_INTF_OPCODE.CurrentConnection,
IntPtr.Zero,
out var dataSize,
out var dataPtr,
out _);
// ERROR_INVALID_STATE means not connected
if (result == ERROR_INVALID_STATE)
{
return new CurrentConnectionInfo(false, null, null, 0);
}
if (result != 0)
{
throw new Win32Exception(result, $"WlanQueryInterface failed with error {result}");
}
try
{
var attrs = Marshal.PtrToStructure<WLAN_CONNECTION_ATTRIBUTES>(dataPtr);
var ssid = WlanNative.SsidToString(attrs.wlanAssociationAttributes.dot11Ssid);
var bssid = WlanNative.MacToString(attrs.wlanAssociationAttributes.dot11Bssid);
var signalQuality = attrs.wlanAssociationAttributes.wlanSignalQuality;
return new CurrentConnectionInfo(true, ssid, bssid, signalQuality);
}
finally
{
WlanFreeMemory(dataPtr);
}
}
/// <summary>
/// Triggers a Wi-Fi scan and returns the list of available networks.
/// </summary>
/// <param name="interfaceId">The GUID of the Wi-Fi interface to use for scanning.</param>
/// <param name="cancellationToken">A token to cancel the scan operation.</param>
/// <returns>A read-only list of available Wi-Fi networks grouped by SSID.</returns>
/// <exception cref="ObjectDisposedException">Thrown when the client has been disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown when the scan fails.</exception>
/// <exception cref="OperationCanceledException">Thrown when the operation is canceled or times out.</exception>
public async Task<IReadOnlyList<AvailableNetwork>> ScanAsync(Guid interfaceId, CancellationToken cancellationToken)
{
await PerformScanAsync(interfaceId, cancellationToken).ConfigureAwait(false);
return GetAvailableNetworks(interfaceId);
}
/// <summary>
/// Triggers a Wi-Fi scan and returns the list of BSS (Basic Service Set) entries.
/// </summary>
/// <param name="interfaceId">The GUID of the Wi-Fi interface to use for scanning.</param>
/// <param name="cancellationToken">A token to cancel the scan operation.</param>
/// <returns>A read-only list of BSS entries with detailed per-access-point information.</returns>
/// <exception cref="ObjectDisposedException">Thrown when the client has been disposed.</exception>
/// <exception cref="InvalidOperationException">Thrown when the scan fails.</exception>
/// <exception cref="OperationCanceledException">Thrown when the operation is canceled or times out.</exception>
public async Task<IReadOnlyList<BssEntry>> ScanBssAsync(Guid interfaceId, CancellationToken cancellationToken)
{
await PerformScanAsync(interfaceId, cancellationToken).ConfigureAwait(false);
return GetBssList(interfaceId);
}
private async Task PerformScanAsync(Guid interfaceId, CancellationToken cancellationToken)
{
EnsureNotDisposed();
TaskCompletionSource<bool>? tcs = null;
try
{
tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
// Set TCS before calling API to avoid race condition where notification arrives before TCS is set
var oldTcs = Interlocked.Exchange(ref _scanTcs, tcs);
oldTcs?.TrySetCanceled();
using var timeoutCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeoutCts.CancelAfter(ScanTimeout);
using var reg = timeoutCts.Token.Register(() => tcs.TrySetCanceled(timeoutCts.Token));
ThrowOnError(WlanScan(_clientHandle, ref interfaceId, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero), "WlanScan");
var completed = await tcs.Task.ConfigureAwait(false);
if (!completed)
{
throw new InvalidOperationException("Scan attempt failed.");
}
}
finally
{
// Only clear if we set it (avoids clearing a newer TCS set by another call)
if (tcs != null)
{
Interlocked.CompareExchange(ref _scanTcs, null, tcs);
}
}
}
private IReadOnlyList<AvailableNetwork> GetAvailableNetworks(Guid interfaceId)
{
ThrowOnError(WlanGetAvailableNetworkList(_clientHandle, ref interfaceId, 0, IntPtr.Zero, out var listPtr), "WlanGetAvailableNetworkList");
try
{
var header = Marshal.PtrToStructure<WLAN_AVAILABLE_NETWORK_LIST_HEADER>(listPtr);
var itemPtr = IntPtr.Add(listPtr, Marshal.SizeOf<WLAN_AVAILABLE_NETWORK_LIST_HEADER>());
var result = new List<AvailableNetwork>((int)header.dwNumberOfItems);
for (var i = 0; i < header.dwNumberOfItems; i++)
{
var net = Marshal.PtrToStructure<WLAN_AVAILABLE_NETWORK>(itemPtr);
result.Add(new AvailableNetwork(
WlanNative.SsidToString(net.dot11Ssid),
net.dot11BssType,
net.wlanSignalQuality,
net.bSecurityEnabled,
net.uNumberOfBssids,
net.dot11DefaultAuthAlgorithm,
net.dot11DefaultCipherAlgorithm));
itemPtr = IntPtr.Add(itemPtr, Marshal.SizeOf<WLAN_AVAILABLE_NETWORK>());
}
return result;
}
finally
{
WlanFreeMemory(listPtr);
}
}
private IReadOnlyList<BssEntry> GetBssList(Guid interfaceId)
{
ThrowOnError(WlanGetNetworkBssList(_clientHandle, ref interfaceId, IntPtr.Zero, DOT11_BSS_TYPE.Any, false, IntPtr.Zero, out var listPtr), "WlanGetNetworkBssList");
try
{
var header = Marshal.PtrToStructure<WLAN_BSS_LIST_HEADER>(listPtr);
var itemPtr = IntPtr.Add(listPtr, Marshal.SizeOf<WLAN_BSS_LIST_HEADER>());
var result = new List<BssEntry>((int)header.dwNumberOfItems);
for (var i = 0; i < header.dwNumberOfItems; i++)
{
var entry = Marshal.PtrToStructure<WLAN_BSS_ENTRY>(itemPtr);
result.Add(new BssEntry(
WlanNative.SsidToString(entry.dot11Ssid),
WlanNative.MacToString(entry.dot11Bssid),
entry.lRssi,
entry.uLinkQuality,
entry.ulChCenterFrequency,
entry.dot11BssType,
entry.dot11BssPhyType));
itemPtr = IntPtr.Add(itemPtr, Marshal.SizeOf<WLAN_BSS_ENTRY>());
}
return result;
}
finally
{
WlanFreeMemory(listPtr);
}
}
public void Dispose()
{
// Thread-safe disposal using Interlocked
if (Interlocked.CompareExchange(ref _disposed, 1, 0) != 0)
{
return;
}
GC.SuppressFinalize(this);
// Close handle first to stop receiving notifications
if (_clientHandle != IntPtr.Zero)
{
WlanCloseHandle(_clientHandle, IntPtr.Zero);
}
// Then unregister event handlers
AppDomain.CurrentDomain.ProcessExit -= _processExitHandler;
AppDomain.CurrentDomain.UnhandledException -= _unhandledExceptionHandler;
}
private void EnsureNotDisposed()
{
if (Volatile.Read(ref _disposed) != 0)
{
throw new ObjectDisposedException(nameof(WlanClient));
}
}
private void OnNotification(ref WLAN_NOTIFICATION_DATA notificationData, IntPtr context)
{
// Skip processing if already disposed to avoid logging during finalization
if (Volatile.Read(ref _disposed) != 0)
{
return;
}
if (notificationData.NotificationSource != WLAN_NOTIFICATION_SOURCE.ACM)
{
return;
}
var code = (WLAN_NOTIFICATION_ACM)notificationData.NotificationCode;
switch (code)
{
case WLAN_NOTIFICATION_ACM.ConnectionComplete:
var success = TryGetConnectionSucceeded(notificationData, out var reasonCode);
var connectTcs = Interlocked.Exchange(ref _connectTcs, null);
if (connectTcs != null)
{
if (!success)
{
Logger.Log($"Connection completed with failure reason {reasonCode} (0x{(uint)reasonCode:X}).");
}
else
{
Logger.Log("Connection completed successfully.");
}
connectTcs.TrySetResult(success);
}
break;
case WLAN_NOTIFICATION_ACM.ConnectionAttemptFail:
var hasReason = TryGetConnectionReason(notificationData, out var failReason);
var reasonText = hasReason ? $"{failReason} (0x{(uint)failReason:X})" : "unknown";
Logger.Log($"Connection attempt failed: {reasonText}.");
break;
case WLAN_NOTIFICATION_ACM.Disconnected:
Logger.Log("Connection Disconnected.");
break;
case WLAN_NOTIFICATION_ACM.ScanComplete:
var scanTcs = Interlocked.Exchange(ref _scanTcs, null);
if (scanTcs != null)
{
Logger.Log("Scan completed.");
scanTcs.TrySetResult(true);
}
break;
case WLAN_NOTIFICATION_ACM.ScanFail:
var scanFailTcs = Interlocked.Exchange(ref _scanTcs, null);
if (scanFailTcs != null)
{
Logger.Log("Scan failed.");
scanFailTcs.TrySetResult(false);
}
break;
}
}
private static bool TryGetConnectionSucceeded(WLAN_NOTIFICATION_DATA notificationData, out WLAN_REASON_CODE reasonCode)
{
var hasReason = TryGetConnectionReason(notificationData, out reasonCode);
return hasReason && reasonCode == WLAN_REASON_CODE.SUCCESS;
}
private static bool TryGetConnectionReason(WLAN_NOTIFICATION_DATA notificationData, out WLAN_REASON_CODE reasonCode)
{
reasonCode = WLAN_REASON_CODE.SUCCESS;
if (notificationData.pData == IntPtr.Zero)
{
return false;
}
var expectedSize = Marshal.SizeOf<WLAN_CONNECTION_NOTIFICATION_DATA>();
if (notificationData.dwDataSize < expectedSize)
{
return false;
}
var data = Marshal.PtrToStructure<WLAN_CONNECTION_NOTIFICATION_DATA>(notificationData.pData);
reasonCode = data.wlanReasonCode;
return true;
}
private static void ThrowOnError(int result, string operation)
{
if (result != 0)
{
throw new Win32Exception(result, $"{operation} failed with error {result}");
}
}
#region Native bindings
private const string WlanApi = "wlanapi.dll";
private delegate void WlanNotificationCallback(ref WLAN_NOTIFICATION_DATA notificationData, IntPtr context);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanOpenHandle(
uint dwClientVersion,
IntPtr pReserved,
out uint pdwNegotiatedVersion,
out IntPtr phClientHandle);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanCloseHandle(
IntPtr hClientHandle,
IntPtr pReserved);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanEnumInterfaces(
IntPtr hClientHandle,
IntPtr pReserved,
out IntPtr ppInterfaceList);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial void WlanFreeMemory(IntPtr pMemory);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanRegisterNotification(
IntPtr hClientHandle,
WLAN_NOTIFICATION_SOURCE dwNotifSource,
[MarshalAs(UnmanagedType.Bool)] bool bIgnoreDuplicate,
WlanNotificationCallback funcCallback,
IntPtr pCallbackContext,
IntPtr pReserved,
out WLAN_NOTIFICATION_SOURCE pdwPrevNotifSource);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanConnect(
IntPtr hClientHandle,
ref Guid pInterfaceGuid,
ref WLAN_CONNECTION_PARAMETERS_NATIVE pConnectionParameters,
IntPtr pReserved);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanScan(
IntPtr hClientHandle,
ref Guid pInterfaceGuid,
IntPtr pDot11Ssid,
IntPtr pIeData,
IntPtr pReserved);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanGetAvailableNetworkList(
IntPtr hClientHandle,
ref Guid pInterfaceGuid,
uint dwFlags,
IntPtr pReserved,
out IntPtr ppAvailableNetworkList);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanGetNetworkBssList(
IntPtr hClientHandle,
ref Guid pInterfaceGuid,
IntPtr pDot11Ssid,
DOT11_BSS_TYPE dot11BssType,
[MarshalAs(UnmanagedType.Bool)] bool bSecurityEnabled,
IntPtr pReserved,
out IntPtr ppWlanBssList);
[LibraryImport(WlanApi, SetLastError = true)]
private static partial int WlanQueryInterface(
IntPtr hClientHandle,
ref Guid pInterfaceGuid,
WLAN_INTF_OPCODE OpCode,
IntPtr pReserved,
out int pdwDataSize,
out IntPtr ppData,
out WLAN_OPCODE_VALUE_TYPE pWlanOpcodeValueType);
#endregion
}
internal static class WlanNative
{
public static DOT11_SSID CreateSsid(string ssid)
{
var ssidBytes = Encoding.UTF8.GetBytes(ssid);
if (ssidBytes.Length > 32)
{
throw new ArgumentException("SSID length must be 32 bytes or less when UTF-8 encoded.", nameof(ssid));
}
var buffer = new byte[32];
Array.Copy(ssidBytes, buffer, ssidBytes.Length);
return new DOT11_SSID((uint)ssidBytes.Length, buffer);
}
public static IntPtr CreateBssidList(string bssid)
{
ArgumentNullException.ThrowIfNull(bssid);
var macBytes = ParseMac(bssid);
// DOT11_BSSID_LIST structure:
// - NDIS_OBJECT_HEADER (4 bytes)
// - uNumOfEntries (4 bytes)
// - uTotalNumOfEntries (4 bytes)
// - BSSIDs[1] (6 bytes for one MAC address)
// Total: 18 bytes, but we use Marshal.SizeOf for proper alignment
var list = new DOT11_BSSID_LIST
{
Header = new NDIS_OBJECT_HEADER(0x80, 1, (ushort)Marshal.SizeOf<DOT11_BSSID_LIST>()), // Type 0x80 = NDIS_OBJECT_TYPE_DEFAULT
NumOfEntries = 1,
TotalNumOfEntries = 1,
BSSIDs = macBytes
};
var ptr = Marshal.AllocHGlobal(Marshal.SizeOf<DOT11_BSSID_LIST>());
Marshal.StructureToPtr(list, ptr, false);
return ptr;
}
public static string SsidToString(DOT11_SSID ssid)
{
if (ssid.SSIDLength == 0)
{
return string.Empty;
}
var length = (int)Math.Min(ssid.SSIDLength, (uint)ssid.SSID.Length);
var span = ssid.SSID.AsSpan(0, length);
// Validate UTF-8 encoding; fall back to Latin-1 (ISO-8859-1) if invalid
// Latin-1 preserves raw bytes as characters, which is better than ASCII for SSIDs with extended characters
if (System.Text.Unicode.Utf8.IsValid(span))
{
// For valid UTF-8, we need to decode properly as char count may differ from byte count
return Encoding.UTF8.GetString(span);
}
// Latin-1: one byte = one char, use string.Create to avoid intermediate allocations
return string.Create(length, ssid.SSID, static (chars, source) =>
{
for (var i = 0; i < chars.Length; i++)
{
chars[i] = (char)source[i];
}
});
}
public static string MacToString(byte[] address)
{
if (address is null || address.Length < 6)
{
return string.Empty;
}
return string.Create(17, address, (span, source) =>
{
span[0] = GetHex(source[0] >> 4);
span[1] = GetHex(source[0]);
span[2] = ':';
span[3] = GetHex(source[1] >> 4);
span[4] = GetHex(source[1]);
span[5] = ':';
span[6] = GetHex(source[2] >> 4);
span[7] = GetHex(source[2]);
span[8] = ':';
span[9] = GetHex(source[3] >> 4);
span[10] = GetHex(source[3]);
span[11] = ':';
span[12] = GetHex(source[4] >> 4);
span[13] = GetHex(source[4]);
span[14] = ':';
span[15] = GetHex(source[5] >> 4);
span[16] = GetHex(source[5]);
});
}
private static char GetHex(int value) => (char)((value & 0xF) > 9 ? 'A' + ((value & 0xF) - 10) : '0' + (value & 0xF));
private static byte[] ParseMac(ReadOnlySpan<char> bssid)
{
var bytes = new byte[6];
var byteIndex = 0;
var nibbleCount = 0;
byte currentByte = 0;
foreach (var c in bssid)
{
if (c == ':' || c == '-') continue;
var nibble = c switch
{
>= '0' and <= '9' => c - '0',
>= 'A' and <= 'F' => c - 'A' + 10,
>= 'a' and <= 'f' => c - 'a' + 10,
_ => throw new ArgumentException($"Invalid hex character '{c}' in BSSID.", nameof(bssid))
};
currentByte = (byte)((currentByte << 4) | nibble);
if (++nibbleCount == 2)
{
if (byteIndex >= 6)
{
throw new ArgumentException("BSSID must contain exactly 12 hexadecimal characters.", nameof(bssid));
}
bytes[byteIndex++] = currentByte;
currentByte = 0;
nibbleCount = 0;
}
}
if (byteIndex != 6)
{
throw new ArgumentException("BSSID must contain exactly 12 hexadecimal characters.", nameof(bssid));
}
return bytes;
}
}
#region Native types
// Naming convention:
// - Native Win32 structs/enums use SCREAMING_SNAKE_CASE (e.g., WLAN_INTERFACE_INFO) to match Windows SDK headers.
// - Managed wrapper types use PascalCase (e.g., AvailableNetwork, BssEntry).
/// <summary>
/// Represents a Wi-Fi network discovered during a scan, grouped by SSID.
/// </summary>
/// <param name="SSID">The Service Set Identifier (network name).</param>
/// <param name="BssType">The BSS type (Infrastructure, Independent, or Any).</param>
/// <param name="SignalQuality">Signal quality as a percentage (0-100).</param>
/// <param name="SecurityEnabled">Indicates whether security is enabled on this network.</param>
/// <param name="BssCount">The number of BSSIDs (access points) broadcasting this SSID.</param>
/// <param name="AuthAlgorithm">The default authentication algorithm used by this network.</param>
/// <param name="CipherAlgorithm">The default cipher algorithm used by this network.</param>
internal sealed record AvailableNetwork(
string SSID,
DOT11_BSS_TYPE BssType,
uint SignalQuality,
bool SecurityEnabled,
uint BssCount,
DOT11_AUTH_ALGORITHM AuthAlgorithm,
DOT11_CIPHER_ALGORITHM CipherAlgorithm);
/// <summary>
/// Represents a single BSS (Basic Service Set) entry, providing detailed per-access-point information.
/// </summary>
/// <param name="Ssid">The Service Set Identifier (network name).</param>
/// <param name="Bssid">The MAC address of the access point.</param>
/// <param name="Rssi">Received Signal Strength Indicator in dBm (typically -30 to -90).</param>
/// <param name="LinkQuality">Link quality as a percentage (0-100).</param>
/// <param name="FrequencyKhz">The channel center frequency in kHz.</param>
/// <param name="BssType">The BSS type (Infrastructure or Independent).</param>
/// <param name="PhyType">The PHY type indicating the Wi-Fi standard (e.g., HE for Wi-Fi 6).</param>
internal sealed record BssEntry(string Ssid, string Bssid, int Rssi, uint LinkQuality, uint FrequencyKhz, DOT11_BSS_TYPE BssType, DOT11_PHY_TYPE PhyType);
[StructLayout(LayoutKind.Sequential)]
internal readonly struct WLAN_INTERFACE_INFO_LIST_HEADER
{
public readonly uint dwNumberOfItems;
public readonly uint dwIndex;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WLAN_INTERFACE_INFO
{
public Guid InterfaceGuid;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strInterfaceDescription;
public WLAN_INTERFACE_STATE isState;
}
internal enum WLAN_INTERFACE_STATE
{
NotReady = 0,
Connected = 1,
AdHocNetworkFormed = 2,
Disconnecting = 3,
Disconnected = 4,
Associating = 5,
Discovering = 6,
Authenticating = 7
}
[StructLayout(LayoutKind.Sequential)]
internal struct WLAN_NOTIFICATION_DATA
{
public WLAN_NOTIFICATION_SOURCE NotificationSource;
public int NotificationCode;
public Guid InterfaceGuid;
public int dwDataSize;
public IntPtr pData;
}
[Flags]
internal enum WLAN_NOTIFICATION_SOURCE
{
None = 0,
Onex = 0x00000004,
ACM = 0x00000008,
MSM = 0x00000010,
Security = 0x00000020,
IHV = 0x00000040,
All = 0x0000FFFF
}
internal enum WLAN_NOTIFICATION_ACM
{
AutoconfEnabled = 1,
AutoconfDisabled,
BackgroundScanEnabled,
BackgroundScanDisabled,
BssTypeChange,
PowerSettingChange,
ScanComplete,
ScanFail,
ConnectionStart,
ConnectionComplete,
ConnectionAttemptFail,
FilterListChange,
InterfaceArrival,
InterfaceRemoval,
ProfileChange,
ProfileNameChange,
ProfilesExhausted,
NetworkNotAvailable,
NetworkAvailable,
Disconnecting,
Disconnected,
AdhocNetworkStateChange
}
internal enum WLAN_REASON_CODE : uint
{
SUCCESS = 0,
// General codes
UNKNOWN = 0x00010000,
RANGE_SIZE = 0x00010000,
BASE = 0x00010000,
// AC (Auto Configuration) codes
AC_BASE = 0x00020000,
AC_CONNECT_REASON_START = 0x00020000,
NETWORK_NOT_COMPATIBLE = 0x00020001,
PROFILE_NOT_COMPATIBLE = 0x00020002,
NO_AUTO_CONNECTION = 0x00020003,
NOT_VISIBLE = 0x00020004,
GP_DENIED = 0x00020005,
USER_DENIED = 0x00020006,
BSS_TYPE_NOT_ALLOWED = 0x00020007,
IN_FAILED_LIST = 0x00020008,
IN_BLOCKED_LIST = 0x00020009,
SSID_LIST_TOO_LONG = 0x0002000A,
CONNECT_CALL_FAIL = 0x0002000B,
SCAN_CALL_FAIL = 0x0002000C,
NETWORK_NOT_AVAILABLE = 0x0002000D,
PROFILE_CHANGED_OR_DELETED = 0x0002000E,
KEY_MISMATCH = 0x0002000F,
USER_NOT_RESPOND = 0x00020010,
AP_PROFILE_NOT_ALLOWED_FOR_CLIENT = 0x00020011,
AP_PROFILE_NOT_ALLOWED = 0x00020012,
// MSM (Media Specific Module) codes
MSM_BASE = 0x00030000,
MSM_CONNECT_REASON_START = 0x00030000,
UNSUPPORTED_SECURITY_SET_BY_OS = 0x00030001,
UNSUPPORTED_SECURITY_SET = 0x00030002,
BSS_TYPE_UNMATCH = 0x00030003,
PHY_TYPE_UNMATCH = 0x00030004,
DATARATE_UNMATCH = 0x00030005,
// 802.1x codes
MSMSEC_BASE = 0x00040000
}
internal enum WLAN_CONNECTION_MODE
{
Profile = 0,
TemporaryProfile,
DiscoverySecure,
DiscoveryUnsecure,
Auto,
Invalid
}
[StructLayout(LayoutKind.Sequential)]
internal readonly struct WLAN_CONNECTION_PARAMETERS_NATIVE
{
public readonly WLAN_CONNECTION_MODE wlanConnectionMode;
public readonly IntPtr strProfile;
public readonly IntPtr pDot11Ssid;
public readonly IntPtr pDesiredBssidList;
public readonly DOT11_BSS_TYPE dot11BssType;
public readonly uint dwFlags;
public WLAN_CONNECTION_PARAMETERS_NATIVE(
WLAN_CONNECTION_MODE connectionMode,
IntPtr profile,
IntPtr ssid,
IntPtr bssidList,
DOT11_BSS_TYPE bssType,
uint flags)
{
wlanConnectionMode = connectionMode;
strProfile = profile;
pDot11Ssid = ssid;
pDesiredBssidList = bssidList;
dot11BssType = bssType;
dwFlags = flags;
}
}
internal enum DOT11_BSS_TYPE : uint
{
Infrastructure = 1,
Independent = 2,
Any = 3
}
[StructLayout(LayoutKind.Sequential)]
internal readonly struct WLAN_AVAILABLE_NETWORK_LIST_HEADER
{
public readonly uint dwNumberOfItems;
public readonly uint dwIndex;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WLAN_AVAILABLE_NETWORK
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strProfileName;
public DOT11_SSID dot11Ssid;
public DOT11_BSS_TYPE dot11BssType;
public uint uNumberOfBssids;
[MarshalAs(UnmanagedType.Bool)]
public bool bNetworkConnectable;
public uint wlanNotConnectableReason;
public uint uNumberOfPhyTypes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public DOT11_PHY_TYPE[] dot11PhyTypes;
[MarshalAs(UnmanagedType.Bool)]
public bool bMorePhyTypes;
public uint wlanSignalQuality;
[MarshalAs(UnmanagedType.Bool)]
public bool bSecurityEnabled;
public DOT11_AUTH_ALGORITHM dot11DefaultAuthAlgorithm;
public DOT11_CIPHER_ALGORITHM dot11DefaultCipherAlgorithm;
public uint dwFlags;
public uint dwReserved;
}
[StructLayout(LayoutKind.Sequential)]
internal readonly struct DOT11_SSID
{
public readonly uint SSIDLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public readonly byte[] SSID;
public DOT11_SSID(uint length, byte[] ssid)
{
SSIDLength = length;
SSID = ssid;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct WLAN_CONNECTION_NOTIFICATION_DATA
{
public WLAN_CONNECTION_MODE wlanConnectionMode;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string strProfileName;
public DOT11_SSID dot11Ssid;
public DOT11_BSS_TYPE dot11BssType;
[MarshalAs(UnmanagedType.Bool)]
public bool bSecurityEnabled;
public WLAN_REASON_CODE wlanReasonCode;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal readonly struct NDIS_OBJECT_HEADER
{
public readonly byte Type;
public readonly byte Revision;
public readonly ushort Size;
public NDIS_OBJECT_HEADER(byte type, byte revision, ushort size)
{
Type = type;
Revision = revision;