我正在尝试使用 libresolv 读取/etc/resolv.conf
文件中的 IPv4 和 IPv6 名称服务器:
# 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
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 127.0.0.53
nameserver 2001:4860:4860:0:0:0:0:8888
这是我的 C 程序:
#include <resolv.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
res_state res = malloc(sizeof(struct __res_state));
res_ninit(res);
printf("IPv4 nscount: %d\n", res->nscount);
printf("IPv6 nscount6: %d\n", res->_u._ext.nscount6);
return 0;
}
产生这个输出:
IPv4 nscount: 2
IPv6 nscount6: 0
这让我很惊讶。为什么将 IPv6 地址计为 IPv4 地址?
GDB 显示第二个地址被清零:
(gdb) display res.nsaddr_list[0]
5: res.nsaddr_list[0] = {sin_family = 2, sin_port = 13568, sin_addr = {s_addr = 889192575}, sin_zero = "\000\000\000\000\000\000\000"}
(gdb) display res.nsaddr_list[1]
6: res.nsaddr_list[1] = {sin_family = 0, sin_port = 0, sin_addr = {s_addr = 0}, sin_zero = "\000\000\000\000\000\000\000"}
谁能帮我理解这种行为?