开发者

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

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.


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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜