From ff756647a96055e19397ef77cbf3ae5c5b3f1b7a Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 23 Mar 2021 21:03:58 +0100 Subject: [PATCH] clear dns cache on network switch --- .../services/XmppConnectionService.java | 1 + .../siacs/conversations/utils/Resolver.java | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index f7da1ad4b..044331698 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -657,6 +657,7 @@ public class XmppConnectionService extends Service { if (Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) { resetAllAttemptCounts(true, false); } + Resolver.clearCache(); } break; case Intent.ACTION_SHUTDOWN: diff --git a/src/main/java/eu/siacs/conversations/utils/Resolver.java b/src/main/java/eu/siacs/conversations/utils/Resolver.java index 6e0973bf4..a3796b253 100644 --- a/src/main/java/eu/siacs/conversations/utils/Resolver.java +++ b/src/main/java/eu/siacs/conversations/utils/Resolver.java @@ -16,10 +16,12 @@ import java.util.Collections; import java.util.List; import de.measite.minidns.AbstractDNSClient; +import de.measite.minidns.DNSCache; import de.measite.minidns.DNSClient; import de.measite.minidns.DNSName; import de.measite.minidns.Question; import de.measite.minidns.Record; +import de.measite.minidns.cache.LRUCache; import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException; import de.measite.minidns.dnsserverlookup.AndroidUsingExec; import de.measite.minidns.hla.DnssecResolverApi; @@ -68,9 +70,7 @@ public class Resolver { final Field useHardcodedDnsServers = DNSClient.class.getDeclaredField("useHardcodedDnsServers"); useHardcodedDnsServers.setAccessible(true); useHardcodedDnsServers.setBoolean(dnsClient, false); - } catch (NoSuchFieldException e) { - Log.e(Config.LOGTAG, "Unable to disable hardcoded DNS servers", e); - } catch (IllegalAccessException e) { + } catch (NoSuchFieldException | IllegalAccessException e) { Log.e(Config.LOGTAG, "Unable to disable hardcoded DNS servers", e); } } @@ -93,6 +93,15 @@ public class Resolver { } } + public static void clearCache() { + final AbstractDNSClient client = ResolverApi.INSTANCE.getClient(); + final DNSCache dnsCache = client.getCache(); + if (dnsCache instanceof LRUCache) { + Log.d(Config.LOGTAG,"clearing DNS cache"); + ((LRUCache) dnsCache).clear(); + } + } + public static boolean useDirectTls(final int port) { return port == 443 || port == 5223; @@ -105,7 +114,7 @@ public class Resolver { } final List results = new ArrayList<>(); final List fallbackResults = new ArrayList<>(); - Thread[] threads = new Thread[3]; + final Thread[] threads = new Thread[3]; threads[0] = new Thread(() -> { try { final List list = resolveSrv(domain, true); @@ -132,7 +141,7 @@ public class Resolver { fallbackResults.addAll(list); } }); - for (Thread thread : threads) { + for (final Thread thread : threads) { thread.start(); } try {