Question

I need to convert wildcard subnets to smallest cidr subnet list. For example:

1.2.3.4/255.0.255.0
converted to
1.0.3.0/24
1.1.3.0/24
1.2.3.0/24
1.3.3.0/24
...
1.254.3.0/24
1.255.3.0/24

Above example is easy but for the wildcard subnet like this 1.2.3.4/252.0.128.0 its more complicated.

Is there a library for that in Java.

Thanks in advance.

Was it helpful?

Solution

You can use SubnetUtils from Apache

It has a constructor SubnetUtils(String address, String mask) that takes as arguments 2 string, the destination IP and the network mask.

Then you can use the nested class SubnetUtilsInfo and it's methods to get what you want .

OTHER TIPS

There are 3 types of masks used in TCP/IP. Subnet masks, simple bit masks, and wildcard masks. Subnet masks must be contiguous. Meaning a series of 1s followed by are series of 0s. It has been this way since 1991 (RFC 1219). The use of this mask is so that devices understand what the network number, broadcast number, and valid host for the subnet in question. Simple bit masks are used in access-control and allowed to be discontiguous. Cisco ASA firewalls, iptables and other systems use these types of masks to test for certian parts of an IP address. For example 10.1.0.254 255.255.0.255 is testing that the first octet is 10, the second is 1, the fourth is 254, but value of the thrid octet can be 0-255. A wildcard mask is an inverted simple bit mask and is commonlly used on Cisco routers and switches. These are also allowed to be discontiguous. Using the same example as before, we would invert the mask to get a test of 10.1.0.254 0.0.255.0. This test is identical to the previous example in that we care about octet 1,2, and 4 matching the exact value, but octet 3 can vary. The KEY point in all of this is that you are trying to convert 2 different things. Subnet masks are not used to same as simple bit masks/wildcard masks. Converting from one to the other isn't possible. Even if the simple/wild card mask is contiguous, the mask may not be represntitive of the actual subnet mask in use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top