Package org.wildfly.common.net
Class CidrAddress
- java.lang.Object
-
- org.wildfly.common.net.CidrAddress
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<CidrAddress>
public final class CidrAddress extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<CidrAddress>
A Classless Inter-Domain Routing address. This is the combination of an IP address and a netmask.- Author:
- David M. Lloyd
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static CidrAddress
INET4_ANY_CIDR
The CIDR address representing all IPv4 addresses.static CidrAddress
INET6_ANY_CIDR
The CIDR address representing all IPv6 addresses.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareAddressBytesTo(byte[] otherBytes, int otherNetmaskBits, int scopeId)
int
compareTo(CidrAddress other)
static CidrAddress
create(byte[] addressBytes, int netmaskBits)
Create a new CIDR address.static CidrAddress
create(java.net.InetAddress networkAddress, int netmaskBits)
Create a new CIDR address.boolean
equals(java.lang.Object obj)
boolean
equals(CidrAddress obj)
java.net.Inet4Address
getBroadcastAddress()
Get the broadcast address for this CIDR block.int
getNetmaskBits()
Get the netmask bits.java.net.InetAddress
getNetworkAddress()
Get the network address.int
getScopeId()
Get the match address scope ID (if it is an IPv6 address).int
hashCode()
boolean
matches(byte[] bytes)
Determine if this CIDR address matches the given address bytes.boolean
matches(byte[] bytes, int scopeId)
Determine if this CIDR address matches the given address bytes.boolean
matches(java.net.Inet4Address address)
Determine if this CIDR address matches the given address.boolean
matches(java.net.Inet6Address address)
Determine if this CIDR address matches the given address.boolean
matches(java.net.InetAddress address)
Determine if this CIDR address matches the given address.boolean
matches(CidrAddress address)
Determine if this CIDR address matches the given CIDR address.java.lang.String
toString()
-
-
-
Field Detail
-
INET4_ANY_CIDR
public static final CidrAddress INET4_ANY_CIDR
The CIDR address representing all IPv4 addresses.
-
INET6_ANY_CIDR
public static final CidrAddress INET6_ANY_CIDR
The CIDR address representing all IPv6 addresses.
-
-
Method Detail
-
create
public static CidrAddress create(java.net.InetAddress networkAddress, int netmaskBits)
Create a new CIDR address.- Parameters:
networkAddress
- the network address (must not benull
)netmaskBits
- the netmask bits (0-32 for IPv4, or 0-128 for IPv6)- Returns:
- the CIDR address (not
null
)
-
create
public static CidrAddress create(byte[] addressBytes, int netmaskBits)
Create a new CIDR address.- Parameters:
addressBytes
- the network address bytes (must not benull
, must be 4 bytes for IPv4 or 16 bytes for IPv6)netmaskBits
- the netmask bits (0-32 for IPv4, or 0-128 for IPv6)- Returns:
- the CIDR address (not
null
)
-
matches
public boolean matches(java.net.InetAddress address)
Determine if this CIDR address matches the given address.- Parameters:
address
- the address to test- Returns:
true
if the address matches,false
otherwise
-
matches
public boolean matches(byte[] bytes)
Determine if this CIDR address matches the given address bytes.- Parameters:
bytes
- the address bytes to test- Returns:
true
if the address bytes match,false
otherwise
-
matches
public boolean matches(byte[] bytes, int scopeId)
Determine if this CIDR address matches the given address bytes.- Parameters:
bytes
- the address bytes to testscopeId
- the scope ID, or 0 to match no scope- Returns:
true
if the address bytes match,false
otherwise
-
matches
public boolean matches(java.net.Inet4Address address)
Determine if this CIDR address matches the given address.- Parameters:
address
- the address to test- Returns:
true
if the address matches,false
otherwise
-
matches
public boolean matches(java.net.Inet6Address address)
Determine if this CIDR address matches the given address.- Parameters:
address
- the address to test- Returns:
true
if the address matches,false
otherwise
-
matches
public boolean matches(CidrAddress address)
Determine if this CIDR address matches the given CIDR address. This will be true only when the given CIDR block is wholly enclosed by this one.- Parameters:
address
- the address to test- Returns:
true
if the given block is enclosed by this one,false
otherwise
-
getNetworkAddress
public java.net.InetAddress getNetworkAddress()
Get the network address. The returned address has a resolved name consisting of the most compact valid string representation of the network of this CIDR address.- Returns:
- the network address (not
null
)
-
getBroadcastAddress
public java.net.Inet4Address getBroadcastAddress()
Get the broadcast address for this CIDR block. If the block has no broadcast address (either because it is IPv6 or it is too small) thennull
is returned.- Returns:
- the broadcast address for this CIDR block, or
null
if there is none
-
getNetmaskBits
public int getNetmaskBits()
Get the netmask bits. This will be in the range 0-32 for IPv4 addresses, and 0-128 for IPv6 addresses.- Returns:
- the netmask bits
-
getScopeId
public int getScopeId()
Get the match address scope ID (if it is an IPv6 address).- Returns:
- the scope ID, or 0 if there is none or the address is an IPv4 address
-
compareTo
public int compareTo(CidrAddress other)
- Specified by:
compareTo
in interfacejava.lang.Comparable<CidrAddress>
-
compareAddressBytesTo
public int compareAddressBytesTo(byte[] otherBytes, int otherNetmaskBits, int scopeId)
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
equals
public boolean equals(CidrAddress obj)
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-