ans.pl revision 675d004f498f4a3a3fd7433deaef230ffddbc263
#
# Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
use strict;
use warnings;
my $localaddr = "10.53.0.2";
my $no_more_waiting = 0;
my @delayed_response;
my $timeout;
$pidf->close or die "cannot close pid file: $!";
my $count = 0;
my $send_response = 0;
sub getlimit {
if ( -e "ans.limit") {
chomp $line;
close FH;
if ($line =~ /^\d+$/) {
return $line;
}
}
return 0;
}
# If $wait == 0 is returned, returned reply will be sent immediately.
# If $wait == 1 is returned, sending the returned reply might be delayed; see
# comments inside handle_UDP() for details.
sub reply_handler {
print ("request: $qname/$qtype\n");
$wait = 0;
$count += 1;
if ($qname eq "count" ) {
if ($qtype eq "TXT") {
print ("\tcount: $count\n");
}
$rcode = "NOERROR";
} elsif ($qname eq "reset" ) {
$count = 0;
$send_response = 0;
$rcode = "NOERROR";
print ("\tlimit: $limit\n");
} elsif ($qname eq "direct.example.org" ) {
if ($qtype eq "A") {
}
$rcode = "NOERROR";
} elsif ($qname eq "indirect1.example.org" ||
$qname eq "indirect2.example.org" ||
$qname eq "indirect3.example.org" ||
$qname eq "indirect4.example.org" ||
$qname eq "indirect5.example.org" ||
$qname eq "indirect6.example.org" ||
$qname eq "indirect7.example.org" ||
$qname eq "indirect8.example.org") {
if (! $send_response) {
} elsif ($qtype eq "A") {
}
$rcode = "NOERROR";
$wait = 1;
} else {
$send_response = 1;
if ($qtype eq "A") {
print("\tresponse: $qname $ttl $qclass $qtype $rdata\n");
}
}
$rcode = "NOERROR";
} elsif ($qname eq "direct.example.net" ) {
if ($qtype eq "A") {
}
$rcode = "NOERROR";
for (my $i = 1; $i < 16; $i++) {
my $s = $next + $i;
}
$rcode = "NOERROR";
} else {
$rcode = "NXDOMAIN";
}
}
sub handleUDP {
$@ and die $@;
if ($wait) {
# reply_handler() asked us to delay sending this reply until
# another reply with $wait == 1 is generated or a timeout
# occurs.
if (@delayed_response) {
# A delayed reply is already queued, so we can now send
# both the delayed reply and the current reply.
return $reply;
} elsif ($no_more_waiting) {
# It was determined before that there is no point in
# waiting for "accompanying" queries. Thus, send the
# current reply immediately.
return $reply;
} else {
# No delayed reply is queued and the client is expected
# to send an "accompanying" query shortly. Do not send
# the current reply right now, just save it for later
# and wait for an "accompanying" query to be received.
$timeout = 0.5;
return;
}
} else {
# Send reply immediately.
return $reply;
}
}
sub send_delayed_response {
# Truncation to 512 bytes is required for triggering "NS explosion" on
# builds without IPv6 support
undef @delayed_response;
undef $timeout;
}
# Main
my $rin;
my $rout;
for (;;) {
$rin = '';
# Truncation to 512 bytes is required for triggering "NS
# explosion" on builds without IPv6 support
} else {
# An "accompanying" query was expected to come in, but did not.
# Assume the client never sends "accompanying" queries to
# prevent pointlessly waiting for them ever again.
$no_more_waiting = 1;
# Send the delayed reply to the query which caused us to wait.
}
}