Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Can't bind to any interface except WIFI #16

Open
tntclaus opened this issue Apr 4, 2017 · 1 comment
Open

Can't bind to any interface except WIFI #16

tntclaus opened this issue Apr 4, 2017 · 1 comment

Comments

@tntclaus
Copy link

tntclaus commented Apr 4, 2017

If android device has multiple interfaces along with WIFI binding to particular address explicitly results in 0.0.0.0 address if WIFI is disabled or in actual WIFI address if WIFI is enabled broadcasted in mDNS service record. Thus value passed to .address method is completely ignored.

Example code in kotlin:

val subscription = RxBonjour.newBroadcast("_http._tcp")
        .name("_my_service")
        .address(InetAddress.getByName("192.168.0.11"))
        .port(HTTP_PORT)
        .build()
        .start(this)
        .subscribe({
            log.info("{}", it)
        })

Result would be either 0.0.0.0 or ip of WIFI network.

This happens because of this method of SupportUtils class:

/**
 * Returns the JmDNS shared among all subscribers for Bonjour events, creating it if necessary.
 *
 * @param context Context used to access the WifiManager for the device's IP address with which JmDNS is initialized
 * @return The JmDNS instance
 * @throws IOException In case the device's address can't be resolved
 */
@Override public JmDNS getManager(Context context) throws IOException {
    synchronized (jmdnsLock) {
        if (jmdnsInstance == null || !isAvailable()) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            InetAddress inetAddress = getInetAddress(wifiManager);
            jmdnsInstance = JmDNS.create(inetAddress, inetAddress.toString());
            jmdnsSubscriberCount.set(0);
        }
        return jmdnsInstance;
    }
}

which gets context passed in SupportBonjourBroadcast class' start method:

final JmDNS jmdns = utils.getManager(context);

To override this behaviour I wrote another implementation of BonjourBroadcast abstract class which is almost the same except it uses differen method in support utils. This is its kotlin code:

    fun getManager(inetAddress: InetAddress): JmDNS? {
        synchronized(jmdnsLock) {
            if (jmdnsInstance == null || !isAvailable()) {
                jmdnsInstance = JmDNS.create(inetAddress, inetAddress.toString())
                jmdnsSubscriberCount.set(0)
            }
            return jmdnsInstance
        }
    }

I am not proposing this exact fix but I think something should be done with the issue of binding to the address specified by corresponding method.

@tntclaus tntclaus changed the title Can't bind to any interfaces except WIFI Can't bind to any interface except WIFI Apr 4, 2017
@mannodermaus
Copy link
Owner

Thanks for the info! It looks like WifiManager should only be consulted for address resolution if no explicit address is provided. I'll look into it.

@mannodermaus mannodermaus added this to the x.0.1 milestone Sep 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants