From 6dad1c0dde9fd85135b08bab7be7a51f2705adef Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 25 Jun 2017 22:46:56 +0200 Subject: [PATCH] refactored resolver to fall back to DNS and use reliable method --- .../siacs/conversations/utils/Resolver.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/utils/Resolver.java b/src/main/java/eu/siacs/conversations/utils/Resolver.java index 0d92bcea9..a8a7ffbc2 100644 --- a/src/main/java/eu/siacs/conversations/utils/Resolver.java +++ b/src/main/java/eu/siacs/conversations/utils/Resolver.java @@ -12,10 +12,14 @@ import java.util.List; import de.measite.minidns.DNSClient; import de.measite.minidns.DNSName; +import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException; +import de.measite.minidns.dnssec.DNSSECValidationFailedException; import de.measite.minidns.hla.DnssecResolverApi; +import de.measite.minidns.hla.ResolverApi; import de.measite.minidns.hla.ResolverResult; import de.measite.minidns.record.A; import de.measite.minidns.record.AAAA; +import de.measite.minidns.record.Data; import de.measite.minidns.record.InternetAddressRR; import de.measite.minidns.record.SRV; import eu.siacs.conversations.Config; @@ -26,8 +30,6 @@ public class Resolver { private static final String STARTTLS_SERICE = "_xmpp-client"; - - public static void registerLookupMechanism(Context context) { DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(context)); } @@ -36,29 +38,33 @@ public class Resolver { List results = new ArrayList<>(); try { results.addAll(resolveSrv(domain,true)); - } catch (Throwable t) { - Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+t.getMessage()); + } catch (IOException e) { + Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+e.getMessage()); } try { results.addAll(resolveSrv(domain,false)); - } catch (Throwable t) { - Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+t.getMessage()); + } catch (IOException e) { + Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+e.getMessage()); } if (results.size() == 0) { results.add(Result.createDefault(domain)); } Collections.sort(results); + Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+results.toString()); return results; } private static List resolveSrv(String domain, final boolean directTls) throws IOException { + if (Thread.interrupted()) { + return Collections.emptyList(); + } DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERICE)+"._tcp."+domain); - ResolverResult result = DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName,SRV.class); + ResolverResult result = resolveWithFallback(dnsName,SRV.class); List results = new ArrayList<>(); for(SRV record : result.getAnswersOrEmptySet()) { - boolean added = results.addAll(resolveIp(record,A.class,result.isAuthenticData(),directTls)); - added |= results.addAll(resolveIp(record,AAAA.class,result.isAuthenticData(),directTls)); - if (!added) { + final boolean addedIPv4 = results.addAll(resolveIp(record,A.class,result.isAuthenticData(),directTls)); + results.addAll(resolveIp(record,AAAA.class,result.isAuthenticData(),directTls)); + if (!addedIPv4 && !Thread.interrupted()) { Result resolverResult = Result.fromRecord(record, directTls); resolverResult.authenticated = resolverResult.isAuthenticated(); results.add(resolverResult); @@ -68,9 +74,12 @@ public class Resolver { } private static List resolveIp(SRV srv, Class type, boolean authenticated, boolean directTls) { + if (Thread.interrupted()) { + return Collections.emptyList(); + } List list = new ArrayList<>(); try { - ResolverResult results = DnssecResolverApi.INSTANCE.resolveDnssecReliable(srv.name, type); + ResolverResult results = resolveWithFallback(srv.name,type); for (D record : results.getAnswersOrEmptySet()) { Result resolverResult = Result.fromRecord(srv, directTls); resolverResult.authenticated = results.isAuthenticData() && authenticated; @@ -83,6 +92,15 @@ public class Resolver { return list; } + private static ResolverResult resolveWithFallback(DNSName dnsName, Class type) throws IOException { + try { + return DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName, type); + } catch (DNSSECValidationFailedException | DNSSECResultNotAuthenticException e) { + Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": error resolving "+type.getSimpleName()+" with DNSSEC. Trying DNS instead "+e.getMessage()); + return ResolverApi.INSTANCE.resolve(dnsName, type); + } + } + public static class Result implements Comparable { private InetAddress ip; private DNSName hostname; @@ -127,9 +145,13 @@ public class Resolver { public int compareTo(@NonNull Result result) { if (result.priority == priority) { if (directTls == result.directTls) { - return 0; + if (ip == null && result.ip == null) { + return 0; + } else { + return ip != null ? -1 : 1; + } } else { - return directTls ? 1 : -1; + return directTls ? -1 : 1; } } else { return priority - result.priority;