CLOUDSTACK-1762 Fixed assigning network or broadcast ip to nic

This commit is contained in:
Jayapal 2013-12-02 10:53:51 +05:30
parent 68406ba29d
commit cd6e6a4d3c
2 changed files with 27 additions and 2 deletions

View File

@ -1644,6 +1644,7 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage
Set<Long> availableIps = _networkModel.getAvailableIps(network, requestedIp);
if (availableIps == null || availableIps.isEmpty()) {
s_logger.debug("There are no free ips in the network " + network );
return null;
}
@ -1656,9 +1657,11 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage
if (!isSameCidr) {
s_logger.warn("Requested ip address " + requestedIp + " doesn't belong to the network " + network + " cidr");
return null;
} else {
return requestedIp;
} else if (NetUtils.IsIpEqualToNetworkOrBroadCastIp(requestedIp, cidr[0], Integer.parseInt(cidr[1]))) {
s_logger.warn("Requested ip address " + requestedIp + " is equal to the to the network/broadcast ip of the network" + network);
return null;
}
return requestedIp;
}
String result;

View File

@ -1427,4 +1427,26 @@ public class NetUtils {
SubnetUtils subnetUtils = new SubnetUtils(cidr);
return subnetUtils.getInfo().isInRange(ipAddress);
}
public static Boolean IsIpEqualToNetworkOrBroadCastIp(String requestedIp, String cidr, long size) {
assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
long ip = ip2Long(cidr);
long startNetMask = ip2Long(getCidrNetmask(size));
long start = (ip & startNetMask);
long end = start;
end = end >> (32 - size);
end++;
end = (end << (32 - size)) - 1;
long reqIp = ip2Long(requestedIp);
if (reqIp == start || reqIp == end) {
return true;
}
return false;
}
}