0

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
  • I've just fixed this problem by change source code of Cache class in dnsjava. In Cache class, just edit this function: public SetResponse addMessage(Message in) , comment on this: if ((type == qtype || qtype == Type.ANY)) //&& // name.equals(curname)) Commented Jun 12, 2018 at 7:39

1 Answer 1

3

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.] }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.