简体   繁体   English

以编程方式检索IPv4和IPv6域名服务器

[英]Retrieve IPv4 and IPv6 nameservers programmatically

I'm trying to use libresolv to read both the IPv4 and IPv6 nameservers in my /etc/resolv.conf file: 我正在尝试使用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

This is my C program: 这是我的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;
}

Which produces this output: 产生以下输出:

IPv4 nscount:  2
IPv6 nscount6: 0

Which surprises me. 这让我感到惊讶。 Why is it counting the IPv6 address as an IPv4 address? 为什么将IPv6地址计为IPv4地址?

GDB shows that the second address is zeroed out: 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"}

Can anyone help me understand this behavior? 谁能帮助我了解这种行为?

You really should not access the _u._ext parts of the resolver state, they are an internal implementation detail. 您实际上不应该访问解析器状态的_u._ext部分,它们是内部实现的细节。 The nscount6 member is currently unused and always zero. nscount6成员当前未使用,并且始终为零。 It had to be kept to avoid changing the ABI as the result of struct offset/size changes. 必须保留它以避免由于结构偏移/大小更改而导致更改ABI。

If you need the nameserver list, you should parse /etc/resolv.conf yourself. 如果需要名称服务器列表,则应该自己解析/etc/resolv.conf Note that eventually, glibc will also support more than three name servers, and those extra resolvers will not be reflected in the public resolver state. 请注意,glibc最终还将支持三个以上的名称服务器,并且那些额外的解析器将不会反映在公共解析器状态中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM