Get DNS SRV record using JNDI

2019-07-03 23:19发布

I am trying to get SRV records from a DNS server using JNDI.

Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns://dns.server.com");
DirContext ctx = new InitialDirContext(env);
Attributes attributes = ctx.getAttributes("_sip._udp", new String [] { "SRV" });
return attributes;

But when trying to get attributes I get the following exception

DNS error [Root exception is java.net.PortUnreachableException: ICMP Port Unreachable]; remaining name '_sip._udp'

I have verified host -t srv _sip._udp.server.com returns valid SRV record.

Any reason as why this might happen?

标签: java dns jndi srv
1条回答
手持菜刀,她持情操
2楼-- · 2019-07-03 23:50

One of the following: dns.server.com not a valid DNS server, does not have a SRV record for _sip._udp, the DNS service does not respond on port 53 (standard DNS port) or your Java code is wrong.

To diagnose DNS server troubles, you could try host -t SRV _sip._udp.server.com dns.server.com or dig @dns.server.com -t SRV _sip._udp.server.com to confirm that the server works.

If host or dig return the expected entry, try the following changes to your code:

Change:

env.put("java.naming.provider.url", "dns://dns.server.com");

To:

env.put("java.naming.provider.url", "dns:");

(i.e., just use your OS's standard DNS resolution)

Change:

ctx.getAttributes("_sip._udp", new String [] { "SRV" });

To:

ctx.getAttributes("_sip._udp.domain.com", new String [] { "SRV" });

as SRV record require a domain name to search, so you'd end up with:

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("_sip._udp.domain.com", new String [] { "SRV" });
return attributes;
查看更多
登录 后发表回答