Fix exception thrown when isUp() is called on an unavailable network interface (#1679)

This commit is contained in:
Craig Schardt
2025-01-03 23:45:57 -06:00
committed by GitHub
parent ab844a77b8
commit a84d681782
2 changed files with 10 additions and 11 deletions

View File

@@ -274,9 +274,8 @@ public class NetworkManager {
logger.warn("Interface " + devName + " is disconnected, check Ethernet!");
}
}
NetworkInterface iFace;
iFace = NetworkInterface.getByName(devName);
if (iFace.isUp()) {
var iFace = NetworkInterface.getByName(devName);
if (iFace != null && iFace.isUp()) {
String tmpAddresses = "";
tmpAddresses = iFace.getInterfaceAddresses().toString();
if (!last.addresses.equals(tmpAddresses)) {

View File

@@ -188,16 +188,16 @@ public class NetworkUtils {
List<String> addresses = new ArrayList<String>();
try {
var iFace = NetworkInterface.getByName(iFaceName);
for (var addr : iFace.getInterfaceAddresses()) {
var addrStr = addr.getAddress().toString();
if (addrStr.startsWith("/")) {
addrStr = addrStr.substring(1);
if (iFace != null && iFace.isUp()) {
for (var addr : iFace.getInterfaceAddresses()) {
var addrStr = addr.getAddress().toString();
if (addrStr.startsWith("/")) {
addrStr = addrStr.substring(1);
}
addrStr = addrStr + "/" + addr.getNetworkPrefixLength();
addresses.add(addrStr);
}
addrStr = addrStr + "/" + addr.getNetworkPrefixLength();
addresses.add(addrStr);
}
// addresses = iFace.inetAddresses().map(a ->
// a.getAddress().toString()).collect(Collectors.joining(","));
} catch (Exception e) {
e.printStackTrace();
}