mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 20:02:29 +01:00
IPv6: Fix getIp6FromRange()
This commit is contained in:
parent
74811fa8b4
commit
fa00ddf07e
@ -806,7 +806,7 @@ public class NetworkServiceImpl implements NetworkService, Manager {
|
||||
int cidrSize = NetUtils.getIp6CidrSize(ip6Cidr);
|
||||
// Ipv6 cidr limit should be at least /64
|
||||
if (cidrSize < 64) {
|
||||
throw new InvalidParameterValueException("The cidr size of IPv6 must be bigger than 64 bits!");
|
||||
throw new InvalidParameterValueException("The cidr size of IPv6 network must be no less than 64 bits!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1178,12 +1178,15 @@ public class NetUtils {
|
||||
public static String getIp6FromRange(String ip6Range) {
|
||||
String[] ips = ip6Range.split("-");
|
||||
String startIp = ips[0];
|
||||
long gap = countIp6InRange(ip6Range);
|
||||
IPv6Address start = IPv6Address.fromString(startIp);
|
||||
// Find a random number based on lower 32 bits
|
||||
int d = _rand.nextInt((int)(gap % Integer.MAX_VALUE));
|
||||
long gap = countIp6InRange(ip6Range);
|
||||
if (gap > Integer.MAX_VALUE) {
|
||||
gap = Integer.MAX_VALUE;
|
||||
}
|
||||
int next = _rand.nextInt((int)(gap));
|
||||
// And a number based on the difference of lower 32 bits
|
||||
IPv6Address ip = start.add(d);
|
||||
IPv6Address ip = start.add(next);
|
||||
return ip.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -16,16 +16,20 @@
|
||||
// under the License.
|
||||
package com.cloud.utils.net;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.googlecode.ipv6.IPv6Address;
|
||||
|
||||
public class NetUtilsTest extends TestCase {
|
||||
|
||||
private static final Logger s_logger = Logger.getLogger(NetUtilsTest.class);
|
||||
|
||||
@Test
|
||||
public void testGetRandomIpFromCidr() {
|
||||
String cidr = "192.168.124.1";
|
||||
@ -82,5 +86,15 @@ public class NetUtilsTest extends TestCase {
|
||||
assertEquals(NetUtils.countIp6InRange("1234:5678::1-1234:5678::2"), 2);
|
||||
assertEquals(NetUtils.countIp6InRange("1234:5678::2-1234:5678::0"), 0);
|
||||
assertEquals(NetUtils.getIp6FromRange("1234:5678::1-1234:5678::1"), "1234:5678::1");
|
||||
String ipString = null;
|
||||
IPv6Address ipStart = IPv6Address.fromString("1234:5678::1");
|
||||
IPv6Address ipEnd = IPv6Address.fromString("1234:5678::8000:0000");
|
||||
for (int i = 0; i < 10; i ++) {
|
||||
ipString = NetUtils.getIp6FromRange(ipStart.toString() + "-" + ipEnd.toString());
|
||||
s_logger.info("IP is " + ipString);
|
||||
IPv6Address ip = IPv6Address.fromString(ipString);
|
||||
assertTrue(ip.compareTo(ipStart) >= 0);
|
||||
assertTrue(ip.compareTo(ipEnd) <= 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user