C# Dns.GetHostEntry doesn't return names of mo

2019-05-14 22:28发布

问题:

I have a windows form application in C# and I'm trying to get the host name of all the clients that I have in a list. Given below is a code example by ra00l from this link: GetHostEntry is very slow (I have a similar code made but this one is cleaner)

private delegate IPHostEntry GetHostEntryHandler(string ip);
public string GetReverseDNS(string ip, int timeout) 
{
     try
     {
          GetHostEntryHandler callback = new GetHostEntryHandler(Dns.GetHostEntry);
          IAsyncResult result = callback.BeginInvoke(ip,null,null);
          if (result.AsyncWaitHandle.WaitOne(timeout, false))
          {
               return callback.EndInvoke(result).HostName;
          }
          else 
          {
               return ip;
          }
     } 
     catch(Exception)
     {
          return ip;
     }
}

When it's given an IP of a Windows Machine in the network, it shows the correct host name given that you enter a reasonable timeout. After the tests that I made, I didn't get any response for the host names of android and apple devices. For example, the picture below is the DHCP Client List of the router that I have. It shows android, apple and laptop devices. I'm using the laptop 'Nathu-Laptop' giving me an IP address of '192.168.1.106'.

If I enter '192.168.1.105' in the C# function, the result is 'Nandwani-PC' but if I input '192.168.1.103', '192.168.1.104', '192.168.1.101', '192.168.1.100', I don't get any hostname.

I also tried using nbtstat but it only gets the laptops in the network.

When trying this out on my iPod, I ensure that there is a network activity going on. This is to keep the connection alive because it disconnects from the network when it's on standby.

EDIT:

So I found out that DNS.GetHostEntry calls getaddrinfo if IPv6 is enabled, otherwise, call gethostbyaddr and these functions may access the data from \System32\drivers\etc\hosts or maybe from the NETBIOS. The thing is that the NETBIOS is legacy right? but how about for mobile devices?

回答1:

About NetBIOS:

In order to answer your specific questions about NetBIOS, and name resolution on network, I'll give more details. If you don't have dns server running on your network, name resolution will rely only on NetBIOS resolution. It is a standard and implemented on several operating systems. However, it's not very fast.

Even if we're old, we're not legacy from the past and obsolete

You can check on Microsoft support the way names are resolved on Windows and NetBIOS is the last one.

However, NetBios name resolution is not always fully functional (like this bug on Android which was fixed in 2014) on all mobile platforms (depends on Android version for example). If you want to improve the performance, I suggest you to install a DNS server in the network.

Did you try ping -a <IP address> or nslookup <IP address> to check if the results are in line with your expectations ?

If your problem still persist, then you may investigate the .Net implementation, thanks to the above links. You can also check a more up to date version of .Net DNS implementation here