如何禁用DNS java的递归查询?

时间:2018-06-11 10:52:15

标签: dnsjava

我正在使用dnsjava进行查询,并且程序执行了2次查询,第一次查询是必要的并返回所需结果(此结果仅查看我是否捕获UDP包,而不是我程序的结果),之后,它自动执行第二次查询并返回错误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 个答案:

答案 0 :(得分:1)

您只需要在查询标题中取消设置Recursion desired标志即可。

假设您有一个具有区域bob.和以下条目的DNS服务器:

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

代码

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);
}

产生:

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