I'm looking for a Java library that is geared towards network math and already tested. Nothing particularly fancy, just something to hold ips and subnets, and do things like print a subnet mask or calculate whether an IP is within a given subnet.
Should I roll my own, or is there already a robust library for this?
The open-source IPAddress Java library can do ip address manipulation such as conversion to/from ipv4/ipv6 and subnet checking. Disclaimer: I am the project manager.
It handles various network math operations, such as masking, bitwise or, setting prefix lengths, switch address to prefix block, iterating through a subnet, checking containment, replacing address segments, reversing addresses, calculating subnet intersection, subtracting one subnet from another, and others.
Here is some example code for testing if an ipv6 address is in a given subnet:
output:
org.apache.lenya.ac.IPRange appears to have these features.
The Apache Lenya project is an open-source content management system. It uses the Apache License, so you may be able to reuse just the code you need. (But as always, read the license yourself; don't trust legal advice from some guy on the internet! :-)
We developed a Java IPv4 arithmetic library ourselves. See it here: http://tufar.com/ipcalculator/ It is under BSD license.