use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class NetworkDataSourceWithAccounting method queryTcp.
@Override
protected DNSMessage queryTcp(DNSMessage message, InetAddress address, int port) throws IOException {
DNSMessage response;
try {
response = super.queryTcp(message, address, port);
} catch (IOException e) {
failedTcpQueries.incrementAndGet();
throw e;
}
successfulTcpQueries.incrementAndGet();
tcpResponseSize.addAndGet(response.toArray().length);
return response;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class DNSWorld method applyZones.
public static DNSWorld applyZones(AbstractDNSClient client, Zone... zones) {
DNSWorld world = new DNSWorld();
client.setDataSource(world);
for (Zone zone : zones) {
for (RRSet rrSet : zone.getRRSets()) {
DNSMessage.Builder req = client.buildMessage(new Question(rrSet.name, rrSet.type, rrSet.clazz, false));
DNSMessage.Builder resp = DNSMessage.builder();
resp.setAnswers(rrSet.records);
resp.setAuthoritativeAnswer(true);
attachGlues(resp, rrSet.records, zone.records);
attachSignatures(resp, zone.records);
DNSMessage request = req.build();
DNSMessage response = resp.build();
if (zone.isRootZone()) {
world.addPreparedResponse(new RootAnswerResponse(request, response));
} else {
world.addPreparedResponse(new AddressedAnswerResponse(zone.address, request, response));
}
if (rrSet.type == TYPE.NS) {
DNSMessage.Builder hintsResp = DNSMessage.builder();
hintsResp.setNameserverRecords(rrSet.records);
hintsResp.setAdditionalResourceRecords(response.additionalSection);
DNSMessage hintsResponse = hintsResp.build();
if (zone.isRootZone()) {
world.addPreparedResponse(new RootHintsResponse(rrSet.name, hintsResponse));
} else {
world.addPreparedResponse(new AddressedHintsResponse(zone.address, rrSet.name, hintsResponse));
}
}
}
}
return world;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class LRUCacheTest method testOutdatedCacheEntry.
@Test
public void testOutdatedCacheEntry() {
DNSMessage message = createSampleMessage(1);
Question q = new Question("", Record.TYPE.A);
DNSMessage question = q.asQueryMessage();
lruCache.put(question, message);
assertNull(lruCache.get(question));
assertNull(lruCache.get(question));
assertEquals(1, lruCache.getExpireCount());
assertEquals(2, lruCache.getMissCount());
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class DNSClient method query.
@Override
public DNSMessage query(DNSMessage.Builder queryBuilder) throws IOException {
DNSMessage q = newQuestion(queryBuilder).build();
// While this query method does in fact re-use query(Question, String)
// we still do a cache lookup here in order to avoid unnecessary
// findDNS()calls, which are expensive on Android. Note that we do not
// put the results back into the Cache, as this is already done by
// query(Question, String).
DNSMessage responseMessage = (cache == null) ? null : cache.get(q);
if (responseMessage != null) {
return responseMessage;
}
List<InetAddress> dnsServerAddresses = getServerAddresses();
List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size());
for (InetAddress dns : dnsServerAddresses) {
if (nonRaServers.contains(dns)) {
LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\"");
continue;
}
try {
responseMessage = query(q, dns);
if (responseMessage == null) {
continue;
}
if (!responseMessage.recursionAvailable) {
boolean newRaServer = nonRaServers.add(dns);
if (newRaServer) {
LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution");
}
continue;
}
if (disableResultFilter) {
return responseMessage;
}
switch(responseMessage.responseCode) {
case NO_ERROR:
case NX_DOMAIN:
break;
default:
String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.';
if (!LOGGER.isLoggable(Level.FINE)) {
// Only append the responseMessage is log level is not fine. If it is fine or higher, the
// response has already been logged.
warning += "\n" + responseMessage;
}
LOGGER.warning(warning);
// TODO Create new IOException and add to ioExceptions.
continue;
}
return responseMessage;
} catch (IOException ioe) {
ioExceptions.add(ioe);
}
}
MultipleIoException.throwIfRequired(ioExceptions);
// TODO assert that we never return null here.
return null;
}
use of org.minidns.dnsmessage.DNSMessage in project minidns by MiniDNS.
the class ExtendedLRUCache method putExtraCaches.
private final void putExtraCaches(DNSMessage reply, Map<DNSMessage, List<Record<? extends Data>>> extraCaches) {
for (Entry<DNSMessage, List<Record<? extends Data>>> entry : extraCaches.entrySet()) {
DNSMessage question = entry.getKey();
DNSMessage answer = reply.asBuilder().setQuestion(question.getQuestion()).setAuthoritativeAnswer(true).addAnswers(entry.getValue()).build();
super.putNormalized(question, answer);
}
}
Aggregations