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