summaryrefslogtreecommitdiffstats
path: root/bin/tests/system/reclimit/ans2/ans.pl
blob: 988445d6cfcbcef7e98d9911f8bad9c71eba2c60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env perl
#
# Copyright (C) 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/.
#
# See the COPYRIGHT file distributed with this work for additional
# information regarding copyright ownership.

use strict;
use warnings;

use IO::File;
use IO::Socket;
use Net::DNS;

my $localaddr = "10.53.0.2";
my $limit = getlimit();
my $no_more_waiting = 0;
my @delayed_response;
my $timeout;

my $localport = int($ENV{'PORT'});
if (!$localport) { $localport = 5300; }

my $udpsock = IO::Socket::INET->new(LocalAddr => "$localaddr",
   LocalPort => $localport, Proto => "udp", Reuse => 1) or die "$!";

my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
print $pidf "$$\n" or die "cannot write pid file: $!";
$pidf->close or die "cannot close pid file: $!";
sub rmpid { unlink "ans.pid"; exit 1; };

$SIG{INT} = \&rmpid;
$SIG{TERM} = \&rmpid;

my $count = 0;
my $send_response = 0;

sub getlimit {
    if ( -e "ans.limit") {
	open(FH, "<", "ans.limit");
	my $line = <FH>;
	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 {
    my ($qname, $qclass, $qtype) = @_;
    my ($rcode, @ans, @auth, @add, $wait);

    print ("request: $qname/$qtype\n");
    STDOUT->flush();

    $wait = 0;
    $count += 1;

    if ($qname eq "count" ) {
	if ($qtype eq "TXT") {
	    my ($ttl, $rdata) = (0, "$count");
	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
	    push @ans, $rr;
	    print ("\tcount: $count\n");
	}
	$rcode = "NOERROR";
    } elsif ($qname eq "reset" ) {
	$count = 0;
	$send_response = 0;
	$limit = getlimit();
	$rcode = "NOERROR";
	print ("\tlimit: $limit\n");
    } elsif ($qname eq "direct.example.org" ) {
	if ($qtype eq "A") {
	    my ($ttl, $rdata) = (3600, $localaddr);
	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
	    push @ans, $rr;
	}
	$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) {
	    my $rr = new Net::DNS::RR("$qname 86400 $qclass NS ns1.1.example.org");
	    push @auth, $rr;
	} elsif ($qtype eq "A") {
	    my ($ttl, $rdata) = (3600, $localaddr);
	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
	    push @ans, $rr;
	} 
	$rcode = "NOERROR";
    } elsif ($qname =~ /^ns1\.(\d+)\.example\.org$/) {
	my $next = $1 + 1;
	$wait = 1;
	if ($limit == 0 || (! $send_response && $next <= $limit)) {
	    my $rr = new Net::DNS::RR("$1.example.org 86400 $qclass NS ns1.$next.example.org");
	    push @auth, $rr;
	} else {
	    $send_response = 1;
	    if ($qtype eq "A") {
		my ($ttl, $rdata) = (3600, $localaddr);
		my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
		print("\tresponse: $qname $ttl $qclass $qtype $rdata\n");
		push @ans, $rr;
	    }
	}
	$rcode = "NOERROR";
    } elsif ($qname eq "direct.example.net" ) {
        if ($qtype eq "A") {
            my ($ttl, $rdata) = (3600, $localaddr);
            my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
            push @ans, $rr;
        }
        $rcode = "NOERROR";
    } elsif( $qname =~ /^ns1\.(\d+)\.example\.net$/ ) {
        my $next = ($1 + 1) * 16;
        for (my $i = 1; $i < 16; $i++) {
            my $s = $next + $i;
            my $rr = new Net::DNS::RR("$1.example.net 86400 $qclass NS ns1.$s.example.net");
            push @auth, $rr;
            $rr = new Net::DNS::RR("ns1.$s.example.net 86400 $qclass A 10.53.0.7");
            push @add, $rr;
        }
        $rcode = "NOERROR";
    } else {
	$rcode = "NXDOMAIN";
    }

    return ($rcode, \@ans, \@auth, \@add, $wait);
}

sub handleUDP {
	my ($buf, $peer) = @_;
	my ($request, $rcode, $ans, $auth, $add, $wait);

	$request = new Net::DNS::Packet(\$buf, 0);
	$@ and die $@;

	my ($question) = $request->question;
	my $qname = $question->qname;
	my $qclass = $question->qclass;
	my $qtype = $question->qtype;

	($rcode, $ans, $auth, $add, $wait) = reply_handler($qname, $qclass, $qtype);

	my $reply = $request->reply();

	$reply->header->rcode($rcode);
	$reply->header->aa(@$ans ? 1 : 0);
	$reply->header->id($request->header->id);
	$reply->{answer} = $ans if $ans;
	$reply->{authority} = $auth if $auth;
	$reply->{additional} = $add if $add;

	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.
			send_delayed_response();
			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.
			@delayed_response = ($reply, $peer);
			$timeout = 0.5;
			return;
		}
	} else {
		# Send reply immediately.
		return $reply;
	}
}

sub send_delayed_response {
	my ($reply, $peer) = @delayed_response;
	# Truncation to 512 bytes is required for triggering "NS explosion" on
	# builds without IPv6 support
	$udpsock->send($reply->data(512), 0, $peer);
	undef @delayed_response;
	undef $timeout;
}

# Main
my $rin;
my $rout;
for (;;) {
	$rin = '';
	vec($rin, fileno($udpsock), 1) = 1;

	select($rout = $rin, undef, undef, $timeout);

	if (vec($rout, fileno($udpsock), 1)) {
		my ($buf, $peer, $reply);
		$udpsock->recv($buf, 512);
		$peer = $udpsock->peername();
		$reply = handleUDP($buf, $peer);
		# Truncation to 512 bytes is required for triggering "NS
		# explosion" on builds without IPv6 support
		$udpsock->send($reply->data(512), 0, $peer) if $reply;
	} 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.
		send_delayed_response();
	}
}