Blame bin/tests/system/resolver/ans2/ans.pl

Packit Service ae04f2
#!/usr/bin/perl
Packit Service ae04f2
#
Packit Service ae04f2
# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
#
Packit Service ae04f2
# This Source Code Form is subject to the terms of the Mozilla Public
Packit Service ae04f2
# License, v. 2.0. If a copy of the MPL was not distributed with this
Packit Service ae04f2
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit Service ae04f2
#
Packit Service ae04f2
# See the COPYRIGHT file distributed with this work for additional
Packit Service ae04f2
# information regarding copyright ownership.
Packit Service ae04f2
Packit Service ae04f2
#
Packit Service ae04f2
# Ad hoc name server
Packit Service ae04f2
#
Packit Service ae04f2
Packit Service ae04f2
use IO::File;
Packit Service ae04f2
use IO::Socket;
Packit Service ae04f2
use Net::DNS;
Packit Service ae04f2
use Net::DNS::Packet;
Packit Service ae04f2
Packit Service ae04f2
my $localport = int($ENV{'PORT'});
Packit Service ae04f2
if (!$localport) { $localport = 5300; }
Packit Service ae04f2
Packit Service ae04f2
my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.2",
Packit Service ae04f2
   LocalPort => $localport, Proto => "udp") or die "$!";
Packit Service ae04f2
Packit Service ae04f2
my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
Packit Service ae04f2
print $pidf "$$\n" or die "cannot write pid file: $!";
Packit Service ae04f2
$pidf->close or die "cannot close pid file: $!";
Packit Service ae04f2
sub rmpid { unlink "ans.pid"; exit 1; };
Packit Service ae04f2
Packit Service ae04f2
$SIG{INT} = \&rmpid;
Packit Service ae04f2
$SIG{TERM} = \&rmpid;
Packit Service ae04f2
Packit Service ae04f2
for (;;) {
Packit Service ae04f2
	$sock->recv($buf, 512);
Packit Service ae04f2
Packit Service ae04f2
	print "**** request from " , $sock->peerhost, " port ", $sock->peerport, "\n";
Packit Service ae04f2
Packit Service ae04f2
	my $packet;
Packit Service ae04f2
Packit Service ae04f2
	if ($Net::DNS::VERSION > 0.68) {
Packit Service ae04f2
		$packet = new Net::DNS::Packet(\$buf, 0);
Packit Service ae04f2
		$@ and die $@;
Packit Service ae04f2
	} else {
Packit Service ae04f2
		my $err;
Packit Service ae04f2
		($packet, $err) = new Net::DNS::Packet(\$buf, 0);
Packit Service ae04f2
		$err and die $err;
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	print "REQUEST:\n";
Packit Service ae04f2
	$packet->print;
Packit Service ae04f2
Packit Service ae04f2
	$packet->header->qr(1);
Packit Service ae04f2
Packit Service ae04f2
	my @questions = $packet->question;
Packit Service ae04f2
	my $qname = $questions[0]->qname;
Packit Service ae04f2
	my $qtype = $questions[0]->qtype;
Packit Service ae04f2
Packit Service ae04f2
	if ($qname eq "cname1.example.com") {
Packit Service ae04f2
		# Data for the "cname + other data / 1" test
Packit Service ae04f2
		$packet->push("answer", new Net::DNS::RR("cname1.example.com 300 CNAME cname1.example.com"));
Packit Service ae04f2
		$packet->push("answer", new Net::DNS::RR("cname1.example.com 300 A 1.2.3.4"));
Packit Service ae04f2
	} elsif ($qname eq "cname2.example.com") {
Packit Service ae04f2
		# Data for the "cname + other data / 2" test: same RRs in opposite order
Packit Service ae04f2
		$packet->push("answer", new Net::DNS::RR("cname2.example.com 300 A 1.2.3.4"));
Packit Service ae04f2
		$packet->push("answer", new Net::DNS::RR("cname2.example.com 300 CNAME cname2.example.com"));
Packit Service ae04f2
	} elsif ($qname eq "www.example.org" || $qname eq "www.example.net" ||
Packit Service ae04f2
		 $qname eq "badcname.example.org" ||
Packit Service ae04f2
		 $qname eq "goodcname.example.org" ||
Packit Service ae04f2
		 $qname eq "foo.baddname.example.org" ||
Packit Service ae04f2
		 $qname eq "foo.gooddname.example.org") {
Packit Service ae04f2
		# Data for address/alias filtering.
Packit Service ae04f2
		$packet->header->aa(1);
Packit Service ae04f2
		if ($qtype eq "A") {
Packit Service ae04f2
			$packet->push("answer",
Packit Service ae04f2
				      new Net::DNS::RR($qname .
Packit Service ae04f2
						       " 300 A 192.0.2.1"));
Packit Service ae04f2
		} elsif ($qtype eq "AAAA") {
Packit Service ae04f2
			$packet->push("answer",
Packit Service ae04f2
				      new Net::DNS::RR($qname .
Packit Service ae04f2
						" 300 AAAA 2001:db8:beef::1"));
Packit Service ae04f2
		}
Packit Service ae04f2
	} elsif ($qname eq "badcname.example.net" ||
Packit Service ae04f2
		 $qname eq "goodcname.example.net") {
Packit Service ae04f2
		# Data for CNAME/DNAME filtering.  We need to make one-level
Packit Service ae04f2
		# delegation to avoid automatic acceptance for subdomain aliases
Packit Service ae04f2
		$packet->push("authority", new Net::DNS::RR("example.net 300 NS ns.example.net"));
Packit Service ae04f2
		$packet->push("additional", new Net::DNS::RR("ns.example.net 300 A 10.53.0.3"));
Packit Service ae04f2
	} elsif ($qname =~ /^nodata\.example\.net$/i) {
Packit Service ae04f2
		$packet->header->aa(1);
Packit Service ae04f2
	} elsif ($qname =~ /^nxdomain\.example\.net$/i) {
Packit Service ae04f2
		$packet->header->aa(1);
Packit Service ae04f2
		$packet->header->rcode(NXDOMAIN);
Packit Service ae04f2
	} elsif ($qname =~ /sub\.example\.org/) {
Packit Service ae04f2
		# Data for CNAME/DNAME filtering.  The final answers are
Packit Service ae04f2
		# expected to be accepted regardless of the filter setting.
Packit Service ae04f2
		$packet->push("authority", new Net::DNS::RR("sub.example.org 300 NS ns.sub.example.org"));
Packit Service ae04f2
		$packet->push("additional", new Net::DNS::RR("ns.sub.example.org 300 A 10.53.0.3"));
Packit Service ae04f2
	} elsif ($qname =~ /\.broken/) {
Packit Service ae04f2
		# Delegation to broken TLD.
Packit Service ae04f2
		$packet->push("authority", new Net::DNS::RR("broken 300 NS ns.broken"));
Packit Service ae04f2
		$packet->push("additional", new Net::DNS::RR("ns.broken 300 A 10.53.0.4"));
Packit Service ae04f2
	} else {
Packit Service ae04f2
		# Data for the "bogus referrals" test
Packit Service ae04f2
		$packet->push("authority", new Net::DNS::RR("below.www.example.com 300 NS ns.below.www.example.com"));
Packit Service ae04f2
		$packet->push("additional", new Net::DNS::RR("ns.below.www.example.com 300 A 10.53.0.3"));
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	$sock->send($packet->data);
Packit Service ae04f2
Packit Service ae04f2
	print "RESPONSE:\n";
Packit Service ae04f2
	$packet->print;
Packit Service ae04f2
	print "\n";
Packit Service ae04f2
}