A tool named dnsjava is an implementation of the DNS protocol in Java. It supports all of the common record types. dnsjava provides both high and low level access to the resolver. The high level functions perform queries for records of a given name, type, and class, and return an array of records. A cache is used to reduce the number of queries sent. The low level functions allow direct manipulation of dns messages and records, as well as allowing additional resolver properties to be set. A 'dig' clone, dynamic update program, and basic server are also included. Extensions to the DNS protocol are supported as well (DNSSEC, TSIG, EDNS0).
dnsjava provides several command line programs, which are documented here.
- dig:
A clone of dig (as distributed with BIND)
dig @server [-x] name type [class] [-p port] [-k name/secret] [-t] \
[-i] [-e n] [-d]
-x : reverse lookup, name must be a dotted quad
-k : use TSIG transaction security
-t : use TCP by default
-i : ignore truncation errors
-e n: Use EDNS level n (only 0 is defined)
-d : Set the DNSSEC OK bit
- update:
A dynamic update client with some extra functionality. This can be
used either interactively or by specifying a file containing commands
to be executed. Running 'help' lists all other commands.
update [file]
- jnamed:
A basic authoritative only (non-caching, non-recursive) server. It's
not very good, but it's also a whole lot better than it used to be.
The config file (jnamed.conf by default) supports the following
directives:
primary <zonename> <masterfile>
secondary <zonename> <IP address>
cache <hintfile>
key [algorithm] <name> <base 64 encoded secret>
address <IP address>
port <port number>
If no addresses are specified, jnamed will listen on all addresses,
using a wildcard socket. If no ports are specified, jnamed will
listen on port 53.
The following is an example:
primary internal /etc/namedb/internal.db
secondary xbill.org 127.0.0.1
cache /etc/namedb/cache.db
key xbill.org 1234
address 127.0.0.1
port 12345
To run:
jnamed [config_file]
jnamed should not be used for production, and should probably
not be used for testing. If the above documentation is not enough,
please do not ask for more, because it really should not be used.
- lookup:
A simple program that looks up records associated with names.
If no type is specified, address lookups are done.
lookup [-t type] name ...
For examples of API usage:
All of these examples are code fragments. Code using these
fragments should check exceptions when appropriate, and should:
import org.xbill.DNS.*;
Get the IP address associated with a name:
InetAddress addr = Address.getByName("www.dnsjava.org");
Get the MX target and preference of a name:
Record [] records = new Lookup("dnsjava.org", Type.MX).run();
for (int i = 0; i < records.length; i++) {
MXRecord mx = (MXRecord) records[i];
System.out.println("Host " + mx.getTarget() + " has preference ", mx.getPriority());
}
Query a remote name server for its version:
Lookup l = new Lookup("version.bind.", Type.TXT, DClass.CH);
l.setResolver(new SimpleResolver(args[0]));
l.run();
if (l.getResult() == Lookup.SUCCESSFUL)
System.out.println(l.getAnswers()[0].rdataToString());
Transfer a zone from a server and print it:
ZoneTransferIn xfr = ZoneTransferIn.newAXFR(new Name("dnsjava.org"), "204.152.186.163", null);
List records = xfr.run();
for (Iterator it = records.iterator(); it.hasNext(); )
System.out.println(it.next());
Use DNS dynamic update to set the address of a host
to a value specified on the command line:
Name zone = Name.fromString("dyn.test.example.");
Name host = Name.fromString("host", zone);
Update update = new Update(zone);
update.replace(host, Type.A, 3600, args[0]);
Resolver res = new SimpleResolver("10.0.0.1");
res.setTSIGKey(new TSIG(host, base64.fromString("1234")));
res.setTCP(true);
Message response = res.send(update);
Manipulate domain names:
Name n = Name.fromString("www.dnsjava.org");
Name o = Name.fromString("dnsjava.org");
System.out.println(n.subdomain(o)); // True
System.out.println(n.compareTo(o)); // > 0
Name rel = n.relativize(o); // the relative name 'www'
Name n2 = Name.concatenate(rel, o);
System.out.println(n2.equals(n)); // True
// www
// dnsjava
// org
for (int i = 0; i < n.labels(); i++)
System.out.println(n.getLabelString(i));
Officail Site:
dnsjava