使用JNDI获取DNS SRV记录 [英] Get DNS SRV record using JNDI
问题描述
我正在尝试使用JNDI从DNS服务器获取SRV记录.
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错误[根异常为 java.net.PortUnreachableException: ICMP端口不可达];剩余的名字 '_sip._udp'
DNS error [Root exception is java.net.PortUnreachableException: ICMP Port Unreachable]; remaining name '_sip._udp'
我已验证主机-t srv _sip._udp.server.com返回有效的SRV记录.
I have verified host -t srv _sip._udp.server.com returns valid SRV record.
为什么会发生这种情况?
Any reason as why this might happen?
推荐答案
以下之一:dns.server.com
不是有效的DNS服务器,没有_sip._udp
的SRV记录,DNS服务未在端口上响应53(标准DNS端口)或您的Java代码错误.
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.
要诊断DNS服务器故障,可以尝试host -t SRV _sip._udp.server.com dns.server.com
或dig @dns.server.com -t SRV _sip._udp.server.com
确认服务器正常工作.
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.
如果host
或dig
返回期望的条目,请尝试对代码进行以下更改:
If host
or dig
return the expected entry, try the following changes to your code:
更改:
env.put("java.naming.provider.url", "dns://dns.server.com");
收件人:
env.put("java.naming.provider.url", "dns:");
(即,仅使用您操作系统的标准DNS解析度)
(i.e., just use your OS's standard DNS resolution)
更改:
ctx.getAttributes("_sip._udp", new String [] { "SRV" });
收件人:
ctx.getAttributes("_sip._udp.domain.com", new String [] { "SRV" });
因为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;
这篇关于使用JNDI获取DNS SRV记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!