winapidnspingwinsockgetaddrinfo

WinSock getaddrinfo to get IP from hostname fails until ipconfig /flushdns is called


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:

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?


Solution

  • 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);