VPC: CS-15580 - don't allow gateway for VPC guest network to be equal to the cidr broadcast ip

This commit is contained in:
anthony 2012-07-13 18:20:30 -07:00
parent b145029718
commit 2557315a5a
2 changed files with 37 additions and 0 deletions

View File

@ -1011,6 +1011,11 @@ public class VpcManagerImpl implements VpcManager, Manager{
if (NetUtils.getCidrSubNet(cidr).equalsIgnoreCase(gateway)) {
throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value");
}
//7) gateway should never be equal to the cidr broadcast ip
if (NetUtils.getCidrBroadcastIp(cidr).equalsIgnoreCase(gateway)) {
throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr broadcast ip");
}
} finally {
s_logger.debug("Releasing lock for " + locked);

View File

@ -728,6 +728,13 @@ public class NetUtils {
return long2Ip(result);
}
public static String getBroadcastIp(String ip, String netmask) {
long ipAddr = ip2Long(ip);
long subnet = ip2Long(netmask);
long result = ipAddr | (~subnet);
return long2Ip(result);
}
public static String getCidrSubNet(String ip, long cidrSize) {
long numericNetmask = (0xffffffff >> (32 - cidrSize)) << (32 - cidrSize);
String netmask = NetUtils.long2Ip(numericNetmask);
@ -841,6 +848,31 @@ public class NetUtils {
String netmask = NetUtils.long2Ip(numericNetmask);
return getSubNet(cidrAddress, netmask);
}
public static String getCidrBroadcastIp(String cidr) {
if (cidr == null || cidr.isEmpty()) {
return null;
}
String[] cidrPair = cidr.split("\\/");
if (cidrPair.length != 2) {
return null;
}
String cidrAddress = cidrPair[0];
String cidrSize = cidrPair[1];
if (!isValidIp(cidrAddress)) {
return null;
}
int cidrSizeNum = -1;
try {
cidrSizeNum = Integer.parseInt(cidrSize);
} catch (Exception e) {
return null;
}
long numericNetmask = (0xffffffff >> (32 - cidrSizeNum)) << (32 - cidrSizeNum);
String netmask = NetUtils.long2Ip(numericNetmask);
return getBroadcastIp(cidrAddress, netmask);
}
public static String getCidrNetmask(long cidrSize) {
long numericNetmask = (0xffffffff >> (32 - cidrSize)) << (32 - cidrSize);