Skip to content

Commit 8581e58

Browse files
author
Jayapal
committed
CLOUDSTACK-1762 Fixed assigning network or broadcast ip to nic
1 parent 72e0354 commit 8581e58

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎server/src/com/cloud/network/IpAddressManagerImpl.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,7 @@ public String acquireGuestIpAddress(Network network, String requestedIp) {
16951695
Set<Long> availableIps = _networkModel.getAvailableIps(network, requestedIp);
16961696

16971697
if (availableIps == null || availableIps.isEmpty()) {
1698+
s_logger.debug("There are no free ips in the network " + network );
16981699
return null;
16991700
}
17001701

@@ -1707,9 +1708,11 @@ public String acquireGuestIpAddress(Network network, String requestedIp) {
17071708
if (!isSameCidr) {
17081709
s_logger.warn("Requested ip address " + requestedIp + " doesn't belong to the network " + network + " cidr");
17091710
return null;
1710-
} else {
1711-
return requestedIp;
1711+
} else if (NetUtils.IsIpEqualToNetworkOrBroadCastIp(requestedIp, cidr[0], Integer.parseInt(cidr[1]))) {
1712+
s_logger.warn("Requested ip address " + requestedIp + " is equal to the to the network/broadcast ip of the network" + network);
1713+
return null;
17121714
}
1715+
return requestedIp;
17131716
}
17141717

17151718
String result;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,4 +1435,26 @@ public static boolean isIpWithtInCidrRange(String ipAddress, String cidr) {
14351435
SubnetUtils subnetUtils = new SubnetUtils(cidr);
14361436
return subnetUtils.getInfo().isInRange(ipAddress);
14371437
}
1438+
1439+
public static Boolean IsIpEqualToNetworkOrBroadCastIp(String requestedIp, String cidr, long size) {
1440+
assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
1441+
1442+
long ip = ip2Long(cidr);
1443+
long startNetMask = ip2Long(getCidrNetmask(size));
1444+
1445+
long start = (ip & startNetMask);
1446+
long end = start;
1447+
1448+
end = end >> (32 - size);
1449+
1450+
end++;
1451+
end = (end << (32 - size)) - 1;
1452+
1453+
long reqIp = ip2Long(requestedIp);
1454+
if (reqIp == start || reqIp == end) {
1455+
return true;
1456+
}
1457+
return false;
1458+
}
1459+
14381460
}

0 commit comments

Comments
 (0)