Class NetworkUtil


  • public final class NetworkUtil
    extends Object
    Utility class for network-related operations.
    • Field Detail

      • MIN_PORT_NUMBER

        public static final int MIN_PORT_NUMBER
        The minimum server port number.
        See Also:
        Constant Field Values
      • MAX_PORT_NUMBER

        public static final int MAX_PORT_NUMBER
        The maximum server port number.
        See Also:
        Constant Field Values
      • LOCAL_ADDRESS_FOR_REMOTE_TIMEOUT

        public static final Duration LOCAL_ADDRESS_FOR_REMOTE_TIMEOUT
      • EXTERNAL_IP_ADDRESS_TIMEOUT

        public static final Duration EXTERNAL_IP_ADDRESS_TIMEOUT
    • Method Detail

      • getExternalIPv4Address

        public static Inet4Address getExternalIPv4Address()
        Determines the external IPv4 address.

        Note: This is a blocking method, because it connects to external server that may react slowly or not at all.

        Returns:
        the external IPv4 address or null in case of error
      • getExternalIPv6Address

        public static Inet6Address getExternalIPv6Address()
        Determines the external IPv6 address.

        Note: This is a blocking method, because it connects to external server that may react slowly or not at all.

        Returns:
        the external IPv6 address or null in case of error
      • available

        public static boolean available​(int port)
        Checks to see if a specific port is available.

        Source: Apache camel

        Parameters:
        port - the port number to check for availability
        Returns:
        true if the port is available, or false if not
        Throws:
        IllegalArgumentException - is thrown if the port number is out of range
      • isValidPort

        public static boolean isValidPort​(int port)
        Checks if a port is valid or not.
        Parameters:
        port - port that should be validated.
        Returns:
        true if valid, otherwise false
      • alive

        public static boolean alive​(String host,
                                    int port)
        Checks to see if a specific host:port is available.
        Parameters:
        host - host name or IP address to check for availability
        port - the port number to check for availability
        Returns:
        true if the host:port is available, or false if not
        Throws:
        IllegalArgumentException - is thrown if the port number is out of range
      • getAddresses

        public static Set<InetAddress> getAddresses()
        Returns a list of the IP addresses of all network interfaces of the local computer. If no IP addresses can be obtained, the loopback address is returned.
        Returns:
        list of IP addresses of all network interfaces of local computer or loopback address if no address can be obtained
      • isValidNonSpecialIPAddress

        public static boolean isValidNonSpecialIPAddress​(InetAddress address)
        Check if the given address is a normal IP address that is neither a loopback, link local nor multicast address.
        Parameters:
        address - the address that should be checked
        Returns:
        true if the address is a normal IP address, false otherwise
      • getLocalHostName

        public static String getLocalHostName()
        Returns the local host name. If no host name can be determined, null is returned.
        Returns:
        the local host name. If no host name can be determined, null is returned.
      • createInetAddress

        public static InetAddress createInetAddress​(String str)
        Creates a InetAddress by parsing the given string.

        This convenience factory method works as if by invoking the InetAddress.getByName(java.lang.String) constructor; any UnknownHostException thrown by the constructor is caught and wrapped in a new IllegalArgumentException object, which is then thrown.

        This method is provided for use in situations where it is known that the given string is a legal InetAddress, for example for InetAddress constants declared within a program, and so it would be considered a programming error for the string not to parse as such. The constructors, which throw UnknownHostException directly, should be used in situations where a InetAddress is being constructed from user input or from some other source that may be prone to errors.

        Parameters:
        str - The string to be parsed into a InetAddress
        Returns:
        The new InetAddress
        Throws:
        IllegalArgumentException - if no IP address for the str could be found, or if a scope_id was specified for a global IPv6 address.
      • getNetworkPrefixLength

        public static short getNetworkPrefixLength​(InetAddress address)
        Returns the network prefix length for given address. This is also known as the subnet mask in the context of IPv4 addresses. Typical IPv4 values would be 8 (255.0.0.0), 16 (255.255.0.0) or 24 (255.255.255.0).

        Typical IPv6 values would be 128 (::1/128) or 10 (fe80::203:baff:fe27:1243/10)

        Parameters:
        address - The InetAddress to search with.
        Returns:
        a short representing the prefix length for the subnet of that address or -1 if there is no network interface with given IP address.
        Throws:
        NullPointerException - If the specified address is null.
      • sameNetwork

        public static boolean sameNetwork​(InetAddress a,
                                          InetAddress b,
                                          short mask)
        Checks if two given addresses are in the same network.
        Parameters:
        a - first address
        b - second address
        mask - the network mask in CIDR notation
        Returns:
        true if the two given addresses are in the same network, false otherwise
      • sameNetwork

        public static boolean sameNetwork​(byte[] x,
                                          byte[] y,
                                          short cidr)
        Checks if two given addresses are in the same network.
        Parameters:
        x - first address
        y - second address
        cidr - the network mask in CIDR notation
        Returns:
        true if the two given addresses are in the same network, false otherwise
      • cidr2Netmask

        public static byte[] cidr2Netmask​(short cidr,
                                          int byteLength)
        Converts a given CIDR mask to valid IPv4 or IPv6 network mask.
        Parameters:
        cidr - the cidr
        byteLength - the final mask length (IPv4 = 4 bytes, IPv6 = 16 bytes)
        Returns:
        the CIDR as network mask byte array
        Throws:
        IllegalArgumentException - if the byteLength argument is invalid
      • getDefaultGateway

        public static InetAddress getDefaultGateway()
      • getIpv4MappedIPv6AddressBytes

        public static byte[] getIpv4MappedIPv6AddressBytes​(InetAddress address)
      • getLocalAddressForRemoteAddress

        public static InetAddress getLocalAddressForRemoteAddress​(InetSocketAddress remoteAddress)
        Establishes a connection to remoteAddress and returns the local address used for it.If an error occurs, null is returned.
        Parameters:
        remoteAddress - The remote address
        Returns:
        local address used to connect to remoteAddress, or null in case of error