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?
Try using res._u._ext.nsaddrs for IPv6.
See https://chromium.googlesource.com/chromium/src/+/master/net/dns/dns_config_service_posix.cc#437
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.