我正在使用libresolv来执行对dns服务器的请求。
我也使用本地dns缓存。 (的dnsmasq /未结合的)
当我使用dig
时,例如dig google.com
,请求被缓存,下一个请求使用缓存值。
我们可以在dnsmasq日志中看到这一点:
Nov 9 10:46:46 y dnsmasq[24003]: query[A] google.com from 127.0.0.1
Nov 9 10:46:46 y dnsmasq[24003]: forwarded google.com to **.**.**.**
Nov 9 10:46:46 y dnsmasq[24003]: reply google.com is 173.194.32.165
Nov 9 10:46:46 y dnsmasq[24003]: reply google.com is 173.194.32.160
Nov 9 10:46:49 y dnsmasq[24003]: query[A] google.com from 127.0.0.1
Nov 9 10:46:49 y dnsmasq[24003]: cached google.com is 173.194.32.165
Nov 9 10:46:49 y dnsmasq[24003]: cached google.com is 173.194.32.168
然后我使用res_query
,我得到了这个:
Nov 9 10:50:29 y dnsmasq[24003]: query[MX] google.com from 127.0.0.1
Nov 9 10:50:29 y dnsmasq[24003]: forwarded google.com to **.**.**.**
Nov 9 10:50:29 y dnsmasq[24003]: forwarded google.com to **.**.**.**
Nov 9 10:51:13 y dnsmasq[24003]: query[MX] google.com from 127.0.0.1
Nov 9 10:51:13 y dnsmasq[24003]: forwarded google.com to **.**.**.**
Nov 9 10:51:13 y dnsmasq[24003]: forwarded google.com to **.**.**.**
所以看起来dns服务器的响应没有达到dns缓存而且没有缓存。
有没有办法从res_query
缓存请求?
这就是我向DNS解析器发出请求的方式:
struct __res_state dnsstate;
int rc = res_ninit(&dnsstate);
if (rc < 0) {
return result;
}
dnsstate.retrans = timeout;
int len = res_nquery(&dnsstate, domain.c_str(), ns_c_in, ns_t_mx, nsbuf, sizeof(nsbuf));
我认为res_nquery不会检查本地DNS缓存,而dig会检查。
答案 0 :(得分:1)
dig和res_nquery()
都会将查询发送到/etc/resolv.conf
文件中的任何内容,即您的dnsmasq实例。两者都默认设置RD
位以请求递归。
dnsmasq将从缓存中提供答案,或者在TTL过期时获取它们。但据我所知,DNS协议本身没有任何内容(因此res_nquery
设置中没有任何内容),客户端可以设置这将影响该行为