xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/digdelv/ans7/ans.pl (revision d536862b7d93d77932ef5de7eebdc48d76921b77)
1#!/usr/bin/perl -w
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 IO::File;
13use IO::Socket;
14use Net::DNS;
15use Net::DNS::Packet;
16
17my $localport = int($ENV{'PORT'});
18if (!$localport) { $localport = 5300; }
19
20my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.7",
21   LocalPort => $localport, Proto => "udp") or die "$!";
22
23my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
24print $pidf "$$\n" or die "cannot write pid file: $!";
25$pidf->close or die "cannot close pid file: $!";
26sub rmpid { unlink "ans.pid"; exit 1; };
27
28$SIG{INT} = \&rmpid;
29$SIG{TERM} = \&rmpid;
30
31STDOUT->autoflush(1);
32
33print "Net::DNS::VERSION => $Net::DNS::VERSION\n";
34
35for (;;) {
36	$sock->recv($buf, 512);
37
38	print "**** request from " , $sock->peerhost, " port ", $sock->peerport, "\n";
39
40	my $packet;
41
42	if ($Net::DNS::VERSION > 0.68) {
43		$packet = new Net::DNS::Packet(\$buf, 0);
44		$@ and die $@;
45	} else {
46		my $err;
47		($packet, $err) = new Net::DNS::Packet(\$buf, 0);
48		$err and die $err;
49	}
50
51	print "REQUEST:\n";
52	$packet->print;
53
54	$packet->header->qr(1);
55	$packet->header->opcode(5);
56
57	my @questions = $packet->question;
58	my $qname = $questions[0]->qname;
59	my $qtype = $questions[0]->qtype;
60	$packet->push("update", rr_del("$qname SOA"));
61
62	print "RESPONSE:\n";
63	$packet->print;
64
65	$sock->send($packet->data);
66}
67