Search in sources :

Example 1 with SRV

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;
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketTimeoutException(java.net.SocketTimeoutException) SRV(de.measite.minidns.record.SRV) ArrayList(java.util.ArrayList) List(java.util.List) DNSMessage(de.measite.minidns.DNSMessage)

Example 2 with SRV

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;
}
Also used : SRV(de.measite.minidns.record.SRV) ResolutionUnsuccessfulException(de.measite.minidns.hla.ResolutionUnsuccessfulException) ResolverApi(de.measite.minidns.hla.ResolverApi) DnssecResolverApi(de.measite.minidns.hla.DnssecResolverApi) IOException(java.io.IOException) SRVRecord(org.jivesoftware.smack.util.dns.SRVRecord) HostAddress(org.jivesoftware.smack.util.dns.HostAddress) InetAddress(java.net.InetAddress) LinkedList(java.util.LinkedList)

Example 3 with SRV

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() + "]");
            }
        }
    }
}
Also used : AAAA(de.measite.minidns.record.AAAA) A(de.measite.minidns.record.A) SRV(de.measite.minidns.record.SRV) Record(de.measite.minidns.Record) Data(de.measite.minidns.record.Data) AAAA(de.measite.minidns.record.AAAA) DNSMessage(de.measite.minidns.DNSMessage)

Aggregations

SRV (de.measite.minidns.record.SRV)3 DNSMessage (de.measite.minidns.DNSMessage)2 IOException (java.io.IOException)2 Bundle (android.os.Bundle)1 Record (de.measite.minidns.Record)1 DnssecResolverApi (de.measite.minidns.hla.DnssecResolverApi)1 ResolutionUnsuccessfulException (de.measite.minidns.hla.ResolutionUnsuccessfulException)1 ResolverApi (de.measite.minidns.hla.ResolverApi)1 A (de.measite.minidns.record.A)1 AAAA (de.measite.minidns.record.AAAA)1 Data (de.measite.minidns.record.Data)1 InetAddress (java.net.InetAddress)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 HostAddress (org.jivesoftware.smack.util.dns.HostAddress)1 SRVRecord (org.jivesoftware.smack.util.dns.SRVRecord)1