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);
}
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);
}
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();
}
}
}
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();
}
}
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())));
}
}
}
Aggregations