Example #1
0
        /// <summary>
        /// Method adds provided IP address and range to the whitelist
        /// </summary>
        /// <param name="ip">IP address we are adding to the whitelist</param>
        /// <param name="bits">network bits of the address. This is used to calculate upper and lower IP address of the range.</param>
        /// <returns>true if ip was added, false if it allready exist in the whitelist</returns>
        public bool AddWhiteList(IPAddress ip, int bits)
        {
            IpNumber _IP = GetIpRangeFromIpAddress(ip, bits);

            if (!_whiteList.ContainsKey(ip.ToString()))
            {
                _whiteList.Add(ip.ToString(), _IP);
                return(true);
            }
            return(false);
        }
Example #2
0
        /// <summary>
        /// Internal helper method to check if provided ip is in range of provided filter.
        /// </summary>
        /// <param name="ip">IP address we are checking in byte format.</param>
        /// <param name="filter">Ip filter (IP range) we are checking against</param>
        /// <returns>true if ip is in range, false if it is not.</returns>
        private bool IpInRange(byte[] ip, IpNumber filter)
        {
            bool _result = true;

            for (int i = 0; i < ip.Length; i++)
            {
                if ((ip[i] >= filter.IPAddressLower[i]) && (ip[i] <= filter.IPAddressUpper[i]))
                {
                    //
                }
                else
                {
                    _result = false;
                }
            }
            return(_result);
        }
Example #3
0
        /// <summary>
        /// internal helper method that calculates upper and lower ip address of ip range, from provided IP address and nework bit mask.
        /// ip if provided as IPAddress class and mask is provided as bits (for example: 0, 8, 16, 24, 32, ...)
        /// </summary>
        /// <param name="ip">IP to be used for range caclulation</param>
        /// <param name="bits">Network bits to be used for range calculation</param>
        /// <returns>IPNumber filter object contain provided ip address and upper and lowe ranges of the network</returns>
        private IpNumber GetIpRangeFromIpAddress(IPAddress ip, int bits)
        {
            uint _IPMask;

            if (bits != 32)
            {
                _IPMask = ~(uint.MaxValue >> bits);
            }
            else
            {
                _IPMask = uint.MaxValue;
            }

            byte[] _ipBytes = ip.GetAddressBytes();

            byte[] _maskBytes;
            if (BitConverter.IsLittleEndian)
            {
                _maskBytes = BitConverter.GetBytes(_IPMask).Reverse().ToArray();
            }
            else
            {
                _maskBytes = BitConverter.GetBytes(_IPMask).ToArray();
            }

            byte[] _lowerIPBytes = new byte[_ipBytes.Length];
            byte[] _upperIPBytes = new byte[_ipBytes.Length];
            for (int i = 0; i < _ipBytes.Length; i++)
            {
                _lowerIPBytes[i] = (byte)(_ipBytes[i] & _maskBytes[i]);
                _upperIPBytes[i] = (byte)(_ipBytes[i] | ~_maskBytes[i]);
            }

            IpNumber _IP = new IpNumber();

            _IP.IPAddress      = _ipBytes;
            _IP.IPAddressLower = _lowerIPBytes;
            _IP.IPAddressUpper = _upperIPBytes;
            return(_IP);
        }