Permalink
Browse files

Introduce DnsQueryResult

  • Loading branch information...
Flowdalic committed Jul 2, 2018
1 parent bd44b54 commit 56bba6b200cc00a4f20b1656ad7b300607f28f58
Showing with 643 additions and 382 deletions.
  1. +1 −1 README.md
  2. +13 −7 minidns-async/src/main/java/org/minidns/source/async/AsyncDnsRequest.java
  3. +4 −3 minidns-async/src/main/java/org/minidns/source/async/AsyncNetworkDataSource.java
  4. +29 −27 minidns-client/src/main/java/org/minidns/AbstractDnsClient.java
  5. +9 −7 minidns-client/src/main/java/org/minidns/DnsCache.java
  6. +18 −16 minidns-client/src/main/java/org/minidns/DnsClient.java
  7. +16 −7 minidns-client/src/main/java/org/minidns/cache/ExtendedLruCache.java
  8. +15 −11 minidns-client/src/main/java/org/minidns/cache/LruCache.java
  9. +28 −0 minidns-client/src/main/java/org/minidns/dnsqueryresult/CachedDnsQueryResult.java
  10. +21 −0 minidns-client/src/main/java/org/minidns/dnsqueryresult/DirectCachedDnsQueryResult.java
  11. +47 −0 minidns-client/src/main/java/org/minidns/dnsqueryresult/DnsQueryResult.java
  12. +29 −0 minidns-client/src/main/java/org/minidns/dnsqueryresult/StandardDnsQueryResult.java
  13. +21 −0 minidns-client/src/main/java/org/minidns/dnsqueryresult/SynthesizedCachedDnsQueryResult.java
  14. +21 −0 minidns-client/src/main/java/org/minidns/dnsqueryresult/TestWorldDnsQueryResult.java
  15. +6 −5 minidns-client/src/main/java/org/minidns/source/AbstractDnsDataSource.java
  16. +4 −3 minidns-client/src/main/java/org/minidns/source/DnsDataSource.java
  17. +10 −5 minidns-client/src/main/java/org/minidns/source/NetworkDataSource.java
  18. +4 −3 minidns-client/src/main/java/org/minidns/source/NetworkDataSourceWithAccounting.java
  19. +10 −7 minidns-client/src/test/java/org/minidns/DnsClientTest.java
  20. +4 −2 minidns-client/src/test/java/org/minidns/DnsWorld.java
  21. +26 −16 minidns-client/src/test/java/org/minidns/LruCacheTest.java
  22. +7 −3 minidns-client/src/test/java/org/minidns/source/NetworkDataSourceTest.java
  23. +3 −3 minidns-dane-java7/src/main/java/org/minidns/dane/java7/DaneExtendedTrustManager.java
  24. +13 −13 minidns-dnssec/src/main/java/org/minidns/dane/DaneVerifier.java
  25. +58 −52 minidns-dnssec/src/main/java/org/minidns/dnssec/DnssecClient.java
  26. +0 −38 minidns-dnssec/src/main/java/org/minidns/dnssec/DnssecMessage.java
  27. +53 −0 minidns-dnssec/src/main/java/org/minidns/dnssec/DnssecQueryResult.java
  28. +5 −5 minidns-dnssec/src/main/java/org/minidns/dnssec/DnssecResultNotAuthenticException.java
  29. +11 −11 ...dns-dnssec/src/main/java/org/minidns/dnssec/{UnverifiedReason.java → DnssecUnverifiedReason.java}
  30. +8 −8 minidns-dnssec/src/main/java/org/minidns/dnssec/Verifier.java
  31. +45 −44 minidns-dnssec/src/test/java/org/minidns/dnssec/DnssecClientTest.java
  32. +8 −8 minidns-hla/src/main/java/org/minidns/hla/DnssecResolverApi.java
  33. +3 −3 minidns-hla/src/main/java/org/minidns/hla/ResolverApi.java
  34. +15 −5 minidns-hla/src/main/java/org/minidns/hla/ResolverResult.java
  35. +1 −1 minidns-hla/src/main/java/org/minidns/hla/SrvResolverResult.java
  36. +7 −7 minidns-integration-test/src/main/java/org/minidns/integrationtest/AsyncApiTest.java
  37. +9 −9 minidns-integration-test/src/main/java/org/minidns/integrationtest/CoreTest.java
  38. +7 −7 minidns-integration-test/src/main/java/org/minidns/integrationtest/DnssecTest.java
  39. +5 −5 minidns-integration-test/src/main/java/org/minidns/integrationtest/IterativeDnssecTest.java
  40. +4 −4 minidns-integration-test/src/main/java/org/minidns/integrationtest/NsidTest.java
  41. +21 −15 minidns-iterative-resolver/src/main/java/org/minidns/iterative/IterativeDnsClient.java
  42. +10 −7 minidns-iterative-resolver/src/main/java/org/minidns/iterative/ReliableDnsClient.java
  43. +5 −5 minidns-iterative-resolver/src/test/java/org/minidns/iterative/IterativeDnsClientTest.java
  44. +5 −5 minidns-repl/src/main/java/org/minidns/minidnsrepl/DnssecStats.java
  45. +4 −4 minidns-repl/src/main/java/org/minidns/minidnsrepl/MiniDnsStats.java
@@ -84,7 +84,7 @@ MiniDNS comes with a REPL which can be used to perform DNS lookups and to test t
minidns $ ./repl
...
scala> c query ("measite.de", TYPE.A)
res4: org.minidns.DnsMessage = DnsMessage@54653(QUERY NO_ERROR qr rd ra) { \
res0: dnsqueryresult.DnsQueryResult = DnsMessage@54653(QUERY NO_ERROR qr rd ra) { \
[Q: measite.de. IN A] \
[A: measite.de. 3599 IN A 85.10.226.249] \
[X: EDNS: version: 0, flags:; udp: 512]
@@ -13,7 +13,6 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.DatagramChannel;
@@ -30,6 +29,9 @@
import org.minidns.MiniDnsFuture;
import org.minidns.MiniDnsFuture.InternalMiniDnsFuture;
import org.minidns.dnsmessage.DnsMessage;
import org.minidns.dnsqueryresult.DnsQueryResult;
import org.minidns.dnsqueryresult.DnsQueryResult.QueryMethod;
import org.minidns.dnsqueryresult.StandardDnsQueryResult;
import org.minidns.source.DnsDataSource.OnResponseCallback;
import org.minidns.source.AbstractDnsDataSource.QueryMode;
import org.minidns.util.MultipleIoException;
@@ -38,7 +40,7 @@

private static final Logger LOGGER = Logger.getLogger(AsyncDnsRequest.class.getName());

private final InternalMiniDnsFuture<DnsMessage, IOException> future = new InternalMiniDnsFuture<DnsMessage, IOException>() {
private final InternalMiniDnsFuture<DnsQueryResult, IOException> future = new InternalMiniDnsFuture<DnsQueryResult, IOException>() {
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
boolean res = super.cancel(mayInterruptIfRunning);
@@ -51,7 +53,7 @@ public boolean cancel(boolean mayInterruptIfRunning) {

private final int udpPayloadSize;

private final SocketAddress socketAddress;
private final InetSocketAddress socketAddress;

private final AsyncNetworkDataSource asyncNds;

@@ -132,15 +134,15 @@ private void addException(IOException e) {
exceptions.add(e);
}

private final void gotResult(DnsMessage result) {
private final void gotResult(DnsQueryResult result) {
if (onResponseCallback != null) {
onResponseCallback.onResponse(request, result);
}
asyncNds.finished(this);
future.setResult(result);
}

MiniDnsFuture<DnsMessage, IOException> getFuture() {
MiniDnsFuture<DnsQueryResult, IOException> getFuture() {
return future;
}

@@ -300,7 +302,9 @@ public void handleChannelSelectedAndNotCancelled(SelectableChannel channel, Sele
return;
}

gotResult(response);
DnsQueryResult result = new StandardDnsQueryResult(socketAddress.getAddress(), socketAddress.getPort(),
QueryMethod.asyncUdp, request, response);
gotResult(result);
}
}

@@ -536,7 +540,9 @@ public void handleChannelSelectedAndNotCancelled(SelectableChannel channel, Sele
return;
}

gotResult(response);
DnsQueryResult result = new StandardDnsQueryResult(socketAddress.getAddress(), socketAddress.getPort(),
QueryMethod.asyncTcp, request, response);
gotResult(result);
}

}
@@ -34,6 +34,7 @@

import org.minidns.MiniDnsFuture;
import org.minidns.dnsmessage.DnsMessage;
import org.minidns.dnsqueryresult.DnsQueryResult;
import org.minidns.source.AbstractDnsDataSource;

public class AsyncNetworkDataSource extends AbstractDnsDataSource {
@@ -81,7 +82,7 @@ public int compare(AsyncDnsRequest o1, AsyncDnsRequest o2) {
}

@Override
public MiniDnsFuture<DnsMessage, IOException> queryAsync(DnsMessage message, InetAddress address, int port, OnResponseCallback onResponseCallback) {
public MiniDnsFuture<DnsQueryResult, IOException> queryAsync(DnsMessage message, InetAddress address, int port, OnResponseCallback onResponseCallback) {
AsyncDnsRequest asyncDnsRequest = new AsyncDnsRequest(message, address, port, udpPayloadSize, this, onResponseCallback);
INCOMING_REQUESTS.add(asyncDnsRequest);
synchronized (DEADLINE_QUEUE) {
@@ -92,8 +93,8 @@ public int compare(AsyncDnsRequest o1, AsyncDnsRequest o2) {
}

@Override
public DnsMessage query(DnsMessage message, InetAddress address, int port) throws IOException {
MiniDnsFuture<DnsMessage, IOException> future = queryAsync(message, address, port, null);
public DnsQueryResult query(DnsMessage message, InetAddress address, int port) throws IOException {
MiniDnsFuture<DnsQueryResult, IOException> future = queryAsync(message, address, port, null);
try {
return future.get();
} catch (InterruptedException e) {
@@ -15,6 +15,7 @@
import org.minidns.dnsmessage.DnsMessage;
import org.minidns.dnsmessage.Question;
import org.minidns.dnsname.DnsName;
import org.minidns.dnsqueryresult.DnsQueryResult;
import org.minidns.record.A;
import org.minidns.record.AAAA;
import org.minidns.record.Data;
@@ -52,7 +53,7 @@
*/
private final DnsDataSource.OnResponseCallback onResponseCallback = new DnsDataSource.OnResponseCallback() {
@Override
public void onResponse(DnsMessage requestMessage, DnsMessage responseMessage) {
public void onResponse(DnsMessage requestMessage, DnsQueryResult responseMessage) {
final Question q = requestMessage.getQuestion();
if (cache != null && isResponseCacheable(q, responseMessage)) {
cache.put(requestMessage.asNormalizedVersion(), responseMessage);
@@ -149,7 +150,7 @@ protected AbstractDnsClient() {
* @return The response (or null on timeout/error).
* @throws IOException if an IO error occurs.
*/
public final DnsMessage query(String name, TYPE type, CLASS clazz) throws IOException {
public final DnsQueryResult query(String name, TYPE type, CLASS clazz) throws IOException {
Question q = new Question(name, type, clazz);
return query(q);
}
@@ -163,7 +164,7 @@ public final DnsMessage query(String name, TYPE type, CLASS clazz) throws IOExce
* @return The response (or null on timeout/error).
* @throws IOException if an IO error occurs.
*/
public final DnsMessage query(DnsName name, TYPE type) throws IOException {
public final DnsQueryResult query(DnsName name, TYPE type) throws IOException {
Question q = new Question(name, type, CLASS.IN);
return query(q);
}
@@ -177,12 +178,12 @@ public final DnsMessage query(DnsName name, TYPE type) throws IOException {
* @return The response (or null on timeout/error).
* @throws IOException if an IO error occurs.
*/
public final DnsMessage query(CharSequence name, TYPE type) throws IOException {
public final DnsQueryResult query(CharSequence name, TYPE type) throws IOException {
Question q = new Question(name, type, CLASS.IN);
return query(q);
}

public DnsMessage query(Question q) throws IOException {
public DnsQueryResult query(Question q) throws IOException {
DnsMessage.Builder query = buildMessage(q);
return query(query);
}
@@ -194,14 +195,14 @@ public DnsMessage query(Question q) throws IOException {
* @return The response (or null).
* @throws IOException if an IO error occurs.
*/
protected abstract DnsMessage query(DnsMessage.Builder query) throws IOException;
protected abstract DnsQueryResult query(DnsMessage.Builder query) throws IOException;

public final MiniDnsFuture<DnsMessage, IOException> queryAsync(CharSequence name, TYPE type) {
public final MiniDnsFuture<DnsQueryResult, IOException> queryAsync(CharSequence name, TYPE type) {
Question q = new Question(name, type, CLASS.IN);
return queryAsync(q);
}

public final MiniDnsFuture<DnsMessage, IOException> queryAsync(Question q) {
public final MiniDnsFuture<DnsQueryResult, IOException> queryAsync(Question q) {
DnsMessage.Builder query = buildMessage(q);
return queryAsync(query);
}
@@ -215,9 +216,9 @@ public DnsMessage query(Question q) throws IOException {
* @param query the query.
* @return a future for this query.
*/
protected MiniDnsFuture<DnsMessage, IOException> queryAsync(DnsMessage.Builder query) {
InternalMiniDnsFuture<DnsMessage, IOException> future = new InternalMiniDnsFuture<>();
DnsMessage result;
protected MiniDnsFuture<DnsQueryResult, IOException> queryAsync(DnsMessage.Builder query) {
InternalMiniDnsFuture<DnsQueryResult, IOException> future = new InternalMiniDnsFuture<>();
DnsQueryResult result;
try {
result = query(query);
} catch (IOException e) {
@@ -228,14 +229,14 @@ public DnsMessage query(Question q) throws IOException {
return future;
}

public final DnsMessage query(Question q, InetAddress server, int port) throws IOException {
public final DnsQueryResult query(Question q, InetAddress server, int port) throws IOException {
DnsMessage query = getQueryFor(q);
return query(query, server, port);
}

public final DnsMessage query(DnsMessage requestMessage, InetAddress address, int port) throws IOException {
public final DnsQueryResult 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);
DnsQueryResult responseMessage = (cache == null) ? null : cache.get(requestMessage);
if (responseMessage != null) {
return responseMessage;
}
@@ -265,9 +266,9 @@ public final DnsMessage query(DnsMessage requestMessage, InetAddress address, in
return responseMessage;
}

public final MiniDnsFuture<DnsMessage, IOException> queryAsync(DnsMessage requestMessage, InetAddress address, int port) {
public final MiniDnsFuture<DnsQueryResult, 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);
DnsQueryResult responseMessage = (cache == null) ? null : cache.get(requestMessage);
if (responseMessage != null) {
return MiniDnsFuture.from(responseMessage);
}
@@ -284,10 +285,11 @@ public final DnsMessage query(DnsMessage requestMessage, InetAddress address, in
* Whether a response from the DNS system should be cached or not.
*
* @param q The question the response message should answer.
* @param dnsMessage The response message received using the DNS client.
* @param result The DNS query result.
* @return True, if the response should be cached, false otherwise.
*/
protected boolean isResponseCacheable(Question q, DnsMessage dnsMessage) {
protected boolean isResponseCacheable(Question q, DnsQueryResult result) {
DnsMessage dnsMessage = result.response;
for (Record<? extends Data> record : dnsMessage.answerSection) {
if (record.isAnswer(q)) {
return true;
@@ -323,7 +325,7 @@ protected boolean isResponseCacheable(Question q, DnsMessage dnsMessage) {
* @return The response (or null on timeout / failure).
* @throws IOException On IO Errors.
*/
public DnsMessage query(String name, TYPE type, CLASS clazz, InetAddress address, int port)
public DnsQueryResult query(String name, TYPE type, CLASS clazz, InetAddress address, int port)
throws IOException {
Question q = new Question(name, type, clazz);
return query(q, address, port);
@@ -339,7 +341,7 @@ public DnsMessage query(String name, TYPE type, CLASS clazz, InetAddress address
* @return The response (or null on timeout / failure).
* @throws IOException On IO Errors.
*/
public DnsMessage query(String name, TYPE type, CLASS clazz, InetAddress address)
public DnsQueryResult query(String name, TYPE type, CLASS clazz, InetAddress address)
throws IOException {
Question q = new Question(name, type, clazz);
return query(q, address);
@@ -354,13 +356,13 @@ public DnsMessage query(String name, TYPE type, CLASS clazz, InetAddress address
* @return The response (or null on timeout / failure).
* @throws IOException On IO Errors.
*/
public DnsMessage query(String name, TYPE type, InetAddress address)
public DnsQueryResult query(String name, TYPE type, InetAddress address)
throws IOException {
Question q = new Question(name, type, CLASS.IN);
return query(q, address);
}

public final DnsMessage query(DnsMessage query, InetAddress host) throws IOException {
public final DnsQueryResult query(DnsMessage query, InetAddress host) throws IOException {
return query(query, host, 53);
}

@@ -369,14 +371,14 @@ public final DnsMessage query(DnsMessage query, InetAddress host) throws IOExcep
*
* @param q The question section of the DNS query.
* @param address The dns server address.
* @return The response (or null on timeout/error).
* @return The a DNS query result.
* @throws IOException On IOErrors.
*/
public DnsMessage query(Question q, InetAddress address) throws IOException {
public DnsQueryResult query(Question q, InetAddress address) throws IOException {
return query(q, address, 53);
}

public final MiniDnsFuture<DnsMessage, IOException> queryAsync(DnsMessage query, InetAddress dnsServer) {
public final MiniDnsFuture<DnsQueryResult, IOException> queryAsync(DnsMessage query, InetAddress dnsServer) {
return queryAsync(query, dnsServer, 53);
}

@@ -423,12 +425,12 @@ protected DnsMessage getQueryFor(Question q) {
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);
DnsQueryResult cachedResult = cache.get(queryDnsNameNs);

if (cachedResult == null)
return Collections.emptySet();

return cachedResult.getAnswersFor(dnsNameNs);
return cachedResult.response.getAnswersFor(dnsNameNs);
}

public Set<NS> getCachedNameserverRecordsFor(DnsName dnsName) {
@@ -12,6 +12,8 @@

import org.minidns.dnsmessage.DnsMessage;
import org.minidns.dnsname.DnsName;
import org.minidns.dnsqueryresult.CachedDnsQueryResult;
import org.minidns.dnsqueryresult.DnsQueryResult;

/**
* Cache for DNS Entries. Implementations must be thread safe.
@@ -24,25 +26,25 @@
* Add an an dns answer/response for a given dns question. Implementations
* should honor the ttl / receive timestamp.
* @param query The query message containing a question.
* @param message The dns message.
* @param result The DNS query result.
*/
public final void put(DnsMessage query, DnsMessage message) {
putNormalized(query.asNormalizedVersion(), message);
public final void put(DnsMessage query, DnsQueryResult result) {
putNormalized(query.asNormalizedVersion(), result);
}

protected abstract void putNormalized(DnsMessage normalizedQuery, DnsMessage reply);
protected abstract void putNormalized(DnsMessage normalizedQuery, DnsQueryResult result);

public abstract void offer(DnsMessage query, DnsMessage reply, DnsName authoritativeZone);
public abstract void offer(DnsMessage query, DnsQueryResult result, DnsName authoritativeZone);

/**
* Request a cached dns response.
* @param query The query message containing a question.
* @return The dns message.
*/
public final DnsMessage get(DnsMessage query) {
public final CachedDnsQueryResult get(DnsMessage query) {
return getNormalized(query.asNormalizedVersion());
}

protected abstract DnsMessage getNormalized(DnsMessage normalizedQuery);
protected abstract CachedDnsQueryResult getNormalized(DnsMessage normalizedQuery);

}
Oops, something went wrong.

0 comments on commit 56bba6b

Please sign in to comment.