Top Description Fields Constructors Methods
org.apache.catalina.util

public final Class NetMask

extends Object
Class Inheritance
Imports
java.net.InetAddress, .UnknownHostException, java.util.Arrays, java.util.regex.Pattern, .PatternSyntaxException, org.apache.tomcat.util.res.StringManager

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.

Field Summary

Modifier and TypeField and Description
private final String
expression

The argument to the constructor, used for .toString()

private final boolean
foundPort

Should we use the port pattern when matching

private final int
lastByteShift

The right shift to apply to the last byte if CIDR % 8 is not 0; if it is 0, this variable is set to 0

private final byte[]
netaddr

The byte array representing the address extracted from the expression

private final int
nrBytes

The number of bytes to test for equality (CIDR / 8)

private final Pattern
portPattern

The regular expression used to test for the server port (optional).

private static final StringManager

Constructor Summary

AccessConstructor and Description
public
NetMask(final String
the CIDR netmask
input
)

Constructor

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean

Returns:

true on match, false otherwise
matches
(final InetAddress addr, int
The port to test
port
)

Test if a given address and port matches this netmask.

public boolean

Returns:

true on match, false otherwise
matches
(final InetAddress addr)

Test if a given address matches this netmask.

public boolean

Returns:

true on match, false otherwise
matches
(final InetAddress addr, boolean
Indicates, whether we already checked the port
checkedPort
)

Test if a given address matches this netmask.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

expressionback to summary
private final String expression

The argument to the constructor, used for .toString()

foundPortback to summary
private final boolean foundPort

Should we use the port pattern when matching

lastByteShiftback to summary
private final int lastByteShift

The right shift to apply to the last byte if CIDR % 8 is not 0; if it is 0, this variable is set to 0

netaddrback to summary
private final byte[] netaddr

The byte array representing the address extracted from the expression

nrBytesback to summary
private final int nrBytes

The number of bytes to test for equality (CIDR / 8)

portPatternback to summary
private final Pattern portPattern

The regular expression used to test for the server port (optional).

smback to summary
private static final StringManager sm

Constructor Detail

NetMaskback to summary
public NetMask(final String input)

Constructor

Parameters
input:String

the CIDR netmask

Exceptions
IllegalArgumentException:
if the netmask is not correct (invalid address specification, malformed CIDR prefix, etc)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
matchesback to summary
public boolean matches(final InetAddress addr, int port)

Test if a given address and port matches this netmask.

Parameters
addr:InetAddress

The java.net.InetAddress to test

port:int

The port to test

Returns:boolean

true on match, false otherwise

matchesback to summary
public boolean matches(final InetAddress addr)

Test if a given address matches this netmask.

Parameters
addr:InetAddress

The java.net.InetAddress to test

Returns:boolean

true on match, false otherwise

matchesback to summary
public boolean matches(final InetAddress addr, boolean checkedPort)

Test if a given address matches this netmask.

Parameters
addr:InetAddress

The java.net.InetAddress to test

checkedPort:boolean

Indicates, whether we already checked the port

Returns:boolean

true on match, false otherwise

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override