Heijden.DNS.Resolver.GetHostEntry C# (CSharp) Method

GetHostEntry() public method

Resolves an IP address to an System.Net.IPHostEntry instance.
public GetHostEntry ( IPAddress ip ) : IPHostEntry
ip System.Net.IPAddress An IP address.
return System.Net.IPHostEntry
        public IPHostEntry GetHostEntry(IPAddress ip)
        {
            DNSResponse response = Query(GetArpaFromIp(ip), DNSQType.PTR, QClass.IN, DEFAULT_TIMEOUT);
            if (response.RecordsPTR.Length > 0)
                return MakeEntry(response.RecordsPTR[0].PTRDNAME, DEFAULT_TIMEOUT);
            else
                return new IPHostEntry();
        }

Same methods

Resolver::GetHostEntry ( string hostNameOrAddress ) : IPHostEntry

Usage Example

Beispiel #1
0
        //private static readonly string openDnsResolver2 = "208.67.220.220";
        /// <summary>
        /// Resolves the IP address of the specified hostname.
        /// </summary>
        /// <param name="hostname">The hostname.</param>
        /// <returns>IPAddress instance.</returns>
        public IPAddress ResolveHostnameIP(string hostname)
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
            {
                throw new ApplicationException(Text.Error_NoNetwork);
            }

            Resolver resolver = new Resolver(openDnsResolver1);
            IPHostEntry host = resolver.GetHostEntry(hostname);

            if (host == null || host.AddressList == null || host.AddressList.Length <= 0)
            {
                throw new ApplicationException(string.Format(Text.Error_CantResolveHostnameIP, hostname));
            }

            IPAddress result = host
                .AddressList
                .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);

            if (result == null)
            {
                throw new ApplicationException(string.Format(Text.Error_CantResolveHostnameIP, hostname));
            }

            log.DebugFormat(Text.Debug_ResolvedHostname, hostname, result.ToString());

            return result;
        }