From 4b10f36e9efbb387cd1a6a73676d1332e009a79c Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 25 Jan 2015 10:07:07 +0100 Subject: [PATCH] Report failed DNS SRV lookup in ConnectionException instead of just logging a warning if the XMPP domain has no DNS SRV lookups, create the failedAddresses list now within DNSUtil and add the information that the SRV lookup failed. --- .../smack/AbstractXMPPConnection.java | 7 ++++-- .../org/jivesoftware/smack/util/DNSUtil.java | 24 +++++++++++++++---- .../smack/tcp/XMPPTCPConnection.java | 4 +--- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index 9870754b2..6ff0bcb96 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -573,8 +573,10 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { /** * Populates {@link #hostAddresses} with at least one host address. * + * @return a list of host addresses where DNS (SRV) RR resolution failed. */ - protected void populateHostAddresses() { + protected List populateHostAddresses() { + List failedAddresses = new LinkedList<>(); // N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName if (config.host != null) { hostAddresses = new ArrayList(1); @@ -582,11 +584,12 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { hostAddress = new HostAddress(config.host, config.port); hostAddresses.add(hostAddress); } else { - hostAddresses = DNSUtil.resolveXMPPDomain(config.serviceName); + hostAddresses = DNSUtil.resolveXMPPDomain(config.serviceName, failedAddresses); } // If we reach this, then hostAddresses *must not* be empty, i.e. there is at least one host added, either the // config.host one or the host representing the service name by DNSUtil assert(!hostAddresses.isEmpty()); + return failedAddresses; } protected Lock getConnectionLock() { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java index 0ab62180f..01b17183b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/DNSUtil.java @@ -105,10 +105,11 @@ public class DNSUtil { *

* * @param domain the domain. + * @param failedAddresses on optional list that will be populated with host addresses that failed to resolve. * @return List of HostAddress, which encompasses the hostname and port that the * XMPP server can be reached at for the specified domain. */ - public static List resolveXMPPDomain(String domain) { + public static List resolveXMPPDomain(String domain, List failedAddresses) { domain = idnaTransformer.transform(domain); if (dnsResolver == null) { LOGGER.warning("No DNS Resolver active in Smack, will be unable to perform DNS SRV lookups"); @@ -116,7 +117,7 @@ public class DNSUtil { addresses.add(new HostAddress(domain, 5222)); return addresses; } - return resolveDomain(domain, DomainType.Client); + return resolveDomain(domain, DomainType.Client, failedAddresses); } /** @@ -129,10 +130,11 @@ public class DNSUtil { *

* * @param domain the domain. + * @param failedAddresses on optional list that will be populated with host addresses that failed to resolve. * @return List of HostAddress, which encompasses the hostname and port that the * XMPP server can be reached at for the specified domain. */ - public static List resolveXMPPServerDomain(String domain) { + public static List resolveXMPPServerDomain(String domain, List failedAddresses) { domain = idnaTransformer.transform(domain); if (dnsResolver == null) { LOGGER.warning("No DNS Resolver active in Smack, will be unable to perform DNS SRV lookups"); @@ -140,10 +142,17 @@ public class DNSUtil { addresses.add(new HostAddress(domain, 5269)); return addresses; } - return resolveDomain(domain, DomainType.Server); + return resolveDomain(domain, DomainType.Server, failedAddresses); } - private static List resolveDomain(String domain, DomainType domainType) { + /** + * + * @param domain the domain. + * @param domainType the XMPP domain type, server or client. + * @param failedAddresses on optional 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 resolveDomain(String domain, DomainType domainType, List failedAddresses) { List addresses = new ArrayList(); // Step one: Do SRV lookups @@ -172,6 +181,11 @@ public class DNSUtil { catch (Exception e) { LOGGER.log(Level.WARNING, "Exception while resovling SRV records for " + domain + ". Consider adding '_xmpp-(server|client)._tcp' DNS SRV Records", e); + if (failedAddresses != null) { + HostAddress failedHostAddress = new HostAddress(srvDomain); + failedHostAddress.setException(e); + failedAddresses.add(failedHostAddress); + } } // Step two: Add the hostname to the end of the list diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 43570bef2..f123500a0 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -517,9 +517,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection { } private void connectUsingConfiguration() throws IOException, ConnectionException { - populateHostAddresses(); - - List failedAddresses = new LinkedList(); + List failedAddresses = populateHostAddresses(); SocketFactory socketFactory = config.getSocketFactory(); if (socketFactory == null) { socketFactory = SocketFactory.getDefault();