Search in sources :

Example 6 with HostAddress

use of org.jivesoftware.smack.util.dns.HostAddress in project Smack by igniterealtime.

the class DNSUtilTest method xmppServerDomainTest.

private void xmppServerDomainTest() {
    List<HostAddress> hostAddresses = DNSUtil.resolveXMPPServerDomain(igniterealtimeDomain);
    HostAddress ha = hostAddresses.get(0);
    assertEquals(ha.getFQDN(), igniterealtimeXMPPServer);
    assertEquals(ha.getPort(), igniterealtimeServerPort);
}
Also used : HostAddress(org.jivesoftware.smack.util.dns.HostAddress)

Example 7 with HostAddress

use of org.jivesoftware.smack.util.dns.HostAddress in project Smack by igniterealtime.

the class DNSUtil method resolveDomain.

/**
     * 
     * @param domain the domain.
     * @param domainType the XMPP domain type, server or client.
     * @param failedAddresses a list that will be populated with host addresses that failed to resolve.
     * @return a list of resolver host addresses for this domain.
     */
private static List<HostAddress> resolveDomain(String domain, DomainType domainType, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
    if (dnsResolver == null) {
        throw new IllegalStateException("No DNS Resolver active in Smack");
    }
    List<HostAddress> addresses = new ArrayList<HostAddress>();
    // Step one: Do SRV lookups
    String srvDomain;
    switch(domainType) {
        case Server:
            srvDomain = "_xmpp-server._tcp." + domain;
            break;
        case Client:
            srvDomain = "_xmpp-client._tcp." + domain;
            break;
        default:
            throw new AssertionError();
    }
    List<SRVRecord> srvRecords = dnsResolver.lookupSRVRecords(srvDomain, failedAddresses, dnssecMode);
    if (srvRecords != null && !srvRecords.isEmpty()) {
        if (LOGGER.isLoggable(Level.FINE)) {
            String logMessage = "Resolved SRV RR for " + srvDomain + ":";
            for (SRVRecord r : srvRecords) logMessage += " " + r;
            LOGGER.fine(logMessage);
        }
        List<HostAddress> sortedRecords = sortSRVRecords(srvRecords);
        addresses.addAll(sortedRecords);
    } else {
        LOGGER.info("Could not resolve DNS SRV resource records for " + srvDomain + ". Consider adding those.");
    }
    int defaultPort = -1;
    switch(domainType) {
        case Client:
            defaultPort = 5222;
            break;
        case Server:
            defaultPort = 5269;
            break;
    }
    // Step two: Add the hostname to the end of the list
    HostAddress hostAddress = dnsResolver.lookupHostAddress(domain, defaultPort, failedAddresses, dnssecMode);
    if (hostAddress != null) {
        addresses.add(hostAddress);
    }
    return addresses;
}
Also used : ArrayList(java.util.ArrayList) SRVRecord(org.jivesoftware.smack.util.dns.SRVRecord) HostAddress(org.jivesoftware.smack.util.dns.HostAddress)

Example 8 with HostAddress

use of org.jivesoftware.smack.util.dns.HostAddress in project Smack by igniterealtime.

the class MiniDnsResolver method shouldAbortIfNotAuthentic.

private static boolean shouldAbortIfNotAuthentic(String name, DnssecMode dnssecMode, ResolverResult<?> result, List<HostAddress> failedAddresses) {
    switch(dnssecMode) {
        case needsDnssec:
        case needsDnssecAndDane:
            // TODO: Use ResolverResult.getDnssecResultNotAuthenticException() of newer MiniDNS versions.
            if (!result.isAuthenticData()) {
                Exception exception = new Exception("DNSSEC verification failed: " + result.getUnverifiedReasons().iterator().next().getReasonString());
                failedAddresses.add(new HostAddress(name, exception));
                return true;
            }
            break;
        case disabled:
            break;
        default:
            throw new IllegalStateException("Unknown DnssecMode: " + dnssecMode);
    }
    return false;
}
Also used : HostAddress(org.jivesoftware.smack.util.dns.HostAddress) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ResolutionUnsuccessfulException(de.measite.minidns.hla.ResolutionUnsuccessfulException)

Example 9 with HostAddress

use of org.jivesoftware.smack.util.dns.HostAddress 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 10 with HostAddress

use of org.jivesoftware.smack.util.dns.HostAddress in project Smack by igniterealtime.

the class XMPPTCPConnection method connectUsingConfiguration.

private void connectUsingConfiguration() throws ConnectionException, IOException {
    List<HostAddress> failedAddresses = populateHostAddresses();
    SocketFactory socketFactory = config.getSocketFactory();
    ProxyInfo proxyInfo = config.getProxyInfo();
    int timeout = config.getConnectTimeout();
    if (socketFactory == null) {
        socketFactory = SocketFactory.getDefault();
    }
    for (HostAddress hostAddress : hostAddresses) {
        Iterator<InetAddress> inetAddresses = null;
        String host = hostAddress.getFQDN();
        int port = hostAddress.getPort();
        if (proxyInfo == null) {
            inetAddresses = hostAddress.getInetAddresses().iterator();
            assert (inetAddresses.hasNext());
            innerloop: while (inetAddresses.hasNext()) {
                // Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not
                // re-usable after a failed connection attempt. See also SMACK-724.
                socket = socketFactory.createSocket();
                final InetAddress inetAddress = inetAddresses.next();
                final String inetAddressAndPort = inetAddress + " at port " + port;
                LOGGER.finer("Trying to establish TCP connection to " + inetAddressAndPort);
                try {
                    socket.connect(new InetSocketAddress(inetAddress, port), timeout);
                } catch (Exception e) {
                    hostAddress.setException(inetAddress, e);
                    if (inetAddresses.hasNext()) {
                        continue innerloop;
                    } else {
                        break innerloop;
                    }
                }
                LOGGER.finer("Established TCP connection to " + inetAddressAndPort);
                // We found a host to connect to, return here
                this.host = host;
                this.port = port;
                return;
            }
            failedAddresses.add(hostAddress);
        } else {
            socket = socketFactory.createSocket();
            StringUtils.requireNotNullOrEmpty(host, "Host of HostAddress " + hostAddress + " must not be null when using a Proxy");
            final String hostAndPort = host + " at port " + port;
            LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
            try {
                proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
            } catch (IOException e) {
                hostAddress.setException(e);
                continue;
            }
            LOGGER.finer("Established TCP connection to " + hostAndPort);
            // We found a host to connect to, return here
            this.host = host;
            this.port = port;
            return;
        }
    }
    // HostAddresses in the exception
    throw ConnectionException.from(failedAddresses);
}
Also used : ProxyInfo(org.jivesoftware.smack.proxy.ProxyInfo) SocketFactory(javax.net.SocketFactory) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) HostAddress(org.jivesoftware.smack.util.dns.HostAddress) InetAddress(java.net.InetAddress) SynchronizationPoint(org.jivesoftware.smack.SynchronizationPoint) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) FailedNonzaException(org.jivesoftware.smack.XMPPException.FailedNonzaException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) XMPPException(org.jivesoftware.smack.XMPPException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) StreamManagementException(org.jivesoftware.smack.sm.StreamManagementException) AlreadyLoggedInException(org.jivesoftware.smack.SmackException.AlreadyLoggedInException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StreamIdDoesNotMatchException(org.jivesoftware.smack.sm.StreamManagementException.StreamIdDoesNotMatchException) StreamManagementNotEnabledException(org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) SecurityRequiredByServerException(org.jivesoftware.smack.SmackException.SecurityRequiredByServerException) AlreadyConnectedException(org.jivesoftware.smack.SmackException.AlreadyConnectedException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

HostAddress (org.jivesoftware.smack.util.dns.HostAddress)10 IOException (java.io.IOException)4 InetAddress (java.net.InetAddress)4 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)4 UnknownHostException (java.net.UnknownHostException)3 SRVRecord (org.jivesoftware.smack.util.dns.SRVRecord)3 DnssecResolverApi (de.measite.minidns.hla.DnssecResolverApi)2 ResolutionUnsuccessfulException (de.measite.minidns.hla.ResolutionUnsuccessfulException)2 ResolverApi (de.measite.minidns.hla.ResolverApi)2 ConnectionException (org.jivesoftware.smack.SmackException.ConnectionException)2 A (de.measite.minidns.record.A)1 AAAA (de.measite.minidns.record.AAAA)1 SRV (de.measite.minidns.record.SRV)1 InetSocketAddress (java.net.InetSocketAddress)1 KeyManagementException (java.security.KeyManagementException)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1