blib/lib/IO/Socket/IP.pm

filter: Statementstmt 80.8% 299 / 370 Branchbran 63.5% 136 / 214 Conditioncond 56.8% 71 / 125 MC/DCmcdc 26.0% 19 / 73 Subroutinesub 78.5% 33 / 42 Podpod 82.6% 19 / 23 totaltotal 68.1% 577 / 847 SCAR 66.0
CC
157
Cov
71%
_io_socket_ip__configure
212.4
CAN_DISABLE_V6ONLY
72.0
setup
27.6
CRAP 735.6
↑ Summary Build »
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, 2010-2023 -- leonerd@leonerd.org.uk
5  
6 package IO::Socket::IP 0.42;
7  
8 22 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 22 ▶use base qw( IO::Socket );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 use Socket 1.97 qw(
16 getaddrinfo getnameinfo
17 sockaddr_family
18 AF_INET
19 AI_PASSIVE
20 IPPROTO_TCP IPPROTO_UDP
21 IPPROTO_IPV6 IPV6_V6ONLY
22 NI_DGRAM NI_NUMERICHOST NI_NUMERICSERV NIx_NOHOST NIx_NOSERV
23 SO_REUSEADDR SO_REUSEPORT SO_BROADCAST SO_ERROR
24 SOCK_DGRAM SOCK_STREAM
25 SOL_SOCKET
26 22 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 22 my $AF_INET6 = eval { Socket::AF_INET6() }; # may not be defined
28 22 ▶my $AI_ADDRCONFIG = eval { Socket::AI_ADDRCONFIG() } || 0;
Condition50%1/2
eval { do { 32 } } || 0
l!l
220
MC/DC0%0/1
eval { do { 32 } } || 0
eval { do { 32 } }
Truth table50%1/2
A
A= eval { do { 32 } }
Aresult
11
00
29 22 ▶use POSIX qw( dup2 );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 22 ▶use Errno qw( EINVAL EINPROGRESS EISCONN ENOTCONN ETIMEDOUT EWOULDBLOCK EOPNOTSUPP );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 22 ▶use constant HAVE_MSWIN32 => ( $^O eq "MSWin32" );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # At least one OS (Android) is known not to have getprotobyname()
35 22 ▶use constant HAVE_GETPROTOBYNAME => defined eval { getprotobyname( "tcp" ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 22 my $IPv6_re = do {
38 # translation of RFC 3986 3.2.2 ABNF to re
39 22 my $IPv4address = do {
40 22 my $dec_octet = q<(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])>;
41 22 qq<$dec_octet(?: \\. $dec_octet){3}>;
42 };
43 22 my $IPv6address = do {
44 22 my $h16 = qq<[0-9A-Fa-f]{1,4}>;
45 22 my $ls32 = qq<(?: $h16 : $h16 | $IPv4address)>;
46 22 qq<(?:
47 (?: $h16 : ){6} $ls32
48 | :: (?: $h16 : ){5} $ls32
49 | (?: $h16 )? :: (?: $h16 : ){4} $ls32
50 | (?: (?: $h16 : ){0,1} $h16 )? :: (?: $h16 : ){3} $ls32
51 | (?: (?: $h16 : ){0,2} $h16 )? :: (?: $h16 : ){2} $ls32
52 | (?: (?: $h16 : ){0,3} $h16 )? :: $h16 : $ls32
53 | (?: (?: $h16 : ){0,4} $h16 )? :: $ls32
54 | (?: (?: $h16 : ){0,5} $h16 )? :: $h16
55 | (?: (?: $h16 : ){0,6} $h16 )? ::
56 )>
57 };
58 22 qr<$IPv6address>xo;
59 };
60  
61 =head1 NAME
62  
63 C<IO::Socket::IP> - Family-neutral IP socket supporting both IPv4 and IPv6
64  
65 =head1 SYNOPSIS
66  
67 use IO::Socket::IP;
68  
69 my $sock = IO::Socket::IP->new(
70 PeerHost => "www.google.com",
71 PeerPort => "http",
72 Type => SOCK_STREAM,
73 ) or die "Cannot construct socket - $IO::Socket::errstr";
74  
75 my $familyname = ( $sock->sockdomain == PF_INET6 ) ? "IPv6" :
76 ( $sock->sockdomain == PF_INET ) ? "IPv4" :
77 "unknown";
78  
79 printf "Connected to google via %s\n", $familyname;
80  
81 =head1 DESCRIPTION
82  
83 This module provides a protocol-independent way to use IPv4 and IPv6 sockets,
84 intended as a replacement for L<IO::Socket::INET>. Most constructor arguments
85 and methods are provided in a backward-compatible way. For a list of known
86 differences, see the C<IO::Socket::INET> INCOMPATIBILITES section below.
87  
88 It uses the C<getaddrinfo(3)> function to convert hostnames and service names
89 or port numbers into sets of possible addresses to connect to or listen on.
90 This allows it to work for IPv6 where the system supports it, while still
91 falling back to IPv4-only on systems which don't.
92  
93 =head1 REPLACING C<IO::Socket> DEFAULT BEHAVIOUR
94  
95 By placing C<-register> in the import list to C<IO::Socket::IP>, it will
96 register itself with L<IO::Socket> as the class that handles C<PF_INET>. It
97 will also ask to handle C<PF_INET6> as well, provided that constant is
98 available.
99  
100 Changing C<IO::Socket>'s default behaviour means that calling the
101 C<IO::Socket> constructor with either C<PF_INET> or C<PF_INET6> as the
102 C<Domain> parameter will yield an C<IO::Socket::IP> object.
103  
104 use IO::Socket::IP -register;
105  
106 my $sock = IO::Socket->new(
107 Domain => PF_INET6,
108 LocalHost => "::1",
109 Listen => 1,
110 ) or die "Cannot create socket - $IO::Socket::errstr\n";
111  
112 print "Created a socket of type " . ref($sock) . "\n";
113  
114 Note that C<-register> is a global setting that applies to the entire program;
115 it cannot be applied only for certain callers, removed, or limited by lexical
116 scope.
117  
118 =cut
119  
120 sub import
121 {
122 22 ▶ my $pkg = shift;
Subroutine import: called CC=4 SCAR=14.3
123 22 my @symbols;
124  
125 22 foreach ( @_ ) {
126 1 ▶ if( $_ eq "-register" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq '-register') { } 1 0 1
127 1 IO::Socket::IP::_ForINET->register_domain( AF_INET );
128 1 ▶ IO::Socket::IP::_ForINET6->register_domain( $AF_INET6 ) if defined $AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
if defined $AF_INET6 1 0 1
129 }
130 else {
131 0 push @symbols, $_;
132 }
133 }
134  
135 22 @_ = ( $pkg, @symbols );
136 22 goto &IO::Socket::import;
137 }
138  
139 # Convenient capability test function
140 {
141 22 my $can_disable_v6only;
142 sub CAN_DISABLE_V6ONLY
143 {
144 0 ▶ return $can_disable_v6only if defined $can_disable_v6only;
Subroutine CAN_DISABLE_V6ONLY: not called CC=8 SCAR=42.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $can_disable_v6only 0 0 0
145  
146 0 ▶ socket my $testsock, Socket::PF_INET6(), SOCK_STREAM, 0 or
Branch: 1 branch
BranchTrueFalseTotal
CORE::socket my $testsock, 10, 1, 0 or die "Cannot socket(PF_INET6) - $!" 0 0 0
147 die "Cannot socket(PF_INET6) - $!";
148  
149 0 ▶ if( setsockopt $testsock, IPPROTO_IPV6, IPV6_V6ONLY, 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (setsockopt $testsock, 41, 26, 0) { } 0 0 0
elsif ($! == 22 or $! == 95) { } 0 0 0
150 0 ▶ if( $^O eq "dragonfly") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "dragonfly") 0 0 0
151 # dragonflybsd 6.4 lies about successfully turning this off
152 0 ▶ if( getsockopt $testsock, IPPROTO_IPV6, IPV6_V6ONLY ) {
Branch: 1 branch
BranchTrueFalseTotal
if (getsockopt $testsock, 41, 26) 0 0 0
153 0 return $can_disable_v6only = 0;
154 }
155 }
156 0 return $can_disable_v6only = 1;
157 }
158 ▶ elsif( $! == EINVAL || $! == EOPNOTSUPP ) {
Condition0%0/3
$! == 22 or $! == 95
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 22 or $! == 95
$! == 22 $! == 95
Truth table0%0/3
A or B
A= $! == 22B= $! == 95
ABresult
1X1
011
000
159 0 return $can_disable_v6only = 0;
160 }
161 else {
162 0 die "Cannot setsockopt() - $!";
163 }
164 }
165 }
166  
167 =head1 CONSTRUCTORS
168  
169 =cut
170  
171 =head2 new
172  
173 $sock = IO::Socket::IP->new( %args )
174  
175 Creates a new C<IO::Socket::IP> object, containing a newly created socket
176 handle according to the named arguments passed. The recognised arguments are:
177  
178 =over 8
179  
180 =item PeerHost => STRING
181  
182 =item PeerService => STRING
183  
184 Hostname and service name for the peer to C<connect()> to. The service name
185 may be given as a port number, as a decimal string.
186  
187 =item PeerAddr => STRING
188  
189 =item PeerPort => STRING
190  
191 For symmetry with the accessor methods and compatibility with
192 C<IO::Socket::INET>, these are accepted as synonyms for C<PeerHost> and
193 C<PeerService> respectively.
194  
195 =item PeerAddrInfo => ARRAY
196  
197 Alternate form of specifying the peer to C<connect()> to. This should be an
198 array of the form returned by C<Socket::getaddrinfo>.
199  
200 This parameter takes precedence over the C<Peer*>, C<Family>, C<Type> and
201 C<Proto> arguments.
202  
203 =item LocalHost => STRING
204  
205 =item LocalService => STRING
206  
207 Hostname and service name for the local address to C<bind()> to.
208  
209 =item LocalAddr => STRING
210  
211 =item LocalPort => STRING
212  
213 For symmetry with the accessor methods and compatibility with
214 C<IO::Socket::INET>, these are accepted as synonyms for C<LocalHost> and
215 C<LocalService> respectively.
216  
217 =item LocalAddrInfo => ARRAY
218  
219 Alternate form of specifying the local address to C<bind()> to. This should be
220 an array of the form returned by C<Socket::getaddrinfo>.
221  
222 This parameter takes precedence over the C<Local*>, C<Family>, C<Type> and
223 C<Proto> arguments.
224  
225 =item Family => INT
226  
227 The address family to pass to C<getaddrinfo> (e.g. C<AF_INET>, C<AF_INET6>).
228 Normally this will be left undefined, and C<getaddrinfo> will search using any
229 address family supported by the system.
230  
231 =item Type => INT
232  
233 The socket type to pass to C<getaddrinfo> (e.g. C<SOCK_STREAM>,
234 C<SOCK_DGRAM>). Normally defined by the caller; if left undefined
235 C<getaddrinfo> may attempt to infer the type from the service name.
236  
237 =item Proto => STRING or INT
238  
239 The IP protocol to use for the socket (e.g. C<'tcp'>, C<IPPROTO_TCP>,
240 C<'udp'>,C<IPPROTO_UDP>). Normally this will be left undefined, and either
241 C<getaddrinfo> or the kernel will choose an appropriate value. May be given
242 either in string name or numeric form.
243  
244 =item GetAddrInfoFlags => INT
245  
246 More flags to pass to the C<getaddrinfo()> function. If not supplied, a
247 default of C<AI_ADDRCONFIG> will be used.
248  
249 These flags will be combined with C<AI_PASSIVE> if the C<Listen> argument is
250 given. For more information see the documentation about C<getaddrinfo()> in
251 the L<Socket> module.
252  
253 =item Listen => INT
254  
255 If defined, puts the socket into listening mode where new connections can be
256 accepted using the C<accept> method. The value given is used as the
257 C<listen(2)> queue size.
258  
259 =item ReuseAddr => BOOL
260  
261 If true, set the C<SO_REUSEADDR> sockopt
262  
263 =item ReusePort => BOOL
264  
265 If true, set the C<SO_REUSEPORT> sockopt (not all OSes implement this sockopt)
266  
267 =item Broadcast => BOOL
268  
269 If true, set the C<SO_BROADCAST> sockopt
270  
271 =item Sockopts => ARRAY
272  
273 An optional array of other socket options to apply after the three listed
274 above. The value is an ARRAY containing 2- or 3-element ARRAYrefs. Each inner
275 array relates to a single option, giving the level and option name, and an
276 optional value. If the value element is missing, it will be given the value of
277 a platform-sized integer 1 constant (i.e. suitable to enable most of the
278 common boolean options).
279  
280 For example, both options given below are equivalent to setting C<ReuseAddr>.
281  
282 Sockopts => [
283 [ SOL_SOCKET, SO_REUSEADDR ],
284 [ SOL_SOCKET, SO_REUSEADDR, pack( "i", 1 ) ],
285 ]
286  
287 =item V6Only => BOOL
288  
289 If defined, set the C<IPV6_V6ONLY> sockopt when creating C<PF_INET6> sockets
290 to the given value. If true, a listening-mode socket will only listen on the
291 C<AF_INET6> addresses; if false it will also accept connections from
292 C<AF_INET> addresses.
293  
294 If not defined, the socket option will not be changed, and default value set
295 by the operating system will apply. For repeatable behaviour across platforms
296 it is recommended this value always be defined for listening-mode sockets.
297  
298 Note that not all platforms support disabling this option. Some, at least
299 OpenBSD and MirBSD, will fail with C<EINVAL> if you attempt to disable it.
300 To determine whether it is possible to disable, you may use the class method
301  
302 if( IO::Socket::IP->CAN_DISABLE_V6ONLY ) {
303 ...
304 }
305 else {
306 ...
307 }
308  
309 If your platform does not support disabling this option but you still want to
310 listen for both C<AF_INET> and C<AF_INET6> connections you will have to create
311 two listening sockets, one bound to each protocol.
312  
313 =item MultiHomed
314  
315 This C<IO::Socket::INET>-style argument is ignored, except if it is defined
316 but false. See the C<IO::Socket::INET> INCOMPATIBILITES section below.
317  
318 However, the behaviour it enables is always performed by C<IO::Socket::IP>.
319  
320 =item Blocking => BOOL
321  
322 If defined but false, the socket will be set to non-blocking mode. Otherwise
323 it will default to blocking mode. See the NON-BLOCKING section below for more
324 detail.
325  
326 =item Timeout => NUM
327  
328 If defined, gives a maximum time in seconds to block per C<connect()> call
329 when in blocking mode. If missing, no timeout is applied other than that
330 provided by the underlying operating system. When in non-blocking mode this
331 parameter is ignored.
332  
333 Note that if the hostname resolves to multiple address candidates, the same
334 timeout will apply to each connection attempt individually, rather than to the
335 operation as a whole. Further note that the timeout does not apply to the
336 initial hostname resolve operation, if connecting by hostname.
337  
338 This behaviour is copied inspired by C<IO::Socket::INET>; for more fine
339 grained control over connection timeouts, consider performing a nonblocking
340 connect directly.
341  
342 =back
343  
344 If neither C<Type> nor C<Proto> hints are provided, a default of
345 C<SOCK_STREAM> and C<IPPROTO_TCP> respectively will be set, to maintain
346 compatibility with C<IO::Socket::INET>. Other named arguments that are not
347 recognised are ignored.
348  
349 If neither C<Family> nor any hosts or addresses are passed, nor any
350 C<*AddrInfo>, then the constructor has no information on which to decide a
351 socket family to create. In this case, it performs a C<getaddinfo> call with
352 the C<AI_ADDRCONFIG> flag, no host name, and a service name of C<"0">, and
353 uses the family of the first returned result.
354  
355 If the constructor fails, it will set C<$IO::Socket::errstr> and C<$@> to
356 an appropriate error message; this may be from C<$!> or it may be some other
357 string; not every failure necessarily has an associated C<errno> value.
358  
359 =head2 new (one arg)
360  
361 $sock = IO::Socket::IP->new( $peeraddr )
362  
363 As a special case, if the constructor is passed a single argument (as
364 opposed to an even-sized list of key/value pairs), it is taken to be the value
365 of the C<PeerAddr> parameter. This is parsed in the same way, according to the
366 behaviour given in the C<PeerHost> AND C<LocalHost> PARSING section below.
367  
368 =cut
369  
370 sub new
371 22 {
372 61 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
373 61 ▶ my %arg = (@_ == 1) ? (PeerHost => $_[0]) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 10 51 61
374 61 return $class->SUPER::new(%arg);
375 }
376  
377 # IO::Socket may call this one; neaten up the arguments from IO::Socket::INET
378 # before calling our real _configure method
379 sub configure
380 {
381 55 ▶ my $self = shift;
Subroutine configure: called CC=12 SCAR=26.0
Pod undocumented
382 55 my ( $arg ) = @_;
383  
384 $arg->{PeerHost} = delete $arg->{PeerAddr}
385 55 ▶ if exists $arg->{PeerAddr} && !exists $arg->{PeerHost};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg->{"PeerAddr"} and not exists $arg->{"PeerHost"} 0 55 55
Condition33%1/3
exists $arg->{"PeerAddr"} and not exists $arg->{"PeerHost"}
!ll&&!rl&&r
5500
MC/DC0%0/2
exists $arg->{"PeerAddr"} and not exists $arg->{"PeerHost"}
exists $arg->{"PeerAddr"} not exists $arg->{"PeerHost"}
Truth table33%1/3
A and not(B)
A= exists $arg->{"PeerAddr"}B= not exists $arg->{"PeerHost"}
ABresult
0X0
100
111
386  
387 $arg->{PeerService} = delete $arg->{PeerPort}
388 55 ▶ if exists $arg->{PeerPort} && !exists $arg->{PeerService};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg->{"PeerPort"} and not exists $arg->{"PeerService"} 5 50 55
Condition66%2/3
exists $arg->{"PeerPort"} and not exists $arg->{"PeerService"}
!ll&&!rl&&r
5005
MC/DC50%1/2
exists $arg->{"PeerPort"} and not exists $arg->{"PeerService"}
exists $arg->{"PeerPort"} not exists $arg->{"PeerService"}
Truth table66%2/3
A and not(B)
A= exists $arg->{"PeerPort"}B= not exists $arg->{"PeerService"}
ABresult
0X0
100
111
389  
390 $arg->{LocalHost} = delete $arg->{LocalAddr}
391 55 ▶ if exists $arg->{LocalAddr} && !exists $arg->{LocalHost};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg->{"LocalAddr"} and not exists $arg->{"LocalHost"} 0 55 55
Condition33%1/3
exists $arg->{"LocalAddr"} and not exists $arg->{"LocalHost"}
!ll&&!rl&&r
5500
MC/DC0%0/2
exists $arg->{"LocalAddr"} and not exists $arg->{"LocalHost"}
exists $arg->{"LocalAddr"} not exists $arg->{"LocalHost"}
Truth table33%1/3
A and not(B)
A= exists $arg->{"LocalAddr"}B= not exists $arg->{"LocalHost"}
ABresult
0X0
100
111
392  
393 $arg->{LocalService} = delete $arg->{LocalPort}
394 55 ▶ if exists $arg->{LocalPort} && !exists $arg->{LocalService};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg->{"LocalPort"} and not exists $arg->{"LocalService"} 13 42 55
Condition66%2/3
exists $arg->{"LocalPort"} and not exists $arg->{"LocalService"}
!ll&&!rl&&r
42013
MC/DC50%1/2
exists $arg->{"LocalPort"} and not exists $arg->{"LocalService"}
exists $arg->{"LocalPort"} not exists $arg->{"LocalService"}
Truth table66%2/3
A and not(B)
A= exists $arg->{"LocalPort"}B= not exists $arg->{"LocalService"}
ABresult
0X0
100
111
395  
396 55 for my $type (qw(Peer Local)) {
397 110 my $host = $type . 'Host';
398 110 my $service = $type . 'Service';
399  
400 110 ▶ if( defined $arg->{$host} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{$host}) 47 63 110
401 47 ( $arg->{$host}, my $s ) = $self->split_addr( $arg->{$host} );
402 # IO::Socket::INET compat - *Host parsed port always takes precedence
403 47 ▶ $arg->{$service} = $s if defined $s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 10 37 47
404 }
405 }
406  
407 55 $self->_io_socket_ip__configure( $arg );
408 }
409  
410 # Avoid simply calling it _configure, as some subclasses of IO::Socket::INET on CPAN already take that
411 sub _io_socket_ip__configure
412 {
413 39 ▶ my $self = shift;
Subroutine _io_socket_ip__configure: called CC=77 SCAR=53.6
414 39 my ( $arg ) = @_;
415  
416 39 my %hints;
417 my @localinfos;
418 39 my @peerinfos;
419  
420 39 my $listenqueue = $arg->{Listen};
421 39 ▶ if( defined $listenqueue and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listenqueue and defined $arg->{"PeerHost"} || defined $arg->{"PeerService"} || defined $arg->{"PeerAddrInfo"}) 0 39 39
Condition44%4/9
defined $listenqueue and defined $arg->{"PeerHost"} || defined $arg->{"PeerService"} || defined $arg->{"PeerAddrInfo"}
!ll&&!rl&&r
28110
defined $arg->{"PeerHost"} or defined $arg->{"PeerService"} or defined $arg->{"PeerAddrInfo"}
l!l&&r!l&&!r
0011
defined $arg->{"PeerHost"} or defined $arg->{"PeerService"}
l!l&&r!l&&!r
0011
MC/DC0%0/4
defined $listenqueue and defined $arg->{"PeerHost"} || defined $arg->{"PeerService"} || defined $arg->{"PeerAddrInfo"}
defined $listenqueue defined $arg->{"PeerHost"} defined $arg->{"PeerService"} defined $arg->{"PeerAddrInfo"}
Truth table40%2/5
A and B or C or D
A= defined $listenqueueB= defined $arg->{"PeerHost"}C= defined $arg->{"PeerService"}D= defined $arg->{"PeerAddrInfo"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
422 ( defined $arg->{PeerHost} || defined $arg->{PeerService} || defined $arg->{PeerAddrInfo} ) ) {
423 0 croak "Cannot Listen with a peer address";
424 }
425  
426 39 ▶ if( defined $arg->{GetAddrInfoFlags} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{'GetAddrInfoFlags'}) { } 6 33 39
427 6 $hints{flags} = $arg->{GetAddrInfoFlags};
428 }
429 else {
430 33 $hints{flags} = $AI_ADDRCONFIG;
431 }
432  
433 39 ▶ if( defined( my $family = $arg->{Family} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $family = $arg->{"Family"})) 3 36 39
434 3 $hints{family} = $family;
435 }
436  
437 39 ▶ if( defined( my $type = $arg->{Type} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $type = $arg->{"Type"})) 23 16 39
438 23 $hints{socktype} = $type;
439 }
440  
441 39 ▶ if( defined( my $proto = $arg->{Proto} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $proto = $arg->{"Proto"})) 0 39 39
442 0 ▶ unless( $proto =~ m/^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($proto =~ /^\d+$/u) 0 0 0
443 my $protonum = HAVE_GETPROTOBYNAME
444 ? getprotobyname( $proto )
445 0 : eval { Socket->${\"IPPROTO_\U$proto"}() };
446 0 ▶ defined $protonum or croak "Unrecognised protocol $proto";
Branch: 1 branch
BranchTrueFalseTotal
defined $protonum or croak("Unrecognised protocol $proto") 0 0 0
447 0 $proto = $protonum;
448 }
449  
450 0 $hints{protocol} = $proto;
451 }
452  
453 # To maintain compatibility with IO::Socket::INET, imply a default of
454 # SOCK_STREAM + IPPROTO_TCP if neither hint is given
455 39 ▶ if( !defined $hints{socktype} and !defined $hints{protocol} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hints{"socktype"} or defined $hints{"protocol"}) 16 23 39
Condition66%2/3
defined $hints{"socktype"} or defined $hints{"protocol"}
l!l&&r!l&&!r
23016
MC/DC50%1/2
defined $hints{"socktype"} or defined $hints{"protocol"}
defined $hints{"socktype"} defined $hints{"protocol"}
Truth table66%2/3
A or B
A= defined $hints{"socktype"}B= defined $hints{"protocol"}
ABresult
1X1
011
000
456 16 $hints{socktype} = SOCK_STREAM;
457 16 $hints{protocol} = IPPROTO_TCP;
458 }
459  
460 # Some OSes (NetBSD) don't seem to like just a protocol hint without a
461 # socktype hint as well. We'll set a couple of common ones
462 39 ▶ if( !defined $hints{socktype} and defined $hints{protocol} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $hints{"socktype"} and defined $hints{"protocol"}) 0 39 39
Condition33%1/3
not defined $hints{"socktype"} and defined $hints{"protocol"}
!ll&&!rl&&r
3900
MC/DC0%0/2
not defined $hints{"socktype"} and defined $hints{"protocol"}
not defined $hints{"socktype"} defined $hints{"protocol"}
Truth table33%1/3
not(A) and B
A= not defined $hints{"socktype"}B= defined $hints{"protocol"}
ABresult
0X0
100
111
463 0 ▶ $hints{socktype} = SOCK_STREAM if $hints{protocol} == IPPROTO_TCP;
Branch: 1 branch
BranchTrueFalseTotal
if $hints{"protocol"} == 6 0 0 0
464 0 ▶ $hints{socktype} = SOCK_DGRAM if $hints{protocol} == IPPROTO_UDP;
Branch: 1 branch
BranchTrueFalseTotal
if $hints{"protocol"} == 17 0 0 0
465 }
466  
467 39 ▶ if( my $info = $arg->{LocalAddrInfo} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $info = $arg->{'LocalAddrInfo'}) { } 1 38 39
elsif (defined $arg->{'LocalHost'} or defined $arg->{'LocalService'} or 0) { } 23 15 38
468 1 ▶ ref $info eq "ARRAY" or croak "Expected 'LocalAddrInfo' to be an ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $info eq "ARRAY" or croak("Expected 'LocalAddrInfo' to be an ARRAY ref") 0 1 1
469 1 @localinfos = @$info;
470 }
471 ▶ elsif( defined $arg->{LocalHost} or
Condition100%5/5
defined $arg->{"LocalHost"} or defined $arg->{"LocalService"} or !!0
l!l
2315
defined $arg->{"LocalHost"} or defined $arg->{"LocalService"}
l!l&&r!l&&!r
20315
MC/DC100%2/2
defined $arg->{"LocalHost"} or defined $arg->{"LocalService"} or !!0
defined $arg->{"LocalHost"} defined $arg->{"LocalService"}
Truth table100%3/3
A or B
A= defined $arg->{"LocalHost"}B= defined $arg->{"LocalService"}
ABresult
1X1
011
000
472 defined $arg->{LocalService} or
473 HAVE_MSWIN32 and $arg->{Listen} ) {
474 # Either may be undef
475 23 my $host = $arg->{LocalHost};
476 23 my $service = $arg->{LocalService};
477  
478 23 ▶ unless ( defined $host or defined $service ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host or defined $service) 0 23 23
Condition66%2/3
defined $host or defined $service
l!l&&r!l&&!r
2030
MC/DC0%0/2
defined $host or defined $service
defined $host defined $service
Truth table66%2/3
A or B
A= defined $hostB= defined $service
ABresult
1X1
011
000
479 0 $service = 0;
480 }
481  
482 23 local $1; # Placate a taint-related bug; [perl #67962]
483 23 ▶ defined $service and $service =~ s/\((\d+)\)$// and
Branch: 1 branch
BranchTrueFalseTotal
defined $service and $service =~ s/\((\d+)\)$//u and my $fallback_port = $1 1 22 23
Condition100%3/3
defined $service and $service =~ s/\((\d+)\)$//u
!ll&&!rl&&r
10121
MC/DC100%2/2
defined $service and $service =~ s/\((\d+)\)$//u
defined $service $service =~ s/\((\d+)\)$//u
Truth table100%3/3
A and B
A= defined $serviceB= $service =~ s/\((\d+)\)$//u
ABresult
0X0
100
111
484 my $fallback_port = $1;
485  
486 23 my %localhints = %hints;
487 23 $localhints{flags} |= AI_PASSIVE;
488 23 ( my $err, @localinfos ) = getaddrinfo( $host, $service, \%localhints );
489  
490 23 ▶ if( $err and defined $fallback_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and defined $fallback_port) 1 22 23
Condition100%3/3
$err and defined $fallback_port
!ll&&!rl&&r
1751
MC/DC100%2/2
$err and defined $fallback_port
$err defined $fallback_port
Truth table100%3/3
A and B
A= $errB= defined $fallback_port
ABresult
0X0
100
111
491 1 ( $err, @localinfos ) = getaddrinfo( $host, $fallback_port, \%localhints );
492 }
493  
494 23 ▶ if( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 5 18 23
495 5 $IO::Socket::errstr = $@ = "$err";
496 5 $! = EINVAL;
497 5 return;
498 }
499 }
500  
501 34 ▶ if( my $info = $arg->{PeerAddrInfo} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $info = $arg->{'PeerAddrInfo'}) { } 1 33 34
elsif (defined $arg->{'PeerHost'} or defined $arg->{'PeerService'}) { } 12 21 33
502 1 ▶ ref $info eq "ARRAY" or croak "Expected 'PeerAddrInfo' to be an ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $info eq "ARRAY" or croak("Expected 'PeerAddrInfo' to be an ARRAY ref") 0 1 1
503 1 @peerinfos = @$info;
504 }
505 ▶ elsif( defined $arg->{PeerHost} or defined $arg->{PeerService} ) {
Condition66%2/3
defined $arg->{"PeerHost"} or defined $arg->{"PeerService"}
l!l&&r!l&&!r
12021
MC/DC50%1/2
defined $arg->{"PeerHost"} or defined $arg->{"PeerService"}
defined $arg->{"PeerHost"} defined $arg->{"PeerService"}
Truth table66%2/3
A or B
A= defined $arg->{"PeerHost"}B= defined $arg->{"PeerService"}
ABresult
1X1
011
000
506 12 ▶ defined( my $host = $arg->{PeerHost} ) or
Branch: 1 branch
BranchTrueFalseTotal
defined(my $host = $arg->{"PeerHost"}) or croak("Expected 'PeerHost'") 0 12 12
507 croak "Expected 'PeerHost'";
508 12 ▶ defined( my $service = $arg->{PeerService} ) or
Branch: 1 branch
BranchTrueFalseTotal
defined(my $service = $arg->{"PeerService"}) or croak("Expected 'PeerService'") 0 12 12
509 croak "Expected 'PeerService'";
510  
511 12 local $1; # Placate a taint-related bug; [perl #67962]
512 12 ▶ defined $service and $service =~ s/\((\d+)\)$// and
Branch: 1 branch
BranchTrueFalseTotal
defined $service and $service =~ s/\((\d+)\)$//u and my $fallback_port = $1 0 12 12
Condition33%1/3
defined $service and $service =~ s/\((\d+)\)$//u
!ll&&!rl&&r
0120
MC/DC0%0/2
defined $service and $service =~ s/\((\d+)\)$//u
defined $service $service =~ s/\((\d+)\)$//u
Truth table33%1/3
A and B
A= defined $serviceB= $service =~ s/\((\d+)\)$//u
ABresult
0X0
100
111
513 my $fallback_port = $1;
514  
515 12 ( my $err, @peerinfos ) = getaddrinfo( $host, $service, \%hints );
516  
517 12 ▶ if( $err and defined $fallback_port ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and defined $fallback_port) 0 12 12
Condition33%1/3
$err and defined $fallback_port
!ll&&!rl&&r
1200
MC/DC0%0/2
$err and defined $fallback_port
$err defined $fallback_port
Truth table33%1/3
A and B
A= $errB= defined $fallback_port
ABresult
0X0
100
111
518 0 ( $err, @peerinfos ) = getaddrinfo( $host, $fallback_port, \%hints );
519 }
520  
521 12 ▶ if( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 12 12
522 0 $IO::Socket::errstr = $@ = "$err";
523 0 $! = EINVAL;
524 0 return;
525 }
526 }
527  
528 34 my $INT_1 = pack "i", 1;
529  
530 34 my @sockopts_enabled;
531 34 ▶ push @sockopts_enabled, [ SOL_SOCKET, SO_REUSEADDR, $INT_1 ] if $arg->{ReuseAddr};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"ReuseAddr"} 1 33 34
532 34 ▶ push @sockopts_enabled, [ SOL_SOCKET, SO_REUSEPORT, $INT_1 ] if $arg->{ReusePort};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"ReusePort"} 1 33 34
533 34 ▶ push @sockopts_enabled, [ SOL_SOCKET, SO_BROADCAST, $INT_1 ] if $arg->{Broadcast};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"Broadcast"} 1 33 34
534  
535 34 ▶ if( my $sockopts = $arg->{Sockopts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sockopts = $arg->{"Sockopts"}) 1 33 34
536 1 ▶ ref $sockopts eq "ARRAY" or croak "Expected 'Sockopts' to be an ARRAY ref";
Branch: 1 branch
BranchTrueFalseTotal
ref $sockopts eq "ARRAY" or croak("Expected 'Sockopts' to be an ARRAY ref") 0 1 1
537 1 foreach ( @$sockopts ) {
538 1 ▶ ref $_ eq "ARRAY" or croak "Bad Sockopts item - expected ARRAYref";
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "ARRAY" or croak("Bad Sockopts item - expected ARRAYref") 0 1 1
539 1 ▶ @$_ >= 2 and @$_ <= 3 or
Branch: 1 branch
BranchTrueFalseTotal
@$_ >= 2 and @$_ <= 3 or croak("Bad Sockopts item - expected 2 or 3 elements") 0 1 1
Condition33%1/3
@$_ >= 2 and @$_ <= 3
!ll&&!rl&&r
001
MC/DC0%0/2
@$_ >= 2 and @$_ <= 3
@$_ >= 2 @$_ <= 3
Truth table33%1/3
A and B
A= @$_ >= 2B= @$_ <= 3
ABresult
0X0
100
111
540 croak "Bad Sockopts item - expected 2 or 3 elements";
541  
542 1 my ( $level, $optname, $value ) = @$_;
543 # TODO: consider more sanity checking on argument values
544  
545 1 ▶ defined $value or $value = $INT_1;
Branch: 1 branch
BranchTrueFalseTotal
defined $value or $value = $INT_1 1 0 1
546 1 push @sockopts_enabled, [ $level, $optname, $value ];
547 }
548 }
549  
550 34 my $blocking = $arg->{Blocking};
551 34 ▶ defined $blocking or $blocking = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $blocking or $blocking = 1 31 3 34
552  
553 34 my $v6only = $arg->{V6Only};
554  
555 # IO::Socket::INET defines this key. IO::Socket::IP always implements the
556 # behaviour it requests, so we can ignore it, unless the caller is for some
557 # reason asking to disable it.
558 34 ▶ if( defined $arg->{MultiHomed} and !$arg->{MultiHomed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"MultiHomed"} and not $arg->{"MultiHomed"}) 0 34 34
Condition33%1/3
defined $arg->{"MultiHomed"} and not $arg->{"MultiHomed"}
!ll&&!rl&&r
3400
MC/DC0%0/2
defined $arg->{"MultiHomed"} and not $arg->{"MultiHomed"}
defined $arg->{"MultiHomed"} not $arg->{"MultiHomed"}
Truth table33%1/3
A and not(B)
A= defined $arg->{"MultiHomed"}B= not $arg->{"MultiHomed"}
ABresult
0X0
100
111
559 0 croak "Cannot disable the MultiHomed parameter";
560 }
561  
562 34 my @infos;
563 34 ▶ foreach my $local ( @localinfos ? @localinfos : {} ) {
Branch: 1 branch
BranchTrueFalseTotal
@localinfos ? : 19 15 34
564 34 ▶ foreach my $peer ( @peerinfos ? @peerinfos : {} ) {
Branch: 1 branch
BranchTrueFalseTotal
@peerinfos ? : 13 21 34
565 next if defined $local->{family} and defined $peer->{family} and
566 34 ▶ $local->{family} != $peer->{family};
Branch: 1 branch
BranchTrueFalseTotal
if defined $local->{"family"} and defined $peer->{"family"} and $local->{"family"} != $peer->{"family"} 0 34 34
Condition50%3/6
defined $local->{"family"} and defined $peer->{"family"} and $local->{"family"} != $peer->{"family"}
!ll&&!rl&&r
3400
defined $local->{"family"} and defined $peer->{"family"}
!ll&&!rl&&r
15190
MC/DC0%0/3
defined $local->{"family"} and defined $peer->{"family"} and $local->{"family"} != $peer->{"family"}
defined $local->{"family"} defined $peer->{"family"} $local->{"family"} != $peer->{"family"}
Truth table50%2/4
A and B and C
A= defined $local->{"family"}B= defined $peer->{"family"}C= $local->{"family"} != $peer->{"family"}
ABCresult
0XX0
10X0
1100
1111
567 next if defined $local->{socktype} and defined $peer->{socktype} and
568 34 ▶ $local->{socktype} != $peer->{socktype};
Branch: 1 branch
BranchTrueFalseTotal
if defined $local->{"socktype"} and defined $peer->{"socktype"} and $local->{"socktype"} != $peer->{"socktype"} 0 34 34
Condition50%3/6
defined $local->{"socktype"} and defined $peer->{"socktype"} and $local->{"socktype"} != $peer->{"socktype"}
!ll&&!rl&&r
3400
defined $local->{"socktype"} and defined $peer->{"socktype"}
!ll&&!rl&&r
15190
MC/DC0%0/3
defined $local->{"socktype"} and defined $peer->{"socktype"} and $local->{"socktype"} != $peer->{"socktype"}
defined $local->{"socktype"} defined $peer->{"socktype"} $local->{"socktype"} != $peer->{"socktype"}
Truth table50%2/4
A and B and C
A= defined $local->{"socktype"}B= defined $peer->{"socktype"}C= $local->{"socktype"} != $peer->{"socktype"}
ABCresult
0XX0
10X0
1100
1111
569 next if defined $local->{protocol} and defined $peer->{protocol} and
570 34 ▶ $local->{protocol} != $peer->{protocol};
Branch: 1 branch
BranchTrueFalseTotal
if defined $local->{"protocol"} and defined $peer->{"protocol"} and $local->{"protocol"} != $peer->{"protocol"} 0 34 34
Condition50%3/6
defined $local->{"protocol"} and defined $peer->{"protocol"} and $local->{"protocol"} != $peer->{"protocol"}
!ll&&!rl&&r
3400
defined $local->{"protocol"} and defined $peer->{"protocol"}
!ll&&!rl&&r
15190
MC/DC0%0/3
defined $local->{"protocol"} and defined $peer->{"protocol"} and $local->{"protocol"} != $peer->{"protocol"}
defined $local->{"protocol"} defined $peer->{"protocol"} $local->{"protocol"} != $peer->{"protocol"}
Truth table50%2/4
A and B and C
A= defined $local->{"protocol"}B= defined $peer->{"protocol"}C= $local->{"protocol"} != $peer->{"protocol"}
ABCresult
0XX0
10X0
1100
1111
571  
572 34 ▶ my $family = $local->{family} || $peer->{family} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $family = $local->{"family"} || $peer->{"family"} or next 2 32 34
Condition100%3/3
$local->{'family'} || $peer->{'family'}
l!l&&r!l&&!r
19132
MC/DC100%2/2
$local->{'family'} || $peer->{'family'}
$local->{'family'} $peer->{'family'}
Truth table100%3/3
A || B
A= $local->{'family'}B= $peer->{'family'}
ABresult
1X1
011
000
573 32 ▶ my $socktype = $local->{socktype} || $peer->{socktype} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $socktype = $local->{"socktype"} || $peer->{"socktype"} or next 0 32 32
Condition66%2/3
$local->{'socktype'} || $peer->{'socktype'}
l!l&&r!l&&!r
19130
MC/DC0%0/2
$local->{'socktype'} || $peer->{'socktype'}
$local->{'socktype'} $peer->{'socktype'}
Truth table66%2/3
A || B
A= $local->{'socktype'}B= $peer->{'socktype'}
ABresult
1X1
011
000
574 32 ▶ my $protocol = $local->{protocol} || $peer->{protocol} || 0;
Condition60%3/5
$local->{'protocol'} || $peer->{'protocol'} || 0
l!l
320
$local->{"protocol"} or $peer->{"protocol"}
l!l&&r!l&&!r
19130
MC/DC0%0/2
$local->{'protocol'} || $peer->{'protocol'} || 0
$local->{"protocol"} $peer->{"protocol"}
Truth table66%2/3
A or B
A= $local->{"protocol"}B= $peer->{"protocol"}
ABresult
1X1
011
000
575  
576 push @infos, {
577 family => $family,
578 socktype => $socktype,
579 protocol => $protocol,
580 localaddr => $local->{addr},
581 peeraddr => $peer->{addr},
582 32 };
583 }
584 }
585  
586 34 ▶ if( !@infos ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@infos) 2 32 34
587 # If there was a Family hint then create a plain unbound, unconnected socket
588 2 ▶ if( defined $hints{family} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hints{'family'}) { } 1 1 2
589 @infos = ( {
590 family => $hints{family},
591 socktype => $hints{socktype},
592 protocol => $hints{protocol},
593 1 } );
594 }
595 # If there wasn't, use getaddrinfo()'s AI_ADDRCONFIG side-effect to guess a
596 # suitable family first.
597 else {
598 1 ( my $err, @infos ) = getaddrinfo( "", "0", \%hints );
599 1 ▶ if( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 1 1
600 0 $IO::Socket::errstr = $@ = "$err";
601 0 $! = EINVAL;
602 0 return;
603 }
604  
605 # We'll take all the @infos anyway, because some OSes (HPUX) are known to
606 # ignore the AI_ADDRCONFIG hint and return AF_INET6 even if they don't
607 # support them
608 }
609 }
610  
611 # In the nonblocking case, caller will be calling ->setup multiple times.
612 # Store configuration in the object for the ->setup method
613 # Yes, these are messy. Sorry, I can't help that...
614  
615 34 ${*$self}{io_socket_ip_infos} = \@infos;
616  
617 34 ${*$self}{io_socket_ip_idx} = -1;
618  
619 34 ${*$self}{io_socket_ip_sockopts} = \@sockopts_enabled;
620 34 ${*$self}{io_socket_ip_v6only} = $v6only;
621 34 ${*$self}{io_socket_ip_listenqueue} = $listenqueue;
622 34 ${*$self}{io_socket_ip_blocking} = $blocking;
623  
624 34 ${*$self}{io_socket_ip_errors} = [ undef, undef, undef ];
625  
626 # ->setup is allowed to return false in nonblocking mode
627 34 ▶ $self->setup or !$blocking or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->setup or not $blocking or return undef 0 34 34
Condition66%2/3
$self->setup or not $blocking
l!l&&r!l&&!r
3130
MC/DC0%0/2
$self->setup or not $blocking
$self->setup not $blocking
Truth table66%2/3
A or not(B)
A= $self->setupB= not $blocking
ABresult
1X1
011
000
628  
629 34 return $self;
630 }
631  
632 sub setup
633 {
634 35 ▶ my $self = shift;
Subroutine setup: called CC=19 SCAR=33.2
Pod undocumented
635  
636 35 while(1) {
637 35 ${*$self}{io_socket_ip_idx}++;
638 35 ▶ last if ${*$self}{io_socket_ip_idx} >= @{ ${*$self}{io_socket_ip_infos} };
Branch: 1 branch
BranchTrueFalseTotal
if do { *$self }->{"io_socket_ip_idx"} >= @{do { *$self }->{"io_socket_ip_infos"};} 1 34 35
639  
640 34 my $info = ${*$self}{io_socket_ip_infos}->[${*$self}{io_socket_ip_idx}];
641  
642 34 $self->socket( @{$info}{qw( family socktype protocol )} ) or
643 34 ▶ ( ${*$self}{io_socket_ip_errors}[2] = $!, next );
Branch: 1 branch
BranchTrueFalseTotal
$self->socket(@{$info;}{"family", "socktype", "protocol"}) or do { *$self }->{"io_socket_ip_errors"}[2] = $!, next 0 34 34
644  
645 34 ▶ $self->blocking( 0 ) unless ${*$self}{io_socket_ip_blocking};
Branch: 1 branch
BranchTrueFalseTotal
unless do { *$self }->{"io_socket_ip_blocking"} 3 31 34
646  
647 34 foreach my $sockopt ( @{ ${*$self}{io_socket_ip_sockopts} } ) {
648 4 my ( $level, $optname, $value ) = @$sockopt;
649 4 ▶ $self->setsockopt( $level, $optname, $value ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->setsockopt($level, $optname, $value) or $IO::Socket::errstr = $@ = "$!", (return undef) 0 4 4
650 ( $IO::Socket::errstr = $@ = "$!", return undef );
651 }
652  
653 34 ▶ if( defined ${*$self}{io_socket_ip_v6only} and defined $AF_INET6 and $info->{family} == $AF_INET6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined do { *$self }->{"io_socket_ip_v6only"} and defined $AF_INET6 and $info->{"family"} == $AF_INET6) 0 34 34
Condition33%2/6
defined do { *$self }->{"io_socket_ip_v6only"} and defined $AF_INET6 and $info->{"family"} == $AF_INET6
!ll&&!rl&&r
3400
defined do { *$self }->{"io_socket_ip_v6only"} and defined $AF_INET6
!ll&&!rl&&r
3400
MC/DC0%0/3
defined do { *$self }->{"io_socket_ip_v6only"} and defined $AF_INET6 and $info->{"family"} == $AF_INET6
defined do { *$self }->{"io_socket_ip_v6only"} defined $AF_INET6 $info->{"family"} == $AF_INET6
Truth table25%1/4
A and B and C
A= defined do { *$self }->{"io_socket_ip_v6only"}B= defined $AF_INET6C= $info->{"family"} == $AF_INET6
ABCresult
0XX0
10X0
1100
1111
654 0 my $v6only = ${*$self}{io_socket_ip_v6only};
655 0 ▶ $self->setsockopt( IPPROTO_IPV6, IPV6_V6ONLY, pack "i", $v6only ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->setsockopt(41, 26, pack("i", $v6only)) or $IO::Socket::errstr = $@ = "$!", (return undef) 0 0 0
656 ( $IO::Socket::errstr = $@ = "$!", return undef );
657 }
658  
659 34 ▶ if( defined( my $addr = $info->{localaddr} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $addr = $info->{"localaddr"})) 19 15 34
660 $self->bind( $addr ) or
661 19 ▶ ( ${*$self}{io_socket_ip_errors}[1] = $!, next );
Branch: 1 branch
BranchTrueFalseTotal
$self->bind($addr) or do { *$self }->{"io_socket_ip_errors"}[1] = $!, next 0 19 19
662 }
663  
664 34 ▶ if( defined( my $listenqueue = ${*$self}{io_socket_ip_listenqueue} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $listenqueue = do { *$self }->{"io_socket_ip_listenqueue"})) 11 23 34
665 11 ▶ $self->listen( $listenqueue ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->listen($listenqueue) or $IO::Socket::errstr = $@ = "$!", (return undef) 0 11 11
666 ( $IO::Socket::errstr = $@ = "$!", return undef );
667 }
668  
669 34 ▶ if( defined( my $addr = $info->{peeraddr} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $addr = $info->{"peeraddr"})) 13 21 34
670 13 ▶ if( $self->connect( $addr ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->connect($addr)) 10 3 13
671 10 $! = 0;
672 10 return 1;
673 }
674  
675 3 ▶ if( $! == EINPROGRESS or $! == EWOULDBLOCK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 115 or $! == 11) 3 0 3
Condition33%1/3
$! == 115 or $! == 11
l!l&&r!l&&!r
300
MC/DC0%0/2
$! == 115 or $! == 11
$! == 115 $! == 11
Truth table33%1/3
A or B
A= $! == 115B= $! == 11
ABresult
1X1
011
000
676 3 ${*$self}{io_socket_ip_connect_in_progress} = 1;
677 3 return 0;
678 }
679  
680 # If connect failed but we have no system error there must be an error
681 # at the application layer, like a bad certificate with
682 # IO::Socket::SSL.
683 # In this case don't continue IP based multi-homing because the problem
684 # cannot be solved at the IP layer.
685 0 ▶ return 0 if ! $!;
Branch: 1 branch
BranchTrueFalseTotal
unless $! 0 0 0
686  
687 0 ${*$self}{io_socket_ip_errors}[0] = $!;
688 0 next;
689 }
690  
691 21 return 1;
692 }
693  
694 # Pick the most appropriate error, stringified
695 1 $! = ( grep defined, @{ ${*$self}{io_socket_ip_errors}} )[0];
696 1 $IO::Socket::errstr = $@ = "$!";
697 1 return undef;
698 }
699  
700 sub connect :method
701 {
702 21 ▶ my $self = shift;
Subroutine connect: called CC=15 SCAR=29.4
Pod undocumented
703  
704 # It seems that IO::Socket hides EINPROGRESS errors, making them look like
705 # a success. This is annoying here.
706 # Instead of putting up with its frankly-irritating intentional breakage of
707 # useful APIs I'm just going to end-run around it and call core's connect()
708 # directly
709  
710 21 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 16 5 21
711 16 my ( $addr ) = @_;
712  
713 # Annoyingly IO::Socket's connect() is where the timeout logic is
714 # implemented, so we'll have to reinvent it here
715 16 my $timeout = ${*$self}{'io_socket_timeout'};
716  
717 16 ▶ return connect( $self, $addr ) unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 15 1 16
718  
719 1 my $was_blocking = $self->blocking( 0 );
720  
721 1 ▶ my $err = defined connect( $self, $addr ) ? 0 : $!+0;
Branch: 1 branch
BranchTrueFalseTotal
defined CORE::connect($self, $addr) ? : 0 1 1
722  
723 1 ▶ if( !$err ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $err) { } 0 1 1
elsif (not $err == 115 || $err == 11) { } 0 1 1
elsif (not $was_blocking) { } 0 1 1
724 # All happy
725 0 $self->blocking( $was_blocking );
726 0 return 1;
727 }
728 ▶ elsif( not( $err == EINPROGRESS or $err == EWOULDBLOCK ) ) {
Condition33%1/3
$err == 115 || $err == 11
l!l&&r!l&&!r
100
MC/DC0%0/2
$err == 115 || $err == 11
$err == 115 $err == 11
Truth table33%1/3
A || B
A= $err == 115B= $err == 11
ABresult
1X1
011
000
729 # Failed for some other reason
730 0 $self->blocking( $was_blocking );
731 0 return undef;
732 }
733 elsif( !$was_blocking ) {
734 # We shouldn't block anyway
735 0 return undef;
736 }
737  
738 1 my $vec = ''; vec( $vec, $self->fileno, 1 ) = 1;
739 1 ▶ if( !select( undef, $vec, $vec, $timeout ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (select undef, $vec, $vec, $timeout) 0 1 1
740 0 $self->blocking( $was_blocking );
741 0 $! = ETIMEDOUT;
742 0 return undef;
743 }
744  
745 # Hoist the error by connect()ing a second time
746 1 $err = $self->getsockopt( SOL_SOCKET, SO_ERROR );
747 1 ▶ $err = 0 if $err == EISCONN; # Some OSes give EISCONN
Branch: 1 branch
BranchTrueFalseTotal
if $err == 106 0 1 1
748  
749 1 $self->blocking( $was_blocking );
750  
751 1 ▶ $! = $err, return undef if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
752 1 return 1;
753 }
754  
755 5 ▶ return 1 if !${*$self}{io_socket_ip_connect_in_progress};
Branch: 1 branch
BranchTrueFalseTotal
unless do { *$self }->{"io_socket_ip_connect_in_progress"} 0 5 5
756  
757 # See if a connect attempt has just failed with an error
758 5 ▶ if( my $errno = $self->getsockopt( SOL_SOCKET, SO_ERROR ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $errno = $self->getsockopt(1, 4)) 1 4 5
759 1 delete ${*$self}{io_socket_ip_connect_in_progress};
760 1 ${*$self}{io_socket_ip_errors}[0] = $! = $errno;
761 1 return $self->setup;
762 }
763  
764 # No error, so either connect is still in progress, or has completed
765 # successfully. We can tell by trying to connect() again; either it will
766 # succeed or we'll get EISCONN (connected successfully), or EALREADY
767 # (still in progress). This even works on MSWin32.
768 4 my $addr = ${*$self}{io_socket_ip_infos}[${*$self}{io_socket_ip_idx}]{peeraddr};
769  
770 4 ▶ if( connect( $self, $addr ) or $! == EISCONN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::connect $self, $addr or $! == 106) { } 2 2 4
Condition66%2/3
CORE::connect $self, $addr or $! == 106
l!l&&r!l&&!r
202
MC/DC50%1/2
CORE::connect $self, $addr or $! == 106
CORE::connect $self, $addr $! == 106
Truth table66%2/3
A or B
A= CORE::connect $self, $addrB= $! == 106
ABresult
1X1
011
000
771 2 delete ${*$self}{io_socket_ip_connect_in_progress};
772 2 $! = 0;
773 2 return 1;
774 }
775 else {
776 2 $! = EINPROGRESS;
777 2 return 0;
778 }
779 }
780  
781 sub connected
782 {
783 11 ▶ my $self = shift;
Subroutine connected: called CC=3 SCAR=11.0
Pod documented
784 return defined $self->fileno &&
785 7 !${*$self}{io_socket_ip_connect_in_progress} &&
786 11 ▶ defined getpeername( $self ); # ->peername caches, we need to detect disconnection
Condition83%5/6
defined $self->fileno && !do { *$self }->{'io_socket_ip_connect_in_progress'} && defined getpeername $self
!ll&&!rl&&r
605
defined $self->fileno and not do { *$self }->{"io_socket_ip_connect_in_progress"}
!ll&&!rl&&r
425
MC/DC66%2/3
defined $self->fileno && !do { *$self }->{'io_socket_ip_connect_in_progress'} && defined getpeername $self
defined $self->fileno not do { *$self }->{"io_socket_ip_connect_in_progress"} defined getpeername $self
Truth table75%3/4
A and not(B) && C
A= defined $self->filenoB= not do { *$self }->{"io_socket_ip_connect_in_progress"}C= defined getpeername $self
ABCresult
0XX0
10X0
1100
1111
787 }
788  
789 =head1 METHODS
790  
791 As well as the following methods, this class inherits all the methods in
792 L<IO::Socket> and L<IO::Handle>.
793  
794 =cut
795  
796 sub _get_host_service
797 {
798 65 ▶ my $self = shift;
Subroutine _get_host_service: called CC=5 SCAR=16.2
799 65 my ( $addr, $flags, $xflags ) = @_;
800  
801 65 ▶ defined $addr or
Branch: 1 branch
BranchTrueFalseTotal
defined $addr or $! = 107, return 8 57 65
802 $! = ENOTCONN, return;
803  
804 57 ▶ $flags |= NI_DGRAM if $self->socktype == SOCK_DGRAM;
Branch: 1 branch
BranchTrueFalseTotal
if $self->socktype == 2 23 34 57
805  
806 57 ▶ my ( $err, $host, $service ) = getnameinfo( $addr, $flags, $xflags || 0 );
Condition50%1/2
$xflags || 0
l!l
570
MC/DC0%0/1
$xflags || 0
$xflags
Truth table50%1/2
A
A= $xflags
Aresult
11
00
807 57 ▶ croak "getnameinfo - $err" if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 57 57
808  
809 57 return ( $host, $service );
810 }
811  
812 sub _unpack_sockaddr
813 {
814 14 ▶ my ( $addr ) = @_;
Subroutine _unpack_sockaddr: called CC=4 SCAR=15.0
815 14 my $family = sockaddr_family $addr;
816  
817 14 ▶ if( $family == AF_INET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($family == 2) { } 8 6 14
elsif (defined $AF_INET6 and $family == $AF_INET6) { } 6 0 6
818 8 return ( Socket::unpack_sockaddr_in( $addr ) )[1];
819 }
820 ▶ elsif( defined $AF_INET6 and $family == $AF_INET6 ) {
Condition33%1/3
defined $AF_INET6 and $family == $AF_INET6
!ll&&!rl&&r
006
MC/DC0%0/2
defined $AF_INET6 and $family == $AF_INET6
defined $AF_INET6 $family == $AF_INET6
Truth table33%1/3
A and B
A= defined $AF_INET6B= $family == $AF_INET6
ABresult
0X0
100
111
821 6 return ( Socket::unpack_sockaddr_in6( $addr ) )[1];
822 }
823 else {
824 0 croak "Unrecognised address family $family";
825 }
826 }
827  
828 =head2 sockhost_service
829  
830 ( $host, $service ) = $sock->sockhost_service( $numeric )
831  
832 Returns the hostname and service name of the local address (that is, the
833 socket address given by the C<sockname> method).
834  
835 If C<$numeric> is true, these will be given in numeric form rather than being
836 resolved into names.
837  
838 The following four convenience wrappers may be used to obtain one of the two
839 values returned here. If both host and service names are required, this method
840 is preferable to the following wrappers, because it will call
841 C<getnameinfo(3)> only once.
842  
843 =cut
844  
845 sub sockhost_service
846 {
847 0 ▶ my $self = shift;
Subroutine sockhost_service: not called CC=2 SCAR=17.9
Pod documented
848 0 my ( $numeric ) = @_;
849  
850 0 ▶ $self->_get_host_service( $self->sockname, $numeric ? NI_NUMERICHOST|NI_NUMERICSERV : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$numeric ? : 0 0 0
851 }
852  
853 =head2 sockhost
854  
855 $addr = $sock->sockhost
856  
857 Return the numeric form of the local address as a textual representation
858  
859 =head2 sockport
860  
861 $port = $sock->sockport
862  
863 Return the numeric form of the local port number
864  
865 =head2 sockhostname
866  
867 $host = $sock->sockhostname
868  
869 Return the resolved name of the local address
870  
871 =head2 sockservice
872  
873 $service = $sock->sockservice
874  
875 Return the resolved name of the local port number
876  
877 =cut
878  
879 11 ▶sub sockhost { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, NI_NUMERICHOST, NIx_NOSERV ) )[0] }
Subroutine sockhost: called CC=1 SCAR=0.0
Pod documented
880 24 ▶sub sockport { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, NI_NUMERICSERV, NIx_NOHOST ) )[1] }
Subroutine sockport: called CC=1 SCAR=0.0
Pod documented
881  
882 0 ▶sub sockhostname { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, 0, NIx_NOSERV ) )[0] }
Subroutine sockhostname: not called CC=1 SCAR=6.9
Pod documented
883 0 ▶sub sockservice { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, 0, NIx_NOHOST ) )[1] }
Subroutine sockservice: not called CC=1 SCAR=6.9
Pod documented
884  
885 =head2 sockaddr
886  
887 $addr = $sock->sockaddr
888  
889 Return the local address as a binary octet string
890  
891 =cut
892  
893 6 ▶sub sockaddr { my $self = shift; _unpack_sockaddr $self->sockname }
Subroutine sockaddr: called CC=1 SCAR=0.0
Pod documented
894  
895 =head2 peerhost_service
896  
897 ( $host, $service ) = $sock->peerhost_service( $numeric )
898  
899 Returns the hostname and service name of the peer address (that is, the
900 socket address given by the C<peername> method), similar to the
901 C<sockhost_service> method.
902  
903 The following four convenience wrappers may be used to obtain one of the two
904 values returned here. If both host and service names are required, this method
905 is preferable to the following wrappers, because it will call
906 C<getnameinfo(3)> only once.
907  
908 =cut
909  
910 sub peerhost_service
911 {
912 0 ▶ my $self = shift;
Subroutine peerhost_service: not called CC=2 SCAR=17.9
Pod documented
913 0 my ( $numeric ) = @_;
914  
915 0 ▶ $self->_get_host_service( $self->peername, $numeric ? NI_NUMERICHOST|NI_NUMERICSERV : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$numeric ? : 0 0 0
916 }
917  
918 =head2 peerhost
919  
920 $addr = $sock->peerhost
921  
922 Return the numeric form of the peer address as a textual representation
923  
924 =head2 peerport
925  
926 $port = $sock->peerport
927  
928 Return the numeric form of the peer port number
929  
930 =head2 peerhostname
931  
932 $host = $sock->peerhostname
933  
934 Return the resolved name of the peer address
935  
936 =head2 peerservice
937  
938 $service = $sock->peerservice
939  
940 Return the resolved name of the peer port number
941  
942 =cut
943  
944 11 ▶sub peerhost { my $self = shift; scalar +( $self->_get_host_service( $self->peername, NI_NUMERICHOST, NIx_NOSERV ) )[0] }
Subroutine peerhost: called CC=1 SCAR=0.0
Pod documented
945 19 ▶sub peerport { my $self = shift; scalar +( $self->_get_host_service( $self->peername, NI_NUMERICSERV, NIx_NOHOST ) )[1] }
Subroutine peerport: called CC=1 SCAR=0.0
Pod documented
946  
947 0 ▶sub peerhostname { my $self = shift; scalar +( $self->_get_host_service( $self->peername, 0, NIx_NOSERV ) )[0] }
Subroutine peerhostname: not called CC=1 SCAR=6.9
Pod documented
948 0 ▶sub peerservice { my $self = shift; scalar +( $self->_get_host_service( $self->peername, 0, NIx_NOHOST ) )[1] }
Subroutine peerservice: not called CC=1 SCAR=6.9
Pod documented
949  
950 =head2 peeraddr
951  
952 $addr = $peer->peeraddr
953  
954 Return the peer address as a binary octet string
955  
956 =cut
957  
958 8 ▶sub peeraddr { my $self = shift; _unpack_sockaddr $self->peername }
Subroutine peeraddr: called CC=1 SCAR=0.0
Pod documented
959  
960 # This unbelievably dodgy hack works around the bug that IO::Socket doesn't do
961 # it
962 # https://rt.cpan.org/Ticket/Display.html?id=61577
963 sub accept
964 {
965 6 ▶ my $self = shift;
Subroutine accept: called CC=4 SCAR=14.2
Pod documented
966 6 ▶ my ( $new, $peer ) = $self->SUPER::accept( @_ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->SUPER::accept(@_) or return 0 6 6
967  
968 6 ${*$new}{$_} = ${*$self}{$_} for qw( io_socket_domain io_socket_type io_socket_proto );
969  
970 6 ▶ return wantarray ? ( $new, $peer )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
971 : $new;
972 }
973  
974 # This second unbelievably dodgy hack guarantees that $self->fileno doesn't
975 # change, which is useful during nonblocking connect
976 sub socket :method
977 {
978 37 ▶ my $self = shift;
Subroutine socket: called CC=4 SCAR=14.2
Pod documented
979 37 ▶ return $self->SUPER::socket(@_) if not defined $self->fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->fileno 35 2 37
980  
981 # I hate core prototypes sometimes...
982 2 ▶ socket( my $tmph, $_[0], $_[1], $_[2] ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
CORE::socket my $tmph, $_[0], $_[1], $_[2] or return undef 0 2 2
983  
984 2 ▶ dup2( $tmph->fileno, $self->fileno ) or die "Unable to dup2 $tmph onto $self - $!";
Branch: 1 branch
BranchTrueFalseTotal
dup2($tmph->fileno, $self->fileno) or die "Unable to dup2 $tmph onto $self - $!" 0 2 2
985 }
986  
987 # Versions of IO::Socket before 1.35 may leave socktype undef if from, say, an
988 # ->fdopen call. In this case we'll apply a fix
989 BEGIN {
990 22 ▶ if( eval($IO::Socket::VERSION) < 1.35 ) {
Subroutine BEGIN: called CC=2 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
if (eval $IO::Socket::VERSION < "1.35") 0 22 22
991 *socktype = sub {
992 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
993 0 my $type = $self->SUPER::socktype;
994 0 ▶ if( !defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 0 0
995 0 $type = $self->sockopt( Socket::SO_TYPE() );
996 }
997 0 return $type;
998 0 };
999 }
1000 }
1001  
1002 =head2 as_inet
1003  
1004 $inet = $sock->as_inet
1005  
1006 Returns a new L<IO::Socket::INET> instance wrapping the same filehandle. This
1007 may be useful in cases where it is required, for backward-compatibility, to
1008 have a real object of C<IO::Socket::INET> type instead of C<IO::Socket::IP>.
1009 The new object will wrap the same underlying socket filehandle as the
1010 original, so care should be taken not to continue to use both objects
1011 concurrently. Ideally the original C<$sock> should be discarded after this
1012 method is called.
1013  
1014 This method checks that the socket domain is C<PF_INET> and will throw an
1015 exception if it isn't.
1016  
1017 =cut
1018  
1019 sub as_inet
1020 {
1021 1 ▶ my $self = shift;
Subroutine as_inet: called CC=2 SCAR=7.1
Pod documented
1022 1 ▶ croak "Cannot downgrade a non-PF_INET socket to IO::Socket::INET" unless $self->sockdomain == AF_INET;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sockdomain == 2 0 1 1
1023 1 return IO::Socket::INET->new_from_fd( $self->fileno, "r+" );
1024 }
1025  
1026 =head1 NON-BLOCKING
1027  
1028 If the constructor is passed a defined but false value for the C<Blocking>
1029 argument then the socket is put into non-blocking mode. When in non-blocking
1030 mode, the socket will not be set up by the time the constructor returns,
1031 because the underlying C<connect(2)> syscall would otherwise have to block.
1032  
1033 The non-blocking behaviour is an extension of the C<IO::Socket::INET> API,
1034 unique to C<IO::Socket::IP>, because the former does not support multi-homed
1035 non-blocking connect.
1036  
1037 When using non-blocking mode, the caller must repeatedly check for
1038 writeability on the filehandle (for instance using C<select> or C<IO::Poll>).
1039 Each time the filehandle is ready to write, the C<connect> method must be
1040 called, with no arguments. Note that some operating systems, most notably
1041 C<MSWin32> do not report a C<connect()> failure using write-ready; so you must
1042 also C<select()> for exceptional status.
1043  
1044 While C<connect> returns false, the value of C<$!> indicates whether it should
1045 be tried again (by being set to the value C<EINPROGRESS>, or C<EWOULDBLOCK> on
1046 MSWin32), or whether a permanent error has occurred (e.g. C<ECONNREFUSED>).
1047  
1048 Once the socket has been connected to the peer, C<connect> will return true
1049 and the socket will now be ready to use.
1050  
1051 Note that calls to the platform's underlying C<getaddrinfo(3)> function may
1052 block. If C<IO::Socket::IP> has to perform this lookup, the constructor will
1053 block even when in non-blocking mode.
1054  
1055 To avoid this blocking behaviour, the caller should pass in the result of such
1056 a lookup using the C<PeerAddrInfo> or C<LocalAddrInfo> arguments. This can be
1057 achieved by using L<Net::LibAsyncNS>, or the C<getaddrinfo(3)> function can be
1058 called in a child process.
1059  
1060 use IO::Socket::IP;
1061 use Errno qw( EINPROGRESS EWOULDBLOCK );
1062  
1063 my @peeraddrinfo = ... # Caller must obtain the getaddinfo result here
1064  
1065 my $socket = IO::Socket::IP->new(
1066 PeerAddrInfo => \@peeraddrinfo,
1067 Blocking => 0,
1068 ) or die "Cannot construct socket - $@";
1069  
1070 while( !$socket->connect and ( $! == EINPROGRESS || $! == EWOULDBLOCK ) ) {
1071 my $wvec = '';
1072 vec( $wvec, fileno $socket, 1 ) = 1;
1073 my $evec = '';
1074 vec( $evec, fileno $socket, 1 ) = 1;
1075  
1076 select( undef, $wvec, $evec, undef ) or die "Cannot select - $!";
1077 }
1078  
1079 die "Cannot connect - $!" if $!;
1080  
1081 ...
1082  
1083 The example above uses C<select()>, but any similar mechanism should work
1084 analogously. C<IO::Socket::IP> takes care when creating new socket filehandles
1085 to preserve the actual file descriptor number, so such techniques as C<poll>
1086 or C<epoll> should be transparent to its reallocation of a different socket
1087 underneath, perhaps in order to switch protocol family between C<PF_INET> and
1088 C<PF_INET6>.
1089  
1090 For another example using C<IO::Poll> and C<Net::LibAsyncNS>, see the
1091 F<examples/nonblocking_libasyncns.pl> file in the module distribution.
1092  
1093 =cut
1094  
1095 =head1 C<PeerHost> AND C<LocalHost> PARSING
1096  
1097 To support the C<IO::Socket::INET> API, the host and port information may be
1098 passed in a single string rather than as two separate arguments.
1099  
1100 If either C<LocalHost> or C<PeerHost> (or their C<...Addr> synonyms) have any
1101 of the following special forms then special parsing is applied.
1102  
1103 The value of the C<...Host> argument will be split to give both the hostname
1104 and port (or service name):
1105  
1106 hostname.example.org:http # Host name
1107 192.0.2.1:80 # IPv4 address
1108 [2001:db8::1]:80 # IPv6 address
1109  
1110 In each case, the port or service name (e.g. C<80>) is passed as the
1111 C<LocalService> or C<PeerService> argument.
1112  
1113 Either of C<LocalService> or C<PeerService> (or their C<...Port> synonyms) can
1114 be either a service name, a decimal number, or a string containing both a
1115 service name and number, in a form such as
1116  
1117 http(80)
1118  
1119 In this case, the name (C<http>) will be tried first, but if the resolver does
1120 not understand it then the port number (C<80>) will be used instead.
1121  
1122 If the C<...Host> argument is in this special form and the corresponding
1123 C<...Service> or C<...Port> argument is also defined, the one parsed from
1124 the C<...Host> argument will take precedence and the other will be ignored.
1125  
1126 =head2 split_addr
1127  
1128 ( $host, $port ) = IO::Socket::IP->split_addr( $addr )
1129  
1130 Utility method that provides the parsing functionality described above.
1131 Returns a 2-element list, containing either the split hostname and port
1132 description if it could be parsed, or the given address and C<undef> if it was
1133 not recognised.
1134  
1135 IO::Socket::IP->split_addr( "hostname:http" )
1136 # ( "hostname", "http" )
1137  
1138 IO::Socket::IP->split_addr( "192.0.2.1:80" )
1139 # ( "192.0.2.1", "80" )
1140  
1141 IO::Socket::IP->split_addr( "[2001:db8::1]:80" )
1142 # ( "2001:db8::1", "80" )
1143  
1144 IO::Socket::IP->split_addr( "something.else" )
1145 # ( "something.else", undef )
1146  
1147 =cut
1148  
1149 sub split_addr
1150 {
1151 51 ▶ shift;
Subroutine split_addr: called CC=5 SCAR=16.1
Pod documented
1152 51 my ( $addr ) = @_;
1153  
1154 51 local ( $1, $2 ); # Placate a taint-related bug; [perl #67962]
1155 51 ▶ if( $addr =~ m/\A\[($IPv6_re)\](?::([^\s:]*))?\z/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($addr =~ /\A\[($IPv6_re)\](?::([^\s:]*))?\z/u or $addr =~ /\A([^\s:]*):([^\s:]*)\z/u) 17 34 51
Condition100%3/3
$addr =~ /\A\[($IPv6_re)\](?::([^\s:]*))?\z/u or $addr =~ /\A([^\s:]*):([^\s:]*)\z/u
l!l&&r!l&&!r
8934
MC/DC100%2/2
$addr =~ /\A\[($IPv6_re)\](?::([^\s:]*))?\z/u or $addr =~ /\A([^\s:]*):([^\s:]*)\z/u
$addr =~ /\A\[($IPv6_re)\](?::([^\s:]*))?\z/u $addr =~ /\A([^\s:]*):([^\s:]*)\z/u
Truth table100%3/3
A or B
A= $addr =~ /\A\[($IPv6_re)\](?::([^\s:]*))?\z/uB= $addr =~ /\A([^\s:]*):([^\s:]*)\z/u
ABresult
1X1
011
000
1156 $addr =~ m/\A([^\s:]*):([^\s:]*)\z/ ) {
1157 17 ▶ return ( $1, $2 ) if defined $2 and length $2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $2 and length $2 13 4 17
Condition100%3/3
defined $2 and length $2
!ll&&!rl&&r
2213
MC/DC100%2/2
defined $2 and length $2
defined $2 length $2
Truth table100%3/3
A and B
A= defined $2B= length $2
ABresult
0X0
100
111
1158 4 return ( $1, undef );
1159 }
1160  
1161 34 return ( $addr, undef );
1162 }
1163  
1164 =head2 join_addr
1165  
1166 $addr = IO::Socket::IP->join_addr( $host, $port )
1167  
1168 Utility method that performs the reverse of C<split_addr>, returning a string
1169 formed by joining the specified host address and port number. The host address
1170 will be wrapped in C<[]> brackets if required (because it is a raw IPv6
1171 numeric address).
1172  
1173 This can be especially useful when combined with the C<sockhost_service> or
1174 C<peerhost_service> methods.
1175  
1176 say "Connected to ", IO::Socket::IP->join_addr( $sock->peerhost_service );
1177  
1178 =cut
1179  
1180 sub join_addr
1181 {
1182 4 ▶ shift;
Subroutine join_addr: called CC=3 SCAR=11.0
Pod documented
1183 4 my ( $host, $port ) = @_;
1184  
1185 4 ▶ $host = "[$host]" if $host =~ m/:/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/u 1 3 4
1186  
1187 4 ▶ return join ":", $host, $port if defined $port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $port 3 1 4
1188 1 return $host;
1189 }
1190  
1191 # Since IO::Socket->new( Domain => ... ) will delete the Domain parameter
1192 # before calling ->configure, we need to keep track of which it was
1193  
1194 package # hide from indexer
1195 IO::Socket::IP::_ForINET;
1196 22 ▶use base qw( IO::Socket::IP );
Subroutine BEGIN: called CC=1 SCAR=0.0
1197  
1198 sub configure
1199 {
1200 # This is evil
1201 2 ▶ my $self = shift;
Subroutine configure: called CC=1 SCAR=0.0
1202 2 my ( $arg ) = @_;
1203  
1204 2 bless $self, "IO::Socket::IP";
1205 2 $self->configure( { %$arg, Family => Socket::AF_INET() } );
1206 }
1207  
1208 package # hide from indexer
1209 IO::Socket::IP::_ForINET6;
1210 22 ▶use base qw( IO::Socket::IP );
Subroutine BEGIN: called CC=1 SCAR=0.0
1211  
1212 sub configure
1213 {
1214 # This is evil
1215 0 ▶ my $self = shift;
Subroutine configure: not called CC=1 SCAR=6.9
1216 0 my ( $arg ) = @_;
1217  
1218 0 bless $self, "IO::Socket::IP";
1219 0 $self->configure( { %$arg, Family => Socket::AF_INET6() } );
1220 }
1221  
1222 =head1 C<IO::Socket::INET> INCOMPATIBILITES
1223  
1224 =over 4
1225  
1226 =item *
1227  
1228 The behaviour enabled by C<MultiHomed> is in fact implemented by
1229 C<IO::Socket::IP> as it is required to correctly support searching for a
1230 useable address from the results of the C<getaddrinfo(3)> call. The
1231 constructor will ignore the value of this argument, except if it is defined
1232 but false. An exception is thrown in this case, because that would request it
1233 disable the C<getaddrinfo(3)> search behaviour in the first place.
1234  
1235 =item *
1236  
1237 C<IO::Socket::IP> implements both the C<Blocking> and C<Timeout> parameters,
1238 but it implements the interaction of both in a different way.
1239  
1240 In C<::INET>, supplying a timeout overrides the non-blocking behaviour,
1241 meaning that the C<connect()> operation will still block despite that the
1242 caller asked for a non-blocking socket. This is not explicitly specified in
1243 its documentation, nor does this author believe that is a useful behaviour -
1244 it appears to come from a quirk of implementation.
1245  
1246 In C<::IP> therefore, the C<Blocking> parameter takes precedence - if a
1247 non-blocking socket is requested, no operation will block. The C<Timeout>
1248 parameter here simply defines the maximum time that a blocking C<connect()>
1249 call will wait, if it blocks at all.
1250  
1251 In order to specifically obtain the "blocking connect then non-blocking send
1252 and receive" behaviour of specifying this combination of options to C<::INET>
1253 when using C<::IP>, perform first a blocking connect, then afterwards turn the
1254 socket into nonblocking mode.
1255  
1256 my $sock = IO::Socket::IP->new(
1257 PeerHost => $peer,
1258 Timeout => 20,
1259 ) or die "Cannot connect - $@";
1260  
1261 $sock->blocking( 0 );
1262  
1263 This code will behave identically under both C<IO::Socket::INET> and
1264 C<IO::Socket::IP>.
1265  
1266 =back
1267  
1268 =cut
1269  
1270 =head1 TODO
1271  
1272 =over 4
1273  
1274 =item *
1275  
1276 Investigate whether C<POSIX::dup2> upsets BSD's C<kqueue> watchers, and if so,
1277 consider what possible workarounds might be applied.
1278  
1279 =back
1280  
1281 =head1 AUTHOR
1282  
1283 Paul Evans <leonerd@leonerd.org.uk>
1284  
1285 =cut
1286  
1287 22 0x55AA;
↑ Summary Build »