aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2015-08-11 20:30:11 +0200
committerlookshe <github@lookshe.org>2015-08-11 20:30:11 +0200
commit639babfdb5289a035e0a22bf607c068caefa5c99 (patch)
tree6a11aa6a189f3a5988d5d1e1c733d2027bc3494f /src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java
parent1b5966ae3b1108c88a810d7d32a0aefa8812d11f (diff)
parent8fd688ca96005152be754eeba1be72c7c0aab9ad (diff)
Merge branch 'trz/rebase' into trz/rename
Conflicts: build.gradle src/main/java/de/thedevstack/conversationsplus/crypto/OtrEngine.java src/main/java/de/thedevstack/conversationsplus/entities/Account.java src/main/java/de/thedevstack/conversationsplus/entities/Contact.java src/main/java/de/thedevstack/conversationsplus/entities/Downloadable.java src/main/java/de/thedevstack/conversationsplus/entities/DownloadableFile.java src/main/java/de/thedevstack/conversationsplus/entities/DownloadablePlaceholder.java src/main/java/de/thedevstack/conversationsplus/entities/Message.java src/main/java/de/thedevstack/conversationsplus/generator/IqGenerator.java src/main/java/de/thedevstack/conversationsplus/http/HttpConnection.java src/main/java/de/thedevstack/conversationsplus/http/HttpConnectionManager.java src/main/java/de/thedevstack/conversationsplus/parser/AbstractParser.java src/main/java/de/thedevstack/conversationsplus/parser/MessageParser.java src/main/java/de/thedevstack/conversationsplus/parser/PresenceParser.java src/main/java/de/thedevstack/conversationsplus/persistance/DatabaseBackend.java src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java src/main/java/de/thedevstack/conversationsplus/services/NotificationService.java src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java src/main/java/de/thedevstack/conversationsplus/ui/ConversationActivity.java src/main/java/de/thedevstack/conversationsplus/ui/ConversationFragment.java src/main/java/de/thedevstack/conversationsplus/ui/SettingsActivity.java src/main/java/de/thedevstack/conversationsplus/ui/StartConversationActivity.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/AccountAdapter.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/ConversationAdapter.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/ListItemAdapter.java src/main/java/de/thedevstack/conversationsplus/ui/adapter/MessageAdapter.java src/main/java/de/thedevstack/conversationsplus/utils/UIHelper.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleConnection.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleConnectionManager.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleInbandTransport.java src/main/java/de/thedevstack/conversationsplus/xmpp/jingle/JingleSocks5Transport.java src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/MessagePacket.java src/main/java/eu/siacs/conversations/crypto/OtrEngine.java src/main/java/eu/siacs/conversations/crypto/OtrService.java src/main/java/eu/siacs/conversations/entities/DownloadablePlaceholder.java src/main/java/eu/siacs/conversations/entities/TransferablePlaceholder.java src/main/java/eu/siacs/conversations/http/HttpConnection.java src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java src/main/res/layout/activity_about.xml
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java136
1 files changed, 66 insertions, 70 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java b/src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java
index 1eedfdfd..8951426f 100644
--- a/src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java
+++ b/src/main/java/de/thedevstack/conversationsplus/utils/DNSHelper.java
@@ -20,11 +20,19 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.TreeMap;
+import java.util.regex.Pattern;
import android.os.Bundle;
import android.util.Log;
public class DNSHelper {
+
+ public static final Pattern PATTERN_IPV4 = Pattern.compile("\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z");
+ public static final Pattern PATTERN_IPV6_HEX4DECCOMPRESSED = Pattern.compile("\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::((?:[0-9A-Fa-f]{1,4}:)*)(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z");
+ public static final Pattern PATTERN_IPV6_6HEX4DEC = Pattern.compile("\\A((?:[0-9A-Fa-f]{1,4}:){6,6})(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z");
+ public static final Pattern PATTERN_IPV6_HEXCOMPRESSED = Pattern.compile("\\A((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)\\z");
+ public static final Pattern PATTERN_IPV6 = Pattern.compile("\\A(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\\z");
+
protected static Client client = new Client();
public static Bundle getSRVRecord(final Jid jid) throws IOException {
@@ -37,9 +45,6 @@ public class DNSHelper {
Bundle b = queryDNS(host, ip);
if (b.containsKey("values")) {
return b;
- } else if (b.containsKey("error")
- && "nosrv".equals(b.getString("error", null))) {
- return b;
}
}
}
@@ -50,113 +55,96 @@ public class DNSHelper {
Bundle bundle = new Bundle();
try {
String qname = "_xmpp-client._tcp." + host;
- Log.d(Config.LOGTAG,
- "using dns server: " + dnsServer.getHostAddress()
- + " to look up " + host);
- DNSMessage message = client.query(qname, 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
+ Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);
+ DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN, dnsServer.getHostAddress());
TreeMap<Integer, ArrayList<SRV>> priorities = new TreeMap<>();
TreeMap<String, ArrayList<String>> ips4 = new TreeMap<>();
TreeMap<String, ArrayList<String>> ips6 = new TreeMap<>();
- for (Record[] rrset : new Record[][] { message.getAnswers(),
- message.getAdditionalResourceRecords() }) {
+ for (Record[] rrset : new Record[][] { message.getAnswers(), message.getAdditionalResourceRecords() }) {
for (Record rr : rrset) {
Data d = rr.getPayload();
- if (d instanceof SRV
- && NameUtil.idnEquals(qname, rr.getName())) {
+ if (d instanceof SRV && NameUtil.idnEquals(qname, rr.getName())) {
SRV srv = (SRV) d;
if (!priorities.containsKey(srv.getPriority())) {
- priorities.put(srv.getPriority(),
- new ArrayList<SRV>(2));
+ priorities.put(srv.getPriority(),new ArrayList<SRV>());
}
priorities.get(srv.getPriority()).add(srv);
}
if (d instanceof A) {
- A arecord = (A) d;
+ A a = (A) d;
if (!ips4.containsKey(rr.getName())) {
- ips4.put(rr.getName(), new ArrayList<String>(3));
+ ips4.put(rr.getName(), new ArrayList<String>());
}
- ips4.get(rr.getName()).add(arecord.toString());
+ ips4.get(rr.getName()).add(a.toString());
}
if (d instanceof AAAA) {
AAAA aaaa = (AAAA) d;
if (!ips6.containsKey(rr.getName())) {
- ips6.put(rr.getName(), new ArrayList<String>(3));
+ ips6.put(rr.getName(), new ArrayList<String>());
}
ips6.get(rr.getName()).add("[" + aaaa.toString() + "]");
}
}
}
- Random rnd = new Random();
- ArrayList<SRV> result = new ArrayList<>(
- priorities.size() * 2 + 1);
+ ArrayList<SRV> result = new ArrayList<>();
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);
-
}
+ ArrayList<Bundle> values = new ArrayList<>();
if (result.size() == 0) {
- bundle.putString("error", "nosrv");
+ DNSMessage response;
+ response = client.query(host, TYPE.A, CLASS.IN, dnsServer.getHostAddress());
+ for(int i = 0; i < response.getAnswers().length; ++i) {
+ values.add(createNamePortBundle(host,5222,response.getAnswers()[i].getPayload()));
+ }
+ response = client.query(host, TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
+ for(int i = 0; i < response.getAnswers().length; ++i) {
+ values.add(createNamePortBundle(host,5222,response.getAnswers()[i].getPayload()));
+ }
+ values.add(createNamePortBundle(host,5222));
+ bundle.putParcelableArrayList("values", values);
return bundle;
}
- ArrayList<Bundle> values = new ArrayList<>();
for (SRV srv : result) {
if (ips6.containsKey(srv.getName())) {
values.add(createNamePortBundle(srv.getName(),srv.getPort(),ips6));
+ } else {
+ DNSMessage response = client.query(srv.getName(), TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
+ for(int i = 0; i < response.getAnswers().length; ++i) {
+ values.add(createNamePortBundle(srv.getName(),srv.getPort(),response.getAnswers()[i].getPayload()));
+ }
}
if (ips4.containsKey(srv.getName())) {
values.add(createNamePortBundle(srv.getName(),srv.getPort(),ips4));
+ } else {
+ DNSMessage response = client.query(srv.getName(), TYPE.A, CLASS.IN, dnsServer.getHostAddress());
+ for(int i = 0; i < response.getAnswers().length; ++i) {
+ values.add(createNamePortBundle(srv.getName(),srv.getPort(),response.getAnswers()[i].getPayload()));
+ }
}
- values.add(createNamePortBundle(srv.getName(),srv.getPort(),null));
+ values.add(createNamePortBundle(srv.getName(), srv.getPort()));
}
bundle.putParcelableArrayList("values", values);
} catch (SocketTimeoutException e) {
bundle.putString("error", "timeout");
} catch (Exception e) {
+ e.printStackTrace();
bundle.putString("error", "unhandled");
}
return bundle;
}
+ private static Bundle createNamePortBundle(String name, int port) {
+ Bundle namePort = new Bundle();
+ namePort.putString("name", name);
+ namePort.putInt("port", port);
+ return namePort;
+ }
+
private static Bundle createNamePortBundle(String name, int port, TreeMap<String, ArrayList<String>> ips) {
Bundle namePort = new Bundle();
namePort.putString("name", name);
@@ -169,15 +157,23 @@ public class DNSHelper {
return namePort;
}
- final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
-
- public static String bytesToHex(byte[] bytes) {
- 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];
+ private static Bundle createNamePortBundle(String name, int port, Data data) {
+ Bundle namePort = new Bundle();
+ namePort.putString("name", name);
+ namePort.putInt("port", port);
+ if (data instanceof A) {
+ namePort.putString("ip", data.toString());
+ } else if (data instanceof AAAA) {
+ namePort.putString("ip","["+data.toString()+"]");
}
- return new String(hexChars);
+ return namePort;
+ }
+
+ public static boolean isIp(final String server) {
+ return PATTERN_IPV4.matcher(server).matches()
+ || PATTERN_IPV6.matcher(server).matches()
+ || PATTERN_IPV6_6HEX4DEC.matcher(server).matches()
+ || PATTERN_IPV6_HEX4DECCOMPRESSED.matcher(server).matches()
+ || PATTERN_IPV6_HEXCOMPRESSED.matcher(server).matches();
}
}