org.glite.security.util
Class IPAddressComparator

java.lang.Object
  extended by org.glite.security.util.IPAddressComparator

public class IPAddressComparator
extends java.lang.Object

An utility class used to compare ip addresses and to check whether an IP address is within a address space defined by IP address - netmask combination.

Author:
Joni Hahkala

Constructor Summary
IPAddressComparator()
           
 
Method Summary
static byte[] andBytes(byte[] ip, byte[] netmask)
          This method does bitwise and between the two byte arrays.
static boolean compare(byte[] item1, byte[] item2)
          Compares two byte arrays.
static byte[][] concatArrayArrays(byte[][] first, byte[][] second)
          Concatenates two arrays of arrays bytes.
static byte[] copyBytes(byte[] array, int start, int end)
          Copies the items from the array to a new array starting from index start and ending at end - 1.
static boolean isWithinAddressSpace(byte[] ipAddress, byte[] ipAddressWithNetmask)
          Tests whether the ipAddress is within the address space defined by the ipAddressWithNetmask.
static byte[] parseIP(java.lang.String ip)
          Parses the string representation of the IP address and returns the address as a byte array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IPAddressComparator

public IPAddressComparator()
Method Detail

parseIP

public static byte[] parseIP(java.lang.String ip)
Parses the string representation of the IP address and returns the address as a byte array. The methods returns bytes of the IP address, 4 bytes for IPv4 address, 16 for the IPv6 address, 5 for IPv4 address with netmask and 17 for the IPv6 address with netmask. example 137.138.125.111/24 would return bytes {137, 138, 125, 111, 24}. So far only the slash-int way of defining the netmask is supported.

Parameters:
ip - The IP address with optional netmask.
Returns:
see above for explanation of the return value.

compare

public static boolean compare(byte[] item1,
                              byte[] item2)
Compares two byte arrays. Can be used to compare two IP addresses or two IP address - netmask combinations.

Parameters:
item1 - The first array to use for comparison.
item2 - The second array to use for comparison.
Returns:
true if the array lengths and the items match.

isWithinAddressSpace

public static boolean isWithinAddressSpace(byte[] ipAddress,
                                           byte[] ipAddressWithNetmask)
Tests whether the ipAddress is within the address space defined by the ipAddressWithNetmask.

Parameters:
ipAddress - The IP address bytes to compare against the address space.
ipAddressWithNetmask - The 8 (IPv4) or 32 (IPv6) byte array containing in the first half the base IP address bytes and in the second half the netmask bytes.
Returns:
true if

andBytes

public static byte[] andBytes(byte[] ip,
                              byte[] netmask)
This method does bitwise and between the two byte arrays. The arrays have to have the same size.

Parameters:
ip - The first array to use for the and operation.
netmask - The second array to use for the and operation.
Returns:
The resulting byte array containing the bytes after the bitwise and operation.

copyBytes

public static byte[] copyBytes(byte[] array,
                               int start,
                               int end)
Copies the items from the array to a new array starting from index start and ending at end - 1.

Parameters:
array - The array holding the bytes to copy.
start - The index where to start copying, inclusive.
end - The index to stop copying, exclusive. The last item copied has index end - 1.
Returns:
The newly created array containing the copied bytes.

concatArrayArrays

public static byte[][] concatArrayArrays(byte[][] first,
                                         byte[][] second)
Concatenates two arrays of arrays bytes.

Parameters:
first - The array of arrays to begin with.
second - The array of arrays to end with.
Returns:
the array of arrays that contains the arrays from both argument arrays.