2017-06-21 21:28:01 +00:00
|
|
|
|
package eu.siacs.conversations.utils;
|
|
|
|
|
|
2018-01-21 19:41:30 +00:00
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.database.Cursor;
|
2021-01-18 17:26:46 +00:00
|
|
|
|
import androidx.annotation.NonNull;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-09-08 09:30:15 +00:00
|
|
|
|
import java.lang.reflect.Field;
|
2017-07-09 17:05:23 +00:00
|
|
|
|
import java.net.Inet4Address;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import java.net.InetAddress;
|
2018-01-21 19:41:30 +00:00
|
|
|
|
import java.net.UnknownHostException;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2018-09-08 09:30:15 +00:00
|
|
|
|
import de.measite.minidns.AbstractDNSClient;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import de.measite.minidns.DNSClient;
|
|
|
|
|
import de.measite.minidns.DNSName;
|
2017-09-21 19:46:11 +00:00
|
|
|
|
import de.measite.minidns.Question;
|
|
|
|
|
import de.measite.minidns.Record;
|
2017-06-25 20:46:56 +00:00
|
|
|
|
import de.measite.minidns.dnssec.DNSSECResultNotAuthenticException;
|
2017-09-21 08:31:21 +00:00
|
|
|
|
import de.measite.minidns.dnsserverlookup.AndroidUsingExec;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import de.measite.minidns.hla.DnssecResolverApi;
|
2017-06-25 20:46:56 +00:00
|
|
|
|
import de.measite.minidns.hla.ResolverApi;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import de.measite.minidns.hla.ResolverResult;
|
2018-09-08 09:30:15 +00:00
|
|
|
|
import de.measite.minidns.iterative.ReliableDNSClient;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import de.measite.minidns.record.A;
|
|
|
|
|
import de.measite.minidns.record.AAAA;
|
2017-07-10 06:49:22 +00:00
|
|
|
|
import de.measite.minidns.record.CNAME;
|
2017-06-25 20:46:56 +00:00
|
|
|
|
import de.measite.minidns.record.Data;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
import de.measite.minidns.record.InternetAddressRR;
|
|
|
|
|
import de.measite.minidns.record.SRV;
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2017-07-10 07:59:25 +00:00
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2017-06-21 21:28:01 +00:00
|
|
|
|
|
|
|
|
|
public class Resolver {
|
|
|
|
|
|
2018-11-10 09:19:29 +00:00
|
|
|
|
public static final int DEFAULT_PORT_XMPP = 5222;
|
|
|
|
|
|
2018-09-08 09:30:15 +00:00
|
|
|
|
private static final String DIRECT_TLS_SERVICE = "_xmpps-client";
|
2020-02-29 14:02:36 +00:00
|
|
|
|
private static final String STARTTLS_SERVICE = "_xmpp-client";
|
2018-09-08 09:30:15 +00:00
|
|
|
|
|
|
|
|
|
private static XmppConnectionService SERVICE = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void init(XmppConnectionService service) {
|
|
|
|
|
Resolver.SERVICE = service;
|
|
|
|
|
DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE);
|
|
|
|
|
DNSClient.addDnsServerLookupMechanism(AndroidUsingExecLowPriority.INSTANCE);
|
|
|
|
|
DNSClient.addDnsServerLookupMechanism(new AndroidUsingLinkProperties(service));
|
|
|
|
|
final AbstractDNSClient client = ResolverApi.INSTANCE.getClient();
|
|
|
|
|
if (client instanceof ReliableDNSClient) {
|
|
|
|
|
disableHardcodedDnsServers((ReliableDNSClient) client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void disableHardcodedDnsServers(ReliableDNSClient reliableDNSClient) {
|
|
|
|
|
try {
|
|
|
|
|
final Field dnsClientField = ReliableDNSClient.class.getDeclaredField("dnsClient");
|
|
|
|
|
dnsClientField.setAccessible(true);
|
|
|
|
|
final DNSClient dnsClient = (DNSClient) dnsClientField.get(reliableDNSClient);
|
2020-02-29 11:52:39 +00:00
|
|
|
|
if (dnsClient != null) {
|
|
|
|
|
dnsClient.getDataSource().setTimeout(3000);
|
|
|
|
|
}
|
2018-09-08 09:30:15 +00:00
|
|
|
|
final Field useHardcodedDnsServers = DNSClient.class.getDeclaredField("useHardcodedDnsServers");
|
|
|
|
|
useHardcodedDnsServers.setAccessible(true);
|
|
|
|
|
useHardcodedDnsServers.setBoolean(dnsClient, false);
|
2018-12-09 21:32:42 +00:00
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
|
Log.e(Config.LOGTAG, "Unable to disable hardcoded DNS servers", e);
|
|
|
|
|
} catch (IllegalAccessException e) {
|
2018-09-08 09:30:15 +00:00
|
|
|
|
Log.e(Config.LOGTAG, "Unable to disable hardcoded DNS servers", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 13:03:20 +00:00
|
|
|
|
public static List<Result> fromHardCoded(final String hostname, final int port) {
|
|
|
|
|
final Result result = new Result();
|
2018-10-01 15:07:37 +00:00
|
|
|
|
result.hostname = DNSName.from(hostname);
|
|
|
|
|
result.port = port;
|
2019-09-05 10:08:58 +00:00
|
|
|
|
result.directTls = useDirectTls(port);
|
2018-10-01 15:07:37 +00:00
|
|
|
|
result.authenticated = true;
|
|
|
|
|
return Collections.singletonList(result);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 13:03:20 +00:00
|
|
|
|
public static boolean invalidHostname(final String hostname) {
|
|
|
|
|
try {
|
|
|
|
|
DNSName.from(hostname);
|
|
|
|
|
return false;
|
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-01 15:07:37 +00:00
|
|
|
|
|
2019-09-05 10:08:58 +00:00
|
|
|
|
public static boolean useDirectTls(final int port) {
|
|
|
|
|
return port == 443 || port == 5223;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-08 09:30:15 +00:00
|
|
|
|
public static List<Result> resolve(String domain) {
|
2018-10-01 15:07:37 +00:00
|
|
|
|
final List<Result> ipResults = fromIpAddress(domain);
|
|
|
|
|
if (ipResults.size() > 0) {
|
|
|
|
|
return ipResults;
|
|
|
|
|
}
|
2018-09-08 09:30:15 +00:00
|
|
|
|
final List<Result> results = new ArrayList<>();
|
|
|
|
|
final List<Result> fallbackResults = new ArrayList<>();
|
|
|
|
|
Thread[] threads = new Thread[3];
|
|
|
|
|
threads[0] = new Thread(() -> {
|
|
|
|
|
try {
|
|
|
|
|
final List<Result> list = resolveSrv(domain, true);
|
|
|
|
|
synchronized (results) {
|
|
|
|
|
results.addAll(list);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (direct TLS)", throwable);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
threads[1] = new Thread(() -> {
|
|
|
|
|
try {
|
|
|
|
|
final List<Result> list = resolveSrv(domain, false);
|
|
|
|
|
synchronized (results) {
|
|
|
|
|
results.addAll(list);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving SRV record (STARTTLS)", throwable);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
threads[2] = new Thread(() -> {
|
|
|
|
|
List<Result> list = resolveNoSrvRecords(DNSName.from(domain), true);
|
|
|
|
|
synchronized (fallbackResults) {
|
|
|
|
|
fallbackResults.addAll(list);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
for (Thread thread : threads) {
|
|
|
|
|
thread.start();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
threads[0].join();
|
|
|
|
|
threads[1].join();
|
|
|
|
|
if (results.size() > 0) {
|
|
|
|
|
threads[2].interrupt();
|
2018-09-20 18:29:21 +00:00
|
|
|
|
synchronized (results) {
|
|
|
|
|
Collections.sort(results);
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + results.toString());
|
|
|
|
|
return new ArrayList<>(results);
|
|
|
|
|
}
|
2018-09-08 09:30:15 +00:00
|
|
|
|
} else {
|
|
|
|
|
threads[2].join();
|
2018-09-20 18:29:21 +00:00
|
|
|
|
synchronized (fallbackResults) {
|
|
|
|
|
Collections.sort(fallbackResults);
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": " + fallbackResults.toString());
|
|
|
|
|
return new ArrayList<>(fallbackResults);
|
|
|
|
|
}
|
2018-09-08 09:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
} catch (InterruptedException e) {
|
2018-10-01 15:07:37 +00:00
|
|
|
|
for (Thread thread : threads) {
|
2018-09-26 08:18:56 +00:00
|
|
|
|
thread.interrupt();
|
|
|
|
|
}
|
2018-09-26 12:39:04 +00:00
|
|
|
|
return Collections.emptyList();
|
2018-09-08 09:30:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-01 15:07:37 +00:00
|
|
|
|
private static List<Result> fromIpAddress(String domain) {
|
|
|
|
|
if (!IP.matches(domain)) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
Result result = new Result();
|
|
|
|
|
result.ip = InetAddress.getByName(domain);
|
2018-11-10 09:19:29 +00:00
|
|
|
|
result.port = DEFAULT_PORT_XMPP;
|
2018-10-01 15:07:37 +00:00
|
|
|
|
return Collections.singletonList(result);
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-08 09:30:15 +00:00
|
|
|
|
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
|
2020-02-29 14:02:36 +00:00
|
|
|
|
DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain);
|
2018-09-08 09:30:15 +00:00
|
|
|
|
ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class);
|
|
|
|
|
final List<Result> results = new ArrayList<>();
|
|
|
|
|
final List<Thread> threads = new ArrayList<>();
|
|
|
|
|
for (SRV record : result.getAnswersOrEmptySet()) {
|
|
|
|
|
if (record.name.length() == 0 && record.priority == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
threads.add(new Thread(() -> {
|
|
|
|
|
final List<Result> ipv4s = resolveIp(record, A.class, result.isAuthenticData(), directTls);
|
|
|
|
|
if (ipv4s.size() == 0) {
|
|
|
|
|
Result resolverResult = Result.fromRecord(record, directTls);
|
2020-02-29 11:52:39 +00:00
|
|
|
|
resolverResult.authenticated = result.isAuthenticData();
|
2018-09-08 09:30:15 +00:00
|
|
|
|
ipv4s.add(resolverResult);
|
|
|
|
|
}
|
|
|
|
|
synchronized (results) {
|
|
|
|
|
results.addAll(ipv4s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}));
|
|
|
|
|
threads.add(new Thread(() -> {
|
|
|
|
|
final List<Result> ipv6s = resolveIp(record, AAAA.class, result.isAuthenticData(), directTls);
|
|
|
|
|
synchronized (results) {
|
|
|
|
|
results.addAll(ipv6s);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
for (Thread thread : threads) {
|
|
|
|
|
thread.start();
|
|
|
|
|
}
|
|
|
|
|
for (Thread thread : threads) {
|
|
|
|
|
try {
|
|
|
|
|
thread.join();
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <D extends InternetAddressRR> List<Result> resolveIp(SRV srv, Class<D> type, boolean authenticated, boolean directTls) {
|
|
|
|
|
List<Result> list = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
ResolverResult<D> results = resolveWithFallback(srv.name, type, authenticated);
|
|
|
|
|
for (D record : results.getAnswersOrEmptySet()) {
|
|
|
|
|
Result resolverResult = Result.fromRecord(srv, directTls);
|
2020-02-29 11:52:39 +00:00
|
|
|
|
resolverResult.authenticated = results.isAuthenticData() && authenticated; //TODO technically it doesn’t matter if the IP was authenticated
|
2018-09-08 09:30:15 +00:00
|
|
|
|
resolverResult.ip = record.getInetAddress();
|
|
|
|
|
list.add(resolverResult);
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable t) {
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " " + t.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static List<Result> resolveNoSrvRecords(DNSName dnsName, boolean withCnames) {
|
|
|
|
|
List<Result> results = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
for (A a : resolveWithFallback(dnsName, A.class, false).getAnswersOrEmptySet()) {
|
|
|
|
|
results.add(Result.createDefault(dnsName, a.getInetAddress()));
|
|
|
|
|
}
|
|
|
|
|
for (AAAA aaaa : resolveWithFallback(dnsName, AAAA.class, false).getAnswersOrEmptySet()) {
|
|
|
|
|
results.add(Result.createDefault(dnsName, aaaa.getInetAddress()));
|
|
|
|
|
}
|
|
|
|
|
if (results.size() == 0 && withCnames) {
|
|
|
|
|
for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, false).getAnswersOrEmptySet()) {
|
|
|
|
|
results.addAll(resolveNoSrvRecords(cname.name, false));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + "error resolving fallback records", throwable);
|
|
|
|
|
}
|
|
|
|
|
results.add(Result.createDefault(dnsName));
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type) throws IOException {
|
|
|
|
|
return resolveWithFallback(dnsName, type, validateHostname());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <D extends Data> ResolverResult<D> resolveWithFallback(DNSName dnsName, Class<D> type, boolean validateHostname) throws IOException {
|
|
|
|
|
final Question question = new Question(dnsName, Record.TYPE.getType(type));
|
|
|
|
|
if (!validateHostname) {
|
|
|
|
|
return ResolverApi.INSTANCE.resolve(question);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return DnssecResolverApi.INSTANCE.resolveDnssecReliable(question);
|
|
|
|
|
} catch (DNSSECResultNotAuthenticException e) {
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " with DNSSEC. Trying DNS instead.", e);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw e;
|
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
|
Log.d(Config.LOGTAG, Resolver.class.getSimpleName() + ": error resolving " + type.getSimpleName() + " with DNSSEC. Trying DNS instead.", throwable);
|
|
|
|
|
}
|
|
|
|
|
return ResolverApi.INSTANCE.resolve(question);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static boolean validateHostname() {
|
|
|
|
|
return SERVICE != null && SERVICE.getBooleanPreference("validate_hostname", R.bool.validate_hostname);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class Result implements Comparable<Result> {
|
|
|
|
|
public static final String DOMAIN = "domain";
|
|
|
|
|
public static final String IP = "ip";
|
|
|
|
|
public static final String HOSTNAME = "hostname";
|
|
|
|
|
public static final String PORT = "port";
|
|
|
|
|
public static final String PRIORITY = "priority";
|
|
|
|
|
public static final String DIRECT_TLS = "directTls";
|
|
|
|
|
public static final String AUTHENTICATED = "authenticated";
|
|
|
|
|
private InetAddress ip;
|
|
|
|
|
private DNSName hostname;
|
2018-11-10 09:19:29 +00:00
|
|
|
|
private int port = DEFAULT_PORT_XMPP;
|
2018-09-08 09:30:15 +00:00
|
|
|
|
private boolean directTls = false;
|
|
|
|
|
private boolean authenticated = false;
|
|
|
|
|
private int priority;
|
|
|
|
|
|
|
|
|
|
static Result fromRecord(SRV srv, boolean directTls) {
|
|
|
|
|
Result result = new Result();
|
|
|
|
|
result.port = srv.port;
|
|
|
|
|
result.hostname = srv.name;
|
|
|
|
|
result.directTls = directTls;
|
|
|
|
|
result.priority = srv.priority;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Result createDefault(DNSName hostname, InetAddress ip) {
|
|
|
|
|
Result result = new Result();
|
2018-11-10 09:19:29 +00:00
|
|
|
|
result.port = DEFAULT_PORT_XMPP;
|
2018-09-08 09:30:15 +00:00
|
|
|
|
result.hostname = hostname;
|
|
|
|
|
result.ip = ip;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Result createDefault(DNSName hostname) {
|
|
|
|
|
return createDefault(hostname, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Result fromCursor(Cursor cursor) {
|
|
|
|
|
final Result result = new Result();
|
|
|
|
|
try {
|
|
|
|
|
result.ip = InetAddress.getByAddress(cursor.getBlob(cursor.getColumnIndex(IP)));
|
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
|
result.ip = null;
|
|
|
|
|
}
|
2018-10-13 14:13:33 +00:00
|
|
|
|
final String hostname = cursor.getString(cursor.getColumnIndex(HOSTNAME));
|
|
|
|
|
result.hostname = hostname == null ? null : DNSName.from(hostname);
|
2018-09-08 09:30:15 +00:00
|
|
|
|
result.port = cursor.getInt(cursor.getColumnIndex(PORT));
|
|
|
|
|
result.priority = cursor.getInt(cursor.getColumnIndex(PRIORITY));
|
|
|
|
|
result.authenticated = cursor.getInt(cursor.getColumnIndex(AUTHENTICATED)) > 0;
|
|
|
|
|
result.directTls = cursor.getInt(cursor.getColumnIndex(DIRECT_TLS)) > 0;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
|
if (this == o) return true;
|
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
|
|
Result result = (Result) o;
|
|
|
|
|
|
|
|
|
|
if (port != result.port) return false;
|
|
|
|
|
if (directTls != result.directTls) return false;
|
|
|
|
|
if (authenticated != result.authenticated) return false;
|
|
|
|
|
if (priority != result.priority) return false;
|
|
|
|
|
if (ip != null ? !ip.equals(result.ip) : result.ip != null) return false;
|
|
|
|
|
return hostname != null ? hostname.equals(result.hostname) : result.hostname == null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
int result = ip != null ? ip.hashCode() : 0;
|
|
|
|
|
result = 31 * result + (hostname != null ? hostname.hashCode() : 0);
|
|
|
|
|
result = 31 * result + port;
|
|
|
|
|
result = 31 * result + (directTls ? 1 : 0);
|
|
|
|
|
result = 31 * result + (authenticated ? 1 : 0);
|
|
|
|
|
result = 31 * result + priority;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InetAddress getIp() {
|
|
|
|
|
return ip;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getPort() {
|
|
|
|
|
return port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DNSName getHostname() {
|
|
|
|
|
return hostname;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isDirectTls() {
|
|
|
|
|
return directTls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isAuthenticated() {
|
|
|
|
|
return authenticated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return "Result{" +
|
|
|
|
|
"ip='" + (ip == null ? null : ip.getHostAddress()) + '\'' +
|
2020-02-29 09:14:52 +00:00
|
|
|
|
", hostame='" + (hostname == null ? null : hostname.toString()) + '\'' +
|
2018-09-08 09:30:15 +00:00
|
|
|
|
", port=" + port +
|
|
|
|
|
", directTls=" + directTls +
|
|
|
|
|
", authenticated=" + authenticated +
|
|
|
|
|
", priority=" + priority +
|
|
|
|
|
'}';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int compareTo(@NonNull Result result) {
|
|
|
|
|
if (result.priority == priority) {
|
|
|
|
|
if (directTls == result.directTls) {
|
|
|
|
|
if (ip == null && result.ip == null) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else if (ip != null && result.ip != null) {
|
|
|
|
|
if (ip instanceof Inet4Address && result.ip instanceof Inet4Address) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return ip instanceof Inet4Address ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return ip != null ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return directTls ? -1 : 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return priority - result.priority;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ContentValues toContentValues() {
|
|
|
|
|
final ContentValues contentValues = new ContentValues();
|
|
|
|
|
contentValues.put(IP, ip == null ? null : ip.getAddress());
|
2018-10-13 14:13:33 +00:00
|
|
|
|
contentValues.put(HOSTNAME, hostname == null ? null : hostname.toString());
|
2018-09-08 09:30:15 +00:00
|
|
|
|
contentValues.put(PORT, port);
|
|
|
|
|
contentValues.put(PRIORITY, priority);
|
|
|
|
|
contentValues.put(DIRECT_TLS, directTls ? 1 : 0);
|
|
|
|
|
contentValues.put(AUTHENTICATED, authenticated ? 1 : 0);
|
|
|
|
|
return contentValues;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-21 21:28:01 +00:00
|
|
|
|
|
|
|
|
|
}
|