public static boolean sameSubnetCIDR(final String ip1, final String ip2, final long cidrSize) { if (ip1 == null || ip1.isEmpty() || ip2 == null || ip2.isEmpty()) { return true; } String subnet1 = NetUtils.getCidrSubNet(ip1, cidrSize); String subnet2 = NetUtils.getCidrSubNet(ip2, cidrSize); return (subnet1.equals(subnet2)); }
public static boolean sameSubnetCIDR(final String ip1, final String ip2, final long cidrSize) { if (ip1 == null || ip1.isEmpty() || ip2 == null || ip2.isEmpty()) { return true; } final String subnet1 = NetUtils.getCidrSubNet(ip1, cidrSize); final String subnet2 = NetUtils.getCidrSubNet(ip2, cidrSize); return subnet1.equals(subnet2); }
private void checkIpRange(final String startIp, final String endIp, final String cidrAddress, final long cidrSize) { //Checking not null for start IP as well. Previously we assumed to be not null always. //But the check is required for the change in updatePod API. if (!Strings.isNullOrEmpty(startIp) && !NetUtils.isValidIp4(startIp)) { throw new InvalidParameterValueException("The start address of the IP range is not a valid IP address."); } if (!Strings.isNullOrEmpty(endIp) && !NetUtils.isValidIp4(endIp)) { throw new InvalidParameterValueException("The end address of the IP range is not a valid IP address."); } //Not null check is required for the change in updatePod API. if (!Strings.isNullOrEmpty(startIp) && !NetUtils.getCidrSubNet(startIp, cidrSize).equalsIgnoreCase(NetUtils.getCidrSubNet(cidrAddress, cidrSize))) { throw new InvalidParameterValueException("The start address of the IP range is not in the CIDR subnet."); } if (!Strings.isNullOrEmpty(endIp) && !NetUtils.getCidrSubNet(endIp, cidrSize).equalsIgnoreCase(NetUtils.getCidrSubNet(cidrAddress, cidrSize))) { throw new InvalidParameterValueException("The end address of the IP range is not in the CIDR subnet."); } if (!Strings.isNullOrEmpty(endIp) && NetUtils.ip2Long(startIp) > NetUtils.ip2Long(endIp)) { throw new InvalidParameterValueException("The start IP address must have a lower value than the end IP address."); } }
cidrSizeToUse = guestCidrSize; String cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSizeToUse); String guestSubnet = NetUtils.getCidrSubNet(guestIpNetwork, cidrSizeToUse); cidrSizeToUse = otherCidrSize; cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSizeToUse); String otherCidrSubnet = NetUtils.getCidrSubNet(otherCidrAddress, cidrSizeToUse);
/** * Validates requested source IP address of Internal Lb rule against sourceNetworkId * @param sourceIpNtwk * @param requestedSourceIp */ protected void validateRequestedSourceIpForInternalLbRule(Network sourceIpNtwk, Ip requestedSourceIp) { //Check if the IP is within the network cidr Pair<String, Integer> cidr = NetUtils.getCidr(sourceIpNtwk.getCidr()); if (!NetUtils.getCidrSubNet(requestedSourceIp.addr(), cidr.second()).equalsIgnoreCase(NetUtils.getCidrSubNet(cidr.first(), cidr.second()))) { throw new InvalidParameterValueException("The requested IP is not in the network's CIDR subnet."); } }
private boolean checkCIDR(Host.Type type, HostPodVO pod, String serverPrivateIP, String serverPrivateNetmask) { if (serverPrivateIP == null) { return true; } // Get the CIDR address and CIDR size String cidrAddress = pod.getCidrAddress(); long cidrSize = pod.getCidrSize(); // If the server's private IP address is not in the same subnet as the // pod's CIDR, return false String cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSize); String serverSubnet = NetUtils.getSubNet(serverPrivateIP, serverPrivateNetmask); if (!cidrSubnet.equals(serverSubnet)) { return false; } return true; }
public String[] generateSRouteRules() { Set<String> toAdd = new HashSet<String>(); for (StaticRouteProfile route : staticRoutes) { /* example : ip:gateway:cidr, */ String cidr = route.getCidr(); String subnet = NetUtils.getCidrSubNet(cidr); String cidrSize = cidr.split("\\/")[1]; String entry; if (route.getState() == StaticRoute.State.Active || route.getState() == StaticRoute.State.Add) { entry = route.getIp4Address() + ":" + route.getGateway() + ":" + subnet + "/" + cidrSize; } else { entry = "Revoke:" + route.getGateway() + ":" + subnet + "/" + cidrSize; } toAdd.add(entry); } return toAdd.toArray(new String[toAdd.size()]); }
private boolean checkCIDR(final HostPodVO pod, final String serverPrivateIP, final String serverPrivateNetmask) { if (serverPrivateIP == null) { return true; } // Get the CIDR address and CIDR size final String cidrAddress = pod.getCidrAddress(); final long cidrSize = pod.getCidrSize(); // If the server's private IP address is not in the same subnet as the // pod's CIDR, return false final String cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSize); final String serverSubnet = NetUtils.getSubNet(serverPrivateIP, serverPrivateNetmask); if (!cidrSubnet.equals(serverSubnet)) { return false; } // If the server's private netmask is less inclusive than the pod's CIDR // netmask, return false final String cidrNetmask = NetUtils.getCidrSubNet("255.255.255.255", cidrSize); final long cidrNetmaskNumeric = NetUtils.ip2Long(cidrNetmask); final long serverNetmaskNumeric = NetUtils.ip2Long(serverPrivateNetmask); if (serverNetmaskNumeric > cidrNetmaskNumeric) { return false; } return true; }
private void updatePodNetmaskIfNeeded(HostPodVO pod, String agentNetmask) { // If the server's private netmask is less inclusive than the pod's CIDR // netmask, update cidrSize of the default POD //(reason: we are maintaining pods only for internal accounting.) long cidrSize = pod.getCidrSize(); String cidrNetmask = NetUtils.getCidrSubNet("255.255.255.255", cidrSize); long cidrNetmaskNumeric = NetUtils.ip2Long(cidrNetmask); long serverNetmaskNumeric = NetUtils.ip2Long(agentNetmask);// if (serverNetmaskNumeric > cidrNetmaskNumeric) { //update pod's cidrsize int newCidrSize = new Long(NetUtils.getCidrSize(agentNetmask)).intValue(); pod.setCidrSize(newCidrSize); _podDao.update(pod.getId(), pod); } }
@Override public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) { final SetStaticRouteCommand command = (SetStaticRouteCommand) cmd; final LinkedList<StaticRoute> routes = new LinkedList<>(); for (final StaticRouteProfile profile : command.getStaticRoutes()) { final String cidr = profile.getCidr(); final String subnet = NetUtils.getCidrSubNet(cidr); final String cidrSize = cidr.split("\\/")[1]; final boolean keep = profile.getState() == com.cloud.network.vpc.StaticRoute.State.Active || profile.getState() == com.cloud.network.vpc.StaticRoute.State.Add; routes.add(new StaticRoute(!keep, profile.getIp4Address(), profile.getGateway(), subnet + "/" + cidrSize)); } return generateConfigItems(new StaticRoutes(routes)); }
@Override public void checkCIDR(final HostPodVO pod, final DataCenterVO dc, final String serverPrivateIP, final String serverPrivateNetmask) throws IllegalArgumentException { if (serverPrivateIP == null) { return; } // Get the CIDR address and CIDR size final String cidrAddress = pod.getCidrAddress(); final long cidrSize = pod.getCidrSize(); // If the server's private IP address is not in the same subnet as the // pod's CIDR, return false final String cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSize); final String serverSubnet = NetUtils.getSubNet(serverPrivateIP, serverPrivateNetmask); if (!cidrSubnet.equals(serverSubnet)) { s_logger.warn("The private ip address of the server (" + serverPrivateIP + ") is not compatible with the CIDR of pod: " + pod.getName() + " and zone: " + dc.getName()); throw new IllegalArgumentException("The private ip address of the server (" + serverPrivateIP + ") is not compatible with the CIDR of pod: " + pod.getName() + " and zone: " + dc.getName()); } // If the server's private netmask is less inclusive than the pod's CIDR // netmask, return false final String cidrNetmask = NetUtils.getCidrSubNet("255.255.255.255", cidrSize); final long cidrNetmaskNumeric = NetUtils.ip2Long(cidrNetmask); final long serverNetmaskNumeric = NetUtils.ip2Long(serverPrivateNetmask); if (serverNetmaskNumeric > cidrNetmaskNumeric) { throw new IllegalArgumentException("The private ip address of the server (" + serverPrivateIP + ") is not compatible with the CIDR of pod: " + pod.getName() + " and zone: " + dc.getName()); } }
private List<Pair<String, String>> getIpAddressRanges(Network network) { List<Pair<String, String>> ipAddressRanges = Lists.newArrayList(); String subnet = NetUtils.getCidrSubNet(network.getCidr()); String netmask = NetUtils.getCidrNetmask(network.getCidr()); long cidrSize = NetUtils.getCidrSize(netmask); Set<Long> allIPsInCidr = NetUtils.getAllIpsFromCidr(subnet, cidrSize, new HashSet<Long>()); if (allIPsInCidr == null || !(allIPsInCidr instanceof TreeSet)) { throw new IllegalStateException("The IPs in CIDR for subnet " + subnet + " where null or returned in a non-ordered set."); } Iterator<Long> ipIterator = allIPsInCidr.iterator(); long ip = ipIterator.next(); long gatewayIp = NetUtils.ip2Long(network.getGateway()); String lastIp = NetUtils.getIpRangeEndIpFromCidr(subnet, cidrSize); if (gatewayIp == ip) { ip = ipIterator.next(); ipAddressRanges.add(Pair.of(NetUtils.long2Ip(ip), lastIp)); } else if (!network.getGateway().equals(lastIp)) { ipAddressRanges.add(Pair.of(NetUtils.long2Ip(ip), NetUtils.long2Ip(gatewayIp - 1))); ipAddressRanges.add(Pair.of(NetUtils.long2Ip(gatewayIp + 1), lastIp)); } else { ipAddressRanges.add(Pair.of(NetUtils.long2Ip(ip), NetUtils.long2Ip(gatewayIp - 1))); } return ipAddressRanges; }
@Test public void testGetCidrSubNet() { final String cidr = "10.10.0.0/16"; String subnet = NetUtils.getCidrSubNet("10.10.10.10/16"); assertTrue(cidr + " does not contain " + subnet,NetUtils.isIpWithInCidrRange(subnet, cidr)); }
@Test public void testGetCidrSubNetWithWidth() { final String cidr = "10.10.0.0/16"; String subnet = NetUtils.getCidrSubNet("10.10.10.10", 16); assertTrue(cidr + " does not contain " + subnet,NetUtils.isIpWithInCidrRange(subnet, cidr)); }
if (!NetUtils.getCidrSubNet(gateway, cidrSize).equalsIgnoreCase(NetUtils.getCidrSubNet(cidrAddress, cidrSize))) { throw new InvalidParameterValueException("The gateway is not in the CIDR subnet.");
if (!NetUtils.getCidrSubNet(existingPodIpRange[0], cidrSize).equalsIgnoreCase(NetUtils.getCidrSubNet(cidrAddress, cidrSize))) { throw new InvalidParameterValueException("The start address of the some IP range is not in the CIDR subnet."); if (!NetUtils.getCidrSubNet(existingPodIpRange[1], cidrSize).equalsIgnoreCase(NetUtils.getCidrSubNet(cidrAddress, cidrSize))) { throw new InvalidParameterValueException("The end address of the some IP range is not in the CIDR subnet.");
if (guestNic.isDefaultNic()) { cidrSize = NetUtils.getCidrSize(guestNic.getIPv4Netmask()); final String cidr = NetUtils.getCidrSubNet(guestNic.getIPv4Gateway(), cidrSize); if (cidr != null) { dhcpRange = NetUtils.getIpRangeStartIpFromCidr(cidr, cidrSize);
protected String getGuestDhcpRange(final NicProfile guestNic, final Network guestNetwork, final DataCenter dc) { String dhcpRange = null; // setup dhcp range if (dc.getNetworkType() == NetworkType.Basic) { final long cidrSize = NetUtils.getCidrSize(guestNic.getIPv4Netmask()); final String cidr = NetUtils.getCidrSubNet(guestNic.getIPv4Gateway(), cidrSize); if (cidr != null) { dhcpRange = NetUtils.getIpRangeStartIpFromCidr(cidr, cidrSize); } } else if (dc.getNetworkType() == NetworkType.Advanced) { final String cidr = guestNetwork.getCidr(); if (cidr != null) { dhcpRange = NetUtils.getDhcpRange(cidr); } } return dhcpRange; } }
String cidr = cmd.getAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR); long cidrSize = NetUtils.cidrToLong(cidr)[1]; String guestVlanSubnet = NetUtils.getCidrSubNet(guestVlanGateway, cidrSize);
if (NetUtils.getCidrSubNet(cidr).equalsIgnoreCase(gateway)) { throw new InvalidParameterValueException("Invalid gateway specified. It should never be equal to the cidr subnet value");