Skip to content

Commit fa00ddf

Browse files
author
Sheng Yang
committed
IPv6: Fix getIp6FromRange()
1 parent 74811fa commit fa00ddf

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

server/src/com/cloud/network/NetworkServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public Network createGuestNetwork(CreateNetworkCmd cmd) throws InsufficientCapac
806806
int cidrSize = NetUtils.getIp6CidrSize(ip6Cidr);
807807
// Ipv6 cidr limit should be at least /64
808808
if (cidrSize < 64) {
809-
throw new InvalidParameterValueException("The cidr size of IPv6 must be bigger than 64 bits!");
809+
throw new InvalidParameterValueException("The cidr size of IPv6 network must be no less than 64 bits!");
810810
}
811811
}
812812

utils/src/com/cloud/utils/net/NetUtils.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,12 +1178,15 @@ public static int getIp6CidrSize(String ip6Cidr) {
11781178
public static String getIp6FromRange(String ip6Range) {
11791179
String[] ips = ip6Range.split("-");
11801180
String startIp = ips[0];
1181-
long gap = countIp6InRange(ip6Range);
11821181
IPv6Address start = IPv6Address.fromString(startIp);
11831182
// Find a random number based on lower 32 bits
1184-
int d = _rand.nextInt((int)(gap % Integer.MAX_VALUE));
1183+
long gap = countIp6InRange(ip6Range);
1184+
if (gap > Integer.MAX_VALUE) {
1185+
gap = Integer.MAX_VALUE;
1186+
}
1187+
int next = _rand.nextInt((int)(gap));
11851188
// And a number based on the difference of lower 32 bits
1186-
IPv6Address ip = start.add(d);
1189+
IPv6Address ip = start.add(next);
11871190
return ip.toString();
11881191
}
11891192

utils/test/com/cloud/utils/net/NetUtilsTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@
1616
// under the License.
1717
package com.cloud.utils.net;
1818

19-
import java.util.Set;
2019
import java.util.SortedSet;
2120
import java.util.TreeSet;
2221

2322
import junit.framework.TestCase;
2423

24+
import org.apache.log4j.Logger;
2525
import org.junit.Test;
2626

27+
import com.googlecode.ipv6.IPv6Address;
28+
2729
public class NetUtilsTest extends TestCase {
2830

31+
private static final Logger s_logger = Logger.getLogger(NetUtilsTest.class);
32+
2933
@Test
3034
public void testGetRandomIpFromCidr() {
3135
String cidr = "192.168.124.1";
@@ -82,5 +86,15 @@ public void testIpv6() {
8286
assertEquals(NetUtils.countIp6InRange("1234:5678::1-1234:5678::2"), 2);
8387
assertEquals(NetUtils.countIp6InRange("1234:5678::2-1234:5678::0"), 0);
8488
assertEquals(NetUtils.getIp6FromRange("1234:5678::1-1234:5678::1"), "1234:5678::1");
89+
String ipString = null;
90+
IPv6Address ipStart = IPv6Address.fromString("1234:5678::1");
91+
IPv6Address ipEnd = IPv6Address.fromString("1234:5678::8000:0000");
92+
for (int i = 0; i < 10; i ++) {
93+
ipString = NetUtils.getIp6FromRange(ipStart.toString() + "-" + ipEnd.toString());
94+
s_logger.info("IP is " + ipString);
95+
IPv6Address ip = IPv6Address.fromString(ipString);
96+
assertTrue(ip.compareTo(ipStart) >= 0);
97+
assertTrue(ip.compareTo(ipEnd) <= 0);
98+
}
8599
}
86100
}

0 commit comments

Comments
 (0)