dnsjava library return "no records found"

795 Views Asked by At

I'm using dnsjava in my android project "android studio" I have imported the library. But when I run the code I'm getting "no records found" does anyone face this? I found a post here that talks about similar problem but the solution didn't work. as I change the type of query to ANY or A or NS. It just doesn't work. Anyone know if this is due to malfunction of the library code? any suggestions welcome.

package org.pctechtips.netdroid.async;

import android.os.AsyncTask;
import android.util.Log;

import org.pctechtips.netdroid.response.DnsAsyncResponse;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

import java.lang.ref.WeakReference;

public class DnsLookupAsyncTask extends AsyncTask<String, Void, String> {

    private final WeakReference<DnsAsyncResponse> delegate;

    /**
     * Constructor to set the delegate
     *
     * @param delegate Called when the DNS lookup finishes
     */
    public DnsLookupAsyncTask(DnsAsyncResponse delegate) {
        this.delegate = new WeakReference<>(delegate);
    }

    /**
     * Performs the appropriate lookup for specified record type
     *
     * @param params
     * @return DNS answer
     */
    @Override
    protected String doInBackground(String... params) {
        String domain = params[0];
        int recordType = Integer.parseInt(params[1]);
        Record[] records;

        try {
            records = new Lookup("google.com", Type.ANY).run();
            Log.d("DNS", records.toString());
            if (records == null) {
                return "No records found.";
            }

            StringBuilder answer = new StringBuilder();
            for (Record record : records) {
                String rClass = this.parseRecordClass(record.getDClass());
                answer.append(String.format("%s\t\t\t\t%s\t\t\t\t%s\t\t\t\t%s%n%n", record.getName(), record.getTTL(), rClass, record.rdataToString()));
            }

            return answer.toString();
        } catch (TextParseException e) {
            return "Error performing lookup!";
        }
    }

    /**
     * Determines the string representation of the DNS record class
     *
     * @param recordClass Numeric record class
     * @return Human readable record class
     */
    private String parseRecordClass(int recordClass) {
        switch (recordClass) {
            case 1:
                return "IN";
            case 2:
                return "CS";
            case 3:
                return "CH";
            case 4:
                return "HS";
            default:
                return "IN";
        }
    }

    /**
     * Calls the delegate when the DNS lookup has finished
     *
     * @param result DNS answer
     */
    @Override
    protected void onPostExecute(String result) {
        DnsAsyncResponse activity = delegate.get();
        if (activity != null) {
            activity.processFinish(result);
        }
    }
}
1

There are 1 best solutions below

0
Kenny Ingle On

Android Oreo made a change that prevents looking up dns servers. https://developer.android.com/about/versions/oreo/android-8.0-changes.html#o-pri

See: https://stackoverflow.com/a/48973823/5420880 for more details and workarounds.

As a specific workaround for dnsjava, you can supply a comma-separated list of dns servers via the dns.server system property.