Search in sources :

Example 6 with DefaultDnsRawRecord

use of io.netty.handler.codec.dns.DefaultDnsRawRecord in project graylog2-server by Graylog2.

the class DnsClient method txtLookup.

public List<TxtDnsAnswer> txtLookup(String hostName) throws InterruptedException, ExecutionException {
    if (isShutdown()) {
        throw new DnsClientNotRunningException();
    }
    LOG.debug("Attempting to perform TXT lookup for hostname [{}]", hostName);
    validateHostName(hostName);
    DnsResponse content = null;
    try {
        content = resolver.query(new DefaultDnsQuestion(hostName, DnsRecordType.TXT)).get(requestTimeout, TimeUnit.MILLISECONDS).content();
        int count = content.count(DnsSection.ANSWER);
        final ArrayList<TxtDnsAnswer> txtRecords = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            final DnsRecord dnsRecord = content.recordAt(DnsSection.ANSWER, i);
            LOG.trace("TXT record [{}] retrieved with content [{}].", i, dnsRecord);
            if (dnsRecord instanceof DefaultDnsRawRecord) {
                final DefaultDnsRawRecord txtRecord = (DefaultDnsRawRecord) dnsRecord;
                final TxtDnsAnswer.Builder dnsAnswerBuilder = TxtDnsAnswer.builder();
                final String decodeTxtRecord = decodeTxtRecord(txtRecord);
                LOG.trace("The decoded TXT record is [{}]", decodeTxtRecord);
                dnsAnswerBuilder.value(decodeTxtRecord).dnsTTL(txtRecord.timeToLive()).build();
                txtRecords.add(dnsAnswerBuilder.build());
            }
        }
        return txtRecords;
    } catch (TimeoutException e) {
        throw new ExecutionException("Resolver future didn't return a result in " + requestTimeout + " ms", e);
    } finally {
        if (content != null) {
            // Must manually release references on content object since the DnsResponse class extends ReferenceCounted
            content.release();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DefaultDnsRawRecord(io.netty.handler.codec.dns.DefaultDnsRawRecord) DnsResponse(io.netty.handler.codec.dns.DnsResponse) DnsRecord(io.netty.handler.codec.dns.DnsRecord) ExecutionException(java.util.concurrent.ExecutionException) DefaultDnsQuestion(io.netty.handler.codec.dns.DefaultDnsQuestion) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

DefaultDnsRawRecord (io.netty.handler.codec.dns.DefaultDnsRawRecord)6 ByteBuf (io.netty.buffer.ByteBuf)4 DnsRecord (io.netty.handler.codec.dns.DnsRecord)4 ArrayList (java.util.ArrayList)3 DefaultDnsQuestion (io.netty.handler.codec.dns.DefaultDnsQuestion)2 Inet4Address (java.net.Inet4Address)2 InetAddress (java.net.InetAddress)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 TransportAddress (org.elasticsearch.common.transport.TransportAddress)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 DefaultDnsQuery (io.netty.handler.codec.dns.DefaultDnsQuery)1 DefaultDnsResponse (io.netty.handler.codec.dns.DefaultDnsResponse)1