xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/tsig/ans2/ans.pl (revision f281902de12281841521aa31ef834ad944d725e2)
1# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
2#
3# SPDX-License-Identifier: MPL-2.0
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
12#
13# An adhoc server that returns a TC=1 response with the final byte
14# removed to generate UNEXPECTEDEND form dns_message_parse.
15#
16
17use IO::File;
18use IO::Socket;
19
20my $localport = int($ENV{'PORT'});
21if (!$localport) { $localport = 5300; }
22printf "localport %u\n", $localport;
23
24my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.2",
25   LocalPort => $localport, Proto => "udp") or die "$!";
26
27my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
28print $pidf "$$\n" or die "cannot write pid file: $!";
29$pidf->close or die "cannot close pid file: $!";
30sub rmpid { unlink "ans.pid"; exit 1; };
31
32$SIG{INT} = \&rmpid;
33$SIG{TERM} = \&rmpid;
34
35sub arraystring {
36    my $string = join("", @_);
37    return $string;
38}
39
40for (;;) {
41	$from = $sock->recv($buf, 512);
42	($port, $ip_address) = unpack_sockaddr_in($from);
43	$l = length($buf);
44	printf "received %u bytes from %s#%u\n", $l, inet_ntoa($ip_address), $port;
45	@up = unpack("C[$l]", $buf);
46	$up[2] |= 0x80; # QR
47	$up[2] |= 0x02; # TC
48	$up[3] |= 0x80; # RA
49	$l -= 1;	# truncate the response 1 byte
50	$replydata = pack("C[$l]", @up);
51	printf "sent %u bytes\n", $sock->send($replydata);
52}
53