What dictates the formatting of IPv6 addresses by System.Net.IPAddress.ToString()?

夙愿已清 提交于 2019-12-05 10:51:52

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.

For some cases it is not dependent on OS supports IPv6 flag. I am getting this issue on windows server 2008 R2.I tried this

  String ipString = "2400:3C00:3FFE:0000:0000:5EFE:8999:48AA";
  System.Net.IPAddress address;
  IPAddress.TryParse(ipString, out address);

but address.ToString() is returning the value "2400:3c00:3ffe::5efe:137.153.72.170".

But if i change IP String to "2400:3C00:3FFE:1000:1000:5EFE:8999:48AA" its working properly.

In my case I needed to ensure consistent formatting without resorting to an unmanaged API call to WSAAddressToString, so I wrote the following extension method. Maybe this will help someone in the future:

/// <summary>
/// Returns the IPv4 or IPv6 address in standard notation. Any transitional suffix (i.e. an IPv4-like address
/// displayed in place of the final two segments of an IPv6 address) returned by .NET is converted to standard colon notation.
/// See http://stackoverflow.com/questions/4863352/what-dictates-the-formatting-of-ipv6-addresses-by-system-net-ipaddress-tostring.
/// </summary>
public static string ToStringNonTransitional(this System.Net.IPAddress oIPAddress)
{
    var sIP = oIPAddress.ToString();

    if (oIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
        return sIP; // Return IPv4 addresses untouched.

    if (oIPAddress.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
        throw new Exception(string.Format("Can't handle '{0}' in '{1}' format. (Only IPv4 or IPv6 supported.)", sIP, oIPAddress.AddressFamily.ToString()));

    if (!sIP.Contains("."))
        return sIP;

    try
    {
        var iTransitionalStart = sIP.LastIndexOf(":") + 1;
        var sTransitionalPart = sIP.Substring(iTransitionalStart);
        sIP = sIP.Substring(0, iTransitionalStart);
        var asOctects = sTransitionalPart.Split('.');
        sIP += string.Format("{0:x2}{1:x2}", Convert.ToInt16(asOctects[0]), Convert.ToInt16(asOctects[1])).TrimStart('0');
        sIP += ":" + string.Format("{0:x2}{1:x2}", Convert.ToInt16(asOctects[2]), Convert.ToInt16(asOctects[3])).TrimStart('0');

        return sIP;
    }
    catch (Exception ex)
    {
        throw new Exception("Failed to convert IPv6 address to standard notation: " + sIP, ex);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!