NetMask (Apache Tomcat 11.0.21 API Documentation)
Description: declaration: package: org.apache.catalina.util, class: NetMask

# Class NetMask

java.lang.Object
org.apache.catalina.util.NetMask

public final class NetMask extends Object
A class representing a CIDR netmask.

The constructor takes a string as an argument which represents a netmask, as per the CIDR notation -- whether this netmask be IPv4 or IPv6. It then extracts the network address (before the /) and the CIDR prefix (after the /), and tells through the #matches() method whether a candidate `InetAddress` object fits in the recorded range.

As byte arrays as returned by `InetAddress.getByName()` are always in network byte order, finding a match is therefore as simple as testing whether the n first bits (where n is the CIDR) are the same in both byte arrays (the one of the network address and the one of the candidate address). We do that by first doing byte comparisons, then testing the last bits if any (that is, if the remainder of the integer division of the CIDR by 8 is not 0).

As a bonus, if no '/' is found in the input, it is assumed that an exact address match is required.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `NetMask(String input)`
  Constructor

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object o)`
  `int`
  `hashCode()`
  `boolean`
  `matches(InetAddress addr)`
  Test if a given address matches this netmask.
  `boolean`
  `matches(InetAddress addr, boolean checkedPort)`
  Test if a given address matches this netmask.
  `boolean`
  `matches(InetAddress addr, int port)`
  Test if a given address and port matches this netmask.
  `String`
  `toString()`

  ### Methods inherited from class Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### NetMask

    public NetMask(String input)
    Constructor
    Parameters:
      `input` - the CIDR netmask
    Throws:
      `IllegalArgumentException` - if the netmask is not correct (invalid address specification, malformed CIDR prefix, etc)

- ## Method Details

  - ### matches

    public boolean matches(InetAddress addr, int port)
    Test if a given address and port matches this netmask.
    Parameters:
      `addr` - The `InetAddress` to test
      `port` - The port to test
    Returns:
      true on match, false otherwise

  - ### matches

    public boolean matches(InetAddress addr)
    Test if a given address matches this netmask.
    Parameters:
      `addr` - The `InetAddress` to test
    Returns:
      true on match, false otherwise

  - ### matches

    public boolean matches(InetAddress addr, boolean checkedPort)
    Test if a given address matches this netmask.
    Parameters:
      `addr` - The `InetAddress` to test
      `checkedPort` - Indicates, whether we already checked the port
    Returns:
      true on match, false otherwise

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### equals

    public boolean equals(Object o)
    Overrides:
      `equals` in class `Object`

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `Object`