8178161: Default multicast interface on Mac

Reviewed-by: michaelm, bpb
This commit is contained in:
Chris Hegarty 2017-04-07 10:39:46 +01:00
parent 504ceb8589
commit 19becf9f61

View File

@ -50,10 +50,11 @@ class DefaultInterface {
} }
/** /**
* Choose a default interface. This method returns an interface that is * Choose a default interface. This method returns the first interface that
* both "up" and supports multicast. This method choses an interface in * is both "up" and supports multicast. This method chooses an interface in
* order of preference: * order of preference:
* 1. neither loopback nor point to point * 1. neither loopback nor point to point
* ( prefer interfaces with dual IP support )
* 2. point to point * 2. point to point
* 3. loopback * 3. loopback
* *
@ -66,32 +67,56 @@ class DefaultInterface {
try { try {
nifs = NetworkInterface.getNetworkInterfaces(); nifs = NetworkInterface.getNetworkInterfaces();
} catch (IOException ignore) { } catch (IOException ignore) {
// unable to enumate network interfaces // unable to enumerate network interfaces
return null; return null;
} }
NetworkInterface preferred = null;
NetworkInterface ppp = null; NetworkInterface ppp = null;
NetworkInterface loopback = null; NetworkInterface loopback = null;
while (nifs.hasMoreElements()) { while (nifs.hasMoreElements()) {
NetworkInterface ni = nifs.nextElement(); NetworkInterface ni = nifs.nextElement();
try { try {
if (ni.isUp() && ni.supportsMulticast()) { if (!ni.isUp() || !ni.supportsMulticast())
boolean isLoopback = ni.isLoopback(); continue;
boolean isPPP = ni.isPointToPoint();
if (!isLoopback && !isPPP) { boolean ip4 = false, ip6 = false;
// found an interface that is not the loopback or a Enumeration<InetAddress> addrs = ni.getInetAddresses();
// point-to-point interface while (addrs.hasMoreElements()) {
InetAddress addr = addrs.nextElement();
if (!addr.isAnyLocalAddress()) {
if (addr instanceof Inet4Address) {
ip4 = true;
} else if (addr instanceof Inet6Address) {
ip6 = true;
}
}
}
boolean isLoopback = ni.isLoopback();
boolean isPPP = ni.isPointToPoint();
if (!isLoopback && !isPPP) {
// found an interface that is not the loopback or a
// point-to-point interface
if (preferred == null) {
preferred = ni;
} else if (ip4 && ip6){
return ni; return ni;
} }
if (ppp == null && isPPP)
ppp = ni;
if (loopback == null && isLoopback)
loopback = ni;
} }
if (ppp == null && isPPP)
ppp = ni;
if (loopback == null && isLoopback)
loopback = ni;
} catch (IOException skip) { } } catch (IOException skip) { }
} }
return (ppp != null) ? ppp : loopback; if (preferred != null) {
return preferred;
} else {
return (ppp != null) ? ppp : loopback;
}
} }
} }