View Javadoc
1   /*
2    * Copyright 2016 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 io.netty.util.internal.StringUtil;
19  
20  /**
21   * An <a href="https://tools.ietf.org/html/rfc6891#section-6.1">OPT RR</a> record.
22   *
23   * This is used for <a href="https://tools.ietf.org/html/rfc6891#section-6.1.3">
24   *     Extension Mechanisms for DNS (EDNS(0))</a>.
25   */
26  public abstract class AbstractDnsOptPseudoRrRecord extends AbstractDnsRecord implements DnsOptPseudoRecord {
27  
28      protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize, int extendedRcode, int version) {
29          super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize, packIntoLong(extendedRcode, version));
30      }
31  
32      protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize) {
33          super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize, 0);
34      }
35  
36      // See https://tools.ietf.org/html/rfc6891#section-6.1.3
37      private static long packIntoLong(int val, int val2) {
38          // We are currently not support DO and Z fields, just use 0.
39          return ((val & 0xffL) << 24 | (val2 & 0xff) << 16) & 0xFFFFFFFFL;
40      }
41  
42      @Override
43      public int extendedRcode() {
44          return (short) (((int) timeToLive() >> 24) & 0xff);
45      }
46  
47      @Override
48      public int version() {
49          return (short) (((int) timeToLive() >> 16) & 0xff);
50      }
51  
52      @Override
53      public int flags() {
54         return (short) ((short) timeToLive() & 0xff);
55      }
56  
57      @Override
58      public String toString() {
59          return toStringBuilder().toString();
60      }
61  
62      final StringBuilder toStringBuilder() {
63          return new StringBuilder(64)
64                  .append(StringUtil.simpleClassName(this))
65                  .append('(')
66                  .append("OPT flags:")
67                  .append(flags())
68                  .append(" version:")
69                  .append(version())
70                  .append(" extendedRecode:")
71                  .append(extendedRcode())
72                  .append(" udp:")
73                  .append(dnsClass())
74                  .append(')');
75      }
76  }