Пример #1
0
        internal static bool ConnectNetwork(Base.WlanClient client, Guid interfaceId, string profileName, BssType bssType)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType)));
        }
Пример #2
0
        internal static IEnumerable <BssNetworkPack> EnumerateBssNetworks(Base.WlanClient client, AvailableNetworkPack network)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                DOT11_SSID ssid;
                ssid.ucSSID      = network.Ssid.ToBytes();
                ssid.uSSIDLength = network.Ssid.GetLength();
                DOT11_BSS_TYPE bssTypes = BssTypeConverter.ConvertBack(network.BssType);

                var networkBssEntryList = Base.GetNetworkBssEntryList(container.Content.Handle, network.Interface.Id, ssid, bssTypes);

                foreach (var networkBssEntry in networkBssEntryList)
                {
                    if (!BssTypeConverter.TryConvert(networkBssEntry.dot11BssType, out BssType bssType))
                    {
                        continue;
                    }

                    //Debug.WriteLine("Interface: {0}, SSID: {1}, BSSID: {2}, Signal: {3} Link: {4}, Frequency: {5}",
                    //	interfaceInfo.Description,
                    //	networkBssEntry.dot11Ssid,
                    //	networkBssEntry.dot11Bssid,
                    //	networkBssEntry.lRssi,
                    //	networkBssEntry.uLinkQuality,
                    //	networkBssEntry.ulChCenterFrequency);


                    yield return(new BssNetworkPack(
                                     interfaceInfo: network.Interface,
                                     network: network,
                                     ssid: new NetworkIdentifier(networkBssEntry.dot11Ssid.ToBytes(), networkBssEntry.dot11Ssid.ToString(), networkBssEntry.dot11Ssid.uSSIDLength),
                                     bssType: bssType,
                                     bssid: new NetworkIdentifier(networkBssEntry.dot11Bssid.ToBytes(), networkBssEntry.dot11Bssid.ToString(), networkBssEntry.dot11Ssid.uSSIDLength),
                                     signalStrength: networkBssEntry.lRssi,
                                     linkQuality: (int)networkBssEntry.uLinkQuality,
                                     frequency: (int)networkBssEntry.ulChCenterFrequency,
                                     channel: DetectChannel(networkBssEntry.ulChCenterFrequency)));
                }
            }
        }
Пример #3
0
        internal static async Task <bool> ConnectNetworkAsync(Base.WlanNotificationClient client, Guid interfaceId, string profileName, BssType bssType, PhysicalAddress bssid, TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }
            if (timeout <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(timeout), "The timeout duration must be positive.");
            }

            using var container = new DisposableContainer <Base.WlanNotificationClient>(client);

            var tcs = new TaskCompletionSource <bool>();

            container.Content.NotificationReceived += (sender, data) =>
            {
                if (data.InterfaceGuid != interfaceId)
                {
                    return;
                }

                switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode)
                {
                case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_complete:
                case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_attempt_fail:
                    break;

                default:
                    return;
                }

                var connectionNotificationData = Marshal.PtrToStructure <WLAN_CONNECTION_NOTIFICATION_DATA>(data.pData);
                if (connectionNotificationData.strProfileName != profileName)
                {
                    return;
                }

                switch ((WLAN_NOTIFICATION_ACM)data.NotificationCode)
                {
                case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_complete:
                    Task.Run(() => tcs.TrySetResult(true));
                    break;

                case WLAN_NOTIFICATION_ACM.wlan_notification_acm_connection_attempt_fail:
                    // This notification will not always mean that a connection has failed.
                    // A connection consists of one or more connection attempts and this notification
                    // may be received zero or more times before the connection completes.
                    Task.Run(() => tcs.TrySetResult(false));
                    break;
                }
            };

            bool result;

            if (bssid is not null)
            {
                var dot11MacAddress = new DOT11_MAC_ADDRESS {
                    ucDot11MacAddress = bssid.GetAddressBytes()
                };
                result = Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType), dot11MacAddress);
            }
            else
            {
                result = Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType));
            }
            if (!result)
            {
                tcs.SetResult(false);
            }

            using (cancellationToken.Register(() => tcs.TrySetCanceled()))
            {
                var connectTask   = tcs.Task;
                var completedTask = await Task.WhenAny(connectTask, Task.Delay(timeout, cancellationToken));

                return((completedTask == connectTask) && connectTask.IsCompleted && connectTask.Result);
            }
        }
Пример #4
0
        internal static bool ConnectNetwork(Base.WlanClient client, Guid interfaceId, string profileName, BssType bssType, PhysicalAddress bssid = null)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException("The specified interface ID is invalid.", nameof(interfaceId));
            }
            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            using var container = new DisposableContainer <Base.WlanClient>(client);

            if (bssid is not null)
            {
                var dot11MacAddress = new DOT11_MAC_ADDRESS {
                    ucDot11MacAddress = bssid.GetAddressBytes()
                };
                return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType), dot11MacAddress));
            }
            else
            {
                return(Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType)));
            }
        }