2014-02-28 12:46:01 -05:00
|
|
|
package eu.siacs.conversations.utils;
|
2014-02-07 00:52:09 -05:00
|
|
|
|
2014-04-03 03:42:58 -04:00
|
|
|
import de.measite.minidns.Client;
|
|
|
|
import de.measite.minidns.DNSMessage;
|
|
|
|
import de.measite.minidns.Record;
|
|
|
|
import de.measite.minidns.Record.TYPE;
|
|
|
|
import de.measite.minidns.Record.CLASS;
|
|
|
|
import de.measite.minidns.record.SRV;
|
2014-04-03 05:01:20 -04:00
|
|
|
import de.measite.minidns.record.A;
|
|
|
|
import de.measite.minidns.record.AAAA;
|
2014-04-03 03:42:58 -04:00
|
|
|
import de.measite.minidns.record.Data;
|
2014-04-03 05:01:20 -04:00
|
|
|
import de.measite.minidns.util.NameUtil;
|
2014-08-31 10:28:21 -04:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-11-05 15:55:47 -05:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-04-03 03:42:58 -04:00
|
|
|
|
2014-02-07 00:52:09 -05:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
2014-04-03 04:12:50 -04:00
|
|
|
import java.net.SocketTimeoutException;
|
2014-03-25 12:21:56 -04:00
|
|
|
import java.util.ArrayList;
|
2014-04-03 03:42:58 -04:00
|
|
|
import java.util.Collections;
|
2014-02-07 00:52:09 -05:00
|
|
|
import java.util.Random;
|
2014-04-03 03:42:58 -04:00
|
|
|
import java.util.TreeMap;
|
2014-02-07 00:52:09 -05:00
|
|
|
|
|
|
|
import android.os.Bundle;
|
2014-03-25 12:21:56 -04:00
|
|
|
import android.util.Log;
|
2014-02-07 00:52:09 -05:00
|
|
|
|
|
|
|
public class DNSHelper {
|
2014-04-03 03:42:58 -04:00
|
|
|
protected static Client client = new Client();
|
|
|
|
|
2014-11-05 15:55:47 -05:00
|
|
|
public static Bundle getSRVRecord(final Jid jid) throws IOException {
|
|
|
|
final String host = jid.getDomainpart();
|
2014-04-03 03:42:58 -04:00
|
|
|
String dns[] = client.findDNS();
|
|
|
|
|
|
|
|
if (dns != null) {
|
|
|
|
for (String dnsserver : dns) {
|
|
|
|
InetAddress ip = InetAddress.getByName(dnsserver);
|
|
|
|
Bundle b = queryDNS(host, ip);
|
2014-10-27 16:48:25 -04:00
|
|
|
if (b.containsKey("values")) {
|
2014-04-03 03:42:58 -04:00
|
|
|
return b;
|
2014-10-05 18:33:52 -04:00
|
|
|
} else if (b.containsKey("error")
|
|
|
|
&& "nosrv".equals(b.getString("error", null))) {
|
2014-10-04 08:33:14 -04:00
|
|
|
return b;
|
2014-03-27 09:31:55 -04:00
|
|
|
}
|
2014-03-25 12:21:56 -04:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 03:42:58 -04:00
|
|
|
return queryDNS(host, InetAddress.getByName("8.8.8.8"));
|
2014-03-27 06:03:10 -04:00
|
|
|
}
|
2014-03-27 09:31:55 -04:00
|
|
|
|
2014-03-27 06:03:10 -04:00
|
|
|
public static Bundle queryDNS(String host, InetAddress dnsServer) {
|
2014-10-27 16:48:25 -04:00
|
|
|
Bundle bundle = new Bundle();
|
2014-03-27 06:03:10 -04:00
|
|
|
try {
|
2014-04-03 05:01:20 -04:00
|
|
|
String qname = "_xmpp-client._tcp." + host;
|
2014-08-31 10:28:21 -04:00
|
|
|
Log.d(Config.LOGTAG,
|
|
|
|
"using dns server: " + dnsServer.getHostAddress()
|
|
|
|
+ " to look up " + host);
|
|
|
|
DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN,
|
|
|
|
dnsServer.getHostAddress());
|
2014-04-03 03:42:58 -04:00
|
|
|
|
|
|
|
// How should we handle priorities and weight?
|
|
|
|
// Wikipedia has a nice article about priorities vs. weights:
|
|
|
|
// https://en.wikipedia.org/wiki/SRV_record#Provisioning_for_high_service_availability
|
|
|
|
|
|
|
|
// we bucket the SRV records based on priority, pick per priority
|
|
|
|
// a random order respecting the weight, and dump that priority by
|
|
|
|
// priority
|
|
|
|
|
2014-11-05 15:55:47 -05:00
|
|
|
TreeMap<Integer, ArrayList<SRV>> priorities = new TreeMap<>();
|
|
|
|
TreeMap<String, ArrayList<String>> ips4 = new TreeMap<>();
|
|
|
|
TreeMap<String, ArrayList<String>> ips6 = new TreeMap<>();
|
2014-04-03 05:01:20 -04:00
|
|
|
|
2014-08-31 10:28:21 -04:00
|
|
|
for (Record[] rrset : new Record[][] { message.getAnswers(),
|
|
|
|
message.getAdditionalResourceRecords() }) {
|
2014-04-03 05:01:20 -04:00
|
|
|
for (Record rr : rrset) {
|
|
|
|
Data d = rr.getPayload();
|
2014-08-31 10:28:21 -04:00
|
|
|
if (d instanceof SRV
|
|
|
|
&& NameUtil.idnEquals(qname, rr.getName())) {
|
2014-04-03 05:01:20 -04:00
|
|
|
SRV srv = (SRV) d;
|
|
|
|
if (!priorities.containsKey(srv.getPriority())) {
|
2014-08-31 10:28:21 -04:00
|
|
|
priorities.put(srv.getPriority(),
|
|
|
|
new ArrayList<SRV>(2));
|
2014-04-03 05:01:20 -04:00
|
|
|
}
|
|
|
|
priorities.get(srv.getPriority()).add(srv);
|
|
|
|
}
|
|
|
|
if (d instanceof A) {
|
|
|
|
A arecord = (A) d;
|
|
|
|
if (!ips4.containsKey(rr.getName())) {
|
|
|
|
ips4.put(rr.getName(), new ArrayList<String>(3));
|
|
|
|
}
|
|
|
|
ips4.get(rr.getName()).add(arecord.toString());
|
|
|
|
}
|
|
|
|
if (d instanceof AAAA) {
|
|
|
|
AAAA aaaa = (AAAA) d;
|
|
|
|
if (!ips6.containsKey(rr.getName())) {
|
|
|
|
ips6.put(rr.getName(), new ArrayList<String>(3));
|
|
|
|
}
|
|
|
|
ips6.get(rr.getName()).add("[" + aaaa.toString() + "]");
|
2014-04-03 03:42:58 -04:00
|
|
|
}
|
2014-03-27 09:31:55 -04:00
|
|
|
}
|
2014-02-07 00:52:09 -05:00
|
|
|
}
|
2014-04-03 03:42:58 -04:00
|
|
|
|
|
|
|
Random rnd = new Random();
|
2014-11-05 15:55:47 -05:00
|
|
|
ArrayList<SRV> result = new ArrayList<>(
|
2014-08-31 10:28:21 -04:00
|
|
|
priorities.size() * 2 + 1);
|
|
|
|
for (ArrayList<SRV> s : priorities.values()) {
|
2014-04-03 03:42:58 -04:00
|
|
|
|
|
|
|
// trivial case
|
|
|
|
if (s.size() <= 1) {
|
|
|
|
result.addAll(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
long totalweight = 0l;
|
2014-08-31 10:28:21 -04:00
|
|
|
for (SRV srv : s) {
|
2014-04-03 03:42:58 -04:00
|
|
|
totalweight += srv.getWeight();
|
2014-03-27 09:31:55 -04:00
|
|
|
}
|
2014-04-03 03:42:58 -04:00
|
|
|
|
|
|
|
while (totalweight > 0l && s.size() > 0) {
|
2014-08-31 10:28:21 -04:00
|
|
|
long p = (rnd.nextLong() & 0x7fffffffffffffffl)
|
|
|
|
% totalweight;
|
2014-04-03 03:42:58 -04:00
|
|
|
int i = 0;
|
|
|
|
while (p > 0) {
|
|
|
|
p -= s.get(i++).getPriority();
|
|
|
|
}
|
|
|
|
i--;
|
2014-08-31 10:28:21 -04:00
|
|
|
// remove is expensive, but we have only a few entries
|
|
|
|
// anyway
|
2014-04-03 03:42:58 -04:00
|
|
|
SRV srv = s.remove(i);
|
|
|
|
totalweight -= srv.getWeight();
|
|
|
|
result.add(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.shuffle(s, rnd);
|
|
|
|
result.addAll(s);
|
|
|
|
|
2014-03-27 09:31:55 -04:00
|
|
|
}
|
2014-04-03 03:42:58 -04:00
|
|
|
|
|
|
|
if (result.size() == 0) {
|
2014-10-27 16:48:25 -04:00
|
|
|
bundle.putString("error", "nosrv");
|
|
|
|
return bundle;
|
2014-03-25 13:12:27 -04:00
|
|
|
}
|
2014-11-05 15:55:47 -05:00
|
|
|
ArrayList<Bundle> values = new ArrayList<>();
|
2014-04-03 03:42:58 -04:00
|
|
|
for (SRV srv : result) {
|
2014-10-27 16:48:25 -04:00
|
|
|
Bundle namePort = new Bundle();
|
|
|
|
namePort.putString("name", srv.getName());
|
|
|
|
namePort.putInt("port", srv.getPort());
|
2014-12-14 12:31:52 -05:00
|
|
|
if (ips6.containsKey(srv.getName())) {
|
|
|
|
ArrayList<String> ip = ips6.get(srv.getName());
|
|
|
|
Collections.shuffle(ip, rnd);
|
|
|
|
namePort.putString("ip", ip.get(0));
|
|
|
|
values.add(namePort);
|
|
|
|
}
|
2014-10-27 16:48:25 -04:00
|
|
|
if (ips4.containsKey(srv.getName())) {
|
|
|
|
ArrayList<String> ip = ips4.get(srv.getName());
|
|
|
|
Collections.shuffle(ip, rnd);
|
2014-12-14 12:31:52 -05:00
|
|
|
namePort.putString("ip", ip.get(0));
|
|
|
|
values.add(namePort);
|
|
|
|
}
|
|
|
|
if (!ips6.containsKey(srv.getName()) && !ips4.containsKey(srv.getName())) {
|
|
|
|
values.add(namePort);
|
2014-10-27 16:48:25 -04:00
|
|
|
}
|
2014-04-03 03:42:58 -04:00
|
|
|
}
|
2014-10-27 16:48:25 -04:00
|
|
|
bundle.putParcelableArrayList("values", values);
|
2014-04-03 04:12:50 -04:00
|
|
|
} catch (SocketTimeoutException e) {
|
2014-10-27 16:48:25 -04:00
|
|
|
bundle.putString("error", "timeout");
|
2014-04-03 04:41:21 -04:00
|
|
|
} catch (Exception e) {
|
2014-10-27 16:48:25 -04:00
|
|
|
bundle.putString("error", "unhandled");
|
2014-03-27 06:03:10 -04:00
|
|
|
}
|
2014-10-27 16:48:25 -04:00
|
|
|
return bundle;
|
2014-02-07 00:52:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
2014-03-27 09:31:55 -04:00
|
|
|
|
2014-02-07 00:52:09 -05:00
|
|
|
public static String bytesToHex(byte[] bytes) {
|
2014-03-27 09:31:55 -04:00
|
|
|
char[] hexChars = new char[bytes.length * 2];
|
|
|
|
for (int j = 0; j < bytes.length; j++) {
|
|
|
|
int v = bytes[j] & 0xFF;
|
|
|
|
hexChars[j * 2] = hexArray[v >>> 4];
|
|
|
|
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
|
|
|
|
}
|
|
|
|
return new String(hexChars);
|
2014-02-07 00:52:09 -05:00
|
|
|
}
|
|
|
|
}
|