Search in sources :

Example 6 with DnsQuestion

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

the class DoTClient method handleQueryResp.

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())));
        }
    }
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) DnsRecord(io.netty.handler.codec.dns.DnsRecord)

Example 7 with DnsQuestion

use of io.netty.handler.codec.dns.DnsQuestion 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)

Example 8 with DnsQuestion

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

the class TcpDnsClient method handleQueryResp.

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())));
        }
    }
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) DnsRecord(io.netty.handler.codec.dns.DnsRecord)

Example 9 with DnsQuestion

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

the class DnsClient method handleQueryResp.

private static void handleQueryResp(DatagramDnsResponse 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())));
        }
    }
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) DnsRecord(io.netty.handler.codec.dns.DnsRecord)

Example 10 with DnsQuestion

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

the class DnsNameResolverContext method query.

private boolean query(String hostname, DnsRecordType type, DnsServerAddressStream nextAddr, Promise<T> promise) {
    final DnsQuestion question;
    try {
        question = new DefaultDnsQuestion(hostname, type);
    } catch (IllegalArgumentException e) {
        // java.net.IDN.toASCII(...) may throw an IllegalArgumentException if it fails to parse the hostname
        promise.tryFailure(e);
        return false;
    }
    query(nextAddr, question, promise);
    return true;
}
Also used : DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Aggregations

DnsQuestion (io.netty.handler.codec.dns.DnsQuestion)13 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)11 DnsRecord (io.netty.handler.codec.dns.DnsRecord)7 DnsRawRecord (io.netty.handler.codec.dns.DnsRawRecord)4 InetSocketAddress (java.net.InetSocketAddress)3 Element (net.sf.ehcache.Element)3 AddressedEnvelope (io.netty.channel.AddressedEnvelope)2 DefaultDnsRawRecord (io.netty.handler.codec.dns.DefaultDnsRawRecord)2 DnsQuery (io.netty.handler.codec.dns.DnsQuery)2 DnsResponse (io.netty.handler.codec.dns.DnsResponse)2 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 DatagramDnsQuery (io.netty.handler.codec.dns.DatagramDnsQuery)1 DefaultDnsQuery (io.netty.handler.codec.dns.DefaultDnsQuery)1