Skip to content

Commit d825b3d

Browse files
SakshamsJayapal
authored andcommitted
CLOUDSTACK-4622:If a VM from guest network is added to network tier of VPC then IP reservation allows the CIDR to be a superset of Network CIDR for that VPC tier
Signed-off-by: Jayapal <jayapal@apache.org>
1 parent 749c77a commit d825b3d

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,13 +845,32 @@ else if (cidrALong[1] == cidrBLong[1]) {
845845
}
846846

847847
public static boolean isNetworkAWithinNetworkB(String cidrA, String cidrB) {
848+
// This utility returns true if IP range of cidrA is same or lies completely in cidrB
849+
// Returns true if networkA is same as networkB or networkA is a subset of networkB
848850
Long[] cidrALong = cidrToLong(cidrA);
849851
Long[] cidrBLong = cidrToLong(cidrB);
850852
if (cidrALong == null || cidrBLong == null) {
851853
return false;
852854
}
853-
long shift = 32 - cidrBLong[1];
854-
return ((cidrALong[0] >> shift) == (cidrBLong[0] >> shift));
855+
if (isSameIpRange(cidrA, cidrB)) {
856+
return true;
857+
}
858+
String[] cidrPairFirst = cidrA.split("\\/");
859+
String[] cidrPairSecond = cidrB.split("\\/");
860+
861+
Long networkSizeFirst = Long.valueOf(cidrPairFirst[1]);
862+
Long networkSizeSecond = Long.valueOf(cidrPairSecond[1]);
863+
String ipRangeFirst [] = NetUtils.getIpRangeFromCidr(cidrPairFirst[0], networkSizeFirst);
864+
String ipRangeSecond [] = NetUtils.getIpRangeFromCidr(cidrPairFirst[0], networkSizeSecond);
865+
866+
long startIpFirst = NetUtils.ip2Long(ipRangeFirst[0]);
867+
long endIpFirst = NetUtils.ip2Long(ipRangeFirst[1]);
868+
long startIpSecond = NetUtils.ip2Long(ipRangeSecond[0]);
869+
long endIpSecond = NetUtils.ip2Long(ipRangeSecond[1]);
870+
871+
if((startIpFirst >= startIpSecond) && (endIpFirst <= endIpSecond))
872+
return true;
873+
return false;
855874
}
856875

857876
public static Long[] cidrToLong(String cidr) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,19 @@ public void testMacGenerateion() {
173173
public void testGetLocalIPString() {
174174
assertNotNull(NetUtils.getLocalIPString());
175175
}
176+
177+
public void testSubnet() {
178+
//Test to check if a cidr is a part of another cidr
179+
//Test 2 same cidrs
180+
assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/25", "10.1.1.0/25"));
181+
//Tests when cidrA is smaller than cidrB
182+
assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/26", "10.1.1.0/25"));
183+
assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/25", "10.1.1.0/24"));
184+
assertTrue(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/23", "10.1.1.0/22"));
185+
assertTrue(NetUtils.isNetworkAWithinNetworkB("192.168.0.0/16" , "192.168.0.0/15"));
186+
//Tests when cidrA is larger than cidrB
187+
assertFalse(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/26", "10.1.1.0/27"));
188+
assertFalse(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/24", "10.1.1.0/25"));
189+
assertFalse(NetUtils.isNetworkAWithinNetworkB("10.1.1.0/22", "10.1.1.0/23"));
190+
}
176191
}

0 commit comments

Comments
 (0)