1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-10 18:15:58 +01:00

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.
This commit is contained in:
Florian Schmaus 2015-01-25 10:07:07 +01:00
parent 30ec2bd072
commit 4b10f36e9e
3 changed files with 25 additions and 10 deletions

View file

@ -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<HostAddress> populateHostAddresses() {
List<HostAddress> failedAddresses = new LinkedList<>();
// N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName
if (config.host != null) {
hostAddresses = new ArrayList<HostAddress>(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() {

View file

@ -105,10 +105,11 @@ public class DNSUtil {
* </p>
*
* @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<HostAddress> resolveXMPPDomain(String domain) {
public static List<HostAddress> resolveXMPPDomain(String domain, List<HostAddress> 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 {
* </p>
*
* @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<HostAddress> resolveXMPPServerDomain(String domain) {
public static List<HostAddress> resolveXMPPServerDomain(String domain, List<HostAddress> 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<HostAddress> 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<HostAddress> resolveDomain(String domain, DomainType domainType, List<HostAddress> failedAddresses) {
List<HostAddress> addresses = new ArrayList<HostAddress>();
// 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

View file

@ -517,9 +517,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
}
private void connectUsingConfiguration() throws IOException, ConnectionException {
populateHostAddresses();
List<HostAddress> failedAddresses = new LinkedList<HostAddress>();
List<HostAddress> failedAddresses = populateHostAddresses();
SocketFactory socketFactory = config.getSocketFactory();
if (socketFactory == null) {
socketFactory = SocketFactory.getDefault();