| 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 IO::Socket; |
| 19 |
use Getopt::Long; |
| 20 |
use Net::DNS; |
| 21 |
use Time::HiRes qw(usleep nanosleep); |
| 22 |
|
| 23 |
my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!"; |
| 24 |
print $pidf "$$\n" or die "cannot write pid file: $!"; |
| 25 |
$pidf->close or die "cannot close pid file: $!"; |
| 26 |
sub rmpid { unlink "ans.pid"; exit 1; }; |
| 27 |
|
| 28 |
$SIG{INT} = \&rmpid; |
| 29 |
$SIG{TERM} = \&rmpid; |
| 30 |
|
| 31 |
# If send_response is set, the server will respond, otherwise the query will |
| 32 |
# be dropped. |
| 33 |
my $send_response = 1; |
| 34 |
# If slow_response is set, a lookup for the CNAME target (target.example) is |
| 35 |
# delayed. Other lookups will not be delayed. |
| 36 |
my $slow_response = 0; |
| 37 |
|
| 38 |
my $localaddr = "10.53.0.2"; |
| 39 |
|
| 40 |
my $localport = int($ENV{'PORT'}); |
| 41 |
if (!$localport) { $localport = 5300; } |
| 42 |
|
| 43 |
my $udpsock = IO::Socket::INET->new(LocalAddr => "$localaddr", |
| 44 |
LocalPort => $localport, Proto => "udp", Reuse => 1) or die "$!"; |
| 45 |
|
| 46 |
# |
| 47 |
# Delegations |
| 48 |
# |
| 49 |
my $SOA = "example 300 IN SOA . . 0 0 0 0 300"; |
| 50 |
my $NS = "example 300 IN NS ns.example"; |
| 51 |
my $A = "ns.example 300 IN A $localaddr"; |
| 52 |
my $ssSOA = "delegated.serve.stale 300 IN SOA . . 0 0 0 0 300"; |
| 53 |
my $ssNS = "delegated.serve.stale 300 IN NS ns.delegated.serve.stale"; |
| 54 |
my $ssA = "ns.delegated.serve.stale 300 IN A $localaddr"; |
| 55 |
|
| 56 |
# |
| 57 |
# Slow delegation |
| 58 |
# |
| 59 |
my $slowSOA = "slow 300 IN SOA . . 0 0 0 0 300"; |
| 60 |
my $slowNS = "slow 300 IN NS ns.slow"; |
| 61 |
my $slowA = "ns.slow 300 IN A $localaddr"; |
| 62 |
my $slowTXT = "data.slow 2 IN TXT \"A slow text record with a 2 second ttl\""; |
| 63 |
my $slownegSOA = "slow 2 IN SOA . . 0 0 0 0 300"; |
| 64 |
|
| 65 |
# |
| 66 |
# Records to be TTL stretched |
| 67 |
# |
| 68 |
my $TXT = "data.example 2 IN TXT \"A text record with a 2 second ttl\""; |
| 69 |
my $LONGTXT = "longttl.example 600 IN TXT \"A text record with a 600 second ttl\""; |
| 70 |
my $CAA = "othertype.example 2 IN CAA 0 issue \"ca1.example.net\""; |
| 71 |
my $negSOA = "example 2 IN SOA . . 0 0 0 0 300"; |
| 72 |
my $ssnegSOA = "delegated.serve.stale 2 IN SOA . . 0 0 0 0 300"; |
| 73 |
my $CNAME = "cname.example 7 IN CNAME target.example"; |
| 74 |
my $TARGET = "target.example 9 IN A $localaddr"; |
| 75 |
my $SHORTCNAME = "shortttl.cname.example 1 IN CNAME longttl.target.example"; |
| 76 |
my $LONGTARGET = "longttl.target.example 600 IN A $localaddr"; |
| 77 |
|
| 78 |
# |
| 79 |
# YWH records |
| 80 |
# |
| 81 |
my $ywhSOA = "source.stale 300 IN SOA . . 0 0 0 0 300"; |
| 82 |
my $ywhNS = "source.stale 300 IN NS ns.source.stale"; |
| 83 |
my $ywhA = "ns.source.stale 300 IN A $localaddr"; |
| 84 |
my $ywhCNAME = "alias.source.stale 2 IN CNAME www.target.stale"; |
| 85 |
my $ywhCNAMENX = "aliasnx.source.stale 2 IN CNAME nonexist.target.stale"; |
| 86 |
|
| 87 |
sub reply_handler { |
| 88 |
my ($qname, $qclass, $qtype) = @_; |
| 89 |
my ($rcode, @ans, @auth, @add); |
| 90 |
|
| 91 |
print ("request: $qname/$qtype\n"); |
| 92 |
STDOUT->flush(); |
| 93 |
|
| 94 |
# Control whether we send a response or not. |
| 95 |
# We always respond to control commands. |
| 96 |
if ($qname eq "enable" ) { |
| 97 |
if ($qtype eq "TXT") { |
| 98 |
$send_response = 1; |
| 99 |
my $rr = new Net::DNS::RR("$qname 0 $qclass TXT \"$send_response\""); |
| 100 |
push @ans, $rr; |
| 101 |
} |
| 102 |
$rcode = "NOERROR"; |
| 103 |
return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); |
| 104 |
} elsif ($qname eq "disable" ) { |
| 105 |
if ($qtype eq "TXT") { |
| 106 |
$send_response = 0; |
| 107 |
my $rr = new Net::DNS::RR("$qname 0 $qclass TXT \"$send_response\""); |
| 108 |
push @ans, $rr; |
| 109 |
} |
| 110 |
$rcode = "NOERROR"; |
| 111 |
return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); |
| 112 |
} elsif ($qname eq "slowdown" ) { |
| 113 |
if ($qtype eq "TXT") { |
| 114 |
$send_response = 1; |
| 115 |
$slow_response = 1; |
| 116 |
my $rr = new Net::DNS::RR("$qname 0 $qclass TXT \"$send_response\""); |
| 117 |
push @ans, $rr; |
| 118 |
} |
| 119 |
$rcode = "NOERROR"; |
| 120 |
return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); |
| 121 |
} |
| 122 |
|
| 123 |
# If we are not responding to queries we are done. |
| 124 |
return if (!$send_response); |
| 125 |
|
| 126 |
if (index($qname, "latency") == 0) { |
| 127 |
# simulate network latency before answering |
| 128 |
print " Sleeping 50 milliseconds\n"; |
| 129 |
select(undef, undef, undef, 0.05); |
| 130 |
} |
| 131 |
|
| 132 |
# Construct the response and send it. |
| 133 |
if ($qname eq "ns.example" ) { |
| 134 |
if ($qtype eq "A") { |
| 135 |
my $rr = new Net::DNS::RR($A); |
| 136 |
push @ans, $rr; |
| 137 |
} else { |
| 138 |
my $rr = new Net::DNS::RR($SOA); |
| 139 |
push @auth, $rr; |
| 140 |
} |
| 141 |
$rcode = "NOERROR"; |
| 142 |
} elsif ($qname eq "example") { |
| 143 |
if ($qtype eq "NS") { |
| 144 |
my $rr = new Net::DNS::RR($NS); |
| 145 |
push @auth, $rr; |
| 146 |
$rr = new Net::DNS::RR($A); |
| 147 |
push @add, $rr; |
| 148 |
} elsif ($qtype eq "SOA") { |
| 149 |
my $rr = new Net::DNS::RR($SOA); |
| 150 |
push @ans, $rr; |
| 151 |
} else { |
| 152 |
my $rr = new Net::DNS::RR($SOA); |
| 153 |
push @auth, $rr; |
| 154 |
} |
| 155 |
$rcode = "NOERROR"; |
| 156 |
} elsif ($qname eq "nodata.example") { |
| 157 |
my $rr = new Net::DNS::RR($negSOA); |
| 158 |
push @auth, $rr; |
| 159 |
$rcode = "NOERROR"; |
| 160 |
} elsif ($qname eq "data.example") { |
| 161 |
if ($qtype eq "TXT") { |
| 162 |
my $rr = new Net::DNS::RR($TXT); |
| 163 |
push @ans, $rr; |
| 164 |
} else { |
| 165 |
my $rr = new Net::DNS::RR($negSOA); |
| 166 |
push @auth, $rr; |
| 167 |
} |
| 168 |
$rcode = "NOERROR"; |
| 169 |
} elsif ($qname eq "a-only.example") { |
| 170 |
if ($qtype eq "A") { |
| 171 |
my $rr = new Net::DNS::RR("a-only.example 2 IN A $localaddr"); |
| 172 |
push @ans, $rr; |
| 173 |
} else { |
| 174 |
my $rr = new Net::DNS::RR($negSOA); |
| 175 |
push @auth, $rr; |
| 176 |
} |
| 177 |
$rcode = "NOERROR"; |
| 178 |
} elsif ($qname eq "cname.example") { |
| 179 |
if ($qtype eq "A") { |
| 180 |
my $rr = new Net::DNS::RR($CNAME); |
| 181 |
push @ans, $rr; |
| 182 |
} else { |
| 183 |
my $rr = new Net::DNS::RR($negSOA); |
| 184 |
push @auth, $rr; |
| 185 |
} |
| 186 |
$rcode = "NOERROR"; |
| 187 |
} elsif ($qname eq "target.example") { |
| 188 |
if ($slow_response) { |
| 189 |
print " Sleeping 3 seconds\n"; |
| 190 |
sleep(3); |
| 191 |
} |
| 192 |
if ($qtype eq "A") { |
| 193 |
my $rr = new Net::DNS::RR($TARGET); |
| 194 |
push @ans, $rr; |
| 195 |
} else { |
| 196 |
my $rr = new Net::DNS::RR($negSOA); |
| 197 |
push @auth, $rr; |
| 198 |
} |
| 199 |
$rcode = "NOERROR"; |
| 200 |
} elsif ($qname eq "shortttl.cname.example") { |
| 201 |
my $rr = new Net::DNS::RR($SHORTCNAME); |
| 202 |
push @ans, $rr; |
| 203 |
$rcode = "NOERROR"; |
| 204 |
} elsif ($qname eq "longttl.target.example") { |
| 205 |
if ($slow_response) { |
| 206 |
print " Sleeping 3 seconds\n"; |
| 207 |
sleep(3); |
| 208 |
} |
| 209 |
if ($qtype eq "A") { |
| 210 |
my $rr = new Net::DNS::RR($LONGTARGET); |
| 211 |
push @ans, $rr; |
| 212 |
} else { |
| 213 |
my $rr = new Net::DNS::RR($negSOA); |
| 214 |
push @auth, $rr; |
| 215 |
} |
| 216 |
$rcode = "NOERROR"; |
| 217 |
} elsif ($qname eq "longttl.example") { |
| 218 |
if ($qtype eq "TXT") { |
| 219 |
my $rr = new Net::DNS::RR($LONGTXT); |
| 220 |
push @ans, $rr; |
| 221 |
} else { |
| 222 |
my $rr = new Net::DNS::RR($negSOA); |
| 223 |
push @auth, $rr; |
| 224 |
} |
| 225 |
$rcode = "NOERROR"; |
| 226 |
} elsif ($qname eq "nxdomain.example") { |
| 227 |
my $rr = new Net::DNS::RR($negSOA); |
| 228 |
push @auth, $rr; |
| 229 |
$rcode = "NXDOMAIN"; |
| 230 |
} elsif ($qname eq "othertype.example") { |
| 231 |
if ($qtype eq "CAA") { |
| 232 |
my $rr = new Net::DNS::RR($CAA); |
| 233 |
push @ans, $rr; |
| 234 |
} else { |
| 235 |
my $rr = new Net::DNS::RR($negSOA); |
| 236 |
push @auth, $rr; |
| 237 |
} |
| 238 |
$rcode = "NOERROR"; |
| 239 |
} elsif ($qname eq "ns.delegated.serve.stale" ) { |
| 240 |
if ($qtype eq "A") { |
| 241 |
my $rr = new Net::DNS::RR($ssA); |
| 242 |
push @ans, $rr; |
| 243 |
} else { |
| 244 |
my $rr = new Net::DNS::RR($ssSOA); |
| 245 |
push @auth, $rr; |
| 246 |
} |
| 247 |
$rcode = "NOERROR"; |
| 248 |
} elsif ($qname eq "delegated.serve.stale") { |
| 249 |
if ($qtype eq "NS") { |
| 250 |
my $rr = new Net::DNS::RR($ssNS); |
| 251 |
push @auth, $rr; |
| 252 |
$rr = new Net::DNS::RR($ssA); |
| 253 |
push @add, $rr; |
| 254 |
} elsif ($qtype eq "SOA") { |
| 255 |
my $rr = new Net::DNS::RR($ssSOA); |
| 256 |
push @ans, $rr; |
| 257 |
} else { |
| 258 |
my $rr = new Net::DNS::RR($ssSOA); |
| 259 |
push @auth, $rr; |
| 260 |
} |
| 261 |
$rcode = "NOERROR"; |
| 262 |
} elsif ($qname eq "www.delegated.serve.stale") { |
| 263 |
if ($qtype eq "A") { |
| 264 |
my $rr = new Net::DNS::RR("www.delegated.serve.stale 2 IN A 10.53.0.99"); |
| 265 |
push @ans, $rr; |
| 266 |
} else { |
| 267 |
my $rr = new Net::DNS::RR($ssnegSOA); |
| 268 |
push @auth, $rr; |
| 269 |
} |
| 270 |
$rcode = "NOERROR"; |
| 271 |
} elsif ($qname eq "cname.delegated.serve.stale") { |
| 272 |
if ($qtype eq "A") { |
| 273 |
my $rr = new Net::DNS::RR("cname.delegated.serve.stale 2 IN CNAME cname-target.serve.stale."); |
| 274 |
push @ans, $rr; |
| 275 |
} else { |
| 276 |
my $rr = new Net::DNS::RR($ssnegSOA); |
| 277 |
push @auth, $rr; |
| 278 |
} |
| 279 |
$rcode = "NOERROR"; |
| 280 |
} elsif ($qname eq "ns.slow" ) { |
| 281 |
if ($qtype eq "A") { |
| 282 |
my $rr = new Net::DNS::RR($slowA); |
| 283 |
push @ans, $rr; |
| 284 |
} else { |
| 285 |
my $rr = new Net::DNS::RR($slowSOA); |
| 286 |
push @auth, $rr; |
| 287 |
} |
| 288 |
$rcode = "NOERROR"; |
| 289 |
} elsif ($qname eq "slow") { |
| 290 |
if ($qtype eq "NS") { |
| 291 |
my $rr = new Net::DNS::RR($slowNS); |
| 292 |
push @auth, $rr; |
| 293 |
$rr = new Net::DNS::RR($slowA); |
| 294 |
push @add, $rr; |
| 295 |
} elsif ($qtype eq "SOA") { |
| 296 |
my $rr = new Net::DNS::RR($slowSOA); |
| 297 |
push @ans, $rr; |
| 298 |
} else { |
| 299 |
my $rr = new Net::DNS::RR($slowSOA); |
| 300 |
push @auth, $rr; |
| 301 |
} |
| 302 |
$rcode = "NOERROR"; |
| 303 |
} elsif ($qname eq "data.slow") { |
| 304 |
if ($slow_response) { |
| 305 |
print " Sleeping 3 seconds\n"; |
| 306 |
sleep(3); |
| 307 |
# only one time |
| 308 |
$slow_response = 0; |
| 309 |
} |
| 310 |
if ($qtype eq "TXT") { |
| 311 |
my $rr = new Net::DNS::RR($slowTXT); |
| 312 |
push @ans, $rr; |
| 313 |
} else { |
| 314 |
my $rr = new Net::DNS::RR($slownegSOA); |
| 315 |
push @auth, $rr; |
| 316 |
} |
| 317 |
$rcode = "NOERROR"; |
| 318 |
} elsif ($qname eq "source.stale") { |
| 319 |
if ($qtype eq "SOA") { |
| 320 |
my $rr = new Net::DNS::RR($ywhSOA); |
| 321 |
push @ans, $rr; |
| 322 |
} elsif ($qtype eq "NS") { |
| 323 |
my $rr = new Net::DNS::RR($ywhNS); |
| 324 |
push @ans, $rr; |
| 325 |
$rr = new Net::DNS::RR($ywhA); |
| 326 |
push @add, $rr; |
| 327 |
} |
| 328 |
$rcode = "NOERROR"; |
| 329 |
} elsif ($qname eq "ns.source.stale") { |
| 330 |
if ($qtype eq "A") { |
| 331 |
my $rr = new Net::DNS::RR($ywhA); |
| 332 |
push @ans, $rr; |
| 333 |
} else { |
| 334 |
my $rr = new Net::DNS::RR($ywhSOA); |
| 335 |
push @auth, $rr; |
| 336 |
} |
| 337 |
$rcode = "NOERROR"; |
| 338 |
} elsif ($qname eq "alias.source.stale") { |
| 339 |
my $rr = new Net::DNS::RR($ywhCNAME); |
| 340 |
push @ans, $rr; |
| 341 |
$rcode = "NOERROR"; |
| 342 |
} elsif ($qname eq "aliasnx.source.stale") { |
| 343 |
my $rr = new Net::DNS::RR($ywhCNAMENX); |
| 344 |
push @ans, $rr; |
| 345 |
$rcode = "NOERROR"; |
| 346 |
} else { |
| 347 |
my $rr = new Net::DNS::RR($SOA); |
| 348 |
push @auth, $rr; |
| 349 |
$rcode = "NXDOMAIN"; |
| 350 |
} |
| 351 |
|
| 352 |
# mark the answer as authoritative (by setting the 'aa' flag) |
| 353 |
return ($rcode, \@ans, \@auth, \@add, { aa => 1 }); |
| 354 |
} |
| 355 |
|
| 356 |
GetOptions( |
| 357 |
'port=i' => \$localport, |
| 358 |
); |
| 359 |
|
| 360 |
my $rin; |
| 361 |
my $rout; |
| 362 |
|
| 363 |
for (;;) { |
| 364 |
$rin = ''; |
| 365 |
vec($rin, fileno($udpsock), 1) = 1; |
| 366 |
|
| 367 |
select($rout = $rin, undef, undef, undef); |
| 368 |
|
| 369 |
if (vec($rout, fileno($udpsock), 1)) { |
| 370 |
my ($buf, $request, $err); |
| 371 |
$udpsock->recv($buf, 512); |
| 372 |
|
| 373 |
if ($Net::DNS::VERSION > 0.68) { |
| 374 |
$request = new Net::DNS::Packet(\$buf, 0); |
| 375 |
$@ and die $@; |
| 376 |
} else { |
| 377 |
my $err; |
| 378 |
($request, $err) = new Net::DNS::Packet(\$buf, 0); |
| 379 |
$err and die $err; |
| 380 |
} |
| 381 |
|
| 382 |
my @questions = $request->question; |
| 383 |
my $qname = $questions[0]->qname; |
| 384 |
my $qclass = $questions[0]->qclass; |
| 385 |
my $qtype = $questions[0]->qtype; |
| 386 |
my $id = $request->header->id; |
| 387 |
|
| 388 |
my ($rcode, $ans, $auth, $add, $headermask) = reply_handler($qname, $qclass, $qtype); |
| 389 |
|
| 390 |
if (!defined($rcode)) { |
| 391 |
print " Silently ignoring query\n"; |
| 392 |
next; |
| 393 |
} |
| 394 |
|
| 395 |
my $reply = Net::DNS::Packet->new(); |
| 396 |
$reply->header->qr(1); |
| 397 |
$reply->header->aa(1) if $headermask->{'aa'}; |
| 398 |
$reply->header->id($id); |
| 399 |
$reply->header->rcode($rcode); |
| 400 |
$reply->push("question", @questions); |
| 401 |
$reply->push("answer", @$ans) if $ans; |
| 402 |
$reply->push("authority", @$auth) if $auth; |
| 403 |
$reply->push("additional", @$add) if $add; |
| 404 |
|
| 405 |
my $num_chars = $udpsock->send($reply->data); |
| 406 |
print " Sent $num_chars bytes via UDP\n"; |
| 407 |
} |
| 408 |
} |