Beispiel #1
0
        private static IPAddress GetAnswer()
        {
            try
            {
                var dnsClient = new DnsQuery
                {
                    Servers = new ArrayList {
                        new IPEndPoint(IPAddress.Parse(@"8.8.8.8"), 53)
                    },
                    Domain = @"www.google.com",
                };
                if (dnsClient.Send())
                {
                    if (dnsClient.Response.Answers[0] is Address ip)
                    {
                        return(ip.IP);
                    }
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #2
0
 private static IPAddress ResolveAddress(string hostname, IPEndPoint[] servers, bool ipv6)
 {
     try
     {
         Types[] types = ipv6 ? new Types[] { Types.AAAA, Types.A } : new Types[] { Types.A, Types.AAAA };
         for (int i = 0; i < types.Length; ++i)
         {
             DnsQuery dns = new DnsQuery(hostname, types[i]);
             dns.RecursionDesired = true;
             dns.Servers.AddRange(servers);
             if (dns.Send())
             {
                 int len = dns.Response.Answers.Count;
                 if (len > 0)
                 {
                     for (int j = 0; j < len; ++j)
                     {
                         if (((ResourceRecord)dns.Response.Answers[j]).Type != types[i])
                         {
                             continue;
                         }
                         return(((Address)dns.Response.Answers[j]).IP);
                     }
                 }
             }
         }
     }
     catch
     {
     }
     try
     {
         IPAddress[] ips = System.Net.Dns.GetHostAddresses(hostname);
         if (ips.Length <= 0)
         {
             return(null);
         }
         return(ips[0]);
     }
     catch (Exception)
     {
         return(null);
     }
 }
        public static IPAddress _QueryDns(string host, string dns_servers, bool IPv6_first = false)
        {
            IPAddress ret_ipAddress = null;

            {
                if (!string.IsNullOrEmpty(dns_servers))
                {
                    OpenDNS.Types[] types;
                    if (IPv6_first)
                    {
                        types = new Types[] { Types.AAAA, Types.A }
                    }
                    ;
                    else
                    {
                        types = new Types[] { Types.A, Types.AAAA }
                    };
                    string[]          _dns_server      = dns_servers.Split(',');
                    List <IPEndPoint> dns_server       = new List <IPEndPoint>();
                    List <IPEndPoint> local_dns_server = new List <IPEndPoint>();
                    foreach (string server_str in _dns_server)
                    {
                        IPAddress ipAddress = null;
                        string    server    = server_str.Trim(' ');
                        int       index     = server.IndexOf(':');
                        string    ip        = null;
                        string    port      = null;
                        if (index >= 0)
                        {
                            if (server.StartsWith("["))
                            {
                                int ipv6_end = server.IndexOf(']', 1);
                                if (ipv6_end >= 0)
                                {
                                    ip = server.Substring(1, ipv6_end - 1);

                                    index = server.IndexOf(':', ipv6_end);
                                    if (index == ipv6_end + 1)
                                    {
                                        port = server.Substring(index + 1);
                                    }
                                }
                            }
                            else
                            {
                                ip   = server.Substring(0, index);
                                port = server.Substring(index + 1);
                            }
                        }
                        else
                        {
                            index = server.IndexOf(' ');
                            if (index >= 0)
                            {
                                ip   = server.Substring(0, index);
                                port = server.Substring(index + 1);
                            }
                            else
                            {
                                ip = server;
                            }
                        }
                        if (ip != null && IPAddress.TryParse(ip, out ipAddress))
                        {
                            int i_port = 53;
                            if (port != null)
                            {
                                int.TryParse(port, out i_port);
                            }
                            dns_server.Add(new IPEndPoint(ipAddress, i_port));
                            //dns_server.Add(port == null ? ip : ip + " " + port);
                        }
                    }
                    for (int query_i = 0; query_i < types.Length; ++query_i)
                    {
                        DnsQuery dns = new DnsQuery(host, types[query_i]);
                        dns.RecursionDesired = true;
                        foreach (IPEndPoint server in dns_server)
                        {
                            dns.Servers.Add(server);
                        }
                        if (dns.Send())
                        {
                            int count = dns.Response.Answers.Count;
                            if (count > 0)
                            {
                                for (int i = 0; i < count; ++i)
                                {
                                    if (((ResourceRecord)dns.Response.Answers[i]).Type != types[query_i])
                                    {
                                        continue;
                                    }
                                    return(((OpenDNS.Address)dns.Response.Answers[i]).IP);
                                }
                            }
                        }
                    }
                }
                {
                    try
                    {
                        GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                        IAsyncResult        result   = callback.BeginInvoke(host, null, null);
                        if (result.AsyncWaitHandle.WaitOne(10000, true))
                        {
                            IPHostEntry ipHostEntry = callback.EndInvoke(result);
                            foreach (IPAddress ad in ipHostEntry.AddressList)
                            {
                                if (ad.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    return(ad);
                                }
                            }
                            foreach (IPAddress ad in ipHostEntry.AddressList)
                            {
                                return(ad);
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return(ret_ipAddress);
        }
Beispiel #4
0
        public static IPAddress QueryDns(string host, string dns_servers, bool IPv6_first = false)
        {
            IPAddress ret_ipAddress = null;

            {
                if (dns_servers != null && dns_servers.Length > 0)
                {
                    OpenDNS.Types[] types;
                    if (IPv6_first)
                    {
                        types = new Types[] { Types.AAAA, Types.A }
                    }
                    ;
                    else
                    {
                        types = new Types[] { Types.A, Types.AAAA }
                    };
                    string[]      _dns_server      = dns_servers.Split(',');
                    List <string> dns_server       = new List <string>();
                    List <string> local_dns_server = new List <string>();
                    foreach (string ip in _dns_server)
                    {
                        IPAddress ipAddress = null;
                        if (IPAddress.TryParse(ip, out ipAddress))
                        {
                            dns_server.Add(ip);
                        }
                    }
                    for (int query_i = 0; query_i < types.Length; ++query_i)
                    {
                        DnsQuery dns = new DnsQuery(host, types[query_i]);
                        dns.RecursionDesired = true;
                        foreach (string server in dns_server)
                        {
                            dns.Servers.Add(server);
                        }
                        if (dns.Send())
                        {
                            int count = dns.Response.Answers.Count;
                            if (count > 0)
                            {
                                for (int i = 0; i < count; ++i)
                                {
                                    if (((ResourceRecord)dns.Response.Answers[i]).Type != types[query_i])
                                    {
                                        continue;
                                    }
                                    return(((OpenDNS.Address)dns.Response.Answers[i]).IP);
                                }
                            }
                        }
                    }
                }
                {
                    try
                    {
                        GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                        IAsyncResult        result   = callback.BeginInvoke(host, null, null);
                        if (result.AsyncWaitHandle.WaitOne(5, true))
                        {
                            foreach (IPAddress ad in callback.EndInvoke(result).AddressList)
                            {
                                return(ad);
                                //if (ad.AddressFamily == AddressFamily.InterNetwork)
                                //{
                                //    return ad;
                                //}
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }
            return(ret_ipAddress);
        }
        private static IPAddress _QueryDns(string host, string dnsServers, bool IPv6_first = false)
        {
            if (!string.IsNullOrEmpty(dnsServers))
            {
                var types        = IPv6_first ? new[] { Types.AAAA, Types.A } : new[] { Types.A, Types.AAAA };
                var dnsServerStr = dnsServers.Split(',');
                var dnsServer    = new List <IPEndPoint>();
                foreach (var serverStr in dnsServerStr)
                {
                    var    server = serverStr.Trim(' ');
                    var    index  = server.IndexOf(':');
                    string ip     = null;
                    string port   = null;
                    if (index >= 0)
                    {
                        if (server.StartsWith("["))
                        {
                            var ipv6_end = server.IndexOf(']', 1);
                            if (ipv6_end >= 0)
                            {
                                ip = server.Substring(1, ipv6_end - 1);

                                index = server.IndexOf(':', ipv6_end);
                                if (index == ipv6_end + 1)
                                {
                                    port = server.Substring(index + 1);
                                }
                            }
                        }
                        else
                        {
                            ip   = server.Substring(0, index);
                            port = server.Substring(index + 1);
                        }
                    }
                    else
                    {
                        index = server.IndexOf(' ');
                        if (index >= 0)
                        {
                            ip   = server.Substring(0, index);
                            port = server.Substring(index + 1);
                        }
                        else
                        {
                            ip = server;
                        }
                    }

                    if (ip != null && IPAddress.TryParse(ip, out var ipAddress))
                    {
                        var iPort = 53;
                        if (port != null)
                        {
                            int.TryParse(port, out iPort);
                        }

                        dnsServer.Add(new IPEndPoint(ipAddress, iPort));
                    }
                }

                foreach (var type in types)
                {
                    var dns = new DnsQuery(host, type)
                    {
                        RecursionDesired = true
                    };
                    foreach (var server in dnsServer)
                    {
                        dns.Servers.Add(server);
                    }

                    if (dns.Send())
                    {
                        var count = dns.Response.Answers.Count;
                        if (count > 0)
                        {
                            for (var i = 0; i < count; ++i)
                            {
                                if (((ResourceRecord)dns.Response.Answers[i]).Type != type)
                                {
                                    continue;
                                }

                                return(((Address)dns.Response.Answers[i]).IP);
                            }
                        }
                    }
                }
            }

            try
            {
                var callback = new GetHostEntryHandler(Dns.GetHostEntry);
                var result   = callback.BeginInvoke(host, null, null);
                if (result.AsyncWaitHandle.WaitOne(10000, true))
                {
                    var ipHostEntry = callback.EndInvoke(result);
                    foreach (var ad in ipHostEntry.AddressList)
                    {
                        if (ad.AddressFamily == AddressFamily.InterNetwork)
                        {
                            return(ad);
                        }
                    }

                    foreach (var ad in ipHostEntry.AddressList)
                    {
                        return(ad);
                    }
                }
            }
            catch
            {
                // ignored
            }

            return(null);
        }