SSF.OS.BGP4.Util
Class IPaddress

java.lang.Object
  |
  +--SSF.OS.BGP4.Util.IPaddress

public class IPaddress
extends java.lang.Object

Each instance of this class represents an IPv4 address.


Field Summary
static long MAX_IP
          The integer value of the "maximum" IPv4 address: 255.255.255.255.
 
Constructor Summary
IPaddress()
          Default constructor: constructs the IP address 1.1.1.1/32.
IPaddress(boolean[] bin)
          Constructs an IP address using a boolean array.
IPaddress(byte[] bytes)
          Constructs an IP address given an array of five bytes.
IPaddress(int v)
          Constructs an IP address using an integer as the address's value.
IPaddress(int v, int pre)
          Constructs an IP address with the given integer value and prefix length.
IPaddress(IPaddress ipa)
          Copy constructor.
IPaddress(long v)
          Constructs an IP address using a (long) integer as the address's value.
IPaddress(long v, int pre)
          Constructs an IP address with the given (long) integer value and prefix length.
IPaddress(java.lang.String str)
          Constructs an IP address using a string.
 
Method Summary
static java.lang.String bin2str(boolean[] b)
          Converts an IP address from boolean array format to string format.
 boolean[] binval()
          Returns the IP address in boolean array format, where the size of the array returned is the same as the prefix length.
 boolean[] binval(boolean fullsize)
          Returns the IP address in boolean array format.
 int bit(int bitnum)
          Returns the value of a single bit in the IP address.
 BitString bits()
          Returns the bits in the IP address as a BitString of length 32 (prefix length is ignored).
 byte[] bytes()
          Returns the IP address as an array of five bytes.
static java.lang.String bytes2str(byte[] bytes)
          Converts an array of five bytes into an IP address in string form.
 boolean equals(java.lang.Object ipaddr)
          Determines whether two IP addresses are equal.
 IPaddress get_incr()
          Creates a new IP address with the same value as this one, then increments the value of that IP address by one and returns it.
 IPaddress get_incr(int block)
          Creates a new IP address with the same value as this one, then increments the value of that IP address by the given amount and returns it.
 int hashCode()
          Returns a hash code value which can be used if an IP address is used as a key in a hash table.
 void incr()
          Increment this IP address by 1.
 void incr(int block)
          Increment this IP address by the given integer value.
 int intval()
          Returns the value of IP address just as in val(), but returns it as an integer, if possible, instead of a long integer.
 boolean is_prefix_of(IPaddress ipa)
          Determines whether this IP address is a proper prefix of a given IP address.
 BitString masked_bits()
          Returns the bits in the IP address, after being masked, as a BitString of length 32.
 int masked_intval()
          Returns the masked value of the IP address just as in masked_val(), but returns it as a plain integer if possible.
 int masked_intval(int len)
          Returns the masked value of the IP address just as in masked_val(int), but returns it as a plain integer if possible.
 long masked_val()
          Returns the value of the IP address when the bits beyond the prefix length are masked out (taken as zeroes).
 long masked_val(int len)
          Returns the value of the IP address when the bits beyond a given point are masked out (taken as zeroes).
 BitString prefix_bits()
          Returns only the prefix bits in the IP address as a BitString of length prefix_len.
 int prefix_len()
          Returns the prefix length.
 void print_binary()
          Prints the binary value of this IP address, ignoring prefix length.
 boolean same_prefix(IPaddress ipaddr)
          Returns whether the given IP address prefix, when masked, is the same as this IP address prefix when masked.
 void set_prefix_len(int pre)
          Set the prefix length to the given value.
 void set_val(int v)
          Set the value of the IP address using an integer.
 void set_val(long v)
          Set the value of the IP address using a long integer.
 void set(IPaddress ip)
          Set the value of the IP address based on another IP address.
static boolean[] str2bin(java.lang.String str)
          Converts an IP address from string format to boolean array format.
static int str2int(java.lang.String str)
          Converts an IP address from string format to integer format.
static long str2long(java.lang.String str)
          Converts an IP address from string format to long integer format.
 java.lang.String toString()
          Returns a string representing this IP address in dotted-quad (a.b.c.d/p) notation, including the prefix length.
 java.lang.String toString(boolean usenhi)
          Converts an IP address object to a string.
 long val()
          Returns the value of IP address when taken as a 32-bit number (prefix length ignored).
 java.lang.String val2str()
          Returns a string representing this IP address in dotted-quad (a.b.c.d) notation, without prefix length.
 boolean valequals(IPaddress ipaddr)
          Determines whether two IP addresses have equal values.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_IP

public static final long MAX_IP
The integer value of the "maximum" IPv4 address: 255.255.255.255.

See Also:
Constant Field Values
Constructor Detail

IPaddress

public IPaddress()
Default constructor: constructs the IP address 1.1.1.1/32. (Primarily for debugging use.)


IPaddress

public IPaddress(IPaddress ipa)
Copy constructor. All data used to initialize the IP address is copied from the given IP address.

Parameters:
ipa - The IP address to copy.

IPaddress

public IPaddress(int v)
Constructs an IP address using an integer as the address's value. The default of 32 is used for the prefix length.

Parameters:
v - The integer value of the IP address.

IPaddress

public IPaddress(long v)
Constructs an IP address using a (long) integer as the address's value. The default of 32 is used for the prefix length.

Parameters:
v - The (long) integer value of the IP address.

IPaddress

public IPaddress(int v,
                 int pre)
Constructs an IP address with the given integer value and prefix length.

Parameters:
v - The integer value of the IP address.
pre - The prefix length of the IP address.

IPaddress

public IPaddress(long v,
                 int pre)
Constructs an IP address with the given (long) integer value and prefix length.

Parameters:
v - The (long) integer value of the IP address.
pre - The prefix length of the IP address.

IPaddress

public IPaddress(byte[] bytes)
Constructs an IP address given an array of five bytes. The first four bytes represent the four bytes of the address, and the fifth contains the value of the prefix length.

Parameters:
bytes - An array of bytes to be converted into an IP address.

IPaddress

public IPaddress(boolean[] bin)
Constructs an IP address using a boolean array. Each boolean in the array represents a bit in the address. A value of 'true' indicates a 1 and 'false' indicates a 0. The last element in the boolean array represents the highest order (first, leftmost) bit in the IP address. The length of the boolean array indicates the prefix length of the IP address. Element 0 in the boolean array represents the lowest-order bit of the IP address that is not beyond the prefix length. All bits beyond the prefix length are set to 0.

Parameters:
bin - An array of booleans which represents an IP address.

IPaddress

public IPaddress(java.lang.String str)
Constructs an IP address using a string. The string must represent an IP address either in dotted-quad notation (a.b.c.d), dotted-quad plus prefix notation (a.b.c.d/p), or binary notation (for example, 01100010010). There may optionally be double-quotes (") surrounding the notation. If no prefix is provided in dotted-quad notation, a value of 32 is used. In binary notation, the prefix length is the length of the string, and the leftmost character represents the most significant bit.

Parameters:
str - The string representing an IP address.
Method Detail

bytes2str

public static java.lang.String bytes2str(byte[] bytes)
Converts an array of five bytes into an IP address in string form.

Parameters:
bytes - An array of bytes to be converted into an IP address.
Returns:
an IP address in string form

bin2str

public static final java.lang.String bin2str(boolean[] b)
Converts an IP address from boolean array format to string format. See constructor IPaddress(boolean[]) for an explanation of the boolean array format.

Parameters:
b - A boolean array representing an IP address.
Returns:
an IP address in String format
See Also:
IPaddress(boolean[])

str2bin

public static final boolean[] str2bin(java.lang.String str)
Converts an IP address from string format to boolean array format.

Parameters:
str - A string representing the IP address to be converted.
Returns:
an IP address in boolean array format

bit

public final int bit(int bitnum)
Returns the value of a single bit in the IP address. Bits are indexed from 1 to 32. Bit 1 is the most significant (leftmost) and bit 32 is the least significant (rightmost).

Parameters:
bitnum - The number of the bit to return.
Returns:
the value of the bit in question

bits

public final BitString bits()
Returns the bits in the IP address as a BitString of length 32 (prefix length is ignored).

Returns:
an IP address in the form of a BitString object

masked_bits

public final BitString masked_bits()
Returns the bits in the IP address, after being masked, as a BitString of length 32.

Returns:
an IP address in the form of a BitString object

prefix_bits

public final BitString prefix_bits()
Returns only the prefix bits in the IP address as a BitString of length prefix_len.

Returns:
a BitString object representing the prefix bits of an IP address

bytes

public final byte[] bytes()
Returns the IP address as an array of five bytes. The first four bytes cointain the values as used in traditional dotted-quad notation. The final bytes is the value of the prefix length. Note that because a Java byte is signed, care must be taken in translating from the byte array back into dotted-quad notation.

Returns:
an IP address as an array of five bytes

binval

public final boolean[] binval()
Returns the IP address in boolean array format, where the size of the array returned is the same as the prefix length.

Returns:
an IP address in boolean array format

binval

public final boolean[] binval(boolean fullsize)
Returns the IP address in boolean array format. The fullsize parameter determines whether or not the array length is 32 or prefix_len.

Parameters:
fullsize - Whether or not to pad the array with zeroes (falses) up to 32 if the prefix length is less than 32.
Returns:
an IP address in boolean array format

val

public final long val()
Returns the value of IP address when taken as a 32-bit number (prefix length ignored).

Returns:
the value of the IP address as a 32-bit integer

intval

public final int intval()
Returns the value of IP address just as in val(), but returns it as an integer, if possible, instead of a long integer. Some IP address values may be too big to fit in a regular integer. This method exists because in many cases it's easier to deal with an int than a long.

Returns:
the value of the IP address as a 32-bit integer

str2int

public static final int str2int(java.lang.String str)
Converts an IP address from string format to integer format.

Parameters:
str - A string representing an IP address.
Returns:
an IP address in integer form

str2long

public static final long str2long(java.lang.String str)
Converts an IP address from string format to long integer format.

Parameters:
str - A string representing an IP address.
Returns:
an IP address in long integer form

set_val

public final void set_val(int v)
Set the value of the IP address using an integer.

Parameters:
v - An integer to use as the value of the IP address.

set_val

public final void set_val(long v)
Set the value of the IP address using a long integer.

Parameters:
v - A long integer to use as the value of the IP address.

set

public final void set(IPaddress ip)
Set the value of the IP address based on another IP address.

Parameters:
ip - An IP address to use as the value for this IP address.

set_prefix_len

public final void set_prefix_len(int pre)
Set the prefix length to the given value.

Parameters:
pre - The value to use as the new prefix length.

prefix_len

public final int prefix_len()
Returns the prefix length.

Returns:
the prefix length

get_incr

public final IPaddress get_incr()
Creates a new IP address with the same value as this one, then increments the value of that IP address by one and returns it.

Returns:
a new IP address with the incremented value

get_incr

public final IPaddress get_incr(int block)
Creates a new IP address with the same value as this one, then increments the value of that IP address by the given amount and returns it.

Parameters:
block - The amount by which to increment the IP address value.
Returns:
a new IP address with the incremented value

incr

public final void incr()
Increment this IP address by 1.


incr

public final void incr(int block)
Increment this IP address by the given integer value. If the given value is negative, it decrements.

Parameters:
block - The amount by which to increment the IP address value.

valequals

public final boolean valequals(IPaddress ipaddr)
Determines whether two IP addresses have equal values. (Whether or not their prefix lengths are the same is ignored.)

Parameters:
ipaddr - The IP address with which to make the comparison.
Returns:
true only if the two values are the same, false otherwise.

equals

public boolean equals(java.lang.Object ipaddr)
Determines whether two IP addresses are equal. They are equal only if both the values and prefix lengths of each are the same.

Overrides:
equals in class java.lang.Object
Parameters:
ipaddr - The IP address with which to make the comparison.
Returns:
true only if the two are identical, false otherwise.

same_prefix

public final boolean same_prefix(IPaddress ipaddr)
Returns whether the given IP address prefix, when masked, is the same as this IP address prefix when masked.

Parameters:
ipaddr - An IP address prefix for comparison.
Returns:
the result of the comparison

masked_val

public final long masked_val(int len)
Returns the value of the IP address when the bits beyond a given point are masked out (taken as zeroes).

Parameters:
len - The length in bits beyond which to mask the address.
Returns:
the masked value of the IP address

masked_val

public final long masked_val()
Returns the value of the IP address when the bits beyond the prefix length are masked out (taken as zeroes).

Returns:
the masked value of the IP address

masked_intval

public final int masked_intval(int len)
Returns the masked value of the IP address just as in masked_val(int), but returns it as a plain integer if possible. Some IP address values may be too big to fit in an plain integer. This method exists because in many cases it's easier to deal with a plain integer than a long integer.

Parameters:
len - The length in bits beyond which to mask the address.
Returns:
the masked value of the IP address in integer form

masked_intval

public final int masked_intval()
Returns the masked value of the IP address just as in masked_val(), but returns it as a plain integer if possible. Some IP address values may be too big to fit in an plain integer. This method exists because in many cases it's easier to deal with a plain integer than a long integer.

Returns:
the masked value of the IP address in integer form

print_binary

public final void print_binary()
Prints the binary value of this IP address, ignoring prefix length.


val2str

public final java.lang.String val2str()
Returns a string representing this IP address in dotted-quad (a.b.c.d) notation, without prefix length.

Returns:
a string in dotted-quad notation representing this IP address

is_prefix_of

public final boolean is_prefix_of(IPaddress ipa)
Determines whether this IP address is a proper prefix of a given IP address. Note that since it must be a proper prefix, if they are identical the result will be false.

Returns:
true only if it is a proper prefix

hashCode

public int hashCode()
Returns a hash code value which can be used if an IP address is used as a key in a hash table.

Overrides:
hashCode in class java.lang.Object
Returns:
an integer hash code value

toString

public java.lang.String toString(boolean usenhi)
Converts an IP address object to a string. The string may be in IP address or NHI address form.

Parameters:
usenhi - Whether to use the NHI or traditional address format.
Returns:
the address as a string

toString

public final java.lang.String toString()
Returns a string representing this IP address in dotted-quad (a.b.c.d/p) notation, including the prefix length.

Overrides:
toString in class java.lang.Object
Returns:
a string in dotted-quad notation representing this IP address