private static ADnsAnswer decodeDnsRecord ( DnsRecord dnsRecord , boolean includeIpVersion ) { if ( dnsRecord == null ) { return null ; } LOG . trace ( "Attempting to decode DNS record [{}]" , dnsRecord ) ; /* Read data from DNS record response. The data is a binary representation of the IP address * IPv4 address: 32 bits, IPv6 address: 128 bits */ byte [ ] ipAddressBytes ; final DefaultDnsRawRecord dnsRawRecord = ( DefaultDnsRawRecord ) dnsRecord ; try { final ByteBuf byteBuf = dnsRawRecord . content ( ) ; ipAddressBytes = new byte [ byteBuf . readableBytes ( ) ] ; int readerIndex = byteBuf . readerIndex ( ) ; byteBuf . getBytes ( readerIndex , ipAddressBytes ) ; } finally { /* Must manually release references on dnsRawRecord object since the DefaultDnsRawRecord class * extends ReferenceCounted. This also releases the above ByteBuf, since DefaultDnsRawRecord is * the holder for it. */ dnsRawRecord . release ( ) ; } LOG . trace ( "The IP address has [{}] bytes" , ipAddressBytes . length ) ; InetAddress ipAddress ; try { ipAddress = InetAddress . getByAddress ( ipAddressBytes ) ; // Takes care of correctly creating an IPv4 or IPv6 address. } catch ( UnknownHostException e ) { // This should not happen. LOG . error ( "Could not extract IP address from DNS entry [{}]. Cause [{}]" , dnsRecord . toString ( ) , ExceptionUtils . getRootCauseMessage ( e ) ) ; return null ; } LOG . trace ( "The resulting IP address is [{}]" , ipAddress . getHostAddress ( ) ) ; final ADnsAnswer . Builder builder = ADnsAnswer . builder ( ) . ipAddress ( ipAddress . getHostAddress ( ) ) . dnsTTL ( dnsRecord . timeToLive ( ) ) ; if ( includeIpVersion ) { builder . ipVersion ( ipAddress instanceof Inet4Address ? IP_4_VERSION : IP_6_VERSION ) ; } return builder . build ( ) ; }
Picks out the IP address and TTL from the answer response for each record .