xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/tsig/ans2/ans.pl (revision dd3ee07da436799d8de85f3055253118b76bf345)
1# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
2#
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, you can obtain one at https://mozilla.org/MPL/2.0/.
6#
7# See the COPYRIGHT file distributed with this work for additional
8# information regarding copyright ownership.
9
10#
11# An adhoc server that returns a TC=1 response with the final byte
12# removed to generate UNEXPECTEDEND form dns_message_parse.
13#
14
15use IO::File;
16use IO::Socket;
17
18my $localport = int($ENV{'PORT'});
19if (!$localport) { $localport = 5300; }
20printf "localport %u\n", $localport;
21
22my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.2",
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
33sub arraystring {
34    my $string = join("", @_);
35    return $string;
36}
37
38for (;;) {
39	$from = $sock->recv($buf, 512);
40	($port, $ip_address) = unpack_sockaddr_in($from);
41	$l = length($buf);
42	printf "received %u bytes from %s#%u\n", $l, inet_ntoa($ip_address), $port;
43	@up = unpack("C[$l]", $buf);
44	$up[2] |= 0x80; # QR
45	$up[2] |= 0x02; # TC
46	$up[3] |= 0x80; # RA
47	$l -= 1;	# truncate the response 1 byte
48	$replydata = pack("C[$l]", @up);
49	printf "sent %u bytes\n", $sock->send($replydata);
50}
51