From cbdb4136136726d572b8bc521fc910f4dc31f00f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 12 May 2016 21:39:47 +0200 Subject: prefer IPv4 DNS servers some devices might have problems contacting the IPv6 DNS server while in sleep mode --- .../java/eu/siacs/conversations/utils/DNSHelper.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/utils/DNSHelper.java b/src/main/java/eu/siacs/conversations/utils/DNSHelper.java index 306d50c2..b96d3c25 100644 --- a/src/main/java/eu/siacs/conversations/utils/DNSHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/DNSHelper.java @@ -12,6 +12,7 @@ import android.os.Parcelable; import android.util.Log; import java.io.IOException; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.SocketTimeoutException; import java.net.UnknownHostException; @@ -77,9 +78,9 @@ public class DNSHelper { LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); if (linkProperties != null) { if (hasDefaultRoute(linkProperties)) { - servers.addAll(0, linkProperties.getDnsServers()); + servers.addAll(0, getIPv4First(linkProperties.getDnsServers())); } else { - servers.addAll(linkProperties.getDnsServers()); + servers.addAll(getIPv4First(linkProperties.getDnsServers())); } } } @@ -89,6 +90,18 @@ public class DNSHelper { return servers.size() > 0 ? servers : getDnsServersPreLollipop(); } + private static List getIPv4First(List in) { + List 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) private static boolean hasDefaultRoute(LinkProperties linkProperties) { for(RouteInfo route: linkProperties.getRoutes()) { @@ -155,7 +168,7 @@ public class DNSHelper { public static Bundle queryDNS(String host, InetAddress dnsServer) { Bundle bundle = new Bundle(); try { - client.setTimeout(Config.PING_TIMEOUT * 1000); + client.setTimeout(Config.SOCKET_TIMEOUT * 1000); final String qname = "_xmpp-client._tcp." + host; final String tlsQname = "_xmpps-client._tcp." + host; Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host); -- cgit v1.2.3