With glibc, why did my gethostbyname fail after I / DHCP changed the DNS server?

If our server (runs on the device) is started before the DHCP lease has been received, it will never be able to connect using the host name.

If this happens, it can find hosts by IP address, but not by DNS.

Initially, I thought the Curl DNS cache was to blame, as the bad curls failed. But I used CURLOPT_DNS_CACHE_TIMEOUT to prevent the cache from hanging up, but the connection still failed.

+6
glibc dns dhcp
source share
1 answer

It turns out that glibc gethostbyname_r will not automatically reload its configuration if this configuration changes. You must manually call res_init. See error report below.

Note. Neither the man page for gethostbyname_r nor rer_init mentioned this limitation.

My solution is very specific. It runs on our server with a long service life, but this is not my ideal solution.

I have a function that checks the mtime of the /etc/resolv.conf file for the last known time mtime (0 for DNE). If the two times are different, then I call res_init. This function is called when the program starts, and then periodically to additionally reload the configuration.


Glibc error message

libc caches resolv.conf forever

...

Why res_init () is needed, name it.

+9
source share

All Articles