blib/lib/IO/Socket/HappyEyeballs.pm

filter: Statementstmt 86.8% 224 / 258 Branchbran 66.3% 77 / 116 Conditioncond 53.0% 44 / 83 MC/DCmcdc 23.4% 11 / 47 Subroutinesub 100% 29 / 29 Podpod 100% 5 / 5 totaltotal 72.4% 390 / 538 CC 93 SCAR 28.7
CC
93
Cov
75%
_attempt_connections
52.1
_last_resort_synthesis
28.7
__ANON__
17.4
CRAP 17.7
↑ Summary
1 package IO::Socket::HappyEyeballs;
2 # ABSTRACT: RFC 8305 Happy Eyeballs v2 connection algorithm
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Errno qw( EINPROGRESS ECONNREFUSED EISCONN );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 getaddrinfo getnameinfo pack_sockaddr_in6 unpack_sockaddr_in6
11 AF_INET AF_INET6 AF_UNSPEC
12 SOCK_STREAM IPPROTO_TCP
13 NI_NUMERICHOST NI_NUMERICSERV
14 SOL_SOCKET SO_ERROR
15 AI_ADDRCONFIG
16 inet_pton inet_ntop
17 11 );
18 11 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 11 ▶use parent qw(IO::Socket::IP);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 11 our $VERSION = '0.002';
23  
24 # Class-level cache: { "host:port" => { family => AF_INET6|AF_INET, expires => time } }
25 11 my %_cache;
26 11 my $CACHE_TTL = 600; # 10 minutes
27  
28 # Default connection attempt delay per RFC 8305 §5: 250ms
29 11 my $DEFAULT_DELAY = 0.250;
30  
31 # Last Resort Local Synthesis Delay per RFC 8305 §7.2: 2 seconds
32 11 my $LAST_RESORT_DELAY = 2;
33  
34 11 my $_override_active;
35  
36 11 ▶sub import {
Subroutine import: called CC=4 SCAR=14.5
37 11 my ($class, @args) = @_;
38 11 for my $arg (@args) {
39 1 ▶ if ($arg eq '-override') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "-override") 1 0 1
40 1 ▶ $class->_install_override unless $_override_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $_override_active 1 0 1
41 }
42 }
43 }
44  
45 1 ▶sub _install_override {
Subroutine _install_override: called CC=1 SCAR=0.0
46 1 my ($class) = @_;
47 1 my $original_new = IO::Socket::IP->can('new');
48 1 my $original_configure = IO::Socket::IP->can('configure');
49 11 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # Override configure to skip connect on already-connected sockets.
52 # When a subclass (e.g. Net::HTTP) calls configure after Happy Eyeballs
53 # has already connected, IO::Socket::IP::configure must not reconnect.
54 3 ▶ *IO::Socket::IP::configure = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
55 3 my ($self, $cnf) = @_;
56 3 ▶ if (delete ${*$self}{io_socket_happyeyeballs_connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete do { *$self }->{"io_socket_happyeyeballs_connected"}) 1 2 3
57 1 return $self;
58 }
59 2 return $original_configure->($self, $cnf);
60 1 };
61  
62 11 ▶ *IO::Socket::IP::new = sub {
Subroutine __ANON__: called CC=15 SCAR=28.6
63 11 my ($ip_class, %args) = @_;
64 # Only intercept TCP connections with a peer
65 11 ▶ if (($args{PeerHost} || $args{PeerAddr}) && $args{PeerPort}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"PeerHost"} || $args{"PeerAddr"} and $args{"PeerPort"}) 4 7 11
Condition83%5/6
$args{"PeerHost"} || $args{"PeerAddr"} and $args{"PeerPort"}
!ll&&!rl&&r
704
$args{"PeerHost"} or $args{"PeerAddr"}
l!l&&r!l&&!r
317
MC/DC66%2/3
$args{"PeerHost"} || $args{"PeerAddr"} and $args{"PeerPort"}
$args{"PeerHost"} $args{"PeerAddr"} $args{"PeerPort"}
Truth table60%3/5
A or B and C
A= $args{"PeerHost"}B= $args{"PeerAddr"}C= $args{"PeerPort"}
ABCresult
00X0
1X00
0100
1X11
0111
66 4 ▶ my $proto = $args{Proto} || '';
Condition100%2/2
$args{'Proto'} || ''
l!l
13
MC/DC100%1/1
$args{'Proto'} || ''
$args{'Proto'}
Truth table100%2/2
A
A= $args{'Proto'}
Aresult
11
00
67 4 ▶ my $type = $args{Type} || 0;
Condition50%1/2
$args{'Type'} || 0
l!l
04
MC/DC0%0/1
$args{'Type'} || 0
$args{'Type'}
Truth table50%1/2
A
A= $args{'Type'}
Aresult
11
00
68 4 ▶ if (!$proto || $proto eq 'tcp' || $type == SOCK_STREAM || !$type) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $proto or $proto eq "tcp" or $type == 1 or not $type) 4 0 4
Condition44%4/9
not $proto or $proto eq "tcp" or $type == 1 or not $type
l!l&&r!l&&!r
400
not $proto or $proto eq "tcp" or $type == 1
l!l&&r!l&&!r
400
not $proto or $proto eq "tcp"
l!l&&r!l&&!r
310
MC/DC0%0/4
not $proto or $proto eq "tcp" or $type == 1 or not $type
not $proto $proto eq "tcp" $type == 1 not $type
Truth table40%2/5
not(A) or B or C or not(D)
A= not $protoB= $proto eq "tcp"C= $type == 1D= not $type
ABCDresult
1XXX1
01XX1
001X1
00011
00000
69 4 my $sock = IO::Socket::HappyEyeballs->_happy_connect(\%args);
70 4 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 3 1 4
71 # For subclasses (e.g. Net::HTTP): rebless and let their
72 # configure run for protocol-specific setup, while our
73 # IO::Socket::IP::configure override skips reconnecting.
74 3 ▶ if ($ip_class ne 'IO::Socket::IP'
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_class ne "IO::Socket::IP" and $ip_class ne "IO::Socket::HappyEyeballs" and $ip_class->isa("IO::Socket::IP")) 1 2 3
Condition66%4/6
$ip_class ne "IO::Socket::IP" and $ip_class ne "IO::Socket::HappyEyeballs" and $ip_class->isa("IO::Socket::IP")
!ll&&!rl&&r
201
$ip_class ne "IO::Socket::IP" and $ip_class ne "IO::Socket::HappyEyeballs"
!ll&&!rl&&r
201
MC/DC33%1/3
$ip_class ne "IO::Socket::IP" and $ip_class ne "IO::Socket::HappyEyeballs" and $ip_class->isa("IO::Socket::IP")
$ip_class ne "IO::Socket::IP" $ip_class ne "IO::Socket::HappyEyeballs" $ip_class->isa("IO::Socket::IP")
Truth table50%2/4
A and B and C
A= $ip_class ne "IO::Socket::IP"B= $ip_class ne "IO::Socket::HappyEyeballs"C= $ip_class->isa("IO::Socket::IP")
ABCresult
0XX0
10X0
1100
1111
75 && $ip_class ne 'IO::Socket::HappyEyeballs'
76 && $ip_class->isa('IO::Socket::IP')) {
77 1 bless $sock, $ip_class;
78 1 ${*$sock}{io_socket_happyeyeballs_connected} = 1;
79 1 ▶ $sock->configure(\%args) if $sock->can('configure');
Branch: 1 branch
BranchTrueFalseTotal
if $sock->can("configure") 1 0 1
80 }
81 3 return $sock;
82 }
83 1 return;
84 }
85 }
86 7 return $original_new->($ip_class, %args);
87 1 };
88 1 $_override_active = 1;
89 }
90  
91 4 ▶sub _happy_connect {
Subroutine _happy_connect: called CC=6 SCAR=18.5
92 4 my ($class, $args) = @_;
93 # Use our Happy Eyeballs algorithm
94 4 ▶ my $peer_host = $args->{PeerHost} || $args->{PeerAddr};
Condition66%2/3
$args->{'PeerHost'} || $args->{'PeerAddr'}
l!l&&r!l&&!r
310
MC/DC0%0/2
$args->{'PeerHost'} || $args->{'PeerAddr'}
$args->{'PeerHost'} $args->{'PeerAddr'}
Truth table66%2/3
A || B
A= $args->{'PeerHost'}B= $args->{'PeerAddr'}
ABresult
1X1
011
000
95 4 my $peer_port = $args->{PeerPort};
96  
97 4 ▶ my $delay = delete $args->{ConnectionAttemptDelay} // $DEFAULT_DELAY;
Condition33%1/3
delete $args->{"ConnectionAttemptDelay"} // $DEFAULT_DELAY
l!l&&r!l&&!r
040
MC/DC0%0/2
delete $args->{"ConnectionAttemptDelay"} // $DEFAULT_DELAY
delete $args->{"ConnectionAttemptDelay"} $DEFAULT_DELAY
Truth table33%1/3
A // B
A= delete $args->{"ConnectionAttemptDelay"}B= $DEFAULT_DELAY
ABresult
1X1
011
000
98 4 ▶ my $timeout = $args->{Timeout} // 30;
Condition100%2/2
$args->{"Timeout"} // 30
l!l
31
MC/DC100%1/1
$args->{"Timeout"} // 30
$args->{"Timeout"}
Truth table100%2/2
A
A= $args->{"Timeout"}
Aresult
11
00
99  
100 4 my @addresses = _resolve($peer_host, $peer_port, $args);
101 4 ▶ unless (@addresses) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@addresses) 0 4 4
102 0 $@ = "Cannot resolve host '$peer_host': no addresses found";
103 0 return;
104 }
105  
106 4 @addresses = _sort_addresses(\@addresses, $peer_host, $peer_port);
107  
108 4 my $sock = _attempt_connections(\@addresses, $delay, $timeout, $args,
109 $peer_host, $peer_port);
110  
111 4 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 3 1 4
112 3 _cache_result($peer_host, $peer_port, $sock->sockdomain);
113 3 return $sock;
114 }
115  
116 1 $@ = "Cannot connect to $peer_host:$peer_port: all attempts failed";
117 1 return;
118 }
119  
120 6 ▶sub new {
Subroutine new: called CC=8 SCAR=21.6
Pod documented
121 6 my ($class, %args) = @_;
122  
123 my $peer_host = $args{PeerHost} || $args{PeerAddr}
124 6 ▶ or croak "PeerHost or PeerAddr is required";
Branch: 1 branch
BranchTrueFalseTotal
my $peer_host = $args{"PeerHost"} || $args{"PeerAddr"} or croak("PeerHost or PeerAddr is required") 6 0 6
Condition33%1/3
$args{'PeerHost'} || $args{'PeerAddr'}
l!l&&r!l&&!r
600
MC/DC0%0/2
$args{'PeerHost'} || $args{'PeerAddr'}
$args{'PeerHost'} $args{'PeerAddr'}
Truth table33%1/3
A || B
A= $args{'PeerHost'}B= $args{'PeerAddr'}
ABresult
1X1
011
000
125 my $peer_port = $args{PeerPort}
126 6 ▶ or croak "PeerPort is required";
Branch: 1 branch
BranchTrueFalseTotal
my $peer_port = $args{"PeerPort"} or croak("PeerPort is required") 6 0 6
127  
128 6 ▶ my $delay = delete $args{ConnectionAttemptDelay} // $DEFAULT_DELAY;
Condition33%1/3
delete $args{"ConnectionAttemptDelay"} // $DEFAULT_DELAY
l!l&&r!l&&!r
060
MC/DC0%0/2
delete $args{"ConnectionAttemptDelay"} // $DEFAULT_DELAY
delete $args{"ConnectionAttemptDelay"} $DEFAULT_DELAY
Truth table33%1/3
A // B
A= delete $args{"ConnectionAttemptDelay"}B= $DEFAULT_DELAY
ABresult
1X1
011
000
129 6 ▶ my $timeout = $args{Timeout} // 30;
Condition50%1/2
$args{"Timeout"} // 30
l!l
60
MC/DC0%0/1
$args{"Timeout"} // 30
$args{"Timeout"}
Truth table50%1/2
A
A= $args{"Timeout"}
Aresult
11
00
130  
131 # Resolve addresses
132 6 my @addresses = _resolve($peer_host, $peer_port, \%args);
133 6 ▶ unless (@addresses) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@addresses) 1 5 6
134 1 $@ = "Cannot resolve host '$peer_host': no addresses found";
135 1 return;
136 }
137  
138 # Sort with interleaving per RFC 8305 §4
139 5 @addresses = _sort_addresses(\@addresses, $peer_host, $peer_port);
140  
141 # Attempt connections with Happy Eyeballs algorithm
142 5 my $sock = _attempt_connections(\@addresses, $delay, $timeout, \%args,
143 $peer_host, $peer_port);
144  
145 5 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 4 1 5
146 4 _cache_result($peer_host, $peer_port, $sock->sockdomain);
147 4 return $sock;
148 }
149  
150 1 $@ = "Cannot connect to $peer_host:$peer_port: all attempts failed";
151 1 return;
152 }
153  
154 10 ▶sub _resolve {
Subroutine _resolve: called CC=7 SCAR=19.7
155 10 my ($host, $port, $args) = @_;
156  
157 # Respect GetAddrInfoFlags from IO::Socket::IP compatibility,
158 # default to AI_ADDRCONFIG per RFC 8305 recommendation.
159 my $flags = exists $args->{GetAddrInfoFlags}
160 ? $args->{GetAddrInfoFlags}
161 10 ▶ : AI_ADDRCONFIG;
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'GetAddrInfoFlags'} ? : 0 10 10
162  
163 10 ▶ my %hints = (
Branch: 1 branch
BranchTrueFalseTotal
$flags ? : 10 0 10
164 socktype => SOCK_STREAM,
165 protocol => IPPROTO_TCP,
166 family => AF_UNSPEC,
167 ($flags ? (flags => $flags) : ()),
168 );
169  
170 10 my ($err, @results) = getaddrinfo($host, $port, \%hints);
171  
172 # If AI_ADDRCONFIG returned nothing (e.g. loopback-only interfaces),
173 # retry without it to avoid filtering out valid addresses.
174 10 ▶ if (($err || !@results) && $flags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err || !@results and $flags) 1 9 10
Condition66%4/6
$err || !@results and $flags
!ll&&!rl&&r
901
$err or not @results
l!l&&r!l&&!r
109
MC/DC33%1/3
$err || !@results and $flags
$err not @results $flags
Truth table40%2/5
A or not(B) and C
A= $errB= not @resultsC= $flags
ABCresult
00X0
1X00
0100
1X11
0111
175 1 delete $hints{flags};
176 1 ($err, @results) = getaddrinfo($host, $port, \%hints);
177 }
178  
179 10 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 9 10
180 1 $@ = "getaddrinfo failed: $err";
181 1 return;
182 }
183  
184 9 return @results;
185 }
186  
187 17 ▶sub _sort_addresses {
Subroutine _sort_addresses: called CC=13 SCAR=25.7
188 17 my ($addresses, $host, $port) = @_;
189  
190 # Check cache for preferred family
191 17 my $cache_key = "$host:$port";
192 17 my $preferred_family;
193 17 ▶ if (my $cached = $_cache{$cache_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $_cache{$cache_key}) 8 9 17
194 8 ▶ if ($cached->{expires} > time()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached->{'expires'} > time) { } 7 1 8
195 7 $preferred_family = $cached->{family};
196 } else {
197 1 delete $_cache{$cache_key};
198 }
199 }
200  
201 # Separate by address family
202 17 my (@ipv6, @ipv4);
203 17 for my $addr (@$addresses) {
204 29 ▶ if ($addr->{family} == AF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr->{'family'} == 10) { } 10 19 29
205 10 push @ipv6, $addr;
206 } else {
207 19 push @ipv4, $addr;
208 }
209 }
210  
211 # Interleave: preferred family first, then alternate
212 # Per RFC 8305 §4
213 17 my @sorted;
214 17 my ($primary, $secondary);
215 17 ▶ if ($preferred_family && $preferred_family == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preferred_family and $preferred_family == 2) { } 6 11 17
Condition100%3/3
$preferred_family and $preferred_family == 2
!ll&&!rl&&r
1016
MC/DC100%2/2
$preferred_family and $preferred_family == 2
$preferred_family $preferred_family == 2
Truth table100%3/3
A and B
A= $preferred_familyB= $preferred_family == 2
ABresult
0X0
100
111
216 6 $primary = \@ipv4;
217 6 $secondary = \@ipv6;
218 } else {
219 11 ▶ $primary = @ipv6 ? \@ipv6 : \@ipv4;
Branch: 1 branch
BranchTrueFalseTotal
@ipv6 ? : 5 6 11
220 11 ▶ $secondary = @ipv6 ? \@ipv4 : \@ipv6;
Branch: 1 branch
BranchTrueFalseTotal
@ipv6 ? : 5 6 11
221 }
222  
223 17 ▶ while (@$primary || @$secondary) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$primary or @$secondary) 21 17 38
Condition66%2/3
@$primary or @$secondary
l!l&&r!l&&!r
21017
MC/DC50%1/2
@$primary or @$secondary
@$primary @$secondary
Truth table66%2/3
A or B
A= @$primaryB= @$secondary
ABresult
1X1
011
000
224 21 ▶ push @sorted, shift @$primary if @$primary;
Branch: 1 branch
BranchTrueFalseTotal
if @$primary 21 0 21
225 21 ▶ push @sorted, shift @$secondary if @$secondary;
Branch: 1 branch
BranchTrueFalseTotal
if @$secondary 8 13 21
226 }
227  
228 17 return @sorted;
229 }
230  
231 9 ▶sub _attempt_connections {
Subroutine _attempt_connections: called CC=22 SCAR=39.5
232 9 my ($addresses, $delay, $timeout, $args, $host, $port) = @_;
233  
234 9 my @pending; # [ socket, addrinfo ] pairs
235 9 my $select = IO::Select->new;
236 9 my $deadline = time() + $timeout;
237 9 my $next_attempt_time = 0; # start first attempt immediately
238  
239 9 my $addr_idx = 0;
240 9 my $last_attempt_time;
241 9 my $last_resort_done = 0;
242  
243 9 ▶ while ($addr_idx < @$addresses || @pending) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr_idx < @$addresses or @pending) 9 2 11
Condition66%2/3
$addr_idx < @$addresses or @pending
l!l&&r!l&&!r
902
MC/DC50%1/2
$addr_idx < @$addresses or @pending
$addr_idx < @$addresses @pending
Truth table66%2/3
A or B
A= $addr_idx < @$addressesB= @pending
ABresult
1X1
011
000
244 9 my $now = time();
245 9 ▶ last if $now >= $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if $now >= $deadline 0 9 9
246  
247 # RFC 8305 §7.2: Last Resort Local Synthesis
248 # When all initial addresses are exhausted and all pending have failed,
249 # wait until $LAST_RESORT_DELAY seconds after last attempt, then try
250 # A-record-only resolution with NAT64 synthesis.
251 9 ▶ if (!$last_resort_done && $addr_idx >= @$addresses && !@pending
Branch: 1 branch
BranchTrueFalseTotal
if (not $last_resort_done and $addr_idx >= @$addresses and not @pending and $last_attempt_time) 0 9 9
Condition33%3/9
not $last_resort_done and $addr_idx >= @$addresses and not @pending and $last_attempt_time
!ll&&!rl&&r
900
not $last_resort_done and $addr_idx >= @$addresses and not @pending
!ll&&!rl&&r
900
not $last_resort_done and $addr_idx >= @$addresses
!ll&&!rl&&r
090
MC/DC0%0/4
not $last_resort_done and $addr_idx >= @$addresses and not @pending and $last_attempt_time
not $last_resort_done $addr_idx >= @$addresses not @pending $last_attempt_time
Truth table20%1/5
not(A) and B and not(C) and D
A= not $last_resort_doneB= $addr_idx >= @$addressesC= not @pendingD= $last_attempt_time
ABCDresult
0XXX0
10XX0
110X0
11100
11111
252 && $last_attempt_time) {
253 0 my $synth_addresses = _last_resort_synthesis(
254 $host, $port, $args, $last_attempt_time, $deadline);
255 0 ▶ if ($synth_addresses && @$synth_addresses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($synth_addresses and @$synth_addresses) 0 0 0
Condition0%0/3
$synth_addresses and @$synth_addresses
!ll&&!rl&&r
000
MC/DC0%0/2
$synth_addresses and @$synth_addresses
$synth_addresses @$synth_addresses
Truth table0%0/3
A and B
A= $synth_addressesB= @$synth_addresses
ABresult
0X0
100
111
256 0 push @$addresses, @$synth_addresses;
257 0 $last_resort_done = 1;
258 0 next; # re-enter loop to process new addresses
259 }
260 0 $last_resort_done = 1;
261 }
262  
263 # Start a new connection attempt if it's time
264 9 ▶ if ($addr_idx < @$addresses && $now >= $next_attempt_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr_idx < @$addresses and $now >= $next_attempt_time) 9 0 9
Condition33%1/3
$addr_idx < @$addresses and $now >= $next_attempt_time
!ll&&!rl&&r
009
MC/DC0%0/2
$addr_idx < @$addresses and $now >= $next_attempt_time
$addr_idx < @$addresses $now >= $next_attempt_time
Truth table33%1/3
A and B
A= $addr_idx < @$addressesB= $now >= $next_attempt_time
ABresult
0X0
100
111
265 9 my $addr = $addresses->[$addr_idx++];
266 9 my $sock = _start_connect($addr, $args);
267  
268 9 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 9 0 9
269 # Check if already connected (localhost etc.)
270 9 ▶ if ($sock->connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock->connected) 7 2 9
271 7 _cleanup_pending(\@pending);
272 7 _restore_blocking($sock, $args);
273 7 return $sock;
274 }
275 2 push @pending, [ $sock, $addr ];
276 2 $select->add($sock);
277 }
278  
279 2 $last_attempt_time = time();
280 2 $next_attempt_time = $last_attempt_time + $delay;
281 }
282  
283 2 ▶ next unless @pending;
Branch: 1 branch
BranchTrueFalseTotal
unless @pending 0 2 2
284  
285 # Calculate how long to wait
286 2 my $wait_time;
287 2 ▶ if ($addr_idx < @$addresses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr_idx < @$addresses) { } 0 2 2
288 # Wait until either a connection succeeds or it's time for the next attempt
289 0 $wait_time = $next_attempt_time - time();
290 0 ▶ $wait_time = 0 if $wait_time < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wait_time < 0 0 0 0
291 } else {
292 # No more addresses to try, wait for remaining connections
293 2 $wait_time = $deadline - time();
294 2 ▶ $wait_time = 0 if $wait_time < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $wait_time < 0 0 2 2
295 }
296  
297 # select() for writable (connected) sockets
298 2 my @ready = IO::Select->select(undef, $select, undef, $wait_time);
299  
300 2 ▶ if (@ready && $ready[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ready and $ready[1]) 2 0 2
Condition33%1/3
@ready and $ready[1]
!ll&&!rl&&r
002
MC/DC0%0/2
@ready and $ready[1]
@ready $ready[1]
Truth table33%1/3
A and B
A= @readyB= $ready[1]
ABresult
0X0
100
111
301 2 for my $sock (@{$ready[1]}) {
302 # Check if the connection actually succeeded
303 2 my $err = $sock->sockopt(SO_ERROR);
304 2 ▶ if ($err == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == 0) { } 0 2 2
305 # Success! Clean up all other pending sockets
306 0 my @others = grep { $_->[0] != $sock } @pending;
307 0 _cleanup_pending(\@others);
308 0 _restore_blocking($sock, $args);
309  
310 0 return $sock;
311 } else {
312 # This connection failed, remove it
313 2 $select->remove($sock);
314 2 $sock->close;
315 2 @pending = grep { $_->[0] != $sock } @pending;
316 }
317 }
318 }
319 }
320  
321 # All failed
322 2 _cleanup_pending(\@pending);
323 2 return;
324 }
325  
326 9 ▶sub _start_connect {
Subroutine _start_connect: called CC=4 SCAR=15.5
327 9 my ($addr, $args) = @_;
328  
329 9 my $sock = IO::Socket::IP->new;
330 $sock->socket($addr->{family}, $addr->{socktype}, $addr->{protocol})
331 9 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
$sock->socket($addr->{"family"}, $addr->{"socktype"}, $addr->{"protocol"}) or return 9 0 9
332  
333 9 $sock->blocking(0);
334  
335 9 my $rv = CORE::connect($sock, $addr->{addr});
336 9 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 0 9 9
337 # Connected immediately
338 0 return $sock;
339 }
340 9 ▶ if ($! == EINPROGRESS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 115) 9 0 9
341 # Connection in progress — this is the normal non-blocking case
342 9 return $sock;
343 }
344  
345 # Immediate failure
346 0 $sock->close;
347 0 return;
348 }
349  
350 7 ▶sub _restore_blocking {
Subroutine _restore_blocking: called CC=3 SCAR=11.0
351 7 my ($sock, $args) = @_;
352 7 ▶ if (exists $args->{Blocking} && !$args->{Blocking}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'Blocking'} and not $args->{'Blocking'}) { } 1 6 7
Condition66%2/3
exists $args->{"Blocking"} and not $args->{"Blocking"}
!ll&&!rl&&r
601
MC/DC50%1/2
exists $args->{"Blocking"} and not $args->{"Blocking"}
exists $args->{"Blocking"} not $args->{"Blocking"}
Truth table66%2/3
A and not(B)
A= exists $args->{"Blocking"}B= not $args->{"Blocking"}
ABresult
0X0
100
111
353 1 $sock->blocking(0);
354 } else {
355 6 $sock->blocking(1);
356 }
357 }
358  
359 9 ▶sub _cleanup_pending {
Subroutine _cleanup_pending: called CC=3 SCAR=16.0
360 9 my ($pending) = @_;
361 9 for my $p (@$pending) {
362 0 ▶ $p->[0]->close if $p->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $p->[0] 0 0 0
363 }
364 }
365  
366 # RFC 8305 §7.2: Last Resort Local Synthesis
367 # After all AAAA-based attempts fail, wait for the synthesis delay,
368 # then query A records and synthesize IPv6 via NAT64 if available.
369 2 ▶sub _last_resort_synthesis {
Subroutine _last_resort_synthesis: called CC=10 SCAR=33.6
370 2 my ($host, $port, $args, $last_attempt_time, $deadline) = @_;
371  
372 2 ▶ return unless defined $host && defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host and defined $port 0 2 2
Condition33%1/3
defined $host and defined $port
!ll&&!rl&&r
002
MC/DC0%0/2
defined $host and defined $port
defined $host defined $port
Truth table33%1/3
A and B
A= defined $hostB= defined $port
ABresult
0X0
100
111
373  
374 # Wait until $LAST_RESORT_DELAY after last attempt fired
375 2 my $synthesis_time = $last_attempt_time + $LAST_RESORT_DELAY;
376 2 my $now = time();
377 2 ▶ if ($now < $synthesis_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now < $synthesis_time) 1 1 2
378 1 my $wait = $synthesis_time - $now;
379 1 ▶ return if $now + $wait > $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if $now + $wait > $deadline 1 0 1
380 0 select(undef, undef, undef, $wait);
381 }
382  
383 # Query A records only (IPv4)
384 1 my ($err, @ipv4_results) = getaddrinfo($host, $port, {
385 socktype => SOCK_STREAM,
386 protocol => IPPROTO_TCP,
387 family => AF_INET,
388 });
389 1 ▶ return unless @ipv4_results;
Branch: 1 branch
BranchTrueFalseTotal
unless @ipv4_results 1 0 1
390  
391 # Detect NAT64 prefix via RFC 7050 (ipv4only.arpa)
392 0 my $nat64_prefix = _detect_nat64_prefix();
393  
394 0 ▶ if ($nat64_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nat64_prefix) 0 0 0
395 # Synthesize IPv6 addresses from IPv4 using NAT64 prefix
396 0 my @synthesized;
397 0 for my $r (@ipv4_results) {
398 0 my $synth = _synthesize_nat64_addr($r, $nat64_prefix, $port);
399 0 ▶ push @synthesized, $synth if $synth;
Branch: 1 branch
BranchTrueFalseTotal
if $synth 0 0 0
400 }
401 0 ▶ return \@synthesized if @synthesized;
Branch: 1 branch
BranchTrueFalseTotal
if @synthesized 0 0 0
402 }
403  
404 # No NAT64: return IPv4 addresses directly as fallback
405 0 return \@ipv4_results;
406 }
407  
408 # RFC 7050: Discovery of the IPv6 Prefix Used for IPv6 Address Synthesis
409 # Resolve ipv4only.arpa AAAA — if we get results, NAT64 is present
410 # and the prefix can be extracted from the response.
411 my $_nat64_prefix_cache;
412 11 my $_nat64_prefix_expires = 0;
413  
414 1 ▶sub _detect_nat64_prefix {
Subroutine _detect_nat64_prefix: called CC=4 SCAR=17.9
415 1 my $now = time();
416 1 ▶ if ($now < $_nat64_prefix_expires) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now < $_nat64_prefix_expires) 0 1 1
417 0 return $_nat64_prefix_cache;
418 }
419  
420 # The well-known IPv4 addresses for ipv4only.arpa are
421 # 192.0.0.170 and 192.0.0.171. If AAAA resolution returns
422 # results, the NAT64 prefix is the first 96 bits.
423 1 my ($err, @results) = getaddrinfo('ipv4only.arpa', '443', {
424 socktype => SOCK_STREAM,
425 family => AF_INET6,
426 });
427  
428 1 ▶ if (!$err && @results) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $err and @results) 0 1 1
Condition33%1/3
not $err and @results
!ll&&!rl&&r
100
MC/DC0%0/2
not $err and @results
not $err @results
Truth table33%1/3
not(A) and B
A= not $errB= @results
ABresult
0X0
100
111
429 0 my ($synth_port, $synth_addr) = unpack_sockaddr_in6($results[0]{addr});
430 # The well-known address 192.0.0.170 = 0xC0000AA in the last 32 bits
431 # Extract the first 12 bytes as the NAT64 prefix
432 0 my $prefix = substr($synth_addr, 0, 12);
433 0 $_nat64_prefix_cache = $prefix;
434 0 $_nat64_prefix_expires = $now + 600; # cache for 10 minutes
435 0 return $prefix;
436 }
437  
438 1 $_nat64_prefix_cache = undef;
439 1 $_nat64_prefix_expires = $now + 60; # negative cache for 1 minute
440 1 return;
441 }
442  
443 # Synthesize an IPv6 address by combining NAT64 prefix with IPv4 address
444 2 ▶sub _synthesize_nat64_addr {
Subroutine _synthesize_nat64_addr: called CC=2 SCAR=6.9
445 2 my ($ipv4_addrinfo, $nat64_prefix, $port) = @_;
446  
447 # Extract the IPv4 address from the sockaddr
448 2 my $family = $ipv4_addrinfo->{family};
449 2 ▶ return unless $family == AF_INET;
Branch: 1 branch
BranchTrueFalseTotal
unless $family == 2 1 1 2
450  
451 1 my ($ipv4_port, $ipv4_packed) = Socket::unpack_sockaddr_in($ipv4_addrinfo->{addr});
452  
453 # Build synthesized IPv6 address: 96-bit prefix + 32-bit IPv4
454 1 my $synth_ipv6 = $nat64_prefix . $ipv4_packed;
455 1 my $synth_sockaddr = pack_sockaddr_in6($ipv4_port, $synth_ipv6);
456  
457 return {
458 1 family => AF_INET6,
459 socktype => SOCK_STREAM,
460 protocol => IPPROTO_TCP,
461 addr => $synth_sockaddr,
462 };
463 }
464  
465 11 ▶sub _cache_result {
Subroutine _cache_result: called CC=1 SCAR=0.0
466 11 my ($host, $port, $family) = @_;
467 11 $_cache{"$host:$port"} = {
468 family => $family,
469 expires => time() + $CACHE_TTL,
470 };
471 }
472  
473 6 ▶sub clear_cache {
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
474 6 %_cache = ();
475 6 $_nat64_prefix_cache = undef;
476 6 $_nat64_prefix_expires = 0;
477 }
478  
479 8 ▶sub last_resort_delay {
Subroutine last_resort_delay: called CC=2 SCAR=6.9
Pod documented
480 8 my ($class, $new_delay) = @_;
481 8 ▶ if (defined $new_delay) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_delay) 6 2 8
482 6 $LAST_RESORT_DELAY = $new_delay;
483 }
484 8 return $LAST_RESORT_DELAY;
485 }
486  
487 4 ▶sub connection_attempt_delay {
Subroutine connection_attempt_delay: called CC=2 SCAR=6.9
Pod documented
488 4 my ($class, $new_delay) = @_;
489 4 ▶ if (defined $new_delay) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_delay) 2 2 4
490 2 $DEFAULT_DELAY = $new_delay;
491 }
492 4 return $DEFAULT_DELAY;
493 }
494  
495 4 ▶sub cache_ttl {
Subroutine cache_ttl: called CC=2 SCAR=6.9
Pod documented
496 4 my ($class, $new_ttl) = @_;
497 4 ▶ if (defined $new_ttl) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_ttl) 2 2 4
498 2 $CACHE_TTL = $new_ttl;
499 }
500 4 return $CACHE_TTL;
501 }
502  
503 11 1;
504  
505 __END__
↑ Summary