minidns

  1commit d5cca3b3a48cfaed3008050fcab9574d97d771cc
  2Author: Rene Treffer <treffer@measite.de>
  3Date:   Wed Apr 2 22:45:57 2014 +0200
  4
  5    Try various ways to get the dns settings
  6
  7diff --git a/src/de/measite/minidns/Client.java b/src/de/measite/minidns/Client.java
  8index c32ec3a1f..beb461bec 100644
  9--- a/src/de/measite/minidns/Client.java
 10+++ b/src/de/measite/minidns/Client.java
 11@@ -4,14 +4,18 @@ import java.io.IOException;
 12 import java.io.InputStream;
 13 import java.io.InputStreamReader;
 14 import java.io.LineNumberReader;
 15+import java.lang.reflect.Method;
 16 import java.net.DatagramPacket;
 17 import java.net.DatagramSocket;
 18 import java.net.InetAddress;
 19 import java.security.NoSuchAlgorithmException;
 20 import java.security.SecureRandom;
 21+import java.util.ArrayList;
 22+import java.util.Arrays;
 23 import java.util.HashSet;
 24 import java.util.Random;
 25 
 26+import android.util.Log;
 27 import de.measite.minidns.Record.CLASS;
 28 import de.measite.minidns.Record.TYPE;
 29 
 30@@ -130,6 +134,33 @@ public class Client {
 31      * @return The server array.
 32      */
 33     public String[] findDNS() {
 34+        String[] result = findDNSByReflection();
 35+        if (result != null) {
 36+            Log.d("minidns/client",
 37+                "Got DNS servers via reflection: " + Arrays.toString(result));
 38+            return result;
 39+        }
 40+
 41+        result = findDNSByExec();
 42+        if (result != null) {
 43+            Log.d("minidns/client",
 44+                "Got DNS servers via exec: " + Arrays.toString(result));
 45+            return result;
 46+        }
 47+
 48+        // fallback for ipv4 and ipv6 connectivity
 49+        // see https://developers.google.com/speed/public-dns/docs/using
 50+        Log.d("minidns/client",
 51+            "No DNS found? Using fallback [8.8.8.8, [2001:4860:4860::8888]]");
 52+
 53+        return new String[]{"8.8.8.8", "[2001:4860:4860::8888]"};
 54+    }
 55+
 56+    /**
 57+     * Try to retrieve the list of dns server by executing getprop.
 58+     * @return Array of servers, or null on failure.
 59+     */
 60+    protected String[] findDNSByExec() {
 61         try {
 62             Process process = Runtime.getRuntime().exec("getprop");
 63             InputStream inputStream = process.getInputStream();
 64@@ -146,7 +177,19 @@ public class Client {
 65                 String value = line.substring(split + 4, line.length() - 1);
 66                 if (property.endsWith(".dns") || property.endsWith(".dns1") ||
 67                     property.endsWith(".dns2") || property.endsWith(".dns3") ||
 68-		    property.endsWith(".dns4")) {
 69+                    property.endsWith(".dns4")) {
 70+
 71+                    // normalize the address
 72+
 73+                    InetAddress ip = InetAddress.getByName(value);
 74+
 75+                    if (ip == null) continue;
 76+
 77+                    value = ip.getHostAddress();
 78+
 79+                    if (value == null) continue;
 80+                    if (value.length() == 0) continue;
 81+
 82                     server.add(value);
 83                 }
 84             }
 85@@ -158,4 +201,50 @@ public class Client {
 86         }
 87         return null;
 88     }
 89+
 90+    /**
 91+     * Try to retrieve the list of dns server by calling SystemProperties.
 92+     * @return Array of servers, or null on failure.
 93+     */
 94+    protected String[] findDNSByReflection() {
 95+        try {
 96+            Class<?> SystemProperties =
 97+                    Class.forName("android.os.SystemProperties");
 98+            Method method = SystemProperties.getMethod("get",
 99+                    new Class[] { String.class });
100+
101+            ArrayList<String> servers = new ArrayList<String>(5);
102+
103+            for (String propKey : new String[] {
104+                    "net.dns1", "net.dns2", "net.dns3", "net.dns4"}) {
105+
106+                String value = (String)method.invoke(null, propKey);
107+
108+                if (value == null) continue;
109+                if (value.length() == 0) continue;
110+                if (servers.contains(value)) continue;
111+
112+                InetAddress ip = InetAddress.getByName(value);
113+
114+                if (ip == null) continue;
115+
116+                value = ip.getHostAddress();
117+
118+                if (value == null) continue;
119+                if (value.length() == 0) continue;
120+                if (servers.contains(value)) continue;
121+
122+                servers.add(value);
123+            }
124+
125+            if (servers.size() > 0) {
126+                return servers.toArray(new String[servers.size()]);
127+            }
128+        } catch (Exception e) {
129+            // we might trigger some problems this way
130+            e.printStackTrace();
131+        }
132+        return null;
133+    }
134+
135 }