View Javadoc
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 DNS response header class which is used when receiving data from a DNS
20   * server. Contains information contained in a DNS response header, such as
21   * recursion availability, and response codes.
22   */
23  public final class DnsResponseHeader extends DnsHeader {
24  
25      private boolean authoritativeAnswer;
26      private boolean truncated;
27      private boolean recursionAvailable;
28  
29      private DnsResponseCode responseCode;
30  
31      /**
32       * Constructor for a DNS packet response header. The id is received by
33       * reading a {@link DnsQuery} and is sent back to the client.
34       *
35       * @param parent
36       *            the {@link DnsMessage} this header belongs to
37       * @param id
38       *            a 2 bit unsigned identification number received from client
39       */
40      public DnsResponseHeader(DnsMessage parent, int id) {
41          super(parent);
42          setId(id);
43      }
44  
45      /**
46       * Returns {@code true} if responding server is authoritative for the domain
47       * name in the query message.
48       */
49      public boolean isAuthoritativeAnswer() {
50          return authoritativeAnswer;
51      }
52  
53      /**
54       * Returns {@code true} if response has been truncated, usually if it is
55       * over 512 bytes.
56       */
57      public boolean isTruncated() {
58          return truncated;
59      }
60  
61      /**
62       * Returns {@code true} if DNS server can handle recursive queries.
63       */
64      public boolean isRecursionAvailable() {
65          return recursionAvailable;
66      }
67  
68      /**
69       * Returns the 4 bit return code.
70       */
71      public DnsResponseCode responseCode() {
72          return responseCode;
73      }
74  
75      /**
76       * Returns the {@link DnsMessage} type. This will always return
77       * {@code TYPE_RESPONSE}.
78       */
79      @Override
80      public int type() {
81          return TYPE_RESPONSE;
82      }
83  
84      /**
85       * Set to {@code true} if responding server is authoritative for the domain
86       * name in the query message.
87       *
88       * @param authoritativeAnswer
89       *            flag for authoritative answer
90       */
91      public DnsResponseHeader setAuthoritativeAnswer(boolean authoritativeAnswer) {
92          this.authoritativeAnswer = authoritativeAnswer;
93          return this;
94      }
95  
96      /**
97       * Set to {@code true} if response has been truncated (usually happens for
98       * responses over 512 bytes).
99       *
100      * @param truncated
101      *            flag for truncation
102      */
103     public DnsResponseHeader setTruncated(boolean truncated) {
104         this.truncated = truncated;
105         return this;
106     }
107 
108     /**
109      * Set to {@code true} if DNS server can handle recursive queries.
110      *
111      * @param recursionAvailable
112      *            flag for recursion availability
113      */
114     public DnsResponseHeader setRecursionAvailable(boolean recursionAvailable) {
115         this.recursionAvailable = recursionAvailable;
116         return this;
117     }
118 
119     /**
120      * Sets the response code for this message.
121      *
122      * @param responseCode
123      *            the response code
124      */
125     public DnsResponseHeader setResponseCode(DnsResponseCode responseCode) {
126         this.responseCode = responseCode;
127         return this;
128     }
129 
130     /**
131      * Sets the {@link DnsHeader} type. Must be {@code TYPE_RESPONSE}.
132      *
133      * @param type
134      *            message type
135      * @return the header to allow method chaining
136      */
137     @Override
138     public DnsResponseHeader setType(int type) {
139         if (type != TYPE_RESPONSE) {
140             throw new IllegalArgumentException("type cannot be anything but TYPE_RESPONSE (1) for a response header.");
141         }
142         super.setType(type);
143         return this;
144     }
145 
146     @Override
147     public DnsResponseHeader setId(int id) {
148         super.setId(id);
149         return this;
150     }
151 
152     @Override
153     public DnsHeader setRecursionDesired(boolean recursionDesired) {
154         return super.setRecursionDesired(recursionDesired);
155     }
156 
157     @Override
158     public DnsResponseHeader setOpcode(int opcode) {
159         super.setOpcode(opcode);
160         return this;
161     }
162 
163     @Override
164     public DnsResponseHeader setZ(int z) {
165         super.setZ(z);
166         return this;
167     }
168 }