Class CidrAddressBlock

java.lang.Object
google.registry.util.CidrAddressBlock
All Implemented Interfaces:
Serializable, Iterable<InetAddress>

public class CidrAddressBlock extends Object implements Iterable<InetAddress>, Serializable
Class representing an RFC 1519 CIDR IP address block.

When creating a CidrAddressBlock from an IP string literal without a specified CIDR netmask (i.e. no trailing "/16" or "/64") or an InetAddress with an accompanying integer netmask, then the maximum length netmask for the address famiy of the specified address is used (i.e. 32 for IPv4, 128 for IPv6). I.e. "1.2.3.4" is automatically treated as "1.2.3.4/32" and, similarly, "2001:db8::1" is automatically treated as "2001:db8::1/128".

See Also:
  • Constructor Details

    • CidrAddressBlock

      public CidrAddressBlock(String s)
      Attempts to parse the given String into a CIDR block.

      If the string is an IP string literal without a specified CIDR netmask (i.e. no trailing "/16" or "/64") then the maximum length netmask for the address famiy of the specified address is used (i.e. 32 for IPv4, 128 for IPv6).

      The specified IP address portion must be properly truncated (i.e. all the host bits must be zero) or the input is considered malformed. For example, "1.2.3.0/24" is accepted but "1.2.3.4/24" is not. Similarly, for IPv6, "2001:db8::/32" is accepted whereas "2001:db8::1/32" is not.

      If inputs might not be properly truncated but would be acceptable to the application consider constructing a CidrAddressBlock via create().

      Parameters:
      s - a String of the form "217.68.0.0/16" or "2001:db8::/32".
      Throws:
      IllegalArgumentException - if s is malformed or does not represent a valid CIDR block.
    • CidrAddressBlock

      public CidrAddressBlock(String ip, int netmask)
      Attempts to parse the given String and int into a CIDR block.

      The specified IP address portion must be properly truncated (i.e. all the host bits must be zero) or the input is considered malformed. For example, "1.2.3.0/24" is accepted but "1.2.3.4/24" is not. Similarly, for IPv6, "2001:db8::/32" is accepted whereas "2001:db8::1/32" is not.

      An IP address without a netmask will automatically have the maximum applicable netmask for its address family. I.e. "1.2.3.4" is automatically treated as "1.2.3.4/32", and "2001:db8::1" is automatically treated as "2001:db8::1/128".

      If inputs might not be properly truncated but would be acceptable to the application consider constructing a CidrAddressBlock via create().

      Parameters:
      ip - a String of the form "217.68.0.0" or "2001:db8::".
      netmask - an int between 0 and 32 (for IPv4) or 128 (for IPv6). This is the number of bits, starting from the big end of the IP, that will be used for network bits (as opposed to host bits) in this CIDR block.
      Throws:
      IllegalArgumentException - if the params are malformed or do not represent a valid CIDR block.
    • CidrAddressBlock

      public CidrAddressBlock(InetAddress ip)
    • CidrAddressBlock

      public CidrAddressBlock(InetAddress ip, int netmask)
  • Method Details

    • create

      public static CidrAddressBlock create(InetAddress ip, int netmask)
      Attempts to construct a CIDR block from the IP address and netmask, truncating the IP address as required.

      The specified IP address portion need not be properly truncated (i.e. all the host bits need not be zero); truncation will be silently performed. For example, "1.2.3.4/24" is accepted and returns the same CidrAddressBlock as "1.2.3.0/24". Similarly, for IPv6, "2001:db8::1/32" is accepted and returns the same CidrAddressBlock as "2001:db8::/32".

      Parameters:
      ip - InetAddress, possibly requiring truncation.
      netmask - an int between 0 and 32 (for IPv4) or 128 (for IPv6). This is the number of bits, starting from the big end of the IP, that will be used for network bits (as opposed to host bits) when truncating the supplied InetAddress.
      Throws:
      IllegalArgumentException - if the params are malformed or do not represent a valid CIDR block.
      NullPointerException - if a parameter is null.
    • create

      public static CidrAddressBlock create(String s)
      Attempts to construct a CIDR block from the IP address and netmask expressed as a String, truncating the IP address as required.

      The specified IP address portion need not be properly truncated (i.e. all the host bits need not be zero); truncation will be silently performed. For example, "1.2.3.4/24" is accepted and returns the same CidrAddressBlock as "1.2.3.0/24". Similarly, for IPv6, "2001:db8::1/32" is accepted and returns the same CidrAddressBlock as "2001:db8::/32".

      Parameters:
      s - String representing either a single IP address or a CIDR netblock, possibly requiring truncation.
      Throws:
      IllegalArgumentException - if the params are malformed or do not represent a valid CIDR block.
      NullPointerException - if a parameter is null.
    • getIp

      public String getIp()
      Returns the standard String representation of the IP portion of this CIDR block (a.b.c.d, or a:b:c::d)

      NOTE: This is not reliable for comparison operations. It is more reliable to normalize strings into InetAddresss and then compare.

      Consider:

      • "10.11.12.0" is equivalent to "10.11.12.000"
      • "2001:db8::" is equivalent to "2001:0DB8:0000:0000:0000:0000:0000:0000"
    • getInetAddress

      public InetAddress getInetAddress()
    • getNetmask

      public int getNetmask()
      Returns the number of leading bits (prefix size) of the routing prefix.
    • contains

      public boolean contains(@Nullable InetAddress ipAddr)
      Returns true if the supplied InetAddress is within this CidrAddressBlock, false otherwise.

      This can be used to test if the argument falls within a well-known network range, a la GoogleIp's isGoogleIp(), isChinaIp(), et alia.

      Parameters:
      ipAddr - InetAddress to evaluate.
      Returns:
      true if ipAddr is logically within this block, false otherwise.
    • contains

      public boolean contains(@Nullable CidrAddressBlock cidr)
      Returns true if the supplied CidrAddressBlock is within this CidrAddressBlock, false otherwise.

      This can be used to test if the argument falls within a well-known network range, a la GoogleIp's isGoogleIp(), isChinaIp(), et alia.

      Parameters:
      cidr - CidrAddressBlock to evaluate.
      Returns:
      true if cidr is logically within this block, false otherwise.
    • contains

      public boolean contains(@Nullable String s)
      Returns true if the supplied String is within this CidrAddressBlock, false otherwise.

      This can be used to test if the argument falls within a well-known network range, a la GoogleIp's isGoogleIp(), isChinaIp(), et alia.

      Parameters:
      s - String to evaluate.
      Returns:
      true if s is logically within this block, false otherwise.
    • getAllOnesAddress

      public InetAddress getAllOnesAddress()
      Returns the address that is contained in this CidrAddressBlock with the most bits set.

      This can be used to calculate the upper bound address of the address range for this CidrAddressBlock.

    • iterator

      public Iterator<InetAddress> iterator()
      Specified by:
      iterator in interface Iterable<InetAddress>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object