xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/rpzrecurse/ans5/ans.pl (revision a45db23f655e22f0c2354600d3b3c2cb98abf2dc)
1#!/usr/bin/perl -w
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
14use IO::File;
15use IO::Socket;
16use Net::DNS;
17use Net::DNS::Packet;
18
19my $localport = int($ENV{'PORT'});
20if (!$localport) { $localport = 5300; }
21
22my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.5",
23   LocalPort => $localport, Proto => "udp") or die "$!";
24
25my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
26print $pidf "$$\n" or die "cannot write pid file: $!";
27$pidf->close or die "cannot close pid file: $!";
28sub rmpid { unlink "ans.pid"; exit 1; };
29
30$SIG{INT} = \&rmpid;
31$SIG{TERM} = \&rmpid;
32
33for (;;) {
34	$sock->recv($buf, 512);
35
36	print "**** request from " , $sock->peerhost, " port ", $sock->peerport, "\n";
37
38	my $packet;
39
40	if ($Net::DNS::VERSION > 0.68) {
41		$packet = new Net::DNS::Packet(\$buf, 0);
42		$@ and die $@;
43	} else {
44		my $err;
45		($packet, $err) = new Net::DNS::Packet(\$buf, 0);
46		$err and die $err;
47	}
48
49	print "REQUEST:\n";
50	$packet->print;
51
52	$packet->header->qr(1);
53
54	my @questions = $packet->question;
55	my $qname = $questions[0]->qname;
56	my $qtype = $questions[0]->qtype;
57
58	my $donotrespond = 0;
59
60	$packet->header->aa(1);
61	if ($qtype eq "A") {
62		$packet->push("answer",
63			      new Net::DNS::RR($qname .
64					       " 300 A 10.53.0.5"));
65	#} elsif ($qtype eq "AAAA") {
66		#$packet->push("answer",
67			      #new Net::DNS::RR($qname .
68					#" 300 AAAA 2001:db8:beef::1"));
69	} elsif ($qtype eq "NS") {
70		$donotrespond = 1;
71	}
72
73	if ($donotrespond == 0) {
74		$sock->send($packet->data);
75		print "RESPONSE:\n";
76		$packet->print;
77		print "\n";
78	} else {
79		print "DROP:\n";
80	}
81}
82