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(
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);
$query->print if $debug;
- my $local = $1 if $qname =~ m{^(.+)\.\Q$server::domain_name\E$};
+ my $local = $1 if $qname =~ m{^(.+)\.\Q$server::domain\E$};
$local = $qname if $qname !~ m{\.};
my $ttl = 3600;
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} ) {
- $rdata .= '.' . $server::domain_name;
+ 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';
$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 });
Verbose => $debug,
) || die "couldn't create nameserver object\n";
- CouchDB::audit('start', { port => 53, domain_name => $server::domain_name });
- warn "DNS $server::domain_name";
+ store::audit('start', { port => 53, domain => $server::domain });
+ warn "DNS $server::domain";
$ns->main_loop;
}