Why does IPAddress.MapToIPv4() throw ArgumentOutOfRangeException?

前端 未结 1 1045
小鲜肉
小鲜肉 2021-02-13 13:39

This code throws an ArgumentOutOfRangeException on the last line

var initAddress = IPAddress.Parse(\"1.65.128.190\");
var ipv6Address = initAddress.MapToIPv6();
         


        
1条回答
  •  广开言路
    2021-02-13 14:30

    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 ints. 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.

    0 讨论(0)
提交回复
热议问题