prefer dns servers from networsk with the default route

This commit is contained in:
Daniel Gultsch 2016-01-26 22:46:00 +01:00
parent c416948f8b
commit 82870b27ed

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.LinkProperties; import android.net.LinkProperties;
import android.net.Network; import android.net.Network;
import android.net.RouteInfo;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
@ -75,15 +76,29 @@ public class DNSHelper {
for(int i = 0; i < networks.length; ++i) { for(int i = 0; i < networks.length; ++i) {
LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]); LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]);
if (linkProperties != null) { if (linkProperties != null) {
if (hasDefaultRoute(linkProperties)) {
servers.addAll(0, linkProperties.getDnsServers());
} else {
servers.addAll(linkProperties.getDnsServers()); servers.addAll(linkProperties.getDnsServers());
} }
} }
}
if (servers.size() > 0) { if (servers.size() > 0) {
Log.d(Config.LOGTAG, "used lollipop variant to discover dns servers in " + networks.length + " networks"); Log.d(Config.LOGTAG, "used lollipop variant to discover dns servers in " + networks.length + " networks");
} }
return servers.size() > 0 ? servers : getDnsServersPreLollipop(); return servers.size() > 0 ? servers : getDnsServersPreLollipop();
} }
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean hasDefaultRoute(LinkProperties linkProperties) {
for(RouteInfo route: linkProperties.getRoutes()) {
if (route.isDefaultRoute()) {
return true;
}
}
return false;
}
private static List<InetAddress> getDnsServersPreLollipop() { private static List<InetAddress> getDnsServersPreLollipop() {
List<InetAddress> servers = new ArrayList<>(); List<InetAddress> servers = new ArrayList<>();
String[] dns = client.findDNS(); String[] dns = client.findDNS();