0

I have a domain joined computer with a literal computername of "mycomputername". It has a DHCP entry and DNS record with literal name "mydnsname". Yes, the local computername and DNS are different.

When I query our DNS servers using reverse NSLOOKUP, the expected name of "mydnsname" is returned.

However, if I use C# code below on the local machine, the local name "mycomputername" is returned.

string ipaddress="123.456.789.123"; //but my Real network IP
IPHostEntry hostEntry = Dns.GetHostEntry(ipaddress);
Console.WriteLine hostEntry.HostName;

This is unexpected. As far as I am aware, it should query DNS?

I made sure I named my computer something that has never been in DNS, so I know DNS cache is not getting in the way at this stage. If I locally rename the computer, restart, and run the code again - the updated computername is returned.

If I modify DNS, that modified DNS name is never returned - when run on the local machine. If I run the code on a different machine, but query the same IP, it returns the expected DNS entry.

I have attempted to flush the cache, but this has no effect: Is there a way to flush the DNS cache from a C# WPF app? (on XP, Vista, Win7)

NSLOOKUP command returns the correct name.

MrBeatnik
  • 87
  • 3
  • 9
  • Does this help? https://stackoverflow.com/questions/7277582/how-do-i-clear-system-net-client-dns-cache – Rand Random Sep 26 '18 at 12:59
  • @RandRandom, Tried a few solutions in that thread - the main answer of `ServicePointManager.DnsRefreshTimeout` makes no difference, nor does the use of `DnsFlushResolverCache();` which I already tried with the DllImport. `request.Keepalive` is of no use in this context. – MrBeatnik Sep 26 '18 at 13:20
  • IIRC, Dns.GetHostEntry() queries the \Windows\System32\drivers\etc\hosts file first. Is mycomputername defined there? – Terry Carmen Sep 26 '18 at 14:32
  • @TerryCarmen hosts file is empty. – MrBeatnik Sep 26 '18 at 14:51
  • If the DNS lookup fails, it will use NETBIOS name resolution using LHOSTS and as any defined WINS server. – Terry Carmen Sep 26 '18 at 15:03
  • @TerryCarmen I don't believe it is failing - NSLOOKUP on the same machine returns the correct DNS name, and if I run the code with a different IP, it returns the correct DNS name for the remote machine. It almost seems like it is checking NETBIOS _first_ since the IP is for the local machine... I would also expect an exception if lookup failed (I see an exception with an IP that has no DNS). – MrBeatnik Sep 26 '18 at 15:07
  • @MrBeatnik Sorry, I'm all out of ideas, except that you could fire up wireshark and see what's happening (or not happening) – Terry Carmen Sep 26 '18 at 15:16
  • @TerryCarmen Thanks for the input. I just have to fallback to Process and RedirectStandardOutput using nslookup. Which works. But I hate it. – MrBeatnik Sep 26 '18 at 15:24

0 Answers0