dnsjava是DNS的一个Java实现。支持所有定义的记录类型包括DNSSEC类型,和未知类型。它还可以用于查询,区域传输,动态更新。dnsjava还包含一个客户端使用的缓存,一个小型DNS服务器。它支持TSIG身份验证的消息,部分DNSSEC验证和EDNS0。
dnsjava提供的DNS高和低级别的访问功能。高级别的功能进行查询,对于一个给定的名称,类型和类的记录,然后返回答案,或者失败原因。也有相似的功能,对于与InetAddress类。缓存是用来减少发送DNS查询的数量。低级别的功能允许直接操纵的DNS的消息和记录,以及允许额外解析器属性进行设置。除dnsjava外,还有一些其他的java实现如:dns4j,dnsjnio,jdnss,eagledns等
maven依赖:http://mvnrepository.com/artifact/dnsjava/dnsjava
dnsjava
dnsjava
2.1.7
实例参考:
package cn.slimsmart.java.demo.dnsjava;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.List;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.DClass;
import org.xbill.DNS.Flags;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Message;
import org.xbill.DNS.Name;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.Section;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TSIG;
import org.xbill.DNS.Type;
import org.xbill.DNS.Update;
import org.xbill.DNS.ZoneTransferIn;
public class DNSJavaTest {
public static void main(String[] args) throws Exception {
// query();
//transferZone();
addRR();
}
// 查询
static void query() throws IOException {
Resolver resolver = new SimpleResolver("192.168.36.54");
resolver.setPort(53);
Lookup lookup = new Lookup("www.test.com", Type.A);
lookup.setResolver(resolver);
lookup.run();
if (lookup.getResult() == Lookup.SUCCESSFUL) {
System.out.println(lookup.getAnswers()[0].rdataToString());
}
}
static void transferZone() throws Exception {
ZoneTransferIn xfr = ZoneTransferIn.newAXFR(new Name("test.com."), "192.168.36.54", null);
List records = xfr.run();
Message response = new Message();
response.getHeader().setFlag(Flags.AA);
response.getHeader().setFlag(Flags.QR);
// response.addRecord(query.getQuestion(),Section.QUESTION);
Iterator it = records.iterator();
while (it.hasNext()) {
response.addRecord((Record) it.next(), Section.ANSWER);
}
System.out.println(response);
}
static void addRR() throws Exception {
Name zone = Name.fromString("test.com.");
Name host = Name.fromString("host", zone);
Update update = new Update(zone, DClass.IN);
Record record = new ARecord(host, DClass.IN, 3600, InetAddress.getByName("192.0.0.2"));
update.add(record);
Resolver resolver = new SimpleResolver("192.168.36.54");
resolver.setPort(53);
TSIG tsig = new TSIG("test_key", "epYaIl5VMJGRSG4WMeFW5g==");
resolver.setTSIGKey(tsig);
resolver.setTCP(true);
Message response = resolver.send(update);
System.out.println(response);
}
}检查添加结果:
$ dig @192.168.36.54 host.test.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> @192.168.36.54 host.test.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 1
;; QUESTION SECTION:
;host.test.com. IN A
;; ANSWER SECTION:
host.test.com. 3600 IN A 192.0.0.2
host.test.com. 3600 IN A 192.0.0.1
;; AUTHORITY SECTION:
test.com. 86400 IN NS dns.test.com.
;; ADDITIONAL SECTION:
dns.test.com. 86400 IN A 192.168.36.189
;; Query time: 2 msec
;; SERVER: 192.168.36.54#53(192.168.36.54)
;; WHEN: Sun Apr 19 00:22:41 2015
;; MSG SIZE rcvd: 97