org.apache.commons.net.util.SubnetUtils.SubnetInfo Java Examples
The following examples show how to use
org.apache.commons.net.util.SubnetUtils.SubnetInfo.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SubnetValidator.java From cloudbreak with Apache License 2.0 | 6 votes |
@Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null) { return true; } if (value.isEmpty()) { return subnetType.equals(SubnetType.RFC_1918_COMPLIANT_ONLY_OR_EMPTY); } try { SubnetInfo info = new SubnetUtils(value).getInfo(); if (!info.getAddress().equals(info.getNetworkAddress())) { return false; } switch (subnetType) { case CUSTOM: return true; case RFC_1918_COMPLIANT_ONLY: default: return isRfc1918CompliantSubnet(info); } } catch (IllegalArgumentException exception) { return false; } }
Example #2
Source File: AwsNetworkService.java From cloudbreak with Apache License 2.0 | 6 votes |
private String calculateSubnet(String stackName, Vpc vpc, Iterable<String> subnetCidrs) { SubnetInfo vpcInfo = new SubnetUtils(vpc.getCidrBlock()).getInfo(); String[] cidrParts = vpcInfo.getCidrSignature().split("/"); int netmask = Integer.parseInt(cidrParts[cidrParts.length - 1]); int netmaskBits = CIDR_PREFIX - netmask; if (netmaskBits <= 0) { throw new CloudConnectorException("The selected VPC has to be in a bigger CIDR range than /24"); } int numberOfSubnets = Double.valueOf(Math.pow(2, netmaskBits)).intValue(); int targetSubnet = 0; if (stackName != null) { byte[] b = stackName.getBytes(Charset.forName("UTF-8")); for (byte ascii : b) { targetSubnet += ascii; } } targetSubnet = Long.valueOf(targetSubnet % numberOfSubnets).intValue(); String cidr = getSubnetCidrInRange(vpc, subnetCidrs, targetSubnet, numberOfSubnets); if (cidr == null) { cidr = getSubnetCidrInRange(vpc, subnetCidrs, 0, targetSubnet); } if (cidr == null) { throw new CloudConnectorException("Cannot find non-overlapping CIDR range"); } return cidr; }
Example #3
Source File: L3NetworkApiInterceptor.java From zstack with Apache License 2.0 | 6 votes |
private void validate(APIAddIpRangeByNetworkCidrMsg msg) { try { SubnetUtils utils = new SubnetUtils(msg.getNetworkCidr()); utils.setInclusiveHostCount(false); SubnetInfo subnet = utils.getInfo(); if (subnet.getAddressCount() == 0) { throw new ApiMessageInterceptionException(argerr("%s is not an allowed network cidr, because it doesn't have usable ip range", msg.getNetworkCidr())); } if (msg.getGateway() != null && !(msg.getGateway().equals(subnet.getLowAddress()) || msg.getGateway().equals(subnet.getHighAddress()))) { throw new ApiMessageInterceptionException(argerr("%s is not the first or last address of the cidr %s", msg.getGateway(), msg.getNetworkCidr())); } } catch (IllegalArgumentException e) { throw new ApiMessageInterceptionException(argerr("%s is not a valid network cidr", msg.getNetworkCidr())); } if (msg.getIpRangeType() == null) { msg.setIpRangeType(IpRangeType.Normal.toString()); } IpRangeInventory ipr = IpRangeInventory.fromMessage(msg); validate(ipr); }
Example #4
Source File: CidrBlockAuthorisation.java From data-highway with Apache License 2.0 | 5 votes |
private static List<SubnetInfo> convertToSubnetInfos(List<String> cidrBlocks) { return cidrBlocks.stream().map(x -> { SubnetUtils subnetUtils = new SubnetUtils(x); subnetUtils.setInclusiveHostCount(true); return subnetUtils.getInfo(); }).collect(toList()); }
Example #5
Source File: SubnetValidator.java From cloudbreak with Apache License 2.0 | 5 votes |
private boolean isRfc1918CompliantSubnet(SubnetInfo info) { Ip ip = new Ip(info.getAddress()); long addr = toLong(info.getAddress()); long lowerAddr = toLong(info.getLowAddress()) - 1; if (addr != lowerAddr) { return false; } return (new Ip("10.0.0.0").compareTo(ip) <= 0 && new Ip("10.255.255.255").compareTo(ip) >= 0) || (new Ip("172.16.0.0").compareTo(ip) <= 0 && new Ip("172.31.255.255").compareTo(ip) >= 0) || (new Ip("192.168.0.0").compareTo(ip) <= 0 && new Ip("192.168.255.255").compareTo(ip) >= 0); }
Example #6
Source File: AwsNetworkService.java From cloudbreak with Apache License 2.0 | 5 votes |
private String getSubnetCidrInRange(Vpc vpc, Iterable<String> subnetCidrs, int start, int end) { SubnetInfo vpcInfo = new SubnetUtils(vpc.getCidrBlock()).getInfo(); String lowProbe = incrementIp(vpcInfo.getLowAddress()); String highProbe = new SubnetUtils(toSubnetCidr(lowProbe)).getInfo().getHighAddress(); // start from the target subnet for (int i = 0; i < start - 1; i++) { lowProbe = incrementIp(lowProbe); highProbe = incrementIp(highProbe); } boolean foundProbe = false; for (int i = start; i < end; i++) { boolean overlapping = false; for (String subnetCidr : subnetCidrs) { SubnetInfo subnetInfo = new SubnetUtils(subnetCidr).getInfo(); if (isInRange(lowProbe, subnetInfo) || isInRange(highProbe, subnetInfo)) { overlapping = true; break; } } if (overlapping) { lowProbe = incrementIp(lowProbe); highProbe = incrementIp(highProbe); } else { foundProbe = true; break; } } if (foundProbe && isInRange(highProbe, vpcInfo)) { String subnet = toSubnetCidr(lowProbe); LOGGER.debug("The following subnet cidr found: {} for VPC: {}", subnet, vpc.getVpcId()); return subnet; } else { return null; } }
Example #7
Source File: ipGetCount.java From openbd-core with GNU General Public License v3.0 | 5 votes |
/** * Gets the SubnetInfo block from the input parameters * @param _session * @param argStruct * @return * @throws cfmRunTimeException */ protected SubnetInfo getInfo(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { String ipaddress = getNamedStringParam(argStruct, "ip", null); if ( ipaddress == null ) throwException(_session, "Missing ip paramter" ); if ( ipaddress.lastIndexOf("/") == -1 ){ String mask = getNamedStringParam(argStruct, "mask", null); if ( mask == null ) throwException(_session, "Since IP address is not in CIDR notation, please provide a mask" ); return new SubnetUtils(ipaddress, mask).getInfo(); }else return new SubnetUtils(ipaddress).getInfo(); }
Example #8
Source File: ipInRange.java From openbd-core with GNU General Public License v3.0 | 5 votes |
/** * Gets the SubnetInfo block from the input parameters * @param _session * @param argStruct * @return * @throws cfmRunTimeException */ protected SubnetInfo getInfo(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { String ipaddress = getNamedStringParam(argStruct, "ip", null); if ( ipaddress == null ) throwException(_session, "Missing ip paramter" ); if ( ipaddress.lastIndexOf("/") == -1 ){ String mask = getNamedStringParam(argStruct, "mask", null); if ( mask == null ) throwException(_session, "Since IP address is not in CIDR notation, please provide a mask" ); return new SubnetUtils(ipaddress, mask).getInfo(); }else return new SubnetUtils(ipaddress).getInfo(); }
Example #9
Source File: IpRangeInventory.java From zstack with Apache License 2.0 | 5 votes |
public static IpRangeInventory fromMessage(APIAddIpRangeByNetworkCidrMsg msg) { SubnetUtils utils = new SubnetUtils(msg.getNetworkCidr()); SubnetInfo subnet = utils.getInfo(); IpRangeInventory ipr = new IpRangeInventory(); ipr.setNetworkCidr(msg.getNetworkCidr()); ipr.setName(msg.getName()); ipr.setDescription(msg.getDescription()); ipr.setIpRangeType(IpRangeType.valueOf(msg.getIpRangeType())); if (ipr.getIpRangeType() == IpRangeType.Normal) { String lowAddress = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress())); String highAddress = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress())); if (msg.getGateway() == null || msg.getGateway().equals(lowAddress)) { ipr.setGateway(lowAddress); ipr.setStartIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) + 1)); ipr.setEndIp(subnet.getHighAddress()); } else if (msg.getGateway().equals(highAddress)) { ipr.setGateway(highAddress); ipr.setStartIp(lowAddress); ipr.setEndIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()) - 1)); } } else { ipr.setGateway(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()))); ipr.setStartIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) - 1)); ipr.setEndIp(NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()) + 1)); } ipr.setNetmask(subnet.getNetmask()); ipr.setPrefixLen(NetworkUtils.getPrefixLengthFromNetwork(subnet.getNetmask())); ipr.setL3NetworkUuid(msg.getL3NetworkUuid()); ipr.setUuid(msg.getResourceUuid()); ipr.setIpVersion(IPv6Constants.IPv4); return ipr; }
Example #10
Source File: NetUtils.java From big-c with Apache License 2.0 | 5 votes |
/** * Return an InetAddress for each interface that matches the * given subnet specified using CIDR notation. * * @param subnet subnet specified using CIDR notation * @param returnSubinterfaces * whether to return IPs associated with subinterfaces * @throws IllegalArgumentException if subnet is invalid */ public static List<InetAddress> getIPs(String subnet, boolean returnSubinterfaces) { List<InetAddress> addrs = new ArrayList<InetAddress>(); SubnetInfo subnetInfo = new SubnetUtils(subnet).getInfo(); Enumeration<NetworkInterface> nifs; try { nifs = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Unable to get host interfaces", e); return addrs; } while (nifs.hasMoreElements()) { NetworkInterface nif = nifs.nextElement(); // NB: adding addresses even if the nif is not up addMatchingAddrs(nif, subnetInfo, addrs); if (!returnSubinterfaces) { continue; } Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces(); while (subNifs.hasMoreElements()) { addMatchingAddrs(subNifs.nextElement(), subnetInfo, addrs); } } return addrs; }
Example #11
Source File: NetUtils.java From big-c with Apache License 2.0 | 5 votes |
/** * Add all addresses associated with the given nif in the * given subnet to the given list. */ private static void addMatchingAddrs(NetworkInterface nif, SubnetInfo subnetInfo, List<InetAddress> addrs) { Enumeration<InetAddress> ifAddrs = nif.getInetAddresses(); while (ifAddrs.hasMoreElements()) { InetAddress ifAddr = ifAddrs.nextElement(); if (subnetInfo.isInRange(ifAddr.getHostAddress())) { addrs.add(ifAddr); } } }
Example #12
Source File: NetUtils.java From hadoop with Apache License 2.0 | 5 votes |
/** * Return an InetAddress for each interface that matches the * given subnet specified using CIDR notation. * * @param subnet subnet specified using CIDR notation * @param returnSubinterfaces * whether to return IPs associated with subinterfaces * @throws IllegalArgumentException if subnet is invalid */ public static List<InetAddress> getIPs(String subnet, boolean returnSubinterfaces) { List<InetAddress> addrs = new ArrayList<InetAddress>(); SubnetInfo subnetInfo = new SubnetUtils(subnet).getInfo(); Enumeration<NetworkInterface> nifs; try { nifs = NetworkInterface.getNetworkInterfaces(); } catch (SocketException e) { LOG.error("Unable to get host interfaces", e); return addrs; } while (nifs.hasMoreElements()) { NetworkInterface nif = nifs.nextElement(); // NB: adding addresses even if the nif is not up addMatchingAddrs(nif, subnetInfo, addrs); if (!returnSubinterfaces) { continue; } Enumeration<NetworkInterface> subNifs = nif.getSubInterfaces(); while (subNifs.hasMoreElements()) { addMatchingAddrs(subNifs.nextElement(), subnetInfo, addrs); } } return addrs; }
Example #13
Source File: NetUtils.java From hadoop with Apache License 2.0 | 5 votes |
/** * Add all addresses associated with the given nif in the * given subnet to the given list. */ private static void addMatchingAddrs(NetworkInterface nif, SubnetInfo subnetInfo, List<InetAddress> addrs) { Enumeration<InetAddress> ifAddrs = nif.getInetAddresses(); while (ifAddrs.hasMoreElements()) { InetAddress ifAddr = ifAddrs.nextElement(); if (subnetInfo.isInRange(ifAddr.getHostAddress())) { addrs.add(ifAddr); } } }
Example #14
Source File: PrivateDNS.java From PacketProxy with Apache License 2.0 | 5 votes |
String getSpoofAddr(String addr) { for (SubnetInfo info : subnets) { if (info.isInRange(addr)) { return info.getAddress(); } } return defaultAddr; }
Example #15
Source File: ipBroadcastMask.java From openbd-core with GNU General Public License v3.0 | 4 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); return new cfStringData( info.getBroadcastAddress() ); }
Example #16
Source File: ipGetHighAddress.java From openbd-core with GNU General Public License v3.0 | 4 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); return new cfStringData( info.getHighAddress() ); }
Example #17
Source File: ipGetLowAddress.java From openbd-core with GNU General Public License v3.0 | 4 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); return new cfStringData( info.getLowAddress() ); }
Example #18
Source File: NfsExports.java From big-c with Apache License 2.0 | 4 votes |
private CIDRMatch(AccessPrivilege accessPrivilege, SubnetInfo subnetInfo) { super(accessPrivilege); this.subnetInfo = subnetInfo; }
Example #19
Source File: ipNetworkAddress.java From openbd-core with GNU General Public License v3.0 | 4 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); return new cfStringData( info.getNetworkAddress() ); }
Example #20
Source File: ipToCidr.java From openbd-core with GNU General Public License v3.0 | 4 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); return new cfStringData( info.getCidrSignature() ); }
Example #21
Source File: ipGetCount.java From openbd-core with GNU General Public License v3.0 | 4 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); return new cfNumberData( info.getAddressCount() ); }
Example #22
Source File: NfsExports.java From hadoop with Apache License 2.0 | 4 votes |
private CIDRMatch(AccessPrivilege accessPrivilege, SubnetInfo subnetInfo) { super(accessPrivilege); this.subnetInfo = subnetInfo; }
Example #23
Source File: AwsNetworkService.java From cloudbreak with Apache License 2.0 | 4 votes |
private boolean isInRange(String address, SubnetInfo subnetInfo) { int low = InetAddresses.coerceToInteger(InetAddresses.forString(subnetInfo.getLowAddress())); int high = InetAddresses.coerceToInteger(InetAddresses.forString(subnetInfo.getHighAddress())); int currentAddress = InetAddresses.coerceToInteger(InetAddresses.forString(address)); return low <= currentAddress && currentAddress <= high; }
Example #24
Source File: CidrBlockAuthorisation.java From data-highway with Apache License 2.0 | 4 votes |
private static LoadingCache<List<String>, List<SubnetInfo>> cache() { return CacheBuilder.newBuilder().expireAfterAccess(10, MINUTES).build( CacheLoader.from(x -> convertToSubnetInfos(x))); }
Example #25
Source File: ipInRange.java From openbd-core with GNU General Public License v3.0 | 3 votes |
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException { SubnetInfo info = getInfo( _session, argStruct ); String checkip = getNamedStringParam(argStruct, "checkip", null); if ( checkip == null ) throwException(_session, "Missing checkip paramter" ); return cfBooleanData.getcfBooleanData( info.isInRange(checkip) ); }