The method System.Net.IPAddress.TryParse() incorrectly parses "::2:3:4:5:6:7:8".
According to RFC 4291, "The use of "::" indicates _one_ or more groups of 16 bits of zeros" (emphasis mine). Therefore, I would expect "::2:3:4:5:6:7:8" to be valid and the leading "::" to represent a zero.
For example, for the following code:
Console.WriteLine(IPAddress.TryParse("::2:3:4:5:6:7:8", out var addr) ? addr.ToString() : "error");
Actual output:
::
Expected output:
0:2:3:4:5:6:7:8
I find the actual output very strange. In .NET Framework, the result was false, which I don't think is correct, but at least wass less surprising.
Environment:
.NET Core SDK (reflecting any global.json):
Version: 2.2.107
Commit: 2212cac826
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.107\
Host (useful for support):
Version: 2.2.5
Commit: 0a3c9209c0
Probably fixed by https://github.com/dotnet/corefx/pull/37734
I did some test seem that bug is present on master yet, investigating.
EDIT: found an issue with leading compressor handling...I'll PR a fix.
I've done some research on whether or not IPv6 addresses such as "::2:3:4:5:6:7" are actually valid IPv6 addresses.
According to WinRT Windows.Networking.Hostname API parsing, it is not valid.
The following code throws an exception "The parameter is incorrect".
```c#
using Windows.Networking;
var host = new HostName("[::2:3:4:5:6:7:8]");
```
Note: It's also interesting that this WinRT API will parse alleged IPv6 addresses without the brackets. But in the case of the string "::2:3:4:5:6:7:8", it will parse into an object but the .Type property says "Bluetooth" and not "IPv6". The WinRT API parses a variety of network addresses including IP and Bluetooth.
I think the premise of the issue raised by dotnet/corefx#38634 is not correct. I think the RFC might have a mis-print in it. I think the intention was that "::" always represented "2 or more" groups of zeros.
I will follow-up after I've discussed with our team's IETF experts for the IPv6 RFC.
And more curious:
c#
var host = new HostName("[1::3:4:5:6:7:8]");
parses successfully as an IPv6 address. So, at least for inner “::” it can be “1 or more” groups of zeroes.
I wonder whether "[::2:3:4:5:6:7:8]" is actually valid (i.e. should result as "[0:2:3:4:5:6:7:8]") but we have a separate bug in the WinRT Windows.Networking.HostName API as well. I'm waiting to hear back from our IETF representative.
"2 or more" groups of zeros.
In that case there is an incorrect handling also for trailing compressor.
Does WinRT throws for 1 group trailing compressor? [1:2:3:4:5:6:7::]
After discussions with the IETF rep and the Windows team, the RFC is correct. So, the fixes for IPAddress class are correct. It turns out there is a separate bug for the WinRT Windows.Networking.HostName class that will be filed separately.
Most helpful comment
I did some test seem that bug is present on master yet, investigating.
EDIT: found an issue with leading compressor handling...I'll PR a fix.