/** * Converts an IP address to a subnet using the provided mask * * @param address * The address to convert into a subnet * @return The subnet as an integer */ private long toSubnet(InetAddress address) { if (address instanceof Inet4Address) { return toInt(address) & (int) subnetMask; } else { return toLong(address) & subnetMask; } }
/** * Checks if the {@link InetAddress} is within this subnet * @param address The {@link InetAddress} to check * @return True if the address is within this subnet, false otherwise */ public boolean inSubnet(InetAddress address) { return toSubnet(address) == subnetInt; }
/** * Unblocks the specified endpoint. * * @param address The address to unblock */ public void unblock(InetAddress address) { if (address == null) { throw new IllegalArgumentException("Adress to unblock can not be null"); } unblock(new Subnet(address, 32)); }
private boolean isBlocked(IoSession session) { SocketAddress remoteAddress = session.getRemoteAddress(); if (remoteAddress instanceof InetSocketAddress) { InetAddress address = ((InetSocketAddress) remoteAddress).getAddress(); // check all subnets for (Subnet subnet : blacklist) { if (subnet.inSubnet(address)) { return true; } } } return false; } }
/** * Converts an IP address to a subnet using the provided * mask * @param address The address to convert into a subnet * @return The subnet as an integer */ private int toSubnet(InetAddress address) { return toInt(address) & subnetMask; }
/** * Blocks the specified endpoint. * * @param address The address to block */ public void block(InetAddress address) { if (address == null) { throw new IllegalArgumentException("Adress to block can not be null"); } block(new Subnet(address, 32)); }
private boolean isBlocked(IoSession session) { SocketAddress remoteAddress = session.getRemoteAddress(); if (remoteAddress instanceof InetSocketAddress) { InetAddress address = ((InetSocketAddress) remoteAddress).getAddress(); // check all subnets for (Subnet subnet : blacklist) { if (subnet.inSubnet(address)) { return true; } } } return false; } }
/** * Creates a subnet from CIDR notation. For example, the subnet * 192.168.0.0/24 would be created using the {@link InetAddress} * 192.168.0.0 and the mask 24. * @param subnet The {@link InetAddress} of the subnet * @param mask The mask */ public Subnet(InetAddress subnet, int mask) { if(subnet == null) { throw new NullPointerException("Subnet address can not be null"); } if(!(subnet instanceof Inet4Address)) { throw new IllegalArgumentException("Only IPv4 supported"); } if(mask < 0 || mask > 32) { throw new IllegalArgumentException("Mask has to be an integer between 0 and 32"); } this.subnet = subnet; this.subnetInt = toInt(subnet); this.suffix = mask; // binary mask for this subnet this.subnetMask = IP_MASK >> (mask - 1); }
/** * Blocks the specified endpoint. * * @param address The address to block */ public void block(InetAddress address) { if (address == null) { throw new IllegalArgumentException("Adress to block can not be null"); } block(new Subnet(address, 32)); }
/** * Converts an IP address to a subnet using the provided mask * * @param address * The address to convert into a subnet * @return The subnet as an integer */ private long toSubnet(InetAddress address) { if (address instanceof Inet4Address) { return toInt(address) & (int) subnetMask; } else { return toLong(address) & subnetMask; } }
private boolean isBlocked(IoSession session) { SocketAddress remoteAddress = session.getRemoteAddress(); if (remoteAddress instanceof InetSocketAddress) { InetAddress address = ((InetSocketAddress) remoteAddress).getAddress(); // check all subnets for(Subnet subnet : blacklist) { if(subnet.inSubnet(address)) { return true; } } } return false; } }
/** * Checks if the {@link InetAddress} is within this subnet * @param address The {@link InetAddress} to check * @return True if the address is within this subnet, false otherwise */ public boolean inSubnet(InetAddress address) { if (address.isAnyLocalAddress()) { return true; } if (address instanceof Inet4Address) { return (int) toSubnet(address) == subnetInt; } else { return toSubnet(address) == subnetLong; } }
/** * Unblocks the specified endpoint. * * @param address The address to unblock */ public void unblock(InetAddress address) { if (address == null) { throw new IllegalArgumentException("Adress to unblock can not be null"); } unblock(new Subnet(address, 32)); }
/** * Checks if the {@link InetAddress} is within this subnet * @param address The {@link InetAddress} to check * @return True if the address is within this subnet, false otherwise */ public boolean inSubnet(InetAddress address) { if (address.isAnyLocalAddress()) { return true; } if (address instanceof Inet4Address) { return (int) toSubnet(address) == subnetInt; } else { return toSubnet(address) == subnetLong; } }
/** * Blocks the specified endpoint. */ public void block(InetAddress address) { if (address == null) { throw new NullPointerException("Adress to block can not be null"); } block(new Subnet(address, 32)); }
/** * Unblocks the specified endpoint. */ public void unblock(InetAddress address) { if (address == null) { throw new NullPointerException("Adress to unblock can not be null"); } unblock(new Subnet(address, 32)); }