C# : Compare and Filter IP Address

C# : Compare and Filter IP Address

Hi Dosto,

Please find class below to compare single ip address and range of ip addresses.



public static class IPAddressFilter
    {
        private static string SingleIP = "192.168.101.0,127.0.0.1,:;1";
 
        private static string RangeIP = "192.168.101.50-192.168.101.100,192.168.100.25-192.168.100.52";
 
        public static bool CompareIP(string ipAddress)
        {
            if (CompareSingleIP(ipAddress))
            {
                return true;
            }
            if (CompareIPInRange(ipAddress))
            {
                return true;
            }
            return false;
        }
 
        public static bool CompareSingleIP(string strIPAddress)
        {
 
            try
            {
                //Split the users IP address into it's 4 octets (Assumes IPv4) 
                string[] incomingOctets = strIPAddress.Trim().Split(new char[] { '.' });
                string[] validIpAddresses = SingleIP.Trim().Split(new char[] { ',' });
 
                //Iterate through each valid IP address 
                foreach (var validIpAddress in validIpAddresses)
                {
                    //Return true if valid IP address matches the users 
                    if (validIpAddress.Trim() == strIPAddress)
                    {
                        return true;
                    }
 
                    //Split the valid IP address into it's 4 octets 
                    string[] validOctets = validIpAddress.Trim().Split(new char[] { '.' });
 
                    bool matches = true;
 
                    //Iterate through each octet 
                    for (int index = 0; index < validOctets.Length; index++)
                    {
                        //Skip if octet is an asterisk indicating an entire 
                        //subnet range is valid 
                        if (validOctets[index] != "*")
                        {
                            if (validOctets[index] != incomingOctets[index])
                            {
                                matches = false;
                                break//Break out of loop 
                            }
                        }
                    }
 
                    if (matches)
                    {
                        return true;
                    }
                }
 
            }
            catch (System.Exception)
            {
 
            }
            return false;
        }
        public static bool CompareIPInRange(string strIPAddress)
        {
            try
            {
                //Split the users IP address into it's 4 octets (Assumes IPv4) 
                string[] incomingOctets = strIPAddress.Trim().Split(new char[] { '.' });
                string[] validIpAddressesRange = RangeIP.Trim().Split(new char[] { ',' });
 
                //Iterate through each valid IP address 
                foreach (var validIpAddress in validIpAddressesRange)
                {
                    IPAddress beginIP = IPAddress.Parse(validIpAddress.Trim().Split(new char[] { '-' })[0]);
                    IPAddress endIP = IPAddress.Parse(validIpAddress.Trim().Split(new char[] { '-' })[1]);
 
                    if (IPAddressRange(beginIP, endIP, IPAddress.Parse(strIPAddress)))
                    {
                        return true;
                    }
                }
 
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return false;
        }
        public static bool IPAddressRange(IPAddress lower, IPAddress upper, IPAddress address)
        {
            AddressFamily addressFamily = lower.AddressFamily;
            byte[] lowerBytes = lower.GetAddressBytes();
            byte[] upperBytes = upper.GetAddressBytes();
 
            if (address.AddressFamily != addressFamily)
            {
                return false;
            }
            byte[] addressBytes = address.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;
        }
 
 
 
    }
Tagged: C#

No comments:

Post a Comment