Class IPv4Util

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

public final class IPv4Util extends Object
Utility methods for working with IPv4 addresses and netmasks.
  • Field Details

    • MIN_VALID_NETMASK_WIDTH

      public static final int MIN_VALID_NETMASK_WIDTH
      See Also:
    • MAX_VALID_NETMASK_WIDTH

      public static final int MAX_VALID_NETMASK_WIDTH
      See Also:
  • Method Details

    • toAddress

      public static Inet4Address toAddress(int address)
      Convert from 32-bit integer representation.
      Parameters:
      address - IP address as a 32-bit integer
      Returns:
      the corresponding IP address
    • toInt32

      public static int toInt32(Inet4Address address)
      Convert to 32-bit integer representation.
      Parameters:
      address - IP address
      Returns:
      the corresponding 32-bit integer
    • toUInt32

      public static long toUInt32(Inet4Address address)
      Convert to an unsigned 32-bit integer representation within a 64-bit long.
      Parameters:
      address - IP address
      Returns:
      the corresponding unsigned 32-bit integer
    • toAddress

      public static Inet4Address toAddress(byte[] bytes)
      Convert raw bytes to an Inet4Address.
      Parameters:
      bytes - IP address bytes (big endian)
      Returns:
      corresponding IP address
      Throws:
      IllegalArgumentException - if array does not have length four
    • toString

      public static String toString(Inet4Address address)
      Convert an IPv4 address to string representation.
      Parameters:
      address - IP address
      Returns:
      address in String form
      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. Example: 192.168.1.39/24
      Parameters:
      address - IP address
      netmask - netmask for network
      Returns:
      address plus netmask width
      Throws:
      IllegalArgumentException - if array does not have length four
    • fromString

      public static Inet4Address fromString(String string)
      Convert from string representation.
      Parameters:
      string - IP address in string form
      Returns:
      parsed IP address
      Throws:
      IllegalArgumentException - if string is not an IP address
    • getWidth

      public static int getWidth(Inet4Address netmask)
      Get netmask width.
      Parameters:
      netmask - netmask
      Returns:
      netmask width in bits, 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).
      Parameters:
      netmask - netmask
      Returns:
      true if netmask is a valid netmask
    • getNetmaskForWidth

      public static Inet4Address getNetmaskForWidth(int width)
      Get netmask with the given width.
      Parameters:
      width - netmask width in bits
      Returns:
      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
      Returns:
      base address for the specified 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
      Returns:
      broadcast address for the specified 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
      Returns:
      true if address is on the specified network
      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
      Returns:
      true if address is valid on the specified network
      Throws:
      IllegalArgumentException - if netmask is not a valid netmask