Class NetworkTools

java.lang.Object
de.aristaflow.adept2.util.net.NetworkTools

public class NetworkTools extends Object
This class provides tooling related to networking, especially handling of IP addresses with trailing subnet masks in CIDR notation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The regexp pattern for an IPv4 address.
    protected static final Random
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    findInterface(byte[] subnetAddress, byte mask)
    Finds the IPv4 address of a network interface of this host that is in the designated subnet.
    findInterface(InetAddress subnetAddress, byte mask)
    Finds the IPv4 address of a network interface of this host that is in the designated subnet.
    static String
    getIp4AddressString(byte[] address, byte mask)
    Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).
    static String
    getIp4AddressString(Inet4Address address, byte mask)
    Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).
    protected static int
    getNetworkPrefix(byte[] address, byte mask)
    Gets the network prefix of the designated IPv4 address using the designated mask (CIDR notation).
    static String
    Determines and returns a unique name for the current platform/cluster node.
    static boolean
    inSubnet(byte[] address, byte[] subnet, byte mask)
    Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask).
    static boolean
    Checks if the specified IPv4 address is one of that addresses which are assigned to this host or if it is an address assigned to a remote host.
    static Pair<byte[],Byte>
    Parses an IPv4 address from the designated string.
    Resolves an IPv4 address from the designated string.
    protected static int
    toInt(byte[] address)
    Converts the (4) designated bytes to an int.
    protected static int
    toIntMask(byte mask)
    Converts the designated mask (i.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • NetworkTools

      public NetworkTools()
  • Method Details

    • getIp4AddressString

      public static String getIp4AddressString(Inet4Address address, byte mask)
      Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).
      Parameters:
      address - The IP address as byte[] for which to get a string representation.
      mask - The subnet mask for the string representation if (0 < mask <= 32).
      Returns:
      A string representation for the designated IP (v4) address including an optional subnet mask.
    • getIp4AddressString

      public static String getIp4AddressString(byte[] address, byte mask)
      Gets a string representation for the designated IP (v4) address including a subnet mask if valid (0 < mask <= 32).
      Parameters:
      address - The IP address as byte[] for which to get a string representation.
      mask - The subnet mask for the string representation if (0 < mask <= 32).
      Returns:
      A string representation for the designated IP (v4) address including an optional subnet mask.
    • parseIp4Address

      public static Pair<byte[],Byte> parseIp4Address(String address)
      Parses an IPv4 address from the designated string. This may include a subnet mask in CIDR notation.
      Parameters:
      address - The IPv4 address/subnet which to parse.
      Returns:
      The parsed IPv4 address and the subnet mask. If no subnet mask is provided, 32 will be returned as subnet mask to represent a single IP address.
      Throws:
      IllegalArgumentException - If the designated address is not a valid IPv4 address (optionally including a subnet mask in CIDR notation), an IllegalArgumentException will be thrown.
    • resolveIp4Address

      public static Pair<Inet4Address,Byte> resolveIp4Address(String address) throws UnknownHostException
      Resolves an IPv4 address from the designated string. This may include a subnet mask in CIDR notation.
      Parameters:
      address - The IPv4 address/subnet which to parse and resolve.
      Returns:
      The parsed IPv4 address and the subnet mask. If no subnet mask is provided, 32 will be returned as subnet mask to represent a single IP address.
      Throws:
      UnknownHostException - If the designated address cannot be resolved to a corresponding host, an UnknownHostException will be thrown.
    • toInt

      protected static int toInt(byte[] address)
      Converts the (4) designated bytes to an int. This is used to represent IPv4 addresses, mostly as subnets.
      Parameters:
      address - The IPv4 address represented as 4 bytes.
      Returns:
      An int representing the designated 4 bytes.
    • toIntMask

      protected static int toIntMask(byte mask)
      Converts the designated mask (i. e. a number from CIDR notation) to an int simply containing the designated amount of consecutive 1s starting on the highest bit.
      Parameters:
      mask - The subnet mask from CIDR notation, which is a number between 0 and 32.
      Returns:
      An int having the designated amount of consecutive 1s.
    • getNetworkPrefix

      protected static int getNetworkPrefix(byte[] address, byte mask)
      Gets the network prefix of the designated IPv4 address using the designated mask (CIDR notation).
      Parameters:
      address - The IP address for which to get the network prefix.
      mask - The subnet mask from CIDR notation, which is a number between 0 and 32.
      Returns:
      The network prefix for the designated IPv4 address using the designated mask.
    • inSubnet

      public static boolean inSubnet(byte[] address, byte[] subnet, byte mask)
      Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask). This just returns whether the network prefix of address and subnet are equal with respect to mask.
      Parameters:
      address - The IPv4 address which to check for whether it is in the designated subnet.
      subnet - The address of the subnet.
      mask - The subnet mask from CIDR notation, which is a number between 0 and 32.
      Returns:
      Whether the designated IPv4 address is in the designated subnet (subnet address + mask).
    • findInterface

      public static InetAddress findInterface(InetAddress subnetAddress, byte mask) throws SocketException
      Finds the IPv4 address of a network interface of this host that is in the designated subnet. If no corresponding network interface is found, null will be returned.
      Parameters:
      subnetAddress - The subnet address.
      mask - The subnet mask from CIDR notation, which is a number between 0 and 32.
      Returns:
      The IPv4 address of a network interface of this host that is in the designated subnet or null if no corresponding network interface is found.
      Throws:
      SocketException - If an I/O error occurs when retrieving all network interfaces of this host, a SocketException will be thrown.
    • findInterface

      public static InetAddress findInterface(byte[] subnetAddress, byte mask) throws SocketException
      Finds the IPv4 address of a network interface of this host that is in the designated subnet. If no corresponding network interface is found, null will be returned.
      Parameters:
      subnetAddress - The subnet address.
      mask - The subnet mask from CIDR notation, which is a number between 0 and 32.
      Returns:
      The IPv4 address of a network interface of this host that is in the designated subnet or null if no corresponding network interface is found.
      Throws:
      SocketException - If an I/O error occurs when retrieving all network interfaces of this host, a SocketException will be thrown.
    • isLocalAddress

      public static boolean isLocalAddress(InetAddress ipToCheck) throws SocketException
      Checks if the specified IPv4 address is one of that addresses which are assigned to this host or if it is an address assigned to a remote host.
      Parameters:
      ipToCheck - The IP address to check.
      Returns:
      Whether the designated IPv4 address belongs to this host (or a remote host).
      Throws:
      SocketException - If an I/O error occurs when retrieving all network interfaces of this host, a SocketException will be thrown.
    • getUniqueNodeName

      public static String getUniqueNodeName(Logger logger)
      Determines and returns a unique name for the current platform/cluster node. This is usually the name of the local host with a trailing random ID. If the host name cannot be determined, a random UUID will be used.
      Parameters:
      logger - The logger where to log information messages on creating the node name.
      Returns:
      A unique node name based on the local host name or a random UUID.