How to disable recursive query of DNS java?

806 Views Asked by At

I'm doing query using dnsjava, and program did query 2 times, the first query is necessary and return desired result (this result only see if I capture UDP package, not in result of my program), and after that, it automatically do the second query and return error Host Not Found for my program tcpdump_picture

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        final Cache cache = new Cache(DClass.IN);           
        SimpleResolver resolver = new SimpleResolver("10.212.9.108");           
        Name name = new Name("8491698766.6.msisdn.sub.cs");
        Lookup.setDefaultResolver(resolver);
        Lookup.setDefaultCache(new Cache(), DClass.IN);

        Lookup l = new Lookup(name);
        l.setResolver(resolver);

        l.run();
        Record[] records = l.getAnswers();
        System.out.println("Error str: " + l.getErrorString());
    //  System.out.println("Record length: " + records.length);
    //  InetAddress[] array = new InetAddress[records.length];
        if(records != null) {
            for(int i=0; i<records.length; i++) {
                Record record = records[i];
                if(record instanceof ARecord) {
                    ARecord a = (ARecord)record;
                    System.out.println("ARecord: " + a.getAddress().getHostName() + ";" + a.getAddress().getHostAddress());
                }else {
                    CNAMERecord cname = (CNAMERecord)record;
                    System.out.println("CnameRecord: " + cname.toString());
                }
            }
        }else {
            System.out.println("Record null");
        }
    }catch(Exception e) {
        e.printStackTrace();
    }
}

}

1

There are 1 best solutions below

0
On

You just need to unset the Recursion desired Flag in the query header.

Assuming you have a DNS server with a zone bob. and the following entries:

peter.bob.              38400   IN      NS      ns2.bobsen-technology.org.
peter.bob.              38400   IN      NS      ns.klaus.de.

the code

Resolver resolver = new SimpleResolver("localhost");

Record question = Record.newRecord(new Name("peter.bob."), Type.NS, DClass.IN);

Message query = Message.newQuery(question);

query.getHeader().unsetFlag(Flags.RD);

Message response = resolver.send(query);

for (RRset rRset : response.getSectionRRsets(Section.AUTHORITY)) {
    System.out.println(rRset);
}

produces:

{ peter.bob. 38400 IN NS [ns2.bobsen-technology.org.] [ns.klaus.de.] }