1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.example.dns.udp;
17
18 import io.netty5.bootstrap.Bootstrap;
19 import io.netty5.buffer.BufferUtil;
20 import io.netty5.channel.Channel;
21 import io.netty5.channel.ChannelHandlerContext;
22 import io.netty5.channel.ChannelInitializer;
23 import io.netty5.channel.ChannelPipeline;
24 import io.netty5.channel.EventLoopGroup;
25 import io.netty5.channel.MultithreadEventLoopGroup;
26 import io.netty5.channel.SimpleChannelInboundHandler;
27 import io.netty5.channel.nio.NioHandler;
28 import io.netty5.channel.socket.DatagramChannel;
29 import io.netty5.channel.socket.nio.NioDatagramChannel;
30 import io.netty5.handler.codec.dns.DatagramDnsQuery;
31 import io.netty5.handler.codec.dns.DatagramDnsQueryEncoder;
32 import io.netty5.handler.codec.dns.DatagramDnsResponse;
33 import io.netty5.handler.codec.dns.DatagramDnsResponseDecoder;
34 import io.netty5.handler.codec.dns.DefaultDnsQuestion;
35 import io.netty5.handler.codec.dns.DnsQuery;
36 import io.netty5.handler.codec.dns.DnsQuestion;
37 import io.netty5.handler.codec.dns.DnsRawRecord;
38 import io.netty5.handler.codec.dns.DnsRecord;
39 import io.netty5.handler.codec.dns.DnsRecordType;
40 import io.netty5.handler.codec.dns.DnsSection;
41 import io.netty5.util.NetUtil;
42
43 import java.net.InetSocketAddress;
44 import java.util.concurrent.TimeUnit;
45
46 public final class DnsClient {
47
48 private static final String QUERY_DOMAIN = "www.example.com";
49 private static final int DNS_SERVER_PORT = 53;
50 private static final String DNS_SERVER_HOST = "8.8.8.8";
51
52 private DnsClient() { }
53
54 private static void handleQueryResp(DatagramDnsResponse msg) {
55 if (msg.count(DnsSection.QUESTION) > 0) {
56 DnsQuestion question = msg.recordAt(DnsSection.QUESTION, 0);
57 System.out.printf("name: %s%n", question.name());
58 }
59 for (int i = 0, count = msg.count(DnsSection.ANSWER); i < count; i++) {
60 DnsRecord record = msg.recordAt(DnsSection.ANSWER, i);
61 if (record.type() == DnsRecordType.A) {
62
63 DnsRawRecord raw = (DnsRawRecord) record;
64 System.out.println(NetUtil.bytesToIpAddress(BufferUtil.getBytes(raw.content())));
65 }
66 }
67 }
68
69 public static void main(String[] args) throws Exception {
70 InetSocketAddress addr = new InetSocketAddress(DNS_SERVER_HOST, DNS_SERVER_PORT);
71 EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
72 try {
73 Bootstrap b = new Bootstrap();
74 b.group(group)
75 .channel(NioDatagramChannel.class)
76 .handler(new ChannelInitializer<DatagramChannel>() {
77 @Override
78 protected void initChannel(DatagramChannel ch) {
79 ChannelPipeline p = ch.pipeline();
80 p.addLast(new DatagramDnsQueryEncoder())
81 .addLast(new DatagramDnsResponseDecoder())
82 .addLast(new SimpleChannelInboundHandler<DatagramDnsResponse>() {
83 @Override
84 protected void messageReceived(ChannelHandlerContext ctx, DatagramDnsResponse msg) {
85 try {
86 handleQueryResp(msg);
87 } finally {
88 ctx.close();
89 }
90 }
91 });
92 }
93 });
94 final Channel ch = b.bind(0).asStage().get();
95 DnsQuery query = new DatagramDnsQuery(null, addr, 1).setRecord(
96 DnsSection.QUESTION,
97 new DefaultDnsQuestion(QUERY_DOMAIN, DnsRecordType.A));
98 ch.writeAndFlush(query).asStage().sync();
99 boolean success = ch.closeFuture().asStage().await(10, TimeUnit.SECONDS);
100 if (!success) {
101 System.err.println("dns query timeout!");
102 ch.close().asStage().sync();
103 }
104 } finally {
105 group.shutdownGracefully();
106 }
107 }
108 }