public IPRange(InetAddress from, InetAddress to)
{
+ initializeFromRange(from, to);
+ }
+
+ private void initializeFromRange(InetAddress from, InetAddress to)
+ {
byte[] fa = from.getAddress(), ta = to.getAddress();
if (fa.length != ta.length)
{
public IPRange(String cidr) throws UnknownHostException
{
/* only verify the basic structure */
- if (!cidr.matches("^(([0-9.]+)|([0-9a-f:]+))(/\\d+)?$"))
+ if (!cidr.matches("(?i)^(([0-9.]+)|([0-9a-f:]+))(-(([0-9.]+)|([0-9a-f:]+))|(/\\d+))?$"))
+ {
+ throw new IllegalArgumentException("Invalid CIDR or range notation");
+ }
+ if (cidr.contains("-"))
{
- throw new IllegalArgumentException("Invalid CIDR notation");
+ String[] parts = cidr.split("-");
+ InetAddress from = InetAddress.getByName(parts[0]);
+ InetAddress to = InetAddress.getByName(parts[1]);
+ initializeFromRange(from, to);
}
- String[] parts = cidr.split("/");
- InetAddress addr = InetAddress.getByName(parts[0]);
- byte[] base = addr.getAddress();
- int prefix = base.length * 8;
- if (parts.length > 1)
+ else
{
- prefix = Integer.parseInt(parts[1]);
+ String[] parts = cidr.split("/");
+ InetAddress addr = InetAddress.getByName(parts[0]);
+ byte[] base = addr.getAddress();
+ int prefix = base.length * 8;
+ if (parts.length > 1)
+ {
+ prefix = Integer.parseInt(parts[1]);
+ }
+ initializeFromCIDR(base, prefix);
}
- initializeFromCIDR(base, prefix);
}
/**
private TreeSet<IPRange> mRanges = new TreeSet<>();
/**
- * Parse the given string (space separated subnets in CIDR notation) and return the resulting
- * set or {@code null} if the string was invalid. And empty set is returned if the given string
+ * Parse the given string (space separated ranges in CIDR or range notation) and return the
+ * resulting set or {@code null} if the string was invalid. An empty set is returned if the given string
* is {@code null}.
*/
public static IPRangeSet fromString(String ranges)
}
@Test
- public void testFromStringInvalidRange() throws UnknownHostException
+ public void testFromStringRange() throws UnknownHostException
+ {
+ IPRangeSet set = IPRangeSet.fromString("192.168.1.0/24 10.0.1.0-10.0.1.16");
+ assertEquals("size", 2, set.size());
+ assertSubnets(set.getSubnets(), new IPRange("10.0.1.0/28"), new IPRange("10.0.1.16/32"),
+ new IPRange("192.168.1.0/24"));
+ }
+
+ @Test
+ public void testFromStringInvalidPrefix() throws UnknownHostException
{
IPRangeSet set = IPRangeSet.fromString("192.168.1.0/65");
assertEquals("failed", null, set);
}
+
+ @Test
+ public void testFromStringInvalidRange() throws UnknownHostException
+ {
+ IPRangeSet set = IPRangeSet.fromString("192.168.1.1 - 192.168.1.10");
+ assertEquals("failed", null, set);
+ }
}
assertEquals("not reached", null, test);
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testRangeMixed() throws UnknownHostException
+ {
+ IPRange test = new IPRange("192.168.1.1-fec1::1");
+ assertEquals("not reached", null, test);
+ }
+
private void testCIDR(String cidr, IPRange exp) throws UnknownHostException
{
IPRange test = new IPRange(cidr);
testCIDR("192.168.1.10/24", new IPRange("192.168.1.0", 24));
testCIDR("192.168.1.1/32", new IPRange("192.168.1.1", 32));
testCIDR("192.168.1.1", new IPRange("192.168.1.1", 32));
+ testCIDR("192.168.1.1-192.168.1.10", new IPRange("192.168.1.1", "192.168.1.10"));
testCIDR("::/0", new IPRange("::", 0));
testCIDR("fec1::/64", new IPRange("fec1::", 64));
testCIDR("fec1::10/64", new IPRange("fec1::", 64));
testCIDR("fec1::1/128", new IPRange("fec1::1", 128));
testCIDR("fec1::1", new IPRange("fec1::1", 128));
+ testCIDR("fec1::1-fec1::5", new IPRange("fec1::1", "fec1::5"));
}
private void testToString(String f, String t, String exp) throws UnknownHostException