Retrieve IPv6 DNS servers list programmatically on Linux

659 views Asked by At

I'm trying to retrieve a list of IPv6 DNS servers programmatically. Currently I'm using libresolv and next code:

#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.

IPv4 DNS servers list is accessible via res->nsaddr_list field, but IPv6 list is always empty. I've checked this with both Ubuntu and CentOS. IPv6 is configured correctly.

1

There are 1 answers

2
Oleksandr Kravchuk On

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.

Try to put the following line into your /etc/resolv.conf:

nameserver 2001:4860:4860::8844