Search in sources :

Example 1 with InternetAddressRR

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

the class RemoteXmppTcpConnectionEndpoints method lookup.

/**
 * Lookups remote connection endpoints on the server for XMPP connections over TCP taking A, AAAA and SRV resource
 * records into account. If no host address was configured and all lookups failed, for example with NX_DOMAIN, then
 * result will be populated with the empty list.
 *
 * @param config the connection configuration to lookup the endpoints for.
 * @return a lookup result.
 */
public static Result<Rfc6120TcpRemoteConnectionEndpoint> lookup(ConnectionConfiguration config) {
    List<Rfc6120TcpRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints;
    List<RemoteConnectionEndpointLookupFailure> lookupFailures;
    final InetAddress hostAddress = config.getHostAddress();
    final DnsName host = config.getHost();
    if (hostAddress != null) {
        lookupFailures = Collections.emptyList();
        IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(hostAddress.toString(), config.getPort(), hostAddress);
        discoveredRemoteConnectionEndpoints = Collections.singletonList(connectionEndpoint);
    } else if (host != null) {
        lookupFailures = new ArrayList<>(1);
        List<InetAddress> hostAddresses = DNSUtil.getDNSResolver().lookupHostAddress(host, lookupFailures, config.getDnssecMode());
        if (hostAddresses != null) {
            discoveredRemoteConnectionEndpoints = new ArrayList<>(hostAddresses.size());
            UInt16 port = config.getPort();
            for (InetAddress inetAddress : hostAddresses) {
                IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(host, port, inetAddress);
                discoveredRemoteConnectionEndpoints.add(connectionEndpoint);
            }
        } else {
            discoveredRemoteConnectionEndpoints = Collections.emptyList();
        }
    } else {
        lookupFailures = new ArrayList<>();
        // N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName
        DnsName dnsName = config.getXmppServiceDomainAsDnsNameIfPossible();
        if (dnsName == null) {
            // name is also a valid DNS name, or that a host is explicitly configured.
            throw new IllegalStateException();
        }
        discoveredRemoteConnectionEndpoints = resolveXmppServiceDomain(dnsName, lookupFailures, config.getDnssecMode());
    }
    // Either the populated host addresses are not empty *or* there must be at least one failed address.
    assert !discoveredRemoteConnectionEndpoints.isEmpty() || !lookupFailures.isEmpty();
    return new Result<>(discoveredRemoteConnectionEndpoints, lookupFailures);
}
Also used : DnsName(org.minidns.dnsname.DnsName) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UInt16(org.jivesoftware.smack.datatypes.UInt16) InetAddress(java.net.InetAddress) InternetAddressRR(org.minidns.record.InternetAddressRR)

Example 2 with InternetAddressRR

use of org.minidns.record.InternetAddressRR in project minidns by MiniDNS.

the class SrvResolverResult method getSortedSrvResolvedAddresses.

public List<ResolvedSrvRecord> getSortedSrvResolvedAddresses() throws IOException {
    if (sortedSrvResolvedAddresses != null) {
        return sortedSrvResolvedAddresses;
    }
    throwIseIfErrorResponse();
    List<SRV> srvRecords = SrvUtil.sortSrvRecords(getAnswers());
    List<ResolvedSrvRecord> res = new ArrayList<>(srvRecords.size());
    for (SRV srvRecord : srvRecords) {
        ResolverResult<A> aRecordsResult = null;
        ResolverResult<AAAA> aaaaRecordsResult = null;
        Set<A> aRecords = Collections.emptySet();
        if (ipVersion.v4) {
            aRecordsResult = resolver.resolve(srvRecord.target, A.class);
            if (aRecordsResult.wasSuccessful() && !aRecordsResult.hasUnverifiedReasons()) {
                aRecords = aRecordsResult.getAnswers();
            }
        }
        Set<AAAA> aaaaRecords = Collections.emptySet();
        if (ipVersion.v6) {
            aaaaRecordsResult = resolver.resolve(srvRecord.target, AAAA.class);
            if (aaaaRecordsResult.wasSuccessful() && !aaaaRecordsResult.hasUnverifiedReasons()) {
                aaaaRecords = aaaaRecordsResult.getAnswers();
            }
        }
        if (aRecords.isEmpty() && aaaaRecords.isEmpty()) {
            /*
                ResolverResult<CNAME> cnameRecordResult = resolve(srvRecord.name, CNAME.class);
                if (cnameRecordResult.wasSuccessful()) {
                }
                */
            continue;
        }
        List<InternetAddressRR> srvAddresses = new ArrayList<>(aRecords.size() + aaaaRecords.size());
        switch(ipVersion) {
            case v4only:
                for (A a : aRecords) {
                    srvAddresses.add(a);
                }
                break;
            case v6only:
                for (AAAA aaaa : aaaaRecords) {
                    srvAddresses.add(aaaa);
                }
                break;
            case v4v6:
                for (A a : aRecords) {
                    srvAddresses.add(a);
                }
                for (AAAA aaaa : aaaaRecords) {
                    srvAddresses.add(aaaa);
                }
                break;
            case v6v4:
                for (AAAA aaaa : aaaaRecords) {
                    srvAddresses.add(aaaa);
                }
                for (A a : aRecords) {
                    srvAddresses.add(a);
                }
                break;
        }
        ResolvedSrvRecord resolvedSrvAddresses = new ResolvedSrvRecord(question.name, srvRecord, srvAddresses, aRecordsResult, aaaaRecordsResult);
        res.add(resolvedSrvAddresses);
    }
    sortedSrvResolvedAddresses = res;
    return res;
}
Also used : AAAA(org.minidns.record.AAAA) A(org.minidns.record.A) ArrayList(java.util.ArrayList) SRV(org.minidns.record.SRV) AAAA(org.minidns.record.AAAA) InternetAddressRR(org.minidns.record.InternetAddressRR)

Example 3 with InternetAddressRR

use of org.minidns.record.InternetAddressRR 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)

Aggregations

ArrayList (java.util.ArrayList)3 InternetAddressRR (org.minidns.record.InternetAddressRR)3 InetAddress (java.net.InetAddress)2 UInt16 (org.jivesoftware.smack.datatypes.UInt16)2 DnsName (org.minidns.dnsname.DnsName)2 SRV (org.minidns.record.SRV)2 List (java.util.List)1 RemoteConnectionEndpointLookupFailure (org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)1 A (org.minidns.record.A)1 AAAA (org.minidns.record.AAAA)1