Search in sources :

Example 1 with TcpDnsResponseEncoder

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

the class TcpDnsServer method main.

public static void main(String[] args) throws Exception {
    ServerBootstrap bootstrap = new ServerBootstrap().group(new NioEventLoopGroup(1), new NioEventLoopGroup()).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new TcpDnsQueryDecoder(), new TcpDnsResponseEncoder(), new SimpleChannelInboundHandler<DnsQuery>() {

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, DnsQuery msg) throws Exception {
                    DnsQuestion question = msg.recordAt(DnsSection.QUESTION);
                    System.out.println("Query domain: " + question);
                    // always return 192.168.1.1
                    ctx.writeAndFlush(newResponse(msg, question, 600, QUERY_RESULT));
                }

                private DefaultDnsResponse newResponse(DnsQuery query, DnsQuestion question, long ttl, byte[]... addresses) {
                    DefaultDnsResponse response = new DefaultDnsResponse(query.id());
                    response.addRecord(DnsSection.QUESTION, question);
                    for (byte[] address : addresses) {
                        DefaultDnsRawRecord queryAnswer = new DefaultDnsRawRecord(question.name(), DnsRecordType.A, ttl, Unpooled.wrappedBuffer(address));
                        response.addRecord(DnsSection.ANSWER, queryAnswer);
                    }
                    return response;
                }
            });
        }
    });
    final Channel channel = bootstrap.bind(DNS_SERVER_PORT).channel();
    Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() {

        @Override
        public void run() {
            try {
                clientQuery();
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 1000, TimeUnit.MILLISECONDS);
    channel.closeFuture().sync();
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) 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) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultDnsRawRecord(io.netty.handler.codec.dns.DefaultDnsRawRecord) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) DefaultDnsResponse(io.netty.handler.codec.dns.DefaultDnsResponse) TcpDnsQueryDecoder(io.netty.handler.codec.dns.TcpDnsQueryDecoder) DefaultDnsQuery(io.netty.handler.codec.dns.DefaultDnsQuery) DnsQuery(io.netty.handler.codec.dns.DnsQuery) TcpDnsResponseEncoder(io.netty.handler.codec.dns.TcpDnsResponseEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultDnsQuery (io.netty.handler.codec.dns.DefaultDnsQuery)1 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)1 DefaultDnsRawRecord (io.netty.handler.codec.dns.DefaultDnsRawRecord)1 DefaultDnsResponse (io.netty.handler.codec.dns.DefaultDnsResponse)1 DnsQuery (io.netty.handler.codec.dns.DnsQuery)1 DnsQuestion (io.netty.handler.codec.dns.DnsQuestion)1 TcpDnsQueryDecoder (io.netty.handler.codec.dns.TcpDnsQueryDecoder)1 TcpDnsResponseEncoder (io.netty.handler.codec.dns.TcpDnsResponseEncoder)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1