Search code examples
javaiprangecidr

Compare a list of Ip Ranges (with CIDR) in Java


I have a list of IP ranges specified with a CIDR. I want to compare the ranges and remove all the ranges which are already included in another one.

Example: I have:

10.152.0.0/14

10.152.2.0/24

10.153.3.0/24

In the Result only 10.152.0.0/14 should remain, because the other two are already included in that.

I tried with the org.apache.commons.net.util Package, but if im not wrong it can only compare a Range to an Adress and not compare Ranges itself.

Is there any library in Java that can help me compare those ranges?


Solution

  • Please see the following link CIDRUtils. You'll need to do the checks yourself but the method call isInRange is implemented (it's not documented). This has also been answered before here