use of de.measite.minidns.record.SRV in project Conversations by siacs.
the class DNSHelper method queryDNS.
public static Bundle queryDNS(String host, InetAddress dnsServer) {
Bundle bundle = new Bundle();
try {
client.setTimeout(Config.SOCKET_TIMEOUT * 1000);
final String qname = "_xmpp-client._tcp." + host.toLowerCase(Locale.US);
final String tlsQname = "_xmpps-client._tcp." + host.toLowerCase(Locale.US);
Log.d(Config.LOGTAG, "using dns server: " + dnsServer.getHostAddress() + " to look up " + host);
final Map<Integer, List<TlsSrv>> priorities = new TreeMap<>();
final Map<String, List<String>> ips4 = new TreeMap<>();
final Map<String, List<String>> ips6 = new TreeMap<>();
fillSrvMaps(qname, dnsServer, priorities, ips4, ips6, false);
fillSrvMaps(tlsQname, dnsServer, priorities, ips4, ips6, true);
final List<TlsSrv> result = new ArrayList<>();
for (final List<TlsSrv> s : priorities.values()) {
result.addAll(s);
}
final ArrayList<Bundle> values = new ArrayList<>();
if (result.size() == 0) {
DNSMessage response;
try {
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(), false));
}
} catch (SocketTimeoutException e) {
Log.d(Config.LOGTAG, "ignoring timeout exception when querying A record on " + dnsServer.getHostAddress());
}
try {
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(), false));
}
} catch (SocketTimeoutException e) {
Log.d(Config.LOGTAG, "ignoring timeout exception when querying AAAA record on " + dnsServer.getHostAddress());
}
values.add(createNamePortBundle(host, 5222, false));
bundle.putParcelableArrayList("values", values);
return bundle;
}
for (final TlsSrv tlsSrv : result) {
final SRV srv = tlsSrv.srv;
final String name = srv.getName() != null ? srv.getName().toLowerCase(Locale.US) : null;
if (ips6.containsKey(name)) {
values.add(createNamePortBundle(name, srv.getPort(), ips6, tlsSrv.tls));
} else {
try {
DNSMessage response = client.query(name, TYPE.AAAA, CLASS.IN, dnsServer.getHostAddress());
for (int i = 0; i < response.getAnswers().length; ++i) {
values.add(createNamePortBundle(name, srv.getPort(), response.getAnswers()[i].getPayload(), tlsSrv.tls));
}
} catch (SocketTimeoutException e) {
Log.d(Config.LOGTAG, "ignoring timeout exception when querying AAAA record on " + dnsServer.getHostAddress());
}
}
if (ips4.containsKey(name)) {
values.add(createNamePortBundle(name, srv.getPort(), ips4, tlsSrv.tls));
} else {
DNSMessage response = client.query(name, TYPE.A, CLASS.IN, dnsServer.getHostAddress());
for (int i = 0; i < response.getAnswers().length; ++i) {
values.add(createNamePortBundle(name, srv.getPort(), response.getAnswers()[i].getPayload(), tlsSrv.tls));
}
}
values.add(createNamePortBundle(name, srv.getPort(), tlsSrv.tls));
}
bundle.putParcelableArrayList("values", values);
} catch (SocketTimeoutException e) {
bundle.putString("error", "timeout");
} catch (Exception e) {
bundle.putString("error", "unhandled");
}
return bundle;
}
use of de.measite.minidns.record.SRV in project Smack by igniterealtime.
the class MiniDnsResolver method lookupSRVRecords0.
@Override
protected List<SRVRecord> lookupSRVRecords0(final String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
final ResolverApi resolver = getResolver(dnssecMode);
ResolverResult<SRV> result;
try {
result = resolver.resolve(name, SRV.class);
} catch (IOException e) {
failedAddresses.add(new HostAddress(name, e));
return null;
}
// TODO: Use ResolverResult.getResolutionUnsuccessfulException() found in newer MiniDNS versions.
if (!result.wasSuccessful()) {
ResolutionUnsuccessfulException resolutionUnsuccessfulException = getExceptionFrom(result);
failedAddresses.add(new HostAddress(name, resolutionUnsuccessfulException));
return null;
}
if (shouldAbortIfNotAuthentic(name, dnssecMode, result, failedAddresses)) {
return null;
}
List<SRVRecord> res = new LinkedList<SRVRecord>();
for (SRV srv : result.getAnswers()) {
String hostname = srv.name.ace;
List<InetAddress> hostAddresses = lookupHostAddress0(hostname, failedAddresses, dnssecMode);
if (hostAddresses == null) {
continue;
}
SRVRecord srvRecord = new SRVRecord(hostname, srv.port, srv.priority, srv.weight, hostAddresses);
res.add(srvRecord);
}
return res;
}
use of de.measite.minidns.record.SRV in project Conversations by siacs.
the class DNSHelper method fillSrvMaps.
private static void fillSrvMaps(final String qname, final InetAddress dnsServer, final Map<Integer, List<TlsSrv>> priorities, final Map<String, List<String>> ips4, final Map<String, List<String>> ips6, final boolean tls) throws IOException {
final DNSMessage message = client.query(qname, TYPE.SRV, CLASS.IN, dnsServer.getHostAddress());
for (Record[] rrset : new Record[][] { message.getAnswers(), message.getAdditionalResourceRecords() }) {
for (Record rr : rrset) {
Data d = rr.getPayload();
final String name = rr.getName() != null ? rr.getName().toLowerCase(Locale.US) : null;
if (d instanceof SRV && NameUtil.idnEquals(qname, name)) {
SRV srv = (SRV) d;
if (!priorities.containsKey(srv.getPriority())) {
priorities.put(srv.getPriority(), new ArrayList<TlsSrv>());
}
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) {
A a = (A) d;
if (!ips4.containsKey(name)) {
ips4.put(name, new ArrayList<String>());
}
ips4.get(name).add(a.toString());
}
if (d instanceof AAAA) {
AAAA aaaa = (AAAA) d;
if (!ips6.containsKey(name)) {
ips6.put(name, new ArrayList<String>());
}
ips6.get(name).add("[" + aaaa.toString() + "]");
}
}
}
}
Aggregations