6 use Net::DNS::Nameserver;
7 use Net::DNS::Resolver;
8 use Data::Dump qw/dump/;
13 our $debug = server::debug;
15 my $res = Net::DNS::Resolver->new(
16 # nameserver => [ '10.60.0.1' ],
21 our ( $ptr_cache, $a_cache );
23 my ( $name, $ip ) = @_;
24 $ptr_cache->{ join('.', reverse split(/\./, $ip)) } = $name;
25 $a_cache->{$name} = $ip;
29 name_ip 'server' => $server::ip;
31 foreach my $ip ( client::all_ips ) {
32 if ( my $name = client::conf( $ip => 'hostname' ) ) {
38 my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;
39 my ($rcode, @ans, @auth, @add);
41 $debug = server::debug;
47 peerhost => $peerhost,
48 sockhost => $conn->{"sockhost"},
52 $query->print if $debug;
54 my $local = $1 if $qname =~ m{^(.+)\.\Q$server::domain\E$};
55 $local = $qname if $qname !~ m{\.};
60 warn "local[$local] $qname $qtype";
63 if ( $qtype eq "A" ) {
64 if ( $rdata = $a_cache->{$local} ) {
65 $audit->{source} = 'local';
68 warn "## no $local in ",dump( $a_cache );
70 } elsif ( $qtype eq 'PTR' ) {
71 $qname =~ s{\.in-addr\.arpa$}{} || warn "W: can't strip suffix from $qtype $qname";
72 if ( my $rdata = $ptr_cache->{$qname} ) {
73 $rdata .= '.' . $server::domain;
74 push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
75 $audit->{source} = 'PTR';
77 warn "## no $qname in ",dump( $ptr_cache );
81 $audit->{warn} = "qtype $qtype not supported";
84 push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl;
86 } elsif ( my $packet = $res->query( $qname, $qtype ) ) {
88 $audit->{source} = 'upstream';
90 push @ans, $_ foreach $packet->answer;
98 warn "rcode: $rcode ",dump( @ans );
100 $audit->{rcode} = $rcode;
101 $audit->{ans} = [ map {
103 foreach my $n ( keys %$_ ) {
104 $data->{$n} = $_->{$n};
109 store::audit( 'response', $audit );
111 # mark the answer as authoritive (by setting the 'aa' flag
112 return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
116 my $ns = Net::DNS::Nameserver->new(
118 LocalAddr => $server::ip,
119 ReplyHandler => sub {
124 ) || die "couldn't create nameserver object\n";
126 store::audit('start', { ip => $server::ip, port => 53, domain => $server::domain });
127 warn "DNS $server::domain";