Ejemplo n.º 1
0
            /// <summary>
            /// Gets the information of all profiles on this interface.
            /// </summary>
            /// <returns>The profiles information.</returns>
            public WlanProfileInfo[] GetProfiles()
            {
                Wlan.ThrowIfError(
                    WlanApi.WlanGetProfileList(client.clientHandle, info.interfaceGuid, IntPtr.Zero, out var profileListPtr));
                try
                {
                    var header =
                        (WlanProfileInfoListHeader)Marshal.PtrToStructure(profileListPtr, typeof(WlanProfileInfoListHeader));
                    WlanProfileInfo[] profileInfos = new WlanProfileInfo[header.numberOfItems];
                    long profileListIterator       = profileListPtr.ToInt64() + Marshal.SizeOf(header);

                    for (int i = 0; i < header.numberOfItems; ++i)
                    {
                        WlanProfileInfo profileInfo =
                            (WlanProfileInfo)Marshal.PtrToStructure(new IntPtr(profileListIterator), typeof(WlanProfileInfo));
                        profileInfos[i]      = profileInfo;
                        profileListIterator += Marshal.SizeOf(profileInfo);
                    }

                    return(profileInfos);
                }
                finally
                {
                    WlanApi.WlanFreeMemory(profileListPtr);
                }
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Gets the profile's XML specification.
            /// </summary>
            /// <param name="profileName">The name of the profile.</param>
            /// <param name="unencryptedPassword">Whether the password should be unencrypted in the returned XML. By default this is false and the password is left encrypted.</param>
            /// <returns>The XML document.</returns>
            public string GetProfileXml(string profileName, bool unencryptedPassword = true)
            {
                var flags = unencryptedPassword ? WlanProfileFlags.GetPlaintextKey : WlanProfileFlags.None;

                Wlan.ThrowIfError(
                    WlanApi.WlanGetProfile(
                        client.clientHandle, info.interfaceGuid, profileName, IntPtr.Zero, out var profileXmlPtr, out flags, out _));

                try
                {
                    return(Marshal.PtrToStringUni(profileXmlPtr));
                }
                finally
                {
                    WlanApi.WlanFreeMemory(profileXmlPtr);
                }
            }
Ejemplo n.º 3
0
        private bool StartListening()
        {
            try
            {
                var result = WlanApi.WlanOpenHandle(2, IntPtr.Zero, out uint currentVersion, out clientHandle);
                if (result != 0)
                {
                    return(false);
                }

                wlanNotifyDelegate = new WlanNotificationCallback(WlanNotifyHook);
                return(WlanRegisterNotification(clientHandle, WlanApi.WlanNotificationSource.MSM, true, wlanNotifyDelegate, IntPtr.Zero, IntPtr.Zero, out WlanApi.WlanNotificationSource prevSource) == 0);
            }
            catch (System.DllNotFoundException e)
            {
                ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Error);
            }

            return(false);
        }
Ejemplo n.º 4
0
        private void button_Start_Click(object sender, EventArgs e)
        {
            _WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS WHNCS = new _WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS
            {
                hostedNetworkSSID  = WlanApi.Create_DOT11_SSID(textBox_SSID.Text),
                dwMaxNumberOfPeers = 1
            };

            WlanApi.WlanOpenHandle(2, IntPtr.Zero, out CurVerison, out WlanHANDLE);
            WlanApi.WlanHostedNetworkInitSettings(WlanHANDLE, out FailReason, IntPtr.Zero);

            IntPtr data = Marshal.AllocHGlobal(Marshal.SizeOf(WHNCS));

            Marshal.StructureToPtr(WHNCS, data, false);

            WlanApi.WlanHostedNetworkSetProperty(WlanHANDLE,
                                                 _WLAN_HOSTED_NETWORK_OPCODE.wlan_hosted_network_opcode_connection_settings,
                                                 (uint)Marshal.SizeOf(WHNCS),
                                                 data, out FailReason, IntPtr.Zero);

            byte[] B = Encoding.Default.GetBytes(textBox_PassWord.Text);

            IntPtr key_Ptr = Marshal.AllocHGlobal(B.Length + 1);

            for (int c = 0; c < B.Length; c++)
            {
                Marshal.WriteByte(key_Ptr, c, B[c]);
            }

            Marshal.WriteByte(key_Ptr, B.Length, 0);
            WlanApi.WlanHostedNetworkSetSecondaryKey(WlanHANDLE,
                                                     (uint)B.Length + 1,
                                                     key_Ptr, true, false, out FailReason, IntPtr.Zero
                                                     );

            WlanApi.WlanHostedNetworkForceStart(WlanHANDLE, out FailReason, IntPtr.Zero);
            WlanApi.WlanCloseHandle(WlanHANDLE, IntPtr.Zero);
        }
Ejemplo n.º 5
0
 private void button_Stop_Click(object sender, EventArgs e)
 {
     WlanApi.WlanOpenHandle(2, IntPtr.Zero, out CurVerison, out WlanHANDLE);
     WlanApi.WlanHostedNetworkForceStop(WlanHANDLE, out FailReason, IntPtr.Zero);
     WlanApi.WlanCloseHandle(WlanHANDLE, IntPtr.Zero);
 }
Ejemplo n.º 6
0
 public Wlan(string url, string userName, string password)
 {
     _wlanApi = new WlanApi(url, userName, password);
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Callback function for reacting to changes in wlan connectivity.
        /// </summary>
        /// <param name="notificationData">WLAN notification data structure containing details about the notification.</param>
        /// <param name="context">Notification context, not used.</param>
        public void WlanNotifyHook(ref WlanApi.WlanNotificationData notificationData, IntPtr context)
        {
            // Do not perform wlan detection if user's unsecure network alert setting is turned off.
            if (!Manager.Settings.Network.UnsecureNetworkAlert)
            {
                return;
            }

            // If connected, ignore any and all messages
            if (Manager.MainWindowViewModel.Status == Models.ConnectionState.Protected)
            {
                return;
            }

            const int clientVersion = 2;

            if ((WlanApi.WlanNotificationMsm)notificationData.NotificationCode == WlanApi.WlanNotificationMsm.Connected)
            {
                if (WlanApi.WlanOpenHandle(clientVersion, IntPtr.Zero, out uint _, out IntPtr clientHandle) != 0)
                {
                    ErrorHandling.ErrorHandler.Handle("Can't open Wlan handle for unsecured network detection.", ErrorHandling.LogLevel.Error);
                    return;
                }

                var queryData       = IntPtr.Zero;
                var wlanQueryResult = WlanApi.WlanQueryInterface(clientHandle, new Guid(notificationData.InterfaceGuid.ToString()), WlanApi.WlanIntfOpcode.CurrentConnection, IntPtr.Zero, out _, ref queryData, IntPtr.Zero);

                if (wlanQueryResult == 0)
                {
                    var connectionData = Marshal.PtrToStructure <WlanApi.WlanConnectionAttributes>(queryData);

                    // Detect open network or WEP secured network
                    if (connectionData.WlanSecurityAttributes.Dot11AuthAlgorithm == WlanApi.Dot11AuthAlgorithm.Open ||
                        (
                            connectionData.WlanSecurityAttributes.Dot11CipherAlgorithm == WlanApi.Dot11CipherAlgorithm.Wep ||
                            connectionData.WlanSecurityAttributes.Dot11CipherAlgorithm == WlanApi.Dot11CipherAlgorithm.Wep40 ||
                            connectionData.WlanSecurityAttributes.Dot11CipherAlgorithm == WlanApi.Dot11CipherAlgorithm.Wep104)
                        )
                    {
                        var showNotification = true;
                        var bSsid            = connectionData.WlanAssociationAttributes.Dot11Bssid.ToString();
                        if (!accessPoints.ContainsKey(connectionData.WlanAssociationAttributes.Dot11Bssid.ToString()))
                        {
                            var newAp = new AccessPoint()
                            {
                                Ssid         = connectionData.ProfileName,
                                Bssid        = bSsid,
                                LastNotified = DateTime.UtcNow,
                            };
                            accessPoints[bSsid] = newAp;
                        }
                        else
                        {
                            if ((DateTime.UtcNow - accessPoints[bSsid].LastNotified).TotalSeconds < ProductConstants.InsecureWiFiTimeout)
                            {
                                showNotification = false;
                            }

                            var accessPoint = accessPoints[bSsid];
                            accessPoint.LastNotified = DateTime.UtcNow;
                            accessPoints[bSsid]      = accessPoint;
                        }

                        if (showNotification)
                        {
                            Manager.TrayIcon.ShowNotification(
                                Manager.TranslationService.GetString("wifi-unsecure-network-detected-msg"),
                                string.Concat(Manager.TranslationService.GetString("wifi-network-unsecure-msg", UI.Resources.Localization.TranslationService.Args("wifiName", connectionData.WlanAssociationAttributes.Dot11Ssid.ToString())), " ", Manager.TranslationService.GetString("wifi-you-should-turn-on-msg")),
                                NotificationArea.ToastIconType.Disconnected
                                );
                        }
                    }

                    if (queryData != IntPtr.Zero)
                    {
                        WlanApi.WlanFreeMemory(queryData);
                    }
                }
            }
        }
Ejemplo n.º 8
0
 private void StopListening()
 {
     WlanApi.WlanCloseHandle(clientHandle, IntPtr.Zero);
 }
Ejemplo n.º 9
0
 public WlanClient()
 {
     Wlan.ThrowIfError(
         WlanApi.WlanOpenHandle(
             Wlan.WLAN_CLIENT_VERSION_XP_SP2, IntPtr.Zero, out negotiatedVersion, out clientHandle));
 }