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