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