Java Code Examples for org.apache.commons.net.util.SubnetUtils#SubnetInfo
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: K8sNetworkingUtil.java From onos with Apache License 2.0 | 6 votes |
/** * Obtains valid IP addresses of the given subnet. * * @param cidr CIDR * @return set of IP addresses */ public static Set<IpAddress> getSubnetIps(String cidr) { SubnetUtils utils = new SubnetUtils(cidr); utils.setInclusiveHostCount(false); SubnetUtils.SubnetInfo info = utils.getInfo(); Set<String> allAddresses = new HashSet<>(Arrays.asList(info.getAllAddresses())); if (allAddresses.size() > 2) { allAddresses.remove(info.getLowAddress()); allAddresses.remove(info.getHighAddress()); } return allAddresses.stream() .map(IpAddress::valueOf).collect(Collectors.toSet()); }
Example 2
Source File: MachineList.java From hadoop with Apache License 2.0 | 6 votes |
/** * returns the contents of the MachineList as a Collection<String> * This can be used for testing * @return contents of the MachineList */ @VisibleForTesting public Collection<String> getCollection() { Collection<String> list = new ArrayList<String>(); if (all) { list.add("*"); } else { if (ipAddresses != null) { list.addAll(ipAddresses); } if (hostNames != null) { list.addAll(hostNames); } if (cidrAddresses != null) { for(SubnetUtils.SubnetInfo cidrAddress : cidrAddresses) { list.add(cidrAddress.getCidrSignature()); } } } return list; }
Example 3
Source File: MachineList.java From big-c with Apache License 2.0 | 6 votes |
/** * returns the contents of the MachineList as a Collection<String> * This can be used for testing * @return contents of the MachineList */ @VisibleForTesting public Collection<String> getCollection() { Collection<String> list = new ArrayList<String>(); if (all) { list.add("*"); } else { if (ipAddresses != null) { list.addAll(ipAddresses); } if (hostNames != null) { list.addAll(hostNames); } if (cidrAddresses != null) { for(SubnetUtils.SubnetInfo cidrAddress : cidrAddresses) { list.add(cidrAddress.getCidrSignature()); } } } return list; }
Example 4
Source File: SubnetUtilsTest.java From peer-os with Apache License 2.0 | 6 votes |
@Test public void testIp() { SubnetUtils utils = new SubnetUtils( "10.12.0.1/24" ); final SubnetUtils.SubnetInfo info = utils.getInfo(); assertEquals( "10.12.0.1", info.getAddress() ); assertEquals( "10.12.0.0", info.getNetworkAddress() ); assertEquals( "10.12.0.255", info.getBroadcastAddress() ); assertEquals( 254, info.getAddressCount() ); assertEquals( "10.12.0.1/24", info.getCidrSignature() ); assertEquals( "10.12.0.1", info.getLowAddress() ); assertEquals( "10.12.0.254", info.getHighAddress() ); assertEquals( "255.255.255.0", info.getNetmask() ); assertEquals( true, info.isInRange( "10.12.0.100" ) ); assertEquals( false, info.isInRange( "10.11.0.1" ) ); }
Example 5
Source File: DefaultSubnetCidrProvider.java From cloudbreak with Apache License 2.0 | 6 votes |
private String calculateSubnet(String networkCidr, Iterable<NetworkSubnetRequest> subnetCidrs) { SubnetUtils.SubnetInfo vpcInfo = new SubnetUtils(networkCidr).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; targetSubnet = Long.valueOf(targetSubnet % numberOfSubnets).intValue(); String cidr = getSubnetCidrInRange(networkCidr, subnetCidrs, targetSubnet, numberOfSubnets); if (cidr == null) { cidr = getSubnetCidrInRange(networkCidr, subnetCidrs, 0, targetSubnet); } if (cidr == null) { throw new CloudConnectorException("Cannot find non-overlapping CIDR range"); } return cidr; }
Example 6
Source File: NetworkUtils.java From zstack with Apache License 2.0 | 5 votes |
public static int getTotalIpInCidr(String cidr) { if (!isCidr(cidr)) { throw new IllegalArgumentException(String.format("%s is not a valid cidr", cidr)); } SubnetUtils.SubnetInfo range = new SubnetUtils(cidr).getInfo(); return getTotalIpInRange(range.getLowAddress(), range.getHighAddress()); }
Example 7
Source File: DefaultK8sNetwork.java From onos with Apache License 2.0 | 5 votes |
private IpAddress getGatewayIp(String cidr) { SubnetUtils utils = new SubnetUtils(cidr); utils.setInclusiveHostCount(false); SubnetUtils.SubnetInfo info = utils.getInfo(); return IpAddress.valueOf(info.getLowAddress()); }
Example 8
Source File: IPAddrUtil.java From sakai with Educational Community License v2.0 | 5 votes |
/** * Match an address against a list of IP CIDR addresses * * @param addrlist * The comma-separated list of addresses * @param addr * The IP address to match * @return true if address is contained in one or more of the CIDR network blocks listed in addrlist, false if not */ public static boolean matchIPList(String addrlist, String addr) { log.info("Checking login IP '" + addr + "' is contained in whitelist '" + addrlist + "'"); // TODO Support IPv6 if (StringUtils.isBlank(addrlist) || StringUtils.isBlank(addr)) return false; boolean match = false; for (String netaddr : Arrays.asList(addrlist.split(","))) { if (netaddr.contains("/")) { // Contained in subnet? try { SubnetUtils.SubnetInfo subnet = new SubnetUtils(netaddr.trim()).getInfo(); if (subnet.isInRange(addr)) { log.debug("IP Address " + addr + " is in network range " + subnet.getCidrSignature()); match = true; break; } } catch (IllegalArgumentException e) { log.warn("IP network address '" + netaddr + "' is not a valid CIDR format"); } } else { // Exact match? if (netaddr.trim().equals(addr)) { match = true; break; } } } return match; }
Example 9
Source File: IPAddrUtil.java From sakai with Educational Community License v2.0 | 5 votes |
/** * Match an address against a list of IP CIDR addresses * * @param addrlist * The comma-separated list of addresses * @param addr * The IP address to match * @return true if address is contained in one or more of the CIDR network blocks listed in addrlist, false if not */ public static boolean matchIPList(String addrlist, String addr) { log.info("Checking login IP '" + addr + "' is contained in whitelist '" + addrlist + "'"); // TODO Support IPv6 if (StringUtils.isBlank(addrlist) || StringUtils.isBlank(addr)) return false; boolean match = false; for (String netaddr : Arrays.asList(addrlist.split(","))) { if (netaddr.contains("/")) { // Contained in subnet? try { SubnetUtils.SubnetInfo subnet = new SubnetUtils(netaddr.trim()).getInfo(); if (subnet.isInRange(addr)) { log.debug("IP Address " + addr + " is in network range " + subnet.getCidrSignature()); match = true; break; } } catch (IllegalArgumentException e) { log.warn("IP network address '" + netaddr + "' is not a valid CIDR format"); } } else { // Exact match? if (netaddr.trim().equals(addr)) { match = true; break; } } } return match; }
Example 10
Source File: NetworkUtils.java From zstack with Apache License 2.0 | 5 votes |
public static boolean isSubCidr(String cidr, String subCidr) { DebugUtils.Assert(isCidr(cidr), String.format("%s is not a cidr", cidr)); DebugUtils.Assert(isCidr(subCidr), String.format("%s is not a cidr", subCidr)); SubnetUtils.SubnetInfo range = new SubnetUtils(cidr).getInfo(); SubnetUtils.SubnetInfo sub = new SubnetUtils(subCidr).getInfo(); return range.isInRange(sub.getLowAddress()) && range.isInRange(sub.getHighAddress()); }
Example 11
Source File: NetworkUtils.java From zstack with Apache License 2.0 | 5 votes |
public static List<String> filterIpv4sInCidr(List<String> ipv4s, String cidr){ DebugUtils.Assert(isCidr(cidr), String.format("%s is not a cidr", cidr)); SubnetUtils.SubnetInfo info = new SubnetUtils(cidr).getInfo(); List<String> results = new ArrayList<>(); for (String ipv4 : ipv4s) { validateIp(ipv4); if (isIpv4InRange(ipv4, info.getLowAddress(), info.getHighAddress())) { results.add(ipv4) ; } } return results; }
Example 12
Source File: NetworkUtils.java From zstack with Apache License 2.0 | 5 votes |
public static boolean isIpv4InCidr(String ipv4, String cidr) { DebugUtils.Assert(isCidr(cidr), String.format("%s is not a cidr", cidr)); validateIp(ipv4); SubnetUtils.SubnetInfo info = new SubnetUtils(cidr).getInfo(); return isIpv4InRange(ipv4, info.getLowAddress(), info.getHighAddress()); }
Example 13
Source File: NetworkUtils.java From zstack with Apache License 2.0 | 5 votes |
public static boolean isCidrOverlap(String cidr1, String cidr2) { DebugUtils.Assert(isCidr(cidr1), String.format("%s is not a cidr", cidr1)); DebugUtils.Assert(isCidr(cidr2), String.format("%s is not a cidr", cidr2)); SubnetUtils su1 = new SubnetUtils(cidr1); SubnetUtils su2 = new SubnetUtils(cidr2); SubnetUtils.SubnetInfo info1 = su1.getInfo(); SubnetUtils.SubnetInfo info2 = su2.getInfo(); return isIpv4RangeOverlap(info1.getLowAddress(), info1.getHighAddress(), info2.getLowAddress(), info2.getHighAddress()); }
Example 14
Source File: LoadBalancerApiInterceptor.java From zstack with Apache License 2.0 | 5 votes |
private void validate(APICreateLoadBalancerMsg msg) { List<String> useFor = Q.New(VipNetworkServicesRefVO.class).select(VipNetworkServicesRefVO_.serviceType).eq(VipNetworkServicesRefVO_.vipUuid, msg.getVipUuid()).listValues(); if(useFor != null && !useFor.isEmpty()){ VipUseForList useForList = new VipUseForList(useFor); if(!useForList.validateNewAdded(LoadBalancerConstants.LB_NETWORK_SERVICE_TYPE_STRING)){ throw new ApiMessageInterceptionException(argerr("the vip[uuid:%s] has been occupied other network service entity[%s]", msg.getVipUuid(), useForList.toString())); } } /* the vip can not the first of the last ip of the cidr */ VipVO vipVO = dbf.findByUuid(msg.getVipUuid(), VipVO.class); if (NetworkUtils.isIpv4Address(vipVO.getIp())) { AddressPoolVO addressPoolVO = dbf.findByUuid(vipVO.getIpRangeUuid(), AddressPoolVO.class); if (addressPoolVO == null) { return; } SubnetUtils utils = new SubnetUtils(addressPoolVO.getNetworkCidr()); SubnetUtils.SubnetInfo subnet = utils.getInfo(); String firstIp = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getLowAddress()) - 1); String lastIp = NetworkUtils.longToIpv4String(NetworkUtils.ipv4StringToLong(subnet.getHighAddress()) + 1); if (vipVO.getIp().equals(firstIp) || vipVO.getIp().equals(lastIp)) { throw new ApiMessageInterceptionException(argerr("loadbalancer vip [%s] can not the first of the last ip of the cidr", vipVO.getIp())); } } }
Example 15
Source File: DefaultSubnetCidrProvider.java From cloudbreak with Apache License 2.0 | 5 votes |
private String getSubnetCidrInRange(String networkCidr, Iterable<NetworkSubnetRequest> subnetCidrs, int start, int end) { SubnetUtils.SubnetInfo vpcInfo = new SubnetUtils(networkCidr).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 (NetworkSubnetRequest subnetCidr : subnetCidrs) { SubnetUtils.SubnetInfo subnetInfo = new SubnetUtils(subnetCidr.getCidr()).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, networkCidr); return subnet; } else { return null; } }
Example 16
Source File: FileBasedClusterNodeFirewall.java From localization_nifi with Apache License 2.0 | 5 votes |
@Override public boolean isPermissible(final String hostOrIp) { try { // if no rules, then permit everything if (subnetInfos.isEmpty()) { return true; } final String ip; try { ip = InetAddress.getByName(hostOrIp).getHostAddress(); } catch (final UnknownHostException uhe) { logger.warn("Blocking unknown host '{}'", hostOrIp, uhe); return false; } // check each subnet to see if IP is in range for (final SubnetUtils.SubnetInfo subnetInfo : subnetInfos) { if (subnetInfo.isInRange(ip)) { return true; } } // no match logger.debug("Blocking host '{}' because it does not match our allowed list.", hostOrIp); return false; } catch (final IllegalArgumentException iae) { logger.debug("Blocking requested host, '{}', because it is malformed.", hostOrIp, iae); return false; } }
Example 17
Source File: DefaultSubnetCidrProvider.java From cloudbreak with Apache License 2.0 | 4 votes |
private boolean isInRange(String address, SubnetUtils.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 18
Source File: SetupP2PStep.java From peer-os with Apache License 2.0 | 4 votes |
public void execute() throws EnvironmentModificationException, PeerException { // create p2p subnet util SubnetUtils.SubnetInfo p2pSubnetInfo = new SubnetUtils( environment.getP2pSubnet(), P2PUtil.P2P_SUBNET_MASK ).getInfo(); //get all subnet ips final Set<String> p2pAddresses = Sets.newHashSet( p2pSubnetInfo.getAllAddresses() ); //subtract already used ips for ( RhP2pIp rhP2pIp : environment.getP2pIps().getP2pIps() ) { p2pAddresses.remove( rhP2pIp.getP2pIp() ); } //obtain target RHs Map<String, Set<String>> peerRhIds = topology.getPeerRhIds(); //count total requested int totalIps = 0; P2pIps envP2pIps = environment.getP2pIps(); for ( Set<String> rhIds : peerRhIds.values() ) { for ( String rhId : rhIds ) { if ( envP2pIps.findByRhId( rhId ) == null ) { totalIps++; } } } if ( totalIps > p2pAddresses.size() ) { throw new EnvironmentModificationException( String.format( "Requested IP count %d is more than available %d", totalIps, p2pAddresses.size() ) ); } //p2p setup setupP2p( environment, peerRhIds, p2pAddresses ); // tunnel setup setupTunnel( environment ); }
Example 19
Source File: MachineList.java From big-c with Apache License 2.0 | 4 votes |
/** * Accepts a collection of ip/cidr/host addresses * * @param hostEntries * @param addressFactory addressFactory to convert host to InetAddress */ public MachineList(Collection<String> hostEntries, InetAddressFactory addressFactory) { this.addressFactory = addressFactory; if (hostEntries != null) { if ((hostEntries.size() == 1) && (hostEntries.contains(WILDCARD_VALUE))) { all = true; ipAddresses = null; hostNames = null; cidrAddresses = null; } else { all = false; Set<String> ips = new HashSet<String>(); List<SubnetUtils.SubnetInfo> cidrs = new LinkedList<SubnetUtils.SubnetInfo>(); Set<String> hosts = new HashSet<String>(); for (String hostEntry : hostEntries) { //ip address range if (hostEntry.indexOf("/") > -1) { try { SubnetUtils subnet = new SubnetUtils(hostEntry); subnet.setInclusiveHostCount(true); cidrs.add(subnet.getInfo()); } catch (IllegalArgumentException e) { LOG.warn("Invalid CIDR syntax : " + hostEntry); throw e; } } else if (InetAddresses.isInetAddress(hostEntry)) { //ip address ips.add(hostEntry); } else { //hostname hosts.add(hostEntry); } } ipAddresses = (ips.size() > 0) ? ips : null; cidrAddresses = (cidrs.size() > 0) ? cidrs : null; hostNames = (hosts.size() > 0) ? hosts : null; } } else { all = false; ipAddresses = null; hostNames = null; cidrAddresses = null; } }
Example 20
Source File: MachineList.java From hadoop with Apache License 2.0 | 4 votes |
/** * Accepts a collection of ip/cidr/host addresses * * @param hostEntries * @param addressFactory addressFactory to convert host to InetAddress */ public MachineList(Collection<String> hostEntries, InetAddressFactory addressFactory) { this.addressFactory = addressFactory; if (hostEntries != null) { if ((hostEntries.size() == 1) && (hostEntries.contains(WILDCARD_VALUE))) { all = true; ipAddresses = null; hostNames = null; cidrAddresses = null; } else { all = false; Set<String> ips = new HashSet<String>(); List<SubnetUtils.SubnetInfo> cidrs = new LinkedList<SubnetUtils.SubnetInfo>(); Set<String> hosts = new HashSet<String>(); for (String hostEntry : hostEntries) { //ip address range if (hostEntry.indexOf("/") > -1) { try { SubnetUtils subnet = new SubnetUtils(hostEntry); subnet.setInclusiveHostCount(true); cidrs.add(subnet.getInfo()); } catch (IllegalArgumentException e) { LOG.warn("Invalid CIDR syntax : " + hostEntry); throw e; } } else if (InetAddresses.isInetAddress(hostEntry)) { //ip address ips.add(hostEntry); } else { //hostname hosts.add(hostEntry); } } ipAddresses = (ips.size() > 0) ? ips : null; cidrAddresses = (cidrs.size() > 0) ? cidrs : null; hostNames = (hosts.size() > 0) ? hosts : null; } } else { all = false; ipAddresses = null; hostNames = null; cidrAddresses = null; } }