Converting IP address ranges into Netmask format

In this tutorial, we will demonstrate you on how to convert IP address ranges into Netmask format. A netmask is a 32-bit number that masks an IP address and divides the IP address into subnets. An example of netmask notation is 8.8.8.0/255.255.255.0. The example represents the IP ranges from 8.8.8.0 to 8.8.8.255. Please pay attention that this tutorial only for the conversion of IPv4 to Netmask format.

This tutorial will not show you a complete project, but the core function of how the conversion work. This function, iprange2netmask, takes 2 input parameters, namely the ipStart and ipEnd, the starting and the ending of an IP address respectively. You can either supply the IP address in dotted format, for example 202.186.13.4, or IP number, for instance, 3401190660, as the input parameters. The function will convert them into IP number if dotted IP address format were supplied and perform the calculation.

Below are the sample codes of the function written in several programming languages, such as PHP, VB.NET, C#, Java and Ruby.

<?php
    function iprange2netmask($ipStart, $ipEnd){
        if (is_string($ipStart) || is_string($ipEnd)){
            $start = ip2long($ipStart);
            $end = ip2long($ipEnd);
        }
        else{
            $start = $ipStart;
            $end = $ipEnd;
        }
 
        $result = array();
 
        while($end >= $start){
            $maxSize = 32;
            while ($maxSize > 0){
                $mask = hexdec(iMask($maxSize - 1));
                $maskBase = $start & $mask;
                if($maskBase != $start) break;
                $maxSize--;
            }
            $x = log($end - $start + 1)/log(2);
            $maxDiff = floor(32 - floor($x));
 
            if($maxSize < $maxDiff){
                $maxSize = $maxDiff;
            }
 
            $ip = long2ip($start);
            $netmask = cidr2netmask($maxSize);
            array_push($result, "$ip/$netmask");
            $start += pow(2, (32-$maxSize));
        }
        return $result;
    }
 
    function iMask($s){
        return base_convert((pow(2, 32) - pow(2, (32-$s))), 10, 16);
    }
 
    function cidr2netmask($cidr) {
         return long2ip(-1 << (32 - (int)$cidr));
    }
 
?>

 

Public Function IPrange2netmask(ByVal ipStart As String, ipEnd As String) As List(Of String)
    Dim startIP As Long = IP2Long(ipStart)
    Dim endIP As Long = IP2Long(ipEnd)
    Dim result As List(Of String) = New List(Of String)
 
    While (endIP >= startIP)
        Dim maxSize As Byte = 32
 
        While (maxSize > 0)
            Dim mask As Long = IMask(maxSize - 1)
            Dim maskBase As Long = startIP And mask
 
            If maskBase <> startIP Then
                Exit While
            End If
 
            maxSize -= 1
        End While
 
        Dim x As Double = Math.Log(endIP - startIP + 1) / Math.Log(2)
        Dim maxDiff As Byte = (32 - Math.Floor(x))
 
        If maxSize < maxDiff Then
            maxSize = maxDiff
        End If
 
        Dim ip As String = Long2IP(startIP)
        Dim netmask As String = CIDR2Netmask(maxSize)
        result.Add(ip + "/" + netmask)
        startIP += Math.Pow(2, (32 - maxSize))
    End While
    Return result
End Function
 
Public Function IPrange2netmask(ByVal ipStart As Integer, ipEnd As Integer) As List(Of String)
    Dim startIP As Long = ipStart
    Dim endIP As Long = ipEnd
    Dim result As List(Of String) = New List(Of String)
 
    While (endIP >= startIP)
        Dim maxSize As Byte = 32
 
        While (maxSize > 0)
            Dim mask As Long = IMask(maxSize - 1)
            Dim maskBase As Long = startIP And mask
 
            If maskBase <> startIP Then
                Exit While
            End If
 
            maxSize -= 1
        End While
 
        Dim x As Double = Math.Log(endIP - startIP + 1) / Math.Log(2)
        Dim maxDiff As Byte = (32 - Math.Floor(x))
 
        If maxSize < maxDiff Then
            maxSize = maxDiff
        End If
 
        Dim ip As String = Long2IP(startIP)
        Dim netmask As String = CIDR2Netmask(maxSize)
        result.Add(ip + "/" + netmask)
        startIP += Math.Pow(2, (32 - maxSize))
    End While
    Return result
End Function
 
Private Function IMask(ByVal s As Integer) As Long
    Return Math.Pow(2, 32) - Math.Pow(2, (32 - s))
End Function
 
Private Function IP2Long(ByVal ipAddress As String) As Long
    Dim ip As System.Net.IPAddress = Net.IPAddress.Parse(ipAddress)
    Return (CLng(ip.GetAddressBytes(0)) << 24) Or (CInt(ip.GetAddressBytes(1)) << 16) Or (CInt(ip.GetAddressBytes(2)) << 8) Or ip.GetAddressBytes(3)
End Function
 
Private Function Long2IP(ByVal ipAddress As Long) As String
    Dim tmpIP As New Net.IPAddress(ipAddress)
    Dim bytes() As Byte = tmpIP.GetAddressBytes()
    Array.Reverse(bytes)
    Dim addr As Long = CLng(BitConverter.ToUInt32(bytes, 0))
    Return New Net.IPAddress(addr).ToString()
End Function
 
Public Function CIDR2Netmask(cidr As Integer) As String
    Dim nmask As Int64 = Convert.ToInt64(New String("1"c, cidr).PadRight(32, "0"c), 2)
    Return (String.Join(".", New Net.IPAddress(nmask).GetAddressBytes.Reverse))
End Function

 

public static List<string> iprange2netmask(string ipStart, string ipEnd)
{
    long start = ip2long(ipStart);
    long end = ip2long(ipEnd);
    var result = new List<string>();
    while (end >= start)
    {
        byte maxSize = 32;
        while (maxSize > 0)
        {
            long mask = iMask(maxSize - 1);
            long maskBase = start & mask;
 
            if (maskBase != start)
            {
                break;
            }
            maxSize--;
        }
        double x = Math.Log(end - start + 1) / Math.Log(2);
        byte maxDiff = (byte)(32 - Math.Floor(x));
        if (maxSize < maxDiff)
        {
            maxSize = maxDiff;
        }
        string ip = long2ip(start);
        string netmask = cidr2netmask(maxSize);
        result.Add(ip + "/" + netmask);
        start += (long)Math.Pow(2, (32 - maxSize));
    }
    return result;
}
 
public static List<string> iprange2netmask(int ipStart, int ipEnd)
{
    long start = ipStart;
    long end = ipEnd;
    var result = new List<string>();
    while (end >= start)
    {
        byte maxSize = 32;
        while (maxSize > 0)
        {
            long mask = iMask(maxSize - 1);
            long maskBase = start & mask;
 
            if (maskBase != start)
            {
                break;
            }
            maxSize--;
        }
        double x = Math.Log(end - start + 1) / Math.Log(2);
        byte maxDiff = (byte)(32 - Math.Floor(x));
        if (maxSize < maxDiff)
        {
            maxSize = maxDiff;
        }
        string ip = long2ip(start);
        string netmask = cidr2netmask(maxSize);
        result.Add(ip + "/" + netmask);
        start += (long)Math.Pow(2, (32 - maxSize));
    }
    return result;
}
 
private static long iMask(int s)
{
    return (long)(Math.Pow(2, 32) - Math.Pow(2, (32 - s)));
}
 
private static string long2ip(long ipAddress)
{
    System.Net.IPAddress ip;
    if (System.Net.IPAddress.TryParse(ipAddress.ToString(), out ip))
    {
        return ip.ToString();
    }
    return "";
}
 
private static long ip2long(string ipAddress)
{
    System.Net.IPAddress ip;
    if (System.Net.IPAddress.TryParse(ipAddress, out ip))
    {
        return (((long)ip.GetAddressBytes()[0] << 24) | ((long)ip.GetAddressBytes()[1] << 16) |
        ((long)ip.GetAddressBytes()[2] << 8) | ip.GetAddressBytes()[3]);
    }
    return -1;
}
 
private static string cidr2netmask(int cidr)
{
    var nmask = 0xFFFFFFFF;
    nmask <<= 32 - cidr;
    byte[] bytes = BitConverter.GetBytes(nmask);
    Array.Reverse(bytes);
    nmask = BitConverter.ToUInt32(bytes, 0);
    var netmask = new System.Net.IPAddress(nmask);
    return netmask.ToString();
}

 

public static List<String> iprange2netmask( String ipStart, String ipEnd ) {
    long start = ip2long(ipStart);
    long end = ip2long(ipEnd);
 
    ArrayList<String> result = new ArrayList<String>();
    while ( end >= start ) {
        byte maxSize = 32;
        while ( maxSize > 0) {
            long mask = iMask( maxSize - 1 );
            long maskBase = start & mask;
 
            if ( maskBase != start ) {
                break;
            }
 
            maxSize--;
        }
        double x = Math.log( end - start + 1) / Math.log( 2 );
        byte maxDiff = (byte)( 32 - Math.floor( x ) );
        if ( maxSize < maxDiff) {
            maxSize = maxDiff;
        }
        String ip = long2ip(start);
        String netmask = cidr2netmask(maxSize);
        result.add( ip + "/" + netmask);
        start += Math.pow( 2, (32 - maxSize) );
    }
    return result;
}
 
public static List<String> iprange2netmask( int ipStart, int ipEnd ) {
    long start = ipStart;
    long end = ipEnd;
 
    ArrayList<String> result = new ArrayList<String>();
    while ( end >= start ) {
        byte maxSize = 32;
        while ( maxSize > 0) {
            long mask = iMask( maxSize - 1 );
            long maskBase = start & mask;
 
            if ( maskBase != start ) {
                break;
            }
 
            maxSize--;
        }
        double x = Math.log( end - start + 1) / Math.log( 2 );
        byte maxDiff = (byte)( 32 - Math.floor( x ) );
        if ( maxSize < maxDiff) {
            maxSize = maxDiff;
        }
        String ip = long2ip(start);
        String netmask = cidr2netmask(maxSize);
        result.add( ip + "/" + netmask);
        start += Math.pow( 2, (32 - maxSize) );
    }
    return result;
}
 
private static long iMask(int s) {
    return Math.round(Math.pow(2, 32) - Math.pow(2, (32 - s)));
}
 
private static long ip2long(String ipstring) {
    String[] ipAddressInArray = ipstring.split("\\.");
    long num = 0;
    long ip = 0;
    for (int x = 3; x >= 0; x--) {
        ip = Long.parseLong(ipAddressInArray[3 - x]);
        num |= ip << (x << 3);
    }
    return num;
}
 
private static String long2ip(long longIP) {
    StringBuffer sbIP = new StringBuffer("");
    sbIP.append(String.valueOf(longIP >>> 24));
    sbIP.append(".");
    sbIP.append(String.valueOf((longIP & 0x00FFFFFF) >>> 16));
    sbIP.append(".");
    sbIP.append(String.valueOf((longIP & 0x0000FFFF) >>> 8));
    sbIP.append(".");
    sbIP.append(String.valueOf(longIP & 0x000000FF));
 
    return sbIP.toString();
}
 
private static String cidr2netmask(long cidr) {
    int mask = 0xFFFFFFFF << (32 - cidr);
    int value = mask;
    byte[] bytes = new byte[]{ (byte)(value >>> 24), (byte)(value >> 16 & 0xFF), (byte)(value >> 8 & 0xFF), (byte)(value & 0xFF) };
    InetAddress netAddr = InetAddress.getByAddress(bytes);
    return netAddr.getHostAddress().toString();
}

 

require 'ipaddr'
 
def iprange2netmask(ipstart, ipend)
    if ipstart.kind_of?(String) || ipend.kind_of?(String)
        startR = ip2long(ipstart)
        endR = ip2long(ipend)
    else
        startR = ipstart
        endR = ipend
    end
 
    result = Array.new
 
    while endR >= startR do
        maxSize = 32
        while maxSize > 0 do
            mask = (iMask(maxSize - 1))
            maskBase = startR & mask
            if maskBase != startR
                break
            end
            maxSize-=1
        end
        x = Math.log(endR - startR + 1)/Math.log(2)
        maxDiff = (32 - x.floor).floor
 
        if maxSize < maxDiff
            maxSize = maxDiff
        end
 
        ip = long2ip(startR)
        netmask = cidr2netmask(maxSize)
        cidr = [ip, netmask].join('/')
        result.push cidr
        startR += 2**(32-maxSize)
    end
    return result
end
 
def iMask(s)
    return (2**32 - 2**(32-s))
end
 
def long2ip(num)
    return IPAddr.new(num, Socket::AF_INET).to_s
end
 
def ip2long(ip)
    return IPAddr.new(ip).to_i
end
 
def cidr2netmask(cidr)
    return IPAddr.new('255.255.255.255').mask(cidr).to_s
end

 

The result that return by the iprange2netmask function is an Array that contain the list of Netmask formatted IP address. Looping have to be done to get all the Netmask formatted IP address from the return result. The following figure shows the sample output with ipStart:192.168.1.0 and ipEnd:192.168.1.9 of the converting IP address ranges into Netmask format.

IP Range to Netmask

 

Was this article helpful?

Related Articles