Top Description Fields Constructors Methods
io.netty5.handler.ipfilter

public Class IpSubnetFilter

extends AbstractRemoteAddressFilter<InetSocketAddress>
Class Inheritance
Imports
io.netty5.channel.Channel, .ChannelHandlerContext, java.net.Inet4Address, .InetSocketAddress, .SocketAddress, java.util.ArrayList, .Arrays, .Collections, .Iterator, .List

This class allows one to filter new Channels based on the IpSubnetFilters passed to its constructor. If no rules are provided, all connections will be accepted since acceptIfNotFound is true by default.

If you would like to explicitly take action on rejected Channels, you should override AbstractRemoteAddressFilter#channelRejected(ChannelHandlerContext, SocketAddress).

Few Points to keep in mind:

  1. Since IpSubnetFilter uses Binary search algorithm, it's a good idea to insert IP addresses in incremental order.
  2. Remove any over-lapping CIDR.

Field Summary

Modifier and TypeField and Description
private final boolean
private final IpFilterRuleType
private final IpFilterRuleType
private final List<IpSubnetFilterRule>
private final List<IpSubnetFilterRule>

Constructor Summary

AccessConstructor and Description
public
IpSubnetFilter(IpSubnetFilterRule...
IpSubnetFilterRule as an array
rules
)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as array.

public
IpSubnetFilter(boolean
true if we'll accept connection if not found in rule(s).
acceptIfNotFound
,
IpSubnetFilterRule...
IpSubnetFilterRule as an array
rules
)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as array and specify if we'll accept a connection if we don't find it in the rule(s).

public
public
IpSubnetFilter(boolean
true if we'll accept connection if not found in rule(s).
acceptIfNotFound
,
List<IpSubnetFilterRule> rules)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as List and specify if we'll accept a connection if we don't find it in the rule(s).

Method Summary

Modifier and TypeMethod and Description
protected boolean
accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress)

Implements abstract io.netty5.handler.ipfilter.AbstractRemoteAddressFilter.accept.

This method is called immediately after a io.netty5.channel.Channel gets registered.
public boolean
isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline.
private static List<IpSubnetFilterRule>
sortAndFilter(List<IpSubnetFilterRule> rules)

  1. Sort the list
  2. Remove over-lapping subnet
  3. Sort the list again
Inherited from io.netty5.handler.ipfilter.AbstractRemoteAddressFilter:
channelAcceptedchannelActivechannelRegisteredchannelRejected

Field Detail

acceptIfNotFoundback to summary
private final boolean acceptIfNotFound
ipFilterRuleTypeIPv4back to summary
private final IpFilterRuleType ipFilterRuleTypeIPv4
ipFilterRuleTypeIPv6back to summary
private final IpFilterRuleType ipFilterRuleTypeIPv6
ipv4Rulesback to summary
private final List<IpSubnetFilterRule> ipv4Rules
ipv6Rulesback to summary
private final List<IpSubnetFilterRule> ipv6Rules

Constructor Detail

IpSubnetFilterback to summary
public IpSubnetFilter(IpSubnetFilterRule... rules)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as array.

acceptIfNotFound is set to true.

Parameters
rules:IpSubnetFilterRule[]

IpSubnetFilterRule as an array

IpSubnetFilterback to summary
public IpSubnetFilter(boolean acceptIfNotFound, IpSubnetFilterRule... rules)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as array and specify if we'll accept a connection if we don't find it in the rule(s).

Parameters
acceptIfNotFound:boolean

true if we'll accept connection if not found in rule(s).

rules:IpSubnetFilterRule[]

IpSubnetFilterRule as an array

IpSubnetFilterback to summary
public IpSubnetFilter(List<IpSubnetFilterRule> rules)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as List.

acceptIfNotFound is set to true.

Parameters
rules:List<IpSubnetFilterRule>

IpSubnetFilterRule as a List

IpSubnetFilterback to summary
public IpSubnetFilter(boolean acceptIfNotFound, List<IpSubnetFilterRule> rules)

Create new IpSubnetFilter Instance with specified IpSubnetFilterRule as List and specify if we'll accept a connection if we don't find it in the rule(s).

Parameters
acceptIfNotFound:boolean

true if we'll accept connection if not found in rule(s).

rules:List<IpSubnetFilterRule>

IpSubnetFilterRule as a List

Method Detail

acceptback to summary
protected boolean accept(ChannelHandlerContext ctx, InetSocketAddress remoteAddress)

Implements abstract io.netty5.handler.ipfilter.AbstractRemoteAddressFilter.accept.

Doc from io.netty5.handler.ipfilter.AbstractRemoteAddressFilter.accept.

This method is called immediately after a io.netty5.channel.Channel gets registered.

Returns:boolean

Return true if connections from this IP address and port should be accepted. False otherwise.

Annotations
@Override
isSharableback to summary
public boolean isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Doc from io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline. By default, this method returns false. If this method returns false, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.

Annotations
@Override
sortAndFilterback to summary
private static List<IpSubnetFilterRule> sortAndFilter(List<IpSubnetFilterRule> rules)

  1. Sort the list
  2. Remove over-lapping subnet
  3. Sort the list again
Annotations
@SuppressWarnings:ConstantConditions