This code throws an ArgumentOutOfRangeException on the last line
var initAddress = IPAddress.Parse(\"1.65.128.190\");
var ipv6Address = initAddress.MapToIPv6();
Ok, I've actually verified this, so let me post this as an answer.
The IPAddress
class has an error when mapping the address back to IPv4.
According to the .NET reference code, it does this:
long address =
(((m_Numbers[6] & 0x0000FF00) >> 8) | ((m_Numbers[6] & 0x000000FF) << 8)) |
((((m_Numbers[7] & 0x0000FF00) >> 8) | ((m_Numbers[7] & 0x000000FF) << 8)) << 16);
The problem should be quite obvious to anyone doing bitwise operations in .NET - the numbers are all int
s. So shifting the second ushort
(m_Numbers[7]
) will give a negative value, because the most significant bit is 1
. This means that all IPv4 addresses that end with a byte higher than 127
will cause an error when mapping back from IPv6.
The simple fix would be this:
long address =
(((m_Numbers[6] & 0x0000FF00) >> 8) | ((m_Numbers[6] & 0x000000FF) << 8))
|
(
(uint)(((m_Numbers[7] & 0x0000FF00) >> 8) | ((m_Numbers[7] & 0x000000FF) << 8))
<< 16
);
Just by casting the int
to an uint
before doing the bitshift does the trick.
Bitwise operations can be quite tricky when you factor in signed types. I guess the code was copied from a C++ library or something, where this issue wouldn't manifest.