use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class AbstractDNSClient method query.
public final DNSMessage query(DNSMessage requestMessage, InetAddress address, int port) throws IOException {
// See if we have the answer to this question already cached
DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
if (responseMessage != null) {
return responseMessage;
}
final Question q = requestMessage.getQuestion();
final Level TRACE_LOG_LEVEL = Level.FINE;
LOGGER.log(TRACE_LOG_LEVEL, "Asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
try {
responseMessage = dataSource.query(requestMessage, address, port);
} catch (IOException e) {
LOGGER.log(TRACE_LOG_LEVEL, "IOException {0} on {1} while resolving {2}: {3}", new Object[] { address, port, q, e });
throw e;
}
if (responseMessage != null) {
LOGGER.log(TRACE_LOG_LEVEL, "Response from {0} on {1} for {2}:\n{3}", new Object[] { address, port, q, responseMessage });
} else {
// TODO When should this ever happen?
LOGGER.log(Level.SEVERE, "NULL response from " + address + " on " + port + " for " + q);
}
if (responseMessage == null)
return null;
onResponseCallback.onResponse(requestMessage, responseMessage);
return responseMessage;
}
use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class AbstractDNSClient method queryAsync.
public final MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage requestMessage, InetAddress address, int port) {
// See if we have the answer to this question already cached
DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
if (responseMessage != null) {
return MiniDnsFuture.from(responseMessage);
}
final Question q = requestMessage.getQuestion();
final Level TRACE_LOG_LEVEL = Level.FINE;
LOGGER.log(TRACE_LOG_LEVEL, "Asynchronusly asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
return dataSource.queryAsync(requestMessage, address, port, onResponseCallback);
}
use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class AbstractDNSClient method getCachedRecordsFor.
private <D extends Data> Set<D> getCachedRecordsFor(DNSName dnsName, TYPE type) {
Question dnsNameNs = new Question(dnsName, type);
DNSMessage queryDnsNameNs = getQueryFor(dnsNameNs);
DNSMessage cachedResult = cache.get(queryDnsNameNs);
if (cachedResult == null)
return Collections.emptySet();
return cachedResult.getAnswersFor(dnsNameNs);
}
use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class LRUCacheTest method testOverfilledCache.
@Test
public void testOverfilledCache() {
Question q = new Question("", Record.TYPE.A);
DNSMessage question = q.asQueryMessage();
lruCache.put(question, createSampleMessage());
assertNotNull(lruCache.get(question));
lruCache.put(new Question("1", Record.TYPE.A).asQueryMessage(), createSampleMessage());
lruCache.put(new Question("2", Record.TYPE.A).asQueryMessage(), createSampleMessage());
lruCache.put(new Question("3", Record.TYPE.A).asQueryMessage(), createSampleMessage());
lruCache.put(new Question("4", Record.TYPE.A).asQueryMessage(), createSampleMessage());
lruCache.put(new Question("5", Record.TYPE.A).asQueryMessage(), createSampleMessage());
assertNull(lruCache.get(question));
assertEquals(0, lruCache.getExpireCount());
assertEquals(1, lruCache.getMissCount());
assertEquals(1, lruCache.getHitCount());
}
use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class IterativeDNSClient method queryRecursive.
private DNSMessage queryRecursive(ResolutionState resolutionState, DNSMessage q, InetAddress address, DNSName authoritativeZone) throws IOException {
resolutionState.recurse(address, q);
DNSMessage resMessage = query(q, address);
if (resMessage == null) {
// TODO throw exception here?
return null;
}
if (resMessage.authoritativeAnswer) {
return resMessage;
}
if (cache != null) {
cache.offer(q, resMessage, authoritativeZone);
}
List<Record<? extends Data>> authorities = resMessage.copyAuthority();
List<IOException> ioExceptions = new LinkedList<>();
// Glued NS first
for (Iterator<Record<? extends Data>> iterator = authorities.iterator(); iterator.hasNext(); ) {
Record<? extends Data> record = iterator.next();
if (record.type != TYPE.NS) {
iterator.remove();
continue;
}
DNSName name = ((NS) record.payloadData).target;
IpResultSet gluedNs = searchAdditional(resMessage, name);
for (Iterator<InetAddress> addressIterator = gluedNs.addresses.iterator(); addressIterator.hasNext(); ) {
InetAddress target = addressIterator.next();
DNSMessage recursive = null;
try {
recursive = queryRecursive(resolutionState, q, target, record.name);
} catch (IOException e) {
abortIfFatal(e);
LOGGER.log(Level.FINER, "Exception while recursing", e);
resolutionState.decrementSteps();
ioExceptions.add(e);
if (!addressIterator.hasNext()) {
iterator.remove();
}
continue;
}
return recursive;
}
}
// Try non-glued NS
for (Record<? extends Data> record : authorities) {
final Question question = q.getQuestion();
DNSName name = ((NS) record.payloadData).target;
// AAAA RR, then we should not continue here as it would result in an endless loop.
if (question.name.equals(name) && (question.type == TYPE.A || question.type == TYPE.AAAA))
continue;
IpResultSet res = null;
try {
res = resolveIpRecursive(resolutionState, name);
} catch (IOException e) {
resolutionState.decrementSteps();
ioExceptions.add(e);
}
if (res == null) {
continue;
}
for (InetAddress target : res.addresses) {
DNSMessage recursive = null;
try {
recursive = queryRecursive(resolutionState, q, target, record.name);
} catch (IOException e) {
resolutionState.decrementSteps();
ioExceptions.add(e);
continue;
}
return recursive;
}
}
MultipleIoException.throwIfRequired(ioExceptions);
// where we able to find glue records or the IPs of the next nameservers.
return null;
}
Aggregations