From 8dade510a60e8d46a252bdf6dfeaeb137c651cfe Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 31 Jan 2016 19:34:24 +0100 Subject: [PATCH] Cache record records by InetAddress --- .../eu/faircode/netguard/SinkholeService.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index 816e3f99..0ffaccf1 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -103,7 +103,7 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS private Object subscriptionsChangedListener = null; private ParcelFileDescriptor vpn = null; - private static final Map mapRR = new HashMap<>(); + private static final Map mapRR = new HashMap<>(); private Map mapHostsBlocked = new HashMap<>(); private Map mapUidAllowed = new HashMap<>(); private Map>> mapUidIPFilters = new HashMap<>(); @@ -662,8 +662,16 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS // Get real name String dname = null; if (filter) { - ResourceRecord rr = reverseDNS(packet.daddr); - dname = (rr == null ? null : rr.QName); + ResourceRecord rr = null; + try { + rr = reverseDNS(InetAddress.getByName(packet.daddr)); + if (rr == null) + Log.w(TAG, "No revered DNS for " + packet); + else + dname = rr.QName; + } catch (UnknownHostException ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } } DatabaseHelper dh = new DatabaseHelper(SinkholeService.this); @@ -1034,11 +1042,15 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS // Called from native code private void dnsResolved(ResourceRecord rr) { synchronized (mapRR) { - mapRR.put(rr.Resource, rr); + try { + mapRR.put(InetAddress.getByName(rr.Resource), rr); + } catch (UnknownHostException ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + } } } - public static ResourceRecord reverseDNS(String ip) { + public static ResourceRecord reverseDNS(InetAddress ip) { synchronized (mapRR) { if (mapRR.containsKey(ip)) return mapRR.get(ip); @@ -1050,8 +1062,8 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS private void cleanupDNS() { long now = new Date().getTime(); synchronized (mapRR) { - List ips = new ArrayList<>(mapRR.keySet()); - for (String ip : ips) { + List ips = new ArrayList<>(mapRR.keySet()); + for (InetAddress ip : ips) { ResourceRecord rr = mapRR.get(ip); if (rr.Time + rr.TTL * 1000L < now && rr.Time + 10 * 60 * 1000L < now) {