0

Problem: there's an input field in an application where the user can enter either a host name or an IP address. I need to tell if the entered address corresponds to a real host.

I'm not talking about a simple regular expression check or an IPAddress.TryParse or Uri.CheckHostName. I don't have difficulty with checking a hostname: if it cannot be resolved to an IP address, then Dns.GetHostEntry will throw an exception. That's a piece of cake.

However. If I get an IP address input, then if I make a Dns.GetHostAddresses call it'll always succeed, even if I enter a stupid IP, like "1.1.1.1" ("1.1.1.1" is an IANA reserved IP address, our DNS server reports "non existent host/domain"). The Dns.GetHostAddresses immediately just spits back the IP I just passed in like everything would be all right whatsoever.

I cannot use the Dns.GetHostEntry either, because there are some IP addresses (like my virtual machines on the local network) which don't have any DNS host names associated with them, but they still have valid IP addresses, and Dns.GetHostEntry would throw exception to those (I guess it tries to resolve a hostname for them?).

I need a method call which actually tells me if it is a bogus IP or not, even if it looks like good IP address (by Uri.CheckHostName), but it doesn't have corresponding DNS host name.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
  • 4
    What's a "real host"? If you use Dns.GetHostEntry with a domain name, you only find out if there is an entry in the DNS for the name, not of there is actually a computer behind it. You could send an ICMP echo request ("ping") to the IP address, but if you don't get a response it doesn't mean there is no computer. – dtb Mar 20 '13 at 20:49
  • Have you looked at http://www.codeproject.com/Articles/12072/C-NET-DNS-query-component or http://www.aspnettutorials.com/tutorials/network/net-dns-aspnet2-csharp.aspx ? They both use Dns.GetHostByName . – RandomUs1r Mar 20 '13 at 20:52
  • and i agree with dtb, i'd just ping it. I'm not sure if you have requirements that would prevent that. – RandomUs1r Mar 20 '13 at 20:52
  • 2
    What you're asking seems unreasonable. Host name and IP address resolution are performed by the Domain Name System. If your DNS server doesn't implement reverse lookups correctly, or there are IP addresses that it doesn't know about, then your results are going to be incomplete. – Jim Mischel Mar 20 '13 at 20:56
  • dtb: "real host" = a hostname or IP address a computer/virtual machine sits behind. If there's just an IP, then DNS as a system cannot solve my problem. At the same time maybe I could look into ARP tables and given the IP is on the local subnet I could see it. But I don't like that ARP would be a lower level approach. I'm thinking... – Csaba Toth Mar 24 '13 at 03:25

1 Answers1

2

The only reasonably check you can make is if IP is some sort of reserved IP. Otherwise there is not much you can do - even lack of "ping" (ICMP) responses and lack of responses on well-known ports (like HTTP - 80) means nothing.

Reserved as in:

  • 127.0.0.0/8 - loopback (may or may not be considered "valid host")
  • 224.0.0.0 - 239.255.255.255 - multicast (unlikely to be considered "valid host")
  • all zeros/all ones in subnet (i.e. 192.168.1.0 and 192.168.1.255 for 192.168.1.0/24 subnet) are all/broadcast - clearly not associated with particular how.

Check IP4 subnetting and linked RFCs for more info on special ranges/IPs.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Yes, unfortunately even if the host doesn't respond to ICMP echo request, the IP can be still a valid one. Should I compile together a list of valid IP addresses? More than 10 years ago when I wrote a real great (paranoid) ipchains firewall script (early Linux years) then the list of reserved + special use IP addresses was quite long. The exhaustion of IPv4 address space seems to freed up lots of those! Look at RFC 5735 vs RFC 3330. I wonder what is a current exact list. But maybe I shouldn't even care that much. Once I know the interesting port number on the host I can try to actually open it – Csaba Toth Mar 24 '13 at 03:20