/// <summary> /// Initializes a new instance of the <see cref="IPAddressRange"/> class. /// 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> /// <param name="begin">The begin IPAddress.</param> /// <param name="end">The end IPAddress.</param> public IPAddressRange(IPAddress begin, IPAddress end) { if (begin == null) { throw new ArgumentNullException("begin"); } if (end == null) { throw new ArgumentNullException("end"); } if (begin.AddressFamily != end.AddressFamily) { throw new ArgumentException("IPAddress must be of the same address family", "end"); } var beginBytes = begin.GetAddressBytes(); var endBytes = end.GetAddressBytes(); if (!Bitwise.LessEqual(endBytes, beginBytes)) { throw new ArgumentException("Begin must be smaller than the End", "begin"); } this.Begin = begin; this.End = end; }
/// <summary> /// Determines whether the current IPAddressRange contains the specified ip address range. /// </summary> /// <param name="ipAddressRange">The ip address range.</param> /// <returns>true if the current IPAddressRange contains the specified ip address range; otherwise, false.</returns> public bool Contains(IPAddressRange ipAddressRange) { if (ipAddressRange == null) { throw new ArgumentNullException("ipAddressRange"); } if (this.Begin.AddressFamily != ipAddressRange.Begin.AddressFamily) { return(false); } return (Bitwise.GreaterEqual(this.Begin.GetAddressBytes(), ipAddressRange.Begin.GetAddressBytes()) && Bitwise.LessEqual(this.End.GetAddressBytes(), ipAddressRange.End.GetAddressBytes())); }
/// <summary> /// Determines whether the current IPAddressRange contains the specified ip address. /// </summary> /// <param name="ipAddress">The ip address.</param> /// <returns>true if the current IPAddressRange contains the specified ip address; otherwise, false.</returns> public bool Contains(IPAddress ipAddress) { if (ipAddress == null) { throw new ArgumentNullException("ipAddress"); } if (ipAddress.AddressFamily != this.Begin.AddressFamily) { return(false); } var adrBytes = ipAddress.GetAddressBytes(); return (Bitwise.GreaterEqual(this.Begin.GetAddressBytes(), adrBytes) && Bitwise.LessEqual(this.End.GetAddressBytes(), adrBytes)); }
/// <summary> /// Determines whether the current IPAddressRange contains the specified ip address. /// </summary> /// <param name="ipAddressString">The ip address.</param> /// <returns>true if the current IPAddressRange contains the specified ip address; otherwise, false.</returns> public bool Contains(string ipAddressString) { if (string.IsNullOrEmpty(ipAddressString)) { throw new ArgumentNullException("ipAddressString"); } var ipAddress = IPAddress.Parse(ipAddressString); if (ipAddress.AddressFamily != this.Begin.AddressFamily) { return(false); } var adrBytes = ipAddress.GetAddressBytes(); return (Bitwise.GreaterEqual(this.Begin.GetAddressBytes(), adrBytes) && Bitwise.LessEqual(this.End.GetAddressBytes(), adrBytes)); }