Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert IP address range to CIDR in Java?

Tags:

java

ip

cidr

I am trying to convert the IP address range into the CIDR notation in Java. Can someone provide an example of how this can be achieved?

I used the SubnetUtils for converting the CIDR to IP address range, but I can't figure out the other way round.

For example: (using http://ip2cidr.com/)

Input 1: 5.10.64.0
Input 2: 5.10.127.255
Result: 5.10.64.0/18

Thanks, Dhaval

like image 325
Dhaval Kotecha Avatar asked Oct 30 '15 19:10

Dhaval Kotecha


3 Answers

The open-source IPAddress Java library can do this for you. Disclaimer: I am the project manager of the IPAddress library.

Here is sample code to do it:

static void toPrefixBlocks(String str1, String str2) {
    IPAddressString string1 = new IPAddressString(str1);
    IPAddressString string2 = new IPAddressString(str2);
    IPAddress one = string1.getAddress(), two = string2.getAddress();
    IPAddressSeqRange range = one.toSequentialRange(two);
    System.out.println("starting with range " + range);
    IPAddress blocks[] = range.spanWithPrefixBlocks();
    System.out.println("prefix blocks are " + Arrays.asList(blocks));
}

This is how you would use IPAddress to produce the range from the original CIDR string:

static String[] toRange(String str) {
    IPAddressString string = new IPAddressString(str);
    IPAddress addr = string.getAddress();
    System.out.println("starting with CIDR " + addr);
    IPAddress lower = addr.getLower(), upper = addr.getUpper();
    System.out.println("range is " + lower + " to " + upper);
    return new String[] {lower.toString(), upper.toString()};
}

For your example, we run this code:

String strs[] = toRange("5.10.64.0/18");
System.out.println();
toPrefixBlocks(strs[0], strs[1]);

The output is:

starting with CIDR 5.10.64.0/18
range is 5.10.64.0/18 to 5.10.127.255/18

starting with range 5.10.64.0 -> 5.10.127.255
prefix blocks are [5.10.64.0/18]
like image 186
Sean F Avatar answered Nov 20 '22 05:11

Sean F


import java.util.ArrayList;
import java.util.List;

public class RangeToCidr {
    public static List<String> range2cidrlist( String startIp, String endIp ) {
        // check parameters
        if (startIp == null || startIp.length() < 8 ||
            endIp == null || endIp.length() < 8) return null;
        long start = ipToLong(startIp);
        long end = ipToLong(endIp);
        // check parameters
        if (start > end) return null;

        List<String> result = new ArrayList<String>();
        while (start <= end) {
            // identify the location of first 1's from lower bit to higher bit of start IP
            // e.g. 00000001.00000001.00000001.01101100, return 4 (100)
            long locOfFirstOne = start & (-start);
            int maxMask = 32 - (int) (Math.log(locOfFirstOne) / Math.log(2));

            // calculate how many IP addresses between the start and end
            // e.g. between 1.1.1.111 and 1.1.1.120, there are 10 IP address
            // 3 bits to represent 8 IPs, from 1.1.1.112 to 1.1.1.119 (119 - 112 + 1 = 8)
            double curRange = Math.log(end - start + 1) / Math.log(2);
            int maxDiff = 32 - (int) Math.floor(curRange);

            // why max?
            // if the maxDiff is larger than maxMask
            // which means the numbers of IPs from start to end is smaller than mask range
            // so we can't use as many as bits we want to mask the start IP to avoid exceed the end IP
            // Otherwise, if maxDiff is smaller than maxMask, which means number of IPs is larger than mask range
            // in this case we can use maxMask to mask as many as IPs from start we want.
            maxMask = Math.max(maxDiff, maxMask);

            // Add to results
            String ip = longToIP(start);
            result.add(ip + "/" + maxMask);
            // We have already included 2^(32 - maxMask) numbers of IP into result
            // So the next round start must add that number
            start += Math.pow(2, (32 - maxMask));
        }
        return result;
    }

    private static long ipToLong(String strIP) {
        String[] ipSegs = strIP.split("\\.");
        long res = 0;
        for (int i = 0; i < 4; i++) {
            res += Long.valueOf(ipSegs[i]) << (8 * (3 - i));
        }
        return res;
    }

    private static String longToIP(long longIP) {
        StringBuffer sb = new StringBuffer();
        sb.append(longIP >>> 24).append(".")
          .append((longIP & 0x00FFFFFF) >>> 16).append(".")
          .append(String.valueOf((longIP & 0x0000FFFF) >>> 8)).append(".")
          .append(String.valueOf(longIP & 0x000000FF));

        return sb.toString();
    }
}
like image 8
叶泰航 Avatar answered Nov 20 '22 05:11

叶泰航


In case you haven't figured it out from my comments:

IP math must be done in binary. IP addresses and masks are unsigned integers (32 bits for IPv4, 128 bits for IPv6). All you need to know is an address and mask, and you can figure out everything else.

This is algorithm for what you want to accomplish, and it applies to both IPv4 and IPv6.

Based on your question, you are given the subnet (Input 1) and last address (Input 2).

  1. Subtract the unsigned integer of Input 1 from the unsigned integer of Input 2. The result is the inverse subnet mask. The inverse subnet mask must be 0, or the inverse subnet mask plus 1 must be a power of 2, else you have an error in one of the inputs (STOP, INPUT ERROR).
  2. The NOT of the inverse mask (result of Step 1) is the subnet mask.
  3. If Input 1 AND the subnet mask does not equal Input 1, you have an error in one of the inputs (STOP, INPUT ERROR).
  4. The mask length (CIDR number) is the number of 1 bits in the subnet mask. There are several ways to calculate the number of 1 bits in a binary number, but if the subnet mask is the maximum integer (or the inverse mask is 0), then the mask length is 32 (IPv4) or 128 (IPv6). You can loop, counting the number of loops and shifting the subnet mask to the left until it equals 0, loop counting the number of loops and shifting the inverse mask to the right until it equals 0 then adding 1 to the total and subtracting the total from 32 (IPv4) or 128 (IPv6), or subtract the exponent of the power of 2 of the total inverse mask plus 1 from 32 (IPv4) or 128 (IPv6).
  5. At this point, you have the verified Input 1 (subnet), Input 2 (last address), and calculated the mask length (CIDR number).
  6. The final result will be <Input 1>/<Mask Length>.

Your example:

Step 1 (5.10.127.255 - 5.10.64.0 = 0.0.64.127):

101000010100111111111111111 - 01000010100100000000000000 = 11111111111111

Step 2 (NOT 0.0.64.255 = 255.255.192.0 is a power of two):

NOT 00000000000000000011111111111111 = 11111111111111111100000000000000

Step 3 (5.10.64.0 AND 255.255.192.0 = 5.10.64.0):

01000010100100000000000000 AND 11111111111111111100000000000000 = 01000010100100000000000000

Step 4 (0.0.64.255 + 1 = 0.0.65.0 = 2^14, exponent of 2^14 = 14, 32 - 14 = 18):

00000000000000000011111111111111 + 1 = 00000000000000000100000000000000 = 2^14, exponent of 2^14 = 14, 32 - 14 = 18

Step 5 (Input 1 = 5.10.64.0, Input 2 = 5.10.127.255, Mask Length = 18)

Step 6 (Final Result = 5.10.64.0/18)

like image 3
Ron Maupin Avatar answered Nov 20 '22 05:11

Ron Maupin