Add ConnectionConfiguration.getXmppServiceDomainAsDnsNameIfPossible()

in preperation of SMACK-870, so that the DNS name can be used for the
certificate verification.
This commit is contained in:
Florian Schmaus 2019-05-16 14:02:01 +02:00
parent edcde28ecd
commit 381190a45c
1 changed files with 32 additions and 1 deletions

View File

@ -1,6 +1,6 @@
/**
*
* Copyright 2003-2007 Jive Software, 2017-2018 Florian Schmaus.
* Copyright 2003-2007 Jive Software, 2017-2019 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,6 +25,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.SocketFactory;
import javax.net.ssl.HostnameVerifier;
@ -46,6 +48,7 @@ import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.jid.parts.Resourcepart;
import org.jxmpp.stringprep.XmppStringprepException;
import org.minidns.dnsname.DnsName;
import org.minidns.dnsname.InvalidDnsNameException;
import org.minidns.util.InetAddressUtil;
/**
@ -61,6 +64,8 @@ public abstract class ConnectionConfiguration {
SmackConfiguration.getVersion();
}
private static final Logger LOGGER = Logger.getLogger(ConnectionConfiguration.class.getName());
/**
* The XMPP domain of the XMPP Service. Usually servers use the same service name as the name
* of the server. However, there are some servers like google where host would be
@ -68,6 +73,8 @@ public abstract class ConnectionConfiguration {
*/
protected final DomainBareJid xmppServiceDomain;
protected final DnsName xmppServiceDomainDnsName;
protected final InetAddress hostAddress;
protected final DnsName host;
protected final int port;
@ -140,6 +147,19 @@ public abstract class ConnectionConfiguration {
if (xmppServiceDomain == null) {
throw new IllegalArgumentException("Must define the XMPP domain");
}
DnsName xmppServiceDomainDnsName;
try {
xmppServiceDomainDnsName = DnsName.from(xmppServiceDomain);
} catch (InvalidDnsNameException e) {
LOGGER.log(Level.INFO,
"Could not transform XMPP service domain '" + xmppServiceDomain
+ "' to a DNS name. TLS X.509 certificate validiation may not be possible.",
e);
xmppServiceDomainDnsName = null;
}
this.xmppServiceDomainDnsName = xmppServiceDomainDnsName;
hostAddress = builder.hostAddress;
host = builder.host;
port = builder.port;
@ -201,6 +221,17 @@ public abstract class ConnectionConfiguration {
return xmppServiceDomain;
}
/**
* Returns the XMPP service domain as DNS name if possible. Note that since not every XMPP address domainpart is a
* valid DNS name, this method may return <code>null</code>.
*
* @return the XMPP service domain as DNS name or <code>null</code>.
* @since 4.3.4
*/
public DnsName getXmppServiceDomainAsDnsNameIfPossible() {
return xmppServiceDomainDnsName;
}
/**
* Returns the TLS security mode used when making the connection. By default,
* the mode is {@link SecurityMode#ifpossible}.