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

filter: Statementstmt 94.6% 53 / 56 Branchbran 62.5% 10 / 16 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 81.1% 82 / 101 SCAR 27.0
CC
14
Cov
83%
send
17.1
BEGIN
1.0
BEGIN
1.0
CRAP 14.9
↑ 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.008';
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 sub new {
162 1 ▶ my ($class, %options) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
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 sub send {
197 4 ▶ my $self = shift;
Subroutine send: called CC=14 SCAR=28.4
Pod documented
198
199 # We could be passed a Net::DNS::Packet object, or a set of strings; handle
200 # both
201 4 my ($packet, $question);
202 4 ▶ if (Scalar::Util::blessed($_[0]) && $_[0]->isa('Net::DNS::Packet')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('Net::DNS::Packet')) { } 1 3 4
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa("Net::DNS::Packet")
!ll&&!rl&&r
301
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa("Net::DNS::Packet")
builtin::blessed($_[0]) $_[0]->isa("Net::DNS::Packet")
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Net::DNS::Packet")
ABresult
0X0
100
111
203 1 $packet = $_[0];
204 # TODO: is it a safe assumption that a packet we're passed will only
205 # contain one Question object?
206 1 ($question) = $packet->question;
207 } else {
208 3 $question = Net::DNS::Question->new(@_);
209 }
210 4 my $domain = lc($question->qname);
211 4 my $rr_type = $question->qtype;
212 4 my $class = $question->qclass;
213
214 4 $self->_reset_errorstring;
215
216 4 my ($result, $aa, @answer_rrs);
217
218 4 ▶ if (defined(my $resolver_code = $self->{resolver_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $resolver_code = $self->{"resolver_code"})) 0 4 4
219 0 ($result, $aa, @answer_rrs) = $resolver_code->($domain, $rr_type, $class);
220 }
221
222 4 ▶ if ( not defined($result) or defined($rcode{$result}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $result or defined $rcode{$result}) { } 4 0 4
Condition33%1/3
not defined $result or defined $rcode{$result}
l!l&&r!l&&!r
400
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
223 # Valid RCODE, return a packet:
224
225 4 ▶ $aa = TRUE if not defined($aa);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $aa 4 0 4
226 4 ▶ $result = 'NOERROR' if not defined($result);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 4 0 4
227
228 4 ▶ if (defined(my $records = $self->{records})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $records = $self->{"records"})) 4 0 4
229 4 ▶ 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 0 4
230 4 foreach my $rr (@$rrs_for_domain) {
231 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
232 if $rr->name eq $domain
233 and $rr->type eq $rr_type
234 and $rr->class eq $class;
235 }
236 }
237 }
238
239 4 my $response_packet = Net::DNS::Packet->new($domain, $rr_type, $class);
240 4 $response_packet->header->qr(TRUE);
241 4 $response_packet->header->rcode($result);
242 4 $response_packet->header->aa($aa);
243 4 $response_packet->push(answer => @answer_rrs);
244
245 4 return $response_packet;
246 }
247 else {
248 # Invalid RCODE, signal error condition by not returning a packet:
249 0 $self->errorstring($result);
250 0 return undef;
251 }
252 }
253  
254 =item B<print>
255  
256 =item B<string>: returns I<string>
257  
258 =item B<searchlist>: returns I<list> of I<string>
259  
260 =item B<defnames>: returns I<boolean>
261  
262 =item B<dnsrch>: returns I<boolean>
263  
264 =item B<debug>: returns I<boolean>
265  
266 =item B<errorstring>: returns I<string>
267  
268 =item B<answerfrom>: returns I<string>
269  
270 =item B<answersize>: returns I<integer>
271  
272 See L<Net::DNS::Resolver/METHODS>.
273  
274 =back
275  
276 Currently the following methods of I<Net::DNS::Resolver> are B<not> supported:
277 B<axfr>, B<axfr_start>, B<axfr_next>, B<nameservers>, B<port>, B<srcport>,
278 B<srcaddr>, B<bgsend>, B<bgread>, B<bgisready>, B<tsig>, B<retrans>, B<retry>,
279 B<recurse>, B<usevc>, B<tcp_timeout>, B<udp_timeout>, B<persistent_tcp>,
280 B<persistent_udp>, B<igntc>, B<dnssec>, B<cdflag>, B<udppacketsize>.
281 The effects of using these on I<Net::DNS::Resolver::Programmable> objects are
282 undefined.
283  
284 =head1 SEE ALSO
285  
286 L<Net::DNS::Resolver>
287  
288 For availability, support, and license information, see the README file
289 included with Net::DNS::Resolver::Programmable.
290  
291 =head1 AUTHORS
292  
293 David Precious (BIGPRESH) C<< <davidp@preshweb.co.uk> >> took on maintainership
294 in July 2017
295  
296 Original author Julian Mehnle C<< <julian@mehnle.net> >>
297  
298 =head1 ACKNOWLEDGEMENTS
299  
300 Dick Franks (rwfranks)
301  
302 (This section was added by BIGPRESH in July 2017, so currently omits
303 acknowledgements for those who contributed things in the past; I may
304 retrospectively add them in future.)
305  
306  
307  
308 =cut
309  
310 1 TRUE;
311  
312 # vim:sts=4 sw=4 et
↑ Summary