Using Java to get detailed DNS information

Not long ago I was curious about using Java to look up DNS information so I decided to put together a little DNSBL/RBL checker so I could experiment with Java DNS lookups and some PHP/Java communications. There isn't a lot of Java DNS stuff out there but it was easy to tell that the tool for this job is the DNSJava library.

We aren't just talking about resolving a name into an IP address or an IP into a name here, we are talking about being able to get all the other information that is stored in the DNS as well. If all you want to do is resolve names then Java has that built in. DNSJava lets you get at all the records for a domain name and as a bonus you could create your own DNS server with DNSJava if you wanted.

When working with a DNSBL you prepend an IP address to the domain for the DNSBL and then query based on that. For example the IP 127.0.0.2 is a test IP for most lists and if you wanted to use the Spamhaus combined SBL/XBL check (domain sbl-xbl.spamhaus.org) you would query on 2.0.0.127.sbl-xbl.spamhaus.org (notice the IP address is reversed). In this case the request will respond with a listing since 127.0.0.2 is a test address that is always listed. The simple fact that the address resolves is enough to know that the IP is listed but if you look at the TXT record of the listing you can find out more information. The following example will display the SBL/XLB record for the IP given in the ipAddress variable (make sure if you change it you reverse the IP address you are trying to query).

import org.xbill.DNS.*;

import java.util.Iterator;
import java.net.UnknownHostException;

/**
*/
public class ExampleOne
{
public static void main(String[] args) throws UnknownHostException, TextParseException
{
String ipAddress = "2.0.0.127";
String dnsblDomain = "sbl-xbl.spamhaus.org";

Lookup lookup = new Lookup(ipAddress + "." + dnsblDomain, Type.ANY);
Resolver resolver = new SimpleResolver();
lookup.setResolver(resolver);
lookup.setCache(null);
Record[] records = lookup.run();
if(lookup.getResult() == Lookup.SUCCESSFUL)
{
String responseMessage = null;
String listingType = null;
for (int i = 0; i < records.length; i++) { if(records[i] instanceof TXTRecord) { TXTRecord txt = (TXTRecord) records[i]; for(Iterator j = txt.getStrings().iterator(); j.hasNext();) { responseMessage += (String)j.next(); } } else if(records[i] instanceof ARecord) { listingType = ((ARecord)records[i]).getAddress().getHostAddress(); } } System.err.println("Found!"); System.err.println("Response Message: " + responseMessage); System.err.println("Listing Type: " + listingType); } else if(lookup.getResult() == Lookup.HOST_NOT_FOUND) { System.err.println("Not found."); } else { System.err.println("Error!"); } } } [/code] The TXT entry is just one of the extra entries you may be interested in. The DNSJava library supports a large number of other types that can all be found by looking at the DNSJava javadocs. For some more examples see the DNSJava example page.

Tags: ,

6 thoughts on “Using Java to get detailed DNS information

  1. Peter

    Is there a way to configure IP addresses of the DNS Name Servers that need to be used to resolve the DNS query?

  2. carson Post author

    I haven't had a reason to try but I can imagine that there might be some restriction. You may try to force the DNS lookups to using TCP if you can.

  3. Jeff Gonzales

    I figured out my issue. It was a permissions setting I needed to change. I can do some lookups but now I get a null pointer exception when trying to do a reverse lookup. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *