使用glibc,为什么在I/DHCP更改DNS服务器后我的gethostbyname失败? [英] Using glibc, why does my gethostbyname fail after I/DHCP has changed the DNS server?

查看:114
本文介绍了使用glibc,为什么在I/DHCP更改DNS服务器后我的gethostbyname失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们的服务器(在设备上运行)在获取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 mtime differ then I call res_init. This function is called on program startup and then periodically to optionally reload the configuration.

glibc 错误报告

libc永远缓存resolv.conf

libc caches resolv.conf forever

...

这就是res_init()的用途,叫做它.

That's what res_init() is for, call it.

这篇关于使用glibc,为什么在I/DHCP更改DNS服务器后我的gethostbyname失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆