blib/lib/IO/Async/Internals/Connector.pm

filter: Statementstmt 79.4% 112 / 141 Branchbran 47.3% 35 / 74 Conditioncond 60.7% 34 / 56 MC/DCmcdc 18.1% 6 / 33 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 2 totaltotal 63.0% 205 / 325 CC 61 SCAR 33.8
CC
61
Cov
67%
connect
64.5
__ANON__
18.3
__ANON__
12.8
CRAP 29.5
« blib/lib/IO/Async/Debug.pm ↑ Summary blib/lib/Future/IO/Impl/IOAsync.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, 2008-2024 -- leonerd@leonerd.org.uk
5  
6 package # hide from CPAN
7 IO::Async::Internals::Connector 0.802;
8  
9 3 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Scalar::Util qw( weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use POSIX qw( EINPROGRESS );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Socket qw( SOL_SOCKET SO_ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use Future 0.44; # ->result
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Future::Utils 0.18 qw( try_repeat_until_success );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use IO::Async::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 use constant {
25 3 CONNECT_EWOULDLBOCK => IO::Async::OS->HAVE_CONNECT_EWOULDBLOCK,
26 HAVE_SOCKADDR_IN6 => IO::Async::OS->HAVE_SOCKADDR_IN6,
27 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Internal constructor
30 sub new
31 {
32 3 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
33 3 my ( %params ) = @_;
34  
35 3 ▶ my $loop = delete $params{loop} or croak "Expected a 'loop'";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = delete $params{"loop"} or croak("Expected a 'loop'") 0 3 3
36  
37 3 my $self = bless {}, $class;
38 3 weaken( $self->{loop} = $loop );
39  
40 3 return $self;
41 }
42  
43 ## Utility function
44 sub _get_sock_err
45 {
46 11 ▶ my ( $sock ) = @_;
Subroutine _get_sock_err: called CC=5 SCAR=24.1
47  
48 11 my $err = $sock->getsockopt( SOL_SOCKET, SO_ERROR );
49  
50 11 ▶ if( defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 11 0 11
51 # 0 means no error, but is still defined
52 11 ▶ return undef if !$err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 10 1 11
53  
54 1 $! = $err;
55 1 return $!;
56 }
57  
58 # It seems we can't call getsockopt to query SO_ERROR. We'll try getpeername
59 0 ▶ if( defined getpeername( $sock ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined getpeername $sock) 0 0 0
60 0 return undef;
61 }
62  
63 0 my $peername_errno = $!+0;
64 0 my $peername_errstr = "$!";
65  
66 # Not connected so we know this ought to fail
67 0 ▶ if( read( $sock, my $buff, 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $sock, my $buff, 1) 0 0 0
68 # That was most unexpected. getpeername fails because we're not
69 # connected, yet read succeeds.
70 0 warn "getpeername fails with $peername_errno ($peername_errstr) but read is successful\n";
71 0 warn "Please see http://rt.cpan.org/Ticket/Display.html?id=38382\n";
72  
73 0 $! = $peername_errno;
74 0 return $!;
75 }
76  
77 0 return $!;
78 }
79  
80 sub _connect_addresses
81 {
82 15 ▶ my $self = shift;
Subroutine _connect_addresses: called CC=1 SCAR=0.4
83 15 my ( $addrlist, $on_fail ) = @_;
84  
85 15 my $loop = $self->{loop};
86  
87 15 my ( $connecterr, $binderr, $socketerr );
88  
89 my $future = try_repeat_until_success {
90 15 ▶ my $addr = shift;
Subroutine __ANON__: called CC=10 SCAR=25.5
91 my ( $family, $socktype, $protocol, $localaddr, $peeraddr ) =
92 15 @{$addr}{qw( family socktype protocol localaddr peeraddr )};
93  
94 15 my $sock = IO::Async::OS->socket( $family, $socktype, $protocol );
95  
96 15 ▶ if( !$sock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sock) 0 15 15
97 0 $socketerr = $!;
98 0 ▶ $on_fail->( "socket", $family, $socktype, $protocol, $! ) if $on_fail;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fail 0 0 0
99 0 return Future->fail( 1 );
100 }
101  
102 15 ▶ if( $localaddr and not $sock->bind( $localaddr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($localaddr and not $sock->bind($localaddr)) 0 15 15
Condition66%2/3
$localaddr and not $sock->bind($localaddr)
!ll&&!rl&&r
1410
MC/DC0%0/2
$localaddr and not $sock->bind($localaddr)
$localaddr not $sock->bind($localaddr)
Truth table66%2/3
A and not(B)
A= $localaddrB= not $sock->bind($localaddr)
ABresult
0X0
100
111
103 0 $binderr = $!;
104 0 ▶ $on_fail->( "bind", $sock, $localaddr, $! ) if $on_fail;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fail 0 0 0
105 0 return Future->fail( 1 );
106 }
107  
108 15 $sock->blocking( 0 );
109  
110 # TODO: $sock->connect returns success masking EINPROGRESS
111 15 my $ret = connect( $sock, $peeraddr );
112 15 ▶ if( $ret ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ret) { } 2 13 15
elsif ($! != 115 and !0) { } 2 22 24
113 # Succeeded already? Dubious, but OK. Can happen e.g. with connections to
114 # localhost, or UNIX sockets, or something like that.
115 2 return Future->done( $sock );
116 }
117 ▶ elsif( $! != EINPROGRESS and !CONNECT_EWOULDLBOCK || $! != POSIX::EWOULDBLOCK ) {
Condition100%2/2
$! != 115 and
!ll
112
MC/DC100%1/1
$! != 115 and
$! != 115
Truth table100%2/2
A
A= $! != 115
Aresult
00
11
118 2 $connecterr = $!;
119 2 ▶ $on_fail->( "connect", $sock, $peeraddr, $! ) if $on_fail;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fail 2 0 2
120 2 return Future->fail( 1 );
121 }
122  
123 # Else
124 11 my $f = $loop->new_future;
125 $loop->watch_io(
126 handle => $sock,
127 on_write_ready => sub {
128 11 ▶ $loop->unwatch_io( handle => $sock, on_write_ready => 1 );
Subroutine __ANON__: called CC=3 SCAR=11.0
129  
130 11 my $err = _get_sock_err( $sock );
131  
132 11 ▶ return $f->done( $sock ) if !$err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 10 1 11
133  
134 1 $connecterr = $!;
135 1 ▶ $on_fail->( "connect", $sock, $peeraddr, $err ) if $on_fail;
Branch: 1 branch
BranchTrueFalseTotal
if $on_fail 1 0 1
136 1 return $f->fail( 1 );
137 },
138 11 );
139 $f->on_cancel(
140 0 ▶ sub { $loop->unwatch_io( handle => $sock, on_write_ready => 1 ); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 11 );
142 11 return $f;
143 15 } foreach => $addrlist;
144  
145 return $future->else_with_f( sub {
146 3 ▶ my $f = shift;
Subroutine __ANON__: called CC=4 SCAR=23.2
147  
148 3 ▶ return $future->new->fail( "connect: $connecterr", connect => connect => $connecterr )
Branch: 1 branch
BranchTrueFalseTotal
if $connecterr 3 0 3
149 if $connecterr;
150 0 ▶ return $future->new->fail( "bind: $binderr", connect => bind => $binderr )
Branch: 1 branch
BranchTrueFalseTotal
if $binderr 0 0 0
151 if $binderr;
152 0 ▶ return $future->new->fail( "socket: $socketerr", connect => socket => $socketerr )
Branch: 1 branch
BranchTrueFalseTotal
if $socketerr 0 0 0
153 if $socketerr;
154  
155 # If it gets this far then something went wrong
156 0 return $f;
157 15 } );
158 }
159  
160 sub connect
161 {
162 15 ▶ my $self = shift;
Subroutine connect: called CC=28 SCAR=41.7
Pod undocumented
163 15 my ( %params ) = @_;
164  
165 15 my $loop = $self->{loop};
166  
167 15 my $on_fail = $params{on_fail};
168  
169 15 my %gai_hints;
170 15 ▶ exists $params{$_} and $gai_hints{$_} = $params{$_} for qw( family socktype protocol flags );
Branch: 1 branch
BranchTrueFalseTotal
exists $params{$_} and $gai_hints{$_} = $params{$_} 8 52 60
171  
172 15 ▶ if( exists $params{host} or exists $params{local_host} or exists $params{local_port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"host"} or exists $params{"local_host"} or exists $params{"local_port"}) 0 9 9
Condition66%4/6
exists $params{"host"} or exists $params{"local_host"} or exists $params{"local_port"}
l!l&&r!l&&!r
609
exists $params{"host"} or exists $params{"local_host"}
l!l&&r!l&&!r
609
MC/DC33%1/3
exists $params{"host"} or exists $params{"local_host"} or exists $params{"local_port"}
exists $params{"host"} exists $params{"local_host"} exists $params{"local_port"}
Truth table50%2/4
A or B or C
A= exists $params{"host"}B= exists $params{"local_host"}C= exists $params{"local_port"}
ABCresult
1XX1
01X1
0011
0000
173 # We'll be making a ->getaddrinfo call
174 defined $gai_hints{socktype} or defined $gai_hints{protocol} or
175 6 ▶ carp "Attempting to ->connect without either 'socktype' or 'protocol' hint is not portable";
Branch: 1 branch
BranchTrueFalseTotal
defined $gai_hints{"socktype"} or defined $gai_hints{"protocol"} or carp("Attempting to ->connect without either 'socktype' or 'protocol' hint is not portable") 0 6 6
Condition33%1/3
defined $gai_hints{"socktype"} or defined $gai_hints{"protocol"}
l!l&&r!l&&!r
600
MC/DC0%0/2
defined $gai_hints{"socktype"} or defined $gai_hints{"protocol"}
defined $gai_hints{"socktype"} defined $gai_hints{"protocol"}
Truth table33%1/3
A or B
A= defined $gai_hints{"socktype"}B= defined $gai_hints{"protocol"}
ABresult
1X1
011
000
176 }
177  
178 15 my $peeraddrfuture;
179 15 ▶ if( exists $params{host} and exists $params{service} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $params{'host'} and exists $params{'service'}) { } 6 9 15
elsif (exists $params{'addrs'} or exists $params{'addr'}) { } 9 0 9
elsif (exists $params{'peer'}) { } 0 0 0
Condition66%2/3
exists $params{"host"} and exists $params{"service"}
!ll&&!rl&&r
906
MC/DC50%1/2
exists $params{"host"} and exists $params{"service"}
exists $params{"host"} exists $params{"service"}
Truth table66%2/3
A and B
A= exists $params{"host"}B= exists $params{"service"}
ABresult
0X0
100
111
180 6 ▶ my $host = $params{host} or croak "Expected 'host'";
Branch: 1 branch
BranchTrueFalseTotal
my $host = $params{"host"} or croak("Expected 'host'") 0 6 6
181 6 ▶ my $service = $params{service} or croak "Expected 'service'";
Branch: 1 branch
BranchTrueFalseTotal
my $service = $params{"service"} or croak("Expected 'service'") 0 6 6
182  
183 6 $peeraddrfuture = $loop->resolver->getaddrinfo(
184 host => $host,
185 service => $service,
186 %gai_hints,
187 );
188 }
189 ▶ elsif( exists $params{addrs} or exists $params{addr} ) {
Condition33%1/3
exists $params{"addrs"} or exists $params{"addr"}
l!l&&r!l&&!r
090
MC/DC0%0/2
exists $params{"addrs"} or exists $params{"addr"}
exists $params{"addrs"} exists $params{"addr"}
Truth table33%1/3
A or B
A= exists $params{"addrs"}B= exists $params{"addr"}
ABresult
1X1
011
000
190 9 ▶ my @addrs = exists $params{addrs} ? @{ $params{addrs} } : ( $params{addr} );
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'addrs'} ? : 0 9 9
191  
192 # Warn about some common mistakes
193 9 foreach my $peer ( @addrs ) {
194 9 my ( $p_family, undef, undef, $p_addr ) = IO::Async::OS->extract_addrinfo( $peer );
195  
196 9 local our @CARP_NOT = qw( IO::Async::Loop IO::Async::Handle );
197  
198 9 ▶ if( $p_family == Socket::AF_INET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p_family == 2) { } 7 2 9
elsif ($p_family == 10) { } 0 2 2
199 7 ▶ carp "Connecting to 0.0.0.0 is non-portable and ill-advised"
Branch: 1 branch
BranchTrueFalseTotal
if (Socket::unpack_sockaddr_in($p_addr))[1] eq "\0\0\0\0" 0 7 7
200 if ( Socket::unpack_sockaddr_in $p_addr )[1] eq Socket::INADDR_ANY;
201 }
202 elsif( HAVE_SOCKADDR_IN6 and $p_family == Socket::AF_INET6 ) {
203 0 ▶ carp "Connecting to :: is non-portable and ill-advised"
Branch: 1 branch
BranchTrueFalseTotal
if (Socket::unpack_sockaddr_in6($p_addr))[1] eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 0 0 0
204 if ( Socket::unpack_sockaddr_in6 $p_addr )[1] eq Socket::IN6ADDR_ANY;
205 }
206 }
207  
208 9 $peeraddrfuture = $loop->new_future->done( @addrs );
209 }
210 elsif( exists $params{peer} ) {
211 0 my $peer = delete $params{peer};
212 0 ▶ croak "Expected 'peer' to be an IO::Socket or subclass"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($peer) and $peer->isa("IO::Socket") 0 0 0
Condition0%0/3
builtin::blessed($peer) and $peer->isa("IO::Socket")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($peer) and $peer->isa("IO::Socket")
builtin::blessed($peer) $peer->isa("IO::Socket")
Truth table0%0/3
A and B
A= builtin::blessed($peer)B= $peer->isa("IO::Socket")
ABresult
0X0
100
111
213 unless blessed $peer and $peer->isa( "IO::Socket" );
214  
215 0 my $p_family = $peer->sockdomain;
216  
217 0 $peeraddrfuture = $loop->new_future->done(
218 [ $p_family, $peer->socktype, $peer->protocol, IO::Async::OS->make_addr_for_peer( $p_family, $peer->sockname ) ]
219 );
220 }
221 else {
222 0 croak "Expected 'host' and 'service' or 'addrs' or 'addr' arguments";
223 }
224  
225 15 my $localaddrfuture;
226 15 ▶ if( defined $params{local_host} or defined $params{local_service} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'local_host'} or defined $params{'local_service'}) { } 1 14 15
elsif (exists $params{'local_addrs'} or exists $params{'local_addr'}) { } 0 14 14
Condition66%2/3
defined $params{"local_host"} or defined $params{"local_service"}
l!l&&r!l&&!r
1014
MC/DC50%1/2
defined $params{"local_host"} or defined $params{"local_service"}
defined $params{"local_host"} defined $params{"local_service"}
Truth table66%2/3
A or B
A= defined $params{"local_host"}B= defined $params{"local_service"}
ABresult
1X1
011
000
227 # Empty is fine on either of these
228 1 my $host = $params{local_host};
229 1 my $service = $params{local_service};
230  
231 1 $localaddrfuture = $loop->resolver->getaddrinfo(
232 host => $host,
233 service => $service,
234 %gai_hints,
235 );
236 }
237 ▶ elsif( exists $params{local_addrs} or exists $params{local_addr} ) {
Condition33%1/3
exists $params{"local_addrs"} or exists $params{"local_addr"}
l!l&&r!l&&!r
0014
MC/DC0%0/2
exists $params{"local_addrs"} or exists $params{"local_addr"}
exists $params{"local_addrs"} exists $params{"local_addr"}
Truth table33%1/3
A or B
A= exists $params{"local_addrs"}B= exists $params{"local_addr"}
ABresult
1X1
011
000
238 0 ▶ $localaddrfuture = $loop->new_future->done( exists $params{local_addrs} ? @{ $params{local_addrs} } : ( $params{local_addr} ) );
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'local_addrs'} ? : 0 0 0
239 }
240 else {
241 14 $localaddrfuture = $loop->new_future->done( {} );
242 }
243  
244 return Future->needs_all( $peeraddrfuture, $localaddrfuture )
245 ->then( sub {
246 15 ▶ my @peeraddrs = $peeraddrfuture->result;
Subroutine __ANON__: called CC=15 SCAR=29.1
247 15 my @localaddrs = $localaddrfuture->result;
248  
249 15 my @addrs;
250  
251 15 foreach my $local ( @localaddrs ) {
252 15 my ( $l_family, $l_socktype, $l_protocol, $l_addr ) =
253 IO::Async::OS->extract_addrinfo( $local, 'local_addr' );
254 15 foreach my $peer ( @peeraddrs ) {
255 15 my ( $p_family, $p_socktype, $p_protocol, $p_addr ) =
256 IO::Async::OS->extract_addrinfo( $peer );
257  
258 15 ▶ next if $l_family and $p_family and $l_family != $p_family;
Branch: 1 branch
BranchTrueFalseTotal
if $l_family and $p_family and $l_family != $p_family 0 15 15
Condition66%4/6
$l_family and $p_family and $l_family != $p_family
!ll&&!rl&&r
1410
$l_family and $p_family
!ll&&!rl&&r
1401
MC/DC0%0/3
$l_family and $p_family and $l_family != $p_family
$l_family $p_family $l_family != $p_family
Truth table50%2/4
A and B and C
A= $l_familyB= $p_familyC= $l_family != $p_family
ABCresult
0XX0
10X0
1100
1111
259 15 ▶ next if $l_socktype and $p_socktype and $l_socktype != $p_socktype;
Branch: 1 branch
BranchTrueFalseTotal
if $l_socktype and $p_socktype and $l_socktype != $p_socktype 0 15 15
Condition66%4/6
$l_socktype and $p_socktype and $l_socktype != $p_socktype
!ll&&!rl&&r
1410
$l_socktype and $p_socktype
!ll&&!rl&&r
1401
MC/DC0%0/3
$l_socktype and $p_socktype and $l_socktype != $p_socktype
$l_socktype $p_socktype $l_socktype != $p_socktype
Truth table50%2/4
A and B and C
A= $l_socktypeB= $p_socktypeC= $l_socktype != $p_socktype
ABCresult
0XX0
10X0
1100
1111
260 15 ▶ next if $l_protocol and $p_protocol and $l_protocol != $p_protocol;
Branch: 1 branch
BranchTrueFalseTotal
if $l_protocol and $p_protocol and $l_protocol != $p_protocol 0 15 15
Condition66%4/6
$l_protocol and $p_protocol and $l_protocol != $p_protocol
!ll&&!rl&&r
1410
$l_protocol and $p_protocol
!ll&&!rl&&r
1401
MC/DC0%0/3
$l_protocol and $p_protocol and $l_protocol != $p_protocol
$l_protocol $p_protocol $l_protocol != $p_protocol
Truth table50%2/4
A and B and C
A= $l_protocolB= $p_protocolC= $l_protocol != $p_protocol
ABCresult
0XX0
10X0
1100
1111
261  
262 15 ▶ push @addrs, {
Condition77%7/9
$l_family || $p_family
l!l&&r!l&&!r
1140
$l_socktype || $p_socktype
l!l&&r!l&&!r
1140
$l_protocol || $p_protocol
l!l&&r!l&&!r
159
MC/DC0%0/2
$l_family || $p_family
$l_family $p_family
MC/DC0%0/2
$l_socktype || $p_socktype
$l_socktype $p_socktype
MC/DC100%2/2
$l_protocol || $p_protocol
$l_protocol $p_protocol
Truth table66%2/3
A || B
A= $l_familyB= $p_family
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $l_socktypeB= $p_socktype
ABresult
1X1
011
000
Truth table100%3/3
A || B
A= $l_protocolB= $p_protocol
ABresult
1X1
011
000
263 family => $l_family || $p_family,
264 socktype => $l_socktype || $p_socktype,
265 protocol => $l_protocol || $p_protocol,
266 localaddr => $l_addr,
267 peeraddr => $p_addr,
268 };
269 }
270 }
271  
272 15 return $self->_connect_addresses( \@addrs, $on_fail );
273 15 } );
274 }
275  
276 3 0x55AA;
« blib/lib/IO/Async/Debug.pm ↑ Summary blib/lib/Future/IO/Impl/IOAsync.pm »