Package org.openhab.core.net
Class NetUtil
java.lang.Object
org.openhab.core.net.NetUtil
- All Implemented Interfaces:
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 Summary
Modifier and TypeFieldDescriptionstatic final int
Default network interface poll interval 60 seconds. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
void
Adds aNetworkAddressChangeListener
that is notified about changes.protected void
Get all broadcast addresses on the current hoststatic Collection<CidrAddress>
Gets every IPv4+IPv6 Address on each Interface except the loopback interface.@Nullable String
Returns the user configured broadcast address, or the broadcast address of the user configured primary IPv4 if not providedstatic String
getIpv4NetAddress
(String ipAddressString, short netMask) Get the network address a specific ip address is instatic String
getIpv4NetBroadcastAddress
(String ipAddressString, short prefix) Get the network broadcast address of the subnet a specific ip address is in@Nullable String
Returns the user configured primary IPv4 address of the systemboolean
Use IPv6.boolean
Use only one address per interface and family (IPv4 and IPv6).static boolean
isValidIPConfig
(String ipAddress) Checks if the given String is a valid IPv4 Address or IPv4 address in CIDR notationvoid
static String
networkPrefixLengthToNetmask
(int prefixLength) Converts a netmask in bits into a string representation i.e.void
Removes aNetworkAddressChangeListener
so that it is no longer notified about changes.
-
Field Details
-
POLL_INTERVAL_SECONDS
public static final int POLL_INTERVAL_SECONDSDefault network interface poll interval 60 seconds.- See Also:
-
-
Constructor Details
-
NetUtil
-
-
Method Details
-
activate
-
deactivate
protected void deactivate() -
modified
-
getPrimaryIpv4HostAddress
Description copied from interface:NetworkAddressService
Returns the user configured primary IPv4 address of the system- Specified by:
getPrimaryIpv4HostAddress
in interfaceNetworkAddressService
- 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 interfaceNetworkAddressService
- 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 interfaceNetworkAddressService
- Returns:
- use IPv6
-
addNetworkAddressChangeListener
Description copied from interface:NetworkAddressService
Adds aNetworkAddressChangeListener
that is notified about changes.- Specified by:
addNetworkAddressChangeListener
in interfaceNetworkAddressService
- Parameters:
listener
- The listener
-
removeNetworkAddressChangeListener
Description copied from interface:NetworkAddressService
Removes aNetworkAddressChangeListener
so that it is no longer notified about changes.- Specified by:
removeNetworkAddressChangeListener
in interfaceNetworkAddressService
- Parameters:
listener
- The listener
-
getAllBroadcastAddresses
Get all broadcast addresses on the current host- Returns:
- list of broadcast addresses, empty list if no broadcast addresses found
-
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 interfaceNetworkAddressService
- 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
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: Listl = 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
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
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
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
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
-