Search code examples
javaip-addresssubnetapache-commons-net

Commons-net 3.3 SubnetUtils does not allow 0.0.0.0/0


I want to make an all-inclusive ip range in commons-net, but when I try

SubnetUtils subnetUtils = new SubnetUtils("0.0.0.0", "0.0.0.0");

or the same:

SubnetUtils subnetUtils = new SubnetUtils("0.0.0.0/0");

I get an exception:

java.lang.IllegalArgumentException: Value [0] not in range (0,32]
at org.apache.commons.net.util.SubnetUtils.rangeCheck(SubnetUtils.java:304)
at org.apache.commons.net.util.SubnetUtils.calculate(SubnetUtils.java:229)
at org.apache.commons.net.util.SubnetUtils.<init>(SubnetUtils.java:63)

I saw there is already a ticket for this: https://issues.apache.org/jira/browse/NET-511 . They say, that the issue is resolved in the next (3.4) release.

By the time commons-net 3.4 is released, is there any workaround (like a List of SubnetUtils objects) that together allows each IPv4 addresses?


Solution

  • Yes there is a workaround, with list of SubnetUtils objects, as you mentioned:

    CIDR Signature: [0.255.255.255/1] Netmask: [128.0.0.0]
    First Address:  [0.0.0.0]
    Last Address:   [127.255.255.255]
    
    CIDR Signature: [255.255.255.255/1] Netmask: [128.0.0.0]
    First Address:  [128.0.0.0]
    Last Address:   [255.255.255.255]
    

    Or with Java code:

    SubnetUtils subnetUtils1 = new SubnetUtils("0.255.255.255/1");
    SubnetUtils subnetUtils2 = new SubnetUtils("255.255.255.255/1");