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 14# This is a TCP-only DNS server whose aim is to facilitate testing how dig 15# copes with prematurely closed TCP connections. 16# 17# This server can be configured (through a separate control socket) with a 18# series of responses to send for subsequent incoming TCP DNS queries. Only 19# one query is handled before closing each connection. In order to keep things 20# simple, the server is not equipped with any mechanism for handling malformed 21# queries. 22# 23# Available response types are defined in the %response_types hash in the 24# getAnswerSection() function below. Each RR returned is generated dynamically 25# based on the QNAME found in the incoming query. 26 27use IO::File; 28use Net::DNS; 29use Net::DNS::Packet; 30 31use strict; 32 33# Ignore SIGPIPE so we won't fail if peer closes a TCP socket early 34local $SIG{PIPE} = 'IGNORE'; 35 36# Flush logged output after every line 37local $| = 1; 38 39my $server_addr = "10.53.0.5"; 40if (@ARGV > 0) { 41 $server_addr = @ARGV[0]; 42} 43 44my $mainport = int($ENV{'PORT'}); 45if (!$mainport) { $mainport = 5300; } 46my $ctrlport = int($ENV{'EXTRAPORT1'}); 47if (!$ctrlport) { $ctrlport = 5301; } 48 49my $ctlsock = IO::Socket::INET->new(LocalAddr => "$server_addr", 50 LocalPort => $ctrlport, Proto => "tcp", Listen => 5, Reuse => 1) or die "$!"; 51 52my $tcpsock = IO::Socket::INET->new(LocalAddr => "$server_addr", 53 LocalPort => $mainport, Proto => "tcp", Listen => 5, Reuse => 1) or die "$!"; 54 55my $pidf = new IO::File "ans.pid", "w" or die "cannot open pid file: $!"; 56print $pidf "$$\n" or die "cannot write pid file: $!"; 57$pidf->close or die "cannot close pid file: $!";; 58sub rmpid { unlink "ans.pid"; exit 1; }; 59 60$SIG{INT} = \&rmpid; 61$SIG{TERM} = \&rmpid; 62 63my @response_sequence = ("complete_axfr"); 64my $connection_counter = 0; 65 66# Return the next answer type to send, incrementing the connection counter and 67# making sure the latter does not exceed the size of the array holding the 68# configured response sequence. 69sub getNextResponseType { 70 my $response_type = $response_sequence[$connection_counter]; 71 72 $connection_counter++; 73 $connection_counter %= scalar(@response_sequence); 74 75 return $response_type; 76} 77 78# Return an array of resource records comprising the answer section of a given 79# response type. 80sub getAnswerSection { 81 my ($response_type, $qname) = @_; 82 83 my %response_types = ( 84 no_response => [], 85 86 partial_axfr => [ 87 Net::DNS::RR->new("$qname 300 IN SOA . . 0 0 0 0 300"), 88 Net::DNS::RR->new("$qname NS ."), 89 ], 90 91 complete_axfr => [ 92 Net::DNS::RR->new("$qname 300 IN SOA . . 0 0 0 0 300"), 93 Net::DNS::RR->new("$qname NS ."), 94 Net::DNS::RR->new("$qname 300 IN SOA . . 0 0 0 0 300"), 95 ], 96 ); 97 98 return $response_types{$response_type}; 99} 100 101 102# Generate a Net::DNS::Packet containing the response to send on the current 103# TCP connection. If the answer section of the response is determined to be 104# empty, no data will be sent on the connection at all (immediate EOF). 105sub generateResponse { 106 my ($buf) = @_; 107 my $request; 108 109 if ($Net::DNS::VERSION > 0.68) { 110 $request = new Net::DNS::Packet(\$buf, 0); 111 $@ and die $@; 112 } else { 113 my $err; 114 ($request, $err) = new Net::DNS::Packet(\$buf, 0); 115 $err and die $err; 116 } 117 118 my @questions = $request->question; 119 my $qname = $questions[0]->qname; 120 my $qtype = $questions[0]->qtype; 121 my $qclass = $questions[0]->qclass; 122 my $id = $request->header->id; 123 124 my $packet = new Net::DNS::Packet($qname, $qtype, $qclass); 125 $packet->header->qr(1); 126 $packet->header->aa(1); 127 $packet->header->id($id); 128 129 my $response_type = getNextResponseType(); 130 my $answers = getAnswerSection($response_type, $qname); 131 for my $rr (@$answers) { 132 $packet->push("answer", $rr); 133 } 134 135 print " Sending \"$response_type\" response\n"; 136 137 return $packet->data if @$answers; 138} 139 140my $rin; 141my $rout; 142for (;;) { 143 $rin = ''; 144 vec($rin, fileno($ctlsock), 1) = 1; 145 vec($rin, fileno($tcpsock), 1) = 1; 146 147 select($rout = $rin, undef, undef, undef); 148 149 if (vec($rout, fileno($ctlsock), 1)) { 150 my $conn = $ctlsock->accept; 151 @response_sequence = split(' ', $conn->getline); 152 $connection_counter = 0; 153 print "Response sequence set to: @response_sequence\n"; 154 $conn->close; 155 } elsif (vec($rout, fileno($tcpsock), 1)) { 156 my $buf; 157 my $lenbuf; 158 my $conn = $tcpsock->accept; 159 my $n = $conn->sysread($lenbuf, 2); 160 die unless $n == 2; 161 my $len = unpack("n", $lenbuf); 162 $n = $conn->sysread($buf, $len); 163 die unless $n == $len; 164 print "TCP request\n"; 165 my $response = generateResponse($buf); 166 if ($response) { 167 $len = length($response); 168 $n = $conn->syswrite(pack("n", $len), 2); 169 $n = $conn->syswrite($response, $len); 170 print " Sent: $n chars via TCP\n"; 171 } else { 172 print " No response sent\n"; 173 } 174 $conn->close; 175 } 176} 177