[dscalarm] Remove org.apache.commons.net.util.SubnetUtils (#17407)

* Remove apache

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
lsiepel 2024-10-13 13:32:49 +02:00 committed by Ciprian Pascu
parent 8601ae4717
commit 75b7783b01

View File

@ -15,15 +15,17 @@ package org.openhab.binding.dscalarm.internal.discovery;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;
import org.apache.commons.net.util.SubnetUtils;
import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
import org.openhab.core.net.CidrAddress;
import org.openhab.core.net.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -57,43 +59,29 @@ public class EnvisalinkBridgeDiscovery {
public synchronized void discoverBridge() {
logger.debug("Starting Envisalink Bridge Discovery.");
SubnetUtils subnetUtils = null;
SubnetInfo subnetInfo = null;
long lowIP = 0;
long highIP = 0;
CidrAddress localCidrAddress;
try {
InetAddress localHost = InetAddress.getLocalHost();
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
subnetUtils = new SubnetUtils(localHost.getHostAddress() + "/"
+ networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength());
subnetInfo = subnetUtils.getInfo();
lowIP = convertIPToNumber(subnetInfo.getLowAddress());
highIP = convertIPToNumber(subnetInfo.getHighAddress());
} catch (IllegalArgumentException e) {
logger.warn("discoverBridge(): Illegal Argument Exception - {}", e.toString());
return;
} catch (Exception e) {
logger.warn("discoverBridge(): Error - Unable to get Subnet Information! {}", e.toString());
localCidrAddress = NetUtil.getAllInterfaceAddresses().stream()
.filter(f -> f.getAddress() instanceof Inet4Address && f.getAddress().equals(localHost)).findFirst()
.orElse(null);
} catch (UnknownHostException e) {
logger.warn("discoverBridge(): UnknownHostException - {}", e.toString());
return;
}
logger.debug(" Local IP Address: {} - {}", subnetInfo.getAddress(),
convertIPToNumber(subnetInfo.getAddress()));
logger.debug(" Subnet: {} - {}", subnetInfo.getNetworkAddress(),
convertIPToNumber(subnetInfo.getNetworkAddress()));
logger.debug(" Network Prefix: {}", subnetInfo.getCidrSignature().split("/")[1]);
logger.debug(" Network Mask: {}", subnetInfo.getNetmask());
logger.debug(" Low IP: {}", convertNumberToIP(lowIP));
logger.debug(" High IP: {}", convertNumberToIP(highIP));
List<InetAddress> addressesToScan = localCidrAddress != null
? NetUtil.getAddressesRangeByCidrAddress(localCidrAddress, 16)
: List.of();
for (long ip = lowIP; ip <= highIP; ip++) {
logger.debug("Performing discovery on {} ip addresses", addressesToScan.size());
for (InetAddress inetAddress : addressesToScan) {
try (Socket socket = new Socket()) {
ipAddress = convertNumberToIP(ip);
socket.setReuseAddress(true);
socket.setReceiveBufferSize(32);
socket.connect(new InetSocketAddress(ipAddress, ENVISALINK_BRIDGE_PORT), CONNECTION_TIMEOUT);
socket.connect(new InetSocketAddress(inetAddress.getHostAddress(), ENVISALINK_BRIDGE_PORT),
CONNECTION_TIMEOUT);
if (socket.isConnected()) {
String message = "";
socket.setSoTimeout(SO_TIMEOUT);
@ -128,55 +116,4 @@ public class EnvisalinkBridgeDiscovery {
}
}
}
/**
* Convert an IP address to a number.
*
* @param ipAddress
* @return
*/
private long convertIPToNumber(String ipAddress) {
String[] octets = ipAddress.split("\\.");
if (octets.length != 4) {
throw new IllegalArgumentException("Invalid IP address: " + ipAddress);
}
long ip = 0;
for (int i = 3; i >= 0; i--) {
long octet = Long.parseLong(octets[3 - i]);
if (octet != (octet & 0xff)) {
throw new IllegalArgumentException("Invalid IP address: " + ipAddress);
}
ip |= octet << (i * 8);
}
return ip;
}
/**
* Convert a number to an IP address.
*
* @param ip
* @return
*/
private String convertNumberToIP(long ip) {
StringBuilder ipAddress = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
ipAddress.insert(0, Long.toString(ip & 0xff));
if (i < 3) {
ipAddress.insert(0, '.');
}
ip = ip >> 8;
}
return ipAddress.toString();
}
}