/[pxelator]/lib/PXElator/dnsd.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/PXElator/dnsd.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 122 by dpavlin, Mon Aug 3 08:57:59 2009 UTC revision 211 by dpavlin, Thu Aug 13 12:00:51 2009 UTC
# Line 4  use warnings; Line 4  use warnings;
4  use strict;  use strict;
5    
6  use Net::DNS::Nameserver;  use Net::DNS::Nameserver;
7    use Net::DNS::Resolver;
8  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
9    use CouchDB;
10    
11    use server;
12    our $debug = server::debug;
13    
14    my $res = Net::DNS::Resolver->new(
15    #       nameserver => [ '10.60.0.1' ],
16            recurse => 1,
17            debug => $debug,
18    );
19    
20    our $ptr_cache;
21    sub name_ip {
22            my ( $name, $ip ) = @_;
23            $ptr_cache->{ join('.', reverse split(/\./, $ip)) } = $name;
24            return $ip;
25    }
26    
27  sub reply_handler {  sub reply_handler {
28          my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;          my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;
29          my ($rcode, @ans, @auth, @add);          my ($rcode, @ans, @auth, @add);
30    
31          print "Received query from $peerhost to ". $conn->{"sockhost"}. "\n";          server->refresh;
32          $query->print;          $debug = server::debug;
33    
34            CouchDB::audit( 'request', {
35                    qname => $qname,
36                    qclass => $qclass,
37                    qtype => $qtype,
38                    peerhost =>  $peerhost,
39                    sockhost => $conn->{"sockhost"}
40            });
41    
42            $query->print if $debug;
43    
44            my $local = $1     if $qname =~ m{^(.+)\.\Q$server::domain_name\E$};
45               $local = $qname if $qname !~ m{\.};
46    
47                    my $ttl = 3600;
48          if ($qtype eq "A" && $qname eq "foo.example.com" ) {  
49                  my ($ttl, $rdata) = (3600, "10.1.2.3");          my $audit = { source => 'unknown' };
50                  push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");  
51            if ( $local ) {
52                    warn "local[$local] $qname $qtype";
53                  $rcode = "NOERROR";                  $rcode = "NOERROR";
54          }elsif( $qname eq "foo.example.com" ) {                  my $rdata;
55                    if ( $qtype eq "A" && $local eq "server" ) {
56                            $rdata = name_ip( $local, $server::ip );
57                            $audit->{source} = 'local';
58                    } else {
59                            $rcode = "NXDOMAIN";
60                    }
61    
62                    push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl;
63    
64            } elsif ( $qtype eq 'PTR' && $qname =~ m{^([0-9\.]*)\.in-addr\.arpa$} ) {
65                            if ( my $rdata = $ptr_cache->{$1} ) {
66                                    $rdata .= '.' . $server::domain_name;
67                                    push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
68                                    $audit->{source} = 'PTR';
69                            } else {
70    warn "## ",dump( $ptr_cache );
71                                    $rcode = "NXDOMAIN";
72                            }
73            } elsif ( my $packet = $res->query( $qname, $qtype ) ) {
74    
75                    $audit->{source} = 'upstream';
76                    $packet->print;
77                    push @ans, $_ foreach $packet->answer;
78                  $rcode = "NOERROR";                  $rcode = "NOERROR";
79    
80          }else{          } else {
81                   $rcode = "NXDOMAIN";                  # not found
82                    $rcode = "NXDOMAIN";
83          }          }
84    
85            warn "rcode: $rcode ",dump( @ans );
86    
87            $audit->{rcode} = $rcode;
88            $audit->{ans} = [ @ans ];
89    
90            CouchDB::audit( 'response', $audit );
91    
92          # mark the answer as authoritive (by setting the 'aa' flag          # mark the answer as authoritive (by setting the 'aa' flag
93          return ($rcode, \@ans, \@auth, \@add, { aa => 1 });          return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
94  }  }
# Line 33  sub start { Line 97  sub start {
97          my $ns = Net::DNS::Nameserver->new(          my $ns = Net::DNS::Nameserver->new(
98                  LocalPort    => 53,                  LocalPort    => 53,
99                  ReplyHandler => \&reply_handler,                  ReplyHandler => \&reply_handler,
100                  Verbose      => 1,                  Verbose      => $debug,
101          ) || die "couldn't create nameserver object\n";          ) || die "couldn't create nameserver object\n";
102    
103          warn dump( $ns );          CouchDB::audit('start', { listen => { port => 53, domain_name => $server::domain_name } });
104            warn "DNS $server::domain_name";
105    
106          $ns->main_loop;          $ns->main_loop;
107  }  }
108    
109  1;  1;

Legend:
Removed from v.122  
changed lines
  Added in v.211

  ViewVC Help
Powered by ViewVC 1.1.26