Get DNS SRV record using JNDI

亡梦爱人 提交于 2019-12-23 17:00:44

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/6473320/get-dns-srv-record-using-jndi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!