Class CidrAddressBlock
- java.lang.Object
-
- google.registry.util.CidrAddressBlock
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<java.net.InetAddress>
public class CidrAddressBlock extends java.lang.Object implements java.lang.Iterable<java.net.InetAddress>, java.io.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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CidrAddressBlock.CidrAddressBlockAdapter
-
Constructor Summary
Constructors Constructor Description CidrAddressBlock(java.lang.String s)
Attempts to parse the given String into a CIDR block.CidrAddressBlock(java.lang.String ip, int netmask)
Attempts to parse the given String and int into a CIDR block.CidrAddressBlock(java.net.InetAddress ip)
CidrAddressBlock(java.net.InetAddress ip, int netmask)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(CidrAddressBlock cidr)
Returnstrue
if the suppliedCidrAddressBlock
is within thisCidrAddressBlock
,false
otherwise.boolean
contains(java.lang.String s)
Returnstrue
if the suppliedString
is within thisCidrAddressBlock
,false
otherwise.boolean
contains(java.net.InetAddress ipAddr)
Returnstrue
if the suppliedInetAddress
is within thisCidrAddressBlock
,false
otherwise.static CidrAddressBlock
create(java.lang.String s)
Attempts to construct a CIDR block from the IP address and netmask expressed as a String, truncating the IP address as required.static CidrAddressBlock
create(java.net.InetAddress ip, int netmask)
Attempts to construct a CIDR block from the IP address and netmask, truncating the IP address as required.boolean
equals(java.lang.Object o)
java.net.InetAddress
getAllOnesAddress()
Returns the address that is contained in thisCidrAddressBlock
with the most bits set.java.net.InetAddress
getInetAddress()
java.lang.String
getIp()
int
getNetmask()
Returns the number of leading bits (prefix size) of the routing prefix.int
hashCode()
java.util.Iterator<java.net.InetAddress>
iterator()
java.lang.String
toString()
-
-
-
Constructor Detail
-
CidrAddressBlock
public CidrAddressBlock(java.lang.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
viacreate()
.- Parameters:
s
- a String of the form "217.68.0.0/16" or "2001:db8::/32".- Throws:
java.lang.IllegalArgumentException
- if s is malformed or does not represent a valid CIDR block.
-
CidrAddressBlock
public CidrAddressBlock(java.lang.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
viacreate()
.- 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:
java.lang.IllegalArgumentException
- if the params are malformed or do not represent a valid CIDR block.
-
CidrAddressBlock
public CidrAddressBlock(java.net.InetAddress ip)
-
CidrAddressBlock
public CidrAddressBlock(java.net.InetAddress ip, int netmask)
-
-
Method Detail
-
create
public static CidrAddressBlock create(java.net.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 sameCidrAddressBlock
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 suppliedInetAddress
.- Throws:
java.lang.IllegalArgumentException
- if the params are malformed or do not represent a valid CIDR block.java.lang.NullPointerException
- if a parameter is null.
-
create
public static CidrAddressBlock create(java.lang.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 sameCidrAddressBlock
as "2001:db8::/32".- Parameters:
s
-String
representing either a single IP address or a CIDR netblock, possibly requiring truncation.- Throws:
java.lang.IllegalArgumentException
- if the params are malformed or do not represent a valid CIDR block.java.lang.NullPointerException
- if a parameter is null.
-
getIp
public java.lang.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
InetAddress
s 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 java.net.InetAddress getInetAddress()
-
getNetmask
public int getNetmask()
Returns the number of leading bits (prefix size) of the routing prefix.
-
contains
public boolean contains(@Nullable java.net.InetAddress ipAddr)
Returnstrue
if the suppliedInetAddress
is within thisCidrAddressBlock
,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
ifipAddr
is logically within this block,false
otherwise.
-
contains
public boolean contains(@Nullable CidrAddressBlock cidr)
Returnstrue
if the suppliedCidrAddressBlock
is within thisCidrAddressBlock
,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
ifcidr
is logically within this block,false
otherwise.
-
contains
public boolean contains(@Nullable java.lang.String s)
Returnstrue
if the suppliedString
is within thisCidrAddressBlock
,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
ifs
is logically within this block,false
otherwise.
-
getAllOnesAddress
public java.net.InetAddress getAllOnesAddress()
Returns the address that is contained in thisCidrAddressBlock
with the most bits set.This can be used to calculate the upper bound address of the address range for this
CidrAddressBlock
.
-
iterator
public java.util.Iterator<java.net.InetAddress> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<java.net.InetAddress>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(@Nullable java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-