use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.
the class DNSClientTest method testReturnNullSource.
@Test
public void testReturnNullSource() throws IOException {
class NullSource extends DNSDataSource {
boolean queried = false;
@Override
public DNSMessage query(DNSMessage message, InetAddress address, int port) {
queried = true;
return null;
}
}
DNSClient client = new DNSClient(new LRUCache(0));
NullSource source = new NullSource();
client.setDataSource(source);
DNSMessage message = client.query("www.example.com", TYPE.A);
assertNull(message);
assertTrue(source.queried);
}
use of org.minidns.dnsmessage.DnsMessage 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.DnsMessage 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;
}
use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.
the class IterativeDNSClient method resolveIpRecursive.
private IpResultSet resolveIpRecursive(ResolutionState resolutionState, DNSName name) throws IOException {
IpResultSet.Builder res = newIpResultSetBuilder();
if (ipVersionSetting.v4) {
// TODO Try to retrieve A records for name out from cache.
Question question = new Question(name, TYPE.A);
final DNSMessage query = getQueryFor(question);
DNSMessage aMessage = queryRecursive(resolutionState, query);
if (aMessage != null) {
for (Record<? extends Data> answer : aMessage.answerSection) {
if (answer.isAnswer(question)) {
InetAddress inetAddress = inetAddressFromRecord(name.ace, (A) answer.payloadData);
res.ipv4Addresses.add(inetAddress);
} else if (answer.type == TYPE.CNAME && answer.name.equals(name)) {
return resolveIpRecursive(resolutionState, ((RRWithTarget) answer.payloadData).target);
}
}
}
}
if (ipVersionSetting.v6) {
// TODO Try to retrieve AAAA records for name out from cache.
Question question = new Question(name, TYPE.AAAA);
final DNSMessage query = getQueryFor(question);
DNSMessage aMessage = queryRecursive(resolutionState, query);
if (aMessage != null) {
for (Record<? extends Data> answer : aMessage.answerSection) {
if (answer.isAnswer(question)) {
InetAddress inetAddress = inetAddressFromRecord(name.ace, (AAAA) answer.payloadData);
res.ipv6Addresses.add(inetAddress);
} else if (answer.type == TYPE.CNAME && answer.name.equals(name)) {
return resolveIpRecursive(resolutionState, ((RRWithTarget) answer.payloadData).target);
}
}
}
}
return res.build();
}
use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.
the class IterativeDNSClient method query.
/**
* Recursively query the DNS system for one entry.
*
* @param queryBuilder The query DNS message builder.
* @return The response (or null on timeout/error).
* @throws IOException if an IO error occurs.
*/
@Override
protected DNSMessage query(DNSMessage.Builder queryBuilder) throws IOException {
DNSMessage q = queryBuilder.build();
ResolutionState resolutionState = new ResolutionState(this);
DNSMessage message = queryRecursive(resolutionState, q);
return message;
}
Aggregations