mirror of https://github.com/apache/cloudstack.git
VPC: fix VPC cidr check
This commit is contained in:
parent
855618e4f4
commit
3b2b8fc143
|
|
@ -2503,20 +2503,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
|
|||
return;
|
||||
}
|
||||
|
||||
String currCidrAddress = getCidrAddress(cidr);
|
||||
int currCidrSize = getCidrSize(cidr);
|
||||
|
||||
for (long networkId : networkToCidr.keySet()) {
|
||||
String ntwkCidr = networkToCidr.get(networkId);
|
||||
String ntwkCidrAddress = getCidrAddress(ntwkCidr);
|
||||
int ntwkCidrSize = getCidrSize(ntwkCidr);
|
||||
|
||||
long cidrSizeToUse = currCidrSize < ntwkCidrSize ? currCidrSize : ntwkCidrSize;
|
||||
|
||||
String ntwkCidrSubnet = NetUtils.getCidrSubNet(ntwkCidrAddress, cidrSizeToUse);
|
||||
String cidrSubnet = NetUtils.getCidrSubNet(currCidrAddress, cidrSizeToUse);
|
||||
|
||||
if (cidrSubnet.equals(ntwkCidrSubnet)) {
|
||||
if (NetUtils.isNetworksOverlap(ntwkCidr, cidr)) {
|
||||
InvalidParameterValueException ex = new InvalidParameterValueException("Warning: The specified existing network has conflict CIDR subnets with new network!");
|
||||
ex.addProxyObject("networks", networkId, "networkId");
|
||||
throw ex;
|
||||
|
|
|
|||
|
|
@ -795,8 +795,7 @@ public class VpcManagerImpl implements VpcManager, Manager{
|
|||
for (Network ntwk : ntwks) {
|
||||
assert (cidr != null) : "Why the network cidr is null when it belongs to vpc?";
|
||||
|
||||
if (NetUtils.isNetworkAWithinNetworkB(ntwk.getCidr(), vpc.getCidr())
|
||||
|| NetUtils.isNetworkAWithinNetworkB(vpc.getCidr(), ntwk.getCidr())) {
|
||||
if (NetUtils.isNetworksOverlap(ntwk.getCidr(), cidr)) {
|
||||
throw new InvalidParameterValueException("Network cidr " + cidr + " crosses other network cidr " + ntwk +
|
||||
" belonging to the same vpc " + vpc);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -780,6 +780,16 @@ public class NetUtils {
|
|||
long shift = 32 - cidrBLong[1];
|
||||
return ((cidrALong[0] >> shift) == (cidrBLong[0] >> shift));
|
||||
}
|
||||
|
||||
public static boolean isNetworksOverlap(String cidrA, String cidrB) {
|
||||
Long[] cidrALong = cidrToLong(cidrA);
|
||||
Long[] cidrBLong = cidrToLong(cidrB);
|
||||
if (cidrALong == null || cidrBLong == null) {
|
||||
return false;
|
||||
}
|
||||
long shift = 32 - (cidrALong[1] > cidrBLong[1] ? cidrBLong[1] : cidrALong[1]);
|
||||
return ((cidrALong[0] >> shift) == (cidrBLong[0] >> shift));
|
||||
}
|
||||
|
||||
public static Long[] cidrToLong(String cidr) {
|
||||
if (cidr == null || cidr.isEmpty()) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue