Search in sources :

Example 1 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.

the class MiniDnsResolver method lookupHostAddress0.

@Override
protected List<InetAddress> lookupHostAddress0(final DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
    final ResolverApi resolver = getResolver(dnssecMode);
    final ResolverResult<A> aResult;
    final ResolverResult<AAAA> aaaaResult;
    try {
        aResult = resolver.resolve(name, A.class);
        aaaaResult = resolver.resolve(name, AAAA.class);
    } catch (IOException e) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
        lookupFailures.add(failure);
        return null;
    }
    if (!aResult.wasSuccessful() && !aaaaResult.wasSuccessful()) {
        // Both results where not successful.
        RemoteConnectionEndpointLookupFailure failureA = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, getExceptionFrom(aResult));
        lookupFailures.add(failureA);
        RemoteConnectionEndpointLookupFailure failureAaaa = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, getExceptionFrom(aaaaResult));
        lookupFailures.add(failureAaaa);
        return null;
    }
    if (shouldAbortIfNotAuthentic(name, dnssecMode, aResult, lookupFailures) || shouldAbortIfNotAuthentic(name, dnssecMode, aaaaResult, lookupFailures)) {
        return null;
    }
    // TODO: Use ResolverResult.getAnswersOrEmptySet() once we updated MiniDNS.
    Set<A> aResults;
    if (aResult.wasSuccessful()) {
        aResults = aResult.getAnswers();
    } else {
        aResults = Collections.emptySet();
    }
    // TODO: Use ResolverResult.getAnswersOrEmptySet() once we updated MiniDNS.
    Set<AAAA> aaaaResults;
    if (aaaaResult.wasSuccessful()) {
        aaaaResults = aaaaResult.getAnswers();
    } else {
        aaaaResults = Collections.emptySet();
    }
    List<InetAddress> inetAddresses = new ArrayList<>(aResults.size() + aaaaResults.size());
    for (A a : aResults) {
        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByAddress(a.getIp());
        } catch (UnknownHostException e) {
            continue;
        }
        inetAddresses.add(inetAddress);
    }
    for (AAAA aaaa : aaaaResults) {
        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByAddress(name.ace, aaaa.getIp());
        } catch (UnknownHostException e) {
            continue;
        }
        inetAddresses.add(inetAddress);
    }
    return inetAddresses;
}
Also used : AAAA(org.minidns.record.AAAA) A(org.minidns.record.A) UnknownHostException(java.net.UnknownHostException) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) DnssecResolverApi(org.minidns.hla.DnssecResolverApi) ResolverApi(org.minidns.hla.ResolverApi) ArrayList(java.util.ArrayList) IOException(java.io.IOException) InetAddress(java.net.InetAddress) AAAA(org.minidns.record.AAAA)

Example 2 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.

the class MiniDnsResolver method shouldAbortIfNotAuthentic.

private static boolean shouldAbortIfNotAuthentic(DnsName name, DnssecMode dnssecMode, ResolverResult<?> result, List<RemoteConnectionEndpointLookupFailure> lookupFailures) {
    switch(dnssecMode) {
        case needsDnssec:
        case needsDnssecAndDane:
            // Check if the result is authentic data, i.e. there a no reasons the result is unverified.
            DnssecResultNotAuthenticException exception = result.getDnssecResultNotAuthenticException();
            if (exception != null) {
                RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, exception);
                lookupFailures.add(failure);
                return true;
            }
            break;
        case disabled:
            break;
        default:
            throw new IllegalStateException("Unknown DnssecMode: " + dnssecMode);
    }
    return false;
}
Also used : DnssecResultNotAuthenticException(org.minidns.dnssec.DnssecResultNotAuthenticException) RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)

Example 3 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure 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 4 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.

the class MiniDnsResolver method lookupSrvRecords0.

@Override
protected Set<SRV> lookupSrvRecords0(final DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
    final ResolverApi resolver = getResolver(dnssecMode);
    SrvResolverResult result;
    try {
        result = resolver.resolveSrv(name);
    } catch (IOException e) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
        lookupFailures.add(failure);
        return null;
    }
    ResolutionUnsuccessfulException resolutionUnsuccessfulException = result.getResolutionUnsuccessfulException();
    if (resolutionUnsuccessfulException != null) {
        RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, resolutionUnsuccessfulException);
        lookupFailures.add(failure);
        return null;
    }
    if (shouldAbortIfNotAuthentic(name, dnssecMode, result, lookupFailures)) {
        return null;
    }
    return result.getAnswers();
}
Also used : RemoteConnectionEndpointLookupFailure(org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure) ResolutionUnsuccessfulException(org.minidns.hla.ResolutionUnsuccessfulException) DnssecResolverApi(org.minidns.hla.DnssecResolverApi) ResolverApi(org.minidns.hla.ResolverApi) IOException(java.io.IOException) SrvResolverResult(org.minidns.hla.SrvResolverResult)

Example 5 with RemoteConnectionEndpointLookupFailure

use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure 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)

Aggregations

RemoteConnectionEndpointLookupFailure (org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)3 InetAddress (java.net.InetAddress)2 UInt16 (org.jivesoftware.smack.datatypes.UInt16)2 DnsName (org.minidns.dnsname.DnsName)2 DnssecResolverApi (org.minidns.hla.DnssecResolverApi)2 ResolverApi (org.minidns.hla.ResolverApi)2 A (org.minidns.record.A)2 SRV (org.minidns.record.SRV)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 List (java.util.List)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 SmackException (org.jivesoftware.smack.SmackException)1