Extract IP address from NS record using Netty

14 Views Asked by At

Trying to extract IP address from A and NS domain records using non-blocking Netty DnsNameResolver as below:

final EventLoopGroup group = new NioEventLoopGroup(1);
final DnsNameResolver resolver = new DnsNameResolverBuilder(group.next())
                .channelType(NioDatagramChannel.class)
                .queryTimeoutMillis(1000)
                .authoritativeDnsServerCache(NoopAuthoritativeDnsServerCache.INSTANCE)
                .cnameCache(NoopDnsCnameCache.INSTANCE)
                .resolveCache(NoopDnsCache.INSTANCE)
                .build();

var nameServer = new InetSocketAddress("194.0.130.34", 53);
var question = new DefaultDnsQuestion("google.com", DnsRecordType.ANY);
var dnsResponse = resolver
                .query(nameServer, question)
                .get()
                .content();
System.out.println("RESPONSE: \n" + dnsResponse);

DnsRawRecord record = dnsResponse.recordAt(DnsSection.ANSWER, 0);
if(record.type() == DnsRecordType.A) {
     // Managed to extract IP address from A record
     System.out.println("A RECORD: " + record.name() + "IP: \n" + NetUtil.bytesToIpAddress(ByteBufUtil.getBytes(record.content()))); 
}

DnsRawRecord record2 = dnsResponse.recordAt(DnsSection.ANSWER, 2);
if(record2.type() == DnsRecordType.NS) {
     // Cannot understand how to extract IP address from NS record
     // System.out.println("NS RECORD: " + record2.name() + "IP: \n" + NetUtil.bytesToIpAddress(ByteBufUtil.getBytes(record2.content())));
}

Cannot figure out how to extract NS record IP address.

0

There are 0 best solutions below