Beispiel #1
0
        /// <summary>
        /// Returns value that indicates the specified address is in range of ip addresses.
        /// </summary>
        /// <param name="address">An <see cref="System.Net.IPAddress"/> for check.</param>
        /// <returns>true if address is in range; otherwise false;</returns>
        public virtual bool IsInRange(IPAddress address)
        {
            EnsureAddressFamilyEquals();
            if (address.AddressFamily != Lower.AddressFamily)
            {
                return(false);
            }

            byte[] addressBytes = address.GetAddressBytes();
            byte[] lowerBytes   = Lower.GetAddressBytes();
            byte[] upperBytes   = Upper.GetAddressBytes();

            bool lowerBoundary = true, upperBoundary = true;

            for (int i = 0; i < lowerBytes.Length &&
                 (lowerBoundary || upperBoundary); i++)
            {
                if ((lowerBoundary && addressBytes[i] < lowerBytes[i]) ||
                    (upperBoundary && addressBytes[i] > upperBytes[i]))
                {
                    return(false);
                }

                lowerBoundary &= (addressBytes[i] == lowerBytes[i]);
                upperBoundary &= (addressBytes[i] == upperBytes[i]);
            }

            return(true);
        }
        public bool IPInRange(IPAddress candidate)
        {
            byte[] lowerBytes   = Lower.GetAddressBytes();
            byte[] upperBytes   = Upper.GetAddressBytes();
            byte[] addressBytes = candidate.GetAddressBytes();

            bool lowerBoundary = true, upperBoundary = true;

            for (int i = 0; i < lowerBytes.Length && (lowerBoundary || upperBoundary); i++)
            {
                if ((lowerBoundary && addressBytes[i] < lowerBytes[i]) ||
                    (upperBoundary && addressBytes[i] > upperBytes[i]))
                {
                    return(false);
                }

                lowerBoundary &= (addressBytes[i] == lowerBytes[i]);
                upperBoundary &= (addressBytes[i] == upperBytes[i]);
            }

            return(true);
        }