private boolean validateIpAddresses() { for (String ipAddress : _ipAddresses) { if (ipAddress.trim().equalsIgnoreCase("localhost")) { continue; } if (!NetUtils.isValidIp4(ipAddress)) { return false; } } return true; }
public static String ipAndNetMaskToCidr(final String ip, final String netmask) { if (!isValidIp4(ip)) { return null; } if (!isValidIp4Netmask(netmask)) { return null; } final long ipAddr = ip2Long(ip); final long subnet = ip2Long(netmask); final long result = ipAddr & subnet; int bits = subnet == 0 ? 0 : 1; long subnet2 = subnet; while ((subnet2 = subnet2 >> 1 & subnet) != 0) { bits++; } return long2Ip(result) + "/" + Integer.toString(bits); }
public static boolean isIpWithInCidrRange(final String ipAddress, final String cidr) { if (!isValidIp4(ipAddress)) { return false; } if (!isValidIp4Cidr(cidr)) { return false; } // check if the gatewayip is the part of the ip range being added. // RFC 3021 - 31-Bit Prefixes on IPv4 Point-to-Point Links // GW Netmask Stat IP End IP // 192.168.24.0 - 255.255.255.254 - 192.168.24.0 - 192.168.24.1 // https://tools.ietf.org/html/rfc3021 // Added by Wilder Rodrigues final SubnetUtils subnetUtils = new SubnetUtils(cidr); subnetUtils.setInclusiveHostCount(true); final boolean isInRange = subnetUtils.getInfo().isInRange(ipAddress); return isInRange; }
/** * validatePoolAndCluster: * A cluster is impossible with a pool. * A pool is impossible without a vip. */ private void validatePoolAndCluster() { if (agentInOvm3Cluster) { LOGGER.debug("Clustering requires a pool, setting pool to true"); agentInOvm3Pool = true; } if (!NetUtils.isValidIp4(ovm3PoolVip)) { LOGGER.debug("No VIP, Setting ovm3pool and ovm3cluster to false"); agentInOvm3Pool = false; agentInOvm3Cluster = false; ovm3PoolVip = ""; } }
@Override public Pod createPod(final long zoneId, final String name, final String startIp, final String endIp, final String gateway, final String netmask, String allocationState) { // Check if the gateway is a valid IP address if (!NetUtils.isValidIp4(gateway)) { throw new InvalidParameterValueException("The gateway is invalid"); } if (!NetUtils.isValidIp4Netmask(netmask)) { throw new InvalidParameterValueException("The netmask is invalid"); } final String cidr = NetUtils.ipAndNetMaskToCidr(gateway, netmask); final Long userId = CallContext.current().getCallingUserId(); if (allocationState == null) { allocationState = Grouping.AllocationState.Enabled.toString(); } return createPod(userId.longValue(), name, zoneId, gateway, cidr, startIp, endIp, allocationState, false); }
public static boolean isValidIp4Cidr(final String cidr) { if (cidr == null || cidr.isEmpty()) { return false; } final String[] cidrPair = cidr.split("\\/"); if (cidrPair.length != 2) { return false; } final String cidrAddress = cidrPair[0]; final String cidrSize = cidrPair[1]; if (!isValidIp4(cidrAddress)) { return false; } int cidrSizeNum = -1; try { cidrSizeNum = Integer.parseInt(cidrSize); } catch (final Exception e) { return false; } if (cidrSizeNum < 0 || cidrSizeNum > MAX_CIDR) { return false; } return true; }
/** * Returns true if the given IP address is IPv4 or false if it is an IPv6. If it is an invalid IP address it throws an exception. */ public static boolean isIpv4(String ipAddr) { boolean isIpv4 = true; if (ipAddr != null) { if (!NetUtils.isValidIp4(ipAddr)) { isIpv4 = false; } if (!NetUtils.isValidIp6(ipAddr) && !isIpv4) { throw new IllegalArgumentException("Invalid ip address " + ipAddr); } } return isIpv4; }
private static String getCorrectIPAddress(String ip) { if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { return null; } if(NetUtils.isValidIp4(ip) || NetUtils.isValidIp6(ip)) { return ip; } //it could be possible to have multiple IPs in HTTP header, this happens if there are multiple proxy in between //the client and the servlet, so parse the client IP String[] ips = ip.split(","); for(String i : ips) { if(NetUtils.isValidIp4(i.trim()) || NetUtils.isValidIp6(i.trim())) { return i.trim(); } } return null; } }
public static boolean isValidIp4Netmask(final String netmask) { if (!isValidIp4(netmask)) { return false; } final long ip = ip2Long(netmask); int count = 0; boolean finished = false; for (int i = 31; i >= 0; i--) { if ((ip >> i & 0x1) == 0) { finished = true; } else { if (finished) { return false; } count += 1; } } if (count == 0) { return false; } return true; }
private boolean validateIpAddresses() { for (String ipAddress : _syslogHostsList) { String[] hostTokens = (ipAddress.trim()).split(":"); String ip = hostTokens[0]; if (hostTokens.length >= 1 && hostTokens.length <= 2) { if (hostTokens.length == 2 && !NetUtils.isValidPort(hostTokens[1])) { return false; } if (ip.equalsIgnoreCase("localhost")) { continue; } if (!NetUtils.isValidIp4(ip)) { return false; } } else { return false; } } return true; }
private void checkOverlapPrivateIpRange(final Long zoneId, final String startIp, final String endIp) { final List<HostPodVO> podsInZone = _podDao.listByDataCenterId(zoneId); for (final HostPodVO hostPod : podsInZone) { final String[] existingPodIpRanges = hostPod.getDescription().split(","); for(String podIpRange: existingPodIpRanges) { final String[] existingPodIpRange = podIpRange.split("-"); if (existingPodIpRange.length > 1) { if (!NetUtils.isValidIp4(existingPodIpRange[0]) || !NetUtils.isValidIp4(existingPodIpRange[1])) { continue; } if (NetUtils.ipRangesOverlap(startIp, endIp, existingPodIpRange[0], existingPodIpRange[1])) { throw new InvalidParameterValueException("The Start IP and EndIP address range overlap with private IP :" + existingPodIpRange[0] + ":" + existingPodIpRange[1]); } } } } }
if (vmIp == null || !NetUtils.isValidIp4(vmIp)) { throw new InvalidParameterValueException("Invalid ip address "+ vmIp +" passed in vmidipmap for " + "vmid " + vmId);
private void checkOverlapPrivateIpRange(long podId, String startIp, String endIp) { HostPodVO pod = _podDao.findById(podId); if (pod == null) { throw new CloudRuntimeException("Cannot find pod " + podId); } final String[] existingPodIpRanges = pod.getDescription().split(","); for(String podIpRange: existingPodIpRanges) { final String[] existingPodIpRange = podIpRange.split("-"); if (existingPodIpRange.length > 1) { if (!NetUtils.isValidIp4(existingPodIpRange[0]) || !NetUtils.isValidIp4(existingPodIpRange[1])) { continue; } if (NetUtils.ipRangesOverlap(startIp, endIp, existingPodIpRange[0], existingPodIpRange[1])) { throw new InvalidParameterValueException("The Storage network Start IP and endIP address range overlap with private IP :" + existingPodIpRange[0] + ":" + existingPodIpRange[1]); } } } }
private void validateRemoteAccessVpnConfiguration() throws ConfigurationException { String ipRange = RemoteAccessVpnClientIpRange.value(); if (ipRange == null) { s_logger.warn("Remote Access VPN global configuration missing client ip range -- ignoring"); return; } Integer pskLength = _pskLength; if (pskLength != null && (pskLength < 8 || pskLength > 256)) { throw new ConfigurationException("Remote Access VPN: IPSec preshared key length should be between 8 and 256"); } String[] range = ipRange.split("-"); if (range.length != 2) { throw new ConfigurationException("Remote Access VPN: Invalid ip range " + ipRange); } if (!NetUtils.isValidIp4(range[0]) || !NetUtils.isValidIp4(range[1])) { throw new ConfigurationException("Remote Access VPN: Invalid ip in range specification " + ipRange); } if (!NetUtils.validIpRange(range[0], range[1])) { throw new ConfigurationException("Remote Access VPN: Invalid ip range " + ipRange); } }
public static String getCidrSubNet(final String cidr) { if (cidr == null || cidr.isEmpty()) { return null; } final String[] cidrPair = cidr.split("\\/"); if (cidrPair.length != 2) { return null; } final String cidrAddress = cidrPair[0]; final String cidrSize = cidrPair[1]; if (!isValidIp4(cidrAddress)) { return null; } long cidrSizeNum = getCidrSizeFromString(cidrSize); final long numericNetmask = netMaskFromCidr(cidrSizeNum); final String netmask = NetUtils.long2Ip(numericNetmask); return getSubNet(cidrAddress, netmask); }
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."); } }
public static Long[] cidrToLong(final String cidr) { if (cidr == null || cidr.isEmpty()) { throw new CloudRuntimeException("empty cidr can not be converted to longs"); } final String[] cidrPair = cidr.split("\\/"); if (cidrPair.length != 2) { throw new CloudRuntimeException("cidr is not formatted correctly: "+ cidr); } final String cidrAddress = cidrPair[0]; final String cidrSize = cidrPair[1]; if (!isValidIp4(cidrAddress)) { throw new CloudRuntimeException("cidr is not valid in ip space" + cidr); } long cidrSizeNum = getCidrSizeFromString(cidrSize); final long numericNetmask = netMaskFromCidr(cidrSizeNum); final long ipAddr = ip2Long(cidrAddress); final Long[] cidrlong = {ipAddr & numericNetmask, cidrSizeNum}; return cidrlong; }
@Override public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException { CallContext.current().setEventDetails("Nic Id: " + getNicId() ); String ip; if ((ip = getIpaddress()) != null) { if (!NetUtils.isValidIp4(ip)) { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip); } } UserVm vm = _userVmService.updateNicIpForVirtualMachine(this); ArrayList<VMDetails> dc = new ArrayList<VMDetails>(); dc.add(VMDetails.valueOf("nics")); EnumSet<VMDetails> details = EnumSet.copyOf(dc); if (vm != null){ UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, vm).get(0); response.setResponseName(getCommandName()); this.setResponseObject(response); } else { throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update ip address on vm NIC. Refer to server logs for details."); } }
@Override public void create() { // cidr list parameter is deprecated if (cidrlist != null) { throw new InvalidParameterValueException( "Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command"); } Ip privateIp = getVmSecondaryIp(); if (privateIp != null) { if (!NetUtils.isValidIp4(privateIp.toString())) { throw new InvalidParameterValueException("Invalid vm ip address"); } } try { PortForwardingRule result = _rulesService.createPortForwardingRule(this, virtualMachineId, privateIp, getOpenFirewall(), isDisplay()); setEntityId(result.getId()); setEntityUuid(result.getUuid()); } catch (NetworkRuleConflictException ex) { s_logger.trace("Network Rule Conflict: ", ex); throw new ServerApiException(ApiErrorCode.NETWORK_RULE_CONFLICT_ERROR, ex.getMessage(), ex); } }
@Override public void doInTransactionWithoutResult(TransactionStatus status) { // if the ip address a part of placeholder, don't release it Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, null); if (placeholderNic != null && placeholderNic.getIPv4Address().equalsIgnoreCase(ip.getAddress().addr())) { s_logger.debug("Not releasing direct ip " + ip.getId() + " yet as its ip is saved in the placeholder"); } else { _ipAddrMgr.markIpAsUnavailable(ip.getId()); _ipAddressDao.unassignIpAddress(ip.getId()); } //unassign nic secondary ip address s_logger.debug("remove nic " + nic.getId() + " secondary ip "); List<String> nicSecIps = null; nicSecIps = _nicSecondaryIpDao.getSecondaryIpAddressesForNic(nic.getId()); for (String secIp : nicSecIps) { if (NetUtils.isValidIp4(secIp)) { IPAddressVO pubIp = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), secIp); _ipAddrMgr.markIpAsUnavailable(pubIp.getId()); _ipAddressDao.unassignIpAddress(pubIp.getId()); } else { NicSecondaryIpVO nicSecIp = nicSecondaryIpDao.findByIp6AddressAndNetworkId(secIp, nic.getNetworkId()); if (nicSecIp != null) { networkService.releaseSecondaryIpFromNic(nicSecIp.getId()); } } } } });