aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/measite/minidns/Client.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main/java/de/measite/minidns/Client.java86
1 files changed, 14 insertions, 72 deletions
diff --git a/src/main/java/de/measite/minidns/Client.java b/src/main/java/de/measite/minidns/Client.java
index 3f152346..462d52ff 100644
--- a/src/main/java/de/measite/minidns/Client.java
+++ b/src/main/java/de/measite/minidns/Client.java
@@ -13,8 +13,6 @@ import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
-import java.util.LinkedHashMap;
-import java.util.Map.Entry;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -48,23 +46,26 @@ public class Client {
/**
* The internal DNS cache.
*/
- protected LinkedHashMap<Question, DNSMessage> cache;
+ protected DNSCache cache;
/**
- * Maximum acceptable ttl.
+ * Create a new DNS client with the given DNS cache.
+ * @param cache The backend DNS cache.
*/
- protected long maxTTL = 60 * 60 * 1000;
-
- /**
- * Create a new DNS client.
- */
- public Client() {
+ public Client(DNSCache cache) {
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e1) {
random = new SecureRandom();
}
- setCacheSize(10);
+ this.cache = cache;
+ }
+
+ /**
+ * Create a new DNS client.
+ */
+ public Client() {
+ this(null);
}
/**
@@ -132,19 +133,8 @@ public class Client {
*/
public DNSMessage query(Question q, String host, int port) throws IOException {
DNSMessage dnsMessage = (cache == null) ? null : cache.get(q);
- if (dnsMessage != null && dnsMessage.getReceiveTimestamp() > 0l) {
- // check the ttl
- long ttl = maxTTL;
- for (Record r : dnsMessage.getAnswers()) {
- ttl = Math.min(ttl, r.ttl);
- }
- for (Record r : dnsMessage.getAdditionalResourceRecords()) {
- ttl = Math.min(ttl, r.ttl);
- }
- if (dnsMessage.getReceiveTimestamp() + ttl <
- System.currentTimeMillis()) {
- return dnsMessage;
- }
+ if (dnsMessage != null) {
+ return dnsMessage;
}
DNSMessage message = new DNSMessage();
message.setQuestions(new Question[]{q});
@@ -326,52 +316,4 @@ public class Client {
return null;
}
- /**
- * Configure the cache size (default 10).
- * @param maximumSize The new cache size or 0 to disable.
- */
- @SuppressWarnings("serial")
- public void setCacheSize(final int maximumSize) {
- if (maximumSize == 0) {
- this.cache = null;
- } else {
- LinkedHashMap<Question,DNSMessage> old = cache;
- cache = new LinkedHashMap<Question,DNSMessage>() {
- @Override
- protected boolean removeEldestEntry(
- Entry<Question, DNSMessage> eldest) {
- return size() > maximumSize;
- }
- };
- if (old != null) {
- cache.putAll(old);
- }
- }
- }
-
- /**
- * Flush the DNS cache.
- */
- public void flushCache() {
- if (cache != null) {
- cache.clear();
- }
- }
-
- /**
- * Get the current maximum record ttl.
- * @return The maximum record ttl.
- */
- public long getMaxTTL() {
- return maxTTL;
- }
-
- /**
- * Set the maximum record ttl.
- * @param maxTTL The new maximum ttl.
- */
- public void setMaxTTL(long maxTTL) {
- this.maxTTL = maxTTL;
- }
-
}