The builtin .Net method System.Net.IPAddress.ToString() behaves inconsistently for IPv6 addresses.
Given the byte array 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
, in some environments "aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa"
is returned, whereas others return "aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:172.172.172.172"
.
I understand that both are valid IPv6 formats, but I would like to be able to explain the difference.
It seems that newer environments (Windows 7 and Server 2008 R2) are more likely to produce the first behaviour, so I've checked obvious differences like .Net framework version, but I've been unable to detect a pattern.
Is there a way I can select one format over the other, or do I have to code around this to get consistent behaviour?
Code to recreate:
byte[] bytes = {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};
IPAddress myIP = new IPAddress(bytes);
Console.WriteLine(myIP.ToString());
Poking around in the internals of ToString, using Reflector, you can see that if it's determined that the OS supports IPv6 (for some value of supports), then it defers to a Win32 function called WSAAddressToString, whereas if this flag isn't set, it does manual formatting (from it's internal byte array) as:
addressString.Append(string.Format(CultureInfo.InvariantCulture, "{0:x4}", new object[] { this.m_Numbers[0] })).Append(':');
addressString.Append(string.Format(CultureInfo.InvariantCulture, "{0:x4}", new object[] { this.m_Numbers[1] })).Append(':');
addressString.Append(string.Format(CultureInfo.InvariantCulture, "{0:x4}", new object[] { this.m_Numbers[2] })).Append(':');
addressString.Append(string.Format(CultureInfo.InvariantCulture, "{0:x4}", new object[] { this.m_Numbers[3] })).Append(':');
addressString.Append(string.Format(CultureInfo.InvariantCulture, "{0:x4}", new object[] { this.m_Numbers[4] })).Append(':');
addressString.Append(string.Format(CultureInfo.InvariantCulture, "{0:x4}", new object[] { this.m_Numbers[5] })).Append(':');
addressString.Append((int) ((this.m_Numbers[6] >> 8) & 0xff)).Append('.');
addressString.Append((int) (this.m_Numbers[6] & 0xff)).Append('.');
addressString.Append((int) ((this.m_Numbers[7] >> 8) & 0xff)).Append('.');
addressString.Append((int) (this.m_Numbers[7] & 0xff));
which will always return the second format you've shown.
Whether the flag "OS supports IPv6" flag is set seems to depend on both internal knowledge within the class (version must be > 2000) and it would appear an actual attempt to create an IPv6 socket - so if your on an XP machine with IPv6 disabled, I think you'll also get this second format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With