lower case incoming dns records

This commit is contained in:
Daniel Gultsch 2016-11-08 10:14:34 +01:00
parent 00b3d5ee35
commit 1a57599da2

View file

@ -19,6 +19,7 @@ import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Random; import java.util.Random;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Map; import java.util.Map;
@ -148,26 +149,29 @@ public class DNSHelper {
for (Record[] rrset : new Record[][] { message.getAnswers(), message.getAdditionalResourceRecords() }) { for (Record[] rrset : new Record[][] { message.getAnswers(), message.getAdditionalResourceRecords() }) {
for (Record rr : rrset) { for (Record rr : rrset) {
Data d = rr.getPayload(); Data d = rr.getPayload();
if (d instanceof SRV && NameUtil.idnEquals(qname, rr.getName())) { final String name = rr.getName() != null ? rr.getName().toLowerCase(Locale.US) : null;
if (d instanceof SRV && NameUtil.idnEquals(qname, name)) {
SRV srv = (SRV) d; SRV srv = (SRV) d;
if (!priorities.containsKey(srv.getPriority())) { if (!priorities.containsKey(srv.getPriority())) {
priorities.put(srv.getPriority(),new ArrayList<TlsSrv>()); priorities.put(srv.getPriority(),new ArrayList<TlsSrv>());
} }
priorities.get(srv.getPriority()).add(new TlsSrv(srv, tls)); priorities.get(srv.getPriority()).add(new TlsSrv(srv, tls));
} else if (d instanceof SRV) {
Log.d(Config.LOGTAG,"found unrecognized SRV record with name: "+name);
} }
if (d instanceof A) { if (d instanceof A) {
A a = (A) d; A a = (A) d;
if (!ips4.containsKey(rr.getName())) { if (!ips4.containsKey(name)) {
ips4.put(rr.getName(), new ArrayList<String>()); ips4.put(name, new ArrayList<String>());
} }
ips4.get(rr.getName()).add(a.toString()); ips4.get(name).add(a.toString());
} }
if (d instanceof AAAA) { if (d instanceof AAAA) {
AAAA aaaa = (AAAA) d; AAAA aaaa = (AAAA) d;
if (!ips6.containsKey(rr.getName())) { if (!ips6.containsKey(name)) {
ips6.put(rr.getName(), new ArrayList<String>()); ips6.put(name, new ArrayList<String>());
} }
ips6.get(rr.getName()).add("[" + aaaa.toString() + "]"); ips6.get(name).add("[" + aaaa.toString() + "]");
} }
} }
} }
@ -177,8 +181,8 @@ public class DNSHelper {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
try { try {
client.setTimeout(Config.SOCKET_TIMEOUT * 1000); client.setTimeout(Config.SOCKET_TIMEOUT * 1000);
final String qname = "_xmpp-client._tcp." + host; final String qname = "_xmpp-client._tcp." + host.toLowerCase(Locale.US);
final String tlsQname = "_xmpps-client._tcp." + host; final String tlsQname = "_xmpps-client._tcp." + host.toLowerCase(Locale.US);
Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host); Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);
final Map<Integer, List<TlsSrv>> priorities = new TreeMap<>(); final Map<Integer, List<TlsSrv>> priorities = new TreeMap<>();
@ -218,27 +222,28 @@ public class DNSHelper {
} }
for (final TlsSrv tlsSrv : result) { for (final TlsSrv tlsSrv : result) {
final SRV srv = tlsSrv.srv; final SRV srv = tlsSrv.srv;
if (ips6.containsKey(srv.getName())) { final String name = srv.getName() != null ? srv.getName().toLowerCase(Locale.US) : null;
values.add(createNamePortBundle(srv.getName(),srv.getPort(),ips6, tlsSrv.tls)); if (ips6.containsKey(name)) {
values.add(createNamePortBundle(name,srv.getPort(),ips6, tlsSrv.tls));
} else { } else {
try { try {
DNSMessage response = client.query(srv.getName(), TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress()); DNSMessage response = client.query(name, TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
for (int i = 0; i < response.getAnswers().length; ++i) { for (int i = 0; i < response.getAnswers().length; ++i) {
values.add(createNamePortBundle(srv.getName(), srv.getPort(), response.getAnswers()[i].getPayload(), tlsSrv.tls)); values.add(createNamePortBundle(name, srv.getPort(), response.getAnswers()[i].getPayload(), tlsSrv.tls));
} }
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
Log.d(Config.LOGTAG,"ignoring timeout exception when querying AAAA record on "+dnsServer.getHostAddress()); Log.d(Config.LOGTAG,"ignoring timeout exception when querying AAAA record on "+dnsServer.getHostAddress());
} }
} }
if (ips4.containsKey(srv.getName())) { if (ips4.containsKey(name)) {
values.add(createNamePortBundle(srv.getName(),srv.getPort(),ips4, tlsSrv.tls)); values.add(createNamePortBundle(name,srv.getPort(),ips4, tlsSrv.tls));
} else { } else {
DNSMessage response = client.query(srv.getName(), TYPE.A, CLASS.IN, dnsServer.getHostAddress()); DNSMessage response = client.query(name, TYPE.A, CLASS.IN, dnsServer.getHostAddress());
for(int i = 0; i < response.getAnswers().length; ++i) { for(int i = 0; i < response.getAnswers().length; ++i) {
values.add(createNamePortBundle(srv.getName(),srv.getPort(),response.getAnswers()[i].getPayload(), tlsSrv.tls)); values.add(createNamePortBundle(name,srv.getPort(),response.getAnswers()[i].getPayload(), tlsSrv.tls));
} }
} }
values.add(createNamePortBundle(srv.getName(), srv.getPort(), tlsSrv.tls)); values.add(createNamePortBundle(name, srv.getPort(), tlsSrv.tls));
} }
bundle.putParcelableArrayList("values", values); bundle.putParcelableArrayList("values", values);
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {