net.shibboleth.utilities.java.support.net
Class IPRange

java.lang.Object
  extended by net.shibboleth.utilities.java.support.net.IPRange

public class IPRange
extends Object

Represents a range of IP addresses.


Field Summary
private  int addressLength
          Number of bits within the address.
private  BitSet host
          The IP host address, if a host address rather than a network address was specified.
private  BitSet mask
          The netmask for the range.
private  BitSet network
          The IP network address for the range.
 
Constructor Summary
IPRange(byte[] address, int maskSize)
          Constructor.
IPRange(InetAddress address, int maskSize)
          Constructor.
 
Method Summary
 boolean 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.
 InetAddress getHostAddress()
          Returns the host address originally specified for this range, if it was a host address rather than a network address.
 InetAddress getNetworkAddress()
          Returns the network address corresponding to this range as an InetAddress.
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)
          Convert a BitSet representing an address into an equivalent array of bytes, sized according to the address length of this IPRange.
private  InetAddress toInetAddress(BitSet bits)
          Convert a BitSet representing an address into an equivalent InetAddress.
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

addressLength

private final int addressLength
Number of bits within the address. 32 bits for IPv4 address, 128 bits for IPv6 addresses.


network

private final BitSet network
The IP network address for the range.


host

private final BitSet host
The IP host address, if a host address rather than a network address was specified.


mask

private BitSet mask
The netmask for the range.

Constructor Detail

IPRange

public IPRange(InetAddress address,
               int maskSize)
Constructor.

Parameters:
address - address to base the range on; may be the network address or the address of a host within the network
maskSize - the number of bits in the netmask

IPRange

public IPRange(byte[] address,
               int maskSize)
Constructor.

Parameters:
address - address to base the range on; may be the network address or the address of a host within the network
maskSize - the number of bits in the netmask
Method Detail

getNetworkAddress

public InetAddress getNetworkAddress()
Returns the network address corresponding to this range as an InetAddress.

Returns:
network address as an InetAddress

getHostAddress

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(String address)
Validate an IPv4 address for use as the base of a CIDR block. Throws IllegalArgumentException if validation fails.

Parameters:
address - the address to validate

validateV6Address

private static void validateV6Address(String address)
Validate an IPv6 address for use as the base of a CIDR block. Just check that any non-empty components are valid hexadecimal integers in the right range; leave most of the hard work to the InetAddress parser. Throws IllegalArgumentException if validation fails.

Parameters:
address - the address to validate

validateIPAddress

private static void validateIPAddress(String address)
Validate an IP address for use as the base of a CIDR block. Throws IllegalArgumentException if validation fails.

Parameters:
address - the address to validate

parseCIDRBlock

public static IPRange parseCIDRBlock(String cidrBlock)
Parses a CIDR block definition in to an IP range.

Parameters:
cidrBlock - the CIDR block definition
Returns:
the resultant IP range

contains

public boolean contains(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(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

protected BitSet toBitSet(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

private byte[] toByteArray(BitSet bits)
Convert a BitSet representing an address into an equivalent array of bytes, sized according to the address length of this IPRange.

Parameters:
bits - BitSet representing an address
Returns:
array of bytes representing the same address

toInetAddress

private InetAddress toInetAddress(BitSet bits)
Convert a BitSet representing an address into an equivalent InetAddress. Returns null for either a null BitSet or for any problems encountered by InetAddress.

Parameters:
bits - BitSet representing an address
Returns:
InetAddress representing the same address


Copyright © 1999-2012. All Rights Reserved.