Orchard

A while back I was working on a Java version of the Repsheet library. I needed it to be functionally equivalent to the C version. Along the way I realized that once again the world lacks a reasonable library for determining if an IP address is within a CIDR block. As simple as this is people just don’t seem to need it. Like the C library I ended up writing something. It didn’t take much and supports both IPv4 and IPv6 as any networking library should at this point. I decided to separate it into a library and publish it in case others needed something like it. I named it Orchard (get it?)

As I mentioned, there really isn’t much to it. You can create a CIDR object from a string value like so:

import com.aaronbedra.orchard.CIDR;

CIDR cidr = CIDR.valueOf("10.0.1.0/24");

The valueOf method does potentially throw OrchardException so you will need to handle that however you choose. Once you have an instance of CIDR you can ask it the following questions:

System.out.println(cidr.contains("10.0.1.5"));
System.out.println(cidr.getAddress());
System.out.println(cidr.getMask());

Effectively you can ask if an IP address is inside of the CIDR block, as well as get the base address and mask of the block. Nothing complicated or tricky here, just simple network math that serves its purpose. Full instructions including the maven dependency info can be found on the project’s github page. If you have any issues or think something could be improved please file an issue or pull request.

comments powered by Disqus