Phi*_*ipp 6 c networking winapi winsock
我正在使用 GetAdaptersAddresses 查找计算机的所有 IPv6 地址。
我想区分全局地址和RFC4941临时地址(RFC4941也称为“隐私扩展”)。
这个答案建议使用地址的首选生命周期来查找临时地址,因为它的生命周期较短。除了这是一个拼凑之外,它在我的机器上也不起作用(使用 Windows 7)。这是 netsh interface ipv6 show address 的输出
Addr Type DAD State Valid Life Pref. Life Address
--------- ----------- ---------- ---------- ------------------------
Public Preferred 1h58m15s 16m36s xxxx:xx:xxxx:2000:71e7:xxxx:xxxx:f45b
Temporary Preferred 1h58m15s 16m36s xxxx:xx:xxxx:2000:8479:xxxx:xxxx:a70a
Other Preferred infinite infinite fe80::71e7:xxxx:xxxx:f45b%19
Run Code Online (Sandbox Code Playgroud)
您可以看到两个地址的生命周期是相同的。
那么,如何才能获取临时地址的标志,或者更挑衅地问,ipconfig 或 netsh 如何知道它们正在使用的 API 是什么?
我知道有点晚了,但这个问题是我搜索如何在 Windows 中获取临时 IPv6 地址时的首要结果之一。
我使用了链接问题中给出的指南,并且可以在 Windows 10 中成功获取临时 IPv6 地址。正如所提到的,主要思想是检查SuffixOrigin
is IpSuffixOriginRandom
。
IP_ADAPTER_ADDRESSES *adapterAddr = NULL;
DWORD dwSize = 0, dwRet = 0;
DWORD flags = GAA_FLAG_INCLUDE_PREFIX | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
while (dwRet = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapterAddr, &dwSize) == ERROR_BUFFER_OVERFLOW) {
adapterAddr = (IP_ADAPTER_ADDRESSES *)LocalAlloc(LMEM_ZEROINIT, dwSize);
}
if (adapterAddr != NULL) {
IP_ADAPTER_ADDRESSES *AI;
int i;
for (i = 0, AI = adapterAddr; AI != NULL; AI = AI->Next, i++) {
if (AI->FirstUnicastAddress != NULL) {
for (PIP_ADAPTER_UNICAST_ADDRESS unicast = AI->FirstUnicastAddress; unicast; unicast = unicast->Next) {
if (unicast->SuffixOrigin == IpSuffixOriginRandom) {
cout << "This is temporary address!" << endl;
}
}
}
}
LocalFree(adapterAddr);
}
Run Code Online (Sandbox Code Playgroud)
希望这会有用