resolve non-srv domains in resolver class as well

this allows us to prefer ipv4
This commit is contained in:
Daniel Gultsch 2017-07-10 08:49:22 +02:00
parent c400f74918
commit 7cbef529ae

View file

@ -19,6 +19,7 @@ import de.measite.minidns.hla.ResolverApi;
import de.measite.minidns.hla.ResolverResult; import de.measite.minidns.hla.ResolverResult;
import de.measite.minidns.record.A; import de.measite.minidns.record.A;
import de.measite.minidns.record.AAAA; import de.measite.minidns.record.AAAA;
import de.measite.minidns.record.CNAME;
import de.measite.minidns.record.Data; import de.measite.minidns.record.Data;
import de.measite.minidns.record.InternetAddressRR; import de.measite.minidns.record.InternetAddressRR;
import de.measite.minidns.record.SRV; import de.measite.minidns.record.SRV;
@ -47,7 +48,7 @@ public class Resolver {
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+e.getMessage()); Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+e.getMessage());
} }
if (results.size() == 0) { if (results.size() == 0) {
results.add(Result.createDefault(domain)); results.addAll(resolveFallback(DNSName.from(domain)));
} }
Collections.sort(results); Collections.sort(results);
Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+results.toString()); Log.d(Config.LOGTAG,Resolver.class.getSimpleName()+": "+results.toString());
@ -55,7 +56,7 @@ public class Resolver {
} }
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException { private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
if (Thread.interrupted()) { if (Thread.currentThread().isInterrupted()) {
return Collections.emptyList(); return Collections.emptyList();
} }
DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERICE)+"._tcp."+domain); DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERICE)+"._tcp."+domain);
@ -64,7 +65,7 @@ public class Resolver {
for(SRV record : result.getAnswersOrEmptySet()) { for(SRV record : result.getAnswersOrEmptySet()) {
final boolean addedIPv4 = results.addAll(resolveIp(record,A.class,result.isAuthenticData(),directTls)); final boolean addedIPv4 = results.addAll(resolveIp(record,A.class,result.isAuthenticData(),directTls));
results.addAll(resolveIp(record,AAAA.class,result.isAuthenticData(),directTls)); results.addAll(resolveIp(record,AAAA.class,result.isAuthenticData(),directTls));
if (!addedIPv4 && !Thread.interrupted()) { if (!addedIPv4 && !Thread.currentThread().isInterrupted()) {
Result resolverResult = Result.fromRecord(record, directTls); Result resolverResult = Result.fromRecord(record, directTls);
resolverResult.authenticated = resolverResult.isAuthenticated(); resolverResult.authenticated = resolverResult.isAuthenticated();
results.add(resolverResult); results.add(resolverResult);
@ -74,12 +75,12 @@ public class Resolver {
} }
private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) { private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
if (Thread.interrupted()) { if (Thread.currentThread().isInterrupted()) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<Result> list = new ArrayList<>(); List<Result> list = new ArrayList<>();
try { try {
ResolverResult<D> results = resolveWithFallback(srv.name,type); ResolverResult<D> results = resolveWithFallback(srv.name,type, !authenticated);
for (D record : results.getAnswersOrEmptySet()) { for (D record : results.getAnswersOrEmptySet()) {
Result resolverResult = Result.fromRecord(srv, directTls); Result resolverResult = Result.fromRecord(srv, directTls);
resolverResult.authenticated = results.isAuthenticData() && authenticated; resolverResult.authenticated = results.isAuthenticData() && authenticated;
@ -92,7 +93,37 @@ public class Resolver {
return list; return list;
} }
private static List<Result> resolveFallback(DNSName dnsName) {
List<Result> results = new ArrayList<>();
try {
for(A a : resolveWithFallback(dnsName,A.class,true).getAnswersOrEmptySet()) {
results.add(Result.createDefault(dnsName,a.getInetAddress()));
}
for(AAAA aaaa : resolveWithFallback(dnsName,AAAA.class,true).getAnswersOrEmptySet()) {
results.add(Result.createDefault(dnsName,aaaa.getInetAddress()));
}
if (results.size() == 0) {
for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, true).getAnswersOrEmptySet()) {
results.addAll(resolveFallback(cname.name));
}
}
} catch (IOException e) {
Log.d(Config.LOGTAG,"error resolving fallback records "+e);
}
if (results.size() == 0) {
results.add(Result.createDefault(dnsName));
}
return results;
}
private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type) throws IOException { private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type) throws IOException {
return resolveWithFallback(dnsName,type,false);
}
private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type, boolean skipDnssec) throws IOException {
if (skipDnssec) {
return ResolverApi.INSTANCE.resolve(dnsName, type);
}
try { try {
final ResolverResult<D> r = DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName, type); final ResolverResult<D> r = DnssecResolverApi.INSTANCE.resolveDnssecReliable(dnsName, type);
if (r.wasSuccessful()) { if (r.wasSuccessful()) {
@ -184,12 +215,17 @@ public class Resolver {
return result; return result;
} }
public static Result createDefault(String domain) { public static Result createDefault(DNSName hostname, InetAddress ip) {
Result result = new Result(); Result result = new Result();
result.port = 5222; result.port = 5222;
result.hostname = DNSName.from(domain); result.hostname = hostname;
result.ip = ip;
return result; return result;
} }
public static Result createDefault(DNSName hostname) {
return createDefault(hostname,null);
}
} }
} }