1#!/usr/bin/env perl 2 3# Copyright (C) Internet Systems Consortium, Inc. ("ISC") 4# 5# SPDX-License-Identifier: MPL-2.0 6# 7# This Source Code Form is subject to the terms of the Mozilla Public 8# License, v. 2.0. If a copy of the MPL was not distributed with this 9# file, you can obtain one at https://mozilla.org/MPL/2.0/. 10# 11# See the COPYRIGHT file distributed with this work for additional 12# information regarding copyright ownership. 13 14use strict; 15use warnings; 16 17use IO::File; 18use Getopt::Long; 19use Net::DNS::Nameserver; 20 21my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!"; 22print $pidf "$$\n" or die "cannot write pid file: $!"; 23$pidf->close or die "cannot close pid file: $!"; 24sub rmpid { unlink "ans.pid"; exit 1; }; 25sub term { }; 26 27$SIG{INT} = \&rmpid; 28if ($Net::DNS::VERSION >= 1.42) { 29 $SIG{TERM} = \&term; 30} else { 31 $SIG{TERM} = \&rmpid; 32} 33 34my $count = 0; 35 36my $localaddr = "10.53.0.7"; 37my $localport = int($ENV{'PORT'}); 38if (!$localport) { $localport = 5300; } 39my $verbose = 0; 40 41sub reply_handler { 42 my ($qname, $qclass, $qtype, $peerhost, $query, $conn) = @_; 43 my ($rcode, @ans, @auth, @add); 44 45 print ("request: $qname/$qtype\n"); 46 STDOUT->flush(); 47 48 $count += 1; 49 50 if ($qname eq "count" ) { 51 if ($qtype eq "TXT") { 52 my ($ttl, $rdata) = (0, "$count"); 53 my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata"); 54 push @ans, $rr; 55 print ("\tcount: $count\n"); 56 } 57 $rcode = "NOERROR"; 58 } elsif ($qname eq "reset") { 59 $count = 0; 60 $rcode = "NOERROR"; 61 } else { 62 $rcode = "REFUSED"; 63 } 64 65 # mark the answer as authoritative (by setting the 'aa' flag 66 return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); 67} 68 69GetOptions( 70 'port=i' => \$localport, 71 'verbose!' => \$verbose, 72); 73 74my $ns = Net::DNS::Nameserver->new( 75 LocalAddr => $localaddr, 76 LocalPort => $localport, 77 ReplyHandler => \&reply_handler, 78 Verbose => $verbose, 79); 80 81if ($Net::DNS::VERSION >= 1.42) { 82 $ns->start_server(); 83 select(undef, undef, undef, undef); 84 $ns->stop_server(); 85 unlink "ans.pid"; 86} else { 87 $ns->main_loop; 88} 89