prefer IPv4 DNS servers

some devices might have problems contacting the IPv6 DNS server while in sleep mode
This commit is contained in:
Daniel Gultsch 2016-05-12 21:39:47 +02:00
parent f4369b29ae
commit cbdb413613

View file

@ -12,6 +12,7 @@ import android.os.Parcelable;
import android.util.Log; import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -77,9 +78,9 @@ public class DNSHelper {
LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]);
if (linkProperties != null) { if (linkProperties != null) {
if (hasDefaultRoute(linkProperties)) { if (hasDefaultRoute(linkProperties)) {
servers.addAll(0, linkProperties.getDnsServers()); servers.addAll(0, getIPv4First(linkProperties.getDnsServers()));
} else { } else {
servers.addAll(linkProperties.getDnsServers()); servers.addAll(getIPv4First(linkProperties.getDnsServers()));
} }
} }
} }
@ -89,6 +90,18 @@ public class DNSHelper {
return servers.size() > 0 ? servers : getDnsServersPreLollipop(); return servers.size() > 0 ? servers : getDnsServersPreLollipop();
} }
private static List<InetAddress> getIPv4First(List<InetAddress> in) {
List<InetAddress> out = new ArrayList<>();
for(InetAddress addr : in) {
if (addr instanceof Inet4Address) {
out.add(0, addr);
} else {
out.add(addr);
}
}
return out;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean hasDefaultRoute(LinkProperties linkProperties) { private static boolean hasDefaultRoute(LinkProperties linkProperties) {
for(RouteInfo route: linkProperties.getRoutes()) { for(RouteInfo route: linkProperties.getRoutes()) {
@ -155,7 +168,7 @@ public class DNSHelper {
public static Bundle queryDNS(String host, InetAddress dnsServer) { public static Bundle queryDNS(String host, InetAddress dnsServer) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
try { try {
client.setTimeout(Config.PING_TIMEOUT * 1000); client.setTimeout(Config.SOCKET_TIMEOUT * 1000);
final String qname = "_xmpp-client._tcp." + host; final String qname = "_xmpp-client._tcp." + host;
final String tlsQname = "_xmpps-client._tcp." + host; final String tlsQname = "_xmpps-client._tcp." + host;
Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host); Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);