Home | History | Annotate | Line # | Download | only in ans7
      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 
     14 use strict;
     15 use warnings;
     16 
     17 use IO::File;
     18 use Getopt::Long;
     19 use Net::DNS::Nameserver;
     20 
     21 my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
     22 print $pidf "$$\n" or die "cannot write pid file: $!";
     23 $pidf->close or die "cannot close pid file: $!";
     24 sub rmpid { unlink "ans.pid"; exit 1; };
     25 sub term { };
     26 
     27 $SIG{INT} = \&rmpid;
     28 if ($Net::DNS::VERSION >= 1.42) {
     29     $SIG{TERM} = \&term;
     30 } else {
     31     $SIG{TERM} = \&rmpid;
     32 }
     33 
     34 my $count = 0;
     35 
     36 my $localaddr = "10.53.0.7";
     37 my $localport = int($ENV{'PORT'});
     38 if (!$localport) { $localport = 5300; }
     39 my $verbose = 0;
     40 
     41 sub 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 
     69 GetOptions(
     70     'port=i' => \$localport,
     71     'verbose!' => \$verbose,
     72 );
     73 
     74 my $ns = Net::DNS::Nameserver->new(
     75     LocalAddr => $localaddr,
     76     LocalPort => $localport,
     77     ReplyHandler => \&reply_handler,
     78     Verbose => $verbose,
     79 );
     80 
     81 if ($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