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.channel.ChannelHandler;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.socket.DatagramPacket;
22  import io.netty.handler.codec.CorruptedFrameException;
23  import io.netty.handler.codec.MessageToMessageDecoder;
24  import io.netty.util.internal.UnstableApi;
25  
26  import java.util.List;
27  
28  import static io.netty.util.internal.ObjectUtil.checkNotNull;
29  
30  /**
31   * Decodes a {@link DatagramPacket} into a {@link DatagramDnsQuery}.
32   */
33  @UnstableApi
34  @ChannelHandler.Sharable
35  public class DatagramDnsQueryDecoder extends MessageToMessageDecoder<DatagramPacket> {
36  
37      private final DnsRecordDecoder recordDecoder;
38  
39      /**
40       * Creates a new decoder with {@linkplain DnsRecordDecoder#DEFAULT the default record decoder}.
41       */
42      public DatagramDnsQueryDecoder() {
43          this(DnsRecordDecoder.DEFAULT);
44      }
45  
46      /**
47       * Creates a new decoder with the specified {@code recordDecoder}.
48       */
49      public DatagramDnsQueryDecoder(DnsRecordDecoder recordDecoder) {
50          this.recordDecoder = checkNotNull(recordDecoder, "recordDecoder");
51      }
52  
53      @Override
54      protected void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
55          final ByteBuf buf = packet.content();
56  
57          final DnsQuery query = newQuery(packet, buf);
58          boolean success = false;
59          try {
60              final int questionCount = buf.readUnsignedShort();
61              final int answerCount = buf.readUnsignedShort();
62              final int authorityRecordCount = buf.readUnsignedShort();
63              final int additionalRecordCount = buf.readUnsignedShort();
64  
65              decodeQuestions(query, buf, questionCount);
66              decodeRecords(query, DnsSection.ANSWER, buf, answerCount);
67              decodeRecords(query, DnsSection.AUTHORITY, buf, authorityRecordCount);
68              decodeRecords(query, DnsSection.ADDITIONAL, buf, additionalRecordCount);
69  
70              out.add(query);
71              success = true;
72          } finally {
73              if (!success) {
74                  query.release();
75              }
76          }
77      }
78  
79      private static DnsQuery newQuery(DatagramPacket packet, ByteBuf buf) {
80          final int id = buf.readUnsignedShort();
81  
82          final int flags = buf.readUnsignedShort();
83          if (flags >> 15 == 1) {
84              throw new CorruptedFrameException("not a query");
85          }
86          final DnsQuery query =
87              new DatagramDnsQuery(
88                  packet.sender(),
89                  packet.recipient(),
90                  id,
91                  DnsOpCode.valueOf((byte) (flags >> 11 & 0xf)));
92          query.setRecursionDesired((flags >> 8 & 1) == 1);
93          query.setZ(flags >> 4 & 0x7);
94          return query;
95      }
96  
97      private void decodeQuestions(DnsQuery query, ByteBuf buf, int questionCount) throws Exception {
98          for (int i = questionCount; i > 0; i--) {
99              query.addRecord(DnsSection.QUESTION, recordDecoder.decodeQuestion(buf));
100         }
101     }
102 
103     private void decodeRecords(
104         DnsQuery query, DnsSection section, ByteBuf buf, int count) throws Exception {
105         for (int i = count; i > 0; i--) {
106             final DnsRecord r = recordDecoder.decodeRecord(buf);
107             if (r == null) {
108                 // Truncated response
109                 break;
110             }
111 
112             query.addRecord(section, r);
113         }
114     }
115 }