Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <remarks>A null or empty string passed as the ipAddress will return true. An invalid ipAddress will be returned as true.</remarks>
        /// <returns></returns>
        public static bool IsNonRoutableIpAddress(string ipAddress)
        {
            //Reference: http://en.wikipedia.org/wiki/Reserved_IP_addresses

            //if the ip address string is empty or null string, we consider it to be non-routable
            if (string.IsNullOrEmpty(ipAddress))
            {
                return(true);
            }

            if (!IPAddress.TryParse(ipAddress, out var tempIpAddress))
            {
                return(true);
            }

            var ipAddressBytes = tempIpAddress.GetAddressBytes();

            if (IPFormatter.IsIPv4Address(tempIpAddress))
            {
                if (IsIpAddressInRange(ipAddressBytes, @"10.0.0.0/8"))                 //Class A Private network check
                {
                    return(true);
                }
                if (IsIpAddressInRange(ipAddressBytes, @"172.16.0.0/12"))                 //Class B private network check
                {
                    return(true);
                }
                if (IsIpAddressInRange(ipAddressBytes, @"192.168.0.0/16"))                 //Class C private network check
                {
                    return(true);
                }
                if (IsIpAddressInRange(ipAddressBytes, @"127.0.0.0/8"))                 //Loopback
                {
                    return(true);
                }
                if (IsIpAddressInRange(ipAddressBytes, @"0.0.0.0/8"))                   //reserved for broadcast messages
                {
                    return(true);
                }
                return(false);
            }

            //if ipAddress is IPv6
            if (IPFormatter.IsIPv6Address(tempIpAddress))
            {
                if (IsIpAddressInRange(ipAddressBytes, @"::/128"))                       //Unspecified address
                {
                    return(true);
                }
                if (IsIpAddressInRange(ipAddressBytes, @"::1/128"))                     //Loopback address for localhost
                {
                    return(true);
                }
                if (IsIpAddressInRange(ipAddressBytes, @"2001:db8::/32"))                   //Addresses used in documentation
                {
                    return(true);
                }
                throw new NotImplementedException();
                //return false;
            }
            return(true);
        }
Beispiel #2
0
        private static bool IsIpAddressInRange(byte[] ipAddressBytes, string reservedIpAddress)
        {
            if (string.IsNullOrEmpty(reservedIpAddress))
            {
                return(false);
            }

            if (ipAddressBytes == null)
            {
                return(false);
            }

            //Split the reserved ip address into a bitmask and ip address
            var ipAddressSplit = reservedIpAddress.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (ipAddressSplit.Length != 2)
            {
                return(false);
            }

            var ipAddressRange = ipAddressSplit[0];

            if (!IPAddress.TryParse(ipAddressRange, out var ipAddress))
            {
                return(false);
            }

            // Convert the IP address to bytes.
            var ipBytes = ipAddress.GetAddressBytes();

            //parse the bits
            if (!int.TryParse(ipAddressSplit[1], out var bits))
            {
                bits = 0;
            }

            // BitConverter gives bytes in opposite order to GetAddressBytes().
            byte[] maskBytes = null;
            if (IPFormatter.IsIPv4Address(ipAddress))
            {
                var mask = ~(uint.MaxValue >> bits);
                maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray();
            }
            else if (IPFormatter.IsIPv6Address(ipAddress))
            {
                //128 places
                var bitArray = new BitArray(128, false);

                //shift <bits> times to the right
                ShiftRight(bitArray, bits, true);

                //turn into byte array
                maskBytes = ConvertToByteArray(bitArray).Reverse().ToArray();
            }


            var result = true;

            //Calculate
            for (var i = 0; i < ipBytes.Length; ++i)
            {
                if (maskBytes != null)
                {
                    result &= (byte)(ipAddressBytes[i] & maskBytes[i]) == ipBytes[i];
                }
            }

            return(result);
        }