What does Dns.GetHostEntry Method(String) actually do?

30,100

Solution 1

Dns.GetHostEntry is built on top of the Windows API and does not use the DNS protocol directly. If IPv6 is enabled it will call getaddrinfo. Otherwise it will call gethostbyaddr. These functions may use the local %SystemRoot%\System32\drivers\etc\hosts file, DNS or even NETBIOS to resolve a host name to an IP address. Resolving a host name to an IP address using DNS will use CNAME records to find the A record.

You can test this by resolving www.google.com that at least right now has a CNAME record that points to www.l.google.com. Using Dns.GetHostEntry will return the IP addresses from the A records for www.l.google.com.

Solution 2

This is the list of addresses returned by

var ips = System.Net.Dns.GetHostEntry("microsoft.com").AddressList;
foreach (var ip in ips)
    Console.WriteLine(ip);

// output
64.4.11.37
65.55.58.201

And these are the A records pulled from network-tools.com, DNS query.

Answer records
microsoft.com       A   64.4.11.37  
microsoft.com       A   65.55.58.201

So I'd say it does pull A records.

Solution 3

To be explicitly clear here, Martin Liversage's answer is the most correct answer. The api will first attempt to locate CNAME records for the domain being resolved. If a CNAME record is found the api will then attempt to resolve the A records of the domain returned from the CNAME record. If there are no CNAME records for the domain being resolved then it will attempt to resolve A records for the domain itself.

To detect when a CNAME record exists the resolved hostname can be checked against the input hostname. Example:

IPHostEntry iphostEntry = Dns.GetHostEntry(inputHostname);
if (iphostEntry.Hostname != inputHostname) {
    Console.WriteLine("CNAME record exists pointing {0} to {1}", inputHostname, iphostEntry.Hostname);
    Console.WriteLine("iphostEntry.AddressList values are the A record values of {0}", iphostEntry.Hostname);
} else {
    Console.WriteLine("CNAME record does NOT exist for {0}", inputHostname);
    Console.WriteLine("iphostEntry.AddressList values are the A record values of {0}", inputHostname);
}
Share:
30,100
Greg Tarr
Author by

Greg Tarr

I am a software development professional based in Guildford, UK. I specialize in the .NET stack incorporating C#, ASP.NET and WCF, although I can also program in VB and Java. I currently work for a Microsoft Dynamics CRM Gold Partner which also uses the open source DotNetNuke content management system. I have developed numerous integration components, web services and single sign on capabilities as part of my role, utilizing technologies such as MSMQ. I have a strong interest in software development methodologies, design patterns and development team practices, incorporating use of source control systems, continuous integration, package repositories and test driven development.

Updated on August 12, 2022

Comments

  • Greg Tarr
    Greg Tarr over 1 year

    I can't find any proper description in the documentation for what this actually does.

    Does it check for the existence of A records or CNAME records or both?

    My understanding is that in .NET 4, this throws a SocketException if the host does not exist, and this is confirmed by my testing.