Single IP address that is wrapped in such as way to quickly compare to a set of restricted IP addresses
        /// <summary> Compares a provided IP address against this range of IPs </summary>
        /// <param name="IpAddress"> IP address to compare to this range </param>
        /// <returns> 0 if this is in the range, 1 if this range is greater than 
        /// the IP, or -1 if the range is less than the comparison IP address </returns>
        public int CompareTo(ComparableIpAddress IpAddress)
        {
            if (!EndIpAddress.HasValue)
            {
                return StartIpAddress.CompareTo(IpAddress.Value);
            }

            if (StartIpAddress > IpAddress.Value)
                return 1;
            if (EndIpAddress.Value < IpAddress.Value)
                return -1;
            return 0;
        }
Ejemplo n.º 2
0
        /// <summary> Check to see if a given IP address is within the IP ranges in this set </summary>
        /// <param name="Address"> IP address to check for inclusion ( as a <see cref="ComparableIpAddress" /> object )</param>
        /// <returns> TRUE if the IP address is within the ranges, otherwise FALSE </returns>
        public bool Contains(ComparableIpAddress Address)
        {
            // If not defined, or no IP ranges included, just return FALSE
            if ((ranges == null) || (ranges.Count == 0))
            {
                return(false);
            }

            // Ensure the ranges have been pulled out
            if (prefixDictionary == null)
            {
                Ready();
            }

            // Look for a matching prefix
            if (!prefixDictionary.ContainsKey(Address.Prefix))
            {
                return(false);
            }

            // Get the list
            IList <SingleIpRangeV4> values = prefixDictionary[Address.Prefix];

            // If a three ranges or less, do the comparison with the slow method and get out
            if (values.Count <= 3)
            {
                return(values.Any(range => range.CompareTo(Address) == 0));
            }

            // Get the start and end indexes to get started
            int start_index = 0;
            int end_index   = values.Count - 1;

            // While the start and index include three ranges ( start, end, and at least one between ) keep
            // checking for where on the list to check next, by dividing the list in half each time
            while (end_index - start_index >= 2)
            {
                // Find the new middle point
                int middle_point = (start_index + end_index) / 2;

                // Perform the comparison to the middle range
                int comparison = values[middle_point].CompareTo(Address);

                // If the comparison shows the range holds the address, we found the fact it is contained!
                if (comparison == 0)
                {
                    return(true);
                }

                // If the comparison is negative, than the range is less than the address
                if (comparison < 0)
                {
                    start_index = middle_point;
                }
                else  // The range is greater than the address
                {
                    end_index = middle_point;
                }
            }

            // Now, just step through the last couple and check for containment
            for (int i = start_index; i <= end_index; i++)
            {
                if (values[i].CompareTo(Address) == 0)
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary> Check to see if a given IP address is within the IP ranges in this set </summary>
        /// <param name="Address"> IP address to check for inclusion ( as a <see cref="ComparableIpAddress" /> object )</param>
        /// <returns> TRUE if the IP address is within the ranges, otherwise FALSE </returns>
        public bool Contains(ComparableIpAddress Address)
        {
            // If not defined, or no IP ranges included, just return FALSE
            if ((ranges == null) || (ranges.Count == 0))
                return false;

            // Ensure the ranges have been pulled out
            if (prefixDictionary == null)
                Ready();

            // Look for a matching prefix
            if (!prefixDictionary.ContainsKey(Address.Prefix))
                return false;

            // Get the list
            IList<SingleIpRangeV4> values = prefixDictionary[Address.Prefix];

            // If a three ranges or less, do the comparison with the slow method and get out
            if (values.Count <= 3)
            {
                return values.Any(range => range.CompareTo(Address) == 0);
            }

            // Get the start and end indexes to get started
            int start_index = 0;
            int end_index = values.Count - 1;

            // While the start and index include three ranges ( start, end, and at least one between ) keep
            // checking for where on the list to check next, by dividing the list in half each time
            while (end_index - start_index >= 2)
            {
                // Find the new middle point
                int middle_point = (start_index + end_index)/2;

                // Perform the comparison to the middle range
                int comparison = values[middle_point].CompareTo(Address);

                // If the comparison shows the range holds the address, we found the fact it is contained!
                if (comparison == 0)
                    return true;

                // If the comparison is negative, than the range is less than the address
                if (comparison < 0)
                {
                    start_index = middle_point;
                }
                else  // The range is greater than the address
                {
                    end_index = middle_point;
                }
            }

            // Now, just step through the last couple and check for containment
            for (int i = start_index; i <= end_index; i++)
            {
                if (values[i].CompareTo(Address) == 0)
                    return true;
            }

            return false;
        }
Ejemplo n.º 4
0
        /// <summary> Check to see if a given IP address is within the IP ranges in this set </summary>
        /// <param name="Address"> IP address to check for inclusion ( as a string )</param>
        /// <returns> TRUE if the IP address is within the ranges, otherwise FALSE </returns>
        public bool Contains(string Address)
        {
            ComparableIpAddress address = new ComparableIpAddress(Address);

            return(Contains(address));
        }
 /// <summary> Check to see if a given IP address is within the IP ranges in this set </summary>
 /// <param name="Address"> IP address to check for inclusion ( as a string )</param>
 /// <returns> TRUE if the IP address is within the ranges, otherwise FALSE </returns>
 public bool Contains(string Address)
 {
     ComparableIpAddress address = new ComparableIpAddress(Address);
     return Contains(address);
 }