如何从IP解析DNS名称?

huangapple go评论98阅读模式
英文:

How to resolve the DNS name from IP?

问题

以下是翻译好的内容:

我想从客户端的 IP 地址解析域名。为了测试,我使用了以下代码来解析 stackoverflow。但是,当我使用 IP 地址来解析域名时,我仍然只能得到 IP 地址作为结果。

String ip = InetAddress.getByName("www.stackoverflow.com").getHostAddress();
System.out.println(ip);
System.out.println(InetAddress.getByName(ip).getHostName());
System.out.println(InetAddress.getByName(ip).getCanonicalHostName());
System.out.println(InetAddress.getByName(ip).getHostAddress());

结果:

151.101.129.69
151.101.129.69
151.101.129.69
151.101.129.69

如果我只有给定的 IP 151.101.129.69,我如何获取域名(stackoverflow / www.stackoverflow.com)?

英文:

I want to resolve the domain name from a clients IP address. For a test, I used stackoverflow as follows. But when I use the ip to resolve the domain, I still get only the IP as result.

	String ip = InetAddress.getByName("www.stackoverflow.com").getHostAddress();
	System.out.println(ip);
	System.out.println(InetAddress.getByName(ip).getHostName());
	System.out.println(InetAddress.getByName(ip).getCanonicalHostName());
	System.out.println(InetAddress.getByName(ip).getHostAddress());

Result:

151.101.129.69
151.101.129.69
151.101.129.69
151.101.129.69

How can I get the domain name (stackoverflow / www.stackoverflow.com) if I'd only have the ip 151.101.129.69 given?

答案1

得分: 2

我尝试在“www.stackoverflow.com”上进行了正向和逆向查找,很明显,stackoverflow的管理员没有为这个IP设置DNS PTR记录,这会使得Java无法进行逆向查找该站点的DNS名称。

(另一种逆向查找失败的情况是没有任何DNS名称映射到给定的IP地址。)

这不是一个特定于Java的问题。无论使用哪种语言,您都会得到相同的行为。实际上,这是DNS设计的结果,以及DNS在实际中的使用方式。

对此没有真正的解决方法。您需要编写应用程序代码来允许处理无法解析为DNS地址的IP地址。


记录一下,以下是我使用“dig”工具得到的内容(出于安全原因,某些细节已被隐藏)。

$ dig www.stackoverflow.com

; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> www.stackoverflow.com
;; 全局选项: +cmd
;; 获得答案:
;; ->>HEADER<<- 操作码: 查询, 状态: NOERROR, id: 52578
;; 标志: qr rd ra; 查询: 1, 回答: 5, 授权: 0, 附加信息: 1

;; OPT 伪段:
; EDNS: 版本: 0, 标志:; udp: 4096
; COOKIE: zzzzzz (good)
;; 问题部分:
;www.stackoverflow.com.		IN	A

;; 回答部分:
www.stackoverflow.com.	3600	IN	CNAME	stackoverflow.com.
stackoverflow.com.	2760	IN	A	151.101.129.69
stackoverflow.com.	2760	IN	A	151.101.193.69
stackoverflow.com.	2760	IN	A	151.101.1.69
stackoverflow.com.	2760	IN	A	151.101.65.69

;; 查询时间: 22 毫秒
;; 服务器: xx.xx.xx.xx
;; 时间: 2020年10月07日周三19:49:12澳大利亚东部夏令时间
;; 接收到的消息大小: 156

$ dig -x 151.101.129.69

; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> -x 151.101.129.69
;; 全局选项: +cmd
;; 获得答案:
;; ->>HEADER<<- 操作码: 查询, 状态: NXDOMAIN, id: 43513
;; 标志: qr rd ra; 查询: 1, 回答: 0, 授权: 1, 附加信息: 1

;; OPT 伪段:
; EDNS: 版本: 0, 标志:; udp: 4096
; COOKIE: zzzzzz (good)
;; 问题部分:
;69.129.101.151.in-addr.arpa.	IN	PTR

;; 授权部分:
151.in-addr.arpa.	3600	IN	SOA	pri.authdns.ripe.net. dns.ripe.net. 1586416382 3600 600 864000 3600

;; 查询时间: 34 毫秒
;; 服务器: xx.xx.xx.xx
;; 时间: 2020年10月07日周三19:49:26澳大利亚东部夏令时间
;; 接收到的消息大小: 168
英文:

I tried a forward and then reverse lookup on "www.stackoverflow.com" and it is clear that the admins for stackoverflow have not set up the DNS PTR record(s) for this IP that would allow Java to reverse lookup the site's DNS name(s).

(Another scenario where reverse lookup will fail is when no DNS name maps to a given IP address.)

This is not a Java specific problem. You will get the same behavior in any language. It is actually a consequence of DNS's design, and the way that DNS is used in practice.

There is no real solution to this. You need to write your application code to allow for IP addresses that you cannot resolve to a DNS address.


For the record, this is what I got using the dig tool. (Some details obscured for security reasons).

$ dig www.stackoverflow.com

; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> www.stackoverflow.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52578
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: zzzzzz (good)
;; QUESTION SECTION:
;www.stackoverflow.com.		IN	A

;; ANSWER SECTION:
www.stackoverflow.com.	3600	IN	CNAME	stackoverflow.com.
stackoverflow.com.	2760	IN	A	151.101.129.69
stackoverflow.com.	2760	IN	A	151.101.193.69
stackoverflow.com.	2760	IN	A	151.101.1.69
stackoverflow.com.	2760	IN	A	151.101.65.69

;; Query time: 22 msec
;; SERVER: xx.xx.xx.xx
;; WHEN: Wed Oct 07 19:49:12 AEDT 2020
;; MSG SIZE  rcvd: 156

$ dig -x 151.101.129.69

; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> -x 151.101.129.69
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 43513
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: zzzzzz (good)
;; QUESTION SECTION:
;69.129.101.151.in-addr.arpa.	IN	PTR

;; AUTHORITY SECTION:
151.in-addr.arpa.	3600	IN	SOA	pri.authdns.ripe.net. dns.ripe.net. 1586416382 3600 600 864000 3600

;; Query time: 34 msec
;; SERVER: xx.xx.xx.xx
;; WHEN: Wed Oct 07 19:49:26 AEDT 2020
;; MSG SIZE  rcvd: 168

huangapple
  • 本文由 发表于 2020年10月7日 16:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64240155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定