You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ki...@apache.org on 2013/02/01 07:11:34 UTC
[8/48] git commit: refs/heads/regions - IPv6: Fix getIp6FromRange()
IPv6: Fix getIp6FromRange()
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/fa00ddf0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/fa00ddf0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/fa00ddf0
Branch: refs/heads/regions
Commit: fa00ddf07ed7ddffb1a14c2623616a959dc2ad4e
Parents: 74811fa
Author: Sheng Yang <sh...@citrix.com>
Authored: Sun Jan 27 13:53:51 2013 -0800
Committer: Sheng Yang <sh...@citrix.com>
Committed: Sun Jan 27 19:07:44 2013 -0800
----------------------------------------------------------------------
.../src/com/cloud/network/NetworkServiceImpl.java | 2 +-
utils/src/com/cloud/utils/net/NetUtils.java | 9 +++++--
utils/test/com/cloud/utils/net/NetUtilsTest.java | 16 ++++++++++++++-
3 files changed, 22 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fa00ddf0/server/src/com/cloud/network/NetworkServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java b/server/src/com/cloud/network/NetworkServiceImpl.java
index 20319ff..df44ce0 100755
--- a/server/src/com/cloud/network/NetworkServiceImpl.java
+++ b/server/src/com/cloud/network/NetworkServiceImpl.java
@@ -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!");
}
}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fa00ddf0/utils/src/com/cloud/utils/net/NetUtils.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/net/NetUtils.java b/utils/src/com/cloud/utils/net/NetUtils.java
index 8bfd376..b81aff6 100755
--- a/utils/src/com/cloud/utils/net/NetUtils.java
+++ b/utils/src/com/cloud/utils/net/NetUtils.java
@@ -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();
}
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/fa00ddf0/utils/test/com/cloud/utils/net/NetUtilsTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/net/NetUtilsTest.java b/utils/test/com/cloud/utils/net/NetUtilsTest.java
index 6290cd6..9beb6ca 100644
--- a/utils/test/com/cloud/utils/net/NetUtilsTest.java
+++ b/utils/test/com/cloud/utils/net/NetUtilsTest.java
@@ -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);
+ }
}
}