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