SPF/Util.pm

filter: Statementstmt 97.2% 104 / 107 Branchbran 58.0% 29 / 50 Conditioncond 63.8% 23 / 36 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 96.1% 25 / 26 Podpod 87.5% 7 / 8 totaltotal 78.9% 195 / 247 CC 40 SCAR 20.9
CC
40
Cov
81%
valid_domain_for_ip_address
22.2
__ANON__
8.0
ip_address_to_string
7.5
CRAP 8.1
« SPF/v2/Record.pm ↑ Summary SPF/Mech/Include.pm »
1 #
2 # Mail::SPF::Util
3 # Mail::SPF utility class.
4 #
5 # (C) 2005-2012 Julian Mehnle <julian@mehnle.net>
6 # 2005 Shevek <cpan@anarres.org>
7 # $Id: Util.pm 57 2012-01-30 08:15:31Z julian $
8 #
9 ##############################################################################
10  
11 package Mail::SPF::Util;
12  
13 =head1 NAME
14  
15 Mail::SPF::Util - Mail::SPF utility class
16  
17 =head1 VERSION
18  
19 version 3.20261105
20  
21 =cut
22  
23 9 our $VERSION = '3.20261105'; # VERSION
24  
25 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 9 ▶use utf8; # Hack to keep Perl 5.6 from whining about /[\p{}]/.
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 9 ▶use base 'Mail::SPF::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 9 ▶use Mail::SPF::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 9 ▶use Error ':try';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶use Sys::Hostname ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 9 ▶use constant TRUE => (0 == 0);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 9 ▶use constant FALSE => not TRUE;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 9 ▶use constant ipv4_mapped_ipv6_address_pattern =>
Subroutine BEGIN: called CC=1 SCAR=0.0
42 9 qr/^::ffff:(\p{IsXDigit}{1,4}):(\p{IsXDigit}{1,4})/i;
43  
44 # Interface:
45 ##############################################################################
46  
47 =head1 SYNOPSIS
48  
49 use Mail::SPF::Util;
50  
51 $hostname = Mail::SPF::Util->hostname;
52  
53 $ipv6_address_v4mapped =
54 Mail::SPF::Util->ipv4_address_to_ipv6($ipv4_address);
55  
56 $ipv4_address =
57 Mail::SPF::Util->ipv6_address_to_ipv4($ipv6_address_v4mapped);
58  
59 $is_v4mapped =
60 Mail::SPF::Util->ipv6_address_is_ipv4_mapped($ipv6_address);
61  
62 $ip_address_string = Mail::SPF::Util->ip_address_to_string($ip_address);
63 $reverse_name = Mail::SPF::Util->ip_address_reverse($ip_address);
64  
65 $validated_domain = Mail::SPF::Util->valid_domain_for_ip_address(
66 $spf_server, $request,
67 $ip_address, $domain,
68 $find_best_match, # defaults to false
69 $accept_any_domain # defaults to false
70 );
71  
72 $sanitized_string = Mail::SPF::Util->sanitize_string($string);
73  
74 =cut
75  
76 # Implementation:
77 ##############################################################################
78  
79 =head1 DESCRIPTION
80  
81 B<Mail::SPF::Util> is Mail::SPF's utility class.
82  
83 =head2 Class methods
84  
85 The following class methods are provided:
86  
87 =over
88  
89 =item B<hostname>: returns I<string>
90  
91 Returns the fully qualified domain name (FQDN) of the local host.
92  
93 =cut
94  
95 9 my $hostname;
96  
97 17 ▶sub hostname {
Subroutine hostname: called CC=2 SCAR=7.1
Pod undocumented
98 17 my ($self) = @_;
99 17 ▶ return $hostname ||= (gethostbyname(Sys::Hostname::hostname))[0];
Condition66%2/3
$hostname ||= (gethostbyname Sys::Hostname::hostname())[0]
l!l&&r!l&&!r
1250
MC/DC0%0/2
$hostname ||= (gethostbyname Sys::Hostname::hostname())[0]
$hostname (gethostbyname Sys::Hostname::hostname())[0]
Truth table66%2/3
A ||= B
A= $hostnameB= (gethostbyname Sys::Hostname::hostname())[0]
ABresult
1X1
011
000
100 # Thanks to Sys::Hostname::FQDN for that trick!
101 }
102  
103 =item B<ipv4_address_to_ipv6($ipv4_address)>: returns I<NetAddr::IP>; throws
104 I<Mail::SPF::EInvalidOptionValue>
105  
106 Converts the specified I<NetAddr::IP> IPv4 address into an IPv4-mapped IPv6
107 address. Throws a I<Mail::SPF::EInvalidOptionValue> exception if the specified
108 IP address is not an IPv4 address.
109  
110 =cut
111  
112 66 ▶sub ipv4_address_to_ipv6 {
Subroutine ipv4_address_to_ipv6: called CC=3 SCAR=11.0
Pod documented
113 66 my ($self, $ipv4_address) = @_;
114 66 ▶ UNIVERSAL::isa($ipv4_address, 'NetAddr::IP') and
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($ipv4_address, "NetAddr::IP") and $ipv4_address->version == 4 or "Mail::SPF::EInvalidOptionValue"->throw("NetAddr::IP IPv4 address expected") 64 2 66
Condition100%3/3
UNIVERSAL::isa($ipv4_address, "NetAddr::IP") and $ipv4_address->version == 4
!ll&&!rl&&r
1164
MC/DC100%2/2
UNIVERSAL::isa($ipv4_address, "NetAddr::IP") and $ipv4_address->version == 4
UNIVERSAL::isa($ipv4_address, "NetAddr::IP") $ipv4_address->version == 4
Truth table100%3/3
A and B
A= UNIVERSAL::isa($ipv4_address, "NetAddr::IP")B= $ipv4_address->version == 4
ABresult
0X0
100
111
115 $ipv4_address->version == 4
116 or throw Mail::SPF::EInvalidOptionValue('NetAddr::IP IPv4 address expected');
117 64 return NetAddr::IP->new(
118 '::ffff:' . $ipv4_address->addr, # address
119 $ipv4_address->masklen - 32 + 128 # netmask length
120 );
121 }
122  
123 =item B<ipv6_address_to_ipv4($ipv6_address)>: returns I<NetAddr::IP>; throws
124 I<Mail::SPF::EInvalidOptionValue>
125  
126 Converts the specified I<NetAddr::IP> IPv4-mapped IPv6 address into a proper
127 IPv4 address. Throws a I<Mail::SPF::EInvalidOptionValue> exception if the
128 specified IP address is not an IPv4-mapped IPv6 address.
129  
130 =cut
131  
132 7 ▶sub ipv6_address_to_ipv4 {
Subroutine ipv6_address_to_ipv4: called CC=5 SCAR=16.1
Pod documented
133 7 my ($self, $ipv6_address) = @_;
134 7 ▶ UNIVERSAL::isa($ipv6_address, 'NetAddr::IP') and
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") and $ipv6_address->version == 6 and $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern or "Mail::SPF::EInvalidOptionValue"->throw("NetAddr::IP IPv4-mapped IPv6 address expected") 4 3 7
Condition100%6/6
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") and $ipv6_address->version == 6 and $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
!ll&&!rl&&r
214
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") and $ipv6_address->version == 6
!ll&&!rl&&r
115
MC/DC100%3/3
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") and $ipv6_address->version == 6 and $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") $ipv6_address->version == 6 $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
Truth table100%4/4
A and B and C
A= UNIVERSAL::isa($ipv6_address, "NetAddr::IP")B= $ipv6_address->version == 6C= $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
ABCresult
0XX0
10X0
1100
1111
135 $ipv6_address->version == 6 and
136 $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
137 or throw Mail::SPF::EInvalidOptionValue('NetAddr::IP IPv4-mapped IPv6 address expected');
138 4 ▶ return NetAddr::IP->new(
Branch: 1 branch
BranchTrueFalseTotal
$ipv6_address->masklen >= 96 ? : 4 0 4
139 join('.', unpack('C4', pack('H8', sprintf('%04s%04s', $1, $2)))), # address
140 $ipv6_address->masklen >= 128 - 32 ? $ipv6_address->masklen - 128 + 32 : 0 # netmask length
141 );
142 }
143  
144 =item B<ipv6_address_is_ipv4_mapped($ipv6_address)>: returns I<boolean>
145  
146 Returns B<true> if the specified I<NetAddr::IP> IPv6 address is an IPv4-mapped
147 address, B<false> otherwise.
148  
149 =cut
150  
151 71 ▶sub ipv6_address_is_ipv4_mapped {
Subroutine ipv6_address_is_ipv4_mapped: called CC=3 SCAR=11.0
Pod documented
152 71 my ($self, $ipv6_address) = @_;
153 return (
154 71 ▶ UNIVERSAL::isa($ipv6_address, 'NetAddr::IP') and
Condition83%5/6
UNIVERSAL::isa($ipv6_address, 'NetAddr::IP') && $ipv6_address->version == 6 && $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
!ll&&!rl&&r
6443
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") and $ipv6_address->version == 6
!ll&&!rl&&r
0647
MC/DC66%2/3
UNIVERSAL::isa($ipv6_address, 'NetAddr::IP') && $ipv6_address->version == 6 && $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
UNIVERSAL::isa($ipv6_address, "NetAddr::IP") $ipv6_address->version == 6 $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
Truth table75%3/4
A and B && C
A= UNIVERSAL::isa($ipv6_address, "NetAddr::IP")B= $ipv6_address->version == 6C= $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
ABCresult
0XX0
10X0
1100
1111
155 $ipv6_address->version == 6 and
156 $ipv6_address->short =~ $self->ipv4_mapped_ipv6_address_pattern
157 );
158 }
159  
160 =item B<ip_address_to_string($ip_address)>: returns I<string>;
161 throws I<Mail::SPF::EInvalidOptionValue>
162  
163 Returns the given I<NetAddr::IP> IPv4 or IPv6 address compactly formatted as a
164 I<string>. For IPv4 addresses, this is equivalent to calling L<NetAddr::IP's
165 C<addr>|NetAddr::IP/addr> method. For IPv6 addresses, this is equivalent to
166 calling L<NetAddr::IP's C<short>|NedAddr::IP/short> method. Throws a
167 I<Mail::SPF::EInvalidOptionValue> exception if the specified object is not a
168 I<NetAddr::IP> IPv4 or IPv6 address object.
169  
170 =cut
171  
172 1 ▶sub ip_address_to_string {
Subroutine ip_address_to_string: called CC=5 SCAR=20.1
Pod documented
173 1 my ($self, $ip_address) = @_;
174 1 ▶ UNIVERSAL::isa($ip_address, 'NetAddr::IP') and
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($ip_address, "NetAddr::IP") and $ip_address->version == 4 || $ip_address->version == 6 or "Mail::SPF::EInvalidOptionValue"->throw("NetAddr::IP IPv4 or IPv6 address expected") 1 0 1
Condition33%2/6
UNIVERSAL::isa($ip_address, "NetAddr::IP") and $ip_address->version == 4 || $ip_address->version == 6
!ll&&!rl&&r
001
$ip_address->version == 4 or $ip_address->version == 6
l!l&&r!l&&!r
100
MC/DC0%0/3
UNIVERSAL::isa($ip_address, "NetAddr::IP") and $ip_address->version == 4 || $ip_address->version == 6
UNIVERSAL::isa($ip_address, "NetAddr::IP") $ip_address->version == 4 $ip_address->version == 6
Truth table25%1/4
A and B or C
A= UNIVERSAL::isa($ip_address, "NetAddr::IP")B= $ip_address->version == 4C= $ip_address->version == 6
ABCresult
0XX0
1000
11X1
1011
175 ($ip_address->version == 4 or $ip_address->version == 6)
176 or throw Mail::SPF::EInvalidOptionValue('NetAddr::IP IPv4 or IPv6 address expected');
177 1 ▶ return $ip_address->version == 4 ? $ip_address->addr : lc($ip_address->short);
Branch: 1 branch
BranchTrueFalseTotal
$ip_address->version == 4 ? : 1 0 1
178 }
179  
180 =item B<ip_address_reverse($ip_address)>: returns I<string>;
181 throws I<Mail::SPF::EInvalidOptionValue>
182  
183 Returns the C<in-addr.arpa.>/C<ip6.arpa.> reverse notation of the given
184 I<NetAddr::IP> IPv4 or IPv6 address. Throws a I<Mail::SPF::EInvalidOptionValue>
185 exception if the specified object is not a I<NetAddr::IP> IPv4 or IPv6 address
186 object.
187  
188 =cut
189  
190 7 ▶sub ip_address_reverse {
Subroutine ip_address_reverse: called CC=6 SCAR=18.3
Pod documented
191 7 my ($self, $ip_address) = @_;
192 7 ▶ UNIVERSAL::isa($ip_address, 'NetAddr::IP') and
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($ip_address, "NetAddr::IP") and $ip_address->version == 4 || $ip_address->version == 6 or "Mail::SPF::EInvalidOptionValue"->throw("NetAddr::IP IPv4 or IPv6 address expected") 7 0 7
Condition50%3/6
UNIVERSAL::isa($ip_address, "NetAddr::IP") and $ip_address->version == 4 || $ip_address->version == 6
!ll&&!rl&&r
007
$ip_address->version == 4 or $ip_address->version == 6
l!l&&r!l&&!r
520
MC/DC0%0/3
UNIVERSAL::isa($ip_address, "NetAddr::IP") and $ip_address->version == 4 || $ip_address->version == 6
UNIVERSAL::isa($ip_address, "NetAddr::IP") $ip_address->version == 4 $ip_address->version == 6
Truth table50%2/4
A and B or C
A= UNIVERSAL::isa($ip_address, "NetAddr::IP")B= $ip_address->version == 4C= $ip_address->version == 6
ABCresult
0XX0
1000
11X1
1011
193 ($ip_address->version == 4 or $ip_address->version == 6)
194 or throw Mail::SPF::EInvalidOptionValue('NetAddr::IP IPv4 or IPv6 address expected');
195 7 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
196 # Treat IPv4-mapped IPv6 addresses as IPv4 addresses:
197 7 ▶ if($ip_address->version == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_address->version == 6) 2 5 7
198 2 $ip_address = $self->ipv6_address_to_ipv4($ip_address);
199 }
200 }
201 7 ▶ catch Mail::SPF::EInvalidOptionValue with {};
Subroutine __ANON__: called CC=1 SCAR=0.0
202 # ...deliberately ignoring conversion errors.
203 7 ▶ if ($ip_address->version == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ip_address->version == 4) { } 6 1 7
elsif ($ip_address->version == 6) { } 1 0 1
204 6 my @octets = split(/\./, $ip_address->addr);
205 6 @octets = @octets[0 .. int($ip_address->masklen / 8) - 1];
206 6 return join('.', reverse(@octets)) . '.in-addr.arpa.';
207 }
208 elsif ($ip_address->version == 6) {
209 1 my @nibbles = split(//, unpack("H32", $ip_address->aton));
210 1 @nibbles = @nibbles[0 .. int($ip_address->masklen / 4) - 1];
211 1 return join('.', reverse(@nibbles)) . '.ip6.arpa.';
212 }
213 }
214  
215 =item B<valid_domain_for_ip_address($server, $request, $ip_address, $domain,
216 $find_best_match = false, $accept_any_domain = false)>:
217 returns I<string> or B<undef>
218  
219 Finds a valid domain name for the given I<NetAddr::IP> IP address that matches
220 the given domain or a sub-domain thereof. A domain name is valid for the given
221 IP address if the IP address reverse-maps to that domain name in DNS, and the
222 domain name in turn forward-maps to the IP address. Uses the given
223 I<Mail::SPF::Server> and I<Mail::SPF::Request> objects to perform DNS look-ups.
224 Returns the validated domain name.
225  
226 If C<$find_best_match> is B<true>, the one domain name is selected that best
227 matches the given domain name, preferring direct matches over sub-domain
228 matches. Defaults to B<false>.
229  
230 If C<$accept_any_domain> is B<true>, I<any> domain names are considered
231 acceptable, even if they differ completely from the given domain name (which
232 is then effectively unused unless a best match is requested). Defaults to
233 B<false>.
234  
235 =cut
236  
237 9 ▶use constant valid_domain_match_none => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
238 9 ▶use constant valid_domain_match_subdomain => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
239 9 ▶use constant valid_domain_match_identical => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
240  
241 4 ▶sub valid_domain_for_ip_address {
Subroutine valid_domain_for_ip_address: called CC=15 SCAR=31.0
Pod documented
242 4 my ($self, $server, $request, $ip_address, $domain, $find_best_match, $accept_any_domain) = @_;
243  
244 4 ▶ my $addr_rr_type = $ip_address->version == 4 ? 'A' : 'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
$ip_address->version == 4 ? : 4 0 4
245  
246 4 my $reverse_ip_name = $self->ip_address_reverse($ip_address);
247 4 my $ptr_packet = $server->dns_lookup($reverse_ip_name, 'PTR');
248 4 ▶ my @ptr_rrs = $ptr_packet->answer
Branch: 1 branch
BranchTrueFalseTotal
my(@ptr_rrs) = $ptr_packet->answer or $server->count_void_dns_lookup($request) 2 2 4
249 or $server->count_void_dns_lookup($request);
250  
251 # Respect the PTR mechanism lookups limit (RFC 4408, 5.5/3/4):
252 3 ▶ @ptr_rrs = splice(@ptr_rrs, 0, $server->max_name_lookups_per_ptr_mech)
Branch: 1 branch
BranchTrueFalseTotal
if defined $server->max_name_lookups_per_ptr_mech 3 0 3
253 if defined($server->max_name_lookups_per_ptr_mech);
254  
255 3 my $best_match_type;
256 my $valid_domain;
257  
258 # Check PTR records:
259 3 foreach my $ptr_rr (@ptr_rrs) {
260 2 ▶ if ($ptr_rr->type eq 'PTR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr_rr->type eq 'PTR') { } 2 0 2
261 2 my $ptr_domain = $ptr_rr->ptrdname;
262  
263 2 my $match_type;
264 2 ▶ if ($ptr_domain =~ /^\Q$domain\E$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ptr_domain =~ /^\Q$domain\E$/i) { } 0 2 2
elsif ($ptr_domain =~ /\.\Q$domain\E$/i) { } 2 0 2
265 0 $match_type = valid_domain_match_identical;
266 }
267 elsif ($ptr_domain =~ /\.\Q$domain\E$/i) {
268 2 $match_type = valid_domain_match_subdomain;
269 }
270 else {
271 0 $match_type = valid_domain_match_none;
272 }
273  
274 # If we're not accepting _any_ domain, and the PTR domain does not match
275 # the requested domain at all, ignore this PTR domain (RFC 4408, 5.5/5):
276 2 ▶ next if not $accept_any_domain and $match_type == valid_domain_match_none;
Branch: 1 branch
BranchTrueFalseTotal
if not $accept_any_domain and $match_type == 0 0 2 2
Condition33%1/3
not $accept_any_domain and $match_type == 0
!ll&&!rl&&r
200
MC/DC0%0/2
not $accept_any_domain and $match_type == 0
not $accept_any_domain $match_type == 0
Truth table33%1/3
not(A) and B
A= not $accept_any_domainB= $match_type == 0
ABresult
0X0
100
111
277  
278 2 my $is_valid_domain = FALSE;
279  
280 2 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=20.9
281 2 my $addr_packet = $server->dns_lookup($ptr_domain, $addr_rr_type);
282 2 ▶ my @addr_rrs = $addr_packet->answer
Branch: 1 branch
BranchTrueFalseTotal
my(@addr_rrs) = $addr_packet->answer or $server->count_void_dns_lookup($request) 2 0 2
283 or $server->count_void_dns_lookup($request);
284 2 foreach my $addr_rr (@addr_rrs) {
285 2 ▶ if ($addr_rr->type eq $addr_rr_type) {
Branch: 2 branches
BranchTrueFalseTotal
if ($addr_rr->type eq $addr_rr_type) { } 2 0 2
elsif ($addr_rr->type =~ /^(CNAME|A|AAAA)$/) { } 0 0 0
286 2 ▶ $is_valid_domain = TRUE, last
Branch: 1 branch
BranchTrueFalseTotal
if $ip_address == "NetAddr::IP"->new($addr_rr->address) 2 0 2
287 if $ip_address == NetAddr::IP->new($addr_rr->address);
288 # IP address reverse and forward mapping match,
289 # PTR domain validated!
290 }
291 elsif ($addr_rr->type =~ /^(CNAME|A|AAAA)$/) {
292 # A CNAME (which has hopefully been resolved by the server
293 # for us already), or an address RR of an unrequested type.
294 # Silently ignore any of those.
295 # FIXME Silently ignoring address RRs of an "unrequested"
296 # FIXME type poses a disparity with how the "ip{4,6}", "a",
297 # FIXME and "mx" mechanisms tolerantly handle alien but
298 # FIXME convertible IP address types.
299 }
300 else {
301 # Unexpected RR type.
302 # TODO Generate debug info or ignore silently.
303 }
304 }
305 }
306 2 ▶ catch Mail::SPF::EDNSError with {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
307 # Ignore DNS errors on doing A/AAAA RR lookups (RFC 4408, 5.5/5/5).
308  
309 2 ▶ if ($is_valid_domain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_valid_domain) 2 0 2
310 # If we're not looking for the _best_ match, any acceptable validated
311 # domain will do (RFC 4408, 5.5/5):
312 2 ▶ return $ptr_domain if not $find_best_match;
Branch: 1 branch
BranchTrueFalseTotal
unless $find_best_match 0 2 2
313  
314 # Otherwise, is this PTR domain the best possible match?
315 2 ▶ return $ptr_domain if $match_type == valid_domain_match_identical;
Branch: 1 branch
BranchTrueFalseTotal
if $match_type == 2 0 2 2
316  
317 # Lastly, record this match as the best one as of yet:
318 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $best_match_type or $match_type > $best_match_type) 2 0 2
Condition33%1/3
not defined $best_match_type or $match_type > $best_match_type
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $best_match_type or $match_type > $best_match_type
not defined $best_match_type $match_type > $best_match_type
Truth table33%1/3
not(A) or B
A= not defined $best_match_typeB= $match_type > $best_match_type
ABresult
1X1
011
000
319 not defined($best_match_type) or
320 $match_type > $best_match_type
321 ) {
322 2 $valid_domain = $ptr_domain;
323 2 $best_match_type = $match_type;
324 }
325 }
326 }
327 else {
328 # Unexpected RR type.
329 # TODO Generate debug info or ignore silently.
330 }
331 }
332  
333 # Return best match, possibly none (undef):
334 3 return $valid_domain;
335 }
336  
337 =item B<sanitize_string($string)>: returns I<string> or B<undef>
338  
339 Replaces all non-printable or non-ascii characters in a string with their
340 hex-escaped representation (e.g., C<\x00>).
341  
342 =cut
343  
344 15 ▶sub sanitize_string {
Subroutine sanitize_string: called CC=2 SCAR=7.1
Pod documented
345 15 my ($self, $string) = @_;
346  
347 15 ▶ return undef if not defined($string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 15 15
348  
349 15 $string =~ s/([\x00-\x1f\x7f-\xff])/sprintf("\\x%02x", ord($1))/gex;
350 0 $string =~ s/([\x{0100}-\x{ffff}]) /sprintf("\\x{%04x}", ord($1))/gex;
351  
352 15 return $string;
353 }
354  
355 =back
356  
357 =head1 SEE ALSO
358  
359 L<Mail::SPF>
360  
361 For availability, support, and license information, see the README file
362 included with Mail::SPF.
363  
364 =head1 AUTHORS
365  
366 Julian Mehnle E<lt>julian@mehnle.netE<gt>, Shevek E<lt>cpan@anarres.orgE<gt>
367  
368 =cut
369  
370 9 TRUE;
« SPF/v2/Record.pm ↑ Summary SPF/Mech/Include.pm »