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    *   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.netty5.handler.codec.dns;
17  
18  import io.netty5.channel.ChannelHandlerContext;
19  import io.netty5.channel.socket.DatagramPacket;
20  import io.netty5.handler.codec.CorruptedFrameException;
21  import io.netty5.handler.codec.MessageToMessageDecoder;
22  import io.netty5.util.internal.UnstableApi;
23  
24  import java.net.InetSocketAddress;
25  
26  /**
27   * Decodes a {@link DatagramPacket} into a {@link DatagramDnsResponse}.
28   */
29  @UnstableApi
30  public class DatagramDnsResponseDecoder extends MessageToMessageDecoder<DatagramPacket> {
31  
32      private final DnsResponseDecoder<InetSocketAddress> responseDecoder;
33  
34      /**
35       * Creates a new decoder with {@linkplain DnsRecordDecoder#DEFAULT the default record decoder}.
36       */
37      public DatagramDnsResponseDecoder() {
38          this(DnsRecordDecoder.DEFAULT);
39      }
40  
41      /**
42       * Creates a new decoder with the specified {@code recordDecoder}.
43       */
44      public DatagramDnsResponseDecoder(DnsRecordDecoder recordDecoder) {
45          responseDecoder = new DnsResponseDecoder<>(recordDecoder) {
46              @Override
47              protected DnsResponse newResponse(InetSocketAddress sender, InetSocketAddress recipient,
48                                                int id, DnsOpCode opCode, DnsResponseCode responseCode) {
49                  return new DatagramDnsResponse(sender, recipient, id, opCode, responseCode);
50              }
51          };
52      }
53  
54      @Override
55      public boolean isSharable() {
56          return true;
57      }
58  
59      @Override
60      protected void decode(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
61          final DnsResponse response;
62          try {
63              response = decodeResponse(ctx, packet);
64          } catch (IndexOutOfBoundsException e) {
65              throw new CorruptedFrameException("Unable to decode response", e);
66          }
67          ctx.fireChannelRead(response);
68      }
69  
70      protected DnsResponse decodeResponse(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
71          return responseDecoder.decode((InetSocketAddress) packet.sender(), (InetSocketAddress) packet.recipient(),
72                  ctx.bufferAllocator(), packet.content());
73      }
74  }