Resolver/Using/Socket.pm

filter: Statementstmt 74.2% 95 / 128 Branchbran 52.0% 25 / 48 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 84.6% 22 / 26 Podpod 0.0% 0 / 3 totaltotal 66.5% 147 / 221 CC 32 SCAR 16.7
CC
32
Cov
67%
run_in_child
20.0
_serialise1
7.4
_deserialise1
7.4
CRAP 5.3
« Resolver/Using/LibAsyncNS.pm ↑ Summary Resolver.pm »
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::Socket 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 # We don't want to import these as they'll get in the way of our named methods
18 6 ▶use Socket qw(); # getaddrinfo getnameinfo
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 ▶use constant RESOLVER_PRIORITY => 50;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 6 ▶use Future::IO::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 Future::IO::Resolver->ADD_BACKEND( __PACKAGE__ );
24  
25 =head1 NAME
26  
27 C<Future::IO::Resolver::Using::Socket> - implement L<Future::IO::Resolver> by wrapping the functions provided by L<Socket>
28  
29 =head1 DESCRIPTION
30  
31 This module provides a backend implementation for L<Future::IO::Resolver>
32 which uses the regular L<Socket> functions to perform lookups. In order to
33 operate asynchronously, these calls are made from a forked side-car process.
34  
35 This should not be used directly, but is instead made available via the main
36 dispatch methods in C<Future::IO::Resolver> itself.
37  
38 =cut
39  
40 7 ▶sub _serialise ( @values )
Subroutine _serialise: called CC=3 SCAR=12.7
41 {
42 0 ▶ if( !@values ) { return "\0" }
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 0 7 7
43 0 ▶ if( @values > 255 ) { die "Cannot serialise list; too big" }
Branch: 1 branch
BranchTrueFalseTotal
if (@values > 255) 0 7 7
44  
45 return pack( "C", scalar @values ) .
46 7 join( "", map { _serialise1( $_ ) } @values );
47 }
48  
49 39 ▶sub _serialise1 ( $v )
Subroutine _serialise1: called CC=7 SCAR=20.1
50 {
51 39 ▶ if( !defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 10 29 39
52 10 return "U";
53 }
54 29 ▶ my $r = ref $v // "";
Condition50%1/2
ref $v // ""
l!l
290
MC/DC0%0/1
ref $v // ""
ref $v
Truth table50%1/2
A
A= ref $v
Aresult
11
00
55 29 ▶ if( $r eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "ARRAY") 0 29 29
56 0 return "[" . _serialise( @$v );
57 }
58 29 ▶ if( $r eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "HASH") 3 26 29
59 3 return "{" . _serialise( %$v );
60 }
61 26 ▶ if( $v =~ m/^-?\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^-?\d+$/u) { } 6 20 26
62 6 return "i" . pack( "i", $v );
63 }
64 else {
65 20 my $l = length $v;
66 20 ▶ return "s" . pack( "C", $l ) . $v if $l < 256;
Branch: 1 branch
BranchTrueFalseTotal
if $l < 256 20 0 20
67 0 return "S" . pack( "I", $l ) . $v;
68 }
69 }
70  
71 11 ▶sub _deserialise ( $sr )
Subroutine _deserialise: called CC=2 SCAR=6.9
72 {
73 11 my $count = unpack( "C", substr $$sr, 0, 1, "" );
74 11 my @v;
75 11 push @v, _deserialise1( $sr ) for 1 .. $count;
76 11 return @v;
77 }
78  
79 93 ▶sub _deserialise1 ( $sr )
Subroutine _deserialise1: called CC=7 SCAR=20.1
80 {
81 93 my $c = substr( $$sr, 0, 1, "" );
82 93 ▶ if( $c eq "U" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "U") 8 85 93
83 8 return undef;
84 }
85 85 ▶ if( $c eq "{" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "{") 8 77 85
86 return +{
87 8 _deserialise( $sr )
88 };
89 }
90 77 ▶ if( $c eq "i" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "i") 28 49 77
91 28 return unpack( "i", substr( $$sr, 0, 4, "" ) );
92 }
93 49 ▶ if( $c eq "s" or $c eq "S" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "s" or $c eq "S") 49 0 49
Condition33%1/3
$c eq "s" or $c eq "S"
l!l&&r!l&&!r
4900
MC/DC0%0/2
$c eq "s" or $c eq "S"
$c eq "s" $c eq "S"
Truth table33%1/3
A or B
A= $c eq "s"B= $c eq "S"
ABresult
1X1
011
000
94 49 ▶ my $l = ( $c eq "s" ) ? unpack( "C", substr( $$sr, 0, 1, "" ) ) :
Branch: 1 branch
BranchTrueFalseTotal
$c eq 's' ? : 49 0 49
95 unpack( "I", substr( $$sr, 0, 4, "" ) );
96 49 return substr( $$sr, 0, $l, "" );
97 }
98  
99 0 die "TODO: deserialise type $c";
100 }
101  
102 6 my %resolvers;
103  
104 0 ▶sub run_in_child ( $rd, $wr )
Subroutine run_in_child: not called CC=4 SCAR=30.0
Pod undocumented
105 {
106 0 ▶ while( !eof $rd ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not eof $rd) 0 0 0
107 0 $rd->read( my $buf, 4 );
108 0 my $len = unpack "I", $buf;
109  
110 0 $buf = "";
111 0 ▶ $rd->read( $buf, $len - length $buf, length $buf ) while $len > length $buf;
Branch: 1 branch
BranchTrueFalseTotal
$len > length $buf and do { $rd->read($buf, $len - length($buf), length $buf) } 0 0 0
112  
113 0 my ( $func, @args ) = _deserialise( \$buf );
114  
115 0 my $code = $resolvers{$func};
116 0 ▶ if( $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 0 0 0
117 0 my @result = $code->( @args );
118  
119 0 $wr->print( pack "I/a*", _serialise( @result ) );
120 }
121 else {
122 0 $wr->print( pack "I/a*", _serialise( -1, "Unrecognised resolver func '$func'" ) );
123 }
124 }
125 }
126  
127 my $runf;
128 6 my $wrpipe;
129 6 my @result_queue;
130  
131 2 ▶sub _start ()
Subroutine _start: called CC=5 SCAR=18.2
132 {
133 2 ▶ pipe( my $child_rd, $wrpipe ) or
Branch: 1 branch
BranchTrueFalseTotal
pipe my $child_rd, $wrpipe or die "Cannot pipe() - $!" 2 0 2
134 die "Cannot pipe() - $!";
135 2 ▶ pipe( my $rdpipe, my $child_wr ) or
Branch: 1 branch
BranchTrueFalseTotal
pipe my $rdpipe, my $child_wr or die "Cannot pipe() - $!" 2 0 2
136 die "Cannot pipe() - $!";
137 2 $rdpipe->blocking(0);
138 2 $wrpipe->blocking(0);
139 2 $wrpipe->autoflush(1);
140  
141 2 ▶ defined( my $pid = fork() ) or
Branch: 1 branch
BranchTrueFalseTotal
defined(my $pid = fork) or die "Cannot fork() - $!" 2 0 2
142 die "Cannot fork() - $!";
143  
144 2 ▶ if( !$pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 2 2
145 # child
146 0 undef $wrpipe;
147 0 undef $rdpipe;
148  
149 0 $child_wr->autoflush(1);
150  
151 0 run_in_child( $child_rd, $child_wr );
152  
153 0 POSIX::_exit(5);
154 }
155  
156 # parent
157 2 undef $child_rd;
158 2 undef $child_wr;
159  
160 my $waitf = Future::IO->waitpid( $pid )
161 0 ▶ ->then( sub ( $wstatus ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
162 0 warn my $msg = "Future::IO::Resolver::Using::Socket child process died: $wstatus\n";
163 0 $_->fail( $msg ) for @result_queue;
164 0 undef $runf;
165 2 } );
166  
167 6 ▶ my $recvf = Future::Utils::repeat {
Subroutine __ANON__: called CC=1 SCAR=0.1
168 4 ▶ Future::IO->read_exactly( $rdpipe, 4 )->then( sub ( $buf ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
169 4 my $len = unpack "I", $buf;
170 4 Future::IO->read_exactly( $rdpipe, $len );
171 4 ▶ })->then( sub ( $buf ) {
Subroutine __ANON__: called CC=3 SCAR=11.7
172 4 ▶ my $f = shift @result_queue or
Branch: 1 branch
BranchTrueFalseTotal
my $f = shift @result_queue or warn "ARGH no result future!" 4 0 4
173 warn "ARGH no result future!";
174 4 ▶ $f->done( $buf ) if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 4 0 4
175 4 Future->done;
176 6 });
177 2 ▶ } while => sub ( $f ) { !$f->failure };
Subroutine __ANON__: called CC=1 SCAR=0.0
178  
179 2 return Future->wait_any( $waitf, $recvf );
180 }
181  
182 4 ▶sub _call ( $func, @args )
Subroutine _call: called CC=2 SCAR=7.2
183 {
184 4 ▶ $runf //= _start();
Condition66%2/3
$runf //= _start()
l!l&&r!l&&!r
220
MC/DC0%0/2
$runf //= _start()
$runf _start()
Truth table66%2/3
A //= B
A= $runfB= _start()
ABresult
1X1
011
000
185  
186 4 $wrpipe->print( pack "I/a*", _serialise( $func, @args ) );
187  
188 4 push @result_queue, my $f = $runf->new;
189 3 ▶ return $f->then( sub ( $buf ) {
Subroutine __ANON__: called CC=2 SCAR=7.6
190 3 my ( $err, @result ) = _deserialise( \$buf );
191 3 ▶ if( !$err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $err) { } 3 0 3
192 3 return Future->done( @result );
193 }
194 else {
195 0 return Future->fail( "$result[0]\n", $func => );
196 }
197 4 } );
198 }
199  
200 0 ▶$resolvers{getaddrinfo} = sub ( @args ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
201 0 my ( $err, @res ) = Socket::getaddrinfo( @args );
202 0 ▶ if( $err ) { return ( $err+0, "$err" ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 0 0
203 0 else { return ( 0, @res ); }
204 6 };
205  
206 3 ▶sub getaddrinfo ( $, %args )
Subroutine getaddrinfo: called CC=2 SCAR=6.9
Pod undocumented
207 {
208 3 my $host = delete $args{host};
209 3 my $service = delete $args{service};
210 3 my %hints;
211 3 $hints{$_} = delete $args{$_} for qw( family socktype protocol flags );
212  
213 3 return _call( getaddrinfo => $host, $service, \%hints );
214 }
215  
216 0 ▶$resolvers{getnameinfo} = sub ( @args ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
217 0 my ( $err, @res ) = Socket::getnameinfo( @args );
218 0 ▶ if( $err ) { return ( $err+0, "$err" ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 0 0 0
219 0 else { return ( 0, @res ); }
220 6 };
221  
222 1 ▶sub getnameinfo ( $, %args )
Subroutine getnameinfo: called CC=2 SCAR=7.1
Pod undocumented
223 {
224 1 my $addr = delete $args{addr};
225 1 ▶ my $flags = delete $args{flags} // 0;
Condition50%1/2
delete $args{"flags"} // 0
l!l
01
MC/DC0%0/1
delete $args{"flags"} // 0
delete $args{"flags"}
Truth table50%1/2
A
A= delete $args{"flags"}
Aresult
11
00
226  
227 1 return _call( getnameinfo => $addr, $flags );
228 }
229  
230 =head1 TODO
231  
232 =over 4
233  
234 =item *
235  
236 The C<pipe()> + C<fork()> mechanism used internally should be extracted into a
237 shared helper module, as none of it is specific to name resolvers. It can then
238 be expanded to permit possibly multiple workers, etc. This would be similar to
239 the way that L<IO::Async::Resolver> uses L<IO::Async::Function>.
240  
241 =back
242  
243 =cut
244  
245 =head1 AUTHOR
246  
247 Paul Evans <leonerd@leonerd.org.uk>
248  
249 =cut
250  
251 6 0x55AA;
« Resolver/Using/LibAsyncNS.pm ↑ Summary Resolver.pm »