Class NetworkUtils


  • public class NetworkUtils
    extends Object
    Utility methods related to networking.
    Author:
    Brian Stansberry (c) 2011 Red Hat Inc.
    • Field Detail

      • can_bind_to_mcast_addr

        private static final boolean can_bind_to_mcast_addr
    • Constructor Detail

      • NetworkUtils

        private NetworkUtils()
    • Method Detail

      • formatPossibleIpv6Address

        public static String formatPossibleIpv6Address​(String address)
      • canonize

        public static String canonize​(String ipv6Address)
                               throws IllegalArgumentException

        Convert IPv6 adress into RFC 5952 form. E.g. 2001:db8:0:1:0:0:0:1 -> 2001:db8:0:1::1

        Method is null safe, and if IPv4 address or host name is passed to the method it is returned wihout any processing.

        Method also supports IPv4 in IPv6 (e.g. 0:0:0:0:0:ffff:192.0.2.1 -> ::ffff:192.0.2.1), and zone ID (e.g. fe80:0:0:0:f0f0:c0c0:1919:1234%4 -> fe80::f0f0:c0c0:1919:1234%4).

        Parameters:
        ipv6Address - String representing valid IPv6 address.
        Returns:
        String representing IPv6 in canonical form.
        Throws:
        IllegalArgumentException - if IPv6 format is unacceptable.
      • mayBeIPv6Address

        private static boolean mayBeIPv6Address​(String input)
        Heuristic check if string might be an IPv6 address.
        Parameters:
        input - Any string or null
        Returns:
        true, if input string contains only hex digits and at least two colons, before '.' or '%' character.
      • isIPv4AddressInIPv6

        private static boolean isIPv4AddressInIPv6​(String ipv6Address)
        Check if it is an IPv4 in IPv6 format. e.g. 0:0:0:0:0:FFFF:127.0.0.1
        Parameters:
        ipv6Address - the address
        Returns:
        true, if input string is an IPv4 address in IPv6 format.
      • formatAddress

        public static String formatAddress​(InetAddress inet)
        Formats input address. For IPV4 returns simply host address, for IPV6 formats address according to RFC5952 rules. It does not embed IPV6 address in '[', ']', since those are part of IPV6 URI literal.
        Parameters:
        inet -
        Returns:
      • formatAddress

        public static String formatAddress​(InetSocketAddress inet)
        Converts socket address into string literal, which has form: 'address:port'. Example:
        • 127.0.0.1:8080
        • dns.name.com:8080
        • [0fe:1::20]:8080
        • [::1]:8080
        Parameters:
        inet -
        Returns:
      • formatAddress6

        private static String formatAddress6​(int[] hexRepresentation)
        Converts IPV6 int[] representation into valid IPV6 string literal. Sequence of '-1' values are converted into '::'.
        Parameters:
        hexRepresentation -
        Returns:
      • isBindingToMulticastDressSupported

        public static boolean isBindingToMulticastDressSupported()
      • compactLongestZeroSequence

        private static void compactLongestZeroSequence​(int[] hexRepresentatoin)
      • checkForLinux

        private static boolean checkForLinux()
      • checkForHp

        private static boolean checkForHp()
      • checkForSolaris

        private static boolean checkForSolaris()
      • checkForWindows

        private static boolean checkForWindows()
      • checkForMac

        public static boolean checkForMac()
      • checkForPresence

        private static boolean checkForPresence​(String key,
                                                String value)