1#!/usr/bin/perl -w
2#
3# Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11# AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15# PERFORMANCE OF THIS SOFTWARE.
16
17# Id
18
19#
20# Ad hoc name server
21#
22
23use IO::File;
24use IO::Socket;
25use Net::DNS;
26use Net::DNS::Packet;
27
28my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.4",
29   LocalPort => 5300, Proto => "udp") 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
39for (;;) {
40	$sock->recv($buf, 512);
41
42	print "**** request from " , $sock->peerhost, " port ", $sock->peerport, "\n";
43
44	my $packet;
45
46	if ($Net::DNS::VERSION > 0.68) {
47		$packet = new Net::DNS::Packet(\$buf, 0);
48		$@ and die $@;
49	} else {
50		my $err;
51		($packet, $err) = new Net::DNS::Packet(\$buf, 0);
52		$err and die $err;
53	}
54
55	print "REQUEST:\n";
56	$packet->print;
57
58	$packet->header->qr(1);
59
60	my @questions = $packet->question;
61	my $qname = $questions[0]->qname;
62	my $qtype = $questions[0]->qtype;
63
64	my $donotrespond = 0;
65
66	if ($qname eq "foo.info") {
67		$donotrespond = 1;
68	} elsif ($qname eq "cname1.example.com") {
69		# Data for the "cname + other data / 1" test
70		$packet->push("answer", new Net::DNS::RR("cname1.example.com 300 CNAME cname1.example.com"));
71		$packet->push("answer", new Net::DNS::RR("cname1.example.com 300 A 1.2.3.4"));
72	} elsif ($qname eq "cname2.example.com") {
73		# Data for the "cname + other data / 2" test: same RRs in opposite order
74		$packet->push("answer", new Net::DNS::RR("cname2.example.com 300 A 1.2.3.4"));
75		$packet->push("answer", new Net::DNS::RR("cname2.example.com 300 CNAME cname2.example.com"));
76	} elsif ($qname eq "www.example.org" || $qname eq "www.example.net" ||
77		 $qname eq "badcname.example.org" ||
78		 $qname eq "goodcname.example.org" ||
79		 $qname eq "foo.baddname.example.org" ||
80		 $qname eq "foo.gooddname.example.org") {
81		# Data for address/alias filtering.
82		$packet->header->aa(1);
83		if ($qtype eq "A") {
84			$packet->push("answer",
85				      new Net::DNS::RR($qname .
86						       " 300 A 192.0.2.1"));
87		} elsif ($qtype eq "AAAA") {
88			$packet->push("answer",
89				      new Net::DNS::RR($qname .
90						" 300 AAAA 2001:db8:beef::1"));
91		}
92	} elsif ($qname eq "badcname.example.net" ||
93		 $qname eq "goodcname.example.net") {
94		# Data for CNAME/DNAME filtering.  We need to make one-level
95		# delegation to avoid automatic acceptance for subdomain aliases
96		$packet->push("authority", new Net::DNS::RR("example.net 300 NS ns.example.net"));
97		$packet->push("additional", new Net::DNS::RR("ns.example.net 300 A 10.53.0.3"));
98	} elsif ($qname =~ /^nodata\.example\.net$/i) {
99		$packet->header->aa(1);
100	} elsif ($qname =~ /^nxdomain\.example\.net$/i) {
101		$packet->header->aa(1);
102		$packet->header->rcode(NXDOMAIN);
103	} elsif ($qname =~ /sub\.example\.org/) {
104		# Data for CNAME/DNAME filtering.  The final answers are
105		# expected to be accepted regardless of the filter setting.
106		$packet->push("authority", new Net::DNS::RR("sub.example.org 300 NS ns.sub.example.org"));
107		$packet->push("additional", new Net::DNS::RR("ns.sub.example.org 300 A 10.53.0.3"));
108	} else {
109		# Data for the "bogus referrals" test
110		$packet->push("authority", new Net::DNS::RR("below.www.example.com 300 NS ns.below.www.example.com"));
111		$packet->push("additional", new Net::DNS::RR("ns.below.www.example.com 300 A 10.53.0.3"));
112	}
113
114	if ($donotrespond == 0) {
115		$sock->send($packet->data);
116		print "RESPONSE:\n";
117		$packet->print;
118		print "\n";
119	}
120}
121