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