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 14use IO::File; 15use IO::Socket; 16use Net::DNS; 17use Net::DNS::Packet; 18 19my $localport = int($ENV{'PORT'}); 20if (!$localport) { $localport = 5300; } 21 22my $sock = IO::Socket::INET->new(LocalAddr => "10.53.0.6", 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 33for (;;) { 34 $sock->recv($buf, 512); 35 36 print "**** request from " , $sock->peerhost, " port ", $sock->peerport, "\n"; 37 38 my $packet; 39 40 if ($Net::DNS::VERSION > 0.68) { 41 $packet = new Net::DNS::Packet(\$buf, 0); 42 $@ and die $@; 43 } else { 44 my $err; 45 ($packet, $err) = new Net::DNS::Packet(\$buf, 0); 46 $err and die $err; 47 } 48 49 print "REQUEST:\n"; 50 $packet->print; 51 52 $packet->header->qr(1); 53 54 my @questions = $packet->question; 55 my $qname = $questions[0]->qname; 56 my $qtype = $questions[0]->qtype; 57 58 my $donotrespond = 0; 59 60 $packet->header->aa(1); 61 if ($qtype eq "A") { 62 $packet->push("answer", 63 new Net::DNS::RR($qname . " 300 A 10.53.0.5")); 64 } else { 65 $donotrespond = 1; 66 } 67 68 if ($donotrespond == 0) { 69 my $sendsock = 70 IO::Socket::INET->new(LocalAddr => "10.53.1.2", 71 PeerAddr => $sock->peerhost, 72 PeerPort => $sock->peerport, 73 Proto => "udp") or die "$!"; 74 print "**** response from ", $sendsock->sockhost, " to " , 75 $sendsock->peerhost, " port ", $sendsock->peerport, "\n"; 76 $sendsock->send($packet->data); 77 $sendsock->close; 78 print "RESPONSE:\n"; 79 $packet->print; 80 print "\n"; 81 } else { 82 print "DROP:\n"; 83 } 84} 85