1 |
package dnsd; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use Net::DNS::Nameserver; |
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
sub reply_handler { |
10 |
my ($qname, $qclass, $qtype, $peerhost,$query,$conn) = @_; |
11 |
my ($rcode, @ans, @auth, @add); |
12 |
|
13 |
print "Received query from $peerhost to ". $conn->{"sockhost"}. "\n"; |
14 |
$query->print; |
15 |
|
16 |
|
17 |
if ($qtype eq "A" && $qname eq "foo.example.com" ) { |
18 |
my ($ttl, $rdata) = (3600, "10.1.2.3"); |
19 |
push @ans, Net::DNS::RR->new("$qname $ttl $qclass $qtype $rdata"); |
20 |
$rcode = "NOERROR"; |
21 |
}elsif( $qname eq "foo.example.com" ) { |
22 |
$rcode = "NOERROR"; |
23 |
|
24 |
}else{ |
25 |
$rcode = "NXDOMAIN"; |
26 |
} |
27 |
|
28 |
# mark the answer as authoritive (by setting the 'aa' flag |
29 |
return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); |
30 |
} |
31 |
|
32 |
sub start { |
33 |
my $ns = Net::DNS::Nameserver->new( |
34 |
LocalPort => 53, |
35 |
ReplyHandler => \&reply_handler, |
36 |
Verbose => 1, |
37 |
) || die "couldn't create nameserver object\n"; |
38 |
|
39 |
warn dump( $ns ); |
40 |
|
41 |
$ns->main_loop; |
42 |
} |
43 |
|
44 |
1; |