[network] Removed last references to org.apache... (#12341)

* Removed unwanted files
* Badly spelled variable and incident code refactor

Signed-off-by: Gaël L'hopital <gael@lhopital.org>
Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-03-04 08:45:23 +01:00 committed by GitHub
parent d4938a6942
commit 84687674d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 102 additions and 67 deletions

View File

@ -28,7 +28,7 @@ import org.openhab.core.thing.ThingTypeUID;
@NonNullByDefault @NonNullByDefault
public class NetworkBindingConstants { public class NetworkBindingConstants {
public static final String BINDING_ID = "network"; private static final String BINDING_ID = "network";
// List of all Thing Type UIDs // List of all Thing Type UIDs
public static final ThingTypeUID BACKWARDS_COMPATIBLE_DEVICE = new ThingTypeUID(BINDING_ID, "device"); public static final ThingTypeUID BACKWARDS_COMPATIBLE_DEVICE = new ThingTypeUID(BINDING_ID, "device");

View File

@ -29,6 +29,7 @@ import java.util.function.Consumer;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.network.internal.dhcp.DHCPListenService; import org.openhab.binding.network.internal.dhcp.DHCPListenService;
import org.openhab.binding.network.internal.dhcp.DHCPPacketListenerServer;
import org.openhab.binding.network.internal.dhcp.IPRequestReceivedCallback; import org.openhab.binding.network.internal.dhcp.IPRequestReceivedCallback;
import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheAsync; import org.openhab.binding.network.internal.toberemoved.cache.ExpiringCacheAsync;
import org.openhab.binding.network.internal.utils.NetworkUtils; import org.openhab.binding.network.internal.utils.NetworkUtils;
@ -49,8 +50,7 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault @NonNullByDefault
public class PresenceDetection implements IPRequestReceivedCallback { public class PresenceDetection implements IPRequestReceivedCallback {
public static final double NOT_REACHABLE = -1; private static final int DESTINATION_TTL = 300 * 1000; // in ms, 300 s
public static final int DESTINATION_TTL = 300 * 1000; // in ms, 300 s
NetworkUtils networkUtils = new NetworkUtils(); NetworkUtils networkUtils = new NetworkUtils();
private final Logger logger = LoggerFactory.getLogger(PresenceDetection.class); private final Logger logger = LoggerFactory.getLogger(PresenceDetection.class);
@ -59,7 +59,7 @@ public class PresenceDetection implements IPRequestReceivedCallback {
private boolean useDHCPsniffing = false; private boolean useDHCPsniffing = false;
private String ipPingState = "Disabled"; private String ipPingState = "Disabled";
protected String arpPingUtilPath = ""; protected String arpPingUtilPath = "";
protected ArpPingUtilEnum arpPingMethod = ArpPingUtilEnum.DISABLED; private ArpPingUtilEnum arpPingMethod = ArpPingUtilEnum.DISABLED;
protected @Nullable IpPingMethodEnum pingMethod = null; protected @Nullable IpPingMethodEnum pingMethod = null;
private boolean iosDevice; private boolean iosDevice;
private Set<Integer> tcpPorts = new HashSet<>(); private Set<Integer> tcpPorts = new HashSet<>();
@ -72,7 +72,7 @@ public class PresenceDetection implements IPRequestReceivedCallback {
private @NonNullByDefault({}) ExpiringCache<@Nullable InetAddress> destination; private @NonNullByDefault({}) ExpiringCache<@Nullable InetAddress> destination;
private @Nullable InetAddress cachedDestination = null; private @Nullable InetAddress cachedDestination = null;
public boolean preferResponseTimeAsLatency; private boolean preferResponseTimeAsLatency;
/// State variables (cannot be final because of test dependency injections) /// State variables (cannot be final because of test dependency injections)
ExpiringCacheAsync<PresenceDetectionValue> cache; ExpiringCacheAsync<PresenceDetectionValue> cache;
@ -80,7 +80,7 @@ public class PresenceDetection implements IPRequestReceivedCallback {
private @Nullable ScheduledFuture<?> refreshJob; private @Nullable ScheduledFuture<?> refreshJob;
protected @Nullable ExecutorService executorService; protected @Nullable ExecutorService executorService;
private String dhcpState = "off"; private String dhcpState = "off";
Integer currentCheck = 0; private Integer currentCheck = 0;
int detectionChecks; int detectionChecks;
public PresenceDetection(final PresenceDetectionListener updateListener, int cacheDeviceStateTimeInMS) public PresenceDetection(final PresenceDetectionListener updateListener, int cacheDeviceStateTimeInMS)
@ -112,12 +112,13 @@ public class PresenceDetection implements IPRequestReceivedCallback {
this.destination = new ExpiringCache<>(DESTINATION_TTL, () -> { this.destination = new ExpiringCache<>(DESTINATION_TTL, () -> {
try { try {
InetAddress destinationAddress = InetAddress.getByName(hostname); InetAddress destinationAddress = InetAddress.getByName(hostname);
if (!destinationAddress.equals(cachedDestination)) { InetAddress cached = cachedDestination;
if (!destinationAddress.equals(cached)) {
logger.trace("host name resolved to other address, (re-)setup presence detection"); logger.trace("host name resolved to other address, (re-)setup presence detection");
setUseArpPing(true, destinationAddress); setUseArpPing(true, destinationAddress);
if (useDHCPsniffing) { if (useDHCPsniffing) {
if (cachedDestination != null) { if (cached != null) {
disableDHCPListen(cachedDestination); disableDHCPListen(cached);
} }
enableDHCPListen(destinationAddress); enableDHCPListen(destinationAddress);
} }
@ -126,8 +127,9 @@ public class PresenceDetection implements IPRequestReceivedCallback {
return destinationAddress; return destinationAddress;
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
logger.trace("hostname resolution failed"); logger.trace("hostname resolution failed");
if (cachedDestination != null) { InetAddress cached = cachedDestination;
disableDHCPListen(cachedDestination); if (cached != null) {
disableDHCPListen(cached);
cachedDestination = null; cachedDestination = null;
} }
return null; return null;
@ -594,8 +596,9 @@ public class PresenceDetection implements IPRequestReceivedCallback {
future.cancel(true); future.cancel(true);
refreshJob = null; refreshJob = null;
} }
if (cachedDestination != null) { InetAddress cached = cachedDestination;
disableDHCPListen(cachedDestination); if (cached != null) {
disableDHCPListen(cached);
} }
} }
@ -608,22 +611,18 @@ public class PresenceDetection implements IPRequestReceivedCallback {
*/ */
private void enableDHCPListen(InetAddress destinationAddress) { private void enableDHCPListen(InetAddress destinationAddress) {
try { try {
if (DHCPListenService.register(destinationAddress.getHostAddress(), this).isUseUnprevilegedPort()) { DHCPPacketListenerServer listener = DHCPListenService.register(destinationAddress.getHostAddress(), this);
dhcpState = "No access right for port 67. Bound to port 6767 instead. Port forwarding necessary!"; dhcpState = String.format("Bound to port %d - %s", listener.getCurrentPort(),
} else { (listener.usingPrivilegedPort() ? "Running normally" : "Port forwarding necessary !"));
dhcpState = "Running normally";
}
} catch (SocketException e) { } catch (SocketException e) {
logger.warn("Cannot use DHCP sniffing.", e); dhcpState = String.format("Cannot use DHCP sniffing: %s", e.getMessage());
logger.warn("{}", dhcpState);
useDHCPsniffing = false; useDHCPsniffing = false;
dhcpState = "Cannot use DHCP sniffing: " + e.getLocalizedMessage();
} }
} }
private void disableDHCPListen(@Nullable InetAddress destinationAddress) { private void disableDHCPListen(InetAddress destinationAddress) {
if (destinationAddress != null) { DHCPListenService.unregister(destinationAddress.getHostAddress());
DHCPListenService.unregister(destinationAddress.getHostAddress()); dhcpState = "off";
dhcpState = "off";
}
} }
} }

View File

@ -44,10 +44,10 @@ public class WakeOnLanPacketSender {
// Wake-on-LAN magic packet constants // Wake-on-LAN magic packet constants
static final int PREFIX_BYTE_SIZE = 6; static final int PREFIX_BYTE_SIZE = 6;
static final int MAC_REPETITIONS = 16; private static final int MAC_REPETITIONS = 16;
static final int MAC_BYTE_SIZE = 6; static final int MAC_BYTE_SIZE = 6;
static final int MAGIC_PACKET_BYTE_SIZE = PREFIX_BYTE_SIZE + MAC_REPETITIONS * MAC_BYTE_SIZE; static final int MAGIC_PACKET_BYTE_SIZE = PREFIX_BYTE_SIZE + MAC_REPETITIONS * MAC_BYTE_SIZE;
static final String[] MAC_SEPARATORS = new String[] { ":", "-" }; private static final String[] MAC_SEPARATORS = new String[] { ":", "-" };
private final Logger logger = LoggerFactory.getLogger(WakeOnLanPacketSender.class); private final Logger logger = LoggerFactory.getLogger(WakeOnLanPacketSender.class);

View File

@ -34,10 +34,9 @@ import org.slf4j.LoggerFactory;
@NonNullByDefault @NonNullByDefault
public class DHCPListenService { public class DHCPListenService {
static @Nullable DHCPPacketListenerServer instance; static @Nullable DHCPPacketListenerServer instance;
static Map<String, IPRequestReceivedCallback> registeredListeners = new TreeMap<>(); private static Map<String, IPRequestReceivedCallback> registeredListeners = new TreeMap<>();
static Logger logger = LoggerFactory.getLogger(DHCPListenService.class); private static Logger logger = LoggerFactory.getLogger(DHCPListenService.class);
@SuppressWarnings({ "null", "unused" })
public static synchronized DHCPPacketListenerServer register(String hostAddress, public static synchronized DHCPPacketListenerServer register(String hostAddress,
IPRequestReceivedCallback dhcpListener) throws SocketException { IPRequestReceivedCallback dhcpListener) throws SocketException {
DHCPPacketListenerServer instance = DHCPListenService.instance; DHCPPacketListenerServer instance = DHCPListenService.instance;

View File

@ -207,7 +207,6 @@ class DHCPPacket {
* *
* @return option type, of <tt>null</tt> if not present. * @return option type, of <tt>null</tt> if not present.
*/ */
@SuppressWarnings({ "null", "unused" })
public @Nullable Byte getDHCPMessageType() { public @Nullable Byte getDHCPMessageType() {
byte[] opt = options.get(DHO_DHCP_MESSAGE_TYPE); byte[] opt = options.get(DHO_DHCP_MESSAGE_TYPE);
if (opt == null) { if (opt == null) {
@ -223,7 +222,6 @@ class DHCPPacket {
/** /**
* Returns the requested IP address of a BOOTREQUEST packet. * Returns the requested IP address of a BOOTREQUEST packet.
*/ */
@SuppressWarnings({ "null", "unused" })
public @Nullable InetAddress getRequestedIPAddress() throws IllegalArgumentException, UnknownHostException { public @Nullable InetAddress getRequestedIPAddress() throws IllegalArgumentException, UnknownHostException {
byte[] opt = options.get(DHO_DHCP_REQUESTED_ADDRESS); byte[] opt = options.get(DHO_DHCP_REQUESTED_ADDRESS);
if (opt == null) { if (opt == null) {

View File

@ -13,7 +13,6 @@
package org.openhab.binding.network.internal.dhcp; package org.openhab.binding.network.internal.dhcp;
import java.io.IOException; import java.io.IOException;
import java.net.BindException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
@ -36,25 +35,27 @@ import org.slf4j.LoggerFactory;
*/ */
@NonNullByDefault @NonNullByDefault
public class DHCPPacketListenerServer extends Thread { public class DHCPPacketListenerServer extends Thread {
private static final int PRIVILEGED_PORT = 67;
private static final int UNPRIVILEGED_PORT = 6767;
private byte[] buffer = new byte[1024]; private byte[] buffer = new byte[1024];
protected @Nullable DatagramSocket dsocket; private @Nullable DatagramSocket dsocket;
private DatagramPacket packet = new DatagramPacket(buffer, buffer.length); private DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
boolean willbeclosed = false;
Logger logger = LoggerFactory.getLogger(DHCPPacketListenerServer.class);
private boolean useUnprevilegedPort = false;
private final IPRequestReceivedCallback listener; private final IPRequestReceivedCallback listener;
private final Logger logger = LoggerFactory.getLogger(DHCPPacketListenerServer.class);
private boolean willbeclosed = false;
private int currentPort = PRIVILEGED_PORT;
DHCPPacketListenerServer(IPRequestReceivedCallback listener) throws SocketException, BindException { DHCPPacketListenerServer(IPRequestReceivedCallback listener) throws SocketException {
this.listener = listener; this.listener = listener;
try { try {
bindSocketTo(67); bindSocketTo(currentPort);
} catch (SocketException e) { } catch (SocketException e) {
useUnprevilegedPort = true; currentPort = UNPRIVILEGED_PORT;
bindSocketTo(6767); bindSocketTo(currentPort);
} }
} }
protected void bindSocketTo(int port) throws SocketException { private void bindSocketTo(int port) throws SocketException {
DatagramSocket dsocket = new DatagramSocket(null); DatagramSocket dsocket = new DatagramSocket(null);
dsocket.setReuseAddress(true); dsocket.setReuseAddress(true);
dsocket.setBroadcast(true); dsocket.setBroadcast(true);
@ -112,10 +113,9 @@ public class DHCPPacketListenerServer extends Thread {
return dsocket; return dsocket;
} }
// Return true if the instance couldn't bind to port 67 and used port 6767 instead // Return true if the instance is using port 67 to listen to DHCP traffic (no port forwarding necessary).
// to listen to DHCP traffic (port forwarding necessary). public boolean usingPrivilegedPort() {
public boolean isUseUnprevilegedPort() { return currentPort == PRIVILEGED_PORT;
return useUnprevilegedPort;
} }
/** /**
@ -137,4 +137,8 @@ public class DHCPPacketListenerServer extends Thread {
dsocket = null; dsocket = null;
} }
} }
public int getCurrentPort() {
return currentPort;
}
} }

View File

@ -31,12 +31,12 @@ import org.eclipse.jdt.annotation.Nullable;
*/ */
@NonNullByDefault @NonNullByDefault
public class ExpiringCacheAsync<V> { public class ExpiringCacheAsync<V> {
final long expiry; private final long expiry;
ExpiringCacheUpdate cacheUpdater; private ExpiringCacheUpdate cacheUpdater;
long expiresAt = 0; long expiresAt = 0;
boolean refreshRequested = false; private boolean refreshRequested = false;
V value; private V value;
final List<Consumer<V>> waitingCacheCallbacks = new LinkedList<>(); private final List<Consumer<V>> waitingCacheCallbacks = new LinkedList<>();
/** /**
* Implement the requestCacheUpdate method which will be called when the cache * Implement the requestCacheUpdate method which will be called when the cache
@ -114,7 +114,7 @@ public class ExpiringCacheAsync<V> {
* *
* @return the new value * @return the new value
*/ */
public void refreshValue(Consumer<V> callback) { private void refreshValue(Consumer<V> callback) {
waitingCacheCallbacks.add(callback); waitingCacheCallbacks.add(callback);
if (refreshRequested) { if (refreshRequested) {
return; return;

View File

@ -28,16 +28,17 @@ import java.net.Socket;
import java.net.SocketAddress; import java.net.SocketAddress;
import java.net.SocketException; import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.net.util.SubnetUtils;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.io.net.exec.ExecUtil; import org.openhab.core.io.net.exec.ExecUtil;
@ -68,6 +69,35 @@ public class NetworkUtils {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
/**
* Gets every IPv4 address on the network defined by its cidr
*
* @return The collected IPv4 Addresses
*/
private List<String> getIPAddresses(CidrAddress adr) {
List<String> result = new ArrayList<>();
byte[] octets = adr.getAddress().getAddress();
final int addressCount = (1 << (32 - adr.getPrefix())) - 2;
final int ipMask = 0xFFFFFFFF << (32 - adr.getPrefix());
octets[0] &= ipMask >> 24;
octets[1] &= ipMask >> 16;
octets[2] &= ipMask >> 8;
octets[3] &= ipMask;
try {
final CidrAddress baseIp = new CidrAddress(InetAddress.getByAddress(octets), (short) adr.getPrefix());
for (int i = 1; i <= addressCount; i++) {
int octet = i & ~ipMask;
byte[] segments = baseIp.getAddress().getAddress();
segments[2] += (octet >> 8);
segments[3] += octet;
result.add(InetAddress.getByAddress(segments).getHostAddress());
}
} catch (UnknownHostException e) {
logger.debug("Could not build net ip address.", e);
}
return result;
}
/** /**
* Get a set of all interface names. * Get a set of all interface names.
* *
@ -108,7 +138,7 @@ public class NetworkUtils {
* @param maximumPerInterface The maximum of IP addresses per interface or 0 to get all. * @param maximumPerInterface The maximum of IP addresses per interface or 0 to get all.
* @return Every single IP which can be assigned on the Networks the computer is connected to * @return Every single IP which can be assigned on the Networks the computer is connected to
*/ */
public Set<String> getNetworkIPs(Set<CidrAddress> interfaceIPs, int maximumPerInterface) { private Set<String> getNetworkIPs(Set<CidrAddress> interfaceIPs, int maximumPerInterface) {
LinkedHashSet<String> networkIPs = new LinkedHashSet<>(); LinkedHashSet<String> networkIPs = new LinkedHashSet<>();
short minCidrPrefixLength = 8; // historic Class A network, addresses = 16777214 short minCidrPrefixLength = 8; // historic Class A network, addresses = 16777214
@ -132,14 +162,13 @@ public class NetworkUtils {
cidrNotation = new CidrAddress(cidrNotation.getAddress(), minCidrPrefixLength); cidrNotation = new CidrAddress(cidrNotation.getAddress(), minCidrPrefixLength);
} }
SubnetUtils utils = new SubnetUtils(cidrNotation.toString()); List<String> addresses = getIPAddresses(cidrNotation);
String[] addresses = utils.getInfo().getAllAddresses(); int len = addresses.size();
int len = addresses.length;
if (maximumPerInterface != 0 && maximumPerInterface < len) { if (maximumPerInterface != 0 && maximumPerInterface < len) {
len = maximumPerInterface; len = maximumPerInterface;
} }
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
networkIPs.add(addresses[i]); networkIPs.add(addresses.get(i));
} }
} }
@ -173,16 +202,22 @@ public class NetworkUtils {
* works JavaPing is returned. * works JavaPing is returned.
*/ */
public IpPingMethodEnum determinePingMethod() { public IpPingMethodEnum determinePingMethod() {
String os = System.getProperty("os.name");
IpPingMethodEnum method; IpPingMethodEnum method;
if (SystemUtils.IS_OS_WINDOWS) { if (os == null) {
method = IpPingMethodEnum.WINDOWS_PING;
} else if (SystemUtils.IS_OS_MAC) {
method = IpPingMethodEnum.MAC_OS_PING;
} else if (SystemUtils.IS_OS_UNIX) {
method = IpPingMethodEnum.IPUTILS_LINUX_PING;
} else {
// We cannot estimate the command line for any other operating system and just return false
return IpPingMethodEnum.JAVA_PING; return IpPingMethodEnum.JAVA_PING;
} else {
os = os.toLowerCase();
if (os.indexOf("win") >= 0) {
method = IpPingMethodEnum.WINDOWS_PING;
} else if (os.indexOf("mac") >= 0) {
method = IpPingMethodEnum.MAC_OS_PING;
} else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") >= 0) {
method = IpPingMethodEnum.IPUTILS_LINUX_PING;
} else {
// We cannot estimate the command line for any other operating system and just return false
return IpPingMethodEnum.JAVA_PING;
}
} }
try { try {