Exemple #1
0
        private ProfileDocument(XDocument root)
        {
            if (root == null)
            {
                return;
            }
            this.Root = root;

            Name = Root.Elements().First().Elements(XName.Get("name", Namespace)).FirstOrDefault()?.Value;

            var ssidElement    = Root.Descendants(XName.Get("SSID", Namespace)).FirstOrDefault();
            var ssidHexString  = ssidElement?.Descendants(XName.Get("hex", Namespace)).FirstOrDefault()?.Value;
            var ssidHexBytes   = HexadecimalStringConverter.ToBytes(ssidHexString);
            var ssidNameString = ssidElement?.Descendants(XName.Get("name", Namespace)).FirstOrDefault()?.Value;

            Ssid = new NetworkIdentifier(ssidHexBytes, ssidNameString);

            var connectionTypeString = Root.Descendants(XName.Get("connectionType", Namespace)).FirstOrDefault()?.Value;

            if (!BssTypeConverter.TryParse(connectionTypeString, out BssType bssType))
            {
                return;
            }
            this.BssType = bssType;

            AuthenticationString = Root.Descendants(XName.Get("authentication", Namespace)).FirstOrDefault()?.Value;
            if (!AuthenticationMethodConverter.TryParse(AuthenticationString, out AuthenticationMethod authentication))
            {
                return;
            }
            this.Authentication = authentication;

            EncryptionString = Root.Descendants(XName.Get("encryption", Namespace)).FirstOrDefault()?.Value;
            if (!EncryptionTypeConverter.TryParse(EncryptionString, out EncryptionType encryption))
            {
                return;
            }
            this.Encryption = encryption;

            //Debug.WriteLine("SSID: {0}, BssType: {1}, Authentication: {2}, Encryption: {3}",
            //	Ssid,
            //	BssType,
            //	Authentication,
            //	Encryption);

            _connectionModeElement = Root.Descendants(XName.Get("connectionMode", Namespace)).FirstOrDefault();
            if (_connectionModeElement == null)
            {
                return;
            }

            _autoSwitchElement = Root.Descendants(XName.Get("autoSwitch", Namespace)).FirstOrDefault();

            IsValid = true;
        }
Exemple #2
0
        internal static IEnumerable <BssNetworkPack> EnumerateBssNetworks(Base.WlanClient client)
        {
            using (var container = new DisposableContainer <Base.WlanClient>(client))
            {
                var interfaceInfoList = Base.GetInterfaceInfoList(container.Content.Handle);

                foreach (var interfaceInfo in interfaceInfoList.Select(x => new InterfaceInfo(x)))
                {
                    var networkBssEntryList = Base.GetNetworkBssEntryList(container.Content.Handle, interfaceInfo.Id);

                    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: interfaceInfo,
                                         ssid: new NetworkIdentifier(networkBssEntry.dot11Ssid.ToBytes(), networkBssEntry.dot11Ssid.ToString()),
                                         bssType: bssType,
                                         bssid: new NetworkIdentifier(networkBssEntry.dot11Bssid.ToBytes(), networkBssEntry.dot11Bssid.ToString()),
                                         signalStrength: networkBssEntry.lRssi,
                                         linkQuality: (int)networkBssEntry.uLinkQuality,
                                         frequency: (int)networkBssEntry.ulChCenterFrequency,
                                         channel: DetectChannel(networkBssEntry.ulChCenterFrequency)));
                    }
                }
            }
        }
Exemple #3
0
        internal static async Task <bool> ConnectNetworkAsync(Base.WlanNotificationClient client, Guid interfaceId, string profileName, BssType bssType, TimeSpan timeout, CancellationToken cancellationToken)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(nameof(interfaceId));
            }

            if (string.IsNullOrWhiteSpace(profileName))
            {
                throw new ArgumentNullException(nameof(profileName));
            }

            if (timeout <= TimeSpan.Zero)
            {
                throw new ArgumentException(nameof(timeout));
            }

            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;
                    }
                };

                var result = Base.Connect(container.Content.Handle, interfaceId, profileName, BssTypeConverter.ConvertBack(bssType));
                if (!result)
                {
                    tcs.SetResult(false);
                }

                var connectTask   = tcs.Task;
                var completedTask = await Task.WhenAny(connectTask, Task.Delay(timeout, cancellationToken));

                return((completedTask == connectTask) && connectTask.Result);
            }
        }
Exemple #4
0
        internal static bool ConnectNetwork(Base.WlanClient client, Guid interfaceId, string profileName, BssType bssType)
        {
            if (interfaceId == Guid.Empty)
            {
                throw new ArgumentException(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)));
            }
        }