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 IO::Socket;
17use Net::DNS;
18
19my $localaddr = "10.53.0.2";
20my $limit = getlimit();
21my $no_more_waiting = 0;
22my @delayed_response;
23my $timeout;
24
25my $localport = int($ENV{'PORT'});
26if (!$localport) { $localport = 5300; }
27
28my $udpsock = IO::Socket::INET->new(LocalAddr => "$localaddr",
29   LocalPort => $localport, Proto => "udp", Reuse => 1) or die "$!";
30
31my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
32print $pidf "$$\n" or die "cannot write pid file: $!";
33$pidf->close or die "cannot close pid file: $!";
34sub rmpid { unlink "ans.pid"; exit 1; };
35
36$SIG{INT} = \&rmpid;
37$SIG{TERM} = \&rmpid;
38
39my $count = 0;
40my $send_response = 0;
41
42sub getlimit {
43    if ( -e "ans.limit") {
44	open(FH, "<", "ans.limit");
45	my $line = <FH>;
46	chomp $line;
47	close FH;
48	if ($line =~ /^\d+$/) {
49	    return $line;
50	}
51    }
52
53    return 0;
54}
55
56# If $wait == 0 is returned, returned reply will be sent immediately.
57# If $wait == 1 is returned, sending the returned reply might be delayed; see
58# comments inside handle_UDP() for details.
59sub reply_handler {
60    my ($qname, $qclass, $qtype) = @_;
61    my ($rcode, @ans, @auth, @add, $wait);
62
63    print ("request: $qname/$qtype\n");
64    STDOUT->flush();
65
66    $wait = 0;
67    $count += 1;
68
69    if ($qname eq "count" ) {
70	if ($qtype eq "TXT") {
71	    my ($ttl, $rdata) = (0, "$count");
72	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
73	    push @ans, $rr;
74	    print ("\tcount: $count\n");
75	}
76	$rcode = "NOERROR";
77    } elsif ($qname eq "reset" ) {
78	$count = 0;
79	$send_response = 0;
80	$limit = getlimit();
81	$rcode = "NOERROR";
82	print ("\tlimit: $limit\n");
83    } elsif ($qname eq "direct.example.org" ) {
84	if ($qtype eq "A") {
85	    my ($ttl, $rdata) = (3600, $localaddr);
86	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
87	    push @ans, $rr;
88	}
89	$rcode = "NOERROR";
90    } elsif ($qname eq "indirect1.example.org" ||
91	     $qname eq "indirect2.example.org" ||
92	     $qname eq "indirect3.example.org" ||
93	     $qname eq "indirect4.example.org" ||
94	     $qname eq "indirect5.example.org" ||
95	     $qname eq "indirect6.example.org" ||
96	     $qname eq "indirect7.example.org" ||
97	     $qname eq "indirect8.example.org") {
98	if (! $send_response) {
99	    my $rr = new Net::DNS::RR("$qname 86400 $qclass NS ns1.1.example.org");
100	    push @auth, $rr;
101	} elsif ($qtype eq "A") {
102	    my ($ttl, $rdata) = (3600, $localaddr);
103	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
104	    push @ans, $rr;
105	}
106	$rcode = "NOERROR";
107    } elsif ($qname =~ /^ns1\.(\d+)\.example\.org$/) {
108	my $next = $1 + 1;
109	$wait = 1;
110	if ($limit == 0 || (! $send_response && $next <= $limit)) {
111	    my $rr = new Net::DNS::RR("$1.example.org 86400 $qclass NS ns1.$next.example.org");
112	    push @auth, $rr;
113	} else {
114	    $send_response = 1;
115	    if ($qtype eq "A") {
116		my ($ttl, $rdata) = (3600, "10.53.0.4");
117		my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
118		print("\tresponse: $qname $ttl $qclass $qtype $rdata\n");
119		push @ans, $rr;
120	    }
121	}
122	$rcode = "NOERROR";
123    } elsif ($qname eq "direct.example.net" ) {
124	if ($qtype eq "A") {
125	    my ($ttl, $rdata) = (3600, $localaddr);
126	    my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata");
127	    push @ans, $rr;
128	}
129	$rcode = "NOERROR";
130    } elsif( $qname =~ /^ns1\.(\d+)\.example\.net$/ ) {
131	my $next = ($1 + 1) * 16;
132	for (my $i = 1; $i < 16; $i++) {
133	    my $s = $next + $i;
134	    my $rr = new Net::DNS::RR("$1.example.net 86400 $qclass NS ns1.$s.example.net");
135	    push @auth, $rr;
136	    $rr = new Net::DNS::RR("ns1.$s.example.net 86400 $qclass A 10.53.0.7");
137	    push @add, $rr;
138	}
139	$rcode = "NOERROR";
140    } else {
141	$rcode = "NXDOMAIN";
142    }
143
144    return ($rcode, \@ans, \@auth, \@add, $wait);
145}
146
147sub handleUDP {
148	my ($buf, $peer) = @_;
149	my ($request, $rcode, $ans, $auth, $add, $wait);
150
151	$request = new Net::DNS::Packet(\$buf, 0);
152	$@ and die $@;
153
154	my ($question) = $request->question;
155	my $qname = $question->qname;
156	my $qclass = $question->qclass;
157	my $qtype = $question->qtype;
158
159	($rcode, $ans, $auth, $add, $wait) = reply_handler($qname, $qclass, $qtype);
160
161	my $reply = $request->reply();
162
163	$reply->header->rcode($rcode);
164	$reply->header->aa(@$ans ? 1 : 0);
165	$reply->header->id($request->header->id);
166	$reply->{answer} = $ans if $ans;
167	$reply->{authority} = $auth if $auth;
168	$reply->{additional} = $add if $add;
169
170	if ($wait) {
171		# reply_handler() asked us to delay sending this reply until
172		# another reply with $wait == 1 is generated or a timeout
173		# occurs.
174		if (@delayed_response) {
175			# A delayed reply is already queued, so we can now send
176			# both the delayed reply and the current reply.
177			send_delayed_response();
178			return $reply;
179		} elsif ($no_more_waiting) {
180			# It was determined before that there is no point in
181			# waiting for "accompanying" queries.  Thus, send the
182			# current reply immediately.
183			return $reply;
184		} else {
185			# No delayed reply is queued and the client is expected
186			# to send an "accompanying" query shortly.  Do not send
187			# the current reply right now, just save it for later
188			# and wait for an "accompanying" query to be received.
189			@delayed_response = ($reply, $peer);
190			$timeout = 0.5;
191			return;
192		}
193	} else {
194		# Send reply immediately.
195		return $reply;
196	}
197}
198
199sub send_delayed_response {
200	my ($reply, $peer) = @delayed_response;
201	# Truncation to 512 bytes is required for triggering "NS explosion" on
202	# builds without IPv6 support
203	$udpsock->send($reply->data(512), 0, $peer);
204	undef @delayed_response;
205	undef $timeout;
206}
207
208# Main
209my $rin;
210my $rout;
211for (;;) {
212	$rin = '';
213	vec($rin, fileno($udpsock), 1) = 1;
214
215	select($rout = $rin, undef, undef, $timeout);
216
217	if (vec($rout, fileno($udpsock), 1)) {
218		my ($buf, $peer, $reply);
219		$udpsock->recv($buf, 512);
220		$peer = $udpsock->peername();
221		$reply = handleUDP($buf, $peer);
222		# Truncation to 512 bytes is required for triggering "NS
223		# explosion" on builds without IPv6 support
224		$udpsock->send($reply->data(512), 0, $peer) if $reply;
225	} else {
226		# An "accompanying" query was expected to come in, but did not.
227		# Assume the client never sends "accompanying" queries to
228		# prevent pointlessly waiting for them ever again.
229		$no_more_waiting = 1;
230		# Send the delayed reply to the query which caused us to wait.
231		send_delayed_response();
232	}
233}
234