【问题标题】:find temporary IPv6 address using GetAdaptersAddresses使用 GetAdaptersAddresses 查找临时 IPv6 地址
【发布时间】:2016-05-28 07:06:55
【问题描述】:

我正在使用 GetAdaptersAddresses 查找机器的所有 IPv6 地址。

我想区分全局地址和 RFC4941 临时地址(RFC4941 也称为“隐私扩展”)。

answer 建议使用地址的首选生命周期来查找临时地址,因为它的生命周期会更短。除了这是一个杂物,它在我的机器上也不起作用(使用 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

您可以看到两个地址的生命周期是相同的。

那么,如何获得临时地址的标志,或者,更挑衅性地问,ipconfig 或 netsh 是如何知道它们使用的 API 是什么?

【问题讨论】:

    标签: c winapi networking winsock


    【解决方案1】:

    我知道这有点晚了,但是当我搜索有关如何在 Windows 中获取临时 IPv6 地址时,这个问题是最佳结果之一。

    我使用了链接问题中给出的指南,并且可以在 Windows 10 中成功获取临时 IPv6 地址。如上所述,主要思想是检查 SuffixOrigin 是否为 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);
    }
    

    希望对你有用

    【讨论】:

    • 哇,是的。我什至不知道如何正确表达我的问题。这似乎是任何人(至少公开地)提出的唯一答案。向你致敬! (甚至微软的文档都错误地描述了这个属性)
    • 感谢您的回答!在ERROR_BUFFER_OVERFLOW的情况下,while循环会产生内存泄漏。
    猜你喜欢
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多