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