org.dellroad.stuff.net
Class IPv4Util

java.lang.Object
  extended by org.dellroad.stuff.net.IPv4Util

public final class IPv4Util
extends Object

Utility methods for working with IPv4 addresses and netmasks.


Field Summary
static int MAX_VALID_NETMASK_WIDTH
           
static int MIN_VALID_NETMASK_WIDTH
           
 
Method Summary
static Inet4Address fromString(String string)
          Convert from string representation.
static Inet4Address getBaseAddress(Inet4Address address, Inet4Address netmask)
          Get the base address (all host bits zero) of the given IP network.
static Inet4Address getBroadcastAddress(Inet4Address address, Inet4Address netmask)
          Get the broadcast address (all host bits one) of the given IP network.
static Inet4Address getNetmaskForWidth(int width)
          Get netmask with the given width.
static int getWidth(Inet4Address netmask)
          Get netmask width.
static boolean isOnNetwork(Inet4Address address, Inet4Address network, Inet4Address netmask)
          Determine if the given address lives on the given network.
static boolean isValidHostOnNetwork(Inet4Address address, Inet4Address network, Inet4Address netmask)
          Determine if the given address is a valid host address on the given network.
static boolean isValidNetmask(Inet4Address netmask)
          Determine if the given address is a valid CIDR netmask.
static Inet4Address toAddress(byte[] bytes)
          Convert raw bytes to an Inet4Address.
static Inet4Address toAddress(int address)
          Convert from 32-bit integer representation.
static int toInt32(Inet4Address address)
          Convert to 32-bit integer representation.
static String toString(Inet4Address address)
          Convert an IPv4 address to string representation.
static String toString(Inet4Address address, Inet4Address netmask)
          Convert a network address (IPv4 base address and netmask) to string representation.
static long toUInt32(Inet4Address address)
          Convert to an unsigned 32-bit integer representation within a 64-bit long.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_VALID_NETMASK_WIDTH

public static final int MIN_VALID_NETMASK_WIDTH
See Also:
Constant Field Values

MAX_VALID_NETMASK_WIDTH

public static final int MAX_VALID_NETMASK_WIDTH
See Also:
Constant Field Values
Method Detail

toAddress

public static Inet4Address toAddress(int address)
Convert from 32-bit integer representation.


toInt32

public static int toInt32(Inet4Address address)
Convert to 32-bit integer representation.


toUInt32

public static long toUInt32(Inet4Address address)
Convert to an unsigned 32-bit integer representation within a 64-bit long.


toAddress

public static Inet4Address toAddress(byte[] bytes)
Convert raw bytes to an Inet4Address.

Throws:
IllegalArgumentException - if array does not have length four

toString

public static String toString(Inet4Address address)
Convert an IPv4 address to string representation.

Throws:
IllegalArgumentException - if array does not have length four

toString

public static String toString(Inet4Address address,
                              Inet4Address netmask)
Convert a network address (IPv4 base address and netmask) to string representation.

Throws:
IllegalArgumentException - if array does not have length four

fromString

public static Inet4Address fromString(String string)
Convert from string representation.

Throws:
IllegalArgumentException - if string is not an IP address

getWidth

public static int getWidth(Inet4Address netmask)
Get netmask width.

Returns:
a value between zero and 32 (inclusive)
Throws:
IllegalArgumentException - if the value is not a valid CIDR netmask

isValidNetmask

public static boolean isValidNetmask(Inet4Address netmask)
Determine if the given address is a valid CIDR netmask. The netmask must have a width between 8 and 30 (inclusive).


getNetmaskForWidth

public static Inet4Address getNetmaskForWidth(int width)
Get netmask with the given width.

Throws:
IllegalArgumentException - if width is less than zero or greater than 32

getBaseAddress

public static Inet4Address getBaseAddress(Inet4Address address,
                                          Inet4Address netmask)
Get the base address (all host bits zero) of the given IP network.

Parameters:
address - any IP address on the network
netmask - the netmask of the network
Throws:
IllegalArgumentException - if netmask is not a valid netmask

getBroadcastAddress

public static Inet4Address getBroadcastAddress(Inet4Address address,
                                               Inet4Address netmask)
Get the broadcast address (all host bits one) of the given IP network.

Parameters:
address - any IP address on the network
netmask - the netmask of the network
Throws:
IllegalArgumentException - if netmask is not a valid netmask

isOnNetwork

public static boolean isOnNetwork(Inet4Address address,
                                  Inet4Address network,
                                  Inet4Address netmask)
Determine if the given address lives on the given network.

Parameters:
address - IP address in question
network - network address
netmask - network netmask
Throws:
IllegalArgumentException - if netmask is not a valid netmask

isValidHostOnNetwork

public static boolean isValidHostOnNetwork(Inet4Address address,
                                           Inet4Address network,
                                           Inet4Address netmask)
Determine if the given address is a valid host address on the given network. The address must lie on the given network and not equal either the base or broadcast addresses.

Parameters:
address - IP address in question
network - network address
netmask - network netmask
Throws:
IllegalArgumentException - if netmask is not a valid netmask