1#!/usr/bin/env perl 2# 3# Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") 4# 5# Permission to use, copy, modify, and/or distribute this software for any 6# purpose with or without fee is hereby granted, provided that the above 7# copyright notice and this permission notice appear in all copies. 8# 9# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 12# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15# PERFORMANCE OF THIS SOFTWARE. 16 17use strict; 18use warnings; 19 20use IO::File; 21use Getopt::Long; 22use Net::DNS::Nameserver; 23 24my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!"; 25print $pidf "$$\n" or die "cannot write pid file: $!"; 26$pidf->close or die "cannot close pid file: $!"; 27sub rmpid { unlink "ans.pid"; exit 1; }; 28 29$SIG{INT} = \&rmpid; 30$SIG{TERM} = \&rmpid; 31 32my $count = 0; 33my $send_response = 0; 34 35my $localaddr = "10.53.0.4"; 36my $localport = 5300; 37my $verbose = 0; 38 39sub reply_handler { 40 my ($qname, $qclass, $qtype, $peerhost, $query, $conn) = @_; 41 my ($rcode, @ans, @auth, @add); 42 43 print ("request: $qname/$qtype\n"); 44 STDOUT->flush(); 45 46 $count += 1; 47 48 if ($qname eq "count" ) { 49 if ($qtype eq "TXT") { 50 my ($ttl, $rdata) = (0, "$count"); 51 my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata"); 52 push @ans, $rr; 53 print ("\tcount: $count\n"); 54 } 55 $rcode = "NOERROR"; 56 } elsif ($qname eq "reset" ) { 57 $count = 0; 58 $send_response = 0; 59 $rcode = "NOERROR"; 60 } elsif ($qname eq "direct.example.net" ) { 61 if ($qtype eq "A") { 62 my ($ttl, $rdata) = (3600, $localaddr); 63 my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata"); 64 push @ans, $rr; 65 } 66 $rcode = "NOERROR"; 67 } elsif( $qname =~ /^ns1\.(\d+)\.example\.net$/ ) { 68 my $next = ($1 + 1) * 16; 69 for (my $i = 1; $i < 16; $i++) { 70 my $s = $next + $i; 71 my $rr = new Net::DNS::RR("$1.example.net 86400 $qclass NS ns1.$s.example.net"); 72 push @auth, $rr; 73 $rr = new Net::DNS::RR("ns1.$s.example.net 86400 $qclass A 10.53.0.7"); 74 push @add, $rr; 75 } 76 $rcode = "NOERROR"; 77 } else { 78 $rcode = "NXDOMAIN"; 79 } 80 81 # mark the answer as authoritive (by setting the 'aa' flag 82 return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); 83} 84 85GetOptions( 86 'port=i' => \$localport, 87 'verbose!' => \$verbose, 88); 89 90my $ns = Net::DNS::Nameserver->new( 91 LocalAddr => $localaddr, 92 LocalPort => $localport, 93 ReplyHandler => \&reply_handler, 94 Verbose => $verbose, 95); 96 97$ns->main_loop; 98