public bool IsInRange(IPAddress address) { // Some people just have to be like that... if (address.AddressFamily != Family || address.GetLength() != LowerBoundary.Length) return false; var bytes = address.GetAddressBytes(); var lowerBoundary = true; var upperBoundary = true; for (var i = 0; i < LowerBoundary.Length && (lowerBoundary || upperBoundary); i++) { var currentByte = bytes[i]; var lowerByte = LowerBoundary[i]; var upperByte = UpperBoundary[i]; if ((lowerBoundary && currentByte < lowerByte) || (upperBoundary && currentByte > upperByte)) return false; lowerBoundary &= currentByte == lowerByte; upperBoundary &= currentByte == upperByte; } return true; }
public IPAddressRange(IPAddress lower, IPAddress upper) { Contract.Requires(lower.AddressFamily == upper.AddressFamily); Contract.Requires(lower.GetLength() == upper.GetLength()); Family = lower.AddressFamily; LowerBoundary = lower.GetAddressBytes(); UpperBoundary = upper.GetAddressBytes(); Contract.Assert(LowerBoundary.Length >= 4); Contract.Assert(UpperBoundary.Length >= 4); }