private void UpdateAvailableNetworks() { lock (@lock) { try { networks.Clear(); foreach (var accessPoint in wifi.GetAccessPoints()) { // The user may only connect to an already configured or connected wireless network! if (accessPoint.HasProfile || accessPoint.IsConnected) { networks.Add(ToNetwork(accessPoint)); } } NetworksChanged?.Invoke(); } catch (Exception e) { logger.Error("Failed to update available networks!", e); } } }
public bool UpdateNetworks() { // foreach (NetworkInterface adapter in Interfaces) // todo: get data rates etc... adapter.GetIPStatistics() if (!UpdateInterfaces) { return(false); } UpdateInterfaces = false; Dictionary <string, FirewallRule.Profiles> NetworkProfiles = new Dictionary <string, FirewallRule.Profiles>(); foreach (INetwork network in netMgr.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED).Cast <INetwork>()) { FirewallRule.Profiles FirewallProfile = FirewallRule.Profiles.Undefined; switch (network.GetCategory()) { case NLM_NETWORK_CATEGORY.NLM_NETWORK_CATEGORY_PRIVATE: FirewallProfile = FirewallRule.Profiles.Private; break; case NLM_NETWORK_CATEGORY.NLM_NETWORK_CATEGORY_PUBLIC: FirewallProfile = FirewallRule.Profiles.Public; break; case NLM_NETWORK_CATEGORY.NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED: FirewallProfile = FirewallRule.Profiles.Domain; break; } foreach (INetworkConnection connection in network.GetNetworkConnections().Cast <INetworkConnection>()) { string id = ("{" + connection.GetAdapterId().ToString() + "}").ToLower(); NetworkProfiles.Add(id, FirewallProfile); } } //DefaultProfile = App.engine.FirewallManager.GetCurrentProfiles(); AdapterInfoByIP.Clear(); //AppLog.Debug("ListingNetworks:"); Interfaces = NetworkInterface.GetAllNetworkInterfaces(); // this is a bit slow! foreach (NetworkInterface adapter in Interfaces) { try { //AppLog.Debug("{0} {1} {2} {3}", adapter.Description, adapter.Id, adapter.NetworkInterfaceType.ToString(), adapter.OperationalStatus.ToString()); string id = adapter.Id.ToLower(); AdapterInfo Info = new AdapterInfo(); if (!NetworkProfiles.TryGetValue(id, out Info.Profile)) { Info.Profile = DefaultProfile; } switch (adapter.NetworkInterfaceType) { case NetworkInterfaceType.Ethernet: case NetworkInterfaceType.GigabitEthernet: case NetworkInterfaceType.FastEthernetT: case NetworkInterfaceType.FastEthernetFx: case NetworkInterfaceType.Ethernet3Megabit: case NetworkInterfaceType.TokenRing: Info.Type = FirewallRule.Interfaces.Lan; break; case NetworkInterfaceType.Wireless80211: Info.Type = FirewallRule.Interfaces.Wireless; break; case NetworkInterfaceType.Ppp: Info.Type = FirewallRule.Interfaces.RemoteAccess; break; default: Info.Type = FirewallRule.Interfaces.All; break; } Info.Addresses = new List <UnicastIPAddressInformation>(); IPInterfaceProperties ip_info = adapter.GetIPProperties(); Info.GatewayAddresses = new List <IPAddress>(); foreach (var gw in ip_info.GatewayAddresses) { Info.GatewayAddresses.Add(gw.Address); } Info.DnsAddresses = ip_info.DnsAddresses; Info.DhcpServerAddresses = ip_info.DhcpServerAddresses; Info.WinsServersAddresses = ip_info.WinsServersAddresses; foreach (UnicastIPAddressInformation ip in adapter.GetIPProperties().UnicastAddresses) { Info.Addresses.Add(ip); // Sanitize IPv6 addresses IPAddress _ip = new IPAddress(ip.Address.GetAddressBytes()); //AppLog.Debug("{2}({5}) has IP {0}/{3}/{4} has profile {1}", ip.Address.ToString(), ((FirewallRule.Profiles)Info.Profile).ToString(), // adapter.Description, ip.IPv4Mask.ToString(), ip.PrefixLength.ToString(), adapter.NetworkInterfaceType.ToString()); if (!AdapterInfoByIP.ContainsKey(_ip)) { AdapterInfoByIP[_ip] = Info; } } } catch { } // in case a adapter becomes invalid justa fter the enumeration } //AppLog.Debug("+++"); NetworksChanged?.Invoke(this, new EventArgs()); return(true); }