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          this(maxPayloadSize, extendedRcode, version, 0);
30      }
31  
32      /**
33       * Creates a new instance.
34       *
35       * @param maxPayloadSize    the maximum UDP payload size that can be reassembled and delivered by this
36       *                          endpoint's network stack, encoded into the {@code CLASS} field
37       * @param extendedRcode     the upper 8 bits of the extended 12-bit RCODE; the lower 4 bits are carried
38       *                          by the DNS message header
39       * @param version           the EDNS version
40       * @param flags             the 16-bit flags field, which holds {@code DO} and {@code Z}; only its
41       *                          low 16 bits are used
42       */
43      protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize, int extendedRcode, int version, int flags) {
44          super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize,
45                  packIntoLong(extendedRcode, version, flags));
46      }
47  
48      protected AbstractDnsOptPseudoRrRecord(int maxPayloadSize) {
49          super(StringUtil.EMPTY_STRING, DnsRecordType.OPT, maxPayloadSize, 0);
50      }
51  
52      // See https://tools.ietf.org/html/rfc6891#section-6.1.3
53      private static long packIntoLong(int extendedRcode, int version, int flags) {
54          return ((extendedRcode & 0xffL) << 24 | (version & 0xffL) << 16 | (flags & 0xffffL)) & 0xFFFFFFFFL;
55      }
56  
57      @Override
58      public int extendedRcode() {
59          return (short) (((int) timeToLive() >> 24) & 0xff);
60      }
61  
62      @Override
63      public int version() {
64          return (short) (((int) timeToLive() >> 16) & 0xff);
65      }
66  
67      @Override
68      public int flags() {
69          return (int) (timeToLive() & 0xffffL);
70      }
71  
72      @Override
73      public String toString() {
74          return toStringBuilder().toString();
75      }
76  
77      final StringBuilder toStringBuilder() {
78          return new StringBuilder(64)
79                  .append(StringUtil.simpleClassName(this))
80                  .append('(')
81                  .append("OPT flags:")
82                  .append(flags())
83                  .append(" version:")
84                  .append(version())
85                  .append(" extendedRecode:")
86                  .append(extendedRcode())
87                  .append(" udp:")
88                  .append(dnsClass())
89                  .append(')');
90      }
91  }