1 /*
2 * Copyright 2013 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.dns;
17
18 /**
19 * The header super-class which includes information shared by DNS query and
20 * response packet headers such as the ID, opcode, and type. The only flag
21 * shared by both classes is the flag for desiring recursion.
22 */
23 public class DnsHeader {
24
25 /**
26 * Message type is query.
27 */
28 public static final int TYPE_QUERY = 0;
29
30 /**
31 * Message type is response.
32 */
33 public static final int TYPE_RESPONSE = 1;
34
35 /**
36 * Message is for a standard query.
37 */
38 public static final int OPCODE_QUERY = 0;
39
40 /**
41 * Message is for an inverse query. <strong>Note: inverse queries have been
42 * obsoleted since RFC 3425, and are not necessarily supported.</strong>
43 */
44 @Deprecated
45 public static final int OPCODE_IQUERY = 1;
46
47 private final DnsMessage parent;
48
49 private boolean recursionDesired;
50 private int opcode;
51 private int id;
52 private int type;
53 private int z;
54
55 // only allow to extend from within the same package
56 DnsHeader(DnsMessage parent) {
57 if (parent == null) {
58 throw new NullPointerException("parent");
59 }
60 this.parent = parent;
61 }
62
63 /**
64 * Returns the number of questions in the {@link DnsMessage}.
65 */
66 public int questionCount() {
67 return parent.questions().size();
68 }
69
70 /**
71 * Returns the number of answer resource records in the {@link DnsMessage}.
72 */
73 public int answerCount() {
74 return parent.answers().size();
75 }
76
77 /**
78 * Returns the number of authority resource records in the
79 * {@link DnsMessage}.
80 */
81 public int authorityResourceCount() {
82 return parent.authorityResources().size();
83 }
84
85 /**
86 * Returns the number of additional resource records in the
87 * {@link DnsMessage}.
88 */
89 public int additionalResourceCount() {
90 return parent.additionalResources().size();
91 }
92
93 /**
94 * Returns {@code true} if a query is to be pursued recursively.
95 */
96 public boolean isRecursionDesired() {
97 return recursionDesired;
98 }
99
100 /**
101 * Returns the 4 bit opcode used for the {@link DnsMessage}.
102 *
103 * @see #OPCODE_QUERY
104 * @see #OPCODE_IQUERY
105 */
106 public int opcode() {
107 return opcode;
108 }
109
110 /**
111 * Returns the type of {@link DnsMessage}.
112 *
113 * @see #TYPE_QUERY
114 */
115 public int type() {
116 return type;
117 }
118
119 /**
120 * Returns the 2 byte unsigned identifier number used for the
121 * {@link DnsMessage}.
122 */
123 public int id() {
124 return id;
125 }
126
127 /**
128 * Sets the opcode for this {@link DnsMessage}.
129 *
130 * @param opcode
131 * opcode to set
132 * @return the header to allow method chaining
133 */
134 public DnsHeader setOpcode(int opcode) {
135 this.opcode = opcode;
136 return this;
137 }
138
139 /**
140 * Sets whether a name server is directed to pursue a query recursively or
141 * not.
142 *
143 * @param recursionDesired
144 * if set to {@code true}, pursues query recursively
145 * @return the header to allow method chaining
146 */
147 public DnsHeader setRecursionDesired(boolean recursionDesired) {
148 this.recursionDesired = recursionDesired;
149 return this;
150 }
151
152 /**
153 * Sets the {@link DnsMessage} type.
154 *
155 * @param type
156 * message type
157 * @return the header to allow method chaining
158 */
159 public DnsHeader setType(int type) {
160 this.type = type;
161 return this;
162 }
163
164 /**
165 * Sets the id for this {@link DnsMessage}.
166 *
167 * @param id
168 * a unique 2 byte unsigned identifier
169 * @return the header to allow method chaining
170 */
171 public DnsHeader setId(int id) {
172 this.id = id;
173 return this;
174 }
175
176 /**
177 * Returns the 3 bit reserved field 'Z'.
178 */
179 public int z() {
180 return z;
181 }
182
183 /**
184 * Sets the field Z. This field is reserved and should remain as 0 if the
185 * DNS server does not make usage of this field.
186 *
187 * @param z
188 * the value for the reserved field Z
189 */
190 public DnsHeader setZ(int z) {
191 this.z = z;
192 return this;
193 }
194 }