Search in sources :

Example 1 with DnsQuestion

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

the class DnsQueryContext method query.

void query() {
    final DnsQuestion question = question();
    final InetSocketAddress nameServerAddr = nameServerAddr();
    final DatagramDnsQuery query = new DatagramDnsQuery(null, nameServerAddr, id);
    query.setRecursionDesired(recursionDesired);
    query.addRecord(DnsSection.QUESTION, question);
    for (DnsRecord record : additionals) {
        query.addRecord(DnsSection.ADDITIONAL, record);
    }
    if (optResource != null) {
        query.addRecord(DnsSection.ADDITIONAL, optResource);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{} WRITE: [{}: {}], {}", parent.ch, id, nameServerAddr, question);
    }
    sendQuery(query);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramDnsQuery(io.netty.handler.codec.dns.DatagramDnsQuery) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) DnsRecord(io.netty.handler.codec.dns.DnsRecord)

Example 2 with DnsQuestion

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

the class NettyNameResolver method resolve.

@Override
public boolean resolve(Event event, String query, String destination) throws ProcessorException {
    DnsQuestion dnsquery = new DefaultDnsQuestion(query, DnsRecordType.PTR);
    Element e = hostCache.get(makeKey(dnsquery));
    if (e != null) {
        DnsCacheEntry cached = (DnsCacheEntry) e.getObjectValue();
        logger.trace("Cached response: {}", cached);
        if (!e.isExpired()) {
            return store(event, cached, destination);
        } else {
            hostCache.remove(cached);
        }
    }
    Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> future = resolver.query(dnsquery);
    throw new ProcessorException.PausedEventException(event, future);
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) Element(net.sf.ehcache.Element) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Example 3 with DnsQuestion

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

the class NettyNameResolver method warmUp.

/**
 * Used by test to warm up the cache
 * @param query
 * @param type
 * @return
 * @throws Throwable
 */
DnsRecord warmUp(String query, DnsRecordType type) throws Throwable {
    AddressedEnvelope<DnsResponse, InetSocketAddress> enveloppe = null;
    try {
        DnsQuestion dnsquery = new DefaultDnsQuestion(query, type);
        Future<AddressedEnvelope<DnsResponse, InetSocketAddress>> future = resolver.query(dnsquery);
        enveloppe = future.get();
        Element cachedEntry = new Element(makeKey(dnsquery), new DnsCacheEntry(enveloppe));
        hostCache.put(cachedEntry);
        return enveloppe.content().recordAt((DnsSection.ANSWER));
    } catch (ExecutionException e) {
        throw e.getCause();
    } finally {
        if (enveloppe != null) {
            enveloppe.release();
        }
    }
}
Also used : AddressedEnvelope(io.netty.channel.AddressedEnvelope) InetSocketAddress(java.net.InetSocketAddress) DnsResponse(io.netty.handler.codec.dns.DnsResponse) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) Element(net.sf.ehcache.Element) ExecutionException(java.util.concurrent.ExecutionException) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion)

Example 4 with DnsQuestion

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

the class NettyNameResolver method process.

@Override
public boolean process(Event ev, AddressedEnvelope<DnsResponse, InetSocketAddress> enveloppe, String destination) throws ProcessorException {
    try {
        DnsResponse response = enveloppe.content();
        DnsQuestion questionRr = (DnsQuestion) response.recordAt((DnsSection.QUESTION));
        DnsRecord answerRr = enveloppe.content().recordAt((DnsSection.ANSWER));
        DnsCacheEntry cached = new DnsCacheEntry(enveloppe);
        // Default to 5s on failure, just to avoid wild loop
        // Also check than the answerRR is not null, some servers are happy to return ok on failure
        int ttl = (response.code().intValue() == NOERROR && answerRr != null) ? (int) answerRr.timeToLive() : 5;
        Element cacheElement = new Element(makeKey(questionRr), cached, ttl / 2, ttl);
        hostCache.put(cacheElement);
        return store(ev, cached, destination);
    } finally {
        enveloppe.release();
    }
}
Also used : DnsResponse(io.netty.handler.codec.dns.DnsResponse) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) DnsQuestion(io.netty.handler.codec.dns.DnsQuestion) Element(net.sf.ehcache.Element) DnsRecord(io.netty.handler.codec.dns.DnsRecord)

Example 5 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)

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