2

Getting an IP address from a hostname is normally done by calling getaddrinfo (which is really just an alias for WspiapiGetAddrInfo).

We see cases where this fails. At that same time as the failure:

  • ping {hostname} is able to resolve the hostname just fine.

  • ping -4 {hostname} also fails to resolve the hostname, just like getaddrinfo.

The solution to fix the getaddrinfo and the ping -4 failure situation is to run

ipconfig /flushdns

The above seems to indicate that ping -4 and getaddrinfo are resolving the address using an (apparently bad) entry in an internal DNS cache.

Is there a way to programmatically clear that cache (doing whatever ipconfig /flushdns does), or better, have getaddrinfo not use the cache?

zx485
  • 28,498
  • 28
  • 50
  • 59
DougN
  • 4,407
  • 11
  • 56
  • 81

1 Answers1

3

Is there a way to programmatically clear that cache (doing whatever ipconfig /flushdns does)

ipconfig /flushdns does this:

typedef BOOL(WINAPI *DFRC)();
DFRC DnsFlushResolverCache;
HMODULE hDll = LoadLibrary(L"DnsApi.dll");
DnsFlushResolverCache = (DFRC)GetProcAddress(hDll, "DnsFlushResolverCache");
BOOL bRet = DnsFlushResolverCache();
// code...
FreeLibrary(hDll);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Castorix
  • 1,465
  • 1
  • 9
  • 11
  • 1
    @DougN Just note that typical networking applications do not need to flush the cache manually. You need to find out why your cache is getting corrupted and fix that issue instead. – Remy Lebeau Jul 15 '19 at 22:14
  • I would love to know why it's getting corrupted but that is somewhere deep in Windows. We've seen it at a few client sites so it's not isolated to just a single computer. If you have any ideas on how to diagnose I am open to any suggestions. – DougN Jul 16 '19 at 18:45