From ad1347b7ff97bb8be89c16ad17ed2c168065cc53 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 16 Jul 2020 23:28:32 +0200 Subject: [PATCH] Better DNS error handling --- .../java/eu/faircode/email/DnsHelper.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DnsHelper.java b/app/src/main/java/eu/faircode/email/DnsHelper.java index 6b719915aa..55310e8298 100644 --- a/app/src/main/java/eu/faircode/email/DnsHelper.java +++ b/app/src/main/java/eu/faircode/email/DnsHelper.java @@ -63,12 +63,25 @@ public class DnsHelper { String domain = email.substring(d + 1); + boolean found = true; try { - lookup(context, domain, "mx"); - } catch (UnknownHostException ex) { - Log.i(ex); - throw new UnknownHostException(context.getString(R.string.title_no_server, domain)); + SimpleResolver resolver = new SimpleResolver(getDnsServer(context)); + Lookup lookup = new Lookup(domain, Type.MX); + lookup.setResolver(resolver); + lookup.run(); + Log.i("Check name=" + domain + " @" + resolver.getAddress() + " result=" + lookup.getResult()); + + if (lookup.getResult() == Lookup.HOST_NOT_FOUND || + lookup.getResult() == Lookup.TYPE_NOT_FOUND) + found = false; + else if (lookup.getResult() != Lookup.SUCCESSFUL) + throw new UnknownHostException("DNS error=" + lookup.getErrorString()); + } catch (Throwable ex) { + Log.e(ex); } + + if (!found) + throw new UnknownHostException(context.getString(R.string.title_no_server, domain)); } } @@ -99,9 +112,8 @@ public class DnsHelper { } try { - Lookup lookup = new Lookup(name, rtype); - SimpleResolver resolver = new SimpleResolver(getDnsServer(context)); + Lookup lookup = new Lookup(name, rtype); lookup.setResolver(resolver); Log.i("Lookup name=" + name + " @" + resolver.getAddress() + " type=" + rtype); Record[] records = lookup.run(); @@ -110,7 +122,7 @@ public class DnsHelper { lookup.getResult() == Lookup.TYPE_NOT_FOUND) throw new UnknownHostException(name); else if (lookup.getResult() != Lookup.SUCCESSFUL) - Log.w("DNS error=" + lookup.getErrorString()); + Log.e("DNS error=" + lookup.getErrorString()); List result = new ArrayList<>();