chrixm chrixm - 1 year ago 89
Java Question

Performing DNS "ANY" Lookup using Java JNDI

I am using Java JNDI to perform DNS lookups using the following basic syntax as per the SSCCE below, but I am trying to query all records using the "ANY" attribute:

import java.util.*;
import javax.naming.*;

public class SSCCE {
public static void main(String[] args) {
try {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
InitialDirContext idc = new InitialDirContext(p);

Attributes attrs = idc.getAttributes("", new String[] { "* *" });
Attribute attr = attrs.get("* *");

if (attr != null) {
for (int i = 0; i < attr.size(); i++) {
System.out.println("Found " + (String)attr.get(i));
else {
System.out.println("Found nothing");
catch (Exception e) {

My question is around being able to query a resource type of "ANY" which should return all the DNS resource records associated with a specific domain - example below using the "host" utility.

chrixm@puffy(:):~$ host -t ANY has SPF record "v=spf1 ~all" mail is handled by 10 mail is handled by 5 mail is handled by 1 mail is handled by 5 mail is handled by 10 name server name server name server name server

I have read, which says:

Superclass attribute identifiers are also defined. These may be useful when querying records using the DirContext.getAttributes() method. If an attribute name has "*" in place of a type name (or class name), it represents records of any type (or class). For example, the attribute identifier "IN " may be passed to the getAttributes() method to find all internet class records. The attribute identifier " *" represents records of any class or type.

However, Java JNDI doesn't understand a resource record of "*" or "* *" as the above code doesn't return any records (I am able to query "NS" or "SOA", etc individually) - has anyone had any experience of getting this working. I can of course query each individual resource type, but considering there is a valid record type of "ANY" as per RFC 1035 (Type ID 255) this seems very inefficient?

Answer Source

After examining the methods of the Attributes class I noticed a getAll() method. After further searching I was able to implement the following which now allows you to search using "*" as the record type and print all the records.

Attributes attrs = idc.getAttributes("", new String[] { "*" });
NamingEnumeration<?> ae = attrs.getAll();

while (ae.hasMore()) {
  Attribute attr = (Attribute);
  for (int i = 0; i < attr.size(); i++) {
    Object a = attr.get(i);
    if (a instanceof String) {
      System.out.println(attr.getID() + " " + a);
    else {
      System.out.println(attr.getID() + " NOT ASCII");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download