really use ptr_cache and a_cache to answer local queries
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 13 Sep 2009 09:38:00 +0000 (09:38 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 13 Sep 2009 09:38:00 +0000 (09:38 +0000)
lib/PXElator/dnsd.pm

index 2af2a05..2afeda4 100644 (file)
@@ -9,6 +9,7 @@ use Data::Dump qw/dump/;
 use CouchDB;
 
 use server;
+use client;
 our $debug = server::debug;
 
 my $res = Net::DNS::Resolver->new(
@@ -17,13 +18,22 @@ my $res = Net::DNS::Resolver->new(
        debug => $debug,
 );
 
-our $ptr_cache;
+our ( $ptr_cache, $a_cache );
 sub name_ip {
        my ( $name, $ip ) = @_;
        $ptr_cache->{ join('.', reverse split(/\./, $ip)) } = $name;
+       $a_cache->{$name} = $ip;
        return $ip;
 }
 
+name_ip 'server' => $server::ip;
+
+foreach my $ip ( client::all_ips ) {
+       if ( my $name = client::conf( $ip => 'hostname' ) ) {
+               name_ip $name => $ip;
+       }
+}
+
 sub reply_handler {
        my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;
        my ($rcode, @ans, @auth, @add);
@@ -50,24 +60,29 @@ sub reply_handler {
                warn "local[$local] $qname $qtype";
                $rcode = "NOERROR";
                my $rdata;
-               if ( $qtype eq "A" && $local eq "server" ) {
-                       $rdata = name_ip( $local, $server::ip );
-                       $audit->{source} = 'local';
-               } else {
-                       $rcode = "NXDOMAIN";
-               }
-
-               push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl;
-
-       } elsif ( $qtype eq 'PTR' && $qname =~ m{^([0-9\.]*)\.in-addr\.arpa$} ) {
-                       if ( my $rdata = $ptr_cache->{$1} ) {
+               if ( $qtype eq "A" ) {
+                       if ( $rdata = $a_cache->{$local} ) {
+                               $audit->{source} = 'local';
+                       } else {
+                               $rcode = "NXDOMAIN";
+warn "## no $local in ",dump( $a_cache );
+                       }
+               } elsif ( $qtype eq 'PTR' ) {
+                       $qname =~ s{\.in-addr\.arpa$}{} || warn "W: can't strip suffix from $qtype $qname";
+                       if ( my $rdata = $ptr_cache->{$qname} ) {
                                $rdata .= '.' . $server::domain;
                                push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
                                $audit->{source} = 'PTR';
                        } else {
-warn "## ",dump( $ptr_cache );
+warn "## no $qname in ",dump( $ptr_cache );
                                $rcode = "NXDOMAIN";
                        }
+               } else {
+                       $audit->{warn} = "qtype $qtype not supported";
+               }
+
+               push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl;
+
        } elsif ( my $packet = $res->query( $qname, $qtype ) ) {
 
                $audit->{source} = 'upstream';