Merge pull request #40 from rtreffer/minidns

Add ability to use a provided ipv4 address on connect
This commit is contained in:
Daniel Gultsch 2014-04-03 11:13:45 +02:00
commit e70c0cfffd
2 changed files with 58 additions and 12 deletions

View File

@ -6,7 +6,10 @@ import de.measite.minidns.Record;
import de.measite.minidns.Record.TYPE; import de.measite.minidns.Record.TYPE;
import de.measite.minidns.Record.CLASS; import de.measite.minidns.Record.CLASS;
import de.measite.minidns.record.SRV; import de.measite.minidns.record.SRV;
import de.measite.minidns.record.A;
import de.measite.minidns.record.AAAA;
import de.measite.minidns.record.Data; import de.measite.minidns.record.Data;
import de.measite.minidns.util.NameUtil;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
@ -43,12 +46,13 @@ public class DNSHelper {
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 {
String qname = "_xmpp-client._tcp." + host;
Log.d("xmppService", "using dns server: " + dnsServer.getHostAddress() Log.d("xmppService", "using dns server: " + dnsServer.getHostAddress()
+ " to look up " + host); + " to look up " + host);
DNSMessage message = DNSMessage message =
client.query( client.query(
"_xmpp-client._tcp." + host, qname,
TYPE.SRV, TYPE.ANY,
CLASS.IN, CLASS.IN,
dnsServer.getHostAddress()); dnsServer.getHostAddress());
@ -62,16 +66,37 @@ public class DNSHelper {
TreeMap<Integer, ArrayList<SRV>> priorities = TreeMap<Integer, ArrayList<SRV>> priorities =
new TreeMap<Integer, ArrayList<SRV>>(); new TreeMap<Integer, ArrayList<SRV>>();
TreeMap<String, ArrayList<String>> ips4 =
new TreeMap<String, ArrayList<String>>();
TreeMap<String, ArrayList<String>> ips6 =
new TreeMap<String, ArrayList<String>>();
for (Record rr : message.getAnswers()) { for (Record[] rrset : new Record[][]{ message.getAnswers(),
message.getAdditionalResourceRecords()}) {
for (Record rr : rrset) {
Data d = rr.getPayload(); Data d = rr.getPayload();
if (d instanceof SRV) { if (d instanceof SRV && NameUtil.idnEquals(qname,rr.getName())) {
SRV srv = (SRV) d; SRV srv = (SRV) d;
if (!priorities.containsKey(srv.getPriority())) { if (!priorities.containsKey(srv.getPriority())) {
priorities.put(srv.getPriority(), new ArrayList<SRV>(2)); priorities.put(srv.getPriority(), new ArrayList<SRV>(2));
} }
priorities.get(srv.getPriority()).add(srv); 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() + "]");
}
}
} }
Random rnd = new Random(); Random rnd = new Random();
@ -114,9 +139,22 @@ public class DNSHelper {
// we now have a list of servers to try :-) // we now have a list of servers to try :-)
// classic name/port pair // classic name/port pair
namePort.putString("name", result.get(0).getName()); String resultName = result.get(0).getName();
namePort.putString("name", resultName);
namePort.putInt("port", result.get(0).getPort()); namePort.putInt("port", result.get(0).getPort());
if (ips4.containsKey(resultName)) {
// we have an ip!
ArrayList<String> ip = ips4.get(resultName);
Collections.shuffle(ip, rnd);
namePort.putString("ipv4", ip.get(0));
}
if (ips6.containsKey(resultName)) {
ArrayList<String> ip = ips6.get(resultName);
Collections.shuffle(ip, rnd);
namePort.putString("ipv6", ip.get(0));
}
// add all other records // add all other records
int i = 0; int i = 0;
for (SRV srv : result) { for (SRV srv : result) {

View File

@ -128,11 +128,19 @@ public class XmppConnection implements Runnable {
return; return;
} }
String srvRecordServer = namePort.getString("name"); String srvRecordServer = namePort.getString("name");
String srvIpServer = namePort.getString("ipv4");
int srvRecordPort = namePort.getInt("port"); int srvRecordPort = namePort.getInt("port");
if (srvRecordServer != null) { if (srvRecordServer != null) {
if (srvIpServer != null) {
Log.d(LOGTAG, account.getJid() + ": using values from dns "
+ srvRecordServer + "[" + srvIpServer + "]:"
+ srvRecordPort);
socket = new Socket(srvIpServer, srvRecordPort);
} else {
Log.d(LOGTAG, account.getJid() + ": using values from dns " Log.d(LOGTAG, account.getJid() + ": using values from dns "
+ srvRecordServer + ":" + srvRecordPort); + srvRecordServer + ":" + srvRecordPort);
socket = new Socket(srvRecordServer, srvRecordPort); socket = new Socket(srvRecordServer, srvRecordPort);
}
} else { } else {
socket = new Socket(account.getServer(), 5222); socket = new Socket(account.getServer(), 5222);
} }