Debian Codegraph @

minidns-iterative-resolver/src/main/java/org/minidns/iterative/ResolutionState.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.net.InetAddress;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 import org.minidns.dnsmessage.DnsMessage;
19 import org.minidns.dnsmessage.Question;
20 import org.minidns.iterative.IterativeClientException.LoopDetected;
21 import org.minidns.iterative.IterativeClientException.MaxIterativeStepsReached;
22
23 public class ResolutionState {
24
25 private final IterativeDnsClient recursiveDnsClient;
26 private final HashMap<InetAddress, Set<Question>> map = new HashMap<>();
27 private int steps;
28
29 ResolutionState(IterativeDnsClient recursiveDnsClient) {
30 this.recursiveDnsClient = recursiveDnsClient;
31 }
32
33 void recurse(InetAddress address, DnsMessage query) throws LoopDetected, MaxIterativeStepsReached {
34 Question question = query.getQuestion();
35 if (!map.containsKey(address)) {
36 map.put(address, new HashSet<Question>());
37 } else if (map.get(address).contains(question)) {
38 throw new IterativeClientException.LoopDetected(address, question);
39 }
40
41 if (++steps > recursiveDnsClient.maxSteps) {
42 throw new IterativeClientException.MaxIterativeStepsReached();
43 }
44
45 boolean isNew = map.get(address).add(question);
46 assert isNew;
47 }
48
49 void decrementSteps() {
50 steps--;
51 }
52
53 }