views:

1459

answers:

3

Hi, 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.

A: 

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 situation when the DNS label ('hostname') exists, but no A records are present is actually quite common: think MX records.
mdb
+1  A: 

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.

mdb
This does not appear to be strictly the case. Right now I am struggling with a problem where an IPHostEntry is being returned with an empty Address list and a hostname of "26l2233b1-04". Currently I have no idea why it's happening...
mdsharpe
A: 

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

VVS