← Index
NYTProf Performance Profile   « line view »
For /home/ss5/perl5/perlbrew/perls/perl-5.22.0/bin/benchmarkanything-storage
  Run on Mon Jan 29 16:55:34 2018
Reported on Mon Jan 29 16:57:07 2018

Filename/home/ss5/perl5/perlbrew/perls/perl-5.22.0/lib/5.22.0/IO/Socket/IP.pm
StatementsExecuted 222264 statements in 621ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
200211134ms611msIO::Socket::IP::::_io_socket_ip__configure IO::Socket::IP::_io_socket_ip__configure
200211109ms109msIO::Socket::IP::::CORE:connect IO::Socket::IP::CORE:connect (opcode)
20021160.3ms225msIO::Socket::IP::::connect IO::Socket::IP::connect
20032149.0ms49.0msIO::Socket::IP::::CORE:gpbyname IO::Socket::IP::CORE:gpbyname (opcode)
20021139.2ms316msIO::Socket::IP::::setup IO::Socket::IP::setup
20021132.9ms674msIO::Socket::IP::::configure IO::Socket::IP::configure
20021119.1ms856msIO::Socket::IP::::new IO::Socket::IP::new
20021118.5ms29.6msIO::Socket::IP::::split_addr IO::Socket::IP::split_addr
20021114.8ms52.1msIO::Socket::IP::::socket IO::Socket::IP::socket
2003219.41ms9.41msIO::Socket::IP::::CORE:regcomp IO::Socket::IP::CORE:regcomp (opcode)
2002116.77ms6.77msIO::Socket::IP::::CORE:sselect IO::Socket::IP::CORE:sselect (opcode)
6006213.27ms3.27msIO::Socket::IP::::CORE:match IO::Socket::IP::CORE:match (opcode)
1112.07ms3.63msIO::Socket::IP::::BEGIN@33 IO::Socket::IP::BEGIN@33
2002111.05ms1.05msIO::Socket::IP::::CORE:subst IO::Socket::IP::CORE:subst (opcode)
11116µs16µsIO::Socket::IP::::BEGIN@921 IO::Socket::IP::BEGIN@921
1119µs57µsIO::Socket::IP::::BEGIN@39 IO::Socket::IP::BEGIN@39
1118µs72µsIO::Socket::IP::::BEGIN@19 IO::Socket::IP::BEGIN@19
1118µs50µsIO::Socket::IP::_ForINET::::BEGIN@1122 IO::Socket::IP::_ForINET::BEGIN@1122
1116µs60µsIO::Socket::IP::::BEGIN@15 IO::Socket::IP::BEGIN@15
1116µs45µsIO::Socket::IP::::BEGIN@36 IO::Socket::IP::BEGIN@36
1116µs6µsIO::Socket::IP::::BEGIN@9 IO::Socket::IP::BEGIN@9
1116µs32µsIO::Socket::IP::::BEGIN@34 IO::Socket::IP::BEGIN@34
1115µs35µsIO::Socket::IP::_ForINET6::::BEGIN@1136IO::Socket::IP::_ForINET6::BEGIN@1136
1115µs7µsIO::Socket::IP::::BEGIN@13 IO::Socket::IP::BEGIN@13
1115µs29µsIO::Socket::IP::::BEGIN@17 IO::Socket::IP::BEGIN@17
1114µs10µsIO::Socket::IP::::BEGIN@14 IO::Socket::IP::BEGIN@14
111900ns900nsIO::Socket::IP::::CORE:qr IO::Socket::IP::CORE:qr (opcode)
0000s0sIO::Socket::IP::::CAN_DISABLE_V6ONLY IO::Socket::IP::CAN_DISABLE_V6ONLY
0000s0sIO::Socket::IP::_ForINET6::::configureIO::Socket::IP::_ForINET6::configure
0000s0sIO::Socket::IP::_ForINET::::configure IO::Socket::IP::_ForINET::configure
0000s0sIO::Socket::IP::::__ANON__[:930] IO::Socket::IP::__ANON__[:930]
0000s0sIO::Socket::IP::::_get_host_service IO::Socket::IP::_get_host_service
0000s0sIO::Socket::IP::::_unpack_sockaddr IO::Socket::IP::_unpack_sockaddr
0000s0sIO::Socket::IP::::accept IO::Socket::IP::accept
0000s0sIO::Socket::IP::::as_inet IO::Socket::IP::as_inet
0000s0sIO::Socket::IP::::connected IO::Socket::IP::connected
0000s0sIO::Socket::IP::::import IO::Socket::IP::import
0000s0sIO::Socket::IP::::join_addr IO::Socket::IP::join_addr
0000s0sIO::Socket::IP::::peeraddr IO::Socket::IP::peeraddr
0000s0sIO::Socket::IP::::peerhost IO::Socket::IP::peerhost
0000s0sIO::Socket::IP::::peerhost_service IO::Socket::IP::peerhost_service
0000s0sIO::Socket::IP::::peerhostname IO::Socket::IP::peerhostname
0000s0sIO::Socket::IP::::peerport IO::Socket::IP::peerport
0000s0sIO::Socket::IP::::peerservice IO::Socket::IP::peerservice
0000s0sIO::Socket::IP::::sockaddr IO::Socket::IP::sockaddr
0000s0sIO::Socket::IP::::sockhost IO::Socket::IP::sockhost
0000s0sIO::Socket::IP::::sockhost_service IO::Socket::IP::sockhost_service
0000s0sIO::Socket::IP::::sockhostname IO::Socket::IP::sockhostname
0000s0sIO::Socket::IP::::sockport IO::Socket::IP::sockport
0000s0sIO::Socket::IP::::sockservice IO::Socket::IP::sockservice
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
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-2015 -- leonerd@leonerd.org.uk
5
6package IO::Socket::IP;
7# $VERSION needs to be set before use base 'IO::Socket'
8# - https://rt.cpan.org/Ticket/Display.html?id=92107
9
# spent 6µs within IO::Socket::IP::BEGIN@9 which was called: # once (6µs+0s) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 11
BEGIN {
1013µs $VERSION = '0.37';
11114µs16µs}
# spent 6µs making 1 call to IO::Socket::IP::BEGIN@9
12
13214µs29µs
# spent 7µs (5+2) within IO::Socket::IP::BEGIN@13 which was called: # once (5µs+2µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 13
use strict;
# spent 7µs making 1 call to IO::Socket::IP::BEGIN@13 # spent 2µs making 1 call to strict::import
14216µs215µs
# spent 10µs (4+5) within IO::Socket::IP::BEGIN@14 which was called: # once (4µs+5µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 14
use warnings;
# spent 10µs making 1 call to IO::Socket::IP::BEGIN@14 # spent 6µs making 1 call to warnings::import
15218µs2114µs
# spent 60µs (6+54) within IO::Socket::IP::BEGIN@15 which was called: # once (6µs+54µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 15
use base qw( IO::Socket );
# spent 60µs making 1 call to IO::Socket::IP::BEGIN@15 # spent 54µs making 1 call to base::import
16
17228µs254µs
# spent 29µs (5+24) within IO::Socket::IP::BEGIN@17 which was called: # once (5µs+24µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 17
use Carp;
# spent 29µs making 1 call to IO::Socket::IP::BEGIN@17 # spent 24µs making 1 call to Exporter::import
18
1913µs155µs
# spent 72µs (8+64) within IO::Socket::IP::BEGIN@19 which was called: # once (8µs+64µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 30
use Socket 1.97 qw(
# spent 55µs making 1 call to Exporter::import
20 getaddrinfo getnameinfo
21 sockaddr_family
22 AF_INET
23 AI_PASSIVE
24 IPPROTO_TCP IPPROTO_UDP
25 IPPROTO_IPV6 IPV6_V6ONLY
26 NI_DGRAM NI_NUMERICHOST NI_NUMERICSERV NIx_NOHOST NIx_NOSERV
27 SO_REUSEADDR SO_REUSEPORT SO_BROADCAST SO_ERROR
28 SOCK_DGRAM SOCK_STREAM
29 SOL_SOCKET
30244µs281µs);
# spent 72µs making 1 call to IO::Socket::IP::BEGIN@19 # spent 9µs making 1 call to UNIVERSAL::VERSION
312900nsmy $AF_INET6 = eval { Socket::AF_INET6() }; # may not be defined
322400nsmy $AI_ADDRCONFIG = eval { Socket::AI_ADDRCONFIG() } || 0;
33268µs24.50ms
# spent 3.63ms (2.07+1.56) within IO::Socket::IP::BEGIN@33 which was called: # once (2.07ms+1.56ms) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 33
use POSIX qw( dup2 );
# spent 3.63ms making 1 call to IO::Socket::IP::BEGIN@33 # spent 869µs making 1 call to POSIX::import
34222µs258µs
# spent 32µs (6+26) within IO::Socket::IP::BEGIN@34 which was called: # once (6µs+26µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 34
use Errno qw( EINVAL EINPROGRESS EISCONN ENOTCONN ETIMEDOUT EWOULDBLOCK );
# spent 32µs making 1 call to IO::Socket::IP::BEGIN@34 # spent 26µs making 1 call to Exporter::import
35
36228µs284µs
# spent 45µs (6+39) within IO::Socket::IP::BEGIN@36 which was called: # once (6µs+39µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 36
use constant HAVE_MSWIN32 => ( $^O eq "MSWin32" );
# spent 45µs making 1 call to IO::Socket::IP::BEGIN@36 # spent 39µs making 1 call to constant::import
37
38# At least one OS (Android) is known not to have getprotobyname()
3931.69ms3104µs
# spent 57µs (9+47) within IO::Socket::IP::BEGIN@39 which was called: # once (9µs+47µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 39
use constant HAVE_GETPROTOBYNAME => defined eval { getprotobyname( "tcp" ) };
# spent 57µs making 1 call to IO::Socket::IP::BEGIN@39 # spent 26µs making 1 call to IO::Socket::IP::CORE:gpbyname # spent 22µs making 1 call to constant::import
40
4116µsmy $IPv6_re = do {
42 # translation of RFC 3986 3.2.2 ABNF to re
431100ns my $IPv4address = do {
441100ns my $dec_octet = q<(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])>;
451900ns qq<$dec_octet(?: \\. $dec_octet){3}>;
46 };
471100ns my $IPv6address = do {
481100ns my $h16 = qq<[0-9A-Fa-f]{1,4}>;
491900ns my $ls32 = qq<(?: $h16 : $h16 | $IPv4address)>;
5014µs qq<(?:
51 (?: $h16 : ){6} $ls32
52 | :: (?: $h16 : ){5} $ls32
53 | (?: $h16 )? :: (?: $h16 : ){4} $ls32
54 | (?: (?: $h16 : ){0,1} $h16 )? :: (?: $h16 : ){3} $ls32
55 | (?: (?: $h16 : ){0,2} $h16 )? :: (?: $h16 : ){2} $ls32
56 | (?: (?: $h16 : ){0,3} $h16 )? :: $h16 : $ls32
57 | (?: (?: $h16 : ){0,4} $h16 )? :: $ls32
58 | (?: (?: $h16 : ){0,5} $h16 )? :: $h16
59 | (?: (?: $h16 : ){0,6} $h16 )? ::
60 )>
61 };
621169µs2158µs qr<$IPv6address>xo;
# spent 157µs making 1 call to IO::Socket::IP::CORE:regcomp # spent 900ns making 1 call to IO::Socket::IP::CORE:qr
63};
64
65=head1 NAME
66
67C<IO::Socket::IP> - Family-neutral IP socket supporting both IPv4 and IPv6
68
69=head1 SYNOPSIS
70
71 use IO::Socket::IP;
72
73 my $sock = IO::Socket::IP->new(
74 PeerHost => "www.google.com",
75 PeerPort => "http",
76 Type => SOCK_STREAM,
77 ) or die "Cannot construct socket - $@";
78
79 my $familyname = ( $sock->sockdomain == PF_INET6 ) ? "IPv6" :
80 ( $sock->sockdomain == PF_INET ) ? "IPv4" :
81 "unknown";
82
83 printf "Connected to google via %s\n", $familyname;
84
85=head1 DESCRIPTION
86
87This module provides a protocol-independent way to use IPv4 and IPv6 sockets,
88intended as a replacement for L<IO::Socket::INET>. Most constructor arguments
89and methods are provided in a backward-compatible way. For a list of known
90differences, see the C<IO::Socket::INET> INCOMPATIBILITES section below.
91
92It uses the C<getaddrinfo(3)> function to convert hostnames and service names
93or port numbers into sets of possible addresses to connect to or listen on.
94This allows it to work for IPv6 where the system supports it, while still
95falling back to IPv4-only on systems which don't.
96
97=head1 REPLACING C<IO::Socket> DEFAULT BEHAVIOUR
98
99By placing C<-register> in the import list, L<IO::Socket> uses
100C<IO::Socket::IP> rather than C<IO::Socket::INET> as the class that handles
101C<PF_INET>. C<IO::Socket> will also use C<IO::Socket::IP> rather than
102C<IO::Socket::INET6> to handle C<PF_INET6>, provided that the C<AF_INET6>
103constant is available.
104
105Changing C<IO::Socket>'s default behaviour means that calling the
106C<IO::Socket> constructor with either C<PF_INET> or C<PF_INET6> as the
107C<Domain> parameter will yield an C<IO::Socket::IP> object.
108
109 use IO::Socket::IP -register;
110
111 my $sock = IO::Socket->new(
112 Domain => PF_INET6,
113 LocalHost => "::1",
114 Listen => 1,
115 ) or die "Cannot create socket - $@\n";
116
117 print "Created a socket of type " . ref($sock) . "\n";
118
119Note that C<-register> is a global setting that applies to the entire program;
120it cannot be applied only for certain callers, removed, or limited by lexical
121scope.
122
123=cut
124
125sub import
126{
127 my $pkg = shift;
128 my @symbols;
129
130 foreach ( @_ ) {
131 if( $_ eq "-register" ) {
132 IO::Socket::IP::_ForINET->register_domain( AF_INET );
133 IO::Socket::IP::_ForINET6->register_domain( $AF_INET6 ) if defined $AF_INET6;
134 }
135 else {
136 push @symbols, $_;
137 }
138 }
139
140 @_ = ( $pkg, @symbols );
141 goto &IO::Socket::import;
142}
143
144# Convenient capability test function
145{
1461200ns my $can_disable_v6only;
147 sub CAN_DISABLE_V6ONLY
148 {
149 return $can_disable_v6only if defined $can_disable_v6only;
150
151 socket my $testsock, Socket::PF_INET6(), SOCK_STREAM, 0 or
152 die "Cannot socket(PF_INET6) - $!";
153
154 if( setsockopt $testsock, IPPROTO_IPV6, IPV6_V6ONLY, 0 ) {
155 return $can_disable_v6only = 1;
156 }
157 elsif( $! == EINVAL ) {
158 return $can_disable_v6only = 0;
159 }
160 else {
161 die "Cannot setsockopt() - $!";
162 }
163 }
164}
165
166=head1 CONSTRUCTORS
167
168=cut
169
170=head2 $sock = IO::Socket::IP->new( %args )
171
172Creates a new C<IO::Socket::IP> object, containing a newly created socket
173handle according to the named arguments passed. The recognised arguments are:
174
175=over 8
176
177=item PeerHost => STRING
178
179=item PeerService => STRING
180
181Hostname and service name for the peer to C<connect()> to. The service name
182may be given as a port number, as a decimal string.
183
184=item PeerAddr => STRING
185
186=item PeerPort => STRING
187
188For symmetry with the accessor methods and compatibility with
189C<IO::Socket::INET>, these are accepted as synonyms for C<PeerHost> and
190C<PeerService> respectively.
191
192=item PeerAddrInfo => ARRAY
193
194Alternate form of specifying the peer to C<connect()> to. This should be an
195array of the form returned by C<Socket::getaddrinfo>.
196
197This parameter takes precedence over the C<Peer*>, C<Family>, C<Type> and
198C<Proto> arguments.
199
200=item LocalHost => STRING
201
202=item LocalService => STRING
203
204Hostname and service name for the local address to C<bind()> to.
205
206=item LocalAddr => STRING
207
208=item LocalPort => STRING
209
210For symmetry with the accessor methods and compatibility with
211C<IO::Socket::INET>, these are accepted as synonyms for C<LocalHost> and
212C<LocalService> respectively.
213
214=item LocalAddrInfo => ARRAY
215
216Alternate form of specifying the local address to C<bind()> to. This should be
217an array of the form returned by C<Socket::getaddrinfo>.
218
219This parameter takes precedence over the C<Local*>, C<Family>, C<Type> and
220C<Proto> arguments.
221
222=item Family => INT
223
224The address family to pass to C<getaddrinfo> (e.g. C<AF_INET>, C<AF_INET6>).
225Normally this will be left undefined, and C<getaddrinfo> will search using any
226address family supported by the system.
227
228=item Type => INT
229
230The socket type to pass to C<getaddrinfo> (e.g. C<SOCK_STREAM>,
231C<SOCK_DGRAM>). Normally defined by the caller; if left undefined
232C<getaddrinfo> may attempt to infer the type from the service name.
233
234=item Proto => STRING or INT
235
236The IP protocol to use for the socket (e.g. C<'tcp'>, C<IPPROTO_TCP>,
237C<'udp'>,C<IPPROTO_UDP>). Normally this will be left undefined, and either
238C<getaddrinfo> or the kernel will choose an appropriate value. May be given
239either in string name or numeric form.
240
241=item GetAddrInfoFlags => INT
242
243More flags to pass to the C<getaddrinfo()> function. If not supplied, a
244default of C<AI_ADDRCONFIG> will be used.
245
246These flags will be combined with C<AI_PASSIVE> if the C<Listen> argument is
247given. For more information see the documentation about C<getaddrinfo()> in
248the L<Socket> module.
249
250=item Listen => INT
251
252If defined, puts the socket into listening mode where new connections can be
253accepted using the C<accept> method. The value given is used as the
254C<listen(2)> queue size.
255
256=item ReuseAddr => BOOL
257
258If true, set the C<SO_REUSEADDR> sockopt
259
260=item ReusePort => BOOL
261
262If true, set the C<SO_REUSEPORT> sockopt (not all OSes implement this sockopt)
263
264=item Broadcast => BOOL
265
266If true, set the C<SO_BROADCAST> sockopt
267
268=item V6Only => BOOL
269
270If defined, set the C<IPV6_V6ONLY> sockopt when creating C<PF_INET6> sockets
271to the given value. If true, a listening-mode socket will only listen on the
272C<AF_INET6> addresses; if false it will also accept connections from
273C<AF_INET> addresses.
274
275If not defined, the socket option will not be changed, and default value set
276by the operating system will apply. For repeatable behaviour across platforms
277it is recommended this value always be defined for listening-mode sockets.
278
279Note that not all platforms support disabling this option. Some, at least
280OpenBSD and MirBSD, will fail with C<EINVAL> if you attempt to disable it.
281To determine whether it is possible to disable, you may use the class method
282
283 if( IO::Socket::IP->CAN_DISABLE_V6ONLY ) {
284 ...
285 }
286 else {
287 ...
288 }
289
290If your platform does not support disabling this option but you still want to
291listen for both C<AF_INET> and C<AF_INET6> connections you will have to create
292two listening sockets, one bound to each protocol.
293
294=item MultiHomed
295
296This C<IO::Socket::INET>-style argument is ignored, except if it is defined
297but false. See the C<IO::Socket::INET> INCOMPATIBILITES section below.
298
299However, the behaviour it enables is always performed by C<IO::Socket::IP>.
300
301=item Blocking => BOOL
302
303If defined but false, the socket will be set to non-blocking mode. Otherwise
304it will default to blocking mode. See the NON-BLOCKING section below for more
305detail.
306
307=item Timeout => NUM
308
309If defined, gives a maximum time in seconds to block per C<connect()> call
310when in blocking mode. If missing, no timeout is applied other than that
311provided by the underlying operating system. When in non-blocking mode this
312parameter is ignored.
313
314Note that if the hostname resolves to multiple address candidates, the same
315timeout will apply to each connection attempt individually, rather than to the
316operation as a whole. Further note that the timeout does not apply to the
317initial hostname resolve operation, if connecting by hostname.
318
319This behviour is copied inspired by C<IO::Socket::INET>; for more fine grained
320control over connection timeouts, consider performing a nonblocking connect
321directly.
322
323=back
324
325If neither C<Type> nor C<Proto> hints are provided, a default of
326C<SOCK_STREAM> and C<IPPROTO_TCP> respectively will be set, to maintain
327compatibility with C<IO::Socket::INET>. Other named arguments that are not
328recognised are ignored.
329
330If neither C<Family> nor any hosts or addresses are passed, nor any
331C<*AddrInfo>, then the constructor has no information on which to decide a
332socket family to create. In this case, it performs a C<getaddinfo> call with
333the C<AI_ADDRCONFIG> flag, no host name, and a service name of C<"0">, and
334uses the family of the first returned result.
335
336If the constructor fails, it will set C<$@> to an appropriate error message;
337this may be from C<$!> or it may be some other string; not every failure
338necessarily has an associated C<errno> value.
339
340=head2 $sock = IO::Socket::IP->new( $peeraddr )
341
342As a special case, if the constructor is passed a single argument (as
343opposed to an even-sized list of key/value pairs), it is taken to be the value
344of the C<PeerAddr> parameter. This is parsed in the same way, according to the
345behaviour given in the C<PeerHost> AND C<LocalHost> PARSING section below.
346
347=cut
348
349sub new
3501300ns
# spent 856ms (19.1+836) within IO::Socket::IP::new which was called 2002 times, avg 427µs/call: # 2002 times (19.1ms+836ms) by HTTP::Tiny::Handle::connect at line 921 of HTTP/Tiny.pm, avg 427µs/call
{
3512002826µs my $class = shift;
35220026.83ms my %arg = (@_ == 1) ? (PeerHost => $_[0]) : @_;
353200218.8ms2002836ms return $class->SUPER::new(%arg);
# spent 836ms making 2002 calls to IO::Socket::new, avg 418µs/call
354}
355
356# IO::Socket may call this one; neaten up the arguments from IO::Socket::INET
357# before calling our real _configure method
358sub configure
359
# spent 674ms (32.9+641) within IO::Socket::IP::configure which was called 2002 times, avg 336µs/call: # 2002 times (32.9ms+641ms) by IO::Socket::new at line 49 of IO/Socket.pm, avg 336µs/call
{
3602002702µs my $self = shift;
3612002766µs my ( $arg ) = @_;
362
363 $arg->{PeerHost} = delete $arg->{PeerAddr}
36420021.03ms if exists $arg->{PeerAddr} && !exists $arg->{PeerHost};
365
366 $arg->{PeerService} = delete $arg->{PeerPort}
36720023.12ms if exists $arg->{PeerPort} && !exists $arg->{PeerService};
368
369 $arg->{LocalHost} = delete $arg->{LocalAddr}
3702002641µs if exists $arg->{LocalAddr} && !exists $arg->{LocalHost};
371
372 $arg->{LocalService} = delete $arg->{LocalPort}
3732002595µs if exists $arg->{LocalPort} && !exists $arg->{LocalService};
374
37520021.75ms for my $type (qw(Peer Local)) {
37640041.72ms my $host = $type . 'Host';
37740041.53ms my $service = $type . 'Service';
378
37940043.06ms if( defined $arg->{$host} ) {
38020025.57ms200229.6ms ( $arg->{$host}, my $s ) = $self->split_addr( $arg->{$host} );
# spent 29.6ms making 2002 calls to IO::Socket::IP::split_addr, avg 15µs/call
381 # IO::Socket::INET compat - *Host parsed port always takes precedence
38220021.12ms $arg->{$service} = $s if defined $s;
383 }
384 }
385
386200219.4ms2002611ms $self->_io_socket_ip__configure( $arg );
# spent 611ms making 2002 calls to IO::Socket::IP::_io_socket_ip__configure, avg 305µs/call
387}
388
389# Avoid simply calling it _configure, as some subclasses of IO::Socket::INET on CPAN already take that
390sub _io_socket_ip__configure
391
# spent 611ms (134+477) within IO::Socket::IP::_io_socket_ip__configure which was called 2002 times, avg 305µs/call: # 2002 times (134ms+477ms) by IO::Socket::IP::configure at line 386, avg 305µs/call
{
3922002579µs my $self = shift;
3932002579µs my ( $arg ) = @_;
394
3952002517µs my %hints;
396 my @localinfos;
397 my @peerinfos;
398
39920021.00ms my $listenqueue = $arg->{Listen};
4002002701µs if( defined $listenqueue and
401 ( defined $arg->{PeerHost} || defined $arg->{PeerService} || defined $arg->{PeerAddrInfo} ) ) {
402 croak "Cannot Listen with a peer address";
403 }
404
40520021.57ms if( defined $arg->{GetAddrInfoFlags} ) {
406 $hints{flags} = $arg->{GetAddrInfoFlags};
407 }
408 else {
40920021.54ms $hints{flags} = $AI_ADDRCONFIG;
410 }
411
41220021.12ms if( defined( my $family = $arg->{Family} ) ) {
413 $hints{family} = $family;
414 }
415
41620022.35ms if( defined( my $type = $arg->{Type} ) ) {
417 $hints{socktype} = $type;
418 }
419
42020021.48ms if( defined( my $proto = $arg->{Proto} ) ) {
421200218.1ms20021.50ms unless( $proto =~ m/^\d+$/ ) {
# spent 1.50ms making 2002 calls to IO::Socket::IP::CORE:match, avg 749ns/call
422 my $protonum = HAVE_GETPROTOBYNAME
423 ? getprotobyname( $proto )
424200261.4ms200249.0ms : eval { Socket->${\"IPPROTO_\U$proto"}() };
# spent 49.0ms making 2002 calls to IO::Socket::IP::CORE:gpbyname, avg 24µs/call
4252002773µs defined $protonum or croak "Unrecognised protocol $proto";
42620021.01ms $proto = $protonum;
427 }
428
42920021.35ms $hints{protocol} = $proto;
430 }
431
432 # To maintain compatibility with IO::Socket::INET, imply a default of
433 # SOCK_STREAM + IPPROTO_TCP if neither hint is given
43420021.12ms if( !defined $hints{socktype} and !defined $hints{protocol} ) {
435 $hints{socktype} = SOCK_STREAM;
436 $hints{protocol} = IPPROTO_TCP;
437 }
438
439 # Some OSes (NetBSD) don't seem to like just a protocol hint without a
440 # socktype hint as well. We'll set a couple of common ones
44120021.06ms if( !defined $hints{socktype} and defined $hints{protocol} ) {
442 $hints{socktype} = SOCK_STREAM if $hints{protocol} == IPPROTO_TCP;
443 $hints{socktype} = SOCK_DGRAM if $hints{protocol} == IPPROTO_UDP;
444 }
445
44620022.90ms if( my $info = $arg->{LocalAddrInfo} ) {
447 ref $info eq "ARRAY" or croak "Expected 'LocalAddrInfo' to be an ARRAY ref";
448 @localinfos = @$info;
449 }
450 elsif( defined $arg->{LocalHost} or
451 defined $arg->{LocalService} or
452 HAVE_MSWIN32 and $arg->{Listen} ) {
453 # Either may be undef
454 my $host = $arg->{LocalHost};
455 my $service = $arg->{LocalService};
456
457 unless ( defined $host or defined $service ) {
458 $service = 0;
459 }
460
461 local $1; # Placate a taint-related bug; [perl #67962]
462 defined $service and $service =~ s/\((\d+)\)$// and
463 my $fallback_port = $1;
464
465 my %localhints = %hints;
466 $localhints{flags} |= AI_PASSIVE;
467 ( my $err, @localinfos ) = getaddrinfo( $host, $service, \%localhints );
468
469 if( $err and defined $fallback_port ) {
470 ( $err, @localinfos ) = getaddrinfo( $host, $fallback_port, \%localhints );
471 }
472
473 if( $err ) {
474 $@ = "$err";
475 $! = EINVAL;
476 return;
477 }
478 }
479
48020022.11ms if( my $info = $arg->{PeerAddrInfo} ) {
481 ref $info eq "ARRAY" or croak "Expected 'PeerAddrInfo' to be an ARRAY ref";
482 @peerinfos = @$info;
483 }
484 elsif( defined $arg->{PeerHost} or defined $arg->{PeerService} ) {
48520021.21ms defined( my $host = $arg->{PeerHost} ) or
486 croak "Expected 'PeerHost'";
48720021.23ms defined( my $service = $arg->{PeerService} ) or
488 croak "Expected 'PeerService'";
489
49020021.65ms local $1; # Placate a taint-related bug; [perl #67962]
491200211.6ms20021.05ms defined $service and $service =~ s/\((\d+)\)$// and
# spent 1.05ms making 2002 calls to IO::Socket::IP::CORE:subst, avg 527ns/call
492 my $fallback_port = $1;
493
4942002121ms2002110ms ( my $err, @peerinfos ) = getaddrinfo( $host, $service, \%hints );
# spent 110ms making 2002 calls to Socket::getaddrinfo, avg 55µs/call
495
4962002550µs if( $err and defined $fallback_port ) {
497 ( $err, @peerinfos ) = getaddrinfo( $host, $fallback_port, \%hints );
498 }
499
50020022.09ms if( $err ) {
501 $@ = "$err";
502 $! = EINVAL;
503 return;
504 }
505 }
506
5072002419µs my @sockopts_enabled;
50820021.08ms push @sockopts_enabled, SO_REUSEADDR if $arg->{ReuseAddr};
5092002809µs push @sockopts_enabled, SO_REUSEPORT if $arg->{ReusePort};
5102002741µs push @sockopts_enabled, SO_BROADCAST if $arg->{Broadcast};
511
5122002821µs my $blocking = $arg->{Blocking};
5132002888µs defined $blocking or $blocking = 1;
514
5152002741µs my $v6only = $arg->{V6Only};
516
517 # IO::Socket::INET defines this key. IO::Socket::IP always implements the
518 # behaviour it requests, so we can ignore it, unless the caller is for some
519 # reason asking to disable it.
5202002681µs if( defined $arg->{MultiHomed} and !$arg->{MultiHomed} ) {
521 croak "Cannot disable the MultiHomed parameter";
522 }
523
5242002347µs my @infos;
52520023.11ms foreach my $local ( @localinfos ? @localinfos : {} ) {
52620022.14ms foreach my $peer ( @peerinfos ? @peerinfos : {} ) {
527 next if defined $local->{family} and defined $peer->{family} and
5282002866µs $local->{family} != $peer->{family};
529 next if defined $local->{socktype} and defined $peer->{socktype} and
5302002637µs $local->{socktype} != $peer->{socktype};
531 next if defined $local->{protocol} and defined $peer->{protocol} and
5322002641µs $local->{protocol} != $peer->{protocol};
533
53420021.54ms my $family = $local->{family} || $peer->{family} or next;
53520021.18ms my $socktype = $local->{socktype} || $peer->{socktype} or next;
53620021.07ms my $protocol = $local->{protocol} || $peer->{protocol} || 0;
537
538 push @infos, {
539 family => $family,
540 socktype => $socktype,
541 protocol => $protocol,
542 localaddr => $local->{addr},
543 peeraddr => $peer->{addr},
54420025.91ms };
545 }
546 }
547
5482002761µs if( !@infos ) {
549 # If there was a Family hint then create a plain unbound, unconnected socket
550 if( defined $hints{family} ) {
551 @infos = ( {
552 family => $hints{family},
553 socktype => $hints{socktype},
554 protocol => $hints{protocol},
555 } );
556 }
557 # If there wasn't, use getaddrinfo()'s AI_ADDRCONFIG side-effect to guess a
558 # suitable family first.
559 else {
560 ( my $err, @infos ) = getaddrinfo( "", "0", \%hints );
561 if( $err ) {
562 $@ = "$err";
563 $! = EINVAL;
564 return;
565 }
566
567 # We'll take all the @infos anyway, because some OSes (HPUX) are known to
568 # ignore the AI_ADDRCONFIG hint and return AF_INET6 even if they don't
569 # support them
570 }
571 }
572
573 # In the nonblocking case, caller will be calling ->setup multiple times.
574 # Store configuration in the object for the ->setup method
575 # Yes, these are messy. Sorry, I can't help that...
576
57720022.17ms ${*$self}{io_socket_ip_infos} = \@infos;
578
57920021.39ms ${*$self}{io_socket_ip_idx} = -1;
580
58120021.19ms ${*$self}{io_socket_ip_sockopts} = \@sockopts_enabled;
5822002884µs ${*$self}{io_socket_ip_v6only} = $v6only;
58320021.03ms ${*$self}{io_socket_ip_listenqueue} = $listenqueue;
58420021.07ms ${*$self}{io_socket_ip_blocking} = $blocking;
585
58620023.84ms ${*$self}{io_socket_ip_errors} = [ undef, undef, undef ];
587
588 # ->setup is allowed to return false in nonblocking mode
58920024.49ms2002316ms $self->setup or !$blocking or return undef;
# spent 316ms making 2002 calls to IO::Socket::IP::setup, avg 158µs/call
590
59120029.63ms return $self;
592}
593
594sub setup
595
# spent 316ms (39.2+277) within IO::Socket::IP::setup which was called 2002 times, avg 158µs/call: # 2002 times (39.2ms+277ms) by IO::Socket::IP::_io_socket_ip__configure at line 589, avg 158µs/call
{
5962002693µs my $self = shift;
597
5982002543µs while(1) {
59920021.01ms ${*$self}{io_socket_ip_idx}++;
60020021.95ms last if ${*$self}{io_socket_ip_idx} >= @{ ${*$self}{io_socket_ip_infos} };
601
60220022.03ms my $info = ${*$self}{io_socket_ip_infos}->[${*$self}{io_socket_ip_idx}];
603
604 $self->socket( @{$info}{qw( family socktype protocol )} ) or
60520025.94ms200252.1ms ( ${*$self}{io_socket_ip_errors}[2] = $!, next );
# spent 52.1ms making 2002 calls to IO::Socket::IP::socket, avg 26µs/call
606
60720021.23ms $self->blocking( 0 ) unless ${*$self}{io_socket_ip_blocking};
608
60920023.37ms foreach my $sockopt ( @{ ${*$self}{io_socket_ip_sockopts} } ) {
61012µs $self->setsockopt( SOL_SOCKET, $sockopt, pack "i", 1 ) or ( $@ = "$!", return undef );
# spent 2µs making 1 call to main::CORE:pack
611 }
612
61320021.06ms if( defined ${*$self}{io_socket_ip_v6only} and defined $AF_INET6 and $info->{family} == $AF_INET6 ) {
614 my $v6only = ${*$self}{io_socket_ip_v6only};
615 $self->setsockopt( IPPROTO_IPV6, IPV6_V6ONLY, pack "i", $v6only ) or ( $@ = "$!", return undef );
616 }
617
61820021.14ms if( defined( my $addr = $info->{localaddr} ) ) {
619 $self->bind( $addr ) or
620 ( ${*$self}{io_socket_ip_errors}[1] = $!, next );
621 }
622
62320021.35ms if( defined( my $listenqueue = ${*$self}{io_socket_ip_listenqueue} ) ) {
624 $self->listen( $listenqueue ) or ( $@ = "$!", return undef );
625 }
626
62720021.34ms if( defined( my $addr = $info->{peeraddr} ) ) {
62820023.80ms2002225ms if( $self->connect( $addr ) ) {
# spent 225ms making 2002 calls to IO::Socket::IP::connect, avg 112µs/call
62920021.54ms $! = 0;
630200211.3ms return 1;
631 }
632
633 if( $! == EINPROGRESS or $! == EWOULDBLOCK ) {
634 ${*$self}{io_socket_ip_connect_in_progress} = 1;
635 return 0;
636 }
637
638 # If connect failed but we have no system error there must be an error
639 # at the application layer, like a bad certificate with
640 # IO::Socket::SSL.
641 # In this case don't continue IP based multi-homing because the problem
642 # cannot be solved at the IP layer.
643 return 0 if ! $!;
644
645 ${*$self}{io_socket_ip_errors}[0] = $!;
646 next;
647 }
648
649 return 1;
650 }
651
652 # Pick the most appropriate error, stringified
653 $! = ( grep defined, @{ ${*$self}{io_socket_ip_errors}} )[0];
654 $@ = "$!";
655 return undef;
656}
657
658
# spent 225ms (60.3+164) within IO::Socket::IP::connect which was called 2002 times, avg 112µs/call: # 2002 times (60.3ms+164ms) by IO::Socket::IP::setup at line 628, avg 112µs/call
sub connect :method
659{
6602002592µs my $self = shift;
661
662 # It seems that IO::Socket hides EINPROGRESS errors, making them look like
663 # a success. This is annoying here.
664 # Instead of putting up with its frankly-irritating intentional breakage of
665 # useful APIs I'm just going to end-run around it and call core's connect()
666 # directly
667
6682002956µs if( @_ ) {
6692002776µs my ( $addr ) = @_;
670
671 # Annoyingly IO::Socket's connect() is where the timeout logic is
672 # implemented, so we'll have to reinvent it here
67320021.65ms my $timeout = ${*$self}{'io_socket_timeout'};
674
6752002619µs return connect( $self, $addr ) unless defined $timeout;
676
67720023.76ms200214.3ms my $was_blocking = $self->blocking( 0 );
# spent 14.3ms making 2002 calls to IO::Socket::blocking, avg 7µs/call
678
6792002127ms2002109ms my $err = defined connect( $self, $addr ) ? 0 : $!+0;
# spent 109ms making 2002 calls to IO::Socket::IP::CORE:connect, avg 54µs/call
680
68120022.51ms if( !$err ) {
682 # All happy
683 $self->blocking( $was_blocking );
684 return 1;
685 }
686 elsif( not( $err == EINPROGRESS or $err == EWOULDBLOCK ) ) {
687 # Failed for some other reason
688 return undef;
689 }
690 elsif( !$was_blocking ) {
691 # We shouldn't block anyway
692 return undef;
693 }
694
69540048.65ms20023.52ms my $vec = ''; vec( $vec, $self->fileno, 1 ) = 1;
# spent 3.52ms making 2002 calls to IO::Handle::fileno, avg 2µs/call
696200211.6ms20026.77ms if( !select( undef, $vec, $vec, $timeout ) ) {
# spent 6.77ms making 2002 calls to IO::Socket::IP::CORE:sselect, avg 3µs/call
697 $! = ETIMEDOUT;
698 return undef;
699 }
700
701 # Hoist the error by connect()ing a second time
70220024.91ms200222.6ms $err = $self->getsockopt( SOL_SOCKET, SO_ERROR );
# spent 22.6ms making 2002 calls to IO::Socket::getsockopt, avg 11µs/call
7032002910µs $err = 0 if $err == EISCONN; # Some OSes give EISCONN
704
70520022.35ms20028.59ms $self->blocking( $was_blocking );
# spent 8.59ms making 2002 calls to IO::Socket::blocking, avg 4µs/call
706
7072002637µs $! = $err, return undef if $err;
708200211.0ms return 1;
709 }
710
711 return 1 if !${*$self}{io_socket_ip_connect_in_progress};
712
713 # See if a connect attempt has just failed with an error
714 if( my $errno = $self->getsockopt( SOL_SOCKET, SO_ERROR ) ) {
715 delete ${*$self}{io_socket_ip_connect_in_progress};
716 ${*$self}{io_socket_ip_errors}[0] = $! = $errno;
717 return $self->setup;
718 }
719
720 # No error, so either connect is still in progress, or has completed
721 # successfully. We can tell by trying to connect() again; either it will
722 # succeed or we'll get EISCONN (connected successfully), or EALREADY
723 # (still in progress). This even works on MSWin32.
724 my $addr = ${*$self}{io_socket_ip_infos}[${*$self}{io_socket_ip_idx}]{peeraddr};
725
726 if( connect( $self, $addr ) or $! == EISCONN ) {
727 delete ${*$self}{io_socket_ip_connect_in_progress};
728 $! = 0;
729 return 1;
730 }
731 else {
732 $! = EINPROGRESS;
733 return 0;
734 }
735}
736
737sub connected
738{
739 my $self = shift;
740 return defined $self->fileno &&
741 !${*$self}{io_socket_ip_connect_in_progress} &&
742 defined getpeername( $self ); # ->peername caches, we need to detect disconnection
743}
744
745=head1 METHODS
746
747As well as the following methods, this class inherits all the methods in
748L<IO::Socket> and L<IO::Handle>.
749
750=cut
751
752sub _get_host_service
753{
754 my $self = shift;
755 my ( $addr, $flags, $xflags ) = @_;
756
757 defined $addr or
758 $! = ENOTCONN, return;
759
760 $flags |= NI_DGRAM if $self->socktype == SOCK_DGRAM;
761
762 my ( $err, $host, $service ) = getnameinfo( $addr, $flags, $xflags || 0 );
763 croak "getnameinfo - $err" if $err;
764
765 return ( $host, $service );
766}
767
768sub _unpack_sockaddr
769{
770 my ( $addr ) = @_;
771 my $family = sockaddr_family $addr;
772
773 if( $family == AF_INET ) {
774 return ( Socket::unpack_sockaddr_in( $addr ) )[1];
775 }
776 elsif( defined $AF_INET6 and $family == $AF_INET6 ) {
777 return ( Socket::unpack_sockaddr_in6( $addr ) )[1];
778 }
779 else {
780 croak "Unrecognised address family $family";
781 }
782}
783
784=head2 ( $host, $service ) = $sock->sockhost_service( $numeric )
785
786Returns the hostname and service name of the local address (that is, the
787socket address given by the C<sockname> method).
788
789If C<$numeric> is true, these will be given in numeric form rather than being
790resolved into names.
791
792The following four convenience wrappers may be used to obtain one of the two
793values returned here. If both host and service names are required, this method
794is preferable to the following wrappers, because it will call
795C<getnameinfo(3)> only once.
796
797=cut
798
799sub sockhost_service
800{
801 my $self = shift;
802 my ( $numeric ) = @_;
803
804 $self->_get_host_service( $self->sockname, $numeric ? NI_NUMERICHOST|NI_NUMERICSERV : 0 );
805}
806
807=head2 $addr = $sock->sockhost
808
809Return the numeric form of the local address as a textual representation
810
811=head2 $port = $sock->sockport
812
813Return the numeric form of the local port number
814
815=head2 $host = $sock->sockhostname
816
817Return the resolved name of the local address
818
819=head2 $service = $sock->sockservice
820
821Return the resolved name of the local port number
822
823=cut
824
825sub sockhost { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, NI_NUMERICHOST, NIx_NOSERV ) )[0] }
826sub sockport { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, NI_NUMERICSERV, NIx_NOHOST ) )[1] }
827
828sub sockhostname { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, 0, NIx_NOSERV ) )[0] }
829sub sockservice { my $self = shift; scalar +( $self->_get_host_service( $self->sockname, 0, NIx_NOHOST ) )[1] }
830
831=head2 $addr = $sock->sockaddr
832
833Return the local address as a binary octet string
834
835=cut
836
837sub sockaddr { my $self = shift; _unpack_sockaddr $self->sockname }
838
839=head2 ( $host, $service ) = $sock->peerhost_service( $numeric )
840
841Returns the hostname and service name of the peer address (that is, the
842socket address given by the C<peername> method), similar to the
843C<sockhost_service> method.
844
845The following four convenience wrappers may be used to obtain one of the two
846values returned here. If both host and service names are required, this method
847is preferable to the following wrappers, because it will call
848C<getnameinfo(3)> only once.
849
850=cut
851
852sub peerhost_service
853{
854 my $self = shift;
855 my ( $numeric ) = @_;
856
857 $self->_get_host_service( $self->peername, $numeric ? NI_NUMERICHOST|NI_NUMERICSERV : 0 );
858}
859
860=head2 $addr = $sock->peerhost
861
862Return the numeric form of the peer address as a textual representation
863
864=head2 $port = $sock->peerport
865
866Return the numeric form of the peer port number
867
868=head2 $host = $sock->peerhostname
869
870Return the resolved name of the peer address
871
872=head2 $service = $sock->peerservice
873
874Return the resolved name of the peer port number
875
876=cut
877
878sub peerhost { my $self = shift; scalar +( $self->_get_host_service( $self->peername, NI_NUMERICHOST, NIx_NOSERV ) )[0] }
879sub peerport { my $self = shift; scalar +( $self->_get_host_service( $self->peername, NI_NUMERICSERV, NIx_NOHOST ) )[1] }
880
881sub peerhostname { my $self = shift; scalar +( $self->_get_host_service( $self->peername, 0, NIx_NOSERV ) )[0] }
882sub peerservice { my $self = shift; scalar +( $self->_get_host_service( $self->peername, 0, NIx_NOHOST ) )[1] }
883
884=head2 $addr = $peer->peeraddr
885
886Return the peer address as a binary octet string
887
888=cut
889
890sub peeraddr { my $self = shift; _unpack_sockaddr $self->peername }
891
892# This unbelievably dodgy hack works around the bug that IO::Socket doesn't do
893# it
894# https://rt.cpan.org/Ticket/Display.html?id=61577
895sub accept
896{
897 my $self = shift;
898 my ( $new, $peer ) = $self->SUPER::accept( @_ ) or return;
899
900 ${*$new}{$_} = ${*$self}{$_} for qw( io_socket_domain io_socket_type io_socket_proto );
901
902 return wantarray ? ( $new, $peer )
903 : $new;
904}
905
906# This second unbelievably dodgy hack guarantees that $self->fileno doesn't
907# change, which is useful during nonblocking connect
908
# spent 52.1ms (14.8+37.3) within IO::Socket::IP::socket which was called 2002 times, avg 26µs/call: # 2002 times (14.8ms+37.3ms) by IO::Socket::IP::setup at line 605, avg 26µs/call
sub socket :method
909{
9102002629µs my $self = shift;
911200212.0ms400437.3ms return $self->SUPER::socket(@_) if not defined $self->fileno;
# spent 33.1ms making 2002 calls to IO::Socket::socket, avg 17µs/call # spent 4.21ms making 2002 calls to IO::Handle::fileno, avg 2µs/call
912
913 # I hate core prototypes sometimes...
914 socket( my $tmph, $_[0], $_[1], $_[2] ) or return undef;
915
916 dup2( $tmph->fileno, $self->fileno ) or die "Unable to dup2 $tmph onto $self - $!";
917}
918
919# Versions of IO::Socket before 1.35 may leave socktype undef if from, say, an
920# ->fdopen call. In this case we'll apply a fix
921
# spent 16µs within IO::Socket::IP::BEGIN@921 which was called: # once (16µs+0s) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 932
BEGIN {
922115µs if( eval($IO::Socket::VERSION) < 1.35 ) {
# spent 2µs executing statements in string eval
923 *socktype = sub {
924 my $self = shift;
925 my $type = $self->SUPER::socktype;
926 if( !defined $type ) {
927 $type = $self->sockopt( Socket::SO_TYPE() );
928 }
929 return $type;
930 };
931 }
9321192µs116µs}
# spent 16µs making 1 call to IO::Socket::IP::BEGIN@921
933
934=head2 $inet = $sock->as_inet
935
936Returns a new L<IO::Socket::INET> instance wrapping the same filehandle. This
937may be useful in cases where it is required, for backward-compatibility, to
938have a real object of C<IO::Socket::INET> type instead of C<IO::Socket::IP>.
939The new object will wrap the same underlying socket filehandle as the
940original, so care should be taken not to continue to use both objects
941concurrently. Ideally the original C<$sock> should be discarded after this
942method is called.
943
944This method checks that the socket domain is C<PF_INET> and will throw an
945exception if it isn't.
946
947=cut
948
949sub as_inet
950{
951 my $self = shift;
952 croak "Cannot downgrade a non-PF_INET socket to IO::Socket::INET" unless $self->sockdomain == AF_INET;
953 return IO::Socket::INET->new_from_fd( $self->fileno, "r+" );
954}
955
956=head1 NON-BLOCKING
957
958If the constructor is passed a defined but false value for the C<Blocking>
959argument then the socket is put into non-blocking mode. When in non-blocking
960mode, the socket will not be set up by the time the constructor returns,
961because the underlying C<connect(2)> syscall would otherwise have to block.
962
963The non-blocking behaviour is an extension of the C<IO::Socket::INET> API,
964unique to C<IO::Socket::IP>, because the former does not support multi-homed
965non-blocking connect.
966
967When using non-blocking mode, the caller must repeatedly check for
968writeability on the filehandle (for instance using C<select> or C<IO::Poll>).
969Each time the filehandle is ready to write, the C<connect> method must be
970called, with no arguments. Note that some operating systems, most notably
971C<MSWin32> do not report a C<connect()> failure using write-ready; so you must
972also C<select()> for exceptional status.
973
974While C<connect> returns false, the value of C<$!> indicates whether it should
975be tried again (by being set to the value C<EINPROGRESS>, or C<EWOULDBLOCK> on
976MSWin32), or whether a permanent error has occurred (e.g. C<ECONNREFUSED>).
977
978Once the socket has been connected to the peer, C<connect> will return true
979and the socket will now be ready to use.
980
981Note that calls to the platform's underlying C<getaddrinfo(3)> function may
982block. If C<IO::Socket::IP> has to perform this lookup, the constructor will
983block even when in non-blocking mode.
984
985To avoid this blocking behaviour, the caller should pass in the result of such
986a lookup using the C<PeerAddrInfo> or C<LocalAddrInfo> arguments. This can be
987achieved by using L<Net::LibAsyncNS>, or the C<getaddrinfo(3)> function can be
988called in a child process.
989
990 use IO::Socket::IP;
991 use Errno qw( EINPROGRESS EWOULDBLOCK );
992
993 my @peeraddrinfo = ... # Caller must obtain the getaddinfo result here
994
995 my $socket = IO::Socket::IP->new(
996 PeerAddrInfo => \@peeraddrinfo,
997 Blocking => 0,
998 ) or die "Cannot construct socket - $@";
999
1000 while( !$socket->connect and ( $! == EINPROGRESS || $! == EWOULDBLOCK ) ) {
1001 my $wvec = '';
1002 vec( $wvec, fileno $socket, 1 ) = 1;
1003 my $evec = '';
1004 vec( $evec, fileno $socket, 1 ) = 1;
1005
1006 select( undef, $wvec, $evec, undef ) or die "Cannot select - $!";
1007 }
1008
1009 die "Cannot connect - $!" if $!;
1010
1011 ...
1012
1013The example above uses C<select()>, but any similar mechanism should work
1014analogously. C<IO::Socket::IP> takes care when creating new socket filehandles
1015to preserve the actual file descriptor number, so such techniques as C<poll>
1016or C<epoll> should be transparent to its reallocation of a different socket
1017underneath, perhaps in order to switch protocol family between C<PF_INET> and
1018C<PF_INET6>.
1019
1020For another example using C<IO::Poll> and C<Net::LibAsyncNS>, see the
1021F<examples/nonblocking_libasyncns.pl> file in the module distribution.
1022
1023=cut
1024
1025=head1 C<PeerHost> AND C<LocalHost> PARSING
1026
1027To support the C<IO::Socket::INET> API, the host and port information may be
1028passed in a single string rather than as two separate arguments.
1029
1030If either C<LocalHost> or C<PeerHost> (or their C<...Addr> synonyms) have any
1031of the following special forms then special parsing is applied.
1032
1033The value of the C<...Host> argument will be split to give both the hostname
1034and port (or service name):
1035
1036 hostname.example.org:http # Host name
1037 192.0.2.1:80 # IPv4 address
1038 [2001:db8::1]:80 # IPv6 address
1039
1040In each case, the port or service name (e.g. C<80>) is passed as the
1041C<LocalService> or C<PeerService> argument.
1042
1043Either of C<LocalService> or C<PeerService> (or their C<...Port> synonyms) can
1044be either a service name, a decimal number, or a string containing both a
1045service name and number, in a form such as
1046
1047 http(80)
1048
1049In this case, the name (C<http>) will be tried first, but if the resolver does
1050not understand it then the port number (C<80>) will be used instead.
1051
1052If the C<...Host> argument is in this special form and the corresponding
1053C<...Service> or C<...Port> argument is also defined, the one parsed from
1054the C<...Host> argument will take precedence and the other will be ignored.
1055
1056=head2 ( $host, $port ) = IO::Socket::IP->split_addr( $addr )
1057
1058Utility method that provides the parsing functionality described above.
1059Returns a 2-element list, containing either the split hostname and port
1060description if it could be parsed, or the given address and C<undef> if it was
1061not recognised.
1062
1063 IO::Socket::IP->split_addr( "hostname:http" )
1064 # ( "hostname", "http" )
1065
1066 IO::Socket::IP->split_addr( "192.0.2.1:80" )
1067 # ( "192.0.2.1", "80" )
1068
1069 IO::Socket::IP->split_addr( "[2001:db8::1]:80" )
1070 # ( "2001:db8::1", "80" )
1071
1072 IO::Socket::IP->split_addr( "something.else" )
1073 # ( "something.else", undef )
1074
1075=cut
1076
1077sub split_addr
1078
# spent 29.6ms (18.5+11.0) within IO::Socket::IP::split_addr which was called 2002 times, avg 15µs/call: # 2002 times (18.5ms+11.0ms) by IO::Socket::IP::configure at line 380, avg 15µs/call
{
10792002484µs shift;
10802002723µs my ( $addr ) = @_;
1081
108220022.88ms local ( $1, $2 ); # Placate a taint-related bug; [perl #67962]
1083200221.2ms600611.0ms if( $addr =~ m/\A\[($IPv6_re)\](?::([^\s:]*))?\z/ or
# spent 9.25ms making 2002 calls to IO::Socket::IP::CORE:regcomp, avg 5µs/call # spent 1.78ms making 4004 calls to IO::Socket::IP::CORE:match, avg 443ns/call
1084 $addr =~ m/\A([^\s:]*):([^\s:]*)\z/ ) {
1085 return ( $1, $2 ) if defined $2 and length $2;
1086 return ( $1, undef );
1087 }
1088
108920027.12ms return ( $addr, undef );
1090}
1091
1092=head2 $addr = IO::Socket::IP->join_addr( $host, $port )
1093
1094Utility method that performs the reverse of C<split_addr>, returning a string
1095formed by joining the specified host address and port number. The host address
1096will be wrapped in C<[]> brackets if required (because it is a raw IPv6
1097numeric address).
1098
1099This can be especially useful when combined with the C<sockhost_service> or
1100C<peerhost_service> methods.
1101
1102 say "Connected to ", IO::Socket::IP->join_addr( $sock->peerhost_service );
1103
1104=cut
1105
1106sub join_addr
1107{
1108 shift;
1109 my ( $host, $port ) = @_;
1110
1111 $host = "[$host]" if $host =~ m/:/;
1112
1113 return join ":", $host, $port if defined $port;
1114 return $host;
1115}
1116
1117# Since IO::Socket->new( Domain => ... ) will delete the Domain parameter
1118# before calling ->configure, we need to keep track of which it was
1119
1120package # hide from indexer
1121 IO::Socket::IP::_ForINET;
1122256µs293µs
# spent 50µs (8+42) within IO::Socket::IP::_ForINET::BEGIN@1122 which was called: # once (8µs+42µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 1122
use base qw( IO::Socket::IP );
# spent 50µs making 1 call to IO::Socket::IP::_ForINET::BEGIN@1122 # spent 42µs making 1 call to base::import
1123
1124sub configure
1125{
1126 # This is evil
1127 my $self = shift;
1128 my ( $arg ) = @_;
1129
1130 bless $self, "IO::Socket::IP";
1131 $self->configure( { %$arg, Family => Socket::AF_INET() } );
1132}
1133
1134package # hide from indexer
1135 IO::Socket::IP::_ForINET6;
1136267µs265µs
# spent 35µs (5+30) within IO::Socket::IP::_ForINET6::BEGIN@1136 which was called: # once (5µs+30µs) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 1136
use base qw( IO::Socket::IP );
# spent 35µs making 1 call to IO::Socket::IP::_ForINET6::BEGIN@1136 # spent 30µs making 1 call to base::import
1137
1138sub configure
1139{
1140 # This is evil
1141 my $self = shift;
1142 my ( $arg ) = @_;
1143
1144 bless $self, "IO::Socket::IP";
1145 $self->configure( { %$arg, Family => Socket::AF_INET6() } );
1146}
1147
1148=head1 C<IO::Socket::INET> INCOMPATIBILITES
1149
1150=over 4
1151
1152=item *
1153
1154The behaviour enabled by C<MultiHomed> is in fact implemented by
1155C<IO::Socket::IP> as it is required to correctly support searching for a
1156useable address from the results of the C<getaddrinfo(3)> call. The
1157constructor will ignore the value of this argument, except if it is defined
1158but false. An exception is thrown in this case, because that would request it
1159disable the C<getaddrinfo(3)> search behaviour in the first place.
1160
1161=item *
1162
1163C<IO::Socket::IP> implements both the C<Blocking> and C<Timeout> parameters,
1164but it implements the interaction of both in a different way.
1165
1166In C<::INET>, supplying a timeout overrides the non-blocking behaviour,
1167meaning that the C<connect()> operation will still block despite that the
1168caller asked for a non-blocking socket. This is not explicitly specified in
1169its documentation, nor does this author believe that is a useful behaviour -
1170it appears to come from a quirk of implementation.
1171
1172In C<::IP> therefore, the C<Blocking> parameter takes precedence - if a
1173non-blocking socket is requested, no operation will block. The C<Timeout>
1174parameter here simply defines the maximum time that a blocking C<connect()>
1175call will wait, if it blocks at all.
1176
1177In order to specifically obtain the "blocking connect then non-blocking send
1178and receive" behaviour of specifying this combination of options to C<::INET>
1179when using C<::IP>, perform first a blocking connect, then afterwards turn the
1180socket into nonblocking mode.
1181
1182 my $sock = IO::Socket::IP->new(
1183 PeerHost => $peer,
1184 Timeout => 20,
1185 ) or die "Cannot connect - $@";
1186
1187 $sock->blocking( 0 );
1188
1189This code will behave identically under both C<IO::Socket::INET> and
1190C<IO::Socket::IP>.
1191
1192=back
1193
1194=cut
1195
1196=head1 TODO
1197
1198=over 4
1199
1200=item *
1201
1202Investigate whether C<POSIX::dup2> upsets BSD's C<kqueue> watchers, and if so,
1203consider what possible workarounds might be applied.
1204
1205=back
1206
1207=head1 AUTHOR
1208
1209Paul Evans <leonerd@leonerd.org.uk>
1210
1211=cut
1212
121317µs0x55AA;
 
# spent 109ms within IO::Socket::IP::CORE:connect which was called 2002 times, avg 54µs/call: # 2002 times (109ms+0s) by IO::Socket::IP::connect at line 679, avg 54µs/call
sub IO::Socket::IP::CORE:connect; # opcode
# spent 49.0ms within IO::Socket::IP::CORE:gpbyname which was called 2003 times, avg 24µs/call: # 2002 times (49.0ms+0s) by IO::Socket::IP::_io_socket_ip__configure at line 424, avg 24µs/call # once (26µs+0s) by IO::Socket::IP::BEGIN@39 at line 39
sub IO::Socket::IP::CORE:gpbyname; # opcode
# spent 3.27ms within IO::Socket::IP::CORE:match which was called 6006 times, avg 545ns/call: # 4004 times (1.78ms+0s) by IO::Socket::IP::split_addr at line 1083, avg 443ns/call # 2002 times (1.50ms+0s) by IO::Socket::IP::_io_socket_ip__configure at line 421, avg 749ns/call
sub IO::Socket::IP::CORE:match; # opcode
# spent 900ns within IO::Socket::IP::CORE:qr which was called: # once (900ns+0s) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 62
sub IO::Socket::IP::CORE:qr; # opcode
# spent 9.41ms within IO::Socket::IP::CORE:regcomp which was called 2003 times, avg 5µs/call: # 2002 times (9.25ms+0s) by IO::Socket::IP::split_addr at line 1083, avg 5µs/call # once (157µs+0s) by Search::Elasticsearch::Cxn::HTTPTiny::BEGIN@6 at line 62
sub IO::Socket::IP::CORE:regcomp; # opcode
# spent 6.77ms within IO::Socket::IP::CORE:sselect which was called 2002 times, avg 3µs/call: # 2002 times (6.77ms+0s) by IO::Socket::IP::connect at line 696, avg 3µs/call
sub IO::Socket::IP::CORE:sselect; # opcode
# spent 1.05ms within IO::Socket::IP::CORE:subst which was called 2002 times, avg 527ns/call: # 2002 times (1.05ms+0s) by IO::Socket::IP::_io_socket_ip__configure at line 491, avg 527ns/call
sub IO::Socket::IP::CORE:subst; # opcode