Class IPRange
java.lang.Object
net.shibboleth.utilities.java.support.net.IPRange
Represents a range of IP addresses.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionIPRange
(byte[] address, int maskSize) Constructor.IPRange
(InetAddress address, int maskSize) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionboolean
contains
(byte[] address) Determines whether the given address is contained in the IP range.boolean
contains
(InetAddress address) Determines whether the given address is contained in the IP range.Returns the host address originally specified for this range, if it was a host address rather than a network address.Returns the network address corresponding to this range as anInetAddress
.static IPRange
parseCIDRBlock
(String cidrBlock) Parses a CIDR block definition in to an IP range.protected BitSet
toBitSet
(byte[] bytes) Converts a byte array to a BitSet.private byte[]
toByteArray
(BitSet bits) private InetAddress
toInetAddress
(BitSet bits) Convert aBitSet
representing an address into an equivalentInetAddress
.private static void
validateIPAddress
(String address) Validate an IP address for use as the base of a CIDR block.private static void
validateV4Address
(String address) Validate an IPv4 address for use as the base of a CIDR block.private static void
validateV6Address
(String address) Validate an IPv6 address for use as the base of a CIDR block.
-
Field Details
-
addressLength
private final int addressLengthNumber of bits within the address. 32 bits for IPv4 address, 128 bits for IPv6 addresses. -
network
The IP network address for the range. -
host
The IP host address, if a host address rather than a network address was specified. -
mask
The netmask for the range.
-
-
Constructor Details
-
IPRange
Constructor.- Parameters:
address
- address to base the range on; may be the network address or the address of a host within the networkmaskSize
- the number of bits in the netmask- Throws:
IllegalArgumentException
- if the address or mask are invalid
-
IPRange
Constructor.- Parameters:
address
- address to base the range on; may be the network address or the address of a host within the networkmaskSize
- the number of bits in the netmask- Throws:
IllegalArgumentException
- if the address or mask are invalid
-
-
Method Details
-
getNetworkAddress
Returns the network address corresponding to this range as anInetAddress
.- Returns:
- network address as an
InetAddress
-
getHostAddress
Returns the host address originally specified for this range, if it was a host address rather than a network address. Returns null if the address specified was a network address.- Returns:
- host address as an
InetAddress
, or null
-
validateV4Address
private static void validateV4Address(@Nonnull @NotEmpty String address) throws IllegalArgumentException Validate an IPv4 address for use as the base of a CIDR block.- Parameters:
address
- the address to validate- Throws:
IllegalArgumentException
- if expression cannot be parsed
-
validateV6Address
private static void validateV6Address(@Nonnull @NotEmpty String address) throws IllegalArgumentException Validate an IPv6 address for use as the base of a CIDR block.Just checks that any non-empty components are valid hexadecimal integers in the right range; leaves most of the hard work to the
InetAddress
parser.- Parameters:
address
- the address to validate- Throws:
IllegalArgumentException
- if expression cannot be parsed
-
validateIPAddress
private static void validateIPAddress(@Nonnull @NotEmpty String address) throws IllegalArgumentException Validate an IP address for use as the base of a CIDR block.- Parameters:
address
- the address to validate- Throws:
IllegalArgumentException
- if expression cannot be parsed
-
parseCIDRBlock
@Nonnull public static IPRange parseCIDRBlock(@Nonnull @NotEmpty String cidrBlock) throws IllegalArgumentException Parses a CIDR block definition in to an IP range.- Parameters:
cidrBlock
- the CIDR block definition- Returns:
- the resultant IP range
- Throws:
IllegalArgumentException
- if expression cannot be parsed
-
contains
Determines whether the given address is contained in the IP range.- Parameters:
address
- the address to check- Returns:
- true if the address is in the range, false it not
-
contains
public boolean contains(@Nonnull byte[] address) Determines whether the given address is contained in the IP range.- Parameters:
address
- the address to check- Returns:
- true if the address is in the range, false it not
-
toBitSet
Converts a byte array to a BitSet. The supplied byte array is assumed to have the most significant bit in element 0.- Parameters:
bytes
- the byte array with most significant bit in element 0.- Returns:
- the BitSet
-
toByteArray
Convert aBitSet
representing an address into an equivalent array of bytes, sized according to the address length of thisIPRange
.- Parameters:
bits
-BitSet
representing an address- Returns:
- array of bytes representing the same address
-
toInetAddress
Convert aBitSet
representing an address into an equivalentInetAddress
.Returns null for either a null
BitSet
or for any problems encountered byInetAddress
.- Parameters:
bits
-BitSet
representing an address- Returns:
InetAddress
representing the same address
-