blib/lib/EV/cares.pm

filter: Statementstmt 100% 163 / 163 Branchbran 98.7% 81 / 82 Conditioncond 71.4% 10 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 22 / 22 Podpod 100% 9 / 9 totaltotal 96.9% 290 / 299 CC 54 SCAR 18.4
CC
54
Cov
98%
search_all
10.0
ptr_name
9.0
reverse_all
9.0
CRAP 6.3
↑ Summary cares.xs »
1 package EV::cares;
2 19 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 BEGIN {
8 19 ▶ our $VERSION = '0.03';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶ use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 XSLoader::load __PACKAGE__, $VERSION;
11 }
12  
13 19 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 19 our @EXPORT_OK;
16 19 our %EXPORT_TAGS;
17  
18 19 $EXPORT_TAGS{status} = [qw(
19 ARES_SUCCESS ARES_ENODATA ARES_EFORMERR ARES_ESERVFAIL ARES_ENOTFOUND
20 ARES_ENOTIMP ARES_EREFUSED ARES_EBADQUERY ARES_EBADNAME ARES_EBADFAMILY
21 ARES_EBADRESP ARES_ECONNREFUSED ARES_ETIMEOUT ARES_EOF ARES_EFILE
22 ARES_ENOMEM ARES_EDESTRUCTION ARES_EBADSTR ARES_EBADFLAGS ARES_ENONAME
23 ARES_EBADHINTS ARES_ENOTINITIALIZED ARES_ECANCELLED ARES_ESERVICE
24 ARES_ENOSERVER
25 )];
26  
27 19 $EXPORT_TAGS{types} = [qw(
28 T_A T_NS T_CNAME T_SOA T_PTR T_MX T_TXT T_AAAA T_SRV T_NAPTR
29 T_DS T_RRSIG T_DNSKEY T_TLSA T_SVCB T_HTTPS T_CAA T_ANY
30 )];
31  
32 19 $EXPORT_TAGS{classes} = [qw(C_IN C_CHAOS C_HS C_ANY)];
33  
34 19 $EXPORT_TAGS{flags} = [qw(
35 ARES_FLAG_USEVC ARES_FLAG_PRIMARY ARES_FLAG_IGNTC ARES_FLAG_NORECURSE
36 ARES_FLAG_STAYOPEN ARES_FLAG_NOSEARCH ARES_FLAG_NOALIASES ARES_FLAG_NOCHECKRESP
37 ARES_FLAG_EDNS ARES_FLAG_NO_DFLT_SVR ARES_FLAG_DNS0x20
38 )];
39  
40 19 $EXPORT_TAGS{ai} = [qw(
41 ARES_AI_CANONNAME ARES_AI_NUMERICHOST ARES_AI_PASSIVE ARES_AI_NUMERICSERV
42 ARES_AI_V4MAPPED ARES_AI_ALL ARES_AI_ADDRCONFIG ARES_AI_NOSORT
43 )];
44  
45 19 $EXPORT_TAGS{ni} = [qw(
46 ARES_NI_NOFQDN ARES_NI_NUMERICHOST ARES_NI_NAMEREQD ARES_NI_NUMERICSERV
47 ARES_NI_DGRAM ARES_NI_TCP ARES_NI_UDP
48 )];
49  
50 19 $EXPORT_TAGS{families} = [qw(AF_INET AF_INET6 AF_UNSPEC)];
51  
52 {
53 19 my %seen;
54 19 @EXPORT_OK = grep { !$seen{$_}++ } map { @$_ } values %EXPORT_TAGS;
55 19 $EXPORT_TAGS{all} = [@EXPORT_OK];
56 }
57  
58 # ptr_name($ip) -> reverse-lookup name (.in-addr.arpa for IPv4, .ip6.arpa for IPv6)
59 # Pure-Perl, no resolver needed.
60 sub ptr_name {
61 11 ▶ my ($ip) = @_;
Subroutine ptr_name: called CC=9 SCAR=22.0
Pod documented
62 11 require Carp;
63 11 ▶ Carp::croak("ptr_name: missing IP") unless defined $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 1 10 11
64 10 ▶ if (my @oct = $ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@oct) = $ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/u) 6 4 10
65 6 for my $o (@oct) {
66 18 ▶ Carp::croak("ptr_name: leading-zero octet '$o' is ambiguous (octal vs decimal)")
Branch: 1 branch
BranchTrueFalseTotal
if length $o > 1 and $o =~ /^0/u 1 17 18
Condition100%3/3
length $o > 1 and $o =~ /^0/u
!ll&&!rl&&r
1071
MC/DC100%2/2
length $o > 1 and $o =~ /^0/u
length $o > 1 $o =~ /^0/u
Truth table100%3/3
A and B
A= length $o > 1B= $o =~ /^0/u
ABresult
0X0
100
111
67 if length($o) > 1 && $o =~ /^0/;
68 17 ▶ Carp::croak("ptr_name: invalid IPv4 octet '$o'") if $o > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $o > 255 1 16 17
69 }
70 4 return "$oct[3].$oct[2].$oct[1].$oct[0].in-addr.arpa";
71 }
72 4 ▶ if ($ip =~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip =~ /:/u) 3 1 4
73 3 require Socket;
74 3 ▶ my $packed = Socket::inet_pton(Socket::AF_INET6(), $ip)
Branch: 1 branch
BranchTrueFalseTotal
my $packed = Socket::inet_pton(Socket::AF_INET6(), $ip) or Carp::croak("ptr_name: invalid IPv6 '${ip}'") 1 2 3
75 or Carp::croak("ptr_name: invalid IPv6 '$ip'");
76 2 my @nibbles = CORE::reverse(split //, unpack 'H*', $packed);
77 2 return join('.', @nibbles) . '.ip6.arpa';
78 }
79 1 Carp::croak("ptr_name: not a recognized IPv4/IPv6 address: '$ip'");
80 }
81  
82 # parse_header($buf) -> hashref of DNS header fields
83 # Useful for inspecting query() raw responses (AD/CD/AA/TC/RA/RD/QR + counts).
84 sub parse_header {
85 7 ▶ my ($buf) = @_;
Subroutine parse_header: called CC=2 SCAR=6.9
Pod documented
86 7 require Carp;
87 7 ▶ Carp::croak("parse_header: buffer too short") if length($buf) < 12;
Branch: 1 branch
BranchTrueFalseTotal
if length $buf < 12 1 6 7
88 6 my ($id, $w, $qd, $an, $ns, $ar) = unpack 'nnnnnn', $buf;
89 return {
90 6 id => $id,
91 qr => ($w >> 15) & 1,
92 opcode => ($w >> 11) & 0xf,
93 aa => ($w >> 10) & 1,
94 tc => ($w >> 9) & 1,
95 rd => ($w >> 8) & 1,
96 ra => ($w >> 7) & 1,
97 z => ($w >> 6) & 1,
98 ad => ($w >> 5) & 1,
99 cd => ($w >> 4) & 1,
100 rcode => $w & 0xf,
101 qdcount => $qd,
102 ancount => $an,
103 nscount => $ns,
104 arcount => $ar,
105 };
106 }
107  
108 sub resolve_all {
109 7 ▶ my ($self, $names, $cb) = @_;
Subroutine resolve_all: called CC=6 SCAR=17.9
Pod documented
110 7 require Carp;
111 7 ▶ Carp::croak("resolve_all: first argument must be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 2 5 7
112 unless ref $names eq 'ARRAY';
113 5 ▶ Carp::croak("resolve_all: callback must be a CODE reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 4 5
114 unless ref $cb eq 'CODE';
115 4 ▶ Carp::croak("resolve_all: resolver is destroyed") if $self->is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_destroyed 1 3 4
116 3 ▶ return $cb->({}) unless @$names;
Branch: 1 branch
BranchTrueFalseTotal
unless @$names 1 2 3
117 2 my %seen;
118 2 my @unique = grep { !$seen{$_}++ } @$names;
119 2 my %res;
120 2 my $pending = @unique;
121 2 for my $name (@unique) {
122 $self->resolve($name, sub {
123 3 ▶ my ($status, @addrs) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
124 3 $res{$name} = { status => $status, addrs => \@addrs };
125 3 ▶ $cb->(\%res) if --$pending == 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$pending == 0 2 1 3
126 3 });
127 }
128 2 return;
129 }
130  
131 sub resolve_ttl_all {
132 5 ▶ my ($self, $names, $cb) = @_;
Subroutine resolve_ttl_all: called CC=6 SCAR=17.9
Pod documented
133 5 require Carp;
134 5 ▶ Carp::croak("resolve_ttl_all: first argument must be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 1 4 5
135 unless ref $names eq 'ARRAY';
136 4 ▶ Carp::croak("resolve_ttl_all: callback must be a CODE reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 3 4
137 unless ref $cb eq 'CODE';
138 3 ▶ Carp::croak("resolve_ttl_all: resolver is destroyed") if $self->is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_destroyed 1 2 3
139 2 ▶ return $cb->({}) unless @$names;
Branch: 1 branch
BranchTrueFalseTotal
unless @$names 1 1 2
140 1 my %seen;
141 1 my @unique = grep { !$seen{$_}++ } @$names;
142 1 my %res;
143 1 my $pending = @unique;
144 1 for my $name (@unique) {
145 $self->resolve_ttl($name, sub {
146 2 ▶ my ($status, @records) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
147 2 $res{$name} = { status => $status, records => \@records };
148 2 ▶ $cb->(\%res) if --$pending == 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$pending == 0 1 1 2
149 2 });
150 }
151 1 return;
152 }
153  
154 sub reverse_all {
155 7 ▶ my ($self, $ips, $cb) = @_;
Subroutine reverse_all: called CC=9 SCAR=22.0
Pod documented
156 7 require Carp;
157 7 ▶ Carp::croak("reverse_all: first argument must be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ips eq "ARRAY" 1 6 7
158 unless ref $ips eq 'ARRAY';
159 6 ▶ Carp::croak("reverse_all: callback must be a CODE reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 5 6
160 unless ref $cb eq 'CODE';
161 5 ▶ Carp::croak("reverse_all: resolver is destroyed") if $self->is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_destroyed 1 4 5
162 4 ▶ return $cb->({}) unless @$ips;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ips 1 3 4
163 3 my %seen;
164 3 my @unique = grep { !$seen{$_}++ } @$ips;
165 # validate every IP upfront. reverse() croaks on invalid input; if we
166 # discovered that mid-loop we would have already dispatched queries 1..k
167 # whose inner callbacks hold $cb and decrement $pending, but $pending
168 # would never reach 0 (we never dispatched k+1..N) -- the completion
169 # callback would be silently orphaned.
170 3 require Socket;
171 3 for my $ip (@unique) {
172 6 ▶ Carp::croak("reverse_all: invalid IP '$ip'")
Branch: 1 branch
BranchTrueFalseTotal
unless Socket::inet_pton(Socket::AF_INET(), $ip) or Socket::inet_pton(Socket::AF_INET6(), $ip) 2 4 6
Condition100%3/3
Socket::inet_pton(Socket::AF_INET(), $ip) or Socket::inet_pton(Socket::AF_INET6(), $ip)
l!l&&r!l&&!r
312
MC/DC100%2/2
Socket::inet_pton(Socket::AF_INET(), $ip) or Socket::inet_pton(Socket::AF_INET6(), $ip)
Socket::inet_pton(Socket::AF_INET(), $ip) Socket::inet_pton(Socket::AF_INET6(), $ip)
Truth table100%3/3
A or B
A= Socket::inet_pton(Socket::AF_INET(), $ip)B= Socket::inet_pton(Socket::AF_INET6(), $ip)
ABresult
1X1
011
000
173 unless Socket::inet_pton(Socket::AF_INET(), $ip)
174 || Socket::inet_pton(Socket::AF_INET6(), $ip);
175 }
176 1 my %res;
177 1 my $pending = @unique;
178 1 for my $ip (@unique) {
179 $self->reverse($ip, sub {
180 2 ▶ my ($status, @hosts) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
181 2 $res{$ip} = { status => $status, hosts => \@hosts };
182 2 ▶ $cb->(\%res) if --$pending == 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$pending == 0 1 1 2
183 2 });
184 }
185 1 return;
186 }
187  
188 sub getaddrinfo_all {
189 5 ▶ my ($self, $nodes, $service, $hints, $cb) = @_;
Subroutine getaddrinfo_all: called CC=6 SCAR=17.9
Pod documented
190 5 require Carp;
191 5 ▶ Carp::croak("getaddrinfo_all: first argument must be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $nodes eq "ARRAY" 1 4 5
192 unless ref $nodes eq 'ARRAY';
193 4 ▶ Carp::croak("getaddrinfo_all: callback must be a CODE reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 1 3 4
194 unless ref $cb eq 'CODE';
195 3 ▶ Carp::croak("getaddrinfo_all: resolver is destroyed") if $self->is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_destroyed 1 2 3
196 2 ▶ return $cb->({}) unless @$nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$nodes 1 1 2
197 1 my %seen;
198 1 my @unique = grep { !$seen{$_}++ } @$nodes;
199 1 my %res;
200 1 my $pending = @unique;
201 1 for my $node (@unique) {
202 $self->getaddrinfo($node, $service, $hints, sub {
203 2 ▶ my ($status, @addrs) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
204 2 $res{$node} = { status => $status, addrs => \@addrs };
205 2 ▶ $cb->(\%res) if --$pending == 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$pending == 0 1 1 2
206 2 });
207 }
208 1 return;
209 }
210  
211 # is_busy: true iff there are pending queries on this resolver. Cheap
212 # wrapper for the most common active_queries comparison.
213 7 ▶sub is_busy { $_[0]->active_queries > 0 }
Subroutine is_busy: called CC=1 SCAR=0.0
Pod documented
214  
215 # wait_idle($timeout_seconds): pump the EV loop until either all of this
216 # resolver's pending queries complete or the timeout elapses. Returns
217 # true if the channel drained, false on timeout. Useful in mostly-
218 # synchronous scripts that want to ensure callbacks have run before
219 # proceeding. Returns immediately if the resolver is already idle.
220 #
221 # Picks up a custom EV::Loop passed to new(loop => $loop): the timer and
222 # the run() call are dispatched on the same loop the resolver's watchers
223 # are armed on. Without this, custom-loop resolvers would hang because
224 # EV::run/EV::timer always target the default loop.
225 sub wait_idle {
226 6 ▶ my ($self, $timeout) = @_;
Subroutine wait_idle: called CC=8 SCAR=21.2
Pod documented
227 6 require Carp;
228 6 ▶ Carp::croak("wait_idle: resolver is destroyed") if $self->is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_destroyed 1 5 6
229 5 ▶ return 1 unless $self->active_queries;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->active_queries 4 1 5
230 1 ▶ $timeout //= 30;
Condition50%1/2
$timeout //= 30
l!l
10
MC/DC0%0/1
$timeout //= 30
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
231 1 my $expired;
232 1 ▶ my $loop = $self->loop // EV::default_loop;
Condition33%1/3
$self->loop // &EV::default_loop()
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->loop // &EV::default_loop()
$self->loop &EV::default_loop()
Truth table33%1/3
A // B
A= $self->loopB= &EV::default_loop()
ABresult
1X1
011
000
233 1 ▶ my $timer = $loop->timer($timeout, 0, sub { $expired = 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
234 1 ▶ while ($self->active_queries && !$expired) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->active_queries and not $expired) 1 1 2
Condition66%2/3
$self->active_queries and not $expired
!ll&&!rl&&r
011
MC/DC50%1/2
$self->active_queries and not $expired
$self->active_queries not $expired
Truth table66%2/3
A and not(B)
A= $self->active_queriesB= not $expired
ABresult
0X0
100
111
235 1 $loop->run(EV::RUN_ONCE);
236 }
237 # Look at the resolver's state rather than $expired: if the timer and
238 # the last query callback both fire in the same RUN_ONCE iteration,
239 # $expired is set but the channel did drain. Don't lie about that.
240 1 ▶ return $self->active_queries ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->active_queries ? : 1 0 1
241 }
242  
243 sub search_all {
244 10 ▶ my ($self, $names, $type, $class_or_cb, $cb) = @_;
Subroutine search_all: called CC=10 SCAR=23.0
Pod documented
245 10 require Carp;
246 # search_all($names, $type, $cb) -> 4 args (class default C_IN)
247 # search_all($names, $type, $class, $cb) -> 5 args
248 10 my $class;
249 10 ▶ if (@_ == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 4) { } 5 5 10
elsif (@_ == 5) { } 4 1 5
250 5 $cb = $class_or_cb;
251 } elsif (@_ == 5) {
252 4 $class = $class_or_cb;
253 4 require Scalar::Util;
254 4 ▶ Carp::croak("search_all: class must be an integer (C_IN, C_CHAOS, ...)")
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($class) 1 3 4
255 unless Scalar::Util::looks_like_number($class);
256 } else {
257 1 Carp::croak("search_all: usage: \$r->search_all(\\\@names, \$type, [\$class,] \$cb)");
258 }
259 8 ▶ Carp::croak("search_all: first argument must be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 1 7 8
260 unless ref $names eq 'ARRAY';
261 7 ▶ Carp::croak("search_all: callback must be a CODE reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 2 5 7
262 unless ref $cb eq 'CODE';
263 5 ▶ Carp::croak("search_all: resolver is destroyed") if $self->is_destroyed;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_destroyed 1 4 5
264 4 ▶ return $cb->({}) unless @$names;
Branch: 1 branch
BranchTrueFalseTotal
unless @$names 2 2 4
265 2 my %seen;
266 2 my @unique = grep { !$seen{$_}++ } @$names;
267 2 my %res;
268 2 my $pending = @unique;
269 2 for my $name (@unique) {
270 my $inner = sub {
271 3 ▶ my ($status, @records) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
272 3 $res{$name} = { status => $status, records => \@records };
273 3 ▶ $cb->(\%res) if --$pending == 0;
Branch: 1 branch
BranchTrueFalseTotal
if --$pending == 0 2 1 3
274 3 };
275 3 ▶ defined $class
Branch: 1 branch
BranchTrueFalseTotal
defined $class ? : 1 2 3
276 ? $self->search($name, $type, $class, $inner)
277 : $self->search($name, $type, $inner);
278 }
279 2 return;
280 }
281  
282 19 1;
283  
284 __END__
↑ Summary cares.xs »