ParseNetworkString 不会解析 21DA:00D3:0000:2F3B:02AA:00FF:FE28:9C5A:8080

ParseNetworkString will not parse 21DA:00D3:0000:2F3B:02AA:00FF:FE28:9C5A:8080

我正在尝试找到一种方法来解析用户输入的 IP 地址信息,例如:

WinAPI 函数 ParseNetworkString 声称能够解析所有这些格式(列表来自文档)。他们中的很多人确实会解析。但有些失败了:

所以伪代码:

NET_ADDRESS_INFO^ addressInfo;
UInt16^           portNumber;
UInt8^            prefixLength;

DWORD res = ParseNetworkString(
      "21DA:00D3:0000:2F3B:02AA:00FF:FE28:9C5A:8080", //NetworkString
      NET_STRING_IPV6_SERVICE_NO_SCOPE, //Types
      addressInfo, portNumber, lengthPrefix);

回来87 The Parameter is incorrect.

我做错了什么?

奖金

以同样的方式 API 说它可以解析:

192.168.100.10:8080
\____________/ \__/
    |           |
 Address       Port

它还声称它可以解析:

21DA:00D3:0000:2F3B:02AA:00FF:FE28:9C5A:8080
\_____________________________________/ \__/
                   |                     |
                Address                 Port
      

有些人可能会反对,并声称在规范 IPv6 地址中包含 端口号 的唯一方法是地址部分包含在 [square backets] 中。那些人完全错了。 Source: RFC5952

最终我们想要的是能够将用户输入的内容传递给WSAConnectByName,这需要:

文档完全不正确。如果你反汇编 ParseNetworkString() 你会发现它调用 RtlIpv6StringToAddressEx() 也就是 documented as (emphasis added):

The string pointed to by the AddressString parameter must be represented in the form for an IPv6 address string followed by an optional percent character and scope ID string. The IPv6 address and scope ID string must be enclosed in square brackets. The right square bracket after the IPv6 address and scope ID string may be followed by an optional colon and a string representation of a port number.