コード例 #1
0
ファイル: NetworkAdapter.cs プロジェクト: ewin66/winipchanger
        /// <summary>
        /// コンストラクタ
        /// </summary>
        /// <param name="adapter">Win32_NetworkAdapter</param>
        /// <param name="config">Win32_NetworkAdapterConfiguration</param>
        public NetworkAdapter(ManagementObject adapter, ManagementObject config) : this()
        {
            if (adapter == null)
            {
                throw new ArgumentNullException("adapter");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            Name          = (string)adapter["NetConnectionID"];
            IsDhcpEnabled = (bool)config["DHCPEnabled"];
            string tmpIPAddress = ((string[])config["IPAddress"]).FirstOrDefault();

            if (!string.IsNullOrWhiteSpace(tmpIPAddress))
            {
                IPAddress = IPAddress.Parse(tmpIPAddress);
            }
            string tmpSubnetMask = ((string[])config["IPSubnet"]).FirstOrDefault();

            if (!string.IsNullOrWhiteSpace(tmpSubnetMask))
            {
                SubnetMask = IPAddress.Parse(tmpSubnetMask);
            }
            string tmpDefaultGateway = config["DefaultIPGateway"] == null ? null : ((string[])config["DefaultIPGateway"]).FirstOrDefault();

            if (!string.IsNullOrWhiteSpace(tmpDefaultGateway) && tmpDefaultGateway != "0.0.0.0" && tmpDefaultGateway != tmpIPAddress)
            {
                DefaultGateway = IPAddress.Parse(tmpDefaultGateway);
            }
            string[] tmpDnsServers = config["DNSServerSearchOrder"] == null ? null : ((string[])config["DNSServerSearchOrder"]);
            if (tmpDnsServers != null && 0 < tmpDnsServers.Length)
            {
                foreach (var dns in tmpDnsServers.Select(dns => IPAddress.Parse(dns)))
                {
                    DnsServers.Add(dns);
                }
            }
        }
コード例 #2
0
        private List <Error> ReadDeviceDefinitionsFromDirectory(
            string deviceDefinitionsDirectory)
        {
            var errors = new List <Error>();
            var deviceDefinitionFiles = new List <string>();

            if (Directory.Exists(deviceDefinitionsDirectory))
            {
                deviceDefinitionFiles =
                    Directory.EnumerateFiles(
                        deviceDefinitionsDirectory
                        )
                    .Where(x => x.Contains(DeviceFileExtension))
                    .ToList();
            }
            else
            {
                errors.Add(
                    Error.DirectoryNotFoundError(deviceDefinitionsDirectory)
                    );
            }

            foreach (var file in deviceDefinitionFiles)
            {
                var fileContents = File.ReadAllText(file);

                var jDoc =
                    JsonDocument.Parse(fileContents);

                foreach (var item in jDoc.RootElement.EnumerateObject())
                {
                    try
                    {
                        switch (item.Name.ToLower())
                        {
                        case "clients":
                            AddNetworkNodes <Client>(item.Value);
                            break;

                        case "contentservers":
                            AddNetworkNodes <ContentServer>(item.Value);
                            break;

                        case "dnsservers":
                            AddNetworkNodes <DnsServer>(item.Value);
                            break;

                        case "switches":
                            AddNetworkNodes <Switch>(item.Value);
                            break;

                        default:
                            break;
                        }
                        ;
                    }
                    catch (Exception ex)
                    {
                        errors.Add(
                            Error.GeneralException(ex)
                            );
                    }
                }
            }

            return(errors);

            void AddNetworkNodes <T>(
                JsonElement array)
            {
                var settings =
                    new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true
                };

                foreach (var item in array.EnumerateArray())
                {
                    switch (typeof(T))
                    {
                    case var x when x == typeof(Client):
                        Clients.Add(
                            JsonSerializer.Deserialize <Client>(
                                item.GetRawText(),
                                settings
                                )
                            );
                        break;

                    case var x when x == typeof(ContentServer):
                        ContentServers.Add(
                            JsonSerializer.Deserialize <ContentServer>(
                                item.GetRawText(),
                                settings
                                )
                            );
                        break;

                    case var x when x == typeof(DnsServer):
                        DnsServers.Add(
                            JsonSerializer.Deserialize <DnsServer>(
                                item.GetRawText(),
                                settings
                                )
                            );
                        break;

                    case var x when x == typeof(Switch):
                        Switches.Add(
                            JsonSerializer.Deserialize <Switch>(
                                item.GetRawText(),
                                settings
                                )
                            );
                        break;

                    default:
                        break;
                    }
                }
            }
        }
コード例 #3
0
 public ResolverContext AddDnsServer(string ipAddress, int port = 53)
 {
     DnsServers.Add(new IPEndPoint(IPAddress.Parse(ipAddress), port));
     return(this);
 }
コード例 #4
0
        /// <summary>
        /// Set the ip address properties of the <see cref="NetworkConnectionProperties"/> instance.
        /// </summary>
        /// <param name="properties">Element of the type <see cref="IPInterfaceProperties"/>.</param>
        private void SetIpProperties(IPInterfaceProperties properties)
        {
            var ipList = properties.UnicastAddresses;

            foreach (var ip in ipList)
            {
                if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    IPv4     = ip.Address.ToString();
                    IPv4Mask = ip.IPv4Mask.ToString();
                }
                else if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    if (string.IsNullOrEmpty(IPv6Primary))
                    {
                        IPv6Primary = ip.Address.ToString();
                    }

                    if (ip.Address.IsIPv6LinkLocal)
                    {
                        IPv6LinkLocal = ip.Address.ToString();
                    }
                    else if (ip.Address.IsIPv6SiteLocal)
                    {
                        IPv6SiteLocal = ip.Address.ToString();
                    }
                    else if (ip.Address.IsIPv6UniqueLocal)
                    {
                        IPv6UniqueLocal = ip.Address.ToString();
                    }
                    else if (ip.SuffixOrigin == SuffixOrigin.Random)
                    {
                        IPv6Temporary = ip.Address.ToString();
                    }
                    else
                    {
                        IPv6Global = ip.Address.ToString();
                    }
                }
            }

            foreach (var ip in properties.GatewayAddresses)
            {
                Gateways.Add(ip.Address.ToString());
            }

            foreach (var ip in properties.DhcpServerAddresses)
            {
                DhcpServers.Add(ip.ToString());
            }

            foreach (var ip in properties.DnsAddresses)
            {
                DnsServers.Add(ip.ToString());
            }

            foreach (var ip in properties.WinsServersAddresses)
            {
                WinsServers.Add(ip.ToString());
            }
        }