How to check equality between different types of ip addresses? RRS feed

  • Question

  • I wish to compare equality between IPV4 and IPV6 format of ip address

    for e.g. how to compare ::1 and ?

    Kiran Saravade [MCP,MCTS]

    Tuesday, December 11, 2012 6:03 AM

All replies

  • Do you want to check if the IPv4 and IPv6 address belong to the same host?

    As far as i know, you can not translate or compare an IPv4 into an IPv6 address. Maybe you are looking for something like tunneling IPv4 in IPv6: http://tools.ietf.org/html/rfc2473

    Tuesday, December 11, 2012 1:26 PM
  • Hi,

    I just want to compare equality between IP addresses in C#..

    I am not looking for any tool. I want source code in C#.

    Kiran Saravade [MCP,MCTS]

    Wednesday, December 12, 2012 4:47 AM
  • Define Equal. Let me explain that a little more. IPv4 and IPv6 share a common name and purpose but they are different formats. The only possible way to say something would be equal would be to compare only certain reserved addresses. There is not a 1 to 1 mapping.



    Friday, December 14, 2012 2:22 PM
  • Run IPAddress.MapToIPv4() for IPv6 address and compare. Remember to catch the exceptions.

    You may also want to add special handling for loopback address according to RFC 4291.

    • Edited by cheong00 Saturday, December 15, 2012 3:09 AM
    Saturday, December 15, 2012 2:34 AM
  • I do not want to change PC settings of any.

    I want to my c# program will do the things for me.

    Kiran Saravade [MCP,MCTS]

    Tuesday, December 18, 2012 6:40 AM
  • I don't see any of the answers here require changing PC settings. Can you point out which one?
    Wednesday, December 19, 2012 1:57 AM
  • Hey,

    Thanks for the reply .. but i am not able to find MapToIPv4() function anywhere.

    Can you please check this please where it will be available?

    Kiran Saravade [MCP,MCTS]

    Wednesday, December 19, 2012 6:46 AM
  • From the MSDN article, you can see that it's in the System.Net namespace, so "using System.Net;" is required if you haven't already add that. And from the article, you can see MapToIPv4() is supported in .NET v4.5 only. Which mean that if your code aren't targeting .NET v4.5, you have to use methods others described.

    You can find varies usage in the main page of IPAddress class, including the use of IPAddress.IsLoopback to determine whether it's loopback interface or not.

    When using .MapToIPv4() on an IPAddress class that holds IPv6 address, it'll return another IPAddress class that holds IPv4 equivalent if exist, which you can in turn use .Equal() to compare to another IPAddress class that holds IPv4 address you want to compare.


    However even if you're using .NET v2, writing .MapToIPv4() yourself is trival. Just using information found in Section 2.5.5 of RFC 4291. Get the bytes from IPv6 through .GetAddressBytes(). Check whether bytes index 0 to 14 are 0, and index 15 is either 0 or 255. If all these are true, copy index 16 to 31 and covert it back to IPv4 (by passing the new array in the IPAddress's constructor) and you're done.

    • Edited by cheong00 Wednesday, December 19, 2012 7:47 AM
    Wednesday, December 19, 2012 7:26 AM