Ejemplo n.º 1
0
        //private static object _SyncHostName = new object();
        public static string GetHostEntryName(string hostNameOrAddress)
        {
            foreach (var pair in _HostNames.ToArray())
            {
                if (pair.Key == hostNameOrAddress)
                {
                    return(pair.Value);
                }
            }

            var newHostName = "unresolve";

            try
            {
                GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                IAsyncResult        result   = callback.BeginInvoke(hostNameOrAddress, null, null);

                // Wait dns response certain amount of time
                if (result.AsyncWaitHandle.WaitOne(DnsWaitTimeSec * 1000, false))
                {
                    newHostName = callback.EndInvoke(result).HostName;
                }
                _HostNames.Add(new KeyValuePair <string, string>(hostNameOrAddress, newHostName));
                return(newHostName);
            }
            catch (Exception)
            {
                _HostNames.Add(new KeyValuePair <string, string>(hostNameOrAddress, newHostName));
                return(newHostName);
            }
        }
Ejemplo n.º 2
0
 private static string GetReverseDns(string ip, int timeout)
 {
     try
     {
         GetHostEntryHandler callback = Dns.GetHostEntry;
         var result = callback.BeginInvoke(ip, null, null);
         return(result.AsyncWaitHandle.WaitOne(timeout, false) ? callback.EndInvoke(result).HostName : ip);
     }
     catch (Exception)
     {
         return(ip);
     }
 }
Ejemplo n.º 3
0
 public static string GetReverseDNS(string ip, int timeout)
 {
     try
     {
         GetHostEntryHandler getHostEntryHandler = new GetHostEntryHandler(Dns.GetHostEntry);
         IAsyncResult        asyncResult         = getHostEntryHandler.BeginInvoke(ip, null, null);
         if (asyncResult.AsyncWaitHandle.WaitOne(timeout, false))
         {
             return(getHostEntryHandler.EndInvoke(asyncResult).HostName);
         }
         return(ip);
     }
     catch (Exception)
     {
         return(ip);
     }
 }
Ejemplo n.º 4
0
        private static string ResolveDNS(string server, int timeout)
        {
            try
            {
                GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                IAsyncResult        result   = callback.BeginInvoke(server, null, null);
                if (result.AsyncWaitHandle.WaitOne(timeout, false))
                {
                    return(callback.EndInvoke(result).AddressList[0].ToString());
                }
            }
            catch (Exception)
            {
                return(null);
            }

            return(null);
        }
Ejemplo n.º 5
0
        public static string GetReverseDNS(string ip, int timeout)
        {
            try
            {
                GetHostEntryHandler callback = Dns.GetHostEntry;

                IAsyncResult result = callback.BeginInvoke(ip, null, null);

                if (result.AsyncWaitHandle.WaitOne(timeout, false))
                {
                    return(callback.EndInvoke(result).HostName);
                }

                return(ip);
            }
            catch (Exception)
            {
                return(ip);
            }
        }
Ejemplo n.º 6
0
 public string GetReverseDNS(string ip, int timeout)
 {
     try
     {
         GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
         IAsyncResult result = callback.BeginInvoke(ip, null, null);
         if (result.AsyncWaitHandle.WaitOne(timeout, false))
         {
             return callback.EndInvoke(result).HostName;
         }
         else
         {
             return "n/a";
         }
     }
     catch (SocketException se)
     {
         return se.Message;
     }
 }
Ejemplo n.º 7
0
 public static string GetHostName(IPAddress ip, int timeout)
 {
     try
     {
         var callback = new GetHostEntryHandler(Dns.GetHostEntry);
         var result   = callback.BeginInvoke(ip.ToString(), null, null);
         if (result.AsyncWaitHandle.WaitOne(timeout, false))
         {
             return(callback.EndInvoke(result).HostName);
         }
         else
         {
             return(ip.ToString());
         }
     }
     catch (Exception)
     {
         return(ip.ToString());
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Performs reverse-DNS lookups
        /// </summary>
        /// <param name="szIPAddress"></param>
        /// <returns></returns>
        public static string dnsReverseLookup(string szIPAddress)
        {
            int dTimeoutMs = 50;

            try
            {
                GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                IAsyncResult        result   = callback.BeginInvoke(szIPAddress, null, null);
                if (result.AsyncWaitHandle.WaitOne(dTimeoutMs, false))
                {
                    return(callback.EndInvoke(result).HostName);
                }
                else
                {
                    return(szIPAddress);
                }
            }
            catch (Exception)
            {
                return(szIPAddress);
            }
        }
Ejemplo n.º 9
0
 public static string GetDomainNameIP(string DomainName, int timeout)
 {
     try
     {
         GetHostEntryHandler getHostEntryHandler = new GetHostEntryHandler(Dns.GetHostEntry);
         IAsyncResult        asyncResult         = getHostEntryHandler.BeginInvoke(new Uri(DomainName).DnsSafeHost, null, null);
         if (asyncResult.AsyncWaitHandle.WaitOne(timeout, false))
         {
             IPAddress iPAddress = Enumerable.FirstOrDefault(getHostEntryHandler.EndInvoke(asyncResult).AddressList);
             if (iPAddress != null)
             {
                 return(iPAddress.ToString());
             }
             return("");
         }
         return("");
     }
     catch (Exception)
     {
         return("");
     }
 }
Ejemplo n.º 10
0
        // Together with GetHostName can generate duplications in cache.
        // It decrease efficiency of cache, but not generate an error.
        public static bool FillHostNameCache(string hostNameOrAddress)
        {
            foreach (var pair in _HostNames.ToArray())
            {
                if (pair.Key == hostNameOrAddress)
                {
                    return(false);
                }
            }

            var newHostName = "unresolve";

            try
            {
                // Waiting time in sec
                int DnsWaitTimeSec           = 3;
                GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                IAsyncResult        result   = callback.BeginInvoke(hostNameOrAddress, null, null);

                // Wait dns response while 1sec
                if (result.AsyncWaitHandle.WaitOne(DnsWaitTimeSec * 1000, false))
                {
                    newHostName = callback.EndInvoke(result).HostName;
                    _HostNames.Add(new KeyValuePair <string, string>(hostNameOrAddress, newHostName));
                    return(true);
                }
                else
                {
                    _HostNames.Add(new KeyValuePair <string, string>(hostNameOrAddress, newHostName));
                    return(false);
                }
            }
            catch (Exception)
            {
                _HostNames.Add(new KeyValuePair <string, string>(hostNameOrAddress, newHostName));
                return(false);
            }
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        private static string ResolveDNS(string server, int timeout)
        {
            try
            {
                GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
                IAsyncResult result = callback.BeginInvoke(server, null, null);
                if (result.AsyncWaitHandle.WaitOne(timeout, false))
                {
                    return callback.EndInvoke(result).AddressList[0].ToString();
                }
            }
            catch (Exception)
            {
                return null;
            }

            return null;
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        private static IPAddress Query(string host, string dnsServers, bool IPv6_first = false)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(dnsServers))
                {
                    var client = new LookupClient(ToIpEndPoints(dnsServers))
                    {
                        UseCache = false
                    };
                    if (IPv6_first)
                    {
                        var r = client.Query(host, QueryType.AAAA).Answers.OfType <AaaaRecord>().FirstOrDefault()
                                ?.Address;
                        if (r != null)
                        {
                            return(r);
                        }

                        r = client.Query(host, QueryType.A).Answers.OfType <ARecord>().FirstOrDefault()?.Address;
                        if (r != null)
                        {
                            return(r);
                        }
                    }
                    else
                    {
                        var r = client.Query(host, QueryType.A).Answers.OfType <ARecord>().FirstOrDefault()?.Address;
                        if (r != null)
                        {
                            return(r);
                        }

                        r = client.Query(host, QueryType.AAAA).Answers.OfType <AaaaRecord>().FirstOrDefault()?.Address;
                        if (r != null)
                        {
                            return(r);
                        }
                    }
                }
            }
            catch
            {
                // ignored
            }

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

                    var type = IPv6_first ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;

                    foreach (var ad in ipHostEntry.AddressList)
                    {
                        if (ad.AddressFamily == type)
                        {
                            return(ad);
                        }
                    }

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

            return(null);
        }