N
Noël Danjou
Hello,
I'd like to verify if an IP address (as an IPAddress parameter) is in a
range defined by two other IP addresses (also passed as IPAddress
parameters), something like IsLoopback() but not only for 127.x.y.z and not
only for x,y,z in the 0-255 range.
There is an Equals member function but it only compares if two IP addresses
are equal which is not enough in my case what I'd need is a Compare()
function which returns <0, 0 or >0 like String.Compare().
Currently I target IPv4 but I'd like it to be compatible with IPv6 too if
the IPAddress and the range are IPv6. In the Framework V1.0 I was using the
Address property like below, but in the V1.1 it is marked as obsolete.
Here is what I was doing:
public static bool IsAddressInRange(IPAddress addrSrc, IPAddressRange range)
{
long addr = IPAddress.NetworkToHostOrder(addrSrc.Address);
return (addr >= IPAddress.NetworkToHostOrder(range.lo.Address) &&
addr <= IPAddress.NetworkToHostOrder(range.hi.Address));
}
Could you recommend a method to do that for both address families? Thank
you.
I'd like to verify if an IP address (as an IPAddress parameter) is in a
range defined by two other IP addresses (also passed as IPAddress
parameters), something like IsLoopback() but not only for 127.x.y.z and not
only for x,y,z in the 0-255 range.
There is an Equals member function but it only compares if two IP addresses
are equal which is not enough in my case what I'd need is a Compare()
function which returns <0, 0 or >0 like String.Compare().
Currently I target IPv4 but I'd like it to be compatible with IPv6 too if
the IPAddress and the range are IPv6. In the Framework V1.0 I was using the
Address property like below, but in the V1.1 it is marked as obsolete.
Here is what I was doing:
public static bool IsAddressInRange(IPAddress addrSrc, IPAddressRange range)
{
long addr = IPAddress.NetworkToHostOrder(addrSrc.Address);
return (addr >= IPAddress.NetworkToHostOrder(range.lo.Address) &&
addr <= IPAddress.NetworkToHostOrder(range.hi.Address));
}
Could you recommend a method to do that for both address families? Thank
you.