iOS 检测 IPv6 支持

标签 ios c ipv6

我有一个 iOS 应用程序,它使用带有内置套接字控制的库(换句话说,我自己不创建套接字,我只是向库传递套接字的参数)。我从库中获得了两个可用的 API,每个 API 都包含一个可配置的地址,即 IPv4 或 IPv6。然后我可以配置一个开关来告诉库它应该使用 IPv6 还是 IPv4。

这里是一个如何加载配置的例子

// IPv6 Configuration
char* ipv6_addr = . . .; // This is defined elsewhere
config.server.s6.sin6_family = AF_INET6;
config.server.s6.sin6_port   = htons(1122);
inet_pton(AF_INET6, ipv6_addr, &(config.server.s6.sin6_addr));

// IPv4 Configuration
char* ipv4_addr = . . .; // This is defined elsewhere
config.server.s4.sin_family = AF_INET;
config.server.s4.sin_port   = htons(1122);
inet_pton(AF_INET, ipv4_addr, &(config.server.s4.sin_addr));

config.use_ipv6 = 1; // Set to 0 to use IPv4

我目前可以成功连接到 IPv4 或 IPv6,具体取决于设备和客户端网络(ISP 等)支持的内容,但我必须手动配置 use_ipv6 标志。

我需要做的是检查客户端的网络(ISP 等)是否支持 IPv6。如果是这样,请使用 IPv6 连接。但如果没有,请使用 IPv4 连接。最好是这样的: config.use_ipv6 = has_ipv6_support(); 其中函数 has_ipv6_support() 返回 10 .

那么,我该如何检查网络是否支持 IPv6?

Notes

a) I can't use the regular methods of getaddrinfo since the sockets are handled inside of the library and I cannot use hostnames due to my infrastructure base shifting IP's around a lot. I only have either an IPv4 address or an IPv6 address at my disposal.

b) This is written in C, but I wouldn't mind a solution in Objective C either.

c) It would be difficult to simply test an IPv6 connection to decide since I'm using UDP for the connection and would have to timeout on a response. That's a bit more logic than I'd like to be involved as this will need to be done every now and again and I don't want to kill the battery by keeping the network adapters alive. I'd prefer to be able to pull this information locally.

最佳答案

作为 UDP 服务器,您的图书馆应该同时监听 IPv4 和 IPv6 地址。因为如果不定期尝试使用 IPv4 和 IPv6 连接到远程主机,就无法知道 IPv4 和 IPv6 中的哪一个可用。这是因为网络故障可能发生在网络中,仅在 IPv6 或 IPv4 路径上的某处,到可能与您的应用程序通信的远程端。所以,要么改变你的图书馆(但似乎不可能),要么定期更换你的电池!抱歉:-(

关于iOS 检测 IPv6 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516328/

相关文章:

internet-explorer - IPv6 地址在 Internet Explorer-10 websocket 中给出语法错误

javascript - 为媒体禁用 iOS Safari 锁屏刷

ios - 核心数据查询值 "is not nil"未返回值等于 0 的对象

c - MPI_Type_create_subarray 和 MPI_Send

ios - 带有 ipv6 的 libssh2 lib 不工作

python - IPv6 中的 RAW 套接字 UDP 多播

ios - 如何从文本文件创建一个 NSNumbers 数组?

ios - 更改首选内容大小后,我的某些可重用单元格标签中的字体大小仍未更改

c - 大整数加法码

c - 如何将输入添加到数组并在用户输入字符串 'stop' 时停止?