blib/lib/Tie/DNS.pm

filter: Statementstmt 13.1% 22 / 167 Branchbran 1.5% 1 / 64 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 23.8% 5 / 21 Podpod 100% 5 / 5 totaltotal 11.6% 33 / 283 CC 43 SCAR 43.7
CC
43
Cov
9%
_lookup_to_thing
240.0
_process_args
90.0
STORE
30.0
CRAP 78.9
↑ Summary
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Tie::DNS;
3 1 $Tie::DNS::VERSION = '1.151560';
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 my $NEW_NETDNS = 0;
9 1 ▶if (Net::DNS->version >= 0.69) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Net::DNS"->version >= "0.69") 1 0 1
10 1 $NEW_NETDNS = 1;
11 }
12  
13 1 my %config_rec_defaults = (
14 'AAAA' => 'address',
15 'AFSDB' => 'subtype',
16 'A' => 'address',
17 'CNAME' => 'cname',
18 'EID' => 'rdlength',
19 'HINFO' => 'cpu',
20 'ISDN' => 'address',
21 'LOC' => 'version',
22 'MB' => 'madname',
23 'MG' => 'mgmname',
24 'MINFO' => 'rmailbx',
25 'MR' => 'newname',
26 'MX' => 'exchange',
27 'NAPTR' => 'order',
28 'NIMLOC' => 'rdlength',
29 'NSAP' => 'idp',
30 'NS' => 'nsdname',
31 'NULL' => 'rdlength',
32 'PTR' => 'ptrdname',
33 'PX' => 'preference',
34 'RP' => 'mbox',
35 'RT' => 'intermediate',
36 'SOA' => 'mname',
37 'SRV' => 'target',
38 'TXT' => 'txtdata'
39 );
40  
41 1 my %config_type = (
42 'AAAA' => ['address','ttl'],
43 'AFSDB' => ['subtype','ttl'],
44 'A' => ['address','ttl'],
45 'CNAME' => ['cname','ttl'],
46 'EID' => ['rdlength','rdata','ttl'],
47 'HINFO' => ['cpu','os','ttl'],
48 'ISDN' => ['address','subaddress','ttl'],
49 'LOC' => [
50 'version','size','horiz_pre','vert_pre',
51 'latitude','longitude','latlon','altitude', 'ttl'
52 ],
53 'MB' => ['madname','ttl'],
54 'MG' => ['mgmname','ttl'],
55 'MINFO' => ['rmailbx','emailbx','ttl'],
56 'MR' => ['newname','ttl'],
57 'MX' => ['exchange','preference'],
58 'NAPTR' => [
59 'order','preference','flags','service',
60 'regexp','replacement','ttl'
61 ],
62 'NIMLOC' => ['rdlength','rdata','ttl'],
63 'NSAP' => [
64 'idp','dsp','afi','idi','dfi','aa',
65 'rsvd','rd','area','id','sel','ttl'
66 ],
67 'NS' => ['nsdname','ttl'],
68 'NULL' => ['rdlength','rdata','ttl'],
69 'PTR' => ['ptrdname','ttl'],
70 'PX' => ['preference','map822','mapx400','ttl'],
71 'RP' => ['mbox','txtdname','ttl'],
72 'RT' => ['intermediate','preference','ttl'],
73 'SOA' => [
74 'mname','rname','serial','refresh',
75 'retry','expire','minimum','ttl'
76 ],
77 'SRV' => ['target','port','weight','priority','ttl'],
78 'TXT' => ['txtdata','ttl']
79 );
80  
81 0 ▶sub TIEHASH {
Subroutine TIEHASH: not called CC=4 SCAR=30.0
82 0 my $class = shift;
83 0 my $args = shift;
84  
85 0 ▶ if (defined $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) { } 0 0 0
86 0 ▶ die 'Bad argument format' unless ref $args eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 0 0
87 } else {
88 0 $args = {};
89 }
90  
91 0 my $self = {};
92 0 bless $self, $class;
93
94 0 ▶ $self->{'dns'} = Net::DNS::Resolver->new(%{($args->{resolver_args} || {})});
Condition0%0/2
$args->{'resolver_args'} || {}
l!l
00
MC/DC0%0/1
$args->{'resolver_args'} || {}
$args->{'resolver_args'}
Truth table0%0/2
A
A= $args->{'resolver_args'}
Aresult
11
00
95  
96 0 $self->args($args);
97  
98 0 return $self;
99 }
100  
101 0 ▶sub STORE {
Subroutine STORE: not called CC=5 SCAR=34.0
102 0 my $self = shift;
103 0 my $key = shift;
104 0 my $value = shift;
105  
106 0 ▶ my $root_server = $self->get_root_server
Branch: 1 branch
BranchTrueFalseTotal
my $root_server = $self->get_root_server or die "Dynamic update attempted but no (or bad) domain specified." 0 0 0
107 or die 'Dynamic update attempted but no (or bad) domain specified.';
108  
109 0 my $update = Net::DNS::Update->new($self->_get_arg('domain'));
110 my $update_string = sprintf('%s. %s %s %s',
111 0 $key, $self->{'ttl'}, $self->{'lookup_type'}, $value);
112 0 $update->push('update', rr_add($update_string));
113  
114 0 ▶ my $res = Net::DNS::Resolver->new(%{($self->args->{resolver_args} || {})});
Condition0%0/2
$self->args->{'resolver_args'} || {}
l!l
00
MC/DC0%0/1
$self->args->{'resolver_args'} || {}
$self->args->{'resolver_args'}
Truth table0%0/2
A
A= $self->args->{'resolver_args'}
Aresult
11
00
115 0 $res->nameservers($root_server);
116 0 my $reply = $res->send($update);
117 0 ▶ if (defined $reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reply) { } 0 0 0
118 0 ▶ if ($reply->header->rcode eq 'NOERROR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reply->header->rcode eq 'NOERROR') { } 0 0 0
119 0 return $value;
120 } else {
121 0 $self->{'errstring'} = $self->{'dns'}->errorstring;
122 0 return 0;
123 }
124 } else {
125 0 $self->{'errstring'} = $self->{'dns'}->errorstring;
126 0 return 0;
127 }
128 }
129  
130 0 ▶sub args {
Subroutine args: not called CC=1 SCAR=6.9
Pod documented
131 0 my $self = shift;
132 0 my $args = shift;
133 0 $self->{'args'} = $args;
134 0 $self->_process_args;
135 }
136  
137 0 ▶sub FETCH {
Subroutine FETCH: not called CC=2 SCAR=17.9
138 0 my $self = shift;
139 0 my $lookup = shift;
140  
141 0 ▶ if ( $lookup =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookup =~ /^\d+\.\d+\.\d+\.\d+$/) { } 0 0 0
142 0 return $self->do_reverse_lookup($lookup);
143 } else {
144 0 return $self->do_forward_lookup($lookup);
145 }
146 }
147  
148 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=4 SCAR=30.0
149 0 my $self = shift;
150 0 my @full_zone = $self->{'dns'}->axfr($self->{'root_name_server'});
151 0 ▶ if (scalar(@full_zone) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @full_zone == 0) 0 0 0
152 0 $self->{'errstring'} = $self->{'dns'}->errorstring;
153 0 return 0;
154 }
155  
156 0 my @zone;
157 0 foreach my $rr (@full_zone) {
158 0 ▶ push @zone, $rr if $rr->type eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
if $rr->type eq "A" 0 0 0
159 }
160 0 my $rr = shift @zone;
161 0 $self->{'zone'} = \@zone;
162 0 return $rr->name;
163 }
164  
165 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
166 0 my $self = shift;
167 0 my @zone = @{$self->{'zone'}};
168 0 ▶ if (scalar(@zone) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @zone == 0) 0 0 0
169 0 return 0;
170 }
171 0 my $rr = shift(@zone);
172 0 $self->{'zone'} = \@zone;
173 0 return $rr->name;
174 }
175  
176 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
177 0 my $self = shift;
178  
179 # die ('dynamic DNS updates are not yet available.');
180 }
181  
182 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
183 0 my $self = shift;
184 0 die 'Tie::DNS: DELETE function not implemented';
185 }
186  
187 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
188 0 my $self = shift;
189  
190 #There isn't any real Net::DNS requirement to call anything when
191 #we go bye-bye, so we'll just go bye-bye quietly.
192 }
193  
194 0 ▶sub _process_args {
Subroutine _process_args: not called CC=9 SCAR=45.0
195 0 my $self = shift;
196  
197 0 ▶ if (defined $self->_get_arg('domain')) { #find the root name
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg("domain")) 0 0 0
198 #server for this domain
199 0 $self->{'root_name_server'} = $self->get_root_server;
200 0 $self->{'dns'}->nameservers($self->{'root_name_server'});
201 }
202  
203 0 ▶ if (defined $self->_get_arg('multiple')) { #multiple return
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg("multiple")) 0 0 0
204 #objects
205 #I don't think there's any setup required for this.
206 }
207  
208 0 ▶ if (defined $self->_get_arg('all_fields')) { #all fields
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg("all_fields")) 0 0 0
209 #I don't think there's any setup for this one either.
210 }
211  
212 0 ▶ if (defined $self->_get_arg('type')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg('type')) { } 0 0 0
213 0 ▶ if ( !defined($config_type{$self->_get_arg('type')})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config_type{$self->_get_arg("type")}) 0 0 0
214 0 die 'Bad record type: ' . $self->_get_arg('type');
215 }
216 0 $self->{'lookup_type'} = $self->_get_arg('type');
217 } else {
218 0 $self->{'lookup_type'} = 'A';
219 }
220  
221 0 ▶ if (defined $self->_get_arg('ttl')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg('ttl')) { } 0 0 0
222 0 $self->{'ttl'} = $self->_get_arg('ttl');
223 } else {
224 0 $self->{'ttl'} = 86400;
225 }
226  
227 0 ▶ if (my $cache_param = $self->_get_arg('cache')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache_param = $self->_get_arg('cache')) { } 0 0 0
228 0 eval { require Tie::Cache; };
229 0 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
230 0 tie my %cache, 'Tie::Cache', $cache_param;
231 0 $self->{cache} = \%cache;
232 }
233 } else {
234 0 delete $self->{'cache'};
235 }
236 }
237  
238 0 ▶sub get_root_server {
Subroutine get_root_server: not called CC=3 SCAR=24.8
Pod documented
239 0 my $self = shift;
240 0 my $query = $self->{'dns'}->query($self->_get_arg('domain'), 'SOA');
241 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 0 0 0
242 0 foreach my $rr ($query->answer) {
243 0 print "Root: $rr->mname\n";
244 0 return $rr->mname;
245 }
246 } else {
247 die 'Domain specified, but unable to get SOA record: '
248 0 . $self->{'dns'}->errorstring;
249 }
250 }
251  
252 0 ▶sub _get_arg {
Subroutine _get_arg: not called CC=2 SCAR=17.9
253 0 my $self = shift;
254 0 my $arg_name = shift;
255 0 ▶ return 0 unless defined $self->{'args'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"args"} 0 0 0
256  
257 0 return $self->{'args'}{$arg_name};
258 }
259  
260 0 ▶sub do_reverse_lookup {
Subroutine do_reverse_lookup: not called CC=5 SCAR=34.0
Pod documented
261 0 my $self = shift;
262 0 my $lookup = shift;
263  
264 0 my $query = $self->{'dns'}->search($lookup);
265 0 my @retvals;
266 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 0 0 0
267 0 foreach my $rr ($query->answer) {
268 0 ▶ next unless $rr->type eq 'PTR';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "PTR" 0 0 0
269 0 push @retvals, $rr->ptrdname;
270 }
271 } else {
272 0 $self->{'errstring'} = $self->{'dns'}->errorstring;
273 0 return 0;
274 }
275 0 ▶ if (defined $self->_get_arg('multiple')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg('multiple')) { } 0 0 0
276 0 return \@retvals;
277 } else {
278 0 return shift @retvals;
279 }
280 }
281  
282 0 ▶sub do_forward_lookup {
Subroutine do_forward_lookup: not called CC=2 SCAR=17.9
Pod documented
283 0 my $self = shift;
284 0 my $lookup = shift;
285 0 my @things = $self->_lookup_to_thing($lookup);
286 0 ▶ if (defined $self->_get_arg('multiple')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg('multiple')) { } 0 0 0
287 0 return \@things;
288 } else {
289 0 return shift @things;
290 }
291 }
292  
293 0 ▶sub _lookup_to_thing {
Subroutine _lookup_to_thing: not called CC=15 SCAR=54.8
294 0 my $self = shift;
295 0 my $lookup = shift;
296  
297 0 my $ttl = 0;
298 0 my $now = time();
299 0 my $cache = $self->{cache};
300  
301 0 ▶ if ($cache and my $old = $cache->{$lookup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and my $old = $cache->{$lookup}) 0 0 0
Condition0%0/3
$cache and my $old = $cache->{$lookup}
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and my $old = $cache->{$lookup}
$cache my $old = $cache->{$lookup}
Truth table0%0/3
A and B
A= $cacheB= my $old = $cache->{$lookup}
ABresult
0X0
100
111
302 0 my ($expire, $ret) = @$old;
303 0 ▶ if ($now > $expire) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now > $expire) { } 0 0 0
304 0 delete $cache->{$lookup};
305 } else {
306 0 return @$ret;
307 }
308 }
309  
310 0 my $query = $self->{'dns'}->search($lookup, $self->{'lookup_type'});
311  
312 0 my @retvals;
313 0 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 0 0 0
314 0 foreach my $rr ($query->answer) {
315 0 ▶ $ttl ||= $rr->{ttl};
Condition0%0/3
$ttl ||= $rr->{"ttl"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ttl ||= $rr->{"ttl"}
$ttl $rr->{"ttl"}
Truth table0%0/3
A ||= B
A= $ttlB= $rr->{"ttl"}
ABresult
1X1
011
000
316 0 ▶ next unless $rr->type eq $self->{'lookup_type'};
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq $self->{"lookup_type"} 0 0 0
317 0 ▶ if (defined $self->_get_arg('all_fields')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_get_arg('all_fields')) { } 0 0 0
318 0 my %fields;
319 0 foreach my $field (@{$config_type{$self->{'lookup_type'}}}) {
320 0 ▶ if ($NEW_NETDNS and $field eq 'address') {
Branch: 1 branch
BranchTrueFalseTotal
if ($NEW_NETDNS and $field eq 'address') { } 0 0 0
Condition0%0/3
$NEW_NETDNS and $field eq "address"
!ll&&!rl&&r
000
MC/DC0%0/2
$NEW_NETDNS and $field eq "address"
$NEW_NETDNS $field eq "address"
Truth table0%0/3
A and B
A= $NEW_NETDNSB= $field eq "address"
ABresult
0X0
100
111
321 0 $fields{$field} = inet_ntoa($rr->{$field});
322 } else {
323 0 $fields{$field} = $rr->{$field};
324 }
325 }
326 0 push @retvals,\%fields;
327 } else {
328 0 ▶ if ( $NEW_NETDNS and
Branch: 1 branch
BranchTrueFalseTotal
if ($NEW_NETDNS and $config_rec_defaults{$self->{'lookup_type'}} eq 'address') { } 0 0 0
Condition0%0/3
$NEW_NETDNS and $config_rec_defaults{$self->{"lookup_type"}} eq "address"
!ll&&!rl&&r
000
MC/DC0%0/2
$NEW_NETDNS and $config_rec_defaults{$self->{"lookup_type"}} eq "address"
$NEW_NETDNS $config_rec_defaults{$self->{"lookup_type"}} eq "address"
Truth table0%0/3
A and B
A= $NEW_NETDNSB= $config_rec_defaults{$self->{"lookup_type"}} eq "address"
ABresult
0X0
100
111
329 $config_rec_defaults{$self->{'lookup_type'}}
330 eq 'address') {
331 push @retvals,
332 inet_ntoa(
333 $rr->{
334 $config_rec_defaults{
335 0 $self->{'lookup_type'}
336 }
337 }
338 );
339 } else {
340 push
341 @retvals,
342 0 $rr->{$config_rec_defaults{$self->{'lookup_type'}}};
343 }
344 }
345 }
346 } else {
347 0 $self->{'errstring'} = $self->{'dns'}->errorstring;
348 }
349  
350 0 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 0 0 0
351 0 $cache->{$lookup} = [$now + $ttl, \@retvals];
352 }
353 0 @retvals;
354 }
355  
356 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
357 0 my $self = shift;
358 0 return $self->{'errstring'};
359 }
360  
361 1 1;
362 __END__
↑ Summary