Debian Codegraph @

minidns-core/src/main/java/org/minidns/dnsmessage/Question.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.dnsmessage;
12
13 import java.io.ByteArrayOutputStream;
14 import java.io.DataInputStream;
15 import java.io.DataOutputStream;
16 import java.io.IOException;
17 import java.util.Arrays;
18
19 import org.minidns.dnsname.DnsName;
20 import org.minidns.record.Record.CLASS;
21 import org.minidns.record.Record.TYPE;
22
23 /**
24 * A DNS question (request).
25 */
26 public class Question {
27
28 /**
29 * The question string (e.g. "measite.de").
30 */
31 public final DnsName name;
32
33 /**
34 * The question type (e.g. A).
35 */
36 public final TYPE type;
37
38 /**
39 * The question class (usually IN for Internet).
40 */
41 public final CLASS clazz;
42
43 /**
44 * UnicastQueries have the highest bit of the CLASS field set to 1.
45 */
46 private final boolean unicastQuery;
47
48 /**
49 * Cache for the serialized object.
50 */
51 private byte[] byteArray;
52
53 /**
54 * Create a dns question for the given name/type/class.
55 * @param name The name e.g. "measite.de".
56 * @param type The type, e.g. A.
57 * @param clazz The class, usually IN (internet).
58 * @param unicastQuery True if this is a unicast query.
59 */
60 public Question(CharSequence name, TYPE type, CLASS clazz, boolean unicastQuery) {
61 this(DnsName.from(name), type, clazz, unicastQuery);
62 }
63
64 public Question(DnsName name, TYPE type, CLASS clazz, boolean unicastQuery) {
65 assert name != null;
66 assert type != null;
67 assert clazz != null;
68 this.name = name;
69 this.type = type;
70 this.clazz = clazz;
71 this.unicastQuery = unicastQuery;
72 }
73
74 /**
75 * Create a dns question for the given name/type/class.
76 * @param name The name e.g. "measite.de".
77 * @param type The type, e.g. A.
78 * @param clazz The class, usually IN (internet).
79 */
80 public Question(DnsName name, TYPE type, CLASS clazz) {
81 this(name, type, clazz, false);
82 }
83
84 /**
85 * Create a dns question for the given name/type/IN (internet class).
86 * @param name The name e.g. "measite.de".
87 * @param type The type, e.g. A.
88 */
89 public Question(DnsName name, TYPE type) {
90 this(name, type, CLASS.IN);
91 }
92
93 /**
94 * Create a dns question for the given name/type/class.
95 * @param name The name e.g. "measite.de".
96 * @param type The type, e.g. A.
97 * @param clazz The class, usually IN (internet).
98 */
99 public Question(CharSequence name, TYPE type, CLASS clazz) {
100 this(DnsName.from(name), type, clazz);
101 }
102
103 /**
104 * Create a dns question for the given name/type/IN (internet class).
105 * @param name The name e.g. "measite.de".
106 * @param type The type, e.g. A.
107 */
108 public Question(CharSequence name, TYPE type) {
109 this(DnsName.from(name), type);
110 }
111
112 /**
113 * Parse a byte array and rebuild the dns question from it.
114 * @param dis The input stream.
115 * @param data The plain data (for dns name references).
116 * @throws IOException On errors (read outside of packet).
117 */
118 public Question(DataInputStream dis, byte[] data) throws IOException {
119 name = DnsName.parse(dis, data);
120 type = TYPE.getType(dis.readUnsignedShort());
121 clazz = CLASS.getClass(dis.readUnsignedShort());
122 unicastQuery = false;
123 }
124
125 /**
126 * Generate a binary paket for this dns question.
127 * @return The dns question.
128 */
129 public byte[] toByteArray() {
130 if (byteArray == null) {
131 ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
132 DataOutputStream dos = new DataOutputStream(baos);
133
134 try {
135 name.writeToStream(dos);
136 dos.writeShort(type.getValue());
137 dos.writeShort(clazz.getValue() | (unicastQuery ? (1 << 15) : 0));
138 dos.flush();
139 } catch (IOException e) {
140 // Should never happen
141 throw new RuntimeException(e);
142 }
143 byteArray = baos.toByteArray();
144 }
145 return byteArray;
146 }
147
148 @Override
149 public int hashCode() {
150 return Arrays.hashCode(toByteArray());
151 }
152
153 @Override
154 public boolean equals(Object other) {
155 if (this == other) {
156 return true;
157 }
158 if (!(other instanceof Question)) {
159 return false;
160 }
161 byte[] t = toByteArray();
162 byte[] o = ((Question) other).toByteArray();
163 return Arrays.equals(t, o);
164 }
165
166 @Override
167 public String toString() {
168 return name.getRawAce() + ".\t" + clazz + '\t' + type;
169 }
170
171 public DnsMessage.Builder asMessageBuilder() {
172 DnsMessage.Builder builder = DnsMessage.builder();
173 builder.setQuestion(this);
174 return builder;
175 }
176
177 public DnsMessage asQueryMessage() {
178 return asMessageBuilder().build();
179 }
180 }