Zack Matthews
Zack Matthews

Reputation: 409

Android: DNS Java SRV lookup fails when network connectivity changes

Happy Friday, everyone!

My Android app relies on DNS Java (http://www.dnsjava.org/doc/) to perform an SRV lookup. We just discovered that if network connectivity changes (Switch from LTE to WiFi or vice versa) each lookup from then on will perpetually timeout regardless of whether internet connectivity is established again on the new network. Sometimes this is resolved by switching back to the old network, but if you change too many times it will not recover. Here is the code we're using below.

If I can provide any other details please let me know. Help is much appreciated!

private static class ConfigUpdater extends AsyncTask<Void, Void, JsonConfig> {
    private static final String SRV_RUE_CONFIG_PREFIX = "_rueconfig._tls.";
    ConfigListener listener;
    String request_url;
    String query_url;
    String username, password;
    String errorMsg;

    public ConfigUpdater(String url, String username, String password, ConfigListener listener) {
        this.username = username;
        this.password = password;
        this.listener = listener;
        query_url = SRV_RUE_CONFIG_PREFIX + url;
        errorMsg = "Failed to Login";
        org.xbill.DNS.ResolverConfig.refresh();
    }

    @Override
    protected JsonConfig doInBackground(Void... params) {
        Record[] records;// = new Record[0];
        try {
            Lookup configLookup = new Lookup(query_url, Type.SRV);
            configLookup.setCache(null);
            records = configLookup.run();
        } catch (TextParseException e) {
            e.printStackTrace();
            return null;
        }
        if(records != null && records.length > 0) {
            for (Record record : records) {
                SRVRecord srv = (SRVRecord) record;
                String hostname = srv.getTarget().toString().replaceFirst("\\.$", "");
                request_url = "https://" + hostname + "/config/v1/config.json";
                Log.d("Auto Config request_url: "+request_url);
            }

            try {
                String reponse_str = getFromHttpURLConnection();
                Log.d("Auto Config JSON: "+reponse_str);
                return parseJson(username, reponse_str, request_url);
            } catch (Throwable e){
                Log.d("Issue parsing json");
                e.printStackTrace();
            }
        }
        return null;
    }

Upvotes: 1

Views: 1728

Answers (1)

Zack Matthews
Zack Matthews

Reputation: 409

For anyone who may ever struggle with SRV lookups failing after a network switch I seem to have found the problem. The issue was that the Resolver property on the Lookup object seems to cache network configuration. If you want to ensure your lookups always have the latest network configuration, simply reinitialize a new Resolver. With the modifications below I was unable to reproduce any of the initial test cases.

Record[] records = null;// = new Record[0];
        try {
            Lookup configLookup = new Lookup(query_url, Type.SRV);
            configLookup.setResolver(new ExtendedResolver()); /** FIX **/
            records = configLookup.run();
        } catch (TextParseException e) {
            e.printStackTrace();
            return null;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

Upvotes: 2

Related Questions