blib/lib/Net/DNS/Resolver/Programmable.pm

filter: Statementstmt 94.3% 50 / 53 Branchbran 68.7% 11 / 16 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 82.8% 77 / 93 CC 13 SCAR 23.3
CC
13
Cov
85%
send
15.3
BEGIN
1.0
BEGIN
1.0
CRAP 10.3
↑ Summary
1 #
2 # Net::DNS::Resolver::Programmable
3 # A Net::DNS::Resolver descendant class for offline emulation of DNS
4 #
5 # (C) 2006-2007 Julian Mehnle <julian@mehnle.net>
6 # Maintained from 2017 by David Precious (BIGPRESH) <davidp@preshweb.co.uk>
7  
8 #
9 ##############################################################################
10  
11 package Net::DNS::Resolver::Programmable;
12  
13 =head1 NAME
14  
15 Net::DNS::Resolver::Programmable - programmable DNS resolver class for offline
16 emulation of DNS
17  
18  
19 =cut
20  
21  
22 1 our $VERSION;
23 1 $VERSION = '0.009';
24  
25 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use base 'Net::DNS::Resolver';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 1 ▶use constant TRUE => (0 == 0);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant FALSE => not TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 my %rcode = map { $_ => 1 } qw(NOERROR FORMERR SERVFAIL NXDOMAIN NOTIMP
36 REFUSED YXDOMAIN YXRRSET NXRRSET NOTAUTH NOTZONE BADVERS
37 BADSIG BADKEY BADTIME BADMODE BADNAME BADALG BADTRUNC);
38  
39  
40 # Interface:
41 ##############################################################################
42  
43 =head1 SYNOPSIS
44  
45 use Net::DNS::Resolver::Programmable;
46 use Net::DNS::RR;
47
48 my $resolver = Net::DNS::Resolver::Programmable->new(
49 records => {
50 'example.com' => [
51 Net::DNS::RR->new('example.com. NS ns.example.org.'),
52 Net::DNS::RR->new('example.com. A 192.168.0.1')
53 ],
54 'ns.example.org' => [
55 Net::DNS::RR->new('ns.example.org. A 192.168.1.1')
56 ]
57 },
58
59 resolver_code => sub {
60 my ($domain, $rr_type, $class) = @_;
61 ...
62 return ($result, $aa, @rrs);
63 }
64 );
65  
66 =cut
67  
68 # Implementation:
69 ##############################################################################
70  
71 =head1 DESCRIPTION
72  
73 B<Net::DNS::Resolver::Programmable> is a B<Net::DNS::Resolver> descendant
74 class that allows a virtual DNS to be emulated instead of querying the real
75 DNS. A set of static DNS records may be supplied, or arbitrary code may be
76 specified as a means for retrieving DNS records, or even generating them on the
77 fly.
78  
79 =head2 Constructor
80  
81 The following constructor is provided:
82  
83 =over
84  
85 =item B<new(%options)>: returns I<Net::DNS::Resolver::Programmable>
86  
87 Creates a new programmed DNS resolver object.
88  
89 %options is a list of key/value pairs representing any of the following
90 options:
91  
92 =over
93  
94 =item B<records>
95  
96 A reference to a hash of arrays containing a static set of I<Net::DNS::RR>
97 objects. The hash entries must be indexed by fully qualified domain names
98 (lower-case, without any trailing dots), and the entries themselves must be
99 arrays of the RR objects pertaining to these domain names. For example:
100  
101 records => {
102 'example.com' => [
103 Net::DNS::RR->new('example.com. NS ns.example.org.'),
104 Net::DNS::RR->new('example.com. A 192.168.0.1')
105 ],
106 'www.example.com' => [
107 Net::DNS::RR->new('www.example.com. A 192.168.0.2')
108 ],
109 'ns.example.org' => [
110 Net::DNS::RR->new('ns.example.org. A 192.168.1.1')
111 ]
112 }
113  
114 If this option is specified, the resolver retrieves requested RRs from this
115 data structure.
116  
117 =item B<resolver_code>
118  
119 A code reference used as a call-back for dynamically retrieving requested RRs.
120  
121 The code must take the following query parameters as arguments: the I<domain>,
122 I<RR type>, and I<class>.
123  
124 It must return a list composed of: the response's I<RCODE> (by name, as
125 returned by L<< Net::DNS::Header->rcode|Net::DNS::Header/rcode >>), the
126 I<< C<aa> (authoritative answer) flag >> (I<boolean>, use B<undef> if you don't
127 care), and the I<Net::DNS::RR answer objects>. If an error string is returned
128 instead of a valid RCODE, a I<Net::DNS::Packet> object is not constructed but
129 an error condition for the resolver is signaled instead.
130  
131 For example:
132  
133 resolver_code => sub {
134 my ($domain, $rr_type, $class) = @_;
135 ...
136 return ($result, $aa, @rrs);
137 }
138  
139 If both this and the C<records> option are specified, then statically
140 programmed records are used in addition to any that are returned by the
141 configured resolver code.
142  
143 =item B<defnames>
144  
145 =item B<dnsrch>
146  
147 =item B<domain>
148  
149 =item B<searchlist>
150  
151 =item B<debug>
152  
153 These Net::DNS::Resolver options are also meaningful with
154 Net::DNS::Resolver::Programmable. See L<Net::DNS::Resolver> for their
155 descriptions.
156  
157 =back
158  
159 =cut
160  
161 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
162 1 my ($class, %options) = @_;
163
164 # Create new object:
165 1 my $self = $class->SUPER::new(%options);
166
167 1 $self->{records} = $options{records};
168 1 $self->{resolver_code} = $options{resolver_code};
169
170 1 return $self;
171 }
172  
173 =back
174  
175 =head2 Instance methods
176  
177 The following instance methods of I<Net::DNS::Resolver> are also supported by
178 I<Net::DNS::Resolver::Programmable>:
179  
180 =over
181  
182 =item B<search>: returns I<Net::DNS::Packet>
183  
184 =item B<query>: returns I<Net::DNS::Packet>
185  
186 =item B<send>: returns I<Net::DNS::Packet>
187  
188 Performs an offline DNS query, using the statically programmed DNS RRs and/or
189 the configured dynamic resolver code. See the L</new> constructor's C<records>
190 and C<resolver_code> options. See the descriptions of L<search, query, and
191 send|Net::DNS::Resolver/search> for details about the calling syntax of these
192 methods.
193  
194 =cut
195  
196 6 ▶sub send {
Subroutine send: called CC=13 SCAR=27.3
Pod documented
197 6 my $self = shift;
198
199 # We could be passed a Net::DNS::Packet object, or an array of strings
200 6 my ($query) = @_;
201 6 ▶ $query = Net::DNS::Packet->new(@_) unless ref ($query);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $query 5 1 6
202  
203 6 my ($question) = $query->question;
204  
205 6 my $domain = lc($question->qname);
206 6 my $rr_type = $question->qtype;
207 6 my $class = $question->qclass;
208
209 6 $self->_reset_errorstring;
210
211 6 my ($result, $aa, @answer_rrs);
212
213 6 ▶ if (defined(my $resolver_code = $self->{resolver_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $resolver_code = $self->{"resolver_code"})) 0 6 6
214 0 ($result, $aa, @answer_rrs) = $resolver_code->($domain, $rr_type, $class);
215 }
216
217 6 ▶ if ( not defined($result) or defined($rcode{$result}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $result or defined $rcode{$result}) { } 6 0 6
Condition33%1/3
not defined $result or defined $rcode{$result}
l!l&&r!l&&!r
600
MC/DC0%0/2
not defined $result or defined $rcode{$result}
not defined $result defined $rcode{$result}
Truth table33%1/3
not(A) or B
A= not defined $resultB= defined $rcode{$result}
ABresult
1X1
011
000
218 # Valid RCODE, return a packet:
219
220 6 ▶ $aa = TRUE if not defined($aa);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aa 6 0 6
221 6 ▶ $result = 'NOERROR' if not defined($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 6 0 6
222
223 6 ▶ if (defined(my $records = $self->{records})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $records = $self->{"records"})) 6 0 6
224 6 ▶ if (ref(my $rrs_for_domain = $records->{$domain}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $rrs_for_domain = $records->{$domain}) eq "ARRAY") 4 2 6
225 4 foreach my $rr (@$rrs_for_domain) {
226 7 ▶ push(@answer_rrs, $rr)
Branch: 1 branch
BranchTrueFalseTotal
if $rr->name eq $domain and $rr->type eq $rr_type and $rr->class eq $class 4 3 7
Condition66%4/6
$rr->name eq $domain and $rr->type eq $rr_type and $rr->class eq $class
!ll&&!rl&&r
304
$rr->name eq $domain and $rr->type eq $rr_type
!ll&&!rl&&r
034
MC/DC33%1/3
$rr->name eq $domain and $rr->type eq $rr_type and $rr->class eq $class
$rr->name eq $domain $rr->type eq $rr_type $rr->class eq $class
Truth table50%2/4
A and B and C
A= $rr->name eq $domainB= $rr->type eq $rr_typeC= $rr->class eq $class
ABCresult
0XX0
10X0
1100
1111
227 if $rr->name eq $domain
228 and $rr->type eq $rr_type
229 and $rr->class eq $class;
230 }
231 }
232 }
233
234 6 my $response_packet = $query->reply;
235 6 $response_packet->header->rcode($result);
236 6 $response_packet->header->aa($aa);
237 6 $response_packet->push(answer => @answer_rrs);
238
239 6 return $response_packet;
240 }
241 else {
242 # Invalid RCODE, signal error condition by not returning a packet:
243 0 $self->errorstring($result);
244 0 return undef;
245 }
246 }
247  
248 =item B<print>
249  
250 =item B<string>: returns I<string>
251  
252 =item B<searchlist>: returns I<list> of I<string>
253  
254 =item B<defnames>: returns I<boolean>
255  
256 =item B<dnsrch>: returns I<boolean>
257  
258 =item B<debug>: returns I<boolean>
259  
260 =item B<errorstring>: returns I<string>
261  
262 =item B<answerfrom>: returns I<string>
263  
264 =item B<answersize>: returns I<integer>
265  
266 See L<Net::DNS::Resolver/METHODS>.
267  
268 =back
269  
270 Currently the following methods of I<Net::DNS::Resolver> are B<not> supported:
271 B<axfr>, B<axfr_start>, B<axfr_next>, B<nameservers>, B<port>, B<srcport>,
272 B<srcaddr>, B<bgsend>, B<bgread>, B<bgisready>, B<tsig>, B<retrans>, B<retry>,
273 B<recurse>, B<usevc>, B<tcp_timeout>, B<udp_timeout>, B<persistent_tcp>,
274 B<persistent_udp>, B<igntc>, B<dnssec>, B<cdflag>, B<udppacketsize>.
275 The effects of using these on I<Net::DNS::Resolver::Programmable> objects are
276 undefined.
277  
278 =head1 SEE ALSO
279  
280 L<Net::DNS::Resolver>
281  
282 For availability, support, and license information, see the README file
283 included with Net::DNS::Resolver::Programmable.
284  
285 =head1 AUTHORS
286  
287 David Precious (BIGPRESH) C<< <davidp@preshweb.co.uk> >> took on maintainership
288 in July 2017
289  
290 Original author Julian Mehnle C<< <julian@mehnle.net> >>
291  
292 =head1 ACKNOWLEDGEMENTS
293  
294 Dick Franks (rwfranks)
295  
296 (This section was added by BIGPRESH in July 2017, so currently omits
297 acknowledgements for those who contributed things in the past; I may
298 retrospectively add them in future.)
299  
300  
301  
302 =cut
303  
304 1 TRUE;
305  
306 # vim:sts=4 sw=4 et
↑ Summary