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);
}
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;
}
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;
}
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;
}
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);
}
Aggregations