-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interop.cs
1535 lines (1438 loc) · 53.4 KB
/
Interop.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
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;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Text;
using System.Diagnostics;
using System.ComponentModel;
namespace NativeWifi
{
public static class Wlan
{
#region P/Invoke API
public enum WlanIntfOpcode
{
AutoconfEnabled = 1,
BackgroundScanEnabled,
MediaStreamingMode,
RadioState,
BssType,
InterfaceState,
CurrentConnection,
ChannelNumber,
SupportedInfrastructureAuthCipherPairs,
SupportedAdhocAuthCipherPairs,
SupportedCountryOrRegionStringList,
CurrentOperationMode,
Statistics = 0x10000101,
RSSI,
SecurityStart = 0x20010000,
SecurityEnd = 0x2fffffff,
IhvStart = 0x30000000,
IhvEnd = 0x3fffffff
}
public enum WlanOpcodeValueType
{
QueryOnly = 0,
SetByGroupPolicy = 1,
SetByUser = 2,
Invalid = 3
}
public const uint WLAN_CLIENT_VERSION_XP_SP2 = 1;
public const uint WLAN_CLIENT_VERSION_LONGHORN = 2;
[DllImport("wlanapi.dll")]
public static extern int WlanOpenHandle(
[In] UInt32 clientVersion,
[In, Out] IntPtr pReserved,
[Out] out UInt32 negotiatedVersion,
[Out] out IntPtr clientHandle);
[DllImport("wlanapi.dll")]
public static extern int WlanCloseHandle(
[In] IntPtr clientHandle,
[In, Out] IntPtr pReserved);
[DllImport("wlanapi.dll")]
public static extern int WlanEnumInterfaces(
[In] IntPtr clientHandle,
[In, Out] IntPtr pReserved,
[Out] out IntPtr ppInterfaceList);
[DllImport("wlanapi.dll")]
public static extern int WlanQueryInterface(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] WlanIntfOpcode opCode,
[In, Out] IntPtr pReserved,
[Out] out int dataSize,
[Out] out IntPtr ppData,
[Out] out WlanOpcodeValueType wlanOpcodeValueType);
[DllImport("wlanapi.dll")]
public static extern int WlanSetInterface(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] WlanIntfOpcode opCode,
[In] uint dataSize,
[In] IntPtr pData,
[In, Out] IntPtr pReserved);
/// <param name="pDot11Ssid">Not supported on Windows XP SP2: must be a <c>null</c> reference.</param>
/// <param name="pIeData">Not supported on Windows XP SP2: must be a <c>null</c> reference.</param>
[DllImport("wlanapi.dll")]
public static extern int WlanScan(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] IntPtr pDot11Ssid,
[In] IntPtr pIeData,
[In, Out] IntPtr pReserved);
/// <summary>
/// Defines flags passed to <see cref="WlanGetAvailableNetworkList"/>.
/// </summary>
[Flags]
public enum WlanGetAvailableNetworkFlags
{
/// <summary>
/// Include all ad-hoc network profiles in the available network list, including profiles that are not visible.
/// </summary>
IncludeAllAdhocProfiles = 0x00000001,
/// <summary>
/// Include all hidden network profiles in the available network list, including profiles that are not visible.
/// </summary>
IncludeAllManualHiddenProfiles = 0x00000002
}
/// <summary>
/// The header of an array of information about available networks.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal struct WlanAvailableNetworkListHeader
{
/// <summary>
/// Contains the number of <see cref=""/> items following the header.
/// </summary>
public uint numberOfItems;
/// <summary>
/// The index of the current item. The index of the first item is 0.
/// </summary>
public uint index;
}
/// <summary>
/// Contains various flags for the network.
/// </summary>
[Flags]
public enum WlanAvailableNetworkFlags
{
/// <summary>
/// This network is currently connected.
/// </summary>
Connected = 0x00000001,
/// <summary>
/// There is a profile for this network.
/// </summary>
HasProfile = 0x00000002
}
/// <summary>
/// Contains information about an available wireless network.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct WlanAvailableNetwork
{
/// <summary>
/// Contains the profile name associated with the network.
/// If the network doesn't have a profile, this member will be empty.
/// If multiple profiles are associated with the network, there will be multiple entries with the same SSID in the visible network list. Profile names are case-sensitive.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string profileName;
/// <summary>
/// Contains the SSID of the visible wireless network.
/// </summary>
public Dot11Ssid dot11Ssid;
/// <summary>
/// Specifies whether the network is infrastructure or ad hoc.
/// </summary>
public Dot11BssType dot11BssType;
/// <summary>
/// Indicates the number of BSSIDs in the network.
/// </summary>
public uint numberOfBssids;
/// <summary>
/// Indicates whether the network is connectable or not.
/// </summary>
public bool networkConnectable;
/// <summary>
/// Indicates why a network cannot be connected to. This member is only valid when <see cref="networkConnectable"/> is <c>false</c>.
/// </summary>
public WlanReasonCode wlanNotConnectableReason;
/// <summary>
/// The number of PHY types supported on available networks.
/// The maximum value of this field is 8. If more than 8 PHY types are supported, <see cref="morePhyTypes"/> must be set to <c>true</c>.
/// </summary>
private uint numberOfPhyTypes;
/// <summary>
/// Contains an array of <see cref="Dot11PhyType"/> values that represent the PHY types supported by the available networks.
/// When <see cref="numberOfPhyTypes"/> is greater than 8, this array contains only the first 8 PHY types.
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
private Dot11PhyType[] dot11PhyTypes;
/// <summary>
/// Gets the <see cref="Dot11PhyType"/> values that represent the PHY types supported by the available networks.
/// </summary>
public Dot11PhyType[] Dot11PhyTypes
{
get
{
Dot11PhyType[] ret = new Dot11PhyType[numberOfPhyTypes];
Array.Copy(dot11PhyTypes, ret, numberOfPhyTypes);
return ret;
}
}
/// <summary>
/// Specifies if there are more than 8 PHY types supported.
/// When this member is set to <c>true</c>, an application must call <see cref="WlanClient.WlanInterface.GetNetworkBssList"/> to get the complete list of PHY types.
/// <see cref="WlanBssEntry.phyId"/> contains the PHY type for an entry.
/// </summary>
public bool morePhyTypes;
/// <summary>
/// A percentage value that represents the signal quality of the network.
/// This field contains a value between 0 and 100.
/// A value of 0 implies an actual RSSI signal strength of -100 dbm.
/// A value of 100 implies an actual RSSI signal strength of -50 dbm.
/// You can calculate the RSSI signal strength value for values between 1 and 99 using linear interpolation.
/// </summary>
public uint wlanSignalQuality;
/// <summary>
/// Indicates whether security is enabled on the network.
/// </summary>
public bool securityEnabled;
/// <summary>
/// Indicates the default authentication algorithm used to join this network for the first time.
/// </summary>
public Dot11AuthAlgorithm dot11DefaultAuthAlgorithm;
/// <summary>
/// Indicates the default cipher algorithm to be used when joining this network.
/// </summary>
public Dot11CipherAlgorithm dot11DefaultCipherAlgorithm;
/// <summary>
/// Contains various flags for the network.
/// </summary>
public WlanAvailableNetworkFlags flags;
/// <summary>
/// Reserved for future use. Must be set to NULL.
/// </summary>
uint reserved;
}
[DllImport("wlanapi.dll")]
public static extern int WlanGetAvailableNetworkList(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] WlanGetAvailableNetworkFlags flags,
[In, Out] IntPtr reservedPtr,
[Out] out IntPtr availableNetworkListPtr);
[Flags]
public enum WlanProfileFlags
{
/// <remarks>
/// The only option available on Windows XP SP2.
/// </remarks>
AllUser = 0,
GroupPolicy = 1,
User = 2
}
[DllImport("wlanapi.dll")]
public static extern int WlanSetProfile(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] WlanProfileFlags flags,
[In, MarshalAs(UnmanagedType.LPWStr)] string profileXml,
[In, Optional, MarshalAs(UnmanagedType.LPWStr)] string allUserProfileSecurity,
[In] bool overwrite,
[In] IntPtr pReserved,
[Out] out WlanReasonCode reasonCode);
/// <summary>
/// Defines the access mask of an all-user profile.
/// </summary>
[Flags]
public enum WlanAccess
{
/// <summary>
/// The user can view profile permissions.
/// </summary>
ReadAccess = 0x00020000 | 0x0001,
/// <summary>
/// The user has read access, and the user can also connect to and disconnect from a network using the profile.
/// </summary>
ExecuteAccess = ReadAccess | 0x0020,
/// <summary>
/// The user has execute access and the user can also modify and delete permissions associated with a profile.
/// </summary>
WriteAccess = ReadAccess | ExecuteAccess | 0x0002 | 0x00010000 | 0x00040000
}
/// <param name="flags">Not supported on Windows XP SP2: must be a <c>null</c> reference.</param>
[DllImport("wlanapi.dll")]
public static extern int WlanGetProfile(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In, MarshalAs(UnmanagedType.LPWStr)] string profileName,
[In] IntPtr pReserved,
[Out] out IntPtr profileXml,
[Out, Optional] out WlanProfileFlags flags,
[Out, Optional] out WlanAccess grantedAccess);
[DllImport("wlanapi.dll")]
public static extern int WlanGetProfileList(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] IntPtr pReserved,
[Out] out IntPtr profileList
);
[DllImport("wlanapi.dll")]
public static extern void WlanFreeMemory(IntPtr pMemory);
[DllImport("wlanapi.dll")]
public static extern int WlanReasonCodeToString(
[In] WlanReasonCode reasonCode,
[In] int bufferSize,
[In, Out] StringBuilder stringBuffer,
IntPtr pReserved
);
/// <summary>
/// Specifies where the notification comes from.
/// </summary>
[Flags]
public enum WlanNotificationSource
{
None = 0,
/// <summary>
/// All notifications, including those generated by the 802.1X module.
/// </summary>
All = 0X0000FFFF,
/// <summary>
/// Notifications generated by the auto configuration module.
/// </summary>
ACM = 0X00000008,
/// <summary>
/// Notifications generated by MSM.
/// </summary>
MSM = 0X00000010,
/// <summary>
/// Notifications generated by the security module.
/// </summary>
Security = 0X00000020,
/// <summary>
/// Notifications generated by independent hardware vendors (IHV).
/// </summary>
IHV = 0X00000040
}
/// <summary>
/// Indicates the type of an ACM (<see cref="WlanNotificationSource.ACM"/>) notification.
/// </summary>
/// <remarks>
/// The enumeration identifiers correspond to the native <c>wlan_notification_acm_</c> identifiers.
/// On Windows XP SP2, only the <c>ConnectionComplete</c> and <c>Disconnected</c> notifications are available.
/// </remarks>
public enum WlanNotificationCodeAcm
{
AutoconfEnabled = 1,
AutoconfDisabled,
BackgroundScanEnabled,
BackgroundScanDisabled,
BssTypeChange,
PowerSettingChange,
ScanComplete,
ScanFail,
ConnectionStart,
ConnectionComplete,
ConnectionAttemptFail,
FilterListChange,
InterfaceArrival,
InterfaceRemoval,
ProfileChange,
ProfileNameChange,
ProfilesExhausted,
NetworkNotAvailable,
NetworkAvailable,
Disconnecting,
Disconnected,
AdhocNetworkStateChange
}
/// <summary>
/// Indicates the type of an MSM (<see cref="WlanNotificationSource.MSM"/>) notification.
/// </summary>
/// <remarks>
/// The enumeration identifiers correspond to the native <c>wlan_notification_msm_</c> identifiers.
/// </remarks>
public enum WlanNotificationCodeMsm
{
Associating = 1,
Associated,
Authenticating,
Connected,
RoamingStart,
RoamingEnd,
RadioStateChange,
SignalQualityChange,
Disassociating,
Disconnected,
PeerJoin,
PeerLeave,
AdapterRemoval,
AdapterOperationModeChange
}
/// <summary>
/// Contains information provided when registering for notifications.
/// </summary>
/// <remarks>
/// Corresponds to the native <c>WLAN_NOTIFICATION_DATA</c> type.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public struct WlanNotificationData
{
/// <summary>
/// Specifies where the notification comes from.
/// </summary>
/// <remarks>
/// On Windows XP SP2, this field must be set to <see cref="WlanNotificationSource.None"/>, <see cref="WlanNotificationSource.All"/> or <see cref="WlanNotificationSource.ACM"/>.
/// </remarks>
public WlanNotificationSource notificationSource;
/// <summary>
/// Indicates the type of notification. The value of this field indicates what type of associated data will be present in <see cref="dataPtr"/>.
/// </summary>
public int notificationCode;
/// <summary>
/// Indicates which interface the notification is for.
/// </summary>
public Guid interfaceGuid;
/// <summary>
/// Specifies the size of <see cref="dataPtr"/>, in bytes.
/// </summary>
public int dataSize;
/// <summary>
/// Pointer to additional data needed for the notification, as indicated by <see cref="notificationCode"/>.
/// </summary>
public IntPtr dataPtr;
/// <summary>
/// Gets the notification code (in the correct enumeration type) according to the notification source.
/// </summary>
public object NotificationCode
{
get
{
if (notificationSource == WlanNotificationSource.MSM)
return (WlanNotificationCodeMsm)notificationCode;
else if (notificationSource == WlanNotificationSource.ACM)
return (WlanNotificationCodeAcm)notificationCode;
else
return notificationCode;
}
}
}
/// <summary>
/// Defines the callback function which accepts WLAN notifications.
/// </summary>
public delegate void WlanNotificationCallbackDelegate(ref WlanNotificationData notificationData, IntPtr context);
[DllImport("wlanapi.dll")]
public static extern int WlanRegisterNotification(
[In] IntPtr clientHandle,
[In] WlanNotificationSource notifSource,
[In] bool ignoreDuplicate,
[In] WlanNotificationCallbackDelegate funcCallback,
[In] IntPtr callbackContext,
[In] IntPtr reserved,
[Out] out WlanNotificationSource prevNotifSource);
/// <summary>
/// Defines connection parameter flags.
/// </summary>
[Flags]
public enum WlanConnectionFlags
{
/// <summary>
/// Connect to the destination network even if the destination is a hidden network. A hidden network does not broadcast its SSID. Do not use this flag if the destination network is an ad-hoc network.
/// <para>If the profile specified by <see cref="WlanConnectionParameters.profile"/> is not <c>null</c>, then this flag is ignored and the nonBroadcast profile element determines whether to connect to a hidden network.</para>
/// </summary>
HiddenNetwork = 0x00000001,
/// <summary>
/// Do not form an ad-hoc network. Only join an ad-hoc network if the network already exists. Do not use this flag if the destination network is an infrastructure network.
/// </summary>
AdhocJoinOnly = 0x00000002,
/// <summary>
/// Ignore the privacy bit when connecting to the network. Ignoring the privacy bit has the effect of ignoring whether packets are encryption and ignoring the method of encryption used. Only use this flag when connecting to an infrastructure network using a temporary profile.
/// </summary>
IgnorePrivacyBit = 0x00000004,
/// <summary>
/// Exempt EAPOL traffic from encryption and decryption. This flag is used when an application must send EAPOL traffic over an infrastructure network that uses Open authentication and WEP encryption. This flag must not be used to connect to networks that require 802.1X authentication. This flag is only valid when <see cref="WlanConnectionParameters.wlanConnectionMode"/> is set to <see cref="WlanConnectionMode.TemporaryProfile"/>. Avoid using this flag whenever possible.
/// </summary>
EapolPassthrough = 0x00000008
}
/// <summary>
/// Specifies the parameters used when using the <see cref="WlanConnect"/> function.
/// </summary>
/// <remarks>
/// Corresponds to the native <c>WLAN_CONNECTION_PARAMETERS</c> type.
/// </remarks>
[StructLayout(LayoutKind.Sequential)]
public struct WlanConnectionParameters
{
/// <summary>
/// Specifies the mode of connection.
/// </summary>
public WlanConnectionMode wlanConnectionMode;
/// <summary>
/// Specifies the profile being used for the connection.
/// The contents of the field depend on the <see cref="wlanConnectionMode"/>:
/// <list type="table">
/// <listheader>
/// <term>Value of <see cref="wlanConnectionMode"/></term>
/// <description>Contents of the profile string</description>
/// </listheader>
/// <item>
/// <term><see cref="WlanConnectionMode.Profile"/></term>
/// <description>The name of the profile used for the connection.</description>
/// </item>
/// <item>
/// <term><see cref="WlanConnectionMode.TemporaryProfile"/></term>
/// <description>The XML representation of the profile used for the connection.</description>
/// </item>
/// <item>
/// <term><see cref="WlanConnectionMode.DiscoverySecure"/>, <see cref="WlanConnectionMode.DiscoveryUnsecure"/> or <see cref="WlanConnectionMode.Auto"/></term>
/// <description><c>null</c></description>
/// </item>
/// </list>
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
public string profile;
/// <summary>
/// Pointer to a <see cref="Dot11Ssid"/> structure that specifies the SSID of the network to connect to.
/// This field is optional. When set to <c>null</c>, all SSIDs in the profile will be tried.
/// This field must not be <c>null</c> if <see cref="wlanConnectionMode"/> is set to <see cref="WlanConnectionMode.DiscoverySecure"/> or <see cref="WlanConnectionMode.DiscoveryUnsecure"/>.
/// </summary>
public IntPtr dot11SsidPtr;
/// <summary>
/// Pointer to a <see cref="Dot11BssidList"/> structure that contains the list of basic service set (BSS) identifiers desired for the connection.
/// </summary>
/// <remarks>
/// On Windows XP SP2, must be set to <c>null</c>.
/// </remarks>
public IntPtr desiredBssidListPtr;
/// <summary>
/// A <see cref="Dot11BssType"/> value that indicates the BSS type of the network. If a profile is provided, this BSS type must be the same as the one in the profile.
/// </summary>
public Dot11BssType dot11BssType;
/// <summary>
/// Specifies ocnnection parameters.
/// </summary>
/// <remarks>
/// On Windows XP SP2, must be set to 0.
/// </remarks>
public WlanConnectionFlags flags;
}
/// <summary>
/// The connection state of an ad hoc network.
/// </summary>
public enum WlanAdhocNetworkState
{
/// <summary>
/// The ad hoc network has been formed, but no client or host is connected to the network.
/// </summary>
Formed = 0,
/// <summary>
/// A client or host is connected to the ad hoc network.
/// </summary>
Connected = 1
}
[DllImport("wlanapi.dll")]
public static extern int WlanConnect(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] ref WlanConnectionParameters connectionParameters,
IntPtr pReserved);
[DllImport("wlanapi.dll")]
public static extern int WlanDeleteProfile(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In, MarshalAs(UnmanagedType.LPWStr)] string profileName,
IntPtr reservedPtr
);
[DllImport("wlanapi.dll")]
public static extern int WlanGetNetworkBssList(
[In] IntPtr clientHandle,
[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
[In] IntPtr dot11SsidInt,
[In] Dot11BssType dot11BssType,
[In] bool securityEnabled,
IntPtr reservedPtr,
[Out] out IntPtr wlanBssList
);
[StructLayout(LayoutKind.Sequential)]
internal struct WlanBssListHeader
{
internal uint totalSize;
internal uint numberOfItems;
}
/// <summary>
/// Contains information about a basic service set (BSS).
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct WlanBssEntry
{
/// <summary>
/// Contains the SSID of the access point (AP) associated with the BSS.
/// </summary>
public Dot11Ssid dot11Ssid;
/// <summary>
/// The identifier of the PHY on which the AP is operating.
/// </summary>
public uint phyId;
/// <summary>
/// Contains the BSS identifier.
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public byte[] dot11Bssid;
/// <summary>
/// Specifies whether the network is infrastructure or ad hoc.
/// </summary>
public Dot11BssType dot11BssType;
public Dot11PhyType dot11BssPhyType;
/// <summary>
/// The received signal strength in dBm.
/// </summary>
public int rssi;
/// <summary>
/// The link quality reported by the driver. Ranges from 0-100.
/// </summary>
public uint linkQuality;
/// <summary>
/// If 802.11d is not implemented, the network interface card (NIC) must set this field to TRUE. If 802.11d is implemented (but not necessarily enabled), the NIC must set this field to TRUE if the BSS operation complies with the configured regulatory domain.
/// </summary>
public bool inRegDomain;
/// <summary>
/// Contains the beacon interval value from the beacon packet or probe response.
/// </summary>
public ushort beaconPeriod;
/// <summary>
/// The timestamp from the beacon packet or probe response.
/// </summary>
public ulong timestamp;
/// <summary>
/// The host timestamp value when the beacon or probe response is received.
/// </summary>
public ulong hostTimestamp;
/// <summary>
/// The capability value from the beacon packet or probe response.
/// </summary>
public ushort capabilityInformation;
/// <summary>
/// The frequency of the center channel, in kHz.
/// </summary>
public uint chCenterFrequency;
/// <summary>
/// Contains the set of data transfer rates supported by the BSS.
/// </summary>
public WlanRateSet wlanRateSet;
/// <summary>
/// Offset of the information element (IE) data blob.
/// </summary>
public uint ieOffset;
/// <summary>
/// Size of the IE data blob, in bytes.
/// </summary>
public uint ieSize;
}
/// <summary>
/// Contains the set of supported data rates.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct WlanRateSet
{
/// <summary>
/// The length, in bytes, of <see cref="rateSet"/>.
/// </summary>
private uint rateSetLength;
/// <summary>
/// An array of supported data transfer rates.
/// If the rate is a basic rate, the first bit of the rate value is set to 1.
/// A basic rate is the data transfer rate that all stations in a basic service set (BSS) can use to receive frames from the wireless medium.
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 126)]
private ushort[] rateSet;
public ushort[] Rates
{
get
{
ushort[] rates = new ushort[rateSetLength / sizeof(ushort)];
Array.Copy(rateSet, rates, rates.Length);
return rates;
}
}
/// <summary>
/// CalculateS the data transfer rate in Mbps for an arbitrary supported rate.
/// </summary>
/// <param name="rate"></param>
/// <returns></returns>
public double GetRateInMbps(int rate)
{
return (rateSet[rate] & 0x7FFF) * 0.5;
}
}
/// <summary>
/// Represents an error occuring during WLAN operations which indicate their failure via a <see cref="WlanReasonCode"/>.
/// </summary>
public class WlanException : Exception
{
private WlanReasonCode reasonCode;
WlanException(WlanReasonCode reasonCode)
{
this.reasonCode = reasonCode;
}
/// <summary>
/// Gets the WLAN reason code.
/// </summary>
/// <value>The WLAN reason code.</value>
public WlanReasonCode ReasonCode
{
get { return reasonCode; }
}
/// <summary>
/// Gets a message that describes the reason code.
/// </summary>
/// <value></value>
/// <returns>The error message that explains the reason for the exception, or an empty string("").</returns>
public override string Message
{
get
{
StringBuilder sb = new StringBuilder(1024);
if (WlanReasonCodeToString(reasonCode, sb.Capacity, sb, IntPtr.Zero) == 0)
return sb.ToString();
else
return string.Empty;
}
}
}
// TODO: .NET-ify the WlanReasonCode enum (naming convention + docs).
/// <summary>
/// Specifies reasons for a failure of a WLAN operation.
/// </summary>
/// <remarks>
/// To get the WLAN API native reason code identifiers, prefix the identifiers with <c>WLAN_REASON_CODE_</c>.
/// </remarks>
public enum WlanReasonCode
{
Success = 0,
// general codes
UNKNOWN = 0x10000 + 1,
RANGE_SIZE = 0x10000,
BASE = 0x10000 + RANGE_SIZE,
// range for Auto Config
//
AC_BASE = 0x10000 + RANGE_SIZE,
AC_CONNECT_BASE = (AC_BASE + RANGE_SIZE / 2),
AC_END = (AC_BASE + RANGE_SIZE - 1),
// range for profile manager
// it has profile adding failure reason codes, but may not have
// connection reason codes
//
PROFILE_BASE = 0x10000 + (7 * RANGE_SIZE),
PROFILE_CONNECT_BASE = (PROFILE_BASE + RANGE_SIZE / 2),
PROFILE_END = (PROFILE_BASE + RANGE_SIZE - 1),
// range for MSM
//
MSM_BASE = 0x10000 + (2 * RANGE_SIZE),
MSM_CONNECT_BASE = (MSM_BASE + RANGE_SIZE / 2),
MSM_END = (MSM_BASE + RANGE_SIZE - 1),
// range for MSMSEC
//
MSMSEC_BASE = 0x10000 + (3 * RANGE_SIZE),
MSMSEC_CONNECT_BASE = (MSMSEC_BASE + RANGE_SIZE / 2),
MSMSEC_END = (MSMSEC_BASE + RANGE_SIZE - 1),
// AC network incompatible reason codes
//
NETWORK_NOT_COMPATIBLE = (AC_BASE + 1),
PROFILE_NOT_COMPATIBLE = (AC_BASE + 2),
// AC connect reason code
//
NO_AUTO_CONNECTION = (AC_CONNECT_BASE + 1),
NOT_VISIBLE = (AC_CONNECT_BASE + 2),
GP_DENIED = (AC_CONNECT_BASE + 3),
USER_DENIED = (AC_CONNECT_BASE + 4),
BSS_TYPE_NOT_ALLOWED = (AC_CONNECT_BASE + 5),
IN_FAILED_LIST = (AC_CONNECT_BASE + 6),
IN_BLOCKED_LIST = (AC_CONNECT_BASE + 7),
SSID_LIST_TOO_LONG = (AC_CONNECT_BASE + 8),
CONNECT_CALL_FAIL = (AC_CONNECT_BASE + 9),
SCAN_CALL_FAIL = (AC_CONNECT_BASE + 10),
NETWORK_NOT_AVAILABLE = (AC_CONNECT_BASE + 11),
PROFILE_CHANGED_OR_DELETED = (AC_CONNECT_BASE + 12),
KEY_MISMATCH = (AC_CONNECT_BASE + 13),
USER_NOT_RESPOND = (AC_CONNECT_BASE + 14),
// Profile validation errors
//
INVALID_PROFILE_SCHEMA = (PROFILE_BASE + 1),
PROFILE_MISSING = (PROFILE_BASE + 2),
INVALID_PROFILE_NAME = (PROFILE_BASE + 3),
INVALID_PROFILE_TYPE = (PROFILE_BASE + 4),
INVALID_PHY_TYPE = (PROFILE_BASE + 5),
MSM_SECURITY_MISSING = (PROFILE_BASE + 6),
IHV_SECURITY_NOT_SUPPORTED = (PROFILE_BASE + 7),
IHV_OUI_MISMATCH = (PROFILE_BASE + 8),
// IHV OUI not present but there is IHV settings in profile
IHV_OUI_MISSING = (PROFILE_BASE + 9),
// IHV OUI is present but there is no IHV settings in profile
IHV_SETTINGS_MISSING = (PROFILE_BASE + 10),
// both/conflict MSMSec and IHV security settings exist in profile
CONFLICT_SECURITY = (PROFILE_BASE + 11),
// no IHV or MSMSec security settings in profile
SECURITY_MISSING = (PROFILE_BASE + 12),
INVALID_BSS_TYPE = (PROFILE_BASE + 13),
INVALID_ADHOC_CONNECTION_MODE = (PROFILE_BASE + 14),
NON_BROADCAST_SET_FOR_ADHOC = (PROFILE_BASE + 15),
AUTO_SWITCH_SET_FOR_ADHOC = (PROFILE_BASE + 16),
AUTO_SWITCH_SET_FOR_MANUAL_CONNECTION = (PROFILE_BASE + 17),
IHV_SECURITY_ONEX_MISSING = (PROFILE_BASE + 18),
PROFILE_SSID_INVALID = (PROFILE_BASE + 19),
TOO_MANY_SSID = (PROFILE_BASE + 20),
// MSM network incompatible reasons
//
UNSUPPORTED_SECURITY_SET_BY_OS = (MSM_BASE + 1),
UNSUPPORTED_SECURITY_SET = (MSM_BASE + 2),
BSS_TYPE_UNMATCH = (MSM_BASE + 3),
PHY_TYPE_UNMATCH = (MSM_BASE + 4),
DATARATE_UNMATCH = (MSM_BASE + 5),
// MSM connection failure reasons, to be defined
// failure reason codes
//
// user called to disconnect
USER_CANCELLED = (MSM_CONNECT_BASE + 1),
// got disconnect while associating
ASSOCIATION_FAILURE = (MSM_CONNECT_BASE + 2),
// timeout for association
ASSOCIATION_TIMEOUT = (MSM_CONNECT_BASE + 3),
// pre-association security completed with failure
PRE_SECURITY_FAILURE = (MSM_CONNECT_BASE + 4),
// fail to start post-association security
START_SECURITY_FAILURE = (MSM_CONNECT_BASE + 5),
// post-association security completed with failure
SECURITY_FAILURE = (MSM_CONNECT_BASE + 6),
// security watchdog timeout
SECURITY_TIMEOUT = (MSM_CONNECT_BASE + 7),
// got disconnect from driver when roaming
ROAMING_FAILURE = (MSM_CONNECT_BASE + 8),
// failed to start security for roaming
ROAMING_SECURITY_FAILURE = (MSM_CONNECT_BASE + 9),
// failed to start security for adhoc-join
ADHOC_SECURITY_FAILURE = (MSM_CONNECT_BASE + 10),
// got disconnection from driver
DRIVER_DISCONNECTED = (MSM_CONNECT_BASE + 11),
// driver operation failed
DRIVER_OPERATION_FAILURE = (MSM_CONNECT_BASE + 12),
// Ihv service is not available
IHV_NOT_AVAILABLE = (MSM_CONNECT_BASE + 13),
// Response from ihv timed out
IHV_NOT_RESPONDING = (MSM_CONNECT_BASE + 14),
// Timed out waiting for driver to disconnect
DISCONNECT_TIMEOUT = (MSM_CONNECT_BASE + 15),
// An internal error prevented the operation from being completed.
INTERNAL_FAILURE = (MSM_CONNECT_BASE + 16),
// UI Request timed out.
UI_REQUEST_TIMEOUT = (MSM_CONNECT_BASE + 17),
// Roaming too often, post security is not completed after 5 times.
TOO_MANY_SECURITY_ATTEMPTS = (MSM_CONNECT_BASE + 18),
// MSMSEC reason codes
//
MSMSEC_MIN = MSMSEC_BASE,
// Key index specified is not valid
MSMSEC_PROFILE_INVALID_KEY_INDEX = (MSMSEC_BASE + 1),
// Key required, PSK present
MSMSEC_PROFILE_PSK_PRESENT = (MSMSEC_BASE + 2),
// Invalid key length
MSMSEC_PROFILE_KEY_LENGTH = (MSMSEC_BASE + 3),
// Invalid PSK length
MSMSEC_PROFILE_PSK_LENGTH = (MSMSEC_BASE + 4),
// No auth/cipher specified
MSMSEC_PROFILE_NO_AUTH_CIPHER_SPECIFIED = (MSMSEC_BASE + 5),
// Too many auth/cipher specified
MSMSEC_PROFILE_TOO_MANY_AUTH_CIPHER_SPECIFIED = (MSMSEC_BASE + 6),
// Profile contains duplicate auth/cipher
MSMSEC_PROFILE_DUPLICATE_AUTH_CIPHER = (MSMSEC_BASE + 7),
// Profile raw data is invalid (1x or key data)
MSMSEC_PROFILE_RAWDATA_INVALID = (MSMSEC_BASE + 8),
// Invalid auth/cipher combination
MSMSEC_PROFILE_INVALID_AUTH_CIPHER = (MSMSEC_BASE + 9),
// 802.1x disabled when it's required to be enabled
MSMSEC_PROFILE_ONEX_DISABLED = (MSMSEC_BASE + 10),
// 802.1x enabled when it's required to be disabled
MSMSEC_PROFILE_ONEX_ENABLED = (MSMSEC_BASE + 11),
MSMSEC_PROFILE_INVALID_PMKCACHE_MODE = (MSMSEC_BASE + 12),
MSMSEC_PROFILE_INVALID_PMKCACHE_SIZE = (MSMSEC_BASE + 13),
MSMSEC_PROFILE_INVALID_PMKCACHE_TTL = (MSMSEC_BASE + 14),
MSMSEC_PROFILE_INVALID_PREAUTH_MODE = (MSMSEC_BASE + 15),
MSMSEC_PROFILE_INVALID_PREAUTH_THROTTLE = (MSMSEC_BASE + 16),
// PreAuth enabled when PMK cache is disabled
MSMSEC_PROFILE_PREAUTH_ONLY_ENABLED = (MSMSEC_BASE + 17),
// Capability matching failed at network
MSMSEC_CAPABILITY_NETWORK = (MSMSEC_BASE + 18),
// Capability matching failed at NIC
MSMSEC_CAPABILITY_NIC = (MSMSEC_BASE + 19),
// Capability matching failed at profile
MSMSEC_CAPABILITY_PROFILE = (MSMSEC_BASE + 20),
// Network does not support specified discovery type
MSMSEC_CAPABILITY_DISCOVERY = (MSMSEC_BASE + 21),
// Passphrase contains invalid character
MSMSEC_PROFILE_PASSPHRASE_CHAR = (MSMSEC_BASE + 22),
// Key material contains invalid character
MSMSEC_PROFILE_KEYMATERIAL_CHAR = (MSMSEC_BASE + 23),
// Wrong key type specified for the auth/cipher pair
MSMSEC_PROFILE_WRONG_KEYTYPE = (MSMSEC_BASE + 24),
// "Mixed cell" suspected (AP not beaconing privacy, we have privacy enabled profile)
MSMSEC_MIXED_CELL = (MSMSEC_BASE + 25),
// Auth timers or number of timeouts in profile is incorrect
MSMSEC_PROFILE_AUTH_TIMERS_INVALID = (MSMSEC_BASE + 26),
// Group key update interval in profile is incorrect
MSMSEC_PROFILE_INVALID_GKEY_INTV = (MSMSEC_BASE + 27),
// "Transition network" suspected, trying legacy 802.11 security
MSMSEC_TRANSITION_NETWORK = (MSMSEC_BASE + 28),
// Key contains characters which do not map to ASCII
MSMSEC_PROFILE_KEY_UNMAPPED_CHAR = (MSMSEC_BASE + 29),
// Capability matching failed at profile (auth not found)
MSMSEC_CAPABILITY_PROFILE_AUTH = (MSMSEC_BASE + 30),
// Capability matching failed at profile (cipher not found)
MSMSEC_CAPABILITY_PROFILE_CIPHER = (MSMSEC_BASE + 31),
// Failed to queue UI request
MSMSEC_UI_REQUEST_FAILURE = (MSMSEC_CONNECT_BASE + 1),
// 802.1x authentication did not start within configured time
MSMSEC_AUTH_START_TIMEOUT = (MSMSEC_CONNECT_BASE + 2),
// 802.1x authentication did not complete within configured time
MSMSEC_AUTH_SUCCESS_TIMEOUT = (MSMSEC_CONNECT_BASE + 3),
// Dynamic key exchange did not start within configured time
MSMSEC_KEY_START_TIMEOUT = (MSMSEC_CONNECT_BASE + 4),
// Dynamic key exchange did not succeed within configured time
MSMSEC_KEY_SUCCESS_TIMEOUT = (MSMSEC_CONNECT_BASE + 5),
// Message 3 of 4 way handshake has no key data (RSN/WPA)
MSMSEC_M3_MISSING_KEY_DATA = (MSMSEC_CONNECT_BASE + 6),
// Message 3 of 4 way handshake has no IE (RSN/WPA)
MSMSEC_M3_MISSING_IE = (MSMSEC_CONNECT_BASE + 7),
// Message 3 of 4 way handshake has no Group Key (RSN)
MSMSEC_M3_MISSING_GRP_KEY = (MSMSEC_CONNECT_BASE + 8),
// Matching security capabilities of IE in M3 failed (RSN/WPA)
MSMSEC_PR_IE_MATCHING = (MSMSEC_CONNECT_BASE + 9),
// Matching security capabilities of Secondary IE in M3 failed (RSN)
MSMSEC_SEC_IE_MATCHING = (MSMSEC_CONNECT_BASE + 10),
// Required a pairwise key but AP configured only group keys
MSMSEC_NO_PAIRWISE_KEY = (MSMSEC_CONNECT_BASE + 11),
// Message 1 of group key handshake has no key data (RSN/WPA)
MSMSEC_G1_MISSING_KEY_DATA = (MSMSEC_CONNECT_BASE + 12),
// Message 1 of group key handshake has no group key
MSMSEC_G1_MISSING_GRP_KEY = (MSMSEC_CONNECT_BASE + 13),
// AP reset secure bit after connection was secured
MSMSEC_PEER_INDICATED_INSECURE = (MSMSEC_CONNECT_BASE + 14),
// 802.1x indicated there is no authenticator but profile requires 802.1x
MSMSEC_NO_AUTHENTICATOR = (MSMSEC_CONNECT_BASE + 15),
// Plumbing settings to NIC failed