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 https://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.4"; 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 = 1; 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 = 1; 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 print ("\twait=$wait ans: $qname $ttl $qclass $qtype $rdata\n"); 89 } 90 $rcode = "NOERROR"; 91 } elsif ($qname eq "indirect1.example.org" || 92 $qname eq "indirect2.example.org" || 93 $qname eq "indirect3.example.org" || 94 $qname eq "indirect4.example.org" || 95 $qname eq "indirect5.example.org" || 96 $qname eq "indirect6.example.org" || 97 $qname eq "indirect7.example.org" || 98 $qname eq "indirect8.example.org") { 99 if ($qtype eq "A") { 100 my ($ttl, $rdata) = (3600, $localaddr); 101 my $rr = new Net::DNS::RR("$qname $ttl $qclass $qtype $rdata"); 102 push @ans, $rr; 103 print ("\twait=$wait ans: $qname $ttl $qclass $qtype $rdata\n"); 104 } 105 $rcode = "NOERROR"; 106 } elsif ($qname =~ /^ns1\.(\d+)\.example\.org$/) { 107 my $next = $1 + 1; 108 $wait = 1; 109 if ($limit == 0) { 110 my $rr = new Net::DNS::RR("$1.example.org 86400 $qclass NS ns1.$next.example.org"); 111 push @auth, $rr; 112 print ("\twait=$wait auth: $1.example.org 86400 $qclass NS ns1.$next.example.org\n"); 113 } else { 114 $send_response = 1; 115 if ($qtype eq "A") { 116 my ($ttl, $rdata) = (3600, $localaddr); 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 print ("\twait=$wait ans: $qname $ttl $qclass $qtype $rdata\n"); 129 } 130 $rcode = "NOERROR"; 131 } elsif( $qname =~ /^ns1\.(\d+)\.example\.net$/ ) { 132 my $next = ($1 + 1) * 16; 133 for (my $i = 1; $i < 16; $i++) { 134 my $s = $next + $i; 135 my $rr = new Net::DNS::RR("$1.example.net 86400 $qclass NS ns1.$s.example.net"); 136 push @auth, $rr; 137 print ("\twait=$wait auth: $1.example.net 86400 $qclass NS ns1.$s.example.net\n"); 138 $rr = new Net::DNS::RR("ns1.$s.example.net 86400 $qclass A 10.53.0.7"); 139 print ("\twait=$wait add: ns1.$s.example.net 86400 $qclass A 10.53.0.7\n"); 140 push @add, $rr; 141 } 142 $rcode = "NOERROR"; 143 } else { 144 $rcode = "NXDOMAIN"; 145 print ("\twait=$wait NXDOMAIN\n"); 146 } 147 148 return ($rcode, \@ans, \@auth, \@add, $wait); 149} 150 151sub handleUDP { 152 my ($buf, $peer) = @_; 153 my ($request, $rcode, $ans, $auth, $add, $wait); 154 155 $request = new Net::DNS::Packet(\$buf, 0); 156 $@ and die $@; 157 158 my ($question) = $request->question; 159 my $qname = $question->qname; 160 my $qclass = $question->qclass; 161 my $qtype = $question->qtype; 162 163 ($rcode, $ans, $auth, $add, $wait) = reply_handler($qname, $qclass, $qtype); 164 165 my $reply = $request->reply(); 166 167 $reply->header->rcode($rcode); 168 $reply->header->aa(@$ans ? 1 : 0); 169 $reply->header->id($request->header->id); 170 $reply->{answer} = $ans if $ans; 171 $reply->{authority} = $auth if $auth; 172 $reply->{additional} = $add if $add; 173 174 if ($wait) { 175 # reply_handler() asked us to delay sending this reply until 176 # another reply with $wait == 1 is generated or a timeout 177 # occurs. 178 if (@delayed_response) { 179 # A delayed reply is already queued, so we can now send 180 # both the delayed reply and the current reply. 181 send_delayed_response(); 182 return $reply; 183 } elsif ($no_more_waiting) { 184 # It was determined before that there is no point in 185 # waiting for "accompanying" queries. Thus, send the 186 # current reply immediately. 187 return $reply; 188 } else { 189 # No delayed reply is queued and the client is expected 190 # to send an "accompanying" query shortly. Do not send 191 # the current reply right now, just save it for later 192 # and wait for an "accompanying" query to be received. 193 @delayed_response = ($reply, $peer); 194 $timeout = 0.5; 195 return; 196 } 197 } else { 198 # Send reply immediately. 199 return $reply; 200 } 201} 202 203sub send_delayed_response { 204 my ($reply, $peer) = @delayed_response; 205 # Truncation to 512 bytes is required for triggering "NS explosion" on 206 # builds without IPv6 support 207 $udpsock->send($reply->data(512), 0, $peer); 208 undef @delayed_response; 209 undef $timeout; 210 print ("send_delayed_response\n"); 211} 212 213# Main 214my $rin; 215my $rout; 216for (;;) { 217 $rin = ''; 218 vec($rin, fileno($udpsock), 1) = 1; 219 220 select($rout = $rin, undef, undef, $timeout); 221 222 if (vec($rout, fileno($udpsock), 1)) { 223 my ($buf, $peer, $reply); 224 $udpsock->recv($buf, 512); 225 $peer = $udpsock->peername(); 226 $reply = handleUDP($buf, $peer); 227 # Truncation to 512 bytes is required for triggering "NS 228 # explosion" on builds without IPv6 support 229 $udpsock->send($reply->data(512), 0, $peer) if $reply; 230 } else { 231 # An "accompanying" query was expected to come in, but did not. 232 # Assume the client never sends "accompanying" queries to 233 # prevent pointlessly waiting for them ever again. 234 $no_more_waiting = 1; 235 # Send the delayed reply to the query which caused us to wait. 236 send_delayed_response(); 237 } 238} 239