我正在尝试通过解析器库获取c中所有DNS服务器的列表,但我无法获得ipv6 DNS服务器的列表,请帮助我。
OS: Debian GNU/Linux 10 (buster)
这是测试代码:
#include <stdio.h>
#include <arpa/inet.h>
#include <resolv.h>
int main(int argc, char **argv)
{
struct __res_state statp;
char addr_str[80];
if(res_ninit(&statp) < 0)
{
fprintf(stderr, "init resolver failed\n");
return 1;
}
//ipv6 dns server
for(int i = 0; i < statp._u._ext.nscount; i++)
{
if(statp._u._ext.nsaddrs[i]->sin6_family == AF_INET6)
{
if(inet_ntop(AF_INET6, &statp._u._ext.nsaddrs[i]->sin6_addr, addr_str, 80) != NULL)
{
printf("ipv6 dns server = %s\n", addr_str);
}
}
}
//ipv4 dns server
for(int i = 0; i < statp.nscount; i++)
{
if(statp.nsaddr_list[i].sin_family == AF_INET)
{
if(inet_ntop(AF_INET, &statp.nsaddr_list[i].sin_addr, addr_str, 80) != NULL)
{
printf("ipv4 dns server = %s\n", addr_str);
}
}
}
res_nclose(&statp);
return 0;
}
/etc/resolv.conf:</p>
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 2001:19f0:300:1704::6
nameserver 108.61.10.10
gdb中的调试代码:</p>
(gdb) p statp._u._ext
$2 = {nscount = 0, nsmap = {0, 77, 0}, nssocks = {-1, -1, -134331088}, nscount6 = 32767, nsinit = 0,
nsaddrs = {0x555555559630, 0x0, 0x555555555090 <_start>}, __glibc_reserved = {1219461217, 648608350}}
(gdb) p *statp._u._ext.nsaddrs[0]
$3 = {sin6_family = 10, sin6_port = 13568, sin6_flowinfo = 0, sin6_addr = {__in6_u = {
__u6_addr8 = " \001\031\360\003\000\027\004\000\000\000\000\000\000\000\006", __u6_addr16 = {288,
61465, 3, 1047, 0, 0, 0, 1536}, __u6_addr32 = {4028170528, 68616195, 0, 100663296}}},
sin6_scope_id = 0}
statp._u._ext.nscount 为 0,但 statp._u._ext.nsaddrs[0] 是正确的 ipv6 dns 服务器 sockaddr。