如何从Java程序中找到Android中的DNS服务器? [英] How do I find the DNS servers in Android from a Java program?

查看:137
本文介绍了如何从Java程序中找到Android中的DNS服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.net.InetAddress.GetByName(String host)方法只能返回 A 记录,以便查找其他记录类型我需要使用 dnsjava 库发送DNS查询。

The java.net.InetAddress.GetByName(String host) method can only return A records so to lookup other record types I need to be able to send DNS queries using the dnsjava library.

然而,通常依赖于可以解析 /etc/resolv.conf 或类似的内容,找到DNS服务器地址,并且在Android上无效。

However that normally relies on being able to parse /etc/resolv.conf or similar to find the DNS server addresses and that doesn't work on Android.

Android上的当前DNS设置显然只能通过使用 getprop 命令从shell中获取。

The current DNS settings on Android can apparently only be obtained from within a shell by using the getprop command.

任何人都可以告诉我如何从Java中获取这些设置,而不是通过使用 Runtime.exec()生成shell,并解析来自 getprop ?

Can anyone tell me how to get those settings from Java other than by spawning a shell with Runtime.exec() and parsing the output from getprop?

推荐答案

DNS协议不是那么复杂 - 不能使用原始套接字(TCP或UDP)进行DNS访问?在快速查看dnsjava doco后,似乎提供了低级别的DNS支持来协助这一点。

The DNS protocol is not that complex - can't you just do the DNS accesses using raw sockets (either TCP or UDP)? After a quick look at the dnsjava doco it seems to provide low level DNS support to assist with this.

另一个可能的方向是从dnsjava开始,以消除依赖在 /etc/resolv.conf 上。我会考虑在启动脚本中使用getprop来设置JVM中的属性,或者在应用程序的目录中创建一个本地化的 resolv.conf 文件,从中可以阅读信息需要。换句话说,使用getprop将信息注入到JVM中,而不是在JVM进行时尝试将其拉入。确定创建dnsjava可以直接使用的文件应该是可行的。

The other possible direction is, starting with dnsjava, to remove the dependence on /etc/resolv.conf. I would think about using getprop in your launch script to set properties in the JVM, or to create a localized resolv.conf file in your app's directory from which you can read the information needed. In other words, use getprop to inject information into the JVM instead of attempting to pull it in once the JVM is going. Surely creating a file that dnsjava can use directly should be doable.

它看起来像 android.net.ConnectivityManager 将使用 getAllNetworkInfo()。然后使用 android.net.NetworkUtils.runDhcp()获取任何给定网络接口的 DhcpInfo - DhcpInfo 结构具有该接口的dns1和dns2的IP地址。令人惊讶的是,DNS是int,因此仅仅意味着IP4。

It looks like android.net.ConnectivityManager will deliver you an array of NetworkInfo's using getAllNetworkInfo(). Then use android.net.NetworkUtils.runDhcp() to get a DhcpInfo for any given network interface - the DhcpInfo structure has the IP address for dns1 and dns2 for that interface. Surprised that the DNS's are int, therefore implying IP4 only, though.

这篇关于如何从Java程序中找到Android中的DNS服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆