/[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 152 by dpavlin, Wed Aug 5 19:49:19 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    
10    use server;
11    our $debug = server::debug;
12    
13    my $res = Net::DNS::Resolver->new(
14    #       nameserver => [ '10.60.0.1' ],
15            recurse => 1,
16            debug => $debug,
17    );
18    
19    our $ptr_cache;
20    sub name_ip {
21            my ( $name, $ip ) = @_;
22            $ptr_cache->{ join('.', reverse split(/\./, $ip)) } = $name;
23            return $ip;
24    }
25    
26  sub reply_handler {  sub reply_handler {
27          my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;          my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_;
28          my ($rcode, @ans, @auth, @add);          my ($rcode, @ans, @auth, @add);
29    
30          print "Received query from $peerhost to ". $conn->{"sockhost"}. "\n";          server->refresh;
31          $query->print;          $debug = server::debug;
32    
33            print "$qname $qclass $qtype $peerhost to ". $conn->{"sockhost"}. "\n";
34            $query->print if $debug;
35    
36                    my $local = $1     if $qname =~ m{^(.+)\.\Q$server::domain_name\E$};
37          if ($qtype eq "A" && $qname eq "foo.example.com" ) {             $local = $qname if $qname !~ m{\.};
38                  my ($ttl, $rdata) = (3600, "10.1.2.3");  
39                  push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");          my $ttl = 3600;
40    
41            if ( $local ) {
42                    warn "local[$local] $qname $qtype";
43                  $rcode = "NOERROR";                  $rcode = "NOERROR";
44          }elsif( $qname eq "foo.example.com" ) {                  my $rdata;
45                    if ( $qtype eq "A" && $local eq "server" ) {
46                            $rdata = name_ip( $local, $server::ip );
47                    } else {
48                            $rcode = "NXDOMAIN";
49                    }
50    
51                    push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata") if $ttl;
52    
53            } elsif ( $qtype eq 'PTR' && $qname =~ m{^([0-9\.]*)\.in-addr\.arpa$} ) {
54                            if ( my $rdata = $ptr_cache->{$1} ) {
55                                    $rdata .= '.' . $server::domain_name;
56                                    push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata");
57                            } else {
58    warn "## ",dump( $ptr_cache );
59                                    $rcode = "NXDOMAIN";
60                            }
61            } elsif ( my $packet = $res->query( $qname, $qtype ) ) {
62    
63                    $packet->print;
64                    push @ans, $_ foreach $packet->answer;
65                  $rcode = "NOERROR";                  $rcode = "NOERROR";
66    
67          }else{          } else {
68                   $rcode = "NXDOMAIN";                  # not found
69                    $rcode = "NXDOMAIN";
70          }          }
71    
72            warn "rcode: $rcode ",dump( @ans );
73    
74          # mark the answer as authoritive (by setting the 'aa' flag          # mark the answer as authoritive (by setting the 'aa' flag
75          return ($rcode, \@ans, \@auth, \@add, { aa => 1 });          return ($rcode, \@ans, \@auth, \@add, { aa => 1 });
76  }  }
# Line 33  sub start { Line 79  sub start {
79          my $ns = Net::DNS::Nameserver->new(          my $ns = Net::DNS::Nameserver->new(
80                  LocalPort    => 53,                  LocalPort    => 53,
81                  ReplyHandler => \&reply_handler,                  ReplyHandler => \&reply_handler,
82                  Verbose      => 1,                  Verbose      => $debug,
83          ) || die "couldn't create nameserver object\n";          ) || die "couldn't create nameserver object\n";
84    
85          warn dump( $ns );          warn "DNS $server::domain_name";
86    
87          $ns->main_loop;          $ns->main_loop;
88  }  }
89    
90  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26