Class IPRange
- java.lang.Object
-
- net.shibboleth.utilities.java.support.net.IPRange
-
public class IPRange extends Object
Represents a range of IP addresses.
-
-
Field Summary
Fields Modifier and Type Field Description private intaddressLengthNumber of bits within the address.private BitSethostThe IP host address, if a host address rather than a network address was specified.private BitSetmaskThe netmask for the range.private BitSetnetworkThe IP network address for the range.
-
Constructor Summary
Constructors Constructor Description IPRange(byte[] address, int maskSize)Constructor.IPRange(InetAddress address, int maskSize)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(byte[] address)Determines whether the given address is contained in the IP range.booleancontains(InetAddress address)Determines whether the given address is contained in the IP range.InetAddressgetHostAddress()Returns the host address originally specified for this range, if it was a host address rather than a network address.InetAddressgetNetworkAddress()Returns the network address corresponding to this range as anInetAddress.static IPRangeparseCIDRBlock(String cidrBlock)Parses a CIDR block definition in to an IP range.protected BitSettoBitSet(byte[] bytes)Converts a byte array to a BitSet.private byte[]toByteArray(BitSet bits)private InetAddresstoInetAddress(BitSet bits)Convert aBitSetrepresenting an address into an equivalentInetAddress.private static voidvalidateIPAddress(String address)Validate an IP address for use as the base of a CIDR block.private static voidvalidateV4Address(String address)Validate an IPv4 address for use as the base of a CIDR block.private static voidvalidateV6Address(String address)Validate an IPv6 address for use as the base of a CIDR block.
-
-
-
Field Detail
-
addressLength
private final int addressLength
Number of bits within the address. 32 bits for IPv4 address, 128 bits for IPv6 addresses.
-
network
@Nonnull private final BitSet network
The IP network address for the range.
-
host
@Nullable private final BitSet host
The IP host address, if a host address rather than a network address was specified.
-
mask
@Nonnull private BitSet mask
The netmask for the range.
-
-
Constructor Detail
-
IPRange
public IPRange(@Nonnull InetAddress address, int maskSize) throws IllegalArgumentExceptionConstructor.- 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
public IPRange(@Nonnull byte[] address, int maskSize) throws IllegalArgumentExceptionConstructor.- 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 Detail
-
getNetworkAddress
@Nullable public InetAddress getNetworkAddress()
Returns the network address corresponding to this range as anInetAddress.- Returns:
- network address as an
InetAddress
-
getHostAddress
@Nullable public 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
InetAddressparser.- 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
public boolean contains(@Nonnull InetAddress 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
-
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
@Nonnull protected BitSet toBitSet(@Nonnull byte[] bytes)
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
@Nonnull private byte[] toByteArray(@Nonnull BitSet bits)Convert aBitSetrepresenting an address into an equivalent array of bytes, sized according to the address length of thisIPRange.- Parameters:
bits-BitSetrepresenting an address- Returns:
- array of bytes representing the same address
-
toInetAddress
@Nullable private InetAddress toInetAddress(@Nullable BitSet bits)
Convert aBitSetrepresenting an address into an equivalentInetAddress.Returns null for either a null
BitSetor for any problems encountered byInetAddress.- Parameters:
bits-BitSetrepresenting an address- Returns:
InetAddressrepresenting the same address
-
-