DKIM/DNS.pm

filter: Statementstmt 77.3% 65 / 84 Branchbran 52.9% 18 / 34 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 63.6% 7 / 11 Podpod 50.0% 2 / 4 totaltotal 65.7% 96 / 146 CC 21 SCAR 27.8
CC
21
Cov
69%
query
29.2
query_async
4.2
resolver
2.0
CRAP 16.1
« DKIM/Policy.pm ↑ Summary DKIM/DkPolicy.pm »
1 package Mail::DKIM::DNS;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 our $VERSION = '1.20261116'; # VERSION
5 # ABSTRACT: performs DNS queries for Mail::DKIM
6  
7 # Copyright 2007, 2012 Messiah College. All rights reserved.
8 # Jason Long <jlong@messiah.edu>
9  
10  
11 # This class contains a method to perform synchronous DNS queries.
12 # Hopefully some day it will have a method to perform
13 # asynchronous DNS queries.
14  
15 21 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 our $TIMEOUT = 10;
17 21 our $RESOLVER;
18  
19 187 ▶sub resolver {
Subroutine resolver: called CC=2 SCAR=7.1
Pod documented
20 187 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 187 0 187
21 187 $RESOLVER = $_[0];
22 }
23 187 return $RESOLVER;
24 }
25  
26 0 ▶sub enable_EDNS0 {
Subroutine enable_EDNS0: not called CC=1 SCAR=6.9
Pod documented
27  
28 # enable EDNS0, set acceptable UDP packet size to a
29 # conservative payload size that should fit into a single
30 # packet (MTU less the IP header size) in most cases;
31 # See also draft-andrews-dnsext-udp-fragmentation
32 # and RFC 3542 section 11.3.
33  
34 0 my $res = Net::DNS::Resolver->new();
35 0 $res->udppacketsize( 1280 - 40 );
36 0 resolver($res);
37 }
38  
39 # query- returns a list of RR objects
40 # or an empty list if the domain record does not exist
41 # (e.g. in the case of NXDOMAIN or NODATA)
42 # or throws an error on a DNS query time-out or other transient error
43 # (e.g. SERVFAIL)
44 #
45 # if an empty list is returned, $@ is also set to a string explaining
46 # why no records were returned (e.g. "NXDOMAIN").
47 #
48 289 ▶sub query {
Subroutine query: called CC=16 SCAR=33.7
Pod undocumented
49 289 my ( $domain, $type ) = @_;
50  
51 289 ▶ if ( !$RESOLVER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Mail::DKIM::DNS::RESOLVER) 1 288 289
52 1 ▶ $RESOLVER = Net::DNS::Resolver->new()
Branch: 1 branch
BranchTrueFalseTotal
$Mail::DKIM::DNS::RESOLVER = "Net::DNS::Resolver"->new or die "Internal error: can't create DNS resolver" 1 0 1
53 or die "Internal error: can't create DNS resolver";
54 }
55  
56 289 my $rslv = $RESOLVER;
57  
58 #
59 # perform the DNS query
60 # if the query takes too long, we should generate an error
61 #
62 289 my $resp;
63 289 my $remaining_time = alarm(0); # check time left, stop the timer
64 289 my $deadline = time + $remaining_time;
65 289 my $E;
66 eval {
67 289 local $SIG{__DIE__};
68  
69 # set a timeout, 10 seconds by default
70 0 ▶ local $SIG{ALRM} = sub { die "DNS query timeout for $domain\n" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
71 289 alarm $TIMEOUT;
72  
73 # the query itself could cause an exception, which would prevent
74 # us from resetting the alarm before leaving the eval {} block
75 # so we wrap the query in a nested eval {} block
76 289 my $E2;
77 eval {
78 289 local $SIG{__DIE__};
79 289 $resp = $rslv->send( $domain, $type );
80 289 1;
81 289 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; $resp = $rslv->send($domain, $type); 1 } } or do { $E2 = $@ } 289 0 289
82 0 $E2 = $@;
83 };
84 289 alarm 0;
85 0 ▶ if ($E2) { chomp $E2; die "$E2\n" } # no line number here
Branch: 1 branch
BranchTrueFalseTotal
if ($E2) 0 289 289
86 289 1;
87 289 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; local $SIG{"ALRM"} = sub { die "DNS query timeout for $domain\n"; } ; alarm $Mail::DKIM::DNS::TIMEOUT; my $E2; eval { do { local $SIG{"__DIE__"}; $resp = $rslv->send($domain, $type); 1 } } or do { $E2 = $@ }; alarm 0; if ($E2) { chomp $E2; die "$E2\n"; } ; 1 } } or do { $E = $@ } 289 0 289
88 0 $E = $@; # the $@ only makes sense if eval returns a false
89 };
90 289 alarm 0;
91  
92 # restart the timer if it was active
93 289 ▶ if ( $remaining_time > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_time > 0) 0 289 289
94 0 my $dt = $deadline - time;
95  
96 # make sure the timer expiration will trigger a signal,
97 # even at the expense of stretching the interval by one second
98 0 ▶ alarm( $dt < 1 ? 1 : $dt );
Branch: 1 branch
BranchTrueFalseTotal
$dt < 1 ? : 0 0 0
99 }
100 0 ▶ if ($E) { chomp $E; die $E } # ensure a line number
Branch: 1 branch
BranchTrueFalseTotal
if ($E) 0 289 289
101  
102 # RFC 2308: NODATA is indicated by an answer with the RCODE set to NOERROR
103 # and no relevant answers in the answer section. The authority section
104 # will contain an SOA record, or there will be no NS records there.
105 # NODATA responses have to be algorithmically determined from the
106 # response's contents as there is no RCODE value to indicate NODATA.
107 # In some cases to determine with certainty that NODATA is the correct
108 # response it can be necessary to send another query.
109  
110 289 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) 289 0 289
111 289 my $header = $resp->header;
112 289 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 289 0 289
113  
114 # NOERROR, NXDOMAIN, SERVFAIL, FORMERR, REFUSED, ...
115 289 my $rcode = $header->rcode;
116  
117 289 $@ = $rcode;
118 289 ▶ if ( $rcode eq 'NOERROR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rcode eq 'NOERROR') { } 287 2 289
elsif ($rcode eq 'NXDOMAIN') { } 2 0 2
119  
120 # may or may not contain RRs in the answer sect
121 287 my @result = grep { lc $_->type eq lc $type } $resp->answer;
122 287 ▶ $@ = 'NODATA' if !@result;
Branch: 1 branch
BranchTrueFalseTotal
unless @result 3 284 287
123 287 return @result; # possibly empty
124 }
125 elsif ( $rcode eq 'NXDOMAIN' ) {
126 2 return; # empty list, rcode in $@
127 }
128 }
129 }
130 0 ▶ if ( $rslv->errorstring eq 'NOERROR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rslv->errorstring eq "NOERROR") 0 0 0
131 0 return;
132 }
133 0 ▶ if ( $rslv->errorstring =~ /\bno error\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rslv->errorstring =~ /\bno error\b/) 0 0 0
134 0 return;
135 }
136 0 die 'DNS error: ' . $rslv->errorstring . "\n";
137 }
138  
139 # query_async() - perform a DNS query asynchronously
140 #
141 # my $waiter = query_async('example.org', 'TXT',
142 # Callbacks => {
143 # Success => \&on_success,
144 # Error => \&on_error,
145 # },
146 # );
147 # my $result = $waiter->();
148 #
149 585 ▶sub query_async {
Subroutine query_async: called CC=4 SCAR=14.3
Pod undocumented
150 585 my ( $domain, $type, %prms ) = @_;
151  
152 585 ▶ my $callbacks = $prms{Callbacks} || {};
Condition50%1/2
$prms{'Callbacks'} || {}
l!l
5850
MC/DC0%0/1
$prms{'Callbacks'} || {}
$prms{'Callbacks'}
Truth table50%1/2
A
A= $prms{'Callbacks'}
Aresult
11
00
153 0 ▶ my $on_success = $callbacks->{Success} || sub { $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$callbacks->{'Success'} || sub { $_[0]; }
l!l&&r!l&&!r
58500
MC/DC0%0/2
$callbacks->{'Success'} || sub { $_[0]; }
$callbacks->{'Success'} sub { $_[0]; }
Truth table33%1/3
A || B
A= $callbacks->{'Success'}B= sub { $_[0]; }
ABresult
1X1
011
000
154 0 ▶ my $on_error = $callbacks->{Error} || sub { die $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$callbacks->{'Error'} || sub { die $_[0]; }
l!l&&r!l&&!r
58230
MC/DC0%0/2
$callbacks->{'Error'} || sub { die $_[0]; }
$callbacks->{'Error'} sub { die $_[0]; }
Truth table66%2/3
A || B
A= $callbacks->{'Error'}B= sub { die $_[0]; }
ABresult
1X1
011
000
155  
156 492 ▶ my $waiter = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
157 492 my @resp;
158 my $rcode;
159 eval {
160 492 local $SIG{__DIE__};
161 492 @resp = query( $domain, $type );
162 489 $rcode = $@;
163 489 1;
164 492 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local $SIG{"__DIE__"}; @resp = query($domain, $type); $rcode = $@; 1 } } or do { return &$on_error($@) } 489 3 492
165 3 return $on_error->($@);
166 };
167 489 $@ = $rcode;
168 489 return $on_success->(@resp);
169 585 };
170 585 return $waiter;
171 }
172  
173 21 1;
174  
175 __END__
« DKIM/Policy.pm ↑ Summary DKIM/DkPolicy.pm »