1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.dns;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
21
22 import java.net.SocketAddress;
23
24 public final class TcpDnsResponseDecoder extends LengthFieldBasedFrameDecoder {
25
26 private final DnsResponseDecoder<SocketAddress> responseDecoder;
27
28
29
30
31 public TcpDnsResponseDecoder() {
32 this(DnsRecordDecoder.DEFAULT, 64 * 1024);
33 }
34
35
36
37
38 public TcpDnsResponseDecoder(DnsRecordDecoder recordDecoder, int maxFrameLength) {
39
40
41 super(maxFrameLength, 0, 2, 0, 2);
42
43 this.responseDecoder = new DnsResponseDecoder<SocketAddress>(recordDecoder) {
44 @Override
45 protected DnsResponse newResponse(SocketAddress sender, SocketAddress recipient,
46 int id, DnsOpCode opCode, DnsResponseCode responseCode) {
47 return new DefaultDnsResponse(id, opCode, responseCode);
48 }
49 };
50 }
51
52 @Override
53 protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
54 ByteBuf frame = (ByteBuf) super.decode(ctx, in);
55 if (frame == null) {
56 return null;
57 }
58
59 try {
60 return responseDecoder.decode(ctx.channel().remoteAddress(), ctx.channel().localAddress(), frame.slice());
61 } finally {
62 frame.release();
63 }
64 }
65
66 @Override
67 protected ByteBuf extractFrame(ChannelHandlerContext ctx, ByteBuf buffer, int index, int length) {
68 return buffer.copy(index, length);
69 }
70 }