src/packages/EmbedIO/Utilities/IPParser.cs
/// <summary>
/// Parse IP-range string in CIDR notation. For example "12.15.0.0/16".
/// </summary>
/// <param name="range">The IP-range string.</param>
/// <returns>A collection of <see cref="IPAddress"/> parsed correctly from <paramref name="range"/>.</returns>
Parse IP-range string in CIDR notation.
Parse IP - range string in CIDR notation .
public static IEnumerable<IPAddress> ParseCidrNotation(string range)
        {
            if (!IsCidrNotation(range))
                return Enumerable.Empty<IPAddress>();
            var parts = range.Split('/');
            var prefix = parts[0];
            if (!byte.TryParse(parts[1], out var prefixLen))
                return Enumerable.Empty<IPAddress>();
            var prefixParts = prefix.Split('.');
            if (prefixParts.Select(x => byte.TryParse(x, out _)).Any(x => !x))
                return Enumerable.Empty<IPAddress>();
            uint ip = 0;
            for (var i = 0; i < 4; i++)
            {
                ip <<= 8;
                ip += uint.Parse(prefixParts[i], NumberFormatInfo.InvariantInfo);
            }
            var shiftBits = (byte)(32 - prefixLen);
            var ip1 = (ip >> shiftBits) << shiftBits;
            if ((ip1 & ip) != ip1) 
                return Enumerable.Empty<IPAddress>();
            var ip2 = ip1 >> shiftBits;
            for (var k = 0; k < shiftBits; k++)
            {
                ip2 = (ip2 << 1) + 1;
            }
            var beginIP = new byte[4];
            var endIP = new byte[4];
            for (var i = 0; i < 4; i++)
            {
                beginIP[i] = (byte)((ip1 >> ((3 - i) * 8)) & 255);
                endIP[i] = (byte)((ip2 >> ((3 - i) * 8)) & 255);
            }
            return GetAllIPAddresses(beginIP, endIP);
        }
public static IEnumerable < IPAddress > ParseCidrNotation ( string range ) { if ( ! IsCidrNotation ( range ) ) return Enumerable . Empty < IPAddress > ( ) ; var parts = range . Split ( ' ' ) ; var prefix = parts [ 0 ] ; if ( ! byte . TryParse ( parts [ 1 ] , out var prefixLen ) ) return Enumerable . Empty < IPAddress > ( ) ; var prefixParts = prefix . Split ( ' ' ) ; if ( prefixParts . Select ( x => byte . TryParse ( x , out _ ) ) . Any ( x => ! x ) ) return Enumerable . Empty < IPAddress > ( ) ; uint ip = 0 ; for ( var i = 0 ; i < 4 ; i ++ ) { ip <<= 8 ; ip += uint . Parse ( prefixParts [ i ] , NumberFormatInfo . InvariantInfo ) ; } var shiftBits = ( byte ) ( 32 - prefixLen ) ; var ip1 = ( ip >> shiftBits ) << shiftBits ; if ( ( ip1 & ip ) != ip1 ) return Enumerable . Empty < IPAddress > ( ) ; var ip2 = ip1 >> shiftBits ; for ( var k = 0 ; k < shiftBits ; k ++ ) { ip2 = ( ip2 << 1 ) + 1 ; } var beginIP = new byte [ 4 ] ; var endIP = new byte [ 4 ] ; for ( var i = 0 ; i < 4 ; i ++ ) { beginIP [ i ] = ( byte ) ( ( ip1 >> ( ( 3 - i ) * 8 ) ) & 255 ) ; endIP [ i ] = ( byte ) ( ( ip2 >> ( ( 3 - i ) * 8 ) ) & 255 ) ; } return GetAllIPAddresses ( beginIP , endIP ) ; }
Parse IP-range string in CIDR notation.
Parse IP - range string in CIDR notation .
// Check correct subnet address
A collection of  parsed correctly from .
A collection of parsed correctly from .
range
string
The IP-range string.
The IP - range string .