Skip to content

Commit cd6e6a4

Browse files
author
Jayapal
committed
CLOUDSTACK-1762 Fixed assigning network or broadcast ip to nic
1 parent 68406ba commit cd6e6a4

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
@@ -1644,6 +1644,7 @@ public String acquireGuestIpAddress(Network network, String requestedIp) {
16441644
Set<Long> availableIps = _networkModel.getAvailableIps(network, requestedIp);
16451645

16461646
if (availableIps == null || availableIps.isEmpty()) {
1647+
s_logger.debug("There are no free ips in the network " + network );
16471648
return null;
16481649
}
16491650

@@ -1656,9 +1657,11 @@ public String acquireGuestIpAddress(Network network, String requestedIp) {
16561657
if (!isSameCidr) {
16571658
s_logger.warn("Requested ip address " + requestedIp + " doesn't belong to the network " + network + " cidr");
16581659
return null;
1659-
} else {
1660-
return requestedIp;
1660+
} else if (NetUtils.IsIpEqualToNetworkOrBroadCastIp(requestedIp, cidr[0], Integer.parseInt(cidr[1]))) {
1661+
s_logger.warn("Requested ip address " + requestedIp + " is equal to the to the network/broadcast ip of the network" + network);
1662+
return null;
16611663
}
1664+
return requestedIp;
16621665
}
16631666

16641667
String result;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,4 +1427,26 @@ public static boolean isIpWithtInCidrRange(String ipAddress, String cidr) {
14271427
SubnetUtils subnetUtils = new SubnetUtils(cidr);
14281428
return subnetUtils.getInfo().isInRange(ipAddress);
14291429
}
1430+
1431+
public static Boolean IsIpEqualToNetworkOrBroadCastIp(String requestedIp, String cidr, long size) {
1432+
assert (size < 32) : "You do know this is not for ipv6 right? Keep it smaller than 32 but you have " + size;
1433+
1434+
long ip = ip2Long(cidr);
1435+
long startNetMask = ip2Long(getCidrNetmask(size));
1436+
1437+
long start = (ip & startNetMask);
1438+
long end = start;
1439+
1440+
end = end >> (32 - size);
1441+
1442+
end++;
1443+
end = (end << (32 - size)) - 1;
1444+
1445+
long reqIp = ip2Long(requestedIp);
1446+
if (reqIp == start || reqIp == end) {
1447+
return true;
1448+
}
1449+
return false;
1450+
}
1451+
14301452
}

0 commit comments

Comments
 (0)