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    *   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.buffer.ByteBuf;
19  import io.netty.handler.codec.CorruptedFrameException;
20  import io.netty.util.CharsetUtil;
21  import io.netty.util.internal.UnstableApi;
22  
23  /**
24   * The default {@link DnsRecordDecoder} implementation.
25   *
26   * @see DefaultDnsRecordEncoder
27   */
28  @UnstableApi
29  public class DefaultDnsRecordDecoder implements DnsRecordDecoder {
30  
31      static final String ROOT = ".";
32  
33      /**
34       * Creates a new instance.
35       */
36      protected DefaultDnsRecordDecoder() { }
37  
38      @Override
39      public final DnsQuestion decodeQuestion(ByteBuf in) throws Exception {
40          String name = decodeName(in);
41          DnsRecordType type = DnsRecordType.valueOf(in.readUnsignedShort());
42          int qClass = in.readUnsignedShort();
43          return new DefaultDnsQuestion(name, type, qClass);
44      }
45  
46      @Override
47      public final <T extends DnsRecord> T decodeRecord(ByteBuf in) throws Exception {
48          final int startOffset = in.readerIndex();
49          final String name = decodeName(in);
50  
51          final int endOffset = in.writerIndex();
52          if (endOffset - startOffset < 10) {
53              // Not enough data
54              in.readerIndex(startOffset);
55              return null;
56          }
57  
58          final DnsRecordType type = DnsRecordType.valueOf(in.readUnsignedShort());
59          final int aClass = in.readUnsignedShort();
60          final long ttl = in.readUnsignedInt();
61          final int length = in.readUnsignedShort();
62          final int offset = in.readerIndex();
63  
64          if (endOffset - offset < length) {
65              // Not enough data
66              in.readerIndex(startOffset);
67              return null;
68          }
69  
70          @SuppressWarnings("unchecked")
71          T record = (T) decodeRecord(name, type, aClass, ttl, in, offset, length);
72          in.readerIndex(offset + length);
73          return record;
74      }
75  
76      /**
77       * Decodes a record from the information decoded so far by {@link #decodeRecord(ByteBuf)}.
78       *
79       * @param name the domain name of the record
80       * @param type the type of the record
81       * @param dnsClass the class of the record
82       * @param timeToLive the TTL of the record
83       * @param in the {@link ByteBuf} that contains the RDATA
84       * @param offset the start offset of the RDATA in {@code in}
85       * @param length the length of the RDATA
86       *
87       * @return a {@link DnsRawRecord}. Override this method to decode RDATA and return other record implementation.
88       */
89      protected DnsRecord decodeRecord(
90              String name, DnsRecordType type, int dnsClass, long timeToLive,
91              ByteBuf in, int offset, int length) throws Exception {
92  
93          // DNS message compression means that domain names may contain "pointers" to other positions in the packet
94          // to build a full message. This means the indexes are meaningful and we need the ability to reference the
95          // indexes un-obstructed, and thus we cannot use a slice here.
96          // See https://www.ietf.org/rfc/rfc1035 [4.1.4. Message compression]
97          if (type == DnsRecordType.PTR) {
98              return new DefaultDnsPtrRecord(
99                      name, dnsClass, timeToLive, decodeName0(in.duplicate().setIndex(offset, offset + length)));
100         }
101         return new DefaultDnsRawRecord(
102                 name, type, dnsClass, timeToLive, in.retainedDuplicate().setIndex(offset, offset + length));
103     }
104 
105     /**
106      * Retrieves a domain name given a buffer containing a DNS packet. If the
107      * name contains a pointer, the position of the buffer will be set to
108      * directly after the pointer's index after the name has been read.
109      *
110      * @param in the byte buffer containing the DNS packet
111      * @return the domain name for an entry
112      */
113     protected String decodeName0(ByteBuf in) {
114         return decodeName(in);
115     }
116 
117     /**
118      * Retrieves a domain name given a buffer containing a DNS packet. If the
119      * name contains a pointer, the position of the buffer will be set to
120      * directly after the pointer's index after the name has been read.
121      *
122      * @param in the byte buffer containing the DNS packet
123      * @return the domain name for an entry
124      */
125     public static String decodeName(ByteBuf in) {
126         int position = -1;
127         int checked = 0;
128         final int end = in.writerIndex();
129         final int readable = in.readableBytes();
130 
131         // Looking at the spec we should always have at least enough readable bytes to read a byte here but it seems
132         // some servers do not respect this for empty names. So just workaround this and return an empty name in this
133         // case.
134         //
135         // See:
136         // - https://github.com/netty/netty/issues/5014
137         // - https://www.ietf.org/rfc/rfc1035.txt , Section 3.1
138         if (readable == 0) {
139             return ROOT;
140         }
141 
142         final StringBuilder name = new StringBuilder(readable << 1);
143         while (in.isReadable()) {
144             final int len = in.readUnsignedByte();
145             final boolean pointer = (len & 0xc0) == 0xc0;
146             if (pointer) {
147                 if (position == -1) {
148                     position = in.readerIndex() + 1;
149                 }
150 
151                 if (!in.isReadable()) {
152                     throw new CorruptedFrameException("truncated pointer in a name");
153                 }
154 
155                 final int next = (len & 0x3f) << 8 | in.readUnsignedByte();
156                 if (next >= end) {
157                     throw new CorruptedFrameException("name has an out-of-range pointer");
158                 }
159                 in.readerIndex(next);
160 
161                 // check for loops
162                 checked += 2;
163                 if (checked >= end) {
164                     throw new CorruptedFrameException("name contains a loop.");
165                 }
166             } else if (len != 0) {
167                 if (!in.isReadable(len)) {
168                     throw new CorruptedFrameException("truncated label in a name");
169                 }
170                 name.append(in.toString(in.readerIndex(), len, CharsetUtil.UTF_8)).append('.');
171                 in.skipBytes(len);
172             } else { // len == 0
173                 break;
174             }
175         }
176 
177         if (position != -1) {
178             in.readerIndex(position);
179         }
180 
181         if (name.length() == 0) {
182             return ROOT;
183         }
184 
185         if (name.charAt(name.length() - 1) != '.') {
186             name.append('.');
187         }
188 
189         return name.toString();
190     }
191 }