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