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 DatagramDnsResponse}.
32   */
33  @UnstableApi
34  @ChannelHandler.Sharable
35  public class DatagramDnsResponseDecoder 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 DatagramDnsResponseDecoder() {
43          this(DnsRecordDecoder.DEFAULT);
44      }
45  
46      /**
47       * Creates a new decoder with the specified {@code recordDecoder}.
48       */
49      public DatagramDnsResponseDecoder(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 DnsResponse response = newResponse(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(response, buf, questionCount);
66              decodeRecords(response, DnsSection.ANSWER, buf, answerCount);
67              decodeRecords(response, DnsSection.AUTHORITY, buf, authorityRecordCount);
68              decodeRecords(response, DnsSection.ADDITIONAL, buf, additionalRecordCount);
69  
70              out.add(response);
71              success = true;
72          } finally {
73              if (!success) {
74                  response.release();
75              }
76          }
77      }
78  
79      private static DnsResponse newResponse(DatagramPacket packet, ByteBuf buf) {
80          final int id = buf.readUnsignedShort();
81  
82          final int flags = buf.readUnsignedShort();
83          if (flags >> 15 == 0) {
84              throw new CorruptedFrameException("not a response");
85          }
86  
87          final DnsResponse response = new DatagramDnsResponse(
88              packet.sender(),
89              packet.recipient(),
90              id,
91              DnsOpCode.valueOf((byte) (flags >> 11 & 0xf)), DnsResponseCode.valueOf((byte) (flags & 0xf)));
92  
93          response.setRecursionDesired((flags >> 8 & 1) == 1);
94          response.setAuthoritativeAnswer((flags >> 10 & 1) == 1);
95          response.setTruncated((flags >> 9 & 1) == 1);
96          response.setRecursionAvailable((flags >> 7 & 1) == 1);
97          response.setZ(flags >> 4 & 0x7);
98          return response;
99      }
100 
101     private void decodeQuestions(DnsResponse response, ByteBuf buf, int questionCount) throws Exception {
102         for (int i = questionCount; i > 0; i --) {
103             response.addRecord(DnsSection.QUESTION, recordDecoder.decodeQuestion(buf));
104         }
105     }
106 
107     private void decodeRecords(
108             DnsResponse response, DnsSection section, ByteBuf buf, int count) throws Exception {
109         for (int i = count; i > 0; i --) {
110             final DnsRecord r = recordDecoder.decodeRecord(buf);
111             if (r == null) {
112                 // Truncated response
113                 break;
114             }
115 
116             response.addRecord(section, r);
117         }
118     }
119 }