public bool Contains(IPAddressRange range) { if (range == null) { throw new ArgumentNullException("range"); } if (this.Begin.AddressFamily != range.Begin.AddressFamily) { return(false); } var offset = 0; if (Begin.IsIPv4MappedToIPv6 && range.Begin.IsIPv4MappedToIPv6) { offset = 12; //ipv4 has prefix of 10 zero bytes and two 255 bytes. } return (BitsUtil.LtECore(this.Begin.GetAddressBytes(), range.Begin.GetAddressBytes(), offset) && BitsUtil.GtECore(this.End.GetAddressBytes(), range.End.GetAddressBytes(), offset)); }
public bool Contains(IPAddress ipaddress) { if (ipaddress == null) { throw new ArgumentNullException("ipaddress"); } if (ipaddress.AddressFamily != this.Begin.AddressFamily) { return(false); } var offset = 0; if (Begin.IsIPv4MappedToIPv6 && ipaddress.IsIPv4MappedToIPv6) { offset = 12; //ipv4 has prefix of 10 zero bytes and two 255 bytes. } var adrBytes = ipaddress.GetAddressBytes(); return (BitsUtil.LtECore(this.Begin.GetAddressBytes(), adrBytes, offset) && BitsUtil.GtECore(this.End.GetAddressBytes(), adrBytes, offset)); }
/// <summary> /// Create a new range from a begin and end address. /// Throws an exception if Begin comes after End, or the /// addresses are not in the same family. /// </summary> public IPAddressRange(IPAddress begin, IPAddress end) { if (begin == null) { throw new ArgumentNullException("begin"); } if (end == null) { throw new ArgumentNullException("end"); } Begin = new IPAddress(begin.GetAddressBytes()); End = new IPAddress(end.GetAddressBytes()); if (Begin.AddressFamily != End.AddressFamily) { throw new ArgumentException("Elements must be of the same address family", "end"); } var beginBytes = Begin.GetAddressBytes(); var endBytes = End.GetAddressBytes(); if (!BitsUtil.GtECore(endBytes, beginBytes)) { throw new ArgumentException("Begin must be smaller than the End", "begin"); } }