iOS - 在仅IPv6网络上获取设备的DNS服务器地址

时间:2015-08-02 05:34:06

标签: ios dns ipv6 ipv4

我正在尝试获取设备的DNS服务器地址,同时连接到iOS上的IPv6网络。以下代码适用于连接到IPv4网络但不适用于IPv6网络的情况。代码取自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");
    }
}

在IPv6网络上,sin_family始终为AF_UNSPEC。有什么建议/选择吗?

1 个答案:

答案 0 :(得分:0)