Class 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.
    • Constructor Summary

      Constructors 
      Constructor Description
      NetworkTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static InetAddress findInterface​(byte[] subnetAddress, byte mask)
      Finds the IPv4 address of a network interface of this host that is in the designated subnet.
      static InetAddress 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 boolean inSubnet​(byte[] address, byte[] subnet, byte mask)
      Gets whether the designated IPv4 address is in the designated subnet (subnet address + mask).
      static boolean isLocalAddress​(InetAddress ipToCheck)
      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> parseIp4Address​(String address)
      Parses an IPv4 address from the designated string.
      static Pair<Inet4Address,​Byte> resolveIp4Address​(String address)
      Resolves an IPv4 address from the designated string.
      static byte[] toBytes​(int address)
      Deprecated.
      Do no longer represent an IP(v4) address as int but always as byte[].
      static int toInt​(byte[] address)
      Deprecated.
      Do no longer represent an IP(v4) address as int but always as byte[].
      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.
    • Field Detail

      • IP_V4_ADDRESS_PATTERN

        public static final String IP_V4_ADDRESS_PATTERN
        The regexp pattern for an IPv4 address.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NetworkTools

        public NetworkTools()
    • Method Detail

      • 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.
      • toBytes

        @Deprecated
        public static byte[] toBytes​(int address)
        Deprecated.
        Do no longer represent an IP(v4) address as int but always as byte[]. This also simplifies switching to IPv6.
        Converts the designated int representing an IPv4 address to an array of 4 bytes containing each address element separately.
        Parameters:
        address - The IPv4 address represented as int.
        Returns:
        4 bytes for the designated IPv4 address.
      • toInt

        @Deprecated
        public static int toInt​(byte[] address)
        Deprecated.
        Do no longer represent an IP(v4) address as int but always as byte[]. This also simplifies switching to IPv6.
        Converts the (4) designated bytes to an int. This is used to represent IPv4 addresses.
        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.