Search in sources :

Example 6 with SRV

use of org.minidns.record.SRV in project Smack by igniterealtime.

the class JavaxResolver method lookupSrvRecords0.

@Override
protected List<SRV> lookupSrvRecords0(DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
    Attribute srvAttribute;
    try {
        Attributes dnsLookup = dirContext.getAttributes(name.ace, new String[] { "SRV" });
        srvAttribute = dnsLookup.get("SRV");
        if (srvAttribute == null)
            return null;
    } catch (NameNotFoundException e) {
        LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
        return null;
    } catch (NamingException e) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
        lookupFailures.add(failure);
        return null;
    }
    List<SRV> res = new ArrayList<>();
    try {
        @SuppressWarnings("unchecked") NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
        while (srvRecords.hasMore()) {
            String srvRecordString = srvRecords.next();
            String[] srvRecordEntries = srvRecordString.split(" ");
            int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
            int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
            int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
            String srvTarget = srvRecordEntries[srvRecordEntries.length - 1];
            // having a trailing dot, so this can possibly be removed in future Smack versions.
            if (srvTarget.length() > 0 && srvTarget.charAt(srvTarget.length() - 1) == '.') {
                srvTarget = srvTarget.substring(0, srvTarget.length() - 1);
            }
            SRV srvRecord = new SRV(priority, weight, port, srvTarget);
            res.add(srvRecord);
        }
    } catch (NamingException e) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
        lookupFailures.add(failure);
    }
    return res;
}
Also used : Attribute(javax.naming.directory.Attribute) NameNotFoundException(javax.naming.NameNotFoundException) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) NamingEnumeration(javax.naming.NamingEnumeration) SRV(org.minidns.record.SRV) NamingException(javax.naming.NamingException)

Example 7 with SRV

use of org.minidns.record.SRV in project Smack by igniterealtime.

the class RemoteXmppTcpConnectionEndpoints method resolveDomain.

/**
 * @param domain the domain.
 * @param domainType the XMPP domain type, server or client.
 * @param lookupFailures a list that will be populated with all failures that oocured during lookup.
 * @param dnssecMode the DNSSEC mode.
 * @param dnsResolver the DNS resolver to use.
 * @return a list of resolved host addresses for this domain.
 */
private static List<Rfc6120TcpRemoteConnectionEndpoint> resolveDomain(DnsName domain, DomainType domainType, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode, DNSResolver dnsResolver) {
    List<Rfc6120TcpRemoteConnectionEndpoint> endpoints = new ArrayList<>();
    // Step one: Do SRV lookups
    DnsName srvDomain = DnsName.from(domainType.srvPrefix, domain);
    Collection<SRV> srvRecords = dnsResolver.lookupSrvRecords(srvDomain, lookupFailures, dnssecMode);
    if (srvRecords != null && !srvRecords.isEmpty()) {
        if (LOGGER.isLoggable(Level.FINE)) {
            String logMessage = "Resolved SRV RR for " + srvDomain + ":";
            for (SRV r : srvRecords) logMessage += " " + r;
            LOGGER.fine(logMessage);
        }
        List<SRV> sortedSrvRecords = SrvUtil.sortSrvRecords(srvRecords);
        for (SRV srv : sortedSrvRecords) {
            List<InetAddress> targetInetAddresses = dnsResolver.lookupHostAddress(srv.target, lookupFailures, dnssecMode);
            if (targetInetAddresses != null) {
                SrvXmppRemoteConnectionEndpoint endpoint = new SrvXmppRemoteConnectionEndpoint(srv, targetInetAddresses);
                endpoints.add(endpoint);
            }
        }
    } else {
        LOGGER.info("Could not resolve DNS SRV resource records for " + srvDomain + ". Consider adding those.");
    }
    UInt16 defaultPort;
    switch(domainType) {
        case client:
            defaultPort = UInt16.from(5222);
            break;
        case server:
            defaultPort = UInt16.from(5269);
            break;
        default:
            throw new AssertionError();
    }
    // Step two: Add the hostname to the end of the list
    List<InetAddress> hostAddresses = dnsResolver.lookupHostAddress(domain, lookupFailures, dnssecMode);
    if (hostAddresses != null) {
        for (InetAddress inetAddress : hostAddresses) {
            IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> endpoint = IpTcpRemoteConnectionEndpoint.from(domain, defaultPort, inetAddress);
            endpoints.add(endpoint);
        }
    }
    return endpoints;
}
Also used : DnsName(org.minidns.dnsname.DnsName) ArrayList(java.util.ArrayList) SRV(org.minidns.record.SRV) UInt16(org.jivesoftware.smack.datatypes.UInt16) InetAddress(java.net.InetAddress) InternetAddressRR(org.minidns.record.InternetAddressRR)

Example 8 with SRV

use of org.minidns.record.SRV in project Smack by igniterealtime.

the class DNSJavaResolver method lookupSrvRecords0.

@Override
protected List<SRV> lookupSrvRecords0(DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
    Lookup lookup;
    try {
        lookup = new Lookup(name.ace, Type.SRV);
    } catch (TextParseException e) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
        lookupFailures.add(failure);
        return null;
    }
    Record[] recs = lookup.run();
    if (recs == null) {
        // TODO: When does this happen? Do we want/need to record a lookup failure?
        return null;
    }
    List<SRV> res = new ArrayList<>();
    for (Record record : recs) {
        org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
        if (srvRecord != null && srvRecord.getTarget() != null) {
            DnsName host = DnsName.from(srvRecord.getTarget().toString());
            int port = srvRecord.getPort();
            int priority = srvRecord.getPriority();
            int weight = srvRecord.getWeight();
            SRV r = new SRV(priority, weight, port, host);
            res.add(r);
        }
    }
    return res;
}
Also used : DnsName(org.minidns.dnsname.DnsName) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) ArrayList(java.util.ArrayList) SRV(org.minidns.record.SRV) Lookup(org.xbill.DNS.Lookup) Record(org.xbill.DNS.Record) TextParseException(org.xbill.DNS.TextParseException)

Aggregations

SRV (org.minidns.record.SRV)8 ArrayList (java.util.ArrayList)6 RemoteConnectionEndpointLookupFailure (org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)2 DnsName (org.minidns.dnsname.DnsName)2 A (org.minidns.record.A)2 InternetAddressRR (org.minidns.record.InternetAddressRR)2 InetAddress (java.net.InetAddress)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingEnumeration (javax.naming.NamingEnumeration)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 Attributes (javax.naming.directory.Attributes)1 UInt16 (org.jivesoftware.smack.datatypes.UInt16)1 Test (org.junit.Test)1 DNSMessage (org.minidns.dnsmessage.DNSMessage)1 AAAA (org.minidns.record.AAAA)1 Data (org.minidns.record.Data)1