Instantate your ip addresses as instances of System.Net.IPAddress
. The look at the following methods:
IPAddress.Equals()
IPAddress.MapToIPv4()
IPAddress.MapToIPv6()
You'll probably want to add special handling for special addresses (such as the TCP/IP loopback adapter: That is a single IPv6 address, ::1
, while for IPv4, even though the most commonly used address for that purpose is 127.0.0.1
, the IETF has reserved the entire 127/8 block (127.0.0.0
–127.255.255.255
inclusive) for that purpose. How you determine equality (or even equivalency) is debatable.
Since IPv4 and IPv6 are completely different and independent addressing schemes, one might reasonably argue that the only true way of determining equivalency is if they both map to the same endpoint (MAC address/network adapter).