From ed1a56dc6813106135a8b196328ed0c2d4015ea6 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 3 Jun 2017 13:34:19 -0700 Subject: [PATCH] Override jmDNS's detection of multicast-capable adapters to fix mDNS not binding to the primary NIC on some devices --- .../nvstream/mdns/MdnsDiscoveryAgent.java | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/moonlight-common/src/main/java/com/limelight/nvstream/mdns/MdnsDiscoveryAgent.java b/moonlight-common/src/main/java/com/limelight/nvstream/mdns/MdnsDiscoveryAgent.java index 2bee06b8..8fe3e816 100644 --- a/moonlight-common/src/main/java/com/limelight/nvstream/mdns/MdnsDiscoveryAgent.java +++ b/moonlight-common/src/main/java/com/limelight/nvstream/mdns/MdnsDiscoveryAgent.java @@ -4,15 +4,18 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.Inet4Address; import java.net.InetAddress; +import java.net.NetworkInterface; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import javax.jmdns.JmmDNS; +import javax.jmdns.NetworkTopologyDiscovery; import javax.jmdns.ServiceEvent; import javax.jmdns.ServiceInfo; import javax.jmdns.ServiceListener; +import javax.jmdns.impl.NetworkTopologyDiscoveryImpl; import com.limelight.LimeLog; @@ -77,7 +80,44 @@ public class MdnsDiscoveryAgent implements ServiceListener { } } }; - + + public static class MyNetworkTopologyDiscovery extends NetworkTopologyDiscoveryImpl { + @Override + public boolean useInetAddress(NetworkInterface networkInterface, InetAddress interfaceAddress) { + // This is an exact copy of jmDNS's implementation, except we omit the multicast check, since + // it seems at least some devices lie about interfaces not supporting multicast when they really do. + try { + if (!networkInterface.isUp()) { + return false; + } + + /* + if (!networkInterface.supportsMulticast()) { + return false; + } + */ + + if (networkInterface.isLoopback()) { + return false; + } + + return true; + } catch (Exception exception) { + return false; + } + } + }; + + static { + // Override jmDNS's default topology discovery class with ours + NetworkTopologyDiscovery.Factory.setClassDelegate(new NetworkTopologyDiscovery.Factory.ClassDelegate() { + @Override + public NetworkTopologyDiscovery newNetworkTopologyDiscovery() { + return new MyNetworkTopologyDiscovery(); + } + }); + } + private static JmmDNS referenceResolver() { synchronized (MdnsDiscoveryAgent.class) { JmmDNS instance = JmmDNS.Factory.getInstance();