Resolver.pm

filter: Statementstmt 86.6% 39 / 45 Branchbran 20.0% 2 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 9 / 12 Podpod 75.0% 3 / 4 totaltotal 71.0% 54 / 76 CC 11 SCAR 13.2
CC
11
Cov
72%
__ANON__
5.5
getaddrinfo
5.5
res_query
5.5
CRAP 3.7
« Resolver/Using/Socket.pm ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2026 -- leonerd@leonerd.org.uk
5  
6 package Future::IO::Resolver 0.04;
7  
8 6 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶no warnings 'meta::experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 C<Future::IO::Resolver> - name resolver methods for L<Future::IO>
22  
23 =head1 SYNOPSIS
24  
25 =for highlighter language=perl
26  
27 use Future::IO;
28 use Future::IO::Resolver;
29  
30 use Socket qw( SOCK_STREAM );
31  
32 my $f = Future::IO::Resolver->getaddrinfo(
33 host => "metacpan.org",
34 service => "http",
35 socktype => SOCK_STREAM,
36 );
37 # when complete, $f will yield socket address structures
38  
39 =head1 DESCRIPTION
40  
41 This package contains a selection of methods for performing name resolver
42 queries, running asynchronously via L<Future::IO>. These are the sorts of
43 things typically performed as part of C<connect> attempts or other operations
44 where names have to be turned into numerical address structures, which may
45 involve communication with the outside world.
46  
47 =head2 Implementation Details
48  
49 This module can optionally use L<Net::LibAsyncNS> to offload the name resolver
50 operations asynchronously. If this is unavailable, it will instead use the
51 regular resolver functions found in L<Socket>, made asynchronous by calling
52 those functions from a forked side-car process. This is Implementated by a
53 flexible plugin system that allows other backends to be provided as well, as
54 may be found in other CPAN modules.
55  
56 =cut
57  
58 1 our @BACKENDS;
59  
60 sub ADD_BACKEND
61 6 ▶{
Subroutine ADD_BACKEND: called CC=3 SCAR=11.6
Pod undocumented
62 6 shift;
63 6 my ( $backend ) = @_;
64  
65 6 my $prio = $backend->RESOLVER_PRIORITY;
66  
67 6 my $idx = 0;
68 6 ▶ $idx++ while $idx < scalar @BACKENDS and $prio > $BACKENDS[$idx]->RESOLVER_PRIORITY;
Branch: 1 branch
BranchTrueFalseTotal
$idx < scalar @Future::IO::Resolver::BACKENDS and $prio > $Future::IO::Resolver::BACKENDS[$idx]->RESOLVER_PRIORITY and do { ++$idx } 0 6 6
Condition33%1/3
$idx < scalar @Future::IO::Resolver::BACKENDS and $prio > $Future::IO::Resolver::BACKENDS[$idx]->RESOLVER_PRIORITY
!ll&&!rl&&r
600
MC/DC0%0/2
$idx < scalar @Future::IO::Resolver::BACKENDS and $prio > $Future::IO::Resolver::BACKENDS[$idx]->RESOLVER_PRIORITY
$idx < scalar @Future::IO::Resolver::BACKENDS $prio > $Future::IO::Resolver::BACKENDS[$idx]->RESOLVER_PRIORITY
Truth table33%1/3
A and B
A= $idx < scalar @Future::IO::Resolver::BACKENDSB= $prio > $Future::IO::Resolver::BACKENDS[$idx]->RESOLVER_PRIORITY
ABresult
0X0
100
111
69  
70 6 splice @BACKENDS, $idx, 0, ( $backend );
71 }
72  
73 # Attempt to load the distribution-supplied modules
74 1 require Future::IO::Resolver::Using::Socket;
75 1 require Future::IO::Resolver::Using::LibAsyncNS;
76  
77 =head1 METHODS
78  
79 =cut
80  
81 1 my $metapkg = meta::get_this_package;
82  
83 1 foreach my $method (qw( getaddrinfo getnameinfo res_query res_search )) {
84 0 ▶ $metapkg->add_named_sub( $method => sub ( $, %args ) {
Subroutine res_query: not called CC=3 SCAR=17.0
Subroutine res_search: not called CC=3 SCAR=17.0
Subroutine getaddrinfo: called CC=3 SCAR=17.0
Subroutine __ANON__: not called CC=3 SCAR=17.0
Pod documented
Pod documented
Pod documented
85 0 foreach my $be ( @BACKENDS ) {
86 0 ▶ $be->can( $method ) and
Branch: 4 branches
BranchTrueFalseTotal
$be->can($method) and return $be->$method(%args) 0 0 0
$be->can($method) and return $be->$method(%args) 0 0 0
$be->can($method) and return $be->$method(%args) 4 0 4
$be->can($method) and return $be->$method(%args) 0 0 0
87 return $be->$method( %args );
88 }
89  
90 0 croak "Cannot find a Future::IO::Resolver backend to handle ->$method";
91 4 } );
92 }
93  
94 =head2 getaddrinfo
95  
96 @res = await Future::IO::Resolver->getaddrinfo( %args );
97  
98 Perform a C<getaddrinfo> resolve operation, which converts human-readable
99 descriptions of network addresses into socket-layer parameters and address
100 structures.
101  
102 C<%args> should contain a C<host> and C<service> key, and may optionally also
103 specify C<family>, C<socktype>, C<protocol>, C<flags>.
104  
105 The returned list will contain HASH reference structures. Each will provide
106 C<family>, C<socktype>, C<protocol>, C<addr> and optionally C<canonname>.
107  
108 =cut
109  
110 =head2 getnameinfo
111  
112 ( $host, $service ) = await Future::IO::Resolver->getnameinfo( %args );
113  
114 Perform a C<getnameinfo> resolve operation, which converts socket-layer
115 address structures into human-readable description strings containing names
116 or numbers.
117  
118 C<%args> should contain a C<addr> key and may optionally also specify
119 C<flags>.
120  
121 =cut
122  
123 =head2 res_query
124  
125 $answer = Future::IO::Resolver->res_query( %args );
126  
127 Perform a C<res_query> resolve operation, which looks up DNS records of
128 various types, returning an answer in the form of a packed byte record. Code
129 using this method will need to understand how to unpack a DNS record from this
130 format.
131  
132 C<%args> should contain a C<dname> and C<type> key and may optionally also
133 specify C<class>; though a default of the C<IN> class is applied.
134  
135 =cut
136  
137 =head2 res_search
138  
139 $answer = Future::IO::Resolver->res_search( %args );
140  
141 Perform a C<res_search> resolve operation, which looks up DNS records of
142 various types, returning an answer in the form of a packed byte record. Code
143 using this method will need to understand how to unpack a DNS record from this
144 format.
145  
146 C<%args> should contain a C<dname> and C<type> key and may optionally also
147 specify C<class>; though a default of the C<IN> class is applied.
148  
149 =cut
150  
151 =head1 TODO
152  
153 =over 4
154  
155 =item *
156  
157 Some wrapping of other resolvers, like the POSIX C<get*ent> family.
158  
159 =item *
160  
161 Add support for direct DNS-based resolving behaviour; possibly in its own
162 CPAN distribution.
163  
164 =back
165  
166 =head1 AUTHOR
167  
168 Paul Evans <leonerd@leonerd.org.uk>
169  
170 =cut
171  
172 1 0x55AA;
« Resolver/Using/Socket.pm ↑ Summary