Class CidrSet

java.lang.Object
org.frontcache.core.CidrSet

public final class CidrSet extends Object
A set of 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 Details

    • EMPTY

      public static final CidrSet EMPTY
      The set with no block: contains nothing.
  • Method Details

    • of

      public static CidrSet of(Collection<Cidr> blocks)
      Parameters:
      blocks - the blocks, nulls skipped; the same block twice counts twice in size()
    • size

      public int size()
      How many blocks it was built from.
    • isEmpty

      public boolean isEmpty()
    • contains

      public boolean contains(String addressLiteral)
      Parameters:
      addressLiteral - an address as Cidr.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

      public boolean contains(InetAddress address)
      Returns:
      whether the address is in one of the blocks; false for null