Guard against null return value from java.net.NetworkInterface.getNetworkInterfaces().

(cherry picked from commit d292372902)
This commit is contained in:
Gary Gregory 2020-11-05 10:13:50 -05:00 committed by jbonofre
parent 2814a2451d
commit 020276f499
1 changed files with 14 additions and 12 deletions

View File

@ -345,20 +345,22 @@ public class MulticastDiscoveryAgent implements DiscoveryAgent, Runnable {
private NetworkInterface findNetworkInterface() throws SocketException {
Enumeration<NetworkInterface> ifcs = NetworkInterface.getNetworkInterfaces();
List<NetworkInterface> possibles = new ArrayList<NetworkInterface>();
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
try {
if (ni.supportsMulticast()
&& ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia != null && ia.getAddress() instanceof java.net.Inet4Address
&& !ia.getAddress().isLoopbackAddress()
&& (ni.getDisplayName()==null || !ni.getDisplayName().startsWith("vnic"))) {
possibles.add(ni);
if (ifcs != null) {
while (ifcs.hasMoreElements()) {
NetworkInterface ni = ifcs.nextElement();
try {
if (ni.supportsMulticast()
&& ni.isUp()) {
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
if (ia != null && ia.getAddress() instanceof java.net.Inet4Address
&& !ia.getAddress().isLoopbackAddress()
&& (ni.getDisplayName()==null || !ni.getDisplayName().startsWith("vnic"))) {
possibles.add(ni);
}
}
}
}
} catch (SocketException ignored) {}
} catch (SocketException ignored) {}
}
}
return possibles.isEmpty() ? null : possibles.get(possibles.size() - 1);
}