mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-16 22:05:07 -05:00
Merge pull request #39 from rtreffer/minidns
Switch to a small DNS library for DNS resolving.
This commit is contained in:
commit
2efb97efa0
4
.gitmodules
vendored
4
.gitmodules
vendored
@ -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
1
libs/minidns
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit d5cca3b3a48cfaed3008050fcab9574d97d771cc
|
@ -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
|
||||||
|
@ -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");
|
// fallback
|
||||||
}
|
return queryDNS(host, InetAddress.getByName("8.8.8.8"));
|
||||||
ip = InetAddress.getByName("8.8.8.8");
|
|
||||||
return queryDNS(host, ip);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
namePort.putInt(
|
// we now have a list of servers to try :-)
|
||||||
"port",
|
|
||||||
calcPort(receiveData[realLenght + 16],
|
// classic name/port pair
|
||||||
receiveData[realLenght + 17]));
|
namePort.putString("name", result.get(0).getName());
|
||||||
int i = realLenght + 18;
|
namePort.putInt("port", result.get(0).getPort());
|
||||||
int wordLenght = 0;
|
|
||||||
StringBuilder builder = new StringBuilder();
|
// add all other records
|
||||||
while (receiveData[i] != 0) {
|
int i = 0;
|
||||||
if (wordLenght > 0) {
|
for (SRV srv : result) {
|
||||||
builder.append((char) receiveData[i]);
|
namePort.putString("name" + i, srv.getName());
|
||||||
--wordLenght;
|
namePort.putInt("port" + i, srv.getPort());
|
||||||
} else {
|
i++;
|
||||||
wordLenght = receiveData[i];
|
|
||||||
builder.append(".");
|
|
||||||
}
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
builder.replace(0, 1, "");
|
|
||||||
byte type = receiveData[i + 1];
|
|
||||||
byte type2 = receiveData[i + 2];
|
|
||||||
if ((type == -64) || (type == type2)) {
|
|
||||||
namePort.putString("name", builder.toString());
|
|
||||||
return namePort;
|
|
||||||
} else {
|
|
||||||
Log.d("xmppService", "type=" + type + " type2=" + type2 + " "
|
|
||||||
+ builder.toString());
|
|
||||||
namePort.putString("error", "nosrv");
|
|
||||||
return namePort;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} 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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user