简体   繁体   English

在Linux上以编程方式检索IPv6 DNS服务器列表

[英]Retrieve IPv6 DNS servers list programmatically on Linux

I'm trying to retrieve a list of IPv6 DNS servers programmatically. 我正在尝试以编程方式检索IPv6 DNS服务器列表。 Currently I'm using libresolv and next code: 目前我正在使用libresolv和下一个代码:

#include <stdio.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv)
{
    res_state res = malloc(sizeof(struct __res_state));
    res_ninit(res);

    printf("%p\n", res->_u._ext.nsaddrs[0]); // (nil) here
}

According to resolv.h, res->_u._ext.nsaddrs field should contain an array of "struct sockaddr_in6 *" with needed addresses, but it contains an array of NULLs. 根据resolv.h,res - > _ u._ext.nsaddrs字段应该包含一个带有所需地址的“struct sockaddr_in6 *”数组,但它包含一个NULL数组。

IPv4 DNS servers list is accessible via res->nsaddr_list field, but IPv6 list is always empty. 可以通过res-> nsaddr_list字段访问IPv4 DNS服务器列表,但IPv6列表始终为空。 I've checked this with both Ubuntu and CentOS. 我用Ubuntu和CentOS检查过这个。 IPv6 is configured correctly. IPv6配置正确。

Your code seems to be correct. 你的代码似乎是正确的。 Most likely res_ninit() doesn't put any IPv6 DNS servers into the struct res_state because your system is not aware of any. 最有可能res_ninit()不会将任何IPv6 DNS服务器放入struct res_state因为您的系统不知道任何。

Try to put the following line into your /etc/resolv.conf : 尝试将以下行放入/etc/resolv.conf

nameserver 2001:4860:4860::8844 nameserver 2001:4860:4860 :: 8844

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

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