gpt4 book ai didi

android - 无法在 Android 上进行反向 DNS 查找

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:36 24 4
gpt4 key购买 nike

我有一台 Android 设备通过 IP 地址连接到 Wifi 网络。我在我的 Linux 计算机上对该 IP 地址进行了 NS 查找,并验证了该 IP 地址是否存在相应的主机名。

我有一段 Java 代码,当它在我的 Windows PC 上运行时可以很好地进行反向 DNS 查找(返回主机名):

String dnSuffix;
String ipAddress = "10.228.59.217";
InetAddress inetAddr;
try {
//inetAddr = InetAddress.getLocalHost();
inetAddr = InetAddress.getByName(ipAddress);
//System.out.println("inetAddr = " + inetAddr);
Log.v(LOG_TAG, "inetAddr = " + inetAddr);
if (inetAddr != null) {
dnSuffix = inetAddr.getHostName();
//System.out.println("dnSuffix is " + dnSuffix);
Log.v(LOG_TAG,"dnSuffix is " + dnSuffix);
}
} catch (UnknownHostException e) {
//System.out.println("Error getting DN suffix: " + e.getMessage());
Log.v(LOG_TAG,"Error getting DN suffix: " + e.getMessage());
}

dnSuffix 是 Windows 上预期的主机名。

但在 Android 上,它返回一个 IP 地址而不是主机名,这表明它失败了。

我拥有我认为在我的应用程序中需要的所有权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我找到了这个文档:

getCanonicalHostName

public String getCanonicalHostName()

Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. If there is a security manager, this method first calls its checkConnect method with the hostname and -1 as its arguments to see if the calling code is allowed to know the hostname for this IP address, i.e., to connect to the host. If the operation is not allowed, it will return the textual representation of the IP address.

Returns:

the fully qualified domain name for this IP address, or if the operation is not allowed by the security check, the textual representation of the IP address.

Since:

1.4

See Also:

SecurityManager.checkConnect(java.lang.String, int)

我也发现有人有类似的问题:

getCanonicalHostName returns an IP address

但没有解决。

任何人都可以提供任何帮助吗?

最佳答案

根据 2.3 源代码(已缩短),我真的不确定它能否正常工作

inetAddr = InetAddress.getByName(ipAddress);
getByName(numeric) -> getAllByName/Impl(numeric) -> lookupHostByName(numeric)
return bytesToInetAddresses(getaddrinfo(host), host)[0]

host 是数字字符串,然后在结果对象上调用 getHostName() 将返回主机(ip)

关于android - 无法在 Android 上进行反向 DNS 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9812260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com