| 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 | 19 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 19 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 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 | 11 | ▶ | sub ptr_name { | ||||||||||||||||||||||||||
|
Subroutine
ptr_name: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 11 | my ($ip) = @_; | |||||||||||||||||||||||||||
| 62 | 11 | require Carp; | |||||||||||||||||||||||||||
| 63 | 11 | ▶ | Carp::croak("ptr_name: missing IP") unless defined $ip; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 10 | ▶ | if (my @oct = $ip =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 6 | for my $o (@oct) { | |||||||||||||||||||||||||||
| 66 | 18 | ▶ | Carp::croak("ptr_name: leading-zero octet '$o' is ambiguous (octal vs decimal)") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $o > 1 and $o =~ /^0/u
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
| |||||||||||||||||||||||||||||
| 67 | if length($o) > 1 && $o =~ /^0/; | ||||||||||||||||||||||||||||
| 68 | 17 | ▶ | Carp::croak("ptr_name: invalid IPv4 octet '$o'") if $o > 255; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 4 | return "$oct[3].$oct[2].$oct[1].$oct[0].in-addr.arpa"; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if ($ip =~ /:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | require Socket; | |||||||||||||||||||||||||||
| 74 | 3 | ▶ | my $packed = Socket::inet_pton(Socket::AF_INET6(), $ip) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub parse_header { | ||||||||||||||||||||||||||
|
Subroutine
parse_header: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 7 | my ($buf) = @_; | |||||||||||||||||||||||||||
| 86 | 7 | require Carp; | |||||||||||||||||||||||||||
| 87 | 7 | ▶ | Carp::croak("parse_header: buffer too short") if length($buf) < 12; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub resolve_all { | ||||||||||||||||||||||||||
|
Subroutine
resolve_all: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 7 | my ($self, $names, $cb) = @_; | |||||||||||||||||||||||||||
| 110 | 7 | require Carp; | |||||||||||||||||||||||||||
| 111 | 7 | ▶ | Carp::croak("resolve_all: first argument must be an arrayref") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | unless ref $names eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 113 | 5 | ▶ | Carp::croak("resolve_all: callback must be a CODE reference") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | unless ref $cb eq 'CODE'; | ||||||||||||||||||||||||||||
| 115 | 4 | ▶ | Carp::croak("resolve_all: resolver is destroyed") if $self->is_destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 3 | ▶ | return $cb->({}) unless @$names; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 3 | ▶ | $self->resolve($name, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 3 | my ($status, @addrs) = @_; | |||||||||||||||||||||||||||
| 124 | 3 | $res{$name} = { status => $status, addrs => \@addrs }; | |||||||||||||||||||||||||||
| 125 | 3 | ▶ | $cb->(\%res) if --$pending == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 3 | }); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 2 | return; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | ▶ | sub resolve_ttl_all { | ||||||||||||||||||||||||||
|
Subroutine
resolve_ttl_all: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 132 | 5 | my ($self, $names, $cb) = @_; | |||||||||||||||||||||||||||
| 133 | 5 | require Carp; | |||||||||||||||||||||||||||
| 134 | 5 | ▶ | Carp::croak("resolve_ttl_all: first argument must be an arrayref") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | unless ref $names eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 136 | 4 | ▶ | Carp::croak("resolve_ttl_all: callback must be a CODE reference") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | unless ref $cb eq 'CODE'; | ||||||||||||||||||||||||||||
| 138 | 3 | ▶ | Carp::croak("resolve_ttl_all: resolver is destroyed") if $self->is_destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 2 | ▶ | return $cb->({}) unless @$names; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | $self->resolve_ttl($name, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 146 | 2 | my ($status, @records) = @_; | |||||||||||||||||||||||||||
| 147 | 2 | $res{$name} = { status => $status, records => \@records }; | |||||||||||||||||||||||||||
| 148 | 2 | ▶ | $cb->(\%res) if --$pending == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 2 | }); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 1 | return; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 7 | ▶ | sub reverse_all { | ||||||||||||||||||||||||||
|
Subroutine
reverse_all: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 7 | my ($self, $ips, $cb) = @_; | |||||||||||||||||||||||||||
| 156 | 7 | require Carp; | |||||||||||||||||||||||||||
| 157 | 7 | ▶ | Carp::croak("reverse_all: first argument must be an arrayref") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | unless ref $ips eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 159 | 6 | ▶ | Carp::croak("reverse_all: callback must be a CODE reference") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | unless ref $cb eq 'CODE'; | ||||||||||||||||||||||||||||
| 161 | 5 | ▶ | Carp::croak("reverse_all: resolver is destroyed") if $self->is_destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 4 | ▶ | return $cb->({}) unless @$ips; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
Socket::inet_pton(Socket::AF_INET(), $ip) or Socket::inet_pton(Socket::AF_INET6(), $ip)
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)
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | $self->reverse($ip, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 180 | 2 | my ($status, @hosts) = @_; | |||||||||||||||||||||||||||
| 181 | 2 | $res{$ip} = { status => $status, hosts => \@hosts }; | |||||||||||||||||||||||||||
| 182 | 2 | ▶ | $cb->(\%res) if --$pending == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 2 | }); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 1 | return; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 5 | ▶ | sub getaddrinfo_all { | ||||||||||||||||||||||||||
|
Subroutine
getaddrinfo_all: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | 5 | my ($self, $nodes, $service, $hints, $cb) = @_; | |||||||||||||||||||||||||||
| 190 | 5 | require Carp; | |||||||||||||||||||||||||||
| 191 | 5 | ▶ | Carp::croak("getaddrinfo_all: first argument must be an arrayref") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | unless ref $nodes eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 193 | 4 | ▶ | Carp::croak("getaddrinfo_all: callback must be a CODE reference") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | unless ref $cb eq 'CODE'; | ||||||||||||||||||||||||||||
| 195 | 3 | ▶ | Carp::croak("getaddrinfo_all: resolver is destroyed") if $self->is_destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | return $cb->({}) unless @$nodes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | $self->getaddrinfo($node, $service, $hints, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 203 | 2 | my ($status, @addrs) = @_; | |||||||||||||||||||||||||||
| 204 | 2 | $res{$node} = { status => $status, addrs => \@addrs }; | |||||||||||||||||||||||||||
| 205 | 2 | ▶ | $cb->(\%res) if --$pending == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 6 | ▶ | sub wait_idle { | ||||||||||||||||||||||||||
|
Subroutine
wait_idle: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 6 | my ($self, $timeout) = @_; | |||||||||||||||||||||||||||
| 227 | 6 | require Carp; | |||||||||||||||||||||||||||
| 228 | 6 | ▶ | Carp::croak("wait_idle: resolver is destroyed") if $self->is_destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 5 | ▶ | return 1 unless $self->active_queries; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 1 | ▶ | $timeout //= 30; | ||||||||||||||||||||||||||
|
Condition50%1/2
$timeout //= 30
MC/DC0%0/1
$timeout //= 30
$timeout
Truth table50%1/2
A
A= $timeout
| |||||||||||||||||||||||||||||
| 231 | 1 | my $expired; | |||||||||||||||||||||||||||
| 232 | 1 | ▶ | my $loop = $self->loop // EV::default_loop; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->loop // &EV::default_loop()
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()
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->active_queries and not $expired
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 10 | ▶ | sub search_all { | ||||||||||||||||||||||||||
|
Subroutine
search_all: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 244 | 10 | my ($self, $names, $type, $class_or_cb, $cb) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 260 | unless ref $names eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 261 | 7 | ▶ | Carp::croak("search_all: callback must be a CODE reference") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | unless ref $cb eq 'CODE'; | ||||||||||||||||||||||||||||
| 263 | 5 | ▶ | Carp::croak("search_all: resolver is destroyed") if $self->is_destroyed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 4 | ▶ | return $cb->({}) unless @$names; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 3 | ▶ | my $inner = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 271 | 3 | my ($status, @records) = @_; | |||||||||||||||||||||||||||
| 272 | 3 | $res{$name} = { status => $status, records => \@records }; | |||||||||||||||||||||||||||
| 273 | 3 | ▶ | $cb->(\%res) if --$pending == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 3 | }; | |||||||||||||||||||||||||||
| 275 | 3 | ▶ | defined $class | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||