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.channel.ChannelHandlerContext;
19 import io.netty5.channel.socket.DatagramPacket;
20 import io.netty5.handler.codec.MessageToMessageDecoder;
21 import io.netty5.util.internal.UnstableApi;
22
23 import static java.util.Objects.requireNonNull;
24
25
26
27
28 @UnstableApi
29 public class DatagramDnsQueryDecoder extends MessageToMessageDecoder<DatagramPacket> {
30
31 private final DnsRecordDecoder recordDecoder;
32
33
34
35
36 public DatagramDnsQueryDecoder() {
37 this(DnsRecordDecoder.DEFAULT);
38 }
39
40
41
42
43 public DatagramDnsQueryDecoder(DnsRecordDecoder recordDecoder) {
44 this.recordDecoder = requireNonNull(recordDecoder, "recordDecoder");
45 }
46
47 @Override
48 public boolean isSharable() {
49 return true;
50 }
51
52 @Override
53 protected void decode(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
54 DnsQuery query = DnsMessageUtil.decodeDnsQuery(recordDecoder, ctx.bufferAllocator(), packet.content(),
55 (id, dnsOpCode) -> new DatagramDnsQuery(packet.sender(), packet.recipient(), id, dnsOpCode));
56 ctx.fireChannelRead(query);
57 }
58 }