Home / Class/ DoTClient Class — netty Architecture

DoTClient Class — netty Architecture

Architecture documentation for the DoTClient class in DoTClient.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  5239d577_66b3_1cd2_3150_13fb7dba265f["DoTClient"]
  71ca1531_122f_c6a4_4d92_3dd7635522e3["DoTClient.java"]
  5239d577_66b3_1cd2_3150_13fb7dba265f -->|defined in| 71ca1531_122f_c6a4_4d92_3dd7635522e3
  420633ad_78bd_c4e5_fcb9_e74dcd851469["DoTClient()"]
  5239d577_66b3_1cd2_3150_13fb7dba265f -->|method| 420633ad_78bd_c4e5_fcb9_e74dcd851469
  e3f30336_5d9e_1f18_3e77_758d6df5b165["handleQueryResp()"]
  5239d577_66b3_1cd2_3150_13fb7dba265f -->|method| e3f30336_5d9e_1f18_3e77_758d6df5b165
  1055fa45_0611_991b_2283_5b8e5df9faab["main()"]
  5239d577_66b3_1cd2_3150_13fb7dba265f -->|method| 1055fa45_0611_991b_2283_5b8e5df9faab

Relationship Graph

Source Code

example/src/main/java/io/netty/example/dns/dot/DoTClient.java lines 51–118

public final class DoTClient {
    private static final String QUERY_DOMAIN = "www.example.com";
    private static final int DNS_SERVER_PORT = 853;
    private static final String DNS_SERVER_HOST = "8.8.8.8";

    private DoTClient() {
    }

    private static void handleQueryResp(DefaultDnsResponse msg) {
        if (msg.count(DnsSection.QUESTION) > 0) {
            DnsQuestion question = msg.recordAt(DnsSection.QUESTION, 0);
            System.out.printf("name: %s%n", question.name());
        }
        for (int i = 0, count = msg.count(DnsSection.ANSWER); i < count; i++) {
            DnsRecord record = msg.recordAt(DnsSection.ANSWER, i);
            if (record.type() == DnsRecordType.A) {
                //just print the IP after query
                DnsRawRecord raw = (DnsRawRecord) record;
                System.out.println(NetUtil.bytesToIpAddress(ByteBufUtil.getBytes(raw.content())));
            }
        }
    }

    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
        try {
            final SslContext sslContext = SslContextBuilder.forClient()
                    .protocols("TLSv1.3", "TLSv1.2")
                    .build();

            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(sslContext.newHandler(ch.alloc(), DNS_SERVER_HOST, DNS_SERVER_PORT))
                                    .addLast(new TcpDnsQueryEncoder())
                                    .addLast(new TcpDnsResponseDecoder())
                                    .addLast(new SimpleChannelInboundHandler<DefaultDnsResponse>() {
                                        @Override
                                        protected void channelRead0(ChannelHandlerContext ctx, DefaultDnsResponse msg) {
                                            try {
                                                handleQueryResp(msg);
                                            } finally {
                                                ctx.close();
                                            }
                                        }
                                    });
                        }
                    });
            final Channel ch = b.connect(DNS_SERVER_HOST, DNS_SERVER_PORT).sync().channel();

            int randomID = new Random().nextInt(60000 - 1000) + 1000;
            DnsQuery query = new DefaultDnsQuery(randomID, DnsOpCode.QUERY)
                    .setRecord(DnsSection.QUESTION, new DefaultDnsQuestion(QUERY_DOMAIN, DnsRecordType.A));
            ch.writeAndFlush(query).sync();
            boolean success = ch.closeFuture().await(10, TimeUnit.SECONDS);
            if (!success) {
                System.err.println("dns query timeout!");
                ch.close().sync();
            }
        } finally {
            group.shutdownGracefully();
        }
    }
}

Frequently Asked Questions

What is the DoTClient class?
DoTClient is a class in the netty codebase, defined in example/src/main/java/io/netty/example/dns/dot/DoTClient.java.
Where is DoTClient defined?
DoTClient is defined in example/src/main/java/io/netty/example/dns/dot/DoTClient.java at line 51.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free