IPv6: Fix ip address in range check

This commit is contained in:
Sheng Yang 2013-02-05 13:04:38 -08:00
parent e5866cf2ef
commit 2e236a8322
3 changed files with 26 additions and 6 deletions

View File

@ -780,14 +780,14 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
if (!NetUtils.isValidIp6Cidr(ip6Cidr)) {
throw new InvalidParameterValueException("Invalid ip6cidr");
}
if (!NetUtils.isIp6InRange(startIPv6, ip6Cidr)) {
throw new InvalidParameterValueException("startIPv6 is not in ip6cidr indicated network range!");
if (!NetUtils.isIp6InNetwork(startIPv6, ip6Cidr)) {
throw new InvalidParameterValueException("startIPv6 is not in ip6cidr indicated network!");
}
if (!NetUtils.isIp6InRange(endIPv6, ip6Cidr)) {
throw new InvalidParameterValueException("endIPv6 is not in ip6cidr indicated network range!");
if (!NetUtils.isIp6InNetwork(endIPv6, ip6Cidr)) {
throw new InvalidParameterValueException("endIPv6 is not in ip6cidr indicated network!");
}
if (!NetUtils.isIp6InRange(ip6Gateway, ip6Cidr)) {
throw new InvalidParameterValueException("ip6Gateway is not in ip6cidr indicated network range!");
if (!NetUtils.isIp6InNetwork(ip6Gateway, ip6Cidr)) {
throw new InvalidParameterValueException("ip6Gateway is not in ip6cidr indicated network!");
}
int cidrSize = NetUtils.getIp6CidrSize(ip6Cidr);

View File

@ -1211,6 +1211,17 @@ public class NetUtils {
return false;
}
public static boolean isIp6InNetwork(String ip6, String ip6Cidr) {
IPv6Network network = null;
try {
network = IPv6Network.fromString(ip6Cidr);
} catch (IllegalArgumentException ex) {
return false;
}
IPv6Address ip = IPv6Address.fromString(ip6);
return network.contains(ip);
}
public static boolean isIp6RangeOverlap(String ipRange1, String ipRange2) {
String[] ips = ipRange1.split("-");
String startIp1 = ips[0];

View File

@ -102,5 +102,14 @@ public class NetUtilsTest extends TestCase {
assertTrue(NetUtils.isIp6RangeOverlap("1234:5678::f-1234:5678::ffff", "1234:5678::2-1234:5678::f"));
assertFalse(NetUtils.isIp6RangeOverlap("1234:5678::f-1234:5678::ffff", "1234:5678::2-1234:5678::e"));
assertFalse(NetUtils.isIp6RangeOverlap("1234:5678::f-1234:5678::f", "1234:5678::2-1234:5678::e"));
//Test getNextIp6InRange
assertEquals(NetUtils.getNextIp6InRange("1234:5678::8000:0000", range), "1234:5678::1");
assertEquals(NetUtils.getNextIp6InRange("1234:5678::7fff:ffff", range), "1234:5678::8000:0");
assertEquals(NetUtils.getNextIp6InRange("1234:5678::1", range), "1234:5678::2");
//Test isIp6InNetwork
assertFalse(NetUtils.isIp6InNetwork("1234:5678:abcd::1", "1234:5678::/64"));
assertTrue(NetUtils.isIp6InNetwork("1234:5678::1", "1234:5678::/64"));
assertTrue(NetUtils.isIp6InNetwork("1234:5678::ffff:ffff:ffff:ffff", "1234:5678::/64"));
assertTrue(NetUtils.isIp6InNetwork("1234:5678::", "1234:5678::/64"));
}
}