Question

I'm just wondering if there can be a case where the hostname can be successfully resolved but the returned hostEntry.AddressList is empty.

Currently I'm doing something like this:

IPHostEntry hostEntry = Dns.GetHostEntry("some.hostname.tld");
if (hostEntry.AddressList.Count() < 1)
{
  // can that ever happen?
  throw new ArgumentException("hostName has no assigned IP-Address");
}
TcpClient client = new TcpClient(hostEntry.AddressList[0], 1234);

My assumption is that Dns.GetHostEntry either throws an exception if the hostname is not found or otherwise the AddressList is nonempty, but I'm not sure about that.

Was it helpful?

Solution

No, you'll not see an empty address list: even if you query a DNS label that does exist, but has no A or AAAA (IPv6) records, a SocketException ("No Such Host is Known") will be thrown.

You can verify this by looking at the function InternalGetHostByName(string hostName, bool includeIPv6) in DNS.cs from the .NET Reference Source release. With the exception of some platform-specific precautions, DNS lookups are a simple wrapper around the Winsock gethostbyname function.

Gethostbyname will either fail, or return an address list. An empty address list is never returned, because the function will fail with WSANO_DATA ("Valid name, no data record of requested type") in this case, which translates to the socket exception we already saw in .NET.

EDIT May 2012, prompted by responses stating that an empty list is returned anyway: do note that this answer only applies to Win32, and that platforms like WinCE may behave quite differently. If you're seeing 'empty list' behavior on Win32, and the request you're making is against a publicly available DNS server, please post your code...

OTHER TIPS

Just for the records.

Thanks to mdb's accepted answer I took a look at the description of the WSANO_DATA error:

The requested name is valid and was found in the database, but it does not have the correct associated data being resolved for. The usual example for this is a host name-to-address translation attempt (using gethostbyname or WSAAsyncGetHostByName) which uses the DNS (Domain Name Server). An MX record is returned but no A record—indicating the host itself exists, but is not directly reachable.

So this pretty much answers my question :)

You have three possible situations here:

  1. The hostname exists (DNS has an A Record) and resolves to an IP Address

    • Condition is never hit
  2. The hostname exists (DNS knows about the domain) however no A records exists.

    • This is an extremely unlikely scenario, and I think this can never happen in the first place.
  3. The hostname doesn't exist

    • Exception is thrown, you never get there.

So no, I don't think that can ever happen.

The answer is YES. The GetHostEntry method queries a DNS server for the IP addresses and aliases associated with an IP address.

IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results where available for the address parameter.

The Aliases property of the IPHostEntry instance returned is not populated by this method and will always be empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top