Networkstack - let OS decide IPv4 or IPv6
This commit is contained in:
parent
0a587aafe7
commit
510e85d244
|
@ -154,7 +154,6 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
||||||
private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
|
private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
|
||||||
+ Resolver.Result.DOMAIN + " TEXT,"
|
+ Resolver.Result.DOMAIN + " TEXT,"
|
||||||
+ Resolver.Result.HOSTNAME + " TEXT,"
|
+ Resolver.Result.HOSTNAME + " TEXT,"
|
||||||
+ Resolver.Result.IP + " BLOB,"
|
|
||||||
+ Resolver.Result.PRIORITY + " NUMBER,"
|
+ Resolver.Result.PRIORITY + " NUMBER,"
|
||||||
+ Resolver.Result.DIRECT_TLS + " NUMBER,"
|
+ Resolver.Result.DIRECT_TLS + " NUMBER,"
|
||||||
+ Resolver.Result.AUTHENTICATED + " NUMBER,"
|
+ Resolver.Result.AUTHENTICATED + " NUMBER,"
|
||||||
|
|
|
@ -7,9 +7,6 @@ import android.util.Log;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -26,11 +23,8 @@ import de.measite.minidns.hla.DnssecResolverApi;
|
||||||
import de.measite.minidns.hla.ResolverApi;
|
import de.measite.minidns.hla.ResolverApi;
|
||||||
import de.measite.minidns.hla.ResolverResult;
|
import de.measite.minidns.hla.ResolverResult;
|
||||||
import de.measite.minidns.iterative.ReliableDNSClient;
|
import de.measite.minidns.iterative.ReliableDNSClient;
|
||||||
import de.measite.minidns.record.A;
|
|
||||||
import de.measite.minidns.record.AAAA;
|
|
||||||
import de.measite.minidns.record.CNAME;
|
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.SRV;
|
import de.measite.minidns.record.SRV;
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
import eu.siacs.conversations.R;
|
||||||
|
@ -146,45 +140,29 @@ public class Resolver {
|
||||||
if (!IP.matches(domain)) {
|
if (!IP.matches(domain)) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
try {
|
final List<Result> results = new ArrayList<>();
|
||||||
|
DNSName hostname = DNSName.from(domain);
|
||||||
|
results.add(Result.createDefault(hostname));
|
||||||
|
|
||||||
Result result = new Result();
|
Result result = new Result();
|
||||||
result.ip = InetAddress.getByName(domain);
|
result.hostname = hostname;
|
||||||
result.port = 5222;
|
result.port = 5222;
|
||||||
return Collections.singletonList(result);
|
results.add(result);
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
return Collections.emptyList();
|
return results;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
|
private static List<Result> resolveSrv(String domain, final boolean directTls) throws IOException {
|
||||||
DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain);
|
DNSName dnsName = DNSName.from((directTls ? DIRECT_TLS_SERVICE : STARTTLS_SERVICE) + "._tcp." + domain);
|
||||||
ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class);
|
ResolverResult<SRV> result = resolveWithFallback(dnsName, SRV.class);
|
||||||
final List<Result> results = new ArrayList<>();
|
final List<Result> results = new ArrayList<>();
|
||||||
final List<Thread> threads = new ArrayList<>();
|
|
||||||
for (SRV record : result.getAnswersOrEmptySet()) {
|
for (SRV record : result.getAnswersOrEmptySet()) {
|
||||||
if (record.name.length() == 0 && record.priority == 0) {
|
if (record.name.length() == 0 && record.priority == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
threads.add(new Thread(() -> {
|
results.add(Result.fromRecord(record,directTls));
|
||||||
final List<Result> ipv6s = resolveIp(record, AAAA.class, result.isAuthenticData(), directTls);
|
|
||||||
synchronized (results) {
|
|
||||||
results.addAll(ipv6s);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
threads.add(new Thread(() -> {
|
|
||||||
final List<Result> ipv4s = resolveIp(record, A.class, result.isAuthenticData(), directTls);
|
|
||||||
synchronized (results) {
|
|
||||||
results.addAll(ipv4s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
for (Thread thread : threads) {
|
|
||||||
thread.start();
|
|
||||||
}
|
|
||||||
for (Thread thread : threads) {
|
|
||||||
try {
|
|
||||||
thread.join();
|
|
||||||
if (results.size() == 0) {
|
if (results.size() == 0) {
|
||||||
for (SRV record : result.getAnswersOrEmptySet()) {
|
for (SRV record : result.getAnswersOrEmptySet()) {
|
||||||
if (record.name.length() == 0 && record.priority == 0) {
|
if (record.name.length() == 0 && record.priority == 0) {
|
||||||
|
@ -195,40 +173,14 @@ public class Resolver {
|
||||||
results.add(resolverResult);
|
results.add(resolverResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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) {
|
return results;
|
||||||
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);
|
|
||||||
resolverResult.authenticated = results.isAuthenticData() && authenticated;
|
|
||||||
resolverResult.ip = record.getInetAddress();
|
|
||||||
resolverResult.rtt = rttTo(resolverResult.ip,resolverResult.port);
|
|
||||||
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) {
|
private static List<Result> resolveNoSrvRecords(DNSName dnsName, boolean withCnames) {
|
||||||
List<Result> results = new ArrayList<>();
|
List<Result> results = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
for (AAAA aaaa : resolveWithFallback(dnsName, AAAA.class, false).getAnswersOrEmptySet()) {
|
if (withCnames) {
|
||||||
results.add(Result.createDefault(dnsName, aaaa.getInetAddress()));
|
|
||||||
}
|
|
||||||
for (A a : resolveWithFallback(dnsName, A.class, false).getAnswersOrEmptySet()) {
|
|
||||||
results.add(Result.createDefault(dnsName, a.getInetAddress()));
|
|
||||||
}
|
|
||||||
if (results.size() == 0 && withCnames) {
|
|
||||||
for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, false).getAnswersOrEmptySet()) {
|
for (CNAME cname : resolveWithFallback(dnsName, CNAME.class, false).getAnswersOrEmptySet()) {
|
||||||
results.addAll(resolveNoSrvRecords(cname.name, false));
|
results.addAll(resolveNoSrvRecords(cname.name, false));
|
||||||
}
|
}
|
||||||
|
@ -261,34 +213,20 @@ public class Resolver {
|
||||||
return ResolverApi.INSTANCE.resolve(question);
|
return ResolverApi.INSTANCE.resolve(question);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static long rttTo(InetAddress ip, int port){
|
|
||||||
long time = System.currentTimeMillis();
|
|
||||||
try (Socket s = new Socket()) {
|
|
||||||
s.connect(new InetSocketAddress(ip, port), 200);
|
|
||||||
s.close();
|
|
||||||
return System.currentTimeMillis() - time;
|
|
||||||
}catch (Exception e){
|
|
||||||
Log.e(Config.LOGTAG, Resolver.class.getSimpleName() + ": error testing connection to " + (ip == null ? null : ip.getHostAddress()) + ":" + port);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean validateHostname() {
|
private static boolean validateHostname() {
|
||||||
return SERVICE != null && SERVICE.getBooleanPreference("validate_hostname", R.bool.validate_hostname);
|
return SERVICE != null && SERVICE.getBooleanPreference("validate_hostname", R.bool.validate_hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Result implements Comparable<Result> {
|
public static class Result implements Comparable<Result> {
|
||||||
public static final String DOMAIN = "domain";
|
public static final String DOMAIN = "domain";
|
||||||
public static final String IP = "ip";
|
|
||||||
public static final String HOSTNAME = "hostname";
|
public static final String HOSTNAME = "hostname";
|
||||||
public static final String PORT = "port";
|
public static final String PORT = "port";
|
||||||
public static final String PRIORITY = "priority";
|
public static final String PRIORITY = "priority";
|
||||||
public static final String DIRECT_TLS = "directTls";
|
public static final String DIRECT_TLS = "directTls";
|
||||||
public static final String AUTHENTICATED = "authenticated";
|
public static final String AUTHENTICATED = "authenticated";
|
||||||
private InetAddress ip;
|
|
||||||
private DNSName hostname;
|
private DNSName hostname;
|
||||||
private int port = 5222;
|
private int port = 5223;
|
||||||
private boolean directTls = false;
|
private boolean directTls = true;
|
||||||
private boolean authenticated = false;
|
private boolean authenticated = false;
|
||||||
private int priority;
|
private int priority;
|
||||||
private long rtt = -1;
|
private long rtt = -1;
|
||||||
|
@ -302,28 +240,15 @@ public class Resolver {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Result createDefault(DNSName hostname, InetAddress ip) {
|
|
||||||
Result result = new Result();
|
|
||||||
result.port = 5222;
|
|
||||||
result.hostname = hostname;
|
|
||||||
result.ip = ip;
|
|
||||||
if (ip != null) {
|
|
||||||
result.rtt = rttTo(result.ip, result.port);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Result createDefault(DNSName hostname) {
|
static Result createDefault(DNSName hostname) {
|
||||||
return createDefault(hostname, null);
|
Result result = new Result();
|
||||||
|
result.port = 5223;
|
||||||
|
result.hostname = hostname;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Result fromCursor(Cursor cursor) {
|
public static Result fromCursor(Cursor cursor) {
|
||||||
final Result result = new Result();
|
final Result result = new Result();
|
||||||
try {
|
|
||||||
result.ip = InetAddress.getByAddress(cursor.getBlob(cursor.getColumnIndex(IP)));
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
result.ip = null;
|
|
||||||
}
|
|
||||||
final String hostname = cursor.getString(cursor.getColumnIndex(HOSTNAME));
|
final String hostname = cursor.getString(cursor.getColumnIndex(HOSTNAME));
|
||||||
result.hostname = hostname == null ? null : DNSName.from(hostname);
|
result.hostname = hostname == null ? null : DNSName.from(hostname);
|
||||||
result.port = cursor.getInt(cursor.getColumnIndex(PORT));
|
result.port = cursor.getInt(cursor.getColumnIndex(PORT));
|
||||||
|
@ -344,14 +269,12 @@ public class Resolver {
|
||||||
if (directTls != result.directTls) return false;
|
if (directTls != result.directTls) return false;
|
||||||
if (authenticated != result.authenticated) return false;
|
if (authenticated != result.authenticated) return false;
|
||||||
if (priority != result.priority) 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;
|
return hostname != null ? hostname.equals(result.hostname) : result.hostname == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = ip != null ? ip.hashCode() : 0;
|
int result = hostname != null ? hostname.hashCode() : 0;
|
||||||
result = 31 * result + (hostname != null ? hostname.hashCode() : 0);
|
|
||||||
result = 31 * result + port;
|
result = 31 * result + port;
|
||||||
result = 31 * result + (directTls ? 1 : 0);
|
result = 31 * result + (directTls ? 1 : 0);
|
||||||
result = 31 * result + (authenticated ? 1 : 0);
|
result = 31 * result + (authenticated ? 1 : 0);
|
||||||
|
@ -359,10 +282,6 @@ public class Resolver {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public InetAddress getIp() {
|
|
||||||
return ip;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
@ -382,13 +301,11 @@ public class Resolver {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Result{" +
|
return "Result{" +
|
||||||
"ip='" + (ip == null ? null : ip.getHostAddress()) + '\'' +
|
|
||||||
", hostame='" + hostname.toString() + '\'' +
|
", hostame='" + hostname.toString() + '\'' +
|
||||||
", port=" + port +
|
", port=" + port +
|
||||||
", directTls=" + directTls +
|
", directTls=" + directTls +
|
||||||
", authenticated=" + authenticated +
|
", authenticated=" + authenticated +
|
||||||
", priority=" + priority +
|
", priority=" + priority +
|
||||||
", rtt=" + rtt +
|
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -396,16 +313,7 @@ public class Resolver {
|
||||||
public int compareTo(@NonNull Result result) {
|
public int compareTo(@NonNull Result result) {
|
||||||
if (result.priority == priority) {
|
if (result.priority == priority) {
|
||||||
if (directTls == result.directTls) {
|
if (directTls == result.directTls) {
|
||||||
if (ip == null && result.ip == null) {
|
|
||||||
return 0;
|
return 0;
|
||||||
} else if (ip != null && result.ip != null) {
|
|
||||||
if (rtt == -1 || result.rtt == -1) {
|
|
||||||
Log.e(Config.LOGTAG, Resolver.class.getSimpleName() + ": unable to read round trip time servers on compare between "+toString()+ " and "+result.toString());
|
|
||||||
}
|
|
||||||
return rtt < result.rtt ? -1 : 1;
|
|
||||||
} else {
|
|
||||||
return ip != null ? -1 : 1;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return directTls ? -1 : 1;
|
return directTls ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
@ -416,7 +324,6 @@ public class Resolver {
|
||||||
|
|
||||||
public ContentValues toContentValues() {
|
public ContentValues toContentValues() {
|
||||||
final ContentValues contentValues = new ContentValues();
|
final ContentValues contentValues = new ContentValues();
|
||||||
contentValues.put(IP, ip == null ? null : ip.getAddress());
|
|
||||||
contentValues.put(HOSTNAME, hostname == null ? null : hostname.toString());
|
contentValues.put(HOSTNAME, hostname == null ? null : hostname.toString());
|
||||||
contentValues.put(PORT, port);
|
contentValues.put(PORT, port);
|
||||||
contentValues.put(PRIORITY, priority);
|
contentValues.put(PRIORITY, priority);
|
||||||
|
|
|
@ -75,7 +75,6 @@ import eu.siacs.conversations.services.MessageArchiveService;
|
||||||
import eu.siacs.conversations.services.NotificationService;
|
import eu.siacs.conversations.services.NotificationService;
|
||||||
import eu.siacs.conversations.services.XmppConnectionService;
|
import eu.siacs.conversations.services.XmppConnectionService;
|
||||||
import eu.siacs.conversations.utils.CryptoHelper;
|
import eu.siacs.conversations.utils.CryptoHelper;
|
||||||
import eu.siacs.conversations.utils.IP;
|
|
||||||
import eu.siacs.conversations.utils.Patterns;
|
import eu.siacs.conversations.utils.Patterns;
|
||||||
import eu.siacs.conversations.utils.Resolver;
|
import eu.siacs.conversations.utils.Resolver;
|
||||||
import eu.siacs.conversations.utils.SSLSocketHelper;
|
import eu.siacs.conversations.utils.SSLSocketHelper;
|
||||||
|
@ -315,18 +314,10 @@ public class XmppConnection implements Runnable {
|
||||||
features.encryptionEnabled = result.isDirectTls();
|
features.encryptionEnabled = result.isDirectTls();
|
||||||
verifiedHostname = result.isAuthenticated() ? result.getHostname().toString() : null;
|
verifiedHostname = result.isAuthenticated() ? result.getHostname().toString() : null;
|
||||||
Log.d(Config.LOGTAG,"verified hostname "+verifiedHostname);
|
Log.d(Config.LOGTAG,"verified hostname "+verifiedHostname);
|
||||||
final InetSocketAddress addr;
|
final InetSocketAddress addr = new InetSocketAddress(IDN.toASCII(result.getHostname().toString()), result.getPort());
|
||||||
if (result.getIp() != null) {
|
|
||||||
addr = new InetSocketAddress(result.getIp(), result.getPort());
|
|
||||||
Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
|
|
||||||
+ ": using values from resolver " + (result.getHostname() == null ? "" : result.getHostname().toString()
|
|
||||||
+ "/") + result.getIp().getHostAddress() + ":" + result.getPort() + " tls: " + features.encryptionEnabled);
|
|
||||||
} else {
|
|
||||||
addr = new InetSocketAddress(IDN.toASCII(result.getHostname().toString()), result.getPort());
|
|
||||||
Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
|
Log.d(Config.LOGTAG, account.getJid().asBareJid().toString()
|
||||||
+ ": using values from resolver "
|
+ ": using values from resolver "
|
||||||
+ result.getHostname().toString() + ":" + result.getPort() + " tls: " + features.encryptionEnabled);
|
+ result.getHostname().toString() + ":" + result.getPort() + " tls: " + features.encryptionEnabled);
|
||||||
}
|
|
||||||
|
|
||||||
if (!features.encryptionEnabled) {
|
if (!features.encryptionEnabled) {
|
||||||
localSocket = new Socket();
|
localSocket = new Socket();
|
||||||
|
|
Loading…
Reference in New Issue