Class NetUtil

  • All Implemented Interfaces:
    NetworkAddressService

    @NonNullByDefault
    public class NetUtil
    extends Object
    implements NetworkAddressService
    Some utility functions related to network interfaces etc.
    Author:
    Markus Rathgeb - Initial contribution, Mark Herwege - Added methods to find broadcast address(es), Stefan Triller - Converted to OSGi service with primary ipv4 conf, Gary Tse - Network address change listener, Tim Roberts - Added primary address change to network address change listener
    • Field Detail

      • POLL_INTERVAL_SECONDS

        public static final int POLL_INTERVAL_SECONDS
        Default network interface poll interval 60 seconds.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NetUtil

        public NetUtil()
    • Method Detail

      • deactivate

        protected void deactivate()
      • getPrimaryIpv4HostAddress

        public @Nullable String getPrimaryIpv4HostAddress()
        Description copied from interface: NetworkAddressService
        Returns the user configured primary IPv4 address of the system
        Specified by:
        getPrimaryIpv4HostAddress in interface NetworkAddressService
        Returns:
        IPv4 address as a String in format xxx.xxx.xxx.xxx or null if there is no interface or an error occurred
      • isUseOnlyOneAddress

        public boolean isUseOnlyOneAddress()
        Use only one address per interface and family (IPv4 and IPv6). If set listeners should bind only to one address per interface and family.
        Specified by:
        isUseOnlyOneAddress in interface NetworkAddressService
        Returns:
        use only one address per interface and family
      • isUseIPv6

        public boolean isUseIPv6()
        Use IPv6. If not set, IPv6 addresses should be completely ignored by listeners.
        Specified by:
        isUseIPv6 in interface NetworkAddressService
        Returns:
        use IPv6
      • getLocalIpv4HostAddress

        @Deprecated
        public static @Nullable String getLocalIpv4HostAddress()
        Deprecated.
        Please use the NetworkAddressService with getPrimaryIpv4HostAddress() Get the first candidate for a local IPv4 host address (non loopback, non localhost).
      • getAllBroadcastAddresses

        public static List<String> getAllBroadcastAddresses()
        Get all broadcast addresses on the current host
        Returns:
        list of broadcast addresses, empty list if no broadcast addresses found
      • getConfiguredBroadcastAddress

        public @Nullable String getConfiguredBroadcastAddress()
        Description copied from interface: NetworkAddressService
        Returns the user configured broadcast address, or the broadcast address of the user configured primary IPv4 if not provided
        Specified by:
        getConfiguredBroadcastAddress in interface NetworkAddressService
        Returns:
        IPv4 broadcast address as a String in format xxx.xxx.xxx or null if no broadcast address is found or an error occurred
      • getBroadcastAddress

        @Deprecated
        public static @Nullable String getBroadcastAddress()
        Deprecated.
        Please use the NetworkAddressService with getConfiguredBroadcastAddress() Get the first candidate for a broadcast address
        Returns:
        broadcast address, null if no broadcast address is found
      • getAllInterfaceAddresses

        public static Collection<CidrAddress> getAllInterfaceAddresses()
        Gets every IPv4+IPv6 Address on each Interface except the loopback interface. The Address format is in the CIDR notation which is ip/prefix-length e.g. 129.31.31.1/24. Example to get a list of only IPv4 addresses in string representation: List l = getAllInterfaceAddresses().stream().filter(a->a.getAddress() instanceof Inet4Address).map(a->a.getAddress().getHostAddress()).collect(Collectors.toList()); down, or loopback interfaces are skipped.
        Returns:
        The collected IPv4 and IPv6 Addresses
      • networkPrefixLengthToNetmask

        public static String networkPrefixLengthToNetmask​(int prefixLength)
        Converts a netmask in bits into a string representation i.e. 24 bits -> 255.255.255.0
        Parameters:
        prefixLength - bits of the netmask
        Returns:
        string representation of netmask (i.e. 255.255.255.0)
      • getIpv4NetAddress

        public static String getIpv4NetAddress​(String ipAddressString,
                                               short netMask)
        Get the network address a specific ip address is in
        Parameters:
        ipAddressString - ipv4 address of the device (i.e. 192.168.5.1)
        netMask - netmask in bits (i.e. 24)
        Returns:
        network a device is in (i.e. 192.168.5.0)
        Throws:
        IllegalArgumentException - if parameters are wrong
      • getIpv4NetBroadcastAddress

        public static String getIpv4NetBroadcastAddress​(String ipAddressString,
                                                        short prefix)
        Get the network broadcast address of the subnet a specific ip address is in
        Parameters:
        ipAddressString - ipv4 address of the device (i.e. 192.168.5.1)
        prefix - network prefix in bits (i.e. 24)
        Returns:
        network broadcast address of the network the device is in (i.e. 192.168.5.255)
        Throws:
        IllegalArgumentException - if parameters are wrong
      • isValidIPConfig

        public static boolean isValidIPConfig​(String ipAddress)
        Checks if the given String is a valid IPv4 Address or IPv4 address in CIDR notation
        Parameters:
        ipAddress - in format xxx.xxx.xxx.xxx or xxx.xxx.xxx.xxx/xx
        Returns:
        true if it is a valid address
      • setSafeCaller

        protected void setSafeCaller​(SafeCaller safeCaller)
      • unsetSafeCaller

        protected void unsetSafeCaller​(SafeCaller safeCaller)