View Javadoc
1   /*
2    * Copyright 2015 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    *   https://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  import static io.netty.util.internal.ObjectUtil.checkNotNull;
19  
20  /**
21   * The DNS {@code OpCode} as defined in <a href="https://tools.ietf.org/html/rfc2929">RFC2929</a>.
22   */
23  public class DnsOpCode implements Comparable<DnsOpCode> {
24  
25      /**
26       * The 'Query' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
27       */
28      public static final DnsOpCode QUERY = new DnsOpCode(0x00, "QUERY");
29  
30      /**
31       * The 'IQuery' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
32       */
33      public static final DnsOpCode IQUERY = new DnsOpCode(0x01, "IQUERY");
34  
35      /**
36       * The 'Status' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1035">RFC1035</a>.
37       */
38      public static final DnsOpCode STATUS = new DnsOpCode(0x02, "STATUS");
39  
40      /**
41       * The 'Notify' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc1996">RFC1996</a>.
42       */
43      public static final DnsOpCode NOTIFY = new DnsOpCode(0x04, "NOTIFY");
44  
45      /**
46       * The 'Update' DNS OpCode, as defined in <a href="https://tools.ietf.org/html/rfc2136">RFC2136</a>.
47       */
48      public static final DnsOpCode UPDATE = new DnsOpCode(0x05, "UPDATE");
49  
50      /**
51       * Returns the {@link DnsOpCode} instance of the specified byte value.
52       */
53      public static DnsOpCode valueOf(int b) {
54          switch (b) {
55          case 0x00:
56              return QUERY;
57          case 0x01:
58              return IQUERY;
59          case 0x02:
60              return STATUS;
61          case 0x04:
62              return NOTIFY;
63          case 0x05:
64              return UPDATE;
65          default:
66              break;
67          }
68  
69          return new DnsOpCode(b);
70      }
71  
72      private final byte byteValue;
73      private final String name;
74      private String text;
75  
76      private DnsOpCode(int byteValue) {
77          this(byteValue, "UNKNOWN");
78      }
79  
80      public DnsOpCode(int byteValue, String name) {
81          this.byteValue = (byte) byteValue;
82          this.name = checkNotNull(name, "name");
83      }
84  
85      public byte byteValue() {
86          return byteValue;
87      }
88  
89      @Override
90      public int hashCode() {
91          return byteValue;
92      }
93  
94      @Override
95      public boolean equals(Object obj) {
96          if (this == obj) {
97              return true;
98          }
99  
100         if (!(obj instanceof DnsOpCode)) {
101             return false;
102         }
103 
104         return byteValue == ((DnsOpCode) obj).byteValue;
105     }
106 
107     @Override
108     public int compareTo(DnsOpCode o) {
109         return byteValue - o.byteValue;
110     }
111 
112     @Override
113     public String toString() {
114         String text = this.text;
115         if (text == null) {
116             this.text = text = name + '(' + (byteValue & 0xFF) + ')';
117         }
118         return text;
119     }
120 }