1

I currently have this method derived from a couple of other S.O. posts:

public bool IsIPAddressInRange(IPAddress ipAddress)
{
    int startIntAddress = BitConverter.ToInt32(StartingIPAddress.GetAddressBytes(), 0);
    int endIntAddress = BitConverter.ToInt32(EndingIPAddress.GetAddressBytes(), 0);

    int intIpAddress = BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0);

    return startIntAddress <= intIpAddress && intIpAddress <= endIntAddress;
}

There are clearly some pitfalls in trying to extend this logic to support IPv6. Namely:

  • Do I need to use a BigInt instead of Int32 to support the longer field length?
  • The representation of an IPv6 Address does not appear to be easily convertible to Bytes?

Just wondering if anyone has any advice on how to tackle this.

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237

0 Answers0