Exemplo n.º 1
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)));
            }
        }
 /// <summary>
 /// Generate a service name that should be unique to the machine
 /// Avoid conflicts with other advertised services by including the MAC address
 /// Avoid conflicts with previous test cases by appending a unique integer
 /// </summary>
 /// <param name="macAddress"></param>
 /// <param name="prefix"></param>
 /// <returns></returns>
 public string GenerateUniqueServiceName(
     DOT11_MAC_ADDRESS macAddress,
     string prefix = "com.microsoft.kit"
     )
 {
     return(string.Format(
                CultureInfo.InvariantCulture,
                "{0}.{1}.{2}",
                prefix,
                macAddress.ToString().Replace(":", null),
                serviceNameSeed++
                ));
 }
Exemplo n.º 3
0
        public static string ConvertToString(this DOT11_MAC_ADDRESS mac)
        {
            var sb = new StringBuilder();

            sb.Append(mac.one.ConvertToHexString());
            sb.Append(":");
            sb.Append(mac.two.ConvertToHexString());
            sb.Append(":");
            sb.Append(mac.three.ConvertToHexString());
            sb.Append(":");
            sb.Append(mac.four.ConvertToHexString());
            sb.Append(":");
            sb.Append(mac.five.ConvertToHexString());
            sb.Append(":");
            sb.Append(mac.six.ConvertToHexString());

            return(sb.ToString());
        }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 5
0
 public static PhysicalAddress GetPhysicalAddress(this DOT11_MAC_ADDRESS mac)
 {
     return(new PhysicalAddress(mac.PhysicalAddress));
 }
Exemplo n.º 6
0
 public static string ConvertToString(this DOT11_MAC_ADDRESS mac)
 {
     return(BitConverter.ToString(mac.PhysicalAddress).Replace('-', ':'));
 }
Exemplo n.º 7
0
 internal NetworkIdentifier(DOT11_MAC_ADDRESS bssid) : this(bssid.ToBytes(), bssid.ToString())
 {
 }