Merge pull request #39 from rtreffer/minidns

Switch to a small DNS library for DNS resolving.
This commit is contained in:
Daniel Gultsch 2014-04-03 09:50:57 +02:00
commit 2efb97efa0
4 changed files with 107 additions and 92 deletions

4
.gitmodules vendored
View File

@ -1,3 +1,7 @@
[submodule "libs/openpgp-keychain"] [submodule "libs/openpgp-keychain"]
path = libs/openpgp-keychain path = libs/openpgp-keychain
url = https://github.com/openpgp-keychain/openpgp-keychain.git url = https://github.com/openpgp-keychain/openpgp-keychain.git
[submodule "libs/minidns"]
path = libs/minidns
url = https://github.com/rtreffer/minidns.git

1
libs/minidns Submodule

@ -0,0 +1 @@
Subproject commit d5cca3b3a48cfaed3008050fcab9574d97d771cc

View File

@ -13,3 +13,4 @@
# Project target. # Project target.
target=android-19 target=android-19
android.library.reference.1=libs/openpgp-keychain/OpenPGP-Keychain-API/libraries/openpgp-api-library android.library.reference.1=libs/openpgp-keychain/OpenPGP-Keychain-API/libraries/openpgp-api-library
android.library.reference.2=libs/minidns

View File

@ -1,125 +1,134 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.utils;
import java.io.ByteArrayOutputStream; 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;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Random; import java.util.Random;
import java.util.TreeMap;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
public class DNSHelper { public class DNSHelper {
public static Bundle getSRVRecord(String host) throws IOException { protected static Client client = new Client();
InetAddress ip = InetAddress.getByName("8.8.8.8");
try {
Class<?> SystemProperties = Class
.forName("android.os.SystemProperties");
Method method = SystemProperties.getMethod("get",
new Class[] { String.class });
ArrayList<String> servers = new ArrayList<String>();
for (String name : new String[] { "net.dns1", "net.dns2",
"net.dns3", "net.dns4", }) {
String value = (String) method.invoke(null, name);
if (value != null && !"".equals(value) public static Bundle getSRVRecord(String host) throws IOException {
&& !servers.contains(value)) { String dns[] = client.findDNS();
ip = InetAddress.getByName(value);
servers.add(value); if (dns != null) {
Bundle result = queryDNS(host, ip); // we have a list of DNS servers, let's go
if (!result.containsKey("error")||("nosrv".equals(result.getString("error")))) { for (String dnsserver : dns) {
return result; InetAddress ip = InetAddress.getByName(dnsserver);
} Bundle b = queryDNS(host, ip);
if (b.containsKey("name")) {
return b;
} }
} }
} catch (Exception e) {
Log.d("xmppService","error during system calls");
} }
ip = InetAddress.getByName("8.8.8.8");
return queryDNS(host, ip); // fallback
return queryDNS(host, InetAddress.getByName("8.8.8.8"));
} }
public static Bundle queryDNS(String host, InetAddress dnsServer) { public static Bundle queryDNS(String host, InetAddress dnsServer) {
Bundle namePort = new Bundle(); Bundle namePort = new Bundle();
try { try {
Log.d("xmppService", "using dns server: " + dnsServer.toString() Log.d("xmppService", "using dns server: " + dnsServer.getHostAddress()
+ " to look up " + host); + " to look up " + host);
String[] hostParts = host.split("\\."); DNSMessage message =
byte[] transId = new byte[2]; client.query(
Random random = new Random(); "_xmpp-client._tcp." + host,
random.nextBytes(transId); TYPE.SRV,
byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, CLASS.IN,
0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63, dnsServer.getHostAddress());
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
ByteArrayOutputStream output = new ByteArrayOutputStream();
output.write(transId);
output.write(header);
for (int i = 0; i < hostParts.length; ++i) {
char[] tmpChars = hostParts[i].toCharArray();
byte[] tmp = new byte[tmpChars.length];
for (int j = 0; j < tmpChars.length; ++j) {
tmp[j] = (byte) tmpChars[j];
}
output.write(tmp.length);
output.write(tmp);
}
output.write(rest);
byte[] sendPaket = output.toByteArray();
int realLenght = sendPaket.length - 11;
DatagramPacket packet = new DatagramPacket(sendPaket,
sendPaket.length, dnsServer, 53);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(packet);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, // How should we handle priorities and weight?
receiveData.length); // Wikipedia has a nice article about priorities vs. weights:
datagramSocket.setSoTimeout(7000); //die sieben ist meine zahl // https://en.wikipedia.org/wiki/SRV_record#Provisioning_for_high_service_availability
datagramSocket.receive(receivePacket);
if (receiveData[3] != -128) { // we bucket the SRV records based on priority, pick per priority
namePort.putString("error", "nosrv"); // a random order respecting the weight, and dump that priority by
return namePort; // priority
}
namePort.putInt( TreeMap<Integer, ArrayList<SRV>> priorities =
"port", new TreeMap<Integer, ArrayList<SRV>>();
calcPort(receiveData[realLenght + 16],
receiveData[realLenght + 17])); for (Record rr : message.getAnswers()) {
int i = realLenght + 18; Data d = rr.getPayload();
int wordLenght = 0; if (d instanceof SRV) {
StringBuilder builder = new StringBuilder(); SRV srv = (SRV) d;
while (receiveData[i] != 0) { if (!priorities.containsKey(srv.getPriority())) {
if (wordLenght > 0) { priorities.put(srv.getPriority(), new ArrayList<SRV>(2));
builder.append((char) receiveData[i]); }
--wordLenght; priorities.get(srv.getPriority()).add(srv);
} else {
wordLenght = receiveData[i];
builder.append(".");
} }
++i;
} }
builder.replace(0, 1, "");
byte type = receiveData[i + 1]; Random rnd = new Random();
byte type2 = receiveData[i + 2]; ArrayList<SRV> result = new ArrayList<SRV>(priorities.size() * 2 + 1);
if ((type == -64) || (type == type2)) { for (ArrayList<SRV> s: priorities.values()) {
namePort.putString("name", builder.toString());
return namePort; // trivial case
} else { if (s.size() <= 1) {
Log.d("xmppService", "type=" + type + " type2=" + type2 + " " result.addAll(s);
+ builder.toString()); continue;
}
long totalweight = 0l;
for (SRV srv: s) {
totalweight += srv.getWeight();
}
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);
}
if (result.size() == 0) {
namePort.putString("error", "nosrv"); namePort.putString("error", "nosrv");
return namePort; return namePort;
} }
// 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++;
}
} catch (IOException e) { } catch (IOException e) {
Log.d("xmppService", "io execpiton during dns"); Log.e("xmppService", "io execpiton during dns", e);
namePort.putString("error", "timeout"); namePort.putString("error", "timeout");
return namePort;
} }
return namePort;
} }
static int calcPort(byte hb, byte lb) { static int calcPort(byte hb, byte lb) {