Debian Codegraph @

minidns-iterative-resolver/src/main/java/org/minidns/iterative/ReliableDnsClient.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.iterative;
12
13 import java.io.IOException;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.logging.Level;
17
18 import org.minidns.AbstractDnsClient;
19 import org.minidns.DnsCache;
20 import org.minidns.DnsClient;
21 import org.minidns.dnsmessage.DnsMessage;
22 import org.minidns.dnsmessage.Question;
23 import org.minidns.dnsqueryresult.DnsQueryResult;
24 import org.minidns.source.DnsDataSource;
25 import org.minidns.util.MultipleIoException;
26
27 /**
28 * A DNS client using a reliable strategy. First the configured resolver of the
29 * system are used, then, in case there is no answer, a fall back to iterative
30 * resolving is performed.
31 */
32 public class ReliableDnsClient extends AbstractDnsClient {
33
34 public enum Mode {
35 /**
36 * Try the recursive servers first and fallback to iterative resolving if it fails. This is the default mode.
37 */
38 recursiveWithIterativeFallback,
39
40 /**
41 * Only try the recursive servers. This makes {@code ReliableDnsClient} behave like a {@link DnsClient}.
42 */
43 recursiveOnly,
44
45 /**
46 * Only use iterative resolving. This makes {@code ReliableDnsClient} behave like a {@link IterativeDnsClient}.
47 */
48 iterativeOnly,
49 }
50
51 private final IterativeDnsClient recursiveDnsClient;
52 private final DnsClient dnsClient;
53
54 private Mode mode = Mode.recursiveWithIterativeFallback;
55
56 public ReliableDnsClient(DnsCache dnsCache) {
57 super(dnsCache);
58 recursiveDnsClient = new IterativeDnsClient(dnsCache) {
59 @Override
60 protected DnsMessage.Builder newQuestion(DnsMessage.Builder questionMessage) {
61 questionMessage = super.newQuestion(questionMessage);
62 return ReliableDnsClient.this.newQuestion(questionMessage);
63 }
64 // TODO: Rename dnsMessage to result.
65 @Override
66 protected boolean isResponseCacheable(Question q, DnsQueryResult dnsMessage) {
67 boolean res = super.isResponseCacheable(q, dnsMessage);
68 return ReliableDnsClient.this.isResponseCacheable(q, dnsMessage) && res;
69 }
70 };
71 dnsClient = new DnsClient(dnsCache) {
72 @Override
73 protected DnsMessage.Builder newQuestion(DnsMessage.Builder questionMessage) {
74 questionMessage = super.newQuestion(questionMessage);
75 return ReliableDnsClient.this.newQuestion(questionMessage);
76 }
77 // TODO: Rename dnsMessage to result.
78 @Override
79 protected boolean isResponseCacheable(Question q, DnsQueryResult dnsMessage) {
80 boolean res = super.isResponseCacheable(q, dnsMessage);
81 return ReliableDnsClient.this.isResponseCacheable(q, dnsMessage) && res;
82 }
83 };
84 }
85
86 public ReliableDnsClient() {
87 this(DEFAULT_CACHE);
88 }
89
90 @Override
91 protected DnsQueryResult query(DnsMessage.Builder q) throws IOException {
92 DnsQueryResult dnsMessage = null;
93 String unacceptableReason = null;
94 List<IOException> ioExceptions = new LinkedList<>();
95
96 if (mode != Mode.iterativeOnly) {
97 // Try a recursive query.
98 try {
99 dnsMessage = dnsClient.query(q);
100 if (dnsMessage != null) {
101 unacceptableReason = isResponseAcceptable(dnsMessage.response);
102 if (unacceptableReason == null) {
103 return dnsMessage;
104 }
105 }
106 } catch (IOException ioException) {
107 ioExceptions.add(ioException);
108 }
109 }
110
111 // Abort if we the are in "recursive only" mode.
112 if (mode == Mode.recursiveOnly) return dnsMessage;
113
114 // Eventually log that we fall back to iterative mode.
115 final Level FALLBACK_LOG_LEVEL = Level.FINE;
116 if (LOGGER.isLoggable(FALLBACK_LOG_LEVEL) && mode != Mode.iterativeOnly) {
117 String logString = "Resolution fall back to iterative mode because: ";
118 if (!ioExceptions.isEmpty()) {
119 logString += ioExceptions.get(0);
120 } else if (dnsMessage == null) {
121 logString += " DnsClient did not return a response";
122 } else if (unacceptableReason != null) {
123 logString += unacceptableReason + ". Response:\n" + dnsMessage;
124 } else {
125 throw new AssertionError("This should never been reached");
126 }
127 LOGGER.log(FALLBACK_LOG_LEVEL, logString);
128 }
129
130 try {
131 dnsMessage = recursiveDnsClient.query(q);
132 assert dnsMessage != null;
133 } catch (IOException ioException) {
134 ioExceptions.add(ioException);
135 }
136
137 if (dnsMessage == null) {
138 assert !ioExceptions.isEmpty();
139 MultipleIoException.throwIfRequired(ioExceptions);
140 }
141
142 return dnsMessage;
143 }
144
145 @Override
146 protected DnsMessage.Builder newQuestion(DnsMessage.Builder questionMessage) {
147 return questionMessage;
148 }
149
150 @Override
151 protected boolean isResponseCacheable(Question q, DnsQueryResult result) {
152 return isResponseAcceptable(result.response) == null;
153 }
154
155 /**
156 * Check if the response from the system's nameserver is acceptable. Must return <code>null</code> if the response
157 * is acceptable, or a String describing why it is not acceptable. If the response is not acceptable then
158 * {@link ReliableDnsClient} will fall back to resolve the query iteratively.
159 *
160 * @param response the response we got from the system's nameserver.
161 * @return <code>null</code> if the response is acceptable, or a String if not.
162 */
163 protected String isResponseAcceptable(DnsMessage response) {
164 return null;
165 }
166
167 @Override
168 public void setDataSource(DnsDataSource dataSource) {
169 super.setDataSource(dataSource);
170 recursiveDnsClient.setDataSource(dataSource);
171 dnsClient.setDataSource(dataSource);
172 }
173
174 /**
175 * Set the mode used when resolving queries.
176 *
177 * @param mode the mode to use.
178 */
179 public void setMode(Mode mode) {
180 if (mode == null) {
181 throw new IllegalArgumentException("Mode must not be null.");
182 }
183 this.mode = mode;
184 }
185
186 public void setUseHardcodedDnsServers(boolean useHardcodedDnsServers) {
187 dnsClient.setUseHardcodedDnsServers(useHardcodedDnsServers);
188 }
189
190 }