use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.
the class MiniDnsResolver method lookupHostAddress0.
@Override
protected List<InetAddress> lookupHostAddress0(final DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
final ResolverApi resolver = getResolver(dnssecMode);
final ResolverResult<A> aResult;
final ResolverResult<AAAA> aaaaResult;
try {
aResult = resolver.resolve(name, A.class);
aaaaResult = resolver.resolve(name, AAAA.class);
} catch (IOException e) {
RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
lookupFailures.add(failure);
return null;
}
if (!aResult.wasSuccessful() && !aaaaResult.wasSuccessful()) {
// Both results where not successful.
RemoteConnectionEndpointLookupFailure failureA = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, getExceptionFrom(aResult));
lookupFailures.add(failureA);
RemoteConnectionEndpointLookupFailure failureAaaa = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, getExceptionFrom(aaaaResult));
lookupFailures.add(failureAaaa);
return null;
}
if (shouldAbortIfNotAuthentic(name, dnssecMode, aResult, lookupFailures) || shouldAbortIfNotAuthentic(name, dnssecMode, aaaaResult, lookupFailures)) {
return null;
}
// TODO: Use ResolverResult.getAnswersOrEmptySet() once we updated MiniDNS.
Set<A> aResults;
if (aResult.wasSuccessful()) {
aResults = aResult.getAnswers();
} else {
aResults = Collections.emptySet();
}
// TODO: Use ResolverResult.getAnswersOrEmptySet() once we updated MiniDNS.
Set<AAAA> aaaaResults;
if (aaaaResult.wasSuccessful()) {
aaaaResults = aaaaResult.getAnswers();
} else {
aaaaResults = Collections.emptySet();
}
List<InetAddress> inetAddresses = new ArrayList<>(aResults.size() + aaaaResults.size());
for (A a : aResults) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByAddress(a.getIp());
} catch (UnknownHostException e) {
continue;
}
inetAddresses.add(inetAddress);
}
for (AAAA aaaa : aaaaResults) {
InetAddress inetAddress;
try {
inetAddress = InetAddress.getByAddress(name.ace, aaaa.getIp());
} catch (UnknownHostException e) {
continue;
}
inetAddresses.add(inetAddress);
}
return inetAddresses;
}
use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.
the class MiniDnsResolver method shouldAbortIfNotAuthentic.
private static boolean shouldAbortIfNotAuthentic(DnsName name, DnssecMode dnssecMode, ResolverResult<?> result, List<RemoteConnectionEndpointLookupFailure> lookupFailures) {
switch(dnssecMode) {
case needsDnssec:
case needsDnssecAndDane:
// Check if the result is authentic data, i.e. there a no reasons the result is unverified.
DnssecResultNotAuthenticException exception = result.getDnssecResultNotAuthenticException();
if (exception != null) {
RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, exception);
lookupFailures.add(failure);
return true;
}
break;
case disabled:
break;
default:
throw new IllegalStateException("Unknown DnssecMode: " + dnssecMode);
}
return false;
}
use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.
the class RemoteXmppTcpConnectionEndpoints method lookup.
/**
* Lookups remote connection endpoints on the server for XMPP connections over TCP taking A, AAAA and SRV resource
* records into account. If no host address was configured and all lookups failed, for example with NX_DOMAIN, then
* result will be populated with the empty list.
*
* @param config the connection configuration to lookup the endpoints for.
* @return a lookup result.
*/
public static Result<Rfc6120TcpRemoteConnectionEndpoint> lookup(ConnectionConfiguration config) {
List<Rfc6120TcpRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints;
List<RemoteConnectionEndpointLookupFailure> lookupFailures;
final InetAddress hostAddress = config.getHostAddress();
final DnsName host = config.getHost();
if (hostAddress != null) {
lookupFailures = Collections.emptyList();
IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(hostAddress.toString(), config.getPort(), hostAddress);
discoveredRemoteConnectionEndpoints = Collections.singletonList(connectionEndpoint);
} else if (host != null) {
lookupFailures = new ArrayList<>(1);
List<InetAddress> hostAddresses = DNSUtil.getDNSResolver().lookupHostAddress(host, lookupFailures, config.getDnssecMode());
if (hostAddresses != null) {
discoveredRemoteConnectionEndpoints = new ArrayList<>(hostAddresses.size());
UInt16 port = config.getPort();
for (InetAddress inetAddress : hostAddresses) {
IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(host, port, inetAddress);
discoveredRemoteConnectionEndpoints.add(connectionEndpoint);
}
} else {
discoveredRemoteConnectionEndpoints = Collections.emptyList();
}
} else {
lookupFailures = new ArrayList<>();
// N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName
DnsName dnsName = config.getXmppServiceDomainAsDnsNameIfPossible();
if (dnsName == null) {
// name is also a valid DNS name, or that a host is explicitly configured.
throw new IllegalStateException();
}
discoveredRemoteConnectionEndpoints = resolveXmppServiceDomain(dnsName, lookupFailures, config.getDnssecMode());
}
// Either the populated host addresses are not empty *or* there must be at least one failed address.
assert !discoveredRemoteConnectionEndpoints.isEmpty() || !lookupFailures.isEmpty();
return new Result<>(discoveredRemoteConnectionEndpoints, lookupFailures);
}
use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.
the class MiniDnsResolver method lookupSrvRecords0.
@Override
protected Set<SRV> lookupSrvRecords0(final DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
final ResolverApi resolver = getResolver(dnssecMode);
SrvResolverResult result;
try {
result = resolver.resolveSrv(name);
} catch (IOException e) {
RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
lookupFailures.add(failure);
return null;
}
ResolutionUnsuccessfulException resolutionUnsuccessfulException = result.getResolutionUnsuccessfulException();
if (resolutionUnsuccessfulException != null) {
RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, resolutionUnsuccessfulException);
lookupFailures.add(failure);
return null;
}
if (shouldAbortIfNotAuthentic(name, dnssecMode, result, lookupFailures)) {
return null;
}
return result.getAnswers();
}
use of org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure in project Smack by igniterealtime.
the class JavaxResolver method lookupSrvRecords0.
@Override
protected List<SRV> lookupSrvRecords0(DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode) {
Attribute srvAttribute;
try {
Attributes dnsLookup = dirContext.getAttributes(name.ace, new String[] { "SRV" });
srvAttribute = dnsLookup.get("SRV");
if (srvAttribute == null)
return null;
} catch (NameNotFoundException e) {
LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
return null;
} catch (NamingException e) {
RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
lookupFailures.add(failure);
return null;
}
List<SRV> res = new ArrayList<>();
try {
@SuppressWarnings("unchecked") NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
while (srvRecords.hasMore()) {
String srvRecordString = srvRecords.next();
String[] srvRecordEntries = srvRecordString.split(" ");
int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
String srvTarget = srvRecordEntries[srvRecordEntries.length - 1];
// having a trailing dot, so this can possibly be removed in future Smack versions.
if (srvTarget.length() > 0 && srvTarget.charAt(srvTarget.length() - 1) == '.') {
srvTarget = srvTarget.substring(0, srvTarget.length() - 1);
}
SRV srvRecord = new SRV(priority, weight, port, srvTarget);
res.add(srvRecord);
}
} catch (NamingException e) {
RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(name, e);
lookupFailures.add(failure);
}
return res;
}
Aggregations