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.AddressedEnvelope;
20 import io.netty5.channel.ChannelHandlerContext;
21 import io.netty5.channel.socket.DatagramPacket;
22 import io.netty5.handler.codec.MessageToMessageEncoder;
23 import io.netty5.util.internal.UnstableApi;
24
25 import java.net.InetSocketAddress;
26 import java.util.List;
27
28 import static java.util.Objects.requireNonNull;
29
30
31
32
33
34 @UnstableApi
35 public class DatagramDnsResponseEncoder
36 extends MessageToMessageEncoder<AddressedEnvelope<DnsResponse, InetSocketAddress>> {
37
38 private final DnsRecordEncoder recordEncoder;
39
40
41
42
43 public DatagramDnsResponseEncoder() {
44 this(DnsRecordEncoder.DEFAULT);
45 }
46
47
48
49
50 public DatagramDnsResponseEncoder(DnsRecordEncoder recordEncoder) {
51 this.recordEncoder = requireNonNull(recordEncoder, "recordEncoder");
52 }
53
54 @Override
55 public boolean isSharable() {
56 return true;
57 }
58
59 @Override
60 protected void encode(ChannelHandlerContext ctx,
61 AddressedEnvelope<DnsResponse, InetSocketAddress> in, List<Object> out) throws Exception {
62
63 final InetSocketAddress recipient = in.recipient();
64 final DnsResponse response = in.content();
65 final Buffer buf = allocateBuffer(ctx, in);
66
67 DnsMessageUtil.encodeDnsResponse(recordEncoder, response, buf);
68
69 out.add(new DatagramPacket(buf, recipient, null));
70 }
71
72
73
74
75
76 protected Buffer allocateBuffer(
77 ChannelHandlerContext ctx,
78 @SuppressWarnings("unused") AddressedEnvelope<DnsResponse, InetSocketAddress> msg) throws Exception {
79 return ctx.bufferAllocator().allocate(1024);
80 }
81 }