File Coverage

blib/lib/Future/IO/Resolver.pm
Criterion Covered Total %
statement 31 32 96.8
branch 1 2 50.0
condition 1 3 33.3
subroutine 12 12 100.0
pod 4 5 80.0
total 49 54 90.7


line stmt bran cond sub pod time code
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     6   790068 use v5.20;
  6         37  
9 6     6   34 use warnings;
  6         10  
  6         308  
10              
11 6     6   30 use feature qw( postderef signatures );
  6         9  
  6         816  
12 6     6   24 no warnings qw( experimental::postderef experimental::signatures );
  6         8  
  6         165  
13              
14 6     6   2578 use meta;
  6         5185  
  6         231  
15 6     6   32 no warnings 'meta::experimental';
  6         8  
  6         217  
16              
17 6     6   21 use Carp;
  6         8  
  6         2065  
18              
19             =head1 NAME
20              
21             C - name resolver methods for L
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. These are the sorts of
43             things typically performed as part of C 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 to offload the name resolver
50             operations asynchronously. If this is unavailable, it will instead use the
51             regular resolver functions found in L, 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             our @BACKENDS;
59              
60             sub ADD_BACKEND
61             {
62 6     6 0 10 shift;
63 6         15 my ( $backend ) = @_;
64              
65 6         49 my $prio = $backend->RESOLVER_PRIORITY;
66              
67 6         10 my $idx = 0;
68 6   33     30 $idx++ while $idx < scalar @BACKENDS and $prio > $BACKENDS[$idx]->RESOLVER_PRIORITY;
69              
70 6         21 splice @BACKENDS, $idx, 0, ( $backend );
71             }
72              
73             # Attempt to load the distribution-supplied modules
74             require Future::IO::Resolver::Using::Socket;
75             require Future::IO::Resolver::Using::LibAsyncNS;
76              
77             =head1 METHODS
78              
79             =cut
80              
81             my $metapkg = meta::get_this_package;
82              
83             foreach my $method (qw( getaddrinfo getnameinfo res_query res_search )) {
84 4     4 1 621338 $metapkg->add_named_sub( $method => sub ( $, %args ) {
  4     4 1 29  
  4     4 1 10  
        4 1    
85 4         42 foreach my $be ( @BACKENDS ) {
86 4 50       79 $be->can( $method ) and
87             return $be->$method( %args );
88             }
89              
90 0           croak "Cannot find a Future::IO::Resolver backend to handle ->$method";
91             } );
92             }
93              
94             =head2 getaddrinfo
95              
96             @res = await Future::IO::Resolver->getaddrinfo( %args );
97              
98             Perform a C 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 and C key, and may optionally also
103             specify C, C, C, C.
104              
105             The returned list will contain HASH reference structures. Each will provide
106             C, C, C, C and optionally C.
107              
108             =cut
109              
110             =head2 getnameinfo
111              
112             ( $host, $service ) = await Future::IO::Resolver->getnameinfo( %args );
113              
114             Perform a C 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 key and may optionally also specify
119             C.
120              
121             =cut
122              
123             =head2 res_query
124              
125             $answer = Future::IO::Resolver->res_query( %args );
126              
127             Perform a C 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 and C key and may optionally also
133             specify C; though a default of the C 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 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 and C key and may optionally also
147             specify C; though a default of the C 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 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
169              
170             =cut
171              
172             0x55AA;