return iso path
[pxelator] / lib / PXElator / dnsd.pm
index 2af2a05..2a68c71 100644 (file)
@@ -6,9 +6,10 @@ use strict;
 use Net::DNS::Nameserver;
 use Net::DNS::Resolver;
 use Data::Dump qw/dump/;
-use CouchDB;
+use store;
 
 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';
@@ -91,7 +106,7 @@ warn "## ",dump( $ptr_cache );
                $data;
        } @ans ];
 
-       CouchDB::audit( 'response', $audit );
+       store::audit( 'response', $audit );
 
        # mark the answer as authoritive (by setting the 'aa' flag
        return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
@@ -107,7 +122,7 @@ sub start {
                Verbose      => $debug,
        ) || die "couldn't create nameserver object\n";
 
-       CouchDB::audit('start', { port => 53, domain => $server::domain });
+       store::audit('start', { port => 53, domain => $server::domain });
        warn "DNS $server::domain";
 
        $ns->main_loop;