blib/lib/Net/DNSBL/Client.pm

filter: Statementstmt 90.8% 178 / 196 Branchbran 67.2% 74 / 110 Conditioncond 53.8% 35 / 65 MC/DCmcdc 16.6% 6 / 36 Subroutinesub 85.7% 18 / 21 Podpod 100% 8 / 8 totaltotal 73.1% 319 / 436 CC 89 SCAR 28.5
CC
89
Cov
77%
_process_reply
45.1
_send_queries
15.3
_matches
15.1
CRAP 17.2
↑ Summary
1 package Net::DNSBL::Client;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Net::DNS::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $VERSION = '0.207';
11  
12 =head1 NAME
13  
14 Net::DNSBL::Client - Client code for querying multiple DNSBLs
15  
16 =head1 SYNOPSIS
17  
18 use Net::DNSBL::Client;
19 my $c = Net::DNSBL::Client->new({ timeout => 3 });
20  
21 $c->query_ip('127.0.0.2', [
22 { domain => 'simple.dnsbl.tld' },
23 { domain => 'masked.dnsbl.tld', type => 'mask', data => '0.0.0.255' },
24 { domain => 'txt.dnsbl.tld', type => 'txt' },
25 { domain => 'need-a-key.example.net' }],
26 { lookup_keys => { 'need-a-key.example.net' => 'my_secret_key' }});
27  
28 # And later...
29 my $answers = $c->get_answers();
30  
31 =head1 METHODS
32  
33 =head2 Class Methods
34  
35 =over 4
36  
37 =item new ( $args )
38  
39 Returns a new Net::DNSBL::Client object.
40  
41 $args is a hash reference and may contain the following key-value pairs:
42  
43 =over 4
44  
45 =item resolver
46  
47 (optional) A Net::DNS::Resolver object. If not provided, a new resolver will be created.
48  
49 =item timeout
50  
51 (optional) An integer number of seconds to use as the upper time limit
52 for the query. If not provided, the default is 10 seconds. If provided,
53 timeout must be a I<positive> integer.
54  
55 =back
56  
57 =back
58  
59 =cut
60  
61 sub new
62 8 ▶{
Subroutine new: called CC=7 SCAR=19.5
Pod documented
63 8 my ($class, $args) = @_;
64 8 my $self = {
65 resolver => undef,
66 timeout => 10,
67 };
68 8 foreach my $possible_arg (keys(%$self)) {
69 16 ▶ if( exists $args->{$possible_arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$possible_arg}) 2 14 16
70 2 $self->{$possible_arg} = delete $args->{$possible_arg};
71 }
72 }
73 8 ▶ if (scalar(%$args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %$args) 1 7 8
74 croak("Unknown arguments to new: " .
75 1 join(', ', (sort { $a cmp $b } keys(%$args))));
76 }
77  
78 # Timeout must be a positive integer
79 7 ▶ if (($self->{timeout} !~ /^\d+$/) || $self->{timeout} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"timeout"} =~ /^\d+$/ or $self->{"timeout"} <= 0) 2 5 7
Condition100%3/3
not $self->{"timeout"} =~ /^\d+$/ or $self->{"timeout"} <= 0
l!l&&r!l&&!r
115
MC/DC100%2/2
not $self->{"timeout"} =~ /^\d+$/ or $self->{"timeout"} <= 0
not $self->{"timeout"} =~ /^\d+$/ $self->{"timeout"} <= 0
Truth table100%3/3
not(A) or B
A= not $self->{"timeout"} =~ /^\d+$/B= $self->{"timeout"} <= 0
ABresult
1X1
011
000
80 2 croak("Timeout must be a positive integer");
81 }
82  
83 5 ▶ $self->{resolver} = Net::DNS::Resolver->new() unless $self->{resolver};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"resolver"} 5 0 5
84  
85 5 $self->{in_flight} = 0;
86 5 $self->{early_exit} = 0;
87  
88 5 bless $self, $class;
89 5 return $self;
90 }
91  
92 sub _matches
93 5 ▶{
Subroutine _matches: called CC=8 SCAR=27.1
94 5 my ($self, $addr, $match) = @_;
95 5 my ($a1, $b1, $c1, $d1) = split(/\./, $addr);
96 5 my ($a2, $b2, $c2, $d2) = split(/\./, lc($match));
97  
98 return (
99 5 ▶ ($a2 eq 'x' || $a1 == $a2) &&
Condition42%9/21
($a2 eq 'x' || $a1 == $a2) && ($b2 eq 'x' || $b1 == $b2) && ($c2 eq 'x' || $c1 == $c2) && ($d2 eq 'x' || $d1 == $d2)
!ll&&!rl&&r
023
$a2 eq "x" || $a1 == $a2 and $b2 eq "x" || $b1 == $b2 and $c2 eq "x" || $c1 == $c2
!ll&&!rl&&r
005
$a2 eq "x" || $a1 == $a2 and $b2 eq "x" || $b1 == $b2
!ll&&!rl&&r
005
$a2 eq "x" or $a1 == $a2
l!l&&r!l&&!r
050
$b2 eq "x" or $b1 == $b2
l!l&&r!l&&!r
050
$c2 eq "x" or $c1 == $c2
l!l&&r!l&&!r
050
$d2 eq "x" or $d1 == $d2
l!l&&r!l&&!r
032
MC/DC12%1/8
($a2 eq 'x' || $a1 == $a2) && ($b2 eq 'x' || $b1 == $b2) && ($c2 eq 'x' || $c1 == $c2) && ($d2 eq 'x' || $d1 == $d2)
$a2 eq "x" $a1 == $a2 $b2 eq "x" $b1 == $b2 $c2 eq "x" $c1 == $c2 $d2 eq "x" $d1 == $d2
Truth table6%2/31
A or B and C or D and E or F && G or H
A= $a2 eq "x"B= $a1 == $a2C= $b2 eq "x"D= $b1 == $b2E= $c2 eq "x"F= $c1 == $c2G= $d2 eq "x"H= $d1 == $d2
ABCDEFGHresult
00XXXXXX0
1X00XXXX0
0100XXXX0
1X1X00XX0
1X0100XX0
011X00XX0
010100XX0
1X1X1X000
1X1X01000
1X011X000
1X0101000
011X1X000
011X01000
01011X000
010101000
1X1X1X1X1
1X1X1X011
1X1X011X1
1X1X01011
1X011X1X1
1X011X011
1X01011X1
1X0101011
011X1X1X1
011X1X011
011X011X1
011X01011
01011X1X1
01011X011
0101011X1
010101011
100 ($b2 eq 'x' || $b1 == $b2) &&
101 ($c2 eq 'x' || $c1 == $c2) &&
102 ($d2 eq 'x' || $d1 == $d2)
103 );
104 }
105  
106 =head2 Instance Methods
107  
108 =over 4
109  
110 =item get_resolver ( )
111  
112 Returns the Net::DNS::Resolver object used for DNS queries.
113  
114 =item get_timeout ( )
115  
116 Returns the timeout in seconds for queries.
117  
118 =item set_timeout ( $secs )
119  
120 Sets the timeout in seconds for queries.
121  
122 =item query_is_in_flight ( )
123  
124 Returns non-zero if "query" has been called, but "get_answers" has not
125 yet been called. Returns zero otherwise.
126  
127 =item query_ip ( $ipaddr, $dnsbls [, $options])
128  
129 Issues a set of DNS queries. Note that the I<query_ip()> method returns as
130 soon as the DNS queries have been issued. It does I<not> wait for
131 DNS responses to come in. Once I<query_ip()> has been called, the
132 Net::DNSBL::Client object is said to have a query I<in flight>. I<query_ip()>
133 may not be called again while a query is in flight.
134  
135 $ipaddr is the text representation of an IPv4 or IPv6 address.
136  
137 $dnsbls is a reference to a list of DNSBL entries; each DNSBL entry
138 is a hash with the following members:
139  
140 =over 4
141  
142 =item domain
143  
144 (required) The domain to query. For example, I<zen.spamhaus.org>.
145  
146 =item type
147  
148 (optional) The type of DNSBL. Possible values are I<normal>, meaning
149 that any returned A record indicates a hit, I<match>, meaning that one
150 of the returned A records must exactly match a given IP address,
151 I<mask>, meaning that one of the returned A records must evaluate to
152 non-zero when bitwise-ANDed against a given IP address, or I<txt>
153 meaning that TXT records should be looked up and returned (rather than
154 A records)a. If omitted, type defaults to I<normal>
155  
156 =item data
157  
158 (optional) For the I<match> and I<mask> types, this data specifies the
159 required match or the bitwise-AND mask. In the case of a I<mask> type,
160 the data can be something like "0.0.0.4", or an integer like "8". In the
161 latter case, the integer I<n> must range from 1 to 255 and is equivalent
162 to 0.0.0.I<n>.
163  
164 For I<match>-type lookups, one or more of the octets can be specified
165 as "x" or "X". For example, specifying a match against
166 127.0.X.3 will match a return code whose first octet is 127, second is zero,
167 third is anything, and fourth is 3.
168  
169 =item userdata
170  
171 (optional) This element can be any scalar or reference that you like.
172 It is simply returned back unchanged in the list of hits.
173  
174 =back
175  
176 $options, if supplied, is a hash of options. Currently, three options
177 are defined:
178  
179 =over 4
180  
181 =item early_exit
182  
183 If set to 1, querying will stop after the first positive result is
184 received, even if other DNSBLs are being queried. Default is 0.
185  
186 =item return_all
187  
188 If set to 1, then the return value from I<get_answers()> will contain
189 all DNSBLs that were supplied to I<query_ip()>, even if the DNSBL did not
190 hit. If set to 0 (the default), then the return value from
191 I<get_answers()> only returns entries for those DNSBLs that actually
192 hit.
193  
194 =item lookup_keys
195  
196 This is a hashref of domain_name => key. Some domains require a secret
197 key to be inserted just before the domain name; rather than including
198 the key in the domain, you can separate it out with the lookup_keys hash,
199 making the returned results more readable.
200  
201 =back
202  
203 =item query_domain ( $domain, $dnsbls [, $options])
204  
205 Similar to query_ip, but considers $domain to be a domain name rather
206 than an IP address, and does not reverse the domain.
207  
208 =item get_answers ( )
209  
210 This method may only be called while a query is in flight. It waits
211 for DNS replies to come back and returns a reference to a list of I<hits>.
212 Once I<get_answers()> returns, a query is no longer in flight.
213  
214 Note that the list of hits is I<not necessarily> returned in the same
215 order as the original list of DNSBLs supplied to I<query_ip()>.
216  
217 Each hit in the returned list is a hash reference containing the
218 following elements:
219  
220 =over 4
221  
222 =item domain
223  
224 The domain of the DNSBL.
225  
226 =item hit
227  
228 Set to 1 if the DNSBL was hit or 0 if it was not. (You will only get
229 entries with hit set to 0 if you used the I<return_all> option to I<query_ip()>.)
230  
231 =item type
232  
233 The type of the DNSBL (normal, match or mask).
234  
235 =item data
236  
237 The data supplied (for normal and mask types)
238  
239 =item userdata
240  
241 The userdata as supplied in the I<query_ip()> call
242  
243 =item actual_hits
244  
245 Reference to array containing actual A or TXT records returned by the
246 lookup that caused a hit.
247  
248 =item replycode
249  
250 The reply code from the DNS server (as a string). Likely to be
251 one of NOERROR, NXDOMAIN, SERVFAIL or TIMEOUT. (TIMEOUT is not
252 a real DNS reply code; it is synthesized by this Perl module if
253 the lookup times out.)
254  
255 =back
256  
257 The hit may contain other elements not documented here; you should count
258 on only the elements documented above.
259  
260 If no DNSBLs were hit, then a reference to a zero-element list is returned.
261  
262 =back
263  
264 =cut
265  
266 sub get_resolver
267 0 ▶{
Subroutine get_resolver: not called CC=1 SCAR=6.9
Pod documented
268 0 my ($self) = @_;
269 0 return $self->{resolver};
270 }
271  
272 sub get_timeout
273 0 ▶{
Subroutine get_timeout: not called CC=1 SCAR=6.9
Pod documented
274 0 my ($self) = @_;
275 0 return $self->{timeout};
276 }
277  
278 sub set_timeout
279 2 ▶{
Subroutine set_timeout: called CC=3 SCAR=12.7
Pod documented
280 2 my ($self, $secs) = @_;
281 2 ▶ if (($secs !~ /^\d+$/) || $secs <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $secs =~ /^\d+$/ or $secs <= 0) 2 0 2
Condition66%2/3
not $secs =~ /^\d+$/ or $secs <= 0
l!l&&r!l&&!r
110
MC/DC0%0/2
not $secs =~ /^\d+$/ or $secs <= 0
not $secs =~ /^\d+$/ $secs <= 0
Truth table66%2/3
not(A) or B
A= not $secs =~ /^\d+$/B= $secs <= 0
ABresult
1X1
011
000
282 2 croak("Timeout must be a positive integer");
283 }
284 0 $self->{timeout} = $secs;
285 0 return $secs;
286 }
287  
288 sub query_is_in_flight
289 0 ▶{
Subroutine query_is_in_flight: not called CC=1 SCAR=6.9
Pod documented
290 0 my ($self) = @_;
291 0 return $self->{in_flight};
292 }
293  
294 sub query_ip
295 9 ▶{
Subroutine query_ip: called CC=4 SCAR=13.9
Pod documented
296 9 my ($self, $ipaddr, $dnsbls, $options) = @_;
297 9 ▶ croak('Cannot issue new query while one is in flight') if $self->{in_flight};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_flight"} 1 8 9
298 8 ▶ croak('First argument (ip address) is required') unless $ipaddr;
Branch: 1 branch
BranchTrueFalseTotal
unless $ipaddr 1 7 8
299 7 ▶ croak('Second argument (dnsbl list) is required') unless $dnsbls;
Branch: 1 branch
BranchTrueFalseTotal
unless $dnsbls 1 6 7
300  
301 # Reverse the IP address in preparation for lookups
302 6 my $revip = $self->_reverse_address($ipaddr);
303  
304 5 return $self->query_domain($revip, $dnsbls, $options);
305 }
306  
307 sub query_domain
308 5 ▶{
Subroutine query_domain: called CC=10 SCAR=24.5
Pod documented
309 5 my ($self, $ip_or_domain, $dnsbls, $options) = @_;
310  
311 5 ▶ croak('Cannot issue new query while one is in flight') if $self->{in_flight};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"in_flight"} 0 5 5
312 5 ▶ croak('First argument (domain) is required') unless $ip_or_domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip_or_domain 0 5 5
313 5 ▶ croak('Second argument (dnsbl list) is required') unless $dnsbls;
Branch: 1 branch
BranchTrueFalseTotal
unless $dnsbls 0 5 5
314  
315 5 foreach my $opt (qw(early_exit return_all)) {
316 10 ▶ if ($options && exists($options->{$opt})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options and exists $options->{$opt}) { } 0 10 10
Condition66%2/3
$options and exists $options->{$opt}
!ll&&!rl&&r
820
MC/DC0%0/2
$options and exists $options->{$opt}
$options exists $options->{$opt}
Truth table66%2/3
A and B
A= $optionsB= exists $options->{$opt}
ABresult
0X0
100
111
317 0 $self->{$opt} = $options->{$opt};
318 } else {
319 10 $self->{$opt} = 0;
320 }
321 }
322  
323 # Build a hash of domains to query. The key is the domain;
324 # value is an arrayref of type/data pairs
325 5 $self->{domains} = $self->_build_domains($dnsbls);
326 5 my $lookup_keys = {};
327 5 ▶ if ($options && exists($options->{lookup_keys}) && ref($options->{lookup_keys}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($options and exists $options->{"lookup_keys"} and ref $options->{"lookup_keys"} eq "HASH") 1 4 5
Condition66%4/6
$options and exists $options->{"lookup_keys"} and ref $options->{"lookup_keys"} eq "HASH"
!ll&&!rl&&r
401
$options and exists $options->{"lookup_keys"}
!ll&&!rl&&r
401
MC/DC33%1/3
$options and exists $options->{"lookup_keys"} and ref $options->{"lookup_keys"} eq "HASH"
$options exists $options->{"lookup_keys"} ref $options->{"lookup_keys"} eq "HASH"
Truth table50%2/4
A and B and C
A= $optionsB= exists $options->{"lookup_keys"}C= ref $options->{"lookup_keys"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
328 1 $lookup_keys = $options->{lookup_keys};
329 }
330  
331 5 $self->_send_queries($ip_or_domain, $lookup_keys);
332 }
333  
334 sub get_answers
335 6 ▶{
Subroutine get_answers: called CC=6 SCAR=17.9
Pod documented
336 6 my ($self) = @_;
337 croak("Cannot call get_answers unless a query is in flight")
338 6 ▶ unless $self->{in_flight};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"in_flight"} 1 5 6
339  
340 5 $self->_collect_results();
341  
342 5 my $ans = [];
343 5 foreach my $d (keys %{$self->{domains}}) {
344 5 foreach my $r (@{$self->{domains}->{$d}}) {
345 7 ▶ push(@$ans, $r) if ( $r->{hit} || $self->{return_all} );
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"hit"} or $self->{"return_all"} 5 2 7
Condition66%2/3
$r->{"hit"} or $self->{"return_all"}
l!l&&r!l&&!r
502
MC/DC50%1/2
$r->{"hit"} or $self->{"return_all"}
$r->{"hit"} $self->{"return_all"}
Truth table66%2/3
A or B
A= $r->{"hit"}B= $self->{"return_all"}
ABresult
1X1
011
000
346 }
347 }
348  
349 5 $self->{in_flight} = 0;
350 5 delete $self->{sel};
351 5 delete $self->{sock_to_domain};
352 5 delete $self->{domains};
353  
354 5 return $ans;
355 }
356  
357 sub _build_domains
358 5 ▶{
Subroutine _build_domains: called CC=3 SCAR=11.0
359 5 my($self, $dnsbls) = @_;
360 5 my $domains = {};
361  
362 5 foreach my $entry (@$dnsbls) {
363 7 ▶ push(@{$domains->{$entry->{domain}}}, {
Condition50%1/2
$entry->{'type'} || 'normal'
l!l
70
MC/DC0%0/1
$entry->{'type'} || 'normal'
$entry->{'type'}
Truth table50%1/2
A
A= $entry->{'type'}
Aresult
11
00
364 domain => $entry->{domain},
365 type => ($entry->{type} || 'normal'),
366 data => $entry->{data},
367 userdata => $entry->{userdata},
368 7 hit => 0,
369 replycode => 'TIMEOUT',
370 });
371 }
372 5 return $domains;
373 }
374  
375 sub _send_queries
376 5 ▶{
Subroutine _send_queries: called CC=14 SCAR=27.3
377 5 my ($self, $ip_or_domain, $lookup_keys) = @_;
378  
379 5 $self->{in_flight} = 1;
380 5 $self->{sel} = IO::Select->new();
381 5 $self->{sock_to_domain} = {};
382  
383 5 foreach my $domain (keys(%{$self->{domains}})) {
384 5 my $lookup_key;
385 5 ▶ if (exists($lookup_keys->{$domain}) && ($lookup_keys->{$domain} ne '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $lookup_keys->{$domain} and $lookup_keys->{$domain} ne '') { } 1 4 5
Condition66%2/3
exists $lookup_keys->{$domain} and $lookup_keys->{$domain} ne ""
!ll&&!rl&&r
401
MC/DC50%1/2
exists $lookup_keys->{$domain} and $lookup_keys->{$domain} ne ""
exists $lookup_keys->{$domain} $lookup_keys->{$domain} ne ""
Truth table66%2/3
A and B
A= exists $lookup_keys->{$domain}B= $lookup_keys->{$domain} ne ""
ABresult
0X0
100
111
386 1 $lookup_key = '.' . $lookup_keys->{$domain};
387 } else {
388 4 $lookup_key = '';
389 }
390 5 my($sock1, $sock2);
391 5 foreach my $e (@{$self->{domains}->{$domain}}) {
392 7 ▶ if ($e->{type} eq 'txt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{'type'} eq 'txt') { } 1 6 7
393 1 ▶ $sock1 ||= $self->{resolver}->bgsend("$ip_or_domain$lookup_key.$domain", 'TXT');
Condition33%1/3
$sock1 ||= $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "TXT")
l!l&&r!l&&!r
010
MC/DC0%0/2
$sock1 ||= $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "TXT")
$sock1 $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "TXT")
Truth table33%1/3
A ||= B
A= $sock1B= $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "TXT")
ABresult
1X1
011
000
394 1 ▶ unless ($sock1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock1) 0 1 1
395 0 die $self->{resolver}->errorstring;
396 }
397 } else {
398 6 ▶ $sock2 ||= $self->{resolver}->bgsend("$ip_or_domain$lookup_key.$domain", 'A');
Condition66%2/3
$sock2 ||= $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "A")
l!l&&r!l&&!r
240
MC/DC0%0/2
$sock2 ||= $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "A")
$sock2 $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "A")
Truth table66%2/3
A ||= B
A= $sock2B= $self->{"resolver"}->bgsend("$ip_or_domain$lookup_key.$domain", "A")
ABresult
1X1
011
000
399 6 ▶ unless ($sock2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock2) 0 6 6
400 0 die $self->{resolver}->errorstring;
401 }
402 }
403 7 ▶ last if ($sock1 && $sock2);
Branch: 1 branch
BranchTrueFalseTotal
if $sock1 and $sock2 0 7 7
Condition66%2/3
$sock1 and $sock2
!ll&&!rl&&r
610
MC/DC0%0/2
$sock1 and $sock2
$sock1 $sock2
Truth table66%2/3
A and B
A= $sock1B= $sock2
ABresult
0X0
100
111
404 }
405  
406  
407 5 ▶ if ($sock1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock1) 1 4 5
408 1 $self->{sock_to_domain}->{$sock1} = $domain;
409 1 $self->{sel}->add($sock1);
410 }
411 5 ▶ if ($sock2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock2) 4 1 5
412 4 $self->{sock_to_domain}->{$sock2} = $domain;
413 4 $self->{sel}->add($sock2);
414 }
415 }
416 }
417  
418 sub _collect_results
419 5 ▶{
Subroutine _collect_results: called CC=9 SCAR=22.8
420 5 my ($self) = @_;
421  
422 5 my $terminate = time() + $self->{timeout};
423 5 my $sel = $self->{sel};
424  
425 5 my $got_a_hit = 0;
426  
427 5 ▶ while(time() <= $terminate) {
Branch: 1 branch
BranchTrueFalseTotal
if (time <= $terminate) 10 0 10
428 10 my $expire = $terminate - time();
429 10 ▶ $expire = 1 if ($expire < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $expire < 1 0 10 10
430 10 my @ready = $sel->can_read($expire);
431  
432 10 ▶ return $got_a_hit unless scalar(@ready);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @ready 5 5 10
433  
434 5 foreach my $sock (@ready) {
435 5 my $pack = $self->{resolver}->bgread($sock);
436 5 my $domain = $self->{sock_to_domain}{$sock};
437 5 $sel->remove($sock);
438 5 undef($sock);
439 5 ▶ next unless $pack;
Branch: 1 branch
BranchTrueFalseTotal
unless $pack 0 5 5
440 5 ▶ if ($self->_process_reply($domain, $pack)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_process_reply($domain, $pack)) 5 0 5
441 5 $got_a_hit = 1;
442 }
443 }
444 5 ▶ return if $got_a_hit && $self->{early_exit};
Branch: 1 branch
BranchTrueFalseTotal
if $got_a_hit and $self->{"early_exit"} 0 5 5
Condition33%1/3
$got_a_hit and $self->{"early_exit"}
!ll&&!rl&&r
050
MC/DC0%0/2
$got_a_hit and $self->{"early_exit"}
$got_a_hit $self->{"early_exit"}
Truth table33%1/3
A and B
A= $got_a_hitB= $self->{"early_exit"}
ABresult
0X0
100
111
445 }
446 }
447  
448 sub _process_reply
449 5 ▶{
Subroutine _process_reply: called CC=25 SCAR=38.1
450 5 my ($self, $domain, $pack, $ans) = @_;
451  
452 5 my $entry = $self->{domains}->{$domain};
453  
454 5 my $rcode = $pack->header->rcode;
455 5 ▶ if ($rcode eq 'SERVFAIL' || $rcode eq 'NXDOMAIN') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rcode eq "SERVFAIL" or $rcode eq "NXDOMAIN") 0 5 5
Condition33%1/3
$rcode eq "SERVFAIL" or $rcode eq "NXDOMAIN"
l!l&&r!l&&!r
005
MC/DC0%0/2
$rcode eq "SERVFAIL" or $rcode eq "NXDOMAIN"
$rcode eq "SERVFAIL" $rcode eq "NXDOMAIN"
Truth table33%1/3
A or B
A= $rcode eq "SERVFAIL"B= $rcode eq "NXDOMAIN"
ABresult
1X1
011
000
456 0 foreach my $dnsbl (@$entry) {
457 0 ▶ next if $dnsbl->{hit};
Branch: 1 branch
BranchTrueFalseTotal
if $dnsbl->{"hit"} 0 0 0
458 0 $dnsbl->{replycode} = $rcode;
459 }
460 0 return 0;
461 }
462  
463 5 my $got_a_hit = 0;
464 5 foreach my $rr ($pack->answer) {
465 5 ▶ next unless ($rr->type eq 'A' || uc($rr->type) eq 'TXT');
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "A" or uc $rr->type eq "TXT" 0 5 5
Condition66%2/3
$rr->type eq "A" or uc $rr->type eq "TXT"
l!l&&r!l&&!r
410
MC/DC0%0/2
$rr->type eq "A" or uc $rr->type eq "TXT"
$rr->type eq "A" uc $rr->type eq "TXT"
Truth table66%2/3
A or B
A= $rr->type eq "A"B= uc $rr->type eq "TXT"
ABresult
1X1
011
000
466 5 foreach my $dnsbl (@$entry) {
467 7 my $this_rr_hit = 0;
468 7 ▶ next if $dnsbl->{hit} && ($dnsbl->{type} eq 'match');
Branch: 1 branch
BranchTrueFalseTotal
if $dnsbl->{"hit"} and $dnsbl->{"type"} eq "match" 0 7 7
Condition33%1/3
$dnsbl->{"hit"} and $dnsbl->{"type"} eq "match"
!ll&&!rl&&r
700
MC/DC0%0/2
$dnsbl->{"hit"} and $dnsbl->{"type"} eq "match"
$dnsbl->{"hit"} $dnsbl->{"type"} eq "match"
Truth table33%1/3
A and B
A= $dnsbl->{"hit"}B= $dnsbl->{"type"} eq "match"
ABresult
0X0
100
111
469 7 $dnsbl->{replycode} = $rcode;
470 7 ▶ if ($dnsbl->{type} eq 'normal') {
Branch: 4 branches
BranchTrueFalseTotal
if ($dnsbl->{'type'} eq 'normal') { } 0 7 7
elsif ($dnsbl->{'type'} eq 'match') { } 5 2 7
elsif ($dnsbl->{'type'} eq 'mask') { } 1 1 2
elsif ($dnsbl->{'type'} eq 'txt') { } 1 0 1
471 0 ▶ next unless $rr->type eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "A" 0 0 0
472 0 $this_rr_hit = 1;
473 } elsif ($dnsbl->{type} eq 'match') {
474 5 ▶ next unless $rr->type eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "A" 0 5 5
475 5 ▶ next unless $self->_matches($rr->address, $dnsbl->{data});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_matches($rr->address, $dnsbl->{"data"}) 2 3 5
476 3 $this_rr_hit = 1;
477 } elsif ($dnsbl->{type} eq 'mask') {
478 1 ▶ next unless $rr->type eq 'A';
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "A" 0 1 1
479 1 my @quads;
480 # For mask, we can be given an IP mask like
481 # a.b.c.d, or an integer n. The latter case
482 # is treated as 0.0.0.n.
483 1 ▶ if ($dnsbl->{data} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dnsbl->{'data'} =~ /^\d+$/) { } 0 1 1
484 0 @quads = (0,0,0,$dnsbl->{data});
485 } else {
486 1 @quads = split(/\./,$dnsbl->{data});
487 }
488  
489 1 my $mask = unpack('N',pack('C4', @quads));
490 1 my $got = unpack('N',pack('C4', split(/\./,$rr->address)));
491 1 ▶ next unless ($got & $mask);
Branch: 1 branch
BranchTrueFalseTotal
unless $got & $mask 0 1 1
492  
493 1 $this_rr_hit = 1;
494 } elsif ($dnsbl->{type} eq 'txt') {
495 1 ▶ next unless uc($rr->type) eq 'TXT';
Branch: 1 branch
BranchTrueFalseTotal
unless uc $rr->type eq "TXT" 0 1 1
496 1 $this_rr_hit = 1;
497 }
498  
499 5 ▶ if( $this_rr_hit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_rr_hit) 5 0 5
500 5 $dnsbl->{hit} = 1;
501 5 ▶ if( ! $dnsbl->{actual_hits} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dnsbl->{"actual_hits"}) 5 0 5
502 5 $dnsbl->{actual_hits} = [];
503 }
504 5 ▶ if ($rr->type eq 'A') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rr->type eq 'A') { } 4 1 5
505 4 push @{$dnsbl->{actual_hits}}, $rr->address;
506 } else {
507 1 push @{$dnsbl->{actual_hits}}, $rr->txtdata;
508 }
509 5 $got_a_hit = 1;
510 }
511 }
512 }
513 5 return $got_a_hit;
514 }
515  
516 sub _reverse_address
517 9 ▶{
Subroutine _reverse_address: called CC=3 SCAR=11.0
518 9 my ($self, $addr) = @_;
519  
520 # The following regex handles both regular IPv4 addresses
521 # and IPv6-mapped IPV4 addresses (::ffff:a.b.c.d)
522 9 ▶ if ($addr =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) 7 2 9
523 7 return "$4.$3.$2.$1";
524 }
525 2 ▶ if ($addr =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr =~ /:/) 1 1 2
526 1 $addr = $self->_expand_ipv6_address($addr);
527 1 $addr =~ s/://g;
528 1 return join('.', reverse(split(//, $addr)));
529 }
530  
531 1 croak("Unrecognized IP address '$addr'");
532 }
533  
534 sub _expand_ipv6_address
535 1 ▶{
Subroutine _expand_ipv6_address: called CC=8 SCAR=21.7
536 1 my ($self, $addr) = @_;
537  
538 1 ▶ return '0000:0000:0000:0000:0000:0000:0000:0000' if ($addr eq '::');
Branch: 1 branch
BranchTrueFalseTotal
if $addr eq "::" 0 1 1
539 1 ▶ if ($addr =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr =~ /::/) 1 0 1
540 # Do nothing if more than one pair of colons
541 1 ▶ return $addr if ($addr =~ /::.*::/);
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /::.*::/ 0 1 1
542  
543 # Make sure we don't begin or end with ::
544 1 ▶ $addr = "0000$addr" if $addr =~ /^::/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /^::/ 0 1 1
545 1 ▶ $addr .= '0000' if $addr =~ /::$/;
Branch: 1 branch
BranchTrueFalseTotal
if $addr =~ /::$/ 0 1 1
546  
547 # Count number of colons
548 1 my $colons = ($addr =~ tr/:/:/);
549 1 ▶ if ($colons < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colons < 8) 1 0 1
550 1 my $missing = ':' . ('0000:' x (8 - $colons));
551 1 $addr =~ s/::/$missing/;
552 }
553 }
554  
555 # Pad short fields
556 1 ▶ return join(':', map { (length($_) < 4 ? ('0' x (4-length($_)) . $_) : $_) } (split(/:/, $addr)));
Branch: 1 branch
BranchTrueFalseTotal
length $_ < 4 ? : 1 7 8
557 }
558  
559 8 1;
560  
561 __END__
↑ Summary