问题描述
如果我们的服务器(在设备上运行)在获取DHCP租约之前启动,则它将永远无法使用主机名进行连接.
If our server (running on a device) starts before a DHCP lease had been acquired then it can never connect using a hostname.
如果发生这种情况,它可以通过IP地址而不是DNS查找主机.
If that happens it can find hosts by IP address but not by DNS.
最初,我认为Curl DNS缓存有问题,因为curl连接失败.但是我使用CURLOPT_DNS_CACHE_TIMEOUT来防止curl缓存地址,但是连接仍然失败.
I initially thought that the Curl DNS cache was at fault as the curl connections failed. But I used CURLOPT_DNS_CACHE_TIMEOUT to prevent curl from caching address but connections still failed.
推荐答案
事实证明,如果glibc gethostbyname_r的配置发生更改,它不会自动重新加载其配置.您必须手动调用res_init.请参见下面的错误报告.
It turns out that glibc gethostbyname_r won't automatically reload it's configuration if that configuration changes. You have to manually call res_init. See bug report below.
注意:gethostbyname_r的手册页和rer_init的手册页均未提及此限制.
Note: Neither the man page for gethostbyname_r nor for rer_init mentioned this limitation.
我的解决方案非常具体.它适用于我们长期运行的服务器,但这不是我理想的解决方案.
My solution is very specific. It works for our long running server but it is not my ideal solution.
我有一个功能,可以根据上次已知的mtime(对于DNE为0)检查/etc/resolv.conf的mtime.如果两个mtime不同,然后我调用res_init.在程序启动时调用此函数,然后定期调用此函数以有选择地重新加载配置.
I have a function that checks the mtime of the /etc/resolv.conf against the last known mtime (0 for DNE). If the two mtimediffer then I call res_init. This function is called on program startup and then periodically to optionally reload the configuration.
glibc 错误报告
...
这就是res_init()的用途,叫做它.
That's what res_init() is for, call it.
这篇关于使用glibc,为什么在I/DHCP更改DNS服务器后我的gethostbyname失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!