internal static async Task <bool> SetIP(this AdapterData adapter, string[] ipAddress, string[] subnetMask, string[] gateway)
        {
            var adapterConfig = GetNetworkAdapter(adapter);

            if (adapterConfig != null)
            {
                var result = await Task.Run(() => adapterConfig.EnableStatic(ipAddress, subnetMask));

                if (result != 0)
                {
                    Show.Message(AdapterDataLoc.EnableStaticFailed, string.Format(AdapterDataLoc.ErrorMessage, result, Helpers.WMI.FormatMessage.GetMessage((int)result)));
                    return(false);
                }
                result = await Task.Run(() => adapterConfig.SetGateways(gateway, new ushort[] { 1 }));

                if (result != 0)
                {
                    Show.Message(AdapterDataLoc.SetGatewaysFailed, string.Format(AdapterDataLoc.ErrorMessage, result, Helpers.WMI.FormatMessage.GetMessage((int)result)));
                    return(false);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static string GetStatusText(this AdapterData adapter)
        {
            switch (adapter.networkAdapter.NetConnectionStatus)
            {
            case 0: return(Resources.AdapterDataModelLoc.Disconnected);

            case 1: return(Resources.AdapterDataModelLoc.Connecting);

            case 2: return(Resources.AdapterDataModelLoc.Connected);

            case 3: return(Resources.AdapterDataModelLoc.Disconnecting);

            case 4: return(Resources.AdapterDataModelLoc.HardwareNotPresent);

            case 5: return(Resources.AdapterDataModelLoc.HardwareDisabled);

            case 6: return(Resources.AdapterDataModelLoc.HardwareMalfunction);

            case 7: return(Resources.AdapterDataModelLoc.MediaDisconnected);

            case 8: return(Resources.AdapterDataModelLoc.Authenticating);

            case 9: return(Resources.AdapterDataModelLoc.AuthenticationSucceeded);

            case 10: return(Resources.AdapterDataModelLoc.AuthenticationFailed);

            case 11: return(Resources.AdapterDataModelLoc.InvalidAddress);

            case 12: return(Resources.AdapterDataModelLoc.CredentialsRequired);

            default:
                return(Resources.AdapterDataModelLoc.Unknown);
            }
        }
        internal static async Task <bool> RenewDhcp(this AdapterData adapter)
        {
            var adapterConfig = GetNetworkAdapter(adapter);

            if (adapterConfig != null)
            {
                var result = await Task.Run(() => adapterConfig.ReleaseDHCPLease());

                if (result != 0)
                {
                    Show.Message(AdapterDataLoc.RenewDHCPLeaseFailed, string.Format(AdapterDataLoc.ErrorMessage, result, Helpers.WMI.FormatMessage.GetMessage((int)result)));
                    return(false);
                }
                await Task.Delay(2000);

                result = await Task.Run(() => adapterConfig.RenewDHCPLease());

                if (result != 0)
                {
                    Show.Message(AdapterDataLoc.RenewDHCPLeaseFailed, string.Format(AdapterDataLoc.ErrorMessage, result, Helpers.WMI.FormatMessage.GetMessage((int)result)));
                    return(false);
                }
                return(true);
            }
            return(false);
        }
        internal static async Task ApplyLocation(this AdapterData adapter, Location.Location location)
        {
            if (location == null)
            {
                return;
            }
            bool result;

            if (location.DHCPEnabled)
            {
                await SetDHCP(adapter);

                return;
            }

            string[] IP     = new string[location.IPList.Count];
            string[] subNet = new string[location.IPList.Count];
            for (byte b = 0; b < location.IPList.Count(); b++)
            {
                IP[b]     = location.IPList[b].IP;
                subNet[b] = location.IPList[b].NetMask;
            }

            string[] gateWay = new string[location.Gateways.Count];
            if (gateWay.Any())
            {
                gateWay = new string[] { IP.FirstOrDefault() }
            }
            ;
            else
            {
                for (byte b = 0; b < location.Gateways.Count(); b++)
                {
                    gateWay[b] = location.Gateways[b].IP;
                }
            }

            result = await adapter.SetIP(IP, subNet, gateWay);

            if (!result)
            {
                return;
            }

            string[] dns = new string[location.DNS.Count];
            for (byte b = 0; b < location.DNS.Count(); b++)
            {
                dns[b] = location.DNS[b].IP;
            }
            if (!await adapter.SetDnsServers(new string[] { IP.FirstOrDefault() }))
            {
                return;
            }
            if (!await adapter.SetDnsServers(dns))
            {
                return;
            }
        }
        public AdapterDataModel(AdapterData adapter)
        {
            if (adapter == null)
            {
                return;
            }

            Update(adapter, null, null);
        }
        internal static async Task <bool> Activate(this AdapterData adapter)
        {
            var couldEnable = await adapter.networkAdapter.EnableAsync();

            if (couldEnable != 0)
            {
                Show.Message(AdapterDataLoc.ActivationFailed, string.Format(AdapterDataLoc.ErrorMessage, couldEnable, Helpers.WMI.FormatMessage.GetMessage((int)couldEnable)));
                return(false);
            }
            return(true);
        }
        internal static Location.Location ExtractConfig(this AdapterData adapter, string NewName)
        {
            var location = new Location.Location {
                Description = AdapterDataLoc.NewLocationDescription, ID = Settings.Default.GetNextID()
            };

            if (adapter.networkInterface == null)
            {
                return(location);
            }

            var properties = adapter.networkInterface.GetIPProperties();

            // DHCP Enabled:
            location.DHCPEnabled = properties.GetIPv4Properties().IsDhcpEnabled;

            location.IPList.Clear();
            foreach (var uniCast in properties.UnicastAddresses)
            {
                // Ignore loop-back addresses & IPv6
                if (!IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6 && uniCast.IPv4Mask != null)
                {
                    var newIp = new IPDefinition {
                        IP = uniCast.Address.ToString(), NetMask = uniCast.IPv4Mask.ToString()
                    };

                    location.IPList.Add(newIp);
                }
            }

            foreach (var gateWay in properties.GatewayAddresses)
            {
                location.Gateways.Add(new IPv4Address {
                    IP = gateWay.Address.ToString()
                });
            }

            foreach (var dns in properties.DnsAddresses)
            {
                location.DNS.Add(new IPv4Address {
                    IP = dns.ToString()
                });
            }

            location.Description = NewName;

            return(location);
        }
        internal static List <AdapterData> GetAdapters(bool GetOnlyPhysicalAdapters = true)
        {
            var data = new List <AdapterData>();

            var adapters   = NetworkAdapter.GetInstances().Cast <NetworkAdapter>().Where(z => !GetOnlyPhysicalAdapters || z.PhysicalAdapter);
            var interfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (var item in adapters)
            {
                var newAdapterData = new AdapterData {
                    networkAdapter = item, networkInterface = interfaces.FirstOrDefault(z => z.Id == item.GUID)
                };

                data.Add(newAdapterData);
            }
            return(data);
        }
        internal static async Task <bool> SetDnsServers(this AdapterData adapter, string[] dnsServers)
        {
            var adapterConfig = GetNetworkAdapter(adapter);

            if (adapterConfig != null)
            {
                var result = await Task.Run(() => adapterConfig.SetDNSServerSearchOrder(dnsServers));

                if (result != 0)
                {
                    Show.Message(AdapterDataLoc.SetDnsServersFailed, string.Format(AdapterDataLoc.ErrorMessage, result, Helpers.WMI.FormatMessage.GetMessage((int)result)));
                    return(false);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#10
0
        public void Update(AdapterData adapter, List <NetworkAdapter> adapters, List <NetworkInterface> interfaces)
        {
            try
            {
                // Refresh data
                if (adapters == null)
                {
                    adapters = NetworkAdapter.GetInstances().Cast <NetworkAdapter>().ToList();
                }
                if (interfaces == null)
                {
                    interfaces = NetworkInterface.GetAllNetworkInterfaces().ToList();
                }

                adapter.Update(adapters, interfaces);

                Name = adapter.networkAdapter.Name;
                Mac  = adapter.networkAdapter.MACAddress;

                Status   = adapter.GetStatusText();
                IsActive = adapter.networkAdapter.ConfigManagerErrorCode != NetworkAdapter.ConfigManagerErrorCodeValues.This_device_is_disabled_;

                HasAdapter = adapter.networkInterface != null;
                if (adapter.networkInterface == null)
                {
                    return;
                }

                var networkInterfaceIPProperties   = adapter.networkInterface.GetIPProperties();
                var networkInterfaceIPv4Properties = networkInterfaceIPProperties.GetIPv4Properties();

                if (adapter.networkAdapter.NetConnectionStatus == 2)
                {
                    Speed = (adapter.networkAdapter.Speed / (1000 * 1000)).ToString("F1") + " Mbps";
                }
                else
                {
                    Speed = null;
                }

                IsDhcpEnabled = networkInterfaceIPv4Properties.IsDhcpEnabled;
                WinsEnabled   = networkInterfaceIPv4Properties.UsesWins.ToActiveText();

                // Ignore loop-back addresses & IPv6
                var tempIp = string.Empty;
                foreach (var item in networkInterfaceIPProperties.UnicastAddresses
                         .Where(z => z.Address.AddressFamily == AddressFamily.InterNetwork)
                         .Where(z => !IPAddress.IsLoopback(z.Address))
                         .Where(z => z.IPv4Mask != null && z.Address != null))
                {
                    tempIp += string.Format(@"{0}\{1}{2}", item.Address, item.IPv4Mask, Environment.NewLine);
                }
                Ip = tempIp.Trim();

                var tempDnsServers = string.Empty;
                foreach (var item in networkInterfaceIPProperties.DnsAddresses.Where(z => z.AddressFamily == AddressFamily.InterNetwork))
                {
                    tempDnsServers += item + Environment.NewLine;
                }
                DnsServers = tempDnsServers.Trim();

                var tempGateways = string.Empty;
                foreach (var item in networkInterfaceIPProperties.GatewayAddresses)
                {
                    tempGateways += item.Address + Environment.NewLine;
                }
                Gateways = tempGateways.Trim();

                var TempDhcpServers = string.Empty;
                foreach (var item in networkInterfaceIPProperties.DhcpServerAddresses.Where(z => z.AddressFamily == AddressFamily.InterNetwork))
                {
                    TempDhcpServers += item + Environment.NewLine;
                }
                DhcpServers = TempDhcpServers.Trim();

                var tempWinsServers = string.Empty;
                foreach (var item in networkInterfaceIPProperties.WinsServersAddresses.Where(z => z.AddressFamily == AddressFamily.InterNetwork))
                {
                    tempWinsServers += item + Environment.NewLine;
                }
                WinsServers = tempWinsServers.Trim();

                var tempAnyCast = string.Empty;
                foreach (var item in networkInterfaceIPProperties.AnycastAddresses.Where(z => z.Address.AddressFamily == AddressFamily.InterNetwork))
                {
                    tempAnyCast += item.Address + Environment.NewLine;
                }
                AnyCast = tempAnyCast.Trim();

                var tempMulticast = string.Empty;
                foreach (var item in networkInterfaceIPProperties.MulticastAddresses.Where(z => z.Address.AddressFamily == AddressFamily.InterNetwork))
                {
                    tempMulticast += item.Address + Environment.NewLine;
                }
                Multicast = tempMulticast.Trim();
            }
            catch (Exception ex)
            {
                SimpleMessenger.Default.SendMessage("ErrorText", ex.Message);
            }
        }
 private static NetworkAdapterConfiguration GetNetworkAdapter(this AdapterData adapter)
 {
     return(NetworkAdapterConfiguration.GetInstances().Cast <NetworkAdapterConfiguration>().FirstOrDefault(z => z.InterfaceIndex == adapter.networkAdapter.InterfaceIndex));
 }