| 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::Using::LibAsyncNS 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 Future::IO 0.19 qw( POLLIN ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 6 | ▶ | use Future::Utils qw( repeat ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | 6 | ▶ | use constant HAVE_LIBASYNCNS => eval { require Net::LibAsyncNS; }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 6 | ▶ | use constant RESOLVER_PRIORITY => 25; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | 6 | ▶ | use Future::IO::Resolver; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 6 | ▶ | HAVE_LIBASYNCNS and | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | Future::IO::Resolver->ADD_BACKEND( __PACKAGE__ ); | ||||||||||||
| 24 | |||||||||||||
| 25 | 6 | ▶ | use constant NS_CLASS_IN => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | |||||||||||||
| 27 | 6 | my $asyncns; | |||||||||||
| 28 | my $runf; | ||||||||||||
| 29 | 6 | my $pending_failuref; | |||||||||||
| 30 | |||||||||||||
| 31 | =head1 NAME | ||||||||||||
| 32 | |||||||||||||
| 33 | C<Future::IO::Resolver::Using::LibAsyncNS> - implement L<Future::IO::Resolver> using F<libasyncns> | ||||||||||||
| 34 | |||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||
| 36 | |||||||||||||
| 37 | This module provides a backend implementation for L<Future::IO::Resolver> | ||||||||||||
| 38 | which uses F<libasyncns> (via the L<Net::LibAsyncNS> module) to perform its | ||||||||||||
| 39 | lookups. | ||||||||||||
| 40 | |||||||||||||
| 41 | This should not be used directly, but is instead made available via the main | ||||||||||||
| 42 | dispatch methods in C<Future::IO::Resolver> itself. | ||||||||||||
| 43 | |||||||||||||
| 44 | =cut | ||||||||||||
| 45 | |||||||||||||
| 46 | 0 | ▶ | sub _asyncns () | ||||||||||
|
Subroutine
_asyncns: not called CC=2 SCAR=17.9
| |||||||||||||
| 47 | { | ||||||||||||
| 48 | 0 | ▶ | return $asyncns if $asyncns; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | |||||||||||||
| 50 | 0 | $asyncns = Net::LibAsyncNS->new( 1 ); | |||||||||||
| 51 | |||||||||||||
| 52 | 0 | my $fh = $asyncns->new_handle_for_fd; | |||||||||||
| 53 | |||||||||||||
| 54 | ## This would be much neater with async/await | ||||||||||||
| 55 | 0 | ▶ | $runf = Future::Utils::repeat { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 56 | 0 | ▶ | Future::IO->poll( $fh, POLLIN )->then( sub ( $ ) { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 57 | 0 | ▶ | $asyncns->wait( 0 ) or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | warn( "Future::IO::Resolver::Using::LibAsyncNS IO failure $!\n" ), return Future->done; | ||||||||||||
| 59 | |||||||||||||
| 60 | 0 | ▶ | while( my $q = $asyncns->getnext ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | my $f = $q->getuserdata; | |||||||||||
| 62 | 0 | $f->done( $q ); | |||||||||||
| 63 | } | ||||||||||||
| 64 | 0 | Future->done; | |||||||||||
| 65 | } ) | ||||||||||||
| 66 | 0 | ▶ | } while => sub ( $f ) { !$f->failure }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 67 | |||||||||||||
| 68 | 0 | ▶ | $runf->on_fail( sub ( $err, @ ) { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 69 | 0 | say "Future::IO::Resolver::Using::LibAsyncNS run future failed - $err"; | |||||||||||
| 70 | 0 | $pending_failuref = $runf; | |||||||||||
| 71 | 0 | undef $runf; | |||||||||||
| 72 | 0 | } ); | |||||||||||
| 73 | |||||||||||||
| 74 | 0 | return $asyncns; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | sub getaddrinfo ( $, %args ) | ||||||||||
|
Subroutine
getaddrinfo: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 78 | { | ||||||||||||
| 79 | 0 | my $asyncns = _asyncns(); | |||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | if( $pending_failuref ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 0 | my $f = $pending_failuref; | |||||||||||
| 83 | 0 | undef $pending_failuref; | |||||||||||
| 84 | 0 | return $f; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | my $host = delete $args{host}; | |||||||||||
| 88 | 0 | my $service = delete $args{service}; | |||||||||||
| 89 | |||||||||||||
| 90 | 0 | my $q = $asyncns->getaddrinfo( $host, $service, \%args ); | |||||||||||
| 91 | |||||||||||||
| 92 | 0 | my $f = $runf->new; | |||||||||||
| 93 | 0 | $q->setuserdata( $f ); | |||||||||||
| 94 | |||||||||||||
| 95 | 0 | ▶ | my $queryf = $f->then( sub ( $q ) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 96 | 0 | my ( $err, @res ) = $asyncns->getaddrinfo_done( $q ); | |||||||||||
| 97 | |||||||||||||
| 98 | 0 | ▶ | if( !$err ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | 0 | Future->done( @res ); | |||||||||||
| 100 | } | ||||||||||||
| 101 | else { | ||||||||||||
| 102 | 0 | Future->fail( "$err\n", getaddrinfo => ); | |||||||||||
| 103 | } | ||||||||||||
| 104 | 0 | ▶ | })->on_cancel( sub ( $ ) { $asyncns->cancel( $q ); } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 105 | |||||||||||||
| 106 | 0 | return Future->wait_any( $queryf, $runf->without_cancel ); | |||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | 0 | ▶ | sub getnameinfo ( $, %args ) | ||||||||||
|
Subroutine
getnameinfo: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 110 | { | ||||||||||||
| 111 | 0 | my $asyncns = _asyncns(); | |||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | if( $pending_failuref ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | my $f = $pending_failuref; | |||||||||||
| 115 | 0 | undef $pending_failuref; | |||||||||||
| 116 | 0 | return $f; | |||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | 0 | my $addr = delete $args{addr}; | |||||||||||
| 120 | 0 | ▶ | my $flags = delete $args{flags} // 0; | ||||||||||
|
Condition0%0/2
delete $args{"flags"} // 0
MC/DC0%0/1
delete $args{"flags"} // 0
delete $args{"flags"}
Truth table0%0/2
A
A= delete $args{"flags"}
| |||||||||||||
| 121 | |||||||||||||
| 122 | 0 | my $q = $asyncns->getnameinfo( $addr, $flags, 1, 1 ); | |||||||||||
| 123 | |||||||||||||
| 124 | 0 | my $f = $runf->new; | |||||||||||
| 125 | 0 | $q->setuserdata( $f ); | |||||||||||
| 126 | |||||||||||||
| 127 | 0 | ▶ | my $queryf = $f->then( sub ( $q ) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 128 | 0 | my ( $err, $host, $service ) = $asyncns->getnameinfo_done( $q ); | |||||||||||
| 129 | |||||||||||||
| 130 | 0 | ▶ | if( !$err ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 0 | Future->done( $host, $service ); | |||||||||||
| 132 | } | ||||||||||||
| 133 | else { | ||||||||||||
| 134 | 0 | Future->fail( "$err\n", getnameinfo => ); | |||||||||||
| 135 | } | ||||||||||||
| 136 | 0 | ▶ | })->on_cancel( sub ( $ ) { $asyncns->cancel( $q ); } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 137 | |||||||||||||
| 138 | 0 | return Future->wait_any( $queryf, $runf->without_cancel ); | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 0 | ▶ | sub res_query ( $, %args ) | ||||||||||
|
Subroutine
res_query: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 142 | { | ||||||||||||
| 143 | 0 | my $asyncns = _asyncns(); | |||||||||||
| 144 | |||||||||||||
| 145 | 0 | ▶ | if( $pending_failuref ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | 0 | my $f = $pending_failuref; | |||||||||||
| 147 | 0 | undef $pending_failuref; | |||||||||||
| 148 | 0 | return $f; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | my $dname = delete $args{dname}; | |||||||||||
| 152 | 0 | ▶ | my $class = delete $args{class} // NS_CLASS_IN; | ||||||||||
|
Condition0%0/2
delete $args{"class"} // 1
MC/DC0%0/1
delete $args{"class"} // 1
delete $args{"class"}
Truth table0%0/2
A
A= delete $args{"class"}
| |||||||||||||
| 153 | 0 | my $type = delete $args{type}; | |||||||||||
| 154 | |||||||||||||
| 155 | 0 | my $q = $asyncns->res_query( $dname, $class, $type ); | |||||||||||
| 156 | |||||||||||||
| 157 | 0 | my $f = $runf->new; | |||||||||||
| 158 | 0 | $q->setuserdata( $f ); | |||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | my $queryf = $f->then( sub ( $q ) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 161 | 0 | my $answer = $asyncns->res_done( $q ); | |||||||||||
| 162 | |||||||||||||
| 163 | 0 | ▶ | if( defined $answer ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 164 | 0 | Future->done( $answer ); | |||||||||||
| 165 | } | ||||||||||||
| 166 | else { | ||||||||||||
| 167 | 0 | Future->fail( "$!", res_query => ); | |||||||||||
| 168 | } | ||||||||||||
| 169 | 0 | ▶ | })->on_cancel( sub ( $ ) { $asyncns->cancel( $q ); } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 170 | |||||||||||||
| 171 | 0 | return Future->wait_any( $queryf, $runf->without_cancel ); | |||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 0 | ▶ | sub res_search ( $, %args ) | ||||||||||
|
Subroutine
res_search: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 175 | { | ||||||||||||
| 176 | 0 | my $asyncns = _asyncns(); | |||||||||||
| 177 | |||||||||||||
| 178 | 0 | ▶ | if( $pending_failuref ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 179 | 0 | my $f = $pending_failuref; | |||||||||||
| 180 | 0 | undef $pending_failuref; | |||||||||||
| 181 | 0 | return $f; | |||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | 0 | my $dname = delete $args{dname}; | |||||||||||
| 185 | 0 | ▶ | my $class = delete $args{class} // NS_CLASS_IN; | ||||||||||
|
Condition0%0/2
delete $args{"class"} // 1
MC/DC0%0/1
delete $args{"class"} // 1
delete $args{"class"}
Truth table0%0/2
A
A= delete $args{"class"}
| |||||||||||||
| 186 | 0 | my $type = delete $args{type}; | |||||||||||
| 187 | |||||||||||||
| 188 | 0 | my $q = $asyncns->res_search( $dname, $class, $type ); | |||||||||||
| 189 | |||||||||||||
| 190 | 0 | my $f = $runf->new; | |||||||||||
| 191 | 0 | $q->setuserdata( $f ); | |||||||||||
| 192 | |||||||||||||
| 193 | 0 | ▶ | my $queryf = $f->then( sub ( $q ) { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 194 | 0 | my $answer = $asyncns->res_done( $q ); | |||||||||||
| 195 | |||||||||||||
| 196 | 0 | ▶ | if( defined $answer ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 197 | 0 | Future->done( $answer ); | |||||||||||
| 198 | } | ||||||||||||
| 199 | else { | ||||||||||||
| 200 | 0 | Future->fail( "$!", res_search => ); | |||||||||||
| 201 | } | ||||||||||||
| 202 | 0 | ▶ | })->on_cancel( sub ( $ ) { $asyncns->cancel( $q ); } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 203 | |||||||||||||
| 204 | 0 | return Future->wait_any( $queryf, $runf->without_cancel ); | |||||||||||
| 205 | } | ||||||||||||
| 206 | |||||||||||||
| 207 | =head1 AUTHOR | ||||||||||||
| 208 | |||||||||||||
| 209 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 210 | |||||||||||||
| 211 | =cut | ||||||||||||
| 212 | |||||||||||||
| 213 | 6 | 0x55AA; | |||||||||||