xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/nsupdate/ans4/ans.pl (revision 4b004442778f1201b2161e87fd65ba87aae6601a)
1#!/usr/bin/perl
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::Socket;
15use IO::File;
16use strict;
17
18# Ignore SIGPIPE so we won't fail if peer closes a TCP socket early
19local $SIG{PIPE} = 'IGNORE';
20
21# Flush logged output after every line
22local $| = 1;
23
24my $server_addr = "10.53.0.4";
25if (@ARGV > 0) {
26	$server_addr = @ARGV[0];
27}
28
29my $localport = int($ENV{'PORT'});
30if (!$localport) { $localport = 5300; }
31
32my $udpsock = IO::Socket::INET->new(LocalAddr => "$server_addr",
33   LocalPort => $localport, Proto => "udp", Reuse => 1) or die "$!";
34
35print "listening on $server_addr:$localport.\n";
36
37my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!";
38print $pidf "$$\n" or die "cannot write pid file: $!";
39$pidf->close or die "cannot close pid file: $!";;
40sub rmpid { unlink "ans.pid"; exit 1; };
41
42$SIG{INT} = \&rmpid;
43$SIG{TERM} = \&rmpid;
44
45# Main
46for (;;) {
47	my $rin;
48	my $rout;
49
50	$rin = '';
51	vec($rin, fileno($udpsock), 1) = 1;
52
53	select($rout = $rin, undef, undef, undef);
54
55	if (vec($rout, fileno($udpsock), 1)) {
56		printf "UDP request\n";
57		my $buf;
58		$udpsock->recv($buf, 512);
59	}
60}
61