I am trying to get device's DNS server address while connected to IPv6 only network on iOS. The following code works well for when connected to IPv4 network but doesn't work on IPv6 network. Code taken from this answer.
res_ninit(&_res);
res_state res = &_res;
for (int i=0; i < res->nscount; i++) {
sa_family_t family = res->nsaddr_list[i].sin_family;
if (family == AF_INET) {
NSLog(@"IPv4");
char str[INET_ADDRSTRLEN]; // String representation of address
inet_ntop(AF_INET, & (res->nsaddr_list[i].sin_addr.s_addr), str, INET_ADDRSTRLEN);
} else if (family == AF_INET6) {
NSLog(@"IPv6");
char address[INET6_ADDRSTRLEN]; // String representation of address
inet_ntop(AF_INET6, &(res->nsaddr_list [i].sin_addr.s_addr), address, INET6_ADDRSTRLEN);
} else {
NSLog(@"Unspecified");
}
}
On IPv6 network, sin_family
is always AF_UNSPEC
. Any suggestions/alternatives?