Class CidrAddressBlock
- All Implemented Interfaces:
Serializable
,Iterable<InetAddress>
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:
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionAttempts to parse the given String into a CIDR block.CidrAddressBlock
(String ip, int netmask) Attempts to parse the given String and int into a CIDR block.CidrAddressBlock
(InetAddress ip, int netmask) -
Method Summary
Modifier and TypeMethodDescriptionboolean
contains
(CidrAddressBlock cidr) Returnstrue
if the suppliedCidrAddressBlock
is within thisCidrAddressBlock
,false
otherwise.boolean
Returnstrue
if the suppliedString
is within thisCidrAddressBlock
,false
otherwise.boolean
contains
(InetAddress ipAddr) static CidrAddressBlock
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
(InetAddress ip, int netmask) Attempts to construct a CIDR block from the IP address and netmask, truncating the IP address as required.boolean
Returns the address that is contained in thisCidrAddressBlock
with the most bits set.getIp()
Returns the standardString
representation of the IP portion of this CIDR block (a.b.c.d, or a:b:c::d)int
Returns the number of leading bits (prefix size) of the routing prefix.int
hashCode()
iterator()
toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
CidrAddressBlock
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:
IllegalArgumentException
- if s is malformed or does not represent a valid CIDR block.
-
CidrAddressBlock
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:
IllegalArgumentException
- if the params are malformed or do not represent a valid CIDR block.
-
CidrAddressBlock
-
CidrAddressBlock
-
-
Method Details
-
create
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:
IllegalArgumentException
- if the params are malformed or do not represent a valid CIDR block.NullPointerException
- if a parameter is null.
-
create
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:
IllegalArgumentException
- if the params are malformed or do not represent a valid CIDR block.NullPointerException
- if a parameter is null.
-
getIp
Returns the standardString
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
-
getNetmask
public int getNetmask()Returns the number of leading bits (prefix size) of the routing prefix. -
contains
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
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
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
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
- Specified by:
iterator
in interfaceIterable<InetAddress>
-
hashCode
public int hashCode() -
equals
-
toString
-