Debian Codegraph @

minidns-client/src/main/java/org/minidns/cache/ExtendedLruCache.java

raw
1 /*
2 * Copyright 2015-2020 the original author or authors
3 *
4 * This software is licensed under the Apache License, Version 2.0,
5 * the GNU Lesser General Public License version 2 or later ("LGPL")
6 * and the WTFPL.
7 * You may choose either license to govern your use of this software only
8 * upon the condition that you accept all of the terms of either
9 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
10 */
11 package org.minidns.cache;
12
13 import java.util.HashMap;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import org.minidns.dnsmessage.DnsMessage;
20 import org.minidns.dnsmessage.Question;
21 import org.minidns.dnsname.DnsName;
22 import org.minidns.dnsqueryresult.CachedDnsQueryResult;
23 import org.minidns.dnsqueryresult.DnsQueryResult;
24 import org.minidns.dnsqueryresult.SynthesizedCachedDnsQueryResult;
25 import org.minidns.record.Data;
26 import org.minidns.record.Record;
27
28 /**
29 * A variant of {@link LruCache} also using the data found in the sections for caching.
30 */
31 public class ExtendedLruCache extends LruCache {
32
33 public ExtendedLruCache() {
34 this(DEFAULT_CACHE_SIZE);
35 }
36
37 public ExtendedLruCache(int capacity) {
38 super(capacity);
39 }
40
41 public ExtendedLruCache(int capacity, long maxTTL) {
42 super(capacity, maxTTL);
43 }
44
45 @SuppressWarnings("UnsynchronizedOverridesSynchronized")
46 @Override
47 protected void putNormalized(DnsMessage q, DnsQueryResult result) {
48 super.putNormalized(q, result);
49 DnsMessage message = result.response;
50 Map<DnsMessage, List<Record<? extends Data>>> extraCaches = new HashMap<>(message.additionalSection.size());
51
52 gather(extraCaches, q, message.answerSection, null);
53 gather(extraCaches, q, message.authoritySection, null);
54 gather(extraCaches, q, message.additionalSection, null);
55
56 putExtraCaches(result, extraCaches);
57 }
58
59 @Override
60 public void offer(DnsMessage query, DnsQueryResult result, DnsName authoritativeZone) {
61 DnsMessage reply = result.response;
62 // The reply shouldn't be an authoritative answers when offer() is used. That would be a case for put().
63 assert !reply.authoritativeAnswer;
64
65 Map<DnsMessage, List<Record<? extends Data>>> extraCaches = new HashMap<>(reply.additionalSection.size());
66
67 // N.B. not gathering from reply.answerSection here. Since it is a non authoritativeAnswer it shouldn't contain anything.
68 gather(extraCaches, query, reply.authoritySection, authoritativeZone);
69 gather(extraCaches, query, reply.additionalSection, authoritativeZone);
70
71 putExtraCaches(result, extraCaches);
72 }
73
74 private void gather(Map<DnsMessage, List<Record<?extends Data>>> extraCaches, DnsMessage q, List<Record<? extends Data>> records, DnsName authoritativeZone) {
75 for (Record<? extends Data> extraRecord : records) {
76 if (!shouldGather(extraRecord, q.getQuestion(), authoritativeZone))
77 continue;
78
79 DnsMessage.Builder additionalRecordQuestionBuilder = extraRecord.getQuestionMessage();
80 if (additionalRecordQuestionBuilder == null)
81 continue;
82
83 additionalRecordQuestionBuilder.copyFlagsFrom(q);
84
85 additionalRecordQuestionBuilder.setAdditionalResourceRecords(q.additionalSection);
86
87 DnsMessage additionalRecordQuestion = additionalRecordQuestionBuilder.build();
88 if (additionalRecordQuestion.equals(q)) {
89 // No need to cache the additional question if it is the same as the original question.
90 continue;
91 }
92
93 List<Record<? extends Data>> additionalRecords = extraCaches.get(additionalRecordQuestion);
94 if (additionalRecords == null) {
95 additionalRecords = new LinkedList<>();
96 extraCaches.put(additionalRecordQuestion, additionalRecords);
97 }
98 additionalRecords.add(extraRecord);
99 }
100 }
101
102 private void putExtraCaches(DnsQueryResult synthesynthesizationSource, Map<DnsMessage, List<Record<? extends Data>>> extraCaches) {
103 DnsMessage reply = synthesynthesizationSource.response;
104 for (Entry<DnsMessage, List<Record<? extends Data>>> entry : extraCaches.entrySet()) {
105 DnsMessage question = entry.getKey();
106 DnsMessage answer = reply.asBuilder()
107 .setQuestion(question.getQuestion())
108 .setAuthoritativeAnswer(true)
109 .addAnswers(entry.getValue())
110 .build();
111 CachedDnsQueryResult cachedDnsQueryResult = new SynthesizedCachedDnsQueryResult(question, answer, synthesynthesizationSource);
112 synchronized (this) {
113 backend.put(question, cachedDnsQueryResult);
114 }
115 }
116 }
117
118 protected boolean shouldGather(Record<? extends Data> extraRecord, Question question, DnsName authoritativeZone) {
119 boolean extraRecordIsChildOfQuestion = extraRecord.name.isChildOf(question.name);
120
121 boolean extraRecordIsChildOfAuthoritativeZone = false;
122 if (authoritativeZone != null) {
123 extraRecordIsChildOfAuthoritativeZone = extraRecord.name.isChildOf(authoritativeZone);
124 }
125
126 return extraRecordIsChildOfQuestion || extraRecordIsChildOfAuthoritativeZone;
127 }
128
129 }