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 query header class which is used to represent the 12 byte header in a
20   * {@link DnsQuery}.
21   */
22  public final class DnsQueryHeader extends DnsHeader {
23  
24      /**
25       * Constructor for a DNS packet query header. The id is user generated and
26       * will be replicated in the response packet by the server.
27       *
28       * @param parent the {@link DnsMessage} this header belongs to
29       * @param id     a 2 bit unsigned identification number for this query
30       */
31      public DnsQueryHeader(DnsMessage parent, int id) {
32          super(parent);
33          setId(id);
34          setRecursionDesired(true);
35      }
36  
37      /**
38       * Returns the {@link DnsMessage} type. This will always return
39       * {@code TYPE_QUERY}.
40       */
41      @Override
42      public int type() {
43          return TYPE_QUERY;
44      }
45  
46      /**
47       * Sets the {@link DnsHeader} type. Must be {@code TYPE_RESPONSE}.
48       *
49       * @param type message type
50       * @return the header to allow method chaining
51       */
52      @Override
53      public DnsQueryHeader setType(int type) {
54          if (type != TYPE_QUERY) {
55              throw new IllegalArgumentException("type cannot be anything but TYPE_QUERY (0) for a query header.");
56          }
57          super.setType(type);
58          return this;
59      }
60  
61      @Override
62      public DnsQueryHeader setId(int id) {
63          super.setId(id);
64          return this;
65      }
66  
67      @Override
68      public DnsQueryHeader setRecursionDesired(boolean recursionDesired) {
69          super.setRecursionDesired(recursionDesired);
70          return this;
71      }
72  
73      @Override
74      public DnsQueryHeader setOpcode(int opcode) {
75          super.setOpcode(opcode);
76          return this;
77      }
78  
79      @Override
80      public DnsQueryHeader setZ(int z) {
81          super.setZ(z);
82          return this;
83      }
84  }