2014-02-28 18:46:01 +01:00
|
|
|
package eu.siacs.conversations.utils;
|
2014-02-07 06:52:09 +01:00
|
|
|
|
2014-04-03 09:42:58 +02: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;
|
|
|
|
import de.measite.minidns.record.Data;
|
|
|
|
|
2014-02-07 06:52:09 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
2014-03-25 17:21:56 +01:00
|
|
|
import java.util.ArrayList;
|
2014-04-03 09:42:58 +02:00
|
|
|
import java.util.Collections;
|
2014-02-07 06:52:09 +01:00
|
|
|
import java.util.Random;
|
2014-04-03 09:42:58 +02:00
|
|
|
import java.util.TreeMap;
|
2014-02-07 06:52:09 +01:00
|
|
|
|
|
|
|
import android.os.Bundle;
|
2014-03-25 17:21:56 +01:00
|
|
|
import android.util.Log;
|
2014-02-07 06:52:09 +01:00
|
|
|
|
|
|
|
public class DNSHelper {
|
2014-04-03 09:42:58 +02:00
|
|
|
protected static Client client = new Client();
|
|
|
|
|
2014-03-06 03:30:03 +01:00
|
|
|
public static Bundle getSRVRecord(String host) throws IOException {
|
2014-04-03 09:42:58 +02:00
|
|
|
String dns[] = client.findDNS();
|
|
|
|
|
|
|
|
if (dns != null) {
|
|
|
|
// we have a list of DNS servers, let's go
|
|
|
|
for (String dnsserver : dns) {
|
|
|
|
InetAddress ip = InetAddress.getByName(dnsserver);
|
|
|
|
Bundle b = queryDNS(host, ip);
|
|
|
|
if (b.containsKey("name")) {
|
|
|
|
return b;
|
2014-03-27 14:31:55 +01:00
|
|
|
}
|
2014-03-25 17:21:56 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-03 09:42:58 +02:00
|
|
|
|
|
|
|
// fallback
|
|
|
|
return queryDNS(host, InetAddress.getByName("8.8.8.8"));
|
2014-03-27 11:03:10 +01:00
|
|
|
}
|
2014-03-27 14:31:55 +01:00
|
|
|
|
2014-03-27 11:03:10 +01:00
|
|
|
public static Bundle queryDNS(String host, InetAddress dnsServer) {
|
2014-02-07 06:52:09 +01:00
|
|
|
Bundle namePort = new Bundle();
|
2014-03-27 11:03:10 +01:00
|
|
|
try {
|
2014-04-03 09:42:58 +02:00
|
|
|
Log.d("xmppService", "using dns server: " + dnsServer.getHostAddress()
|
2014-03-27 14:31:55 +01:00
|
|
|
+ " to look up " + host);
|
2014-04-03 09:42:58 +02:00
|
|
|
DNSMessage message =
|
|
|
|
client.query(
|
|
|
|
"_xmpp-client._tcp." + host,
|
|
|
|
TYPE.SRV,
|
|
|
|
CLASS.IN,
|
|
|
|
dnsServer.getHostAddress());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
TreeMap<Integer, ArrayList<SRV>> priorities =
|
|
|
|
new TreeMap<Integer, ArrayList<SRV>>();
|
|
|
|
|
|
|
|
for (Record rr : message.getAnswers()) {
|
|
|
|
Data d = rr.getPayload();
|
|
|
|
if (d instanceof SRV) {
|
|
|
|
SRV srv = (SRV) d;
|
|
|
|
if (!priorities.containsKey(srv.getPriority())) {
|
|
|
|
priorities.put(srv.getPriority(), new ArrayList<SRV>(2));
|
|
|
|
}
|
|
|
|
priorities.get(srv.getPriority()).add(srv);
|
2014-03-27 14:31:55 +01:00
|
|
|
}
|
2014-02-07 06:52:09 +01:00
|
|
|
}
|
2014-04-03 09:42:58 +02:00
|
|
|
|
|
|
|
Random rnd = new Random();
|
|
|
|
ArrayList<SRV> result = new ArrayList<SRV>(priorities.size() * 2 + 1);
|
|
|
|
for (ArrayList<SRV> s: priorities.values()) {
|
|
|
|
|
|
|
|
// trivial case
|
|
|
|
if (s.size() <= 1) {
|
|
|
|
result.addAll(s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
long totalweight = 0l;
|
|
|
|
for (SRV srv: s) {
|
|
|
|
totalweight += srv.getWeight();
|
2014-03-27 14:31:55 +01:00
|
|
|
}
|
2014-04-03 09:42:58 +02:00
|
|
|
|
|
|
|
while (totalweight > 0l && s.size() > 0) {
|
|
|
|
long p = (rnd.nextLong() & 0x7fffffffffffffffl) % totalweight;
|
|
|
|
int i = 0;
|
|
|
|
while (p > 0) {
|
|
|
|
p -= s.get(i++).getPriority();
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
// remove is expensive, but we have only a few entries anyway
|
|
|
|
SRV srv = s.remove(i);
|
|
|
|
totalweight -= srv.getWeight();
|
|
|
|
result.add(srv);
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.shuffle(s, rnd);
|
|
|
|
result.addAll(s);
|
|
|
|
|
2014-03-27 14:31:55 +01:00
|
|
|
}
|
2014-04-03 09:42:58 +02:00
|
|
|
|
|
|
|
if (result.size() == 0) {
|
2014-03-27 14:31:55 +01:00
|
|
|
namePort.putString("error", "nosrv");
|
|
|
|
return namePort;
|
2014-03-25 18:12:27 +01:00
|
|
|
}
|
2014-04-03 09:42:58 +02:00
|
|
|
// we now have a list of servers to try :-)
|
|
|
|
|
|
|
|
// classic name/port pair
|
|
|
|
namePort.putString("name", result.get(0).getName());
|
|
|
|
namePort.putInt("port", result.get(0).getPort());
|
|
|
|
|
|
|
|
// add all other records
|
|
|
|
int i = 0;
|
|
|
|
for (SRV srv : result) {
|
|
|
|
namePort.putString("name" + i, srv.getName());
|
|
|
|
namePort.putInt("port" + i, srv.getPort());
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2014-03-27 11:03:10 +01:00
|
|
|
} catch (IOException e) {
|
2014-04-03 09:42:58 +02:00
|
|
|
Log.e("xmppService", "io execpiton during dns", e);
|
2014-03-28 19:00:01 +01:00
|
|
|
namePort.putString("error", "timeout");
|
2014-03-27 11:03:10 +01:00
|
|
|
}
|
2014-04-03 09:42:58 +02:00
|
|
|
return namePort;
|
2014-02-07 06:52:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int calcPort(byte hb, byte lb) {
|
2014-03-25 18:12:27 +01:00
|
|
|
int port = ((int) hb << 8) | ((int) lb & 0xFF);
|
2014-03-27 14:31:55 +01:00
|
|
|
if (port >= 0) {
|
2014-03-25 18:12:27 +01:00
|
|
|
return port;
|
|
|
|
} else {
|
|
|
|
return 65536 + port;
|
|
|
|
}
|
2014-02-07 06:52:09 +01:00
|
|
|
}
|
2014-03-27 14:31:55 +01:00
|
|
|
|
2014-02-07 06:52:09 +01:00
|
|
|
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
|
2014-03-27 14:31:55 +01:00
|
|
|
|
2014-02-07 06:52:09 +01:00
|
|
|
public static String bytesToHex(byte[] bytes) {
|
2014-03-27 14:31:55 +01: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 06:52:09 +01:00
|
|
|
}
|
|
|
|
}
|