1 |
package dnsd; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Net::DNS::Nameserver; |
7 |
use Net::DNS::Resolver; |
8 |
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 |
sub reply_handler { |
20 |
my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_; |
21 |
my ($rcode, @ans, @auth, @add); |
22 |
|
23 |
server->refresh; |
24 |
|
25 |
print "Received query from $peerhost to ". $conn->{"sockhost"}. "\n"; |
26 |
$query->print; |
27 |
|
28 |
if ( $qtype eq "A" && $qname eq "pxelator" ) { |
29 |
my ($ttl, $rdata) = (3600, "172.16.10.1"); |
30 |
push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata"); |
31 |
$rcode = "NOERROR"; |
32 |
} elsif ( $qname eq "foo.example.com" ) { |
33 |
$rcode = "NOERROR"; |
34 |
|
35 |
} elsif ( my $packet = $res->query( $qname, $qtype ) ) { |
36 |
|
37 |
$packet->print; |
38 |
push @ans, $_ foreach $packet->answer; |
39 |
$rcode = "NOERROR"; |
40 |
|
41 |
} else { |
42 |
# not found |
43 |
$rcode = "NXDOMAIN"; |
44 |
} |
45 |
|
46 |
# mark the answer as authoritive (by setting the 'aa' flag |
47 |
return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); |
48 |
} |
49 |
|
50 |
sub start { |
51 |
my $ns = Net::DNS::Nameserver->new( |
52 |
LocalPort => 53, |
53 |
ReplyHandler => \&reply_handler, |
54 |
Verbose => $debug, |
55 |
) || die "couldn't create nameserver object\n"; |
56 |
|
57 |
$ns->main_loop; |
58 |
} |
59 |
|
60 |
1; |