Class CidrSet
Cidr blocks that answers "is this address in one of them" in a handful of hash lookups, however
many blocks it holds - what a client-ip: rule asks on every request it reaches.
Until 2.13.0 a rule walked its blocks one by one and each block parsed the visitor's address again. Harmless for
the dozen blocks an operator writes by hand; not for a list a plugin maintains (conf/lists/, written
through PluginContext.writeList), which may hold tens of thousands of single addresses - a ban list - and
would have cost every request that many comparisons and address parses. Here the address is parsed once, and the
blocks are grouped by family and prefix length: one masked lookup per distinct prefix length the set holds (at most
33 for IPv4, 129 for IPv6; a list of single addresses is one).
Immutable, so a reader needs no lock. Matching is Cidr.contains(InetAddress)'s: by raw address bytes, so an IPv4 block
never covers an IPv6 address (an IPv4-mapped literal is parsed by the JDK as the IPv4 address).
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleancontains(InetAddress address) booleanisEmpty()static CidrSetof(Collection<Cidr> blocks) intsize()How many blocks it was built from.
-
Field Details
-
EMPTY
The set with no block: contains nothing.
-
-
Method Details
-
of
- Parameters:
blocks- the blocks, nulls skipped; the same block twice counts twice insize()
-
size
public int size()How many blocks it was built from. -
isEmpty
public boolean isEmpty() -
contains
- Parameters:
addressLiteral- an address asCidr.parseAddress(String)reads it (a trailing port, brackets and a zone id are tolerated) - never a host name, so this never looks anything up- Returns:
- whether it is in one of the blocks; false for null or anything that is not an address
-
contains
- Returns:
- whether the address is in one of the blocks; false for null
-