Class NetUtil

java.lang.Object
org.openhab.core.net.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, Leo Siepel - Added methods to improve support for network scanning
  • Field Details

    • POLL_INTERVAL_SECONDS

      public static final int POLL_INTERVAL_SECONDS
      Default network interface poll interval 60 seconds.
      See Also:
  • Constructor Details

  • Method Details

    • activate

      protected void activate(Map<String,Object> props)
    • deactivate

      protected void deactivate()
    • modified

      public void modified(Map<String,Object> config)
    • 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
    • addNetworkAddressChangeListener

      public void addNetworkAddressChangeListener(NetworkAddressChangeListener listener)
      Description copied from interface: NetworkAddressService
      Adds a NetworkAddressChangeListener that is notified about changes.
      Specified by:
      addNetworkAddressChangeListener in interface NetworkAddressService
      Parameters:
      listener - The listener
    • removeNetworkAddressChangeListener

      public void removeNetworkAddressChangeListener(NetworkAddressChangeListener listener)
      Description copied from interface: NetworkAddressService
      Removes a NetworkAddressChangeListener so that it is no longer notified about changes.
      Specified by:
      removeNetworkAddressChangeListener in interface NetworkAddressService
      Parameters:
      listener - The listener
    • 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
    • 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()).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
    • getFullRangeOfAddressesToScan

      public static List<InetAddress> getFullRangeOfAddressesToScan()
      For all network interfaces (except loopback) all IPv4 addresses are returned. This list can for example, be used to scan the network for available devices.
      Returns:
      A full list of IP InetAddress (except network and broadcast)
    • getAddressesRangeByCidrAddress

      public static List<InetAddress> getAddressesRangeByCidrAddress(CidrAddress iFaceAddress, int maxAllowedPrefixLength)
      For the given CidrAddress all IPv4 addresses are returned. This list can, for example, be used to scan the network for available devices.
      Parameters:
      iFaceAddress - The CidrAddress of the network interface
      maxAllowedPrefixLength - Control the maximum allowed prefix length of the network (e.g. 24 for class C). iFaceAddress's with a larger prefix are ignored and return an empty result.
      Returns:
      A full list of IP InetAddress (except network and broadcast)