JNDI DNS lookup with partial domain name
For example if I have the following SRV record defined in my DNS config
_dev._tcp IN SRV 0 0 8400 dev.server.com.
I can execute the following command
host -lt SRV server.com
And it gives me complete list of SRV records in the dns server(server.com). I开发者_JAVA百科f I want to do the same thing using JNDI lookup
Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
DirContext ctx = new InitialDirContext(env);
Attributes attributes = ctx.getAttributes("server.com", new String [] { "SRV" });
return attributes;
The above code is not returning any attributes. If I change the penultimate line in the above code to this,
Attributes attributes = ctx.getAttributes("_dev._tcp.server.com", new String [] { "SRV" });
it works.
But the problem is I don't know the complete domain name in prior and I have to lookup the SRV record to find the complete domain name.
Any ideas as how to do this?
The -l
parameter to /usr/bin/host
instructs a DNS zone transfer. Unlike a conventional DNS query, zone transfers work over TCP.
Here's what host -lt SRV server.com
does:
- Finds out the name servers of
server.com
. - Connects to the first listed name server's port 53 with TCP.
- Initiates a zone transfer.
- Filters out the results in accordance to its
-t srv
filter.
You need to initiate a zone transfer via JNDI and filter out the results for what you are looking for. The method to invoke a DNS zone transfer in DnsContext
is list()
.
NamingEnumeration<NameClassPair> names = ctx.list("server.com");
IMHO, JNDI has a terrible interface. It is designed to be very generic to be all naming and directory services and both mechanical and technical mismatch between the interface and DNS is simply frustrating.
If you are looking for a more pragmatic, easier to use and full features DNS library, have a look at dnsjava project at http://www.dnsjava.org
Also keep in mind not every DNS server allows zone transfers from untrusted hosts. Even LAN DNS servers won't allow zone transfers by default nowadays.
精彩评论