/// <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(nameof(begin)); } if (end == null) { throw new ArgumentNullException(nameof(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", nameof(end)); } var beginBytes = Begin.GetAddressBytes(); var endBytes = End.GetAddressBytes(); if (!Bits.GtECore(endBytes, beginBytes)) { throw new ArgumentException("Begin must be smaller than the End", nameof(begin)); } }
public bool Contains(IPAddressRange range) { if (range == null) { throw new ArgumentNullException(nameof(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 (Bits.LtECore(this.Begin.GetAddressBytes(), range.Begin.GetAddressBytes(), offset) && Bits.GtECore(this.End.GetAddressBytes(), range.End.GetAddressBytes(), offset)); }
public bool Contains(IPAddress ipaddress) { if (ipaddress == null) { throw new ArgumentNullException(nameof(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(Bits.LtECore(this.Begin.GetAddressBytes(), adrBytes, offset) && Bits.GtECore(this.End.GetAddressBytes(), adrBytes, offset)); }