#!/usr/bin/perl

# give insight into DNS


use strict;
use Net::DNS;
use Getopt::Std;
use Sys::SigAction qw(set_sig_handler);

my %opt;
getopts('Dh:n:r:', \%opt);
# Debug
# host to quer
# name to query
# record type

$opt{h} || die "must specify -h <host>\n";
$opt{n} || die "must specify -n <name>\n";

unless( $opt{r} =~ /^(?:A|CNAME|PTR|NS)$/ ){
	die "musy specify -r [A|CNAME|PTR|NS]\n";
}

my %matrix = ( NS    => 'nsdname',
               A     => 'address',
	       CNAME => 'cname',
	       PTR   => 'ptrdname',
             );

my $res = Net::DNS::Resolver->new(
	resolvers => $opt{h},
);

my $reply;
eval {
	my $h = set_sig_handler( 'ALRM', sub { die "timed out!\n"; } );
	alarm(2);
	$reply = $res->query($opt{n},$opt{r});
	alarm(0);
};
die "couldnt resolve: $@\n" if $@;

die "query failed: ", $res->errorstring, "\n" unless $reply;


my $string = $matrix{$opt{r}};
for my $rr (grep { $_->type eq $opt{r} } $reply->answer){
	print $rr->$string, "\n";
}


