/** * Matches an InetAddress with the CIDR notation of a subnet. * * @param cidrPattern CIDR notation of the subnet * @param inetAddress address to be matched * @return true if inetAddress matches cidrPattern, false otherwise */ public static boolean matchCidrPattern(InetAddress inetAddress, String cidrPattern) { return Subnet.create(cidrPattern).contains(inetAddress); }
/** * @return an instance of Subnet that matches all IPs */ public static Subnet create() { return new Subnet() { private static final long serialVersionUID = 97750694361406752L; @Override public boolean contains(InetAddress inetAddress) { return true; } }; }
/** * @param hostname hostname to be checked agains the subnet * @return true if hostname can be evaluated to one or more IPs that are contained in the subnet represented by this instance, otherwise false * @throws UnknownHostException */ public boolean containsHost(String hostname) throws UnknownHostException { return containsAny(InetAddress.getAllByName(hostname)); }
public static Subnet validateCreateSubnet(String subnet) { try { return Subnet.create(subnet); } catch (IllegalArgumentException iae) { throw new IllegalArgumentException( String.format("Invalid IP/subnet '%s': %s\n.", subnet, iae.getMessage())); } } }
/** * @param inetAddresses addresses to be checked against the subnet * @return true if any of the inetAddresses is contained in the subnet represented by this instance, otherwise false */ public boolean containsAny(InetAddress[] inetAddresses) { for (InetAddress inetAddress : inetAddresses) { if (contains(inetAddress)) { return true; } } return false; }
/** * @param inetAddress address to be checked against the subnet * @return true if inetAddress is contained in the subnet represented by this instance, otherwise false */ public boolean contains(InetAddress inetAddress) { return IPMatcher.match(inetAddress, getSubnetAddress(), getMask() ); }
@Override public boolean equals(Object other) { if ((other == null) || !(other instanceof Subnet)) { return false; } return other.toString().equals(this.toString()); }
!_allowed.isEmpty() && _allowed.stream() .noneMatch(s -> s.contains(remoteAddress.getAddress()))) { throw new IOException("Remote Host (" + remoteAddress.getAddress()
/** * Creates an instance of a subnet from an InetAddress and a mask in CIDR notation. * * @param subnetAddress subnet base address * @param mask mask in CIDR notation (number of topmost relevant bits) * @return an instance of Subnet created from address and mask */ public static Subnet create(InetAddress subnetAddress, int mask) { return new Subnet(subnetAddress, mask); }