概述
经过测试发现/etc/resolv.conf可以配置很多个dns server但是最多生效3个。
dns解析器配置内容
文件路径/etc/resolv.conf
,文件内容如下:
# Generated by NetworkManager
nameserver 192.168.100.24
nameserver 192.168.100.25
nameserver 114.114.114.114
nameserver 114.114.114.114
修改保存即生效,无需做其他操作。经过测试发现最多生效3个。感到好奇,一顿搜索后,找到一些蛛丝马迹。
蛛丝马迹
控制程序
控制程序为glibc中的glibc/resolv/resolv.conf.c
,核心代码段 if (nserv > MAXNS) nserv = MAXNS;
:
{
size_t nserv = conf->nameserver_list_size;
if (nserv > MAXNS)
nserv = MAXNS;
/* _ext.nscount is 0 until initialized by res_send.c. */
if (resp->nscount != nserv
|| (resp->_u._ext.nscount != 0 && resp->_u._ext.nscount != nserv))
return false;
for (size_t i = 0; i < nserv; ++i)
{
if (resp->nsaddr_list[i].sin_family == 0)
{
if (resp->_u._ext.nsaddrs[i]->sin6_family != AF_INET6)
return false;
if (!same_address ((struct sockaddr *) resp->_u._ext.nsaddrs[i],
conf->nameserver_list[i]))
return false;
}
else if (resp->nsaddr_list[i].sin_family != AF_INET)
return false;
else if (!same_address ((struct sockaddr *) &resp->nsaddr_list[i],
conf->nameserver_list[i]))
return false;
}
}
MAXNS
最大值是写死在[resolv.h](https://git.musl-libc.org/cgit/musl/tree/include/resolv.h?h=v1.2.2&id=85e0e3519655220688e757b9d5bfd314923548bd)
中,最大值为3.
#define MAXNS 3
官方man 手册对MAXNS有详细说明。
nameserver Name server IP address
Internet address of a name server that the resolver should
query, either an IPv4 address (in dot notation), or an
IPv6 address in colon (and possibly dot) notation as per
RFC 2373. Up to MAXNS (currently 3, see <resolv.h>) name
servers may be listed, one per keyword. If there are
multiple servers, the resolver library queries them in the
order listed. If no nameserver entries are present, the
default is to use the name server on the local machine.
(The algorithm used is to try a name server, and if the
query times out, try the next, until out of name servers,
then repeat trying all the name servers until a maximum
number of retries are made.)
原创文章,作者:速盾高防cdn,如若转载,请注明出处:https://www.sudun.com/ask/76721.html