Search in sources :

Example 1 with TcpDnsResponseDecoder

use of io.netty.handler.codec.dns.TcpDnsResponseDecoder in project netty by netty.

the class TcpDnsClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        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(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();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TcpDnsQueryEncoder(io.netty.handler.codec.dns.TcpDnsQueryEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline) DefaultDnsResponse(io.netty.handler.codec.dns.DefaultDnsResponse) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Random(java.util.Random) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) Bootstrap(io.netty.bootstrap.Bootstrap) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) DnsQuery(io.netty.handler.codec.dns.DnsQuery) TcpDnsResponseDecoder(io.netty.handler.codec.dns.TcpDnsResponseDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Example 2 with TcpDnsResponseDecoder

use of io.netty.handler.codec.dns.TcpDnsResponseDecoder in project netty by netty.

the class DoTClient method main.

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    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();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TcpDnsQueryEncoder(io.netty.handler.codec.dns.TcpDnsQueryEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline) DefaultDnsResponse(io.netty.handler.codec.dns.DefaultDnsResponse) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Random(java.util.Random) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) Bootstrap(io.netty.bootstrap.Bootstrap) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) DnsQuery(io.netty.handler.codec.dns.DnsQuery) TcpDnsResponseDecoder(io.netty.handler.codec.dns.TcpDnsResponseDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Example 3 with TcpDnsResponseDecoder

use of io.netty.handler.codec.dns.TcpDnsResponseDecoder in project netty by netty.

the class TcpDnsServer method clientQuery.

// copy from TcpDnsClient.java
private static void clientQuery() throws Exception {
    NioEventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) {
                ch.pipeline().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();
    }
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TcpDnsQueryEncoder(io.netty.handler.codec.dns.TcpDnsQueryEncoder) DefaultDnsResponse(io.netty.handler.codec.dns.DefaultDnsResponse) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Random(java.util.Random) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) DnsQuery(io.netty.handler.codec.dns.DnsQuery) TcpDnsResponseDecoder(io.netty.handler.codec.dns.TcpDnsResponseDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)3 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)3 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 DefaultDnsQuery (io.netty.handler.codec.dns.DefaultDnsQuery)3 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)3 DefaultDnsResponse (io.netty.handler.codec.dns.DefaultDnsResponse)3 DnsQuery (io.netty.handler.codec.dns.DnsQuery)3 TcpDnsQueryEncoder (io.netty.handler.codec.dns.TcpDnsQueryEncoder)3 TcpDnsResponseDecoder (io.netty.handler.codec.dns.TcpDnsResponseDecoder)3 Random (java.util.Random)3 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 SslContext (io.netty.handler.ssl.SslContext)1