Can Dns.GetHostEntry ever return an IPHostEntry with an empty AddressList?

前端 未结 4 1475
予麋鹿
予麋鹿 2021-01-15 10:57

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 some

相关标签:
4条回答
  • 2021-01-15 11:44

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

    0 讨论(0)
  • 2021-01-15 11:52

    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.

    0 讨论(0)
  • 2021-01-15 11:53

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

    0 讨论(0)
  • 2021-01-15 12:01

    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.

    0 讨论(0)
提交回复
热议问题