blib/lib/AnyEvent/Socket.pm

filter: Statementstmt 50.6% 163 / 322 Branchbran 31.1% 71 / 228 Conditioncond 21.1% 22 / 104 MC/DCmcdc 9.8% 6 / 61 Subroutinesub 53.3% 32 / 60 Podpod 100% 20 / 20 totaltotal 39.5% 314 / 795 CC 155 SCAR 43.2
CC
155
Cov
39%
_tcp_bind
420.0
format_ipv6
156.0
__ANON__
132.0
CRAP 75.2
« blib/lib/AnyEvent/Handle.pm ↑ Summary blib/lib/AnyEvent/Debug.pm »
1 =head1 NAME
2  
3 AnyEvent::Socket - useful IPv4 and IPv6 stuff. also unix domain sockets. and stuff.
4  
5 =head1 SYNOPSIS
6  
7 use AnyEvent::Socket;
8
9 tcp_connect "gameserver.deliantra.net", 13327, sub {
10 my ($fh) = @_
11 or die "gameserver.deliantra.net connect failed: $!";
12
13 # enjoy your filehandle
14 };
15
16 # a simple tcp server
17 tcp_server undef, 8888, sub {
18 my ($fh, $host, $port) = @_;
19
20 syswrite $fh, "The internet is full, $host:$port. Go away!\015\012";
21 };
22  
23 =head1 DESCRIPTION
24  
25 This module implements various utility functions for handling internet
26 protocol addresses and sockets, in an as transparent and simple way as
27 possible.
28  
29 All functions documented without C<AnyEvent::Socket::> prefix are exported
30 by default.
31  
32 =over 4
33  
34 =cut
35  
36 package AnyEvent::Socket;
37  
38 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 5 ▶use Socket qw(AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM SOL_SOCKET SO_REUSEADDR);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 5 ▶use AnyEvent (); BEGIN { AnyEvent::common_sense }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 ▶use AnyEvent::Util qw(guard AF_INET6);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 5 ▶use AnyEvent::DNS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 5 our @EXPORT = qw(
49 getprotobyname
50 parse_hostport format_hostport
51 parse_ipv4 parse_ipv6
52 parse_ip parse_address
53 format_ipv4 format_ipv6
54 format_ip format_address
55 address_family
56 inet_aton
57 tcp_server
58 tcp_connect
59 );
60  
61 5 our $VERSION = $AnyEvent::VERSION;
62  
63 =item $ipn = parse_ipv4 $dotted_quad
64  
65 Tries to parse the given dotted quad IPv4 address and return it in
66 octet form (or undef when it isn't in a parsable format). Supports all
67 forms specified by POSIX (e.g. C<10.0.0.1>, C<10.1>, C<10.0x020304>,
68 C<0x12345678> or C<0377.0377.0377.0377>).
69  
70 =cut
71  
72 9 ▶sub parse_ipv4($) {
Subroutine parse_ipv4: called CC=5 SCAR=16.3
Pod documented
73 9 ▶ $_[0] =~ /^ (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* )
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^ (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) (?:\. (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) ){0,3}$/x or return undef 7 2 9
74 (?:\. (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) ){0,3}$/x
75 or return undef;
76  
77 7 ▶ @_ = map /^0/ ? oct : $_, split /\./, $_[0];
Branch: 1 branch
BranchTrueFalseTotal
/^0/ ? : 8 20 28
78  
79 # check leading parts against range
80 7 ▶ return undef if grep $_ >= 256, @_[0 .. @_ - 2];
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ >= 256), @_[0 .. @_ - 2]) 0 7 7
81  
82 # check trailing part against range
83 7 ▶ return undef if $_[-1] >= 2 ** (8 * (4 - $#_));
Branch: 1 branch
BranchTrueFalseTotal
if $_[-1] >= 2 ** (8 * (4 - $#_)) 0 7 7
84  
85 7 pack "N", (pop)
86 + ($_[0] << 24)
87 + ($_[1] << 16)
88 + ($_[2] << 8);
89 }
90  
91 =item $ipn = parse_ipv6 $textual_ipv6_address
92  
93 Tries to parse the given IPv6 address and return it in
94 octet form (or undef when it isn't in a parsable format).
95  
96 Should support all forms specified by RFC 2373 (and additionally all IPv4
97 forms supported by parse_ipv4). Note that scope-id's are not supported
98 (and will not parse).
99  
100 This function works similarly to C<inet_pton AF_INET6, ...>.
101  
102 Example:
103  
104 print unpack "H*", parse_ipv6 "2002:5345::10.0.0.1";
105 # => 2002534500000000000000000a000001
106  
107 print unpack "H*", parse_ipv6 "192.89.98.1";
108 # => 00000000000000000000ffffc0596201
109  
110 =cut
111  
112 16 ▶sub parse_ipv6($) {
Subroutine parse_ipv6: called CC=14 SCAR=27.9
Pod documented
113 # quick test to avoid longer processing
114 16 my $n = $_[0] =~ y/://;
115  
116 16 ▶ if ($n < 2 || $n > 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 2 or $n > 8) 4 12 16
Condition66%2/3
$n < 2 or $n > 8
l!l&&r!l&&!r
4012
MC/DC50%1/2
$n < 2 or $n > 8
$n < 2 $n > 8
Truth table66%2/3
A or B
A= $n < 2B= $n > 8
ABresult
1X1
011
000
117 4 ▶ if (!$n && (my $ipn = parse_ipv4 $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $n and my $ipn = &parse_ipv4($_[0])) 2 2 4
Condition66%2/3
not $n and my $ipn = &parse_ipv4($_[0])
!ll&&!rl&&r
022
MC/DC50%1/2
not $n and my $ipn = &parse_ipv4($_[0])
not $n my $ipn = &parse_ipv4($_[0])
Truth table66%2/3
not(A) and B
A= not $nB= my $ipn = &parse_ipv4($_[0])
ABresult
0X0
100
111
118 2 return "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff$ipn";
119 }
120 2 return undef;
121 }
122  
123 12 my ($h, $t) = split /::/, $_[0], 2;
124  
125 12 ▶ unless (defined $t) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t) 0 12 12
126 0 ($h, $t) = (undef, $h);
127 }
128  
129 12 my @h = split /:/, $h, -1;
130 12 my @t = split /:/, $t, -1;
131  
132 # check for ipv4 tail
133 12 ▶ if (@t && $t[-1]=~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t and $t[-1] =~ /\./) 4 8 12
Condition66%2/3
@t and $t[-1] =~ /\./
!ll&&!rl&&r
084
MC/DC50%1/2
@t and $t[-1] =~ /\./
@t $t[-1] =~ /\./
Truth table66%2/3
A and B
A= @tB= $t[-1] =~ /\./
ABresult
0X0
100
111
134 4 ▶ return undef if $n > 6;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 6 0 4 4
135  
136 4 ▶ my $ipn = parse_ipv4 pop @t
Branch: 1 branch
BranchTrueFalseTotal
my $ipn = &parse_ipv4(pop @t) or return undef 4 0 4
137 or return undef;
138  
139 4 push @t, map +(sprintf "%x", $_), unpack "nn", $ipn;
140 }
141  
142 # no :: then we need to have exactly 8 components
143 12 ▶ return undef unless @h + @t == 8 || $_[0] =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless @h + @t == 8 or $_[0] =~ /::/ 0 12 12
Condition33%1/3
@h + @t == 8 or $_[0] =~ /::/
l!l&&r!l&&!r
0120
MC/DC0%0/2
@h + @t == 8 or $_[0] =~ /::/
@h + @t == 8 $_[0] =~ /::/
Truth table33%1/3
A or B
A= @h + @t == 8B= $_[0] =~ /::/
ABresult
1X1
011
000
144  
145 # now check all parts for validity
146 12 ▶ return undef if grep !/^[0-9a-fA-F]{1,4}$/, @h, @t;
Branch: 1 branch
BranchTrueFalseTotal
if grep((!/^[0-9a-fA-F]{1,4}$/), @h, @t) 0 12 12
147  
148 # now pad...
149 12 ▶ push @h, 0 while @h + @t < 8;
Branch: 1 branch
BranchTrueFalseTotal
@h + @t < 8 and do { push @h, 0 } 66 12 78
150  
151 # and done
152 12 pack "n*", map hex, @h, @t
153 }
154  
155 =item $token = parse_unix $hostname
156  
157 This function exists mainly for symmetry to the other C<parse_protocol>
158 functions - it takes a hostname and, if it is C<unix/>, it returns a
159 special address token, otherwise C<undef>.
160  
161 The only use for this function is probably to detect whether a hostname
162 matches whatever AnyEvent uses for unix domain sockets.
163  
164 =cut
165  
166 2 ▶sub parse_unix($) {
Subroutine parse_unix: called CC=2 SCAR=7.6
Pod documented
167 2 ▶ $_[0] eq "unix/"
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'unix/' ? : 0 2 2
168 ? pack "S", AF_UNIX
169 : undef
170  
171 }
172  
173 =item $ipn = parse_address $ip
174  
175 Combines C<parse_ipv4>, C<parse_ipv6> and C<parse_unix> in one
176 function. The address here refers to the host address (not socket address)
177 in network form (binary).
178  
179 If the C<$text> is C<unix/>, then this function returns a special token
180 recognised by the other functions in this module to mean "UNIX domain
181 socket".
182  
183 If the C<$text> to parse is a plain IPv4 or mapped IPv4 in IPv6 address
184 (:ffff::<ipv4>), then it will be treated as an IPv4 address and four
185 octets will be returned. If you don't want that, you have to call
186 C<parse_ipv4> and/or C<parse_ipv6> manually (the latter always returning a
187 16 octet IPv6 address for mapped IPv4 addresses).
188  
189 Example:
190  
191 print unpack "H*", parse_address "10.1.2.3";
192 # => 0a010203
193  
194 =item $ipn = AnyEvent::Socket::aton $ip
195  
196 Same as C<parse_address>, but not exported (think C<Socket::inet_aton> but
197 I<without> name resolution).
198  
199 =cut
200  
201 4 ▶sub parse_address($) {
Subroutine parse_address: called CC=3 SCAR=11.0
Pod documented
202 4 for (&parse_ipv6) {
203 4 ▶ if ($_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_) { } 2 2 4
204 2 s/^\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff//;
205 2 return $_
206 } else {
207 2 return &parse_unix
208 }
209 }
210 }
211  
212 5 *aton = \&parse_address;
213  
214 =item ($name, $aliases, $proto) = getprotobyname $name
215  
216 Works like the builtin function of the same name, except it tries hard to
217 work even on broken platforms (well, that's windows), where getprotobyname
218 is traditionally very unreliable.
219  
220 Example: get the protocol number for TCP (usually 6)
221  
222 my $proto = getprotobyname "tcp";
223  
224 =cut
225  
226 # microsoft can't even get getprotobyname working (the etc/protocols file
227 # gets lost fairly often on windows), so we have to hardcode some common
228 # protocol numbers ourselves.
229 5 our %PROTO_BYNAME;
230  
231 5 ▶$PROTO_BYNAME{tcp} = Socket::IPPROTO_TCP () if defined &Socket::IPPROTO_TCP;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Socket::IPPROTO_TCP 5 0 5
232 5 ▶$PROTO_BYNAME{udp} = Socket::IPPROTO_UDP () if defined &Socket::IPPROTO_UDP;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Socket::IPPROTO_UDP 5 0 5
233 5 ▶$PROTO_BYNAME{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP;
Branch: 1 branch
BranchTrueFalseTotal
if defined &Socket::IPPROTO_ICMP 5 0 5
234  
235 2 ▶sub getprotobyname($) {
Subroutine getprotobyname: called CC=3 SCAR=12.5
Pod documented
236 2 my $name = lc shift;
237  
238 2 ▶ defined (my $proton = $PROTO_BYNAME{$name} || (getprotobyname $name)[2])
Branch: 1 branch
BranchTrueFalseTotal
defined(my $proton = $AnyEvent::Socket::PROTO_BYNAME{$name} || (CORE::getprotobyname $name)[2]) or return 2 0 2
Condition33%1/3
$AnyEvent::Socket::PROTO_BYNAME{$name} || (CORE::getprotobyname $name)[2]
l!l&&r!l&&!r
200
MC/DC0%0/2
$AnyEvent::Socket::PROTO_BYNAME{$name} || (CORE::getprotobyname $name)[2]
$AnyEvent::Socket::PROTO_BYNAME{$name} (CORE::getprotobyname $name)[2]
Truth table33%1/3
A || B
A= $AnyEvent::Socket::PROTO_BYNAME{$name}B= (CORE::getprotobyname $name)[2]
ABresult
1X1
011
000
239 or return;
240  
241 2 ($name, uc $name, $proton)
242 }
243  
244 =item ($host, $service) = parse_hostport $string[, $default_service]
245  
246 Splitting a string of the form C<hostname:port> is a common
247 problem. Unfortunately, just splitting on the colon makes it hard to
248 specify IPv6 addresses and doesn't support the less common but well
249 standardised C<[ip literal]> syntax.
250  
251 This function tries to do this job in a better way, it supports (at
252 least) the following formats, where C<port> can be a numerical port
253 number of a service name, or a C<name=port> string, and the C< port> and
254 C<:port> parts are optional. Also, everywhere where an IP address is
255 supported a hostname or unix domain socket address is also supported (see
256 C<parse_unix>), and strings starting with C</> will also be interpreted as
257 unix domain sockets.
258  
259 hostname:port e.g. "www.linux.org", "www.x.de:443", "www.x.de:https=443",
260 ipv4:port e.g. "198.182.196.56", "127.1:22"
261 ipv6 e.g. "::1", "affe::1"
262 [ipv4or6]:port e.g. "[::1]", "[10.0.1]:80"
263 [ipv4or6] port e.g. "[127.0.0.1]", "[www.x.org] 17"
264 ipv4or6 port e.g. "::1 443", "10.0.0.1 smtp"
265 unix/:path e.g. "unix/:/path/to/socket"
266 /path e.g. "/path/to/socket"
267  
268 It also supports defaulting the service name in a simple way by using
269 C<$default_service> if no service was detected. If neither a service was
270 detected nor a default was specified, then this function returns the
271 empty list. The same happens when a parse error was detected, such as a
272 hostname with a colon in it (the function is rather forgiving, though).
273  
274 Example:
275  
276 print join ",", parse_hostport "localhost:443";
277 # => "localhost,443"
278  
279 print join ",", parse_hostport "localhost", "https";
280 # => "localhost,https"
281  
282 print join ",", parse_hostport "[::1]";
283 # => "," (empty list)
284  
285 print join ",", parse_hostport "/tmp/debug.sock";
286 # => "unix/", "/tmp/debug.sock"
287  
288 =cut
289  
290 18 ▶sub parse_hostport($;$) {
Subroutine parse_hostport: called CC=12 SCAR=25.0
Pod documented
291 18 my ($host, $port);
292  
293 18 for ("$_[0]") { # work on a copy, just in case, and also reset pos
294  
295 # shortcut for /path
296 18 ▶ return ("unix/", $_)
Branch: 1 branch
BranchTrueFalseTotal
if m[^/] 0 18 18
297 if m%^/%;
298  
299 # parse host, special cases: "ipv6" or "ipv6[#p ]port"
300 18 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/cgx and &parse_ipv6($host)) 13 5 18
Condition66%2/3
($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/cgx and &parse_ipv6($host)
!ll&&!rl&&r
1305
MC/DC50%1/2
($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/cgx and &parse_ipv6($host)
($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/cgx &parse_ipv6($host)
Truth table66%2/3
A and B
A= ($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/cgxB= &parse_ipv6($host)
ABresult
0X0
100
111
301 ($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/xgc
302 and parse_ipv6 $host
303 ) {
304 13 /^\s*/xgc;
305  
306 13 ▶ if (/^ \[ ([^\[\]]+) \]/xgc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^ \[ ([^\[\]]+) \]/cgx) { } 6 7 13
elsif (/^ ([^\[\]:\ ]+) /cgx) { } 6 1 7
307 6 $host = $1;
308 } elsif (/^ ([^\[\]:\ ]+) /xgc) {
309 6 $host = $1;
310 } else {
311 1 return;
312 }
313 }
314  
315 # parse port
316 17 ▶ if (/\G (?:\s+|:|\#) ([^:[:space:]]+) \s*$/xgc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G (?:\s+|:|\#) ([^:[:space:]]+) \s*$/cgx) { } 8 9 17
elsif (/\G\s*$/cg and length $_[1]) { } 6 3 9
317 8 $port = $1;
318 ▶ } elsif (/\G\s*$/gc && length $_[1]) {
Condition100%3/3
/\G\s*$/cg and length $_[1]
!ll&&!rl&&r
216
MC/DC100%2/2
/\G\s*$/cg and length $_[1]
/\G\s*$/cg length $_[1]
Truth table100%3/3
A and B
A= /\G\s*$/cgB= length $_[1]
ABresult
0X0
100
111
319 6 $port = $_[1];
320 } else {
321 3 return;
322 }
323  
324 }
325  
326 # hostnames must not contain :'s
327 14 ▶ return if $host =~ /:/ && !parse_ipv6 $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/ and not &parse_ipv6($host) 0 14 14
Condition66%2/3
$host =~ /:/ and not &parse_ipv6($host)
!ll&&!rl&&r
770
MC/DC0%0/2
$host =~ /:/ and not &parse_ipv6($host)
$host =~ /:/ not &parse_ipv6($host)
Truth table66%2/3
A and not(B)
A= $host =~ /:/B= not &parse_ipv6($host)
ABresult
0X0
100
111
328  
329 14 ($host, $port)
330 }
331  
332 =item $string = format_hostport $host, $port
333  
334 Takes a host (in textual form) and a port and formats in unambigiously in
335 a way that C<parse_hostport> can parse it again. C<$port> can be C<undef>.
336  
337 =cut
338  
339 0 ▶sub format_hostport($;$) {
Subroutine format_hostport: not called CC=3 SCAR=24.8
Pod documented
340 0 my ($host, $port) = @_;
341  
342 0 ▶ $port = ":$port" if length $port;
Branch: 1 branch
BranchTrueFalseTotal
if length $port 0 0 0
343 0 ▶ $host = "[$host]" if $host =~ /:/;
Branch: 1 branch
BranchTrueFalseTotal
if $host =~ /:/ 0 0 0
344  
345 0 "$host$port"
346 }
347  
348 =item $sa_family = address_family $ipn
349  
350 Returns the address family/protocol-family (AF_xxx/PF_xxx, in one value :)
351 of the given host address in network format.
352  
353 =cut
354  
355 6 ▶sub address_family($) {
Subroutine address_family: called CC=3 SCAR=16.0
Pod documented
356 6 ▶ 4 == length $_[0]
Branch: 2 branches
BranchTrueFalseTotal
4 == length $_[0] ? : 6 0 6
16 == length $_[0] ? : 0 0 0
357 ? AF_INET
358 : 16 == length $_[0]
359 ? AF_INET6
360 : unpack "S", $_[0]
361 }
362  
363 =item $text = format_ipv4 $ipn
364  
365 Expects a four octet string representing a binary IPv4 address and returns
366 its textual format. Rarely used, see C<format_address> for a nicer
367 interface.
368  
369 =item $text = format_ipv6 $ipn
370  
371 Expects a sixteen octet string representing a binary IPv6 address and
372 returns its textual format. Rarely used, see C<format_address> for a
373 nicer interface.
374  
375 =item $text = format_address $ipn
376  
377 Covnvert a host address in network format (e.g. 4 octets for IPv4 or 16
378 octets for IPv6) and convert it into textual form.
379  
380 Returns C<unix/> for UNIX domain sockets.
381  
382 This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>,
383 except it automatically detects the address type.
384  
385 Returns C<undef> if it cannot detect the type.
386  
387 If the C<$ipn> is a mapped IPv4 in IPv6 address (:ffff::<ipv4>), then just
388 the contained IPv4 address will be returned. If you do not want that, you
389 have to call C<format_ipv6> manually.
390  
391 Example:
392  
393 print format_address "\x01\x02\x03\x05";
394 => 1.2.3.5
395  
396 =item $text = AnyEvent::Socket::ntoa $ipn
397  
398 Same as format_address, but not exported (think C<inet_ntoa>).
399  
400 =cut
401  
402 2 ▶sub format_ipv4($) {
Subroutine format_ipv4: called CC=1 SCAR=0.0
Pod documented
403 2 join ".", unpack "C4", $_[0]
404 }
405  
406 0 ▶sub format_ipv6($) {
Subroutine format_ipv6: not called CC=12 SCAR=50.5
Pod documented
407 0 ▶ if ($_[0] =~ /^\x00\x00\x00\x00\x00\x00\x00\x00/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\x00\x00\x00\x00\x00\x00\x00\x00/) 0 0 0
408 0 ▶ if (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 eq $_[0]) {
Branch: 5 branches
BranchTrueFalseTotal
if (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 eq $_[0]) { } 0 0 0
elsif (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1 eq $_[0]) { } 0 0 0
elsif (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr($_[0], 0, 12)) { } 0 0 0
elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr($_[0], 0, 12)) { } 0 0 0
elsif (v0.0.0.0.0.0.0.0.255.255.0.0 eq substr($_[0], 0, 12)) { } 0 0 0
409 0 return "::";
410 } elsif (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1 eq $_[0]) {
411 0 return "::1";
412 } elsif (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr $_[0], 0, 12) {
413 # v4compatible
414 0 return "::" . format_ipv4 substr $_[0], 12;
415 } elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) {
416 # v4mapped
417 0 return "::ffff:" . format_ipv4 substr $_[0], 12;
418 } elsif (v0.0.0.0.0.0.0.0.255.255.0.0 eq substr $_[0], 0, 12) {
419 # v4translated
420 0 return "::ffff:0:" . format_ipv4 substr $_[0], 12;
421 }
422 }
423  
424 0 my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0];
425  
426 # this is admittedly rather sucky
427 0 ▶ $ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0 (?:$|:)/::/x 0 0 0
Condition0%0/12
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0 (?:$|:)/::/x
l!l&&r!l&&!r
000
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/x
l!l&&r!l&&!r
000
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x
l!l&&r!l&&!r
000
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x
l!l&&r!l&&!r
000
MC/DC0%0/5
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/x or $ip =~ s/(?:^|:) 0:0:0 (?:$|:)/::/x
$ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/x $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/x $ip =~ s/(?:^|:) 0:0:0 (?:$|:)/::/x
Truth table0%0/6
A or B or C or D or E
A= $ip =~ s/(?:^|:) 0:0:0:0:0:0:0 (?:$|:)/::/xB= $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/xC= $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/xD= $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/xE= $ip =~ s/(?:^|:) 0:0:0 (?:$|:)/::/x
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
428 or $ip =~ s/(?:^|:) 0:0:0:0:0:0 (?:$|:)/::/x
429 or $ip =~ s/(?:^|:) 0:0:0:0:0 (?:$|:)/::/x
430 or $ip =~ s/(?:^|:) 0:0:0:0 (?:$|:)/::/x
431 or $ip =~ s/(?:^|:) 0:0:0 (?:$|:)/::/x
432 or $ip =~ s/(?:^|:) 0:0 (?:$|:)/::/x;
433  
434 0 $ip
435 }
436  
437 2 ▶sub format_address($) {
Subroutine format_address: called CC=5 SCAR=28.0
Pod documented
438 2 ▶ if (4 == length $_[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (4 == length $_[0]) { } 2 0 2
elsif (16 == length $_[0]) { } 0 0 0
elsif (1 == &address_family($_[0])) { } 0 0 0
439 2 return &format_ipv4;
440 } elsif (16 == length $_[0]) {
441 0 ▶ return $_[0] =~ /^\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff(....)$/s
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff(....)$/s ? : 0 0 0
442 ? format_ipv4 $1
443 : &format_ipv6;
444 } elsif (AF_UNIX == address_family $_[0]) {
445 0 return "unix/"
446 } else {
447 return undef
448 0 }
449 }
450  
451 5 *ntoa = \&format_address;
452  
453 =item inet_aton $name_or_address, $cb->(@addresses)
454  
455 Works similarly to its Socket counterpart, except that it uses a
456 callback. Use the length to distinguish between ipv4 and ipv6 (4 octets
457 for IPv4, 16 for IPv6), or use C<format_address> to convert it to a more
458 readable format.
459  
460 Note that C<resolve_sockaddr>, while initially a more complex interface,
461 resolves host addresses, IDNs, service names and SRV records and gives you
462 an ordered list of socket addresses to try and should be preferred over
463 C<inet_aton>.
464  
465 Example.
466  
467 inet_aton "www.google.com", my $cv = AE::cv;
468 say unpack "H*", $_
469 for $cv->recv;
470 # => d155e363
471 # => d155e367 etc.
472  
473 inet_aton "ipv6.google.com", my $cv = AE::cv;
474 say unpack "H*", $_
475 for $cv->recv;
476 # => 20014860a00300000000000000000068
477  
478 =cut
479  
480 0 ▶sub inet_aton {
Subroutine inet_aton: not called CC=7 SCAR=40.3
Pod documented
481 0 my ($name, $cb) = @_;
482  
483 0 ▶ if (my $ipn = &parse_ipv4) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $ipn = &parse_ipv4) { } 0 0 0
elsif (my $ipn = &parse_ipv6) { } 0 0 0
elsif ($name eq 'localhost') { } 0 0 0
484 0 $cb->($ipn);
485 } elsif (my $ipn = &parse_ipv6) {
486 0 $cb->($ipn);
487 } elsif ($name eq "localhost") { # rfc2606 et al.
488 0 $cb->(v127.0.0.1, v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1);
489 } else {
490 0 ▶ require AnyEvent::DNS unless $AnyEvent::DNS::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless $AnyEvent::DNS::VERSION 0 0 0
491  
492 0 my $ipv4 = $AnyEvent::PROTOCOL{ipv4};
493 0 my $ipv6 = $AnyEvent::PROTOCOL{ipv6};
494  
495 0 my @res;
496  
497 0 ▶ my $cv = AE::cv {
Subroutine __ANON__: not called CC=1 SCAR=6.9
498 0 $cb->(map @$_, reverse @res);
499 0 };
500  
501 0 $cv->begin;
502  
503 0 ▶ if ($ipv4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipv4) 0 0 0
504 0 $cv->begin;
505 0 ▶ AnyEvent::DNS::a ($name, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
506 0 $res[$ipv4] = [map { parse_ipv4 $_ } @_];
507 0 $cv->end;
508 0 });
509 };
510  
511 0 ▶ if ($ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipv6) 0 0 0
512 0 $cv->begin;
513 0 ▶ AnyEvent::DNS::aaaa ($name, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
514 0 $res[$ipv6] = [map { parse_ipv6 $_ } @_];
515 0 $cv->end;
516 0 });
517 };
518  
519 0 $cv->end;
520 }
521 }
522  
523 5 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=19.2
524 *sockaddr_family = $Socket::VERSION >= 1.75
525 ? \&Socket::sockaddr_family
526 : # for 5.6.x, we need to do something much more horrible
527 (Socket::pack_sockaddr_in 0x5555, "\x55\x55\x55\x55"
528 0 | eval { Socket::pack_sockaddr_un "U" }) =~ /^\x00/
529 0 ▶ ? sub { unpack "xC", $_[0] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
530 0 ▶ : sub { unpack "S" , $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
Branch: 2 branches
BranchTrueFalseTotal
$Socket::VERSION >= 1.75 ? : 5 0 5
Socket::pack_sockaddr_in(21845, 'UUUU' | eval { do { Socket::pack_sockaddr_un('U') } }) =~ /^\x00/ ? : 0 0 0
531 }
532  
533 # check for broken platforms with an extra field in sockaddr structure
534 # kind of a rfc vs. bsd issue, as usual (ok, normally it's a
535 # unix vs. bsd issue, a iso C vs. bsd issue or simply a
536 # correctness vs. bsd issue.)
537 5 ▶my $pack_family = 0x55 == sockaddr_family ("\x55\x55")
Branch: 1 branch
BranchTrueFalseTotal
85 == sockaddr_family('UU') ? : 0 5 5
538 ? "xC" : "S";
539  
540 =item $sa = AnyEvent::Socket::pack_sockaddr $service, $host
541  
542 Pack the given port/host combination into a binary sockaddr
543 structure. Handles both IPv4 and IPv6 host addresses, as well as UNIX
544 domain sockets (C<$host> == C<unix/> and C<$service> == absolute
545 pathname).
546  
547 Example:
548  
549 my $bind = AnyEvent::Socket::pack_sockaddr 43, v195.234.53.120;
550 bind $socket, $bind
551 or die "bind: $!";
552  
553 =cut
554  
555 6 ▶sub pack_sockaddr($$) {
Subroutine pack_sockaddr: called CC=4 SCAR=21.7
Pod documented
556 6 my $af = address_family $_[1];
557  
558 6 ▶ if ($af == AF_INET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($af == 2) { } 6 0 6
elsif ($af == 10) { } 0 0 0
elsif ($af == 1) { } 0 0 0
559 6 Socket::pack_sockaddr_in $_[0], $_[1]
560 } elsif ($af == AF_INET6) {
561 0 pack "$pack_family nL a16 L",
562 AF_INET6,
563 $_[0], # port
564 0, # flowinfo
565 $_[1], # addr
566 0 # scope id
567 } elsif ($af == AF_UNIX) {
568 0 Socket::pack_sockaddr_un $_[0]
569 } else {
570 0 Carp::croak "pack_sockaddr: invalid host";
571 }
572 }
573  
574 =item ($service, $host) = AnyEvent::Socket::unpack_sockaddr $sa
575  
576 Unpack the given binary sockaddr structure (as used by bind, getpeername
577 etc.) into a C<$service, $host> combination.
578  
579 For IPv4 and IPv6, C<$service> is the port number and C<$host> the host
580 address in network format (binary).
581  
582 For UNIX domain sockets, C<$service> is the absolute pathname and C<$host>
583 is a special token that is understood by the other functions in this
584 module (C<format_address> converts it to C<unix/>).
585  
586 =cut
587  
588 # perl contains a bug (imho) where it requires that the kernel always returns
589 # sockaddr_un structures of maximum length (which is not, AFAICS, required
590 # by any standard). try to 0-pad structures for the benefit of those platforms.
591 # unfortunately, the IO::Async author chose to break Socket again in version
592 # 2.011 - it now contains a bogus length check, so we disable the workaround.
593  
594 my $sa_un_zero = $Socket::VERSION >= 2.011
595 ? ""
596 0 ▶ : eval { Socket::pack_sockaddr_un "" };
Branch: 1 branch
BranchTrueFalseTotal
$Socket::VERSION >= 2.011 ? : 5 0 5
597  
598 5 $sa_un_zero ^= $sa_un_zero;
599  
600 6 ▶sub unpack_sockaddr($) {
Subroutine unpack_sockaddr: called CC=4 SCAR=21.7
Pod documented
601 6 my $af = sockaddr_family $_[0];
602  
603 6 ▶ if ($af == AF_INET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($af == 2) { } 6 0 6
elsif ($af == 10) { } 0 0 0
elsif ($af == 1) { } 0 0 0
604 6 Socket::unpack_sockaddr_in $_[0]
605 } elsif ($af == AF_INET6) {
606 0 unpack "x2 n x4 a16", $_[0]
607 } elsif ($af == AF_UNIX) {
608 0 ((Socket::unpack_sockaddr_un $_[0] ^ $sa_un_zero), pack "S", AF_UNIX)
609 } else {
610 0 Carp::croak "unpack_sockaddr: unsupported protocol family $af";
611 }
612 }
613  
614 =item AnyEvent::Socket::resolve_sockaddr $node, $service, $proto, $family, $type, $cb->([$family, $type, $proto, $sockaddr], ...)
615  
616 Tries to resolve the given nodename and service name into protocol families
617 and sockaddr structures usable to connect to this node and service in a
618 protocol-independent way. It works remotely similar to the getaddrinfo
619 posix function.
620  
621 For internet addresses, C<$node> is either an IPv4 or IPv6 address, an
622 internet hostname (DNS domain name or IDN), and C<$service> is either
623 a service name (port name from F</etc/services>) or a numerical port
624 number. If both C<$node> and C<$service> are names, then SRV records
625 will be consulted to find the real service, otherwise they will be
626 used as-is. If you know that the service name is not in your services
627 database, then you can specify the service in the format C<name=port>
628 (e.g. C<http=80>).
629  
630 If a host cannot be found via DNS, then it will be looked up in
631 F</etc/hosts> (or the file specified via C<< $ENV{PERL_ANYEVENT_HOSTS}
632 >>). If they are found, the addresses there will be used. The effect is as
633 if entries from F</etc/hosts> would yield C<A> and C<AAAA> records for the
634 host name unless DNS already had records for them.
635  
636 For UNIX domain sockets, C<$node> must be the string C<unix/> and
637 C<$service> must be the absolute pathname of the socket. In this case,
638 C<$proto> will be ignored.
639  
640 C<$proto> must be a protocol name, currently C<tcp>, C<udp> or
641 C<sctp>. The default is currently C<tcp>, but in the future, this function
642 might try to use other protocols such as C<sctp>, depending on the socket
643 type and any SRV records it might find.
644  
645 C<$family> must be either C<0> (meaning any protocol is OK), C<4> (use
646 only IPv4) or C<6> (use only IPv6). The default is influenced by
647 C<$ENV{PERL_ANYEVENT_PROTOCOLS}>.
648  
649 C<$type> must be C<SOCK_STREAM>, C<SOCK_DGRAM> or C<SOCK_SEQPACKET> (or
650 C<undef> in which case it gets automatically chosen to be C<SOCK_STREAM>
651 unless C<$proto> is C<udp>).
652  
653 The callback will receive zero or more array references that contain
654 C<$family, $type, $proto> for use in C<socket> and a binary
655 C<$sockaddr> for use in C<connect> (or C<bind>).
656  
657 The application should try these in the order given.
658  
659 Example:
660  
661 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... };
662  
663 =cut
664  
665 5 our %HOSTS; # $HOSTS{$nodename}[$ipv6] = [@aliases...]
666 5 our @HOSTS_CHECKING; # callbacks to call when hosts have been loaded
667 5 our $HOSTS_MTIME;
668  
669 1 ▶sub _parse_hosts($) {
Subroutine _parse_hosts: called CC=7 SCAR=22.1
670 1 %HOSTS = ();
671  
672 1 for (split /\n/, $_[0]) {
673 1 s/#.*$//;
674 1 s/^[ \t]+//;
675 1 y/A-Z/a-z/;
676  
677 1 my ($addr, @aliases) = split /[ \t]+/;
678 1 ▶ next unless @aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless @aliases 0 1 1
679  
680 1 ▶ if (my $ip = parse_ipv4 $addr) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $ip = &parse_ipv4($addr)) { } 1 0 1
elsif (my $ip = &parse_ipv6($addr)) { } 0 0 0
681 1 ($ip) = $ip =~ /^(.*)$/s if AnyEvent::TAINT;
682 1 push @{ $HOSTS{$_}[0] }, $ip
683 1 for @aliases;
684 } elsif (my $ip = parse_ipv6 $addr) {
685 0 ($ip) = $ip =~ /^(.*)$/s if AnyEvent::TAINT;
686 0 push @{ $HOSTS{$_}[1] }, $ip
687 0 for @aliases;
688 }
689 }
690 }
691  
692 # helper function - unless dns delivered results, check and parse hosts, then call continuation code
693 4 ▶sub _load_hosts_unless(&$@) {
Subroutine _load_hosts_unless: called CC=4 SCAR=14.0
694 4 my ($cont, $cv, @dns) = @_;
695  
696 4 ▶ if (@dns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dns) { } 0 4 4
697 0 $cv->end;
698 } else {
699 my $etc_hosts = length $ENV{PERL_ANYEVENT_HOSTS} ? $ENV{PERL_ANYEVENT_HOSTS}
700 4 ▶ : AnyEvent::WIN32 ? "$ENV{SystemRoot}/system32/drivers/etc/hosts"
Branch: 1 branch
BranchTrueFalseTotal
length $ENV{'PERL_ANYEVENT_HOSTS'} ? : 4 0 4
701 : "/etc/hosts";
702  
703 4 ▶ push @HOSTS_CHECKING, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
704 4 $cont->();
705 4 $cv->end;
706 4 };
707  
708 4 ▶ unless ($#HOSTS_CHECKING) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#AnyEvent::Socket::HOSTS_CHECKING) 4 0 4
709 # we are not the first, so we actually have to do the work
710 4 require AnyEvent::IO;
711  
712 4 ▶ AnyEvent::IO::aio_stat ($etc_hosts, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
713 4 ▶ if ((stat _)[9] ne $HOSTS_MTIME) {
Branch: 1 branch
BranchTrueFalseTotal
if ((stat _)[9] ne $AnyEvent::Socket::HOSTS_MTIME) { } 1 3 4
714 1 AE::log 8 => "(re)loading $etc_hosts.";
715 1 $HOSTS_MTIME = (stat _)[9];
716 # we might load a newer version of hosts,but that's a harmless race,
717 # as the next call will just load it again.
718 1 ▶ AnyEvent::IO::aio_load ($etc_hosts, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
719 1 _parse_hosts $_[0];
720 1 ▶ (shift @HOSTS_CHECKING)->() while @HOSTS_CHECKING;
Branch: 1 branch
BranchTrueFalseTotal
@AnyEvent::Socket::HOSTS_CHECKING and do { shift(@AnyEvent::Socket::HOSTS_CHECKING)->() } 1 1 2
721 1 });
722 } else {
723 3 ▶ (shift @HOSTS_CHECKING)->() while @HOSTS_CHECKING;
Branch: 1 branch
BranchTrueFalseTotal
@AnyEvent::Socket::HOSTS_CHECKING and do { shift(@AnyEvent::Socket::HOSTS_CHECKING)->() } 3 3 6
724 }
725 4 });
726 }
727 }
728 }
729  
730 2 ▶sub resolve_sockaddr($$$$$$) {
Subroutine resolve_sockaddr: called CC=24 SCAR=47.5
Pod documented
731 2 my ($node, $service, $proto, $family, $type, $cb) = @_;
732  
733 2 ▶ if ($node eq "unix/") {
Branch: 1 branch
BranchTrueFalseTotal
if ($node eq "unix/") 0 2 2
734 0 ▶ return $cb->() if $family || $service !~ /^\//; # no can do
Branch: 1 branch
BranchTrueFalseTotal
if $family or not $service =~ m[^/] 0 0 0
Condition0%0/3
$family or not $service =~ m[^/]
l!l&&r!l&&!r
000
MC/DC0%0/2
$family or not $service =~ m[^/]
$family not $service =~ m[^/]
Truth table0%0/3
A or not(B)
A= $familyB= not $service =~ m[^/]
ABresult
1X1
011
000
735  
736 0 ▶ return $cb->([AF_UNIX, defined $type ? $type : SOCK_STREAM, 0, Socket::pack_sockaddr_un $service]);
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 0 0 0
737 }
738  
739 2 unless (AF_INET6) {
740 $family != 6
741 or return $cb->();
742  
743 $family = 4;
744 }
745  
746 2 ▶ $cb->() if $family == 4 && !$AnyEvent::PROTOCOL{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
if $family == 4 and not $AnyEvent::PROTOCOL{"ipv4"} 0 2 2
Condition33%1/3
$family == 4 and not $AnyEvent::PROTOCOL{"ipv4"}
!ll&&!rl&&r
200
MC/DC0%0/2
$family == 4 and not $AnyEvent::PROTOCOL{"ipv4"}
$family == 4 not $AnyEvent::PROTOCOL{"ipv4"}
Truth table33%1/3
A and not(B)
A= $family == 4B= not $AnyEvent::PROTOCOL{"ipv4"}
ABresult
0X0
100
111
747 2 ▶ $cb->() if $family == 6 && !$AnyEvent::PROTOCOL{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
if $family == 6 and not $AnyEvent::PROTOCOL{"ipv6"} 0 2 2
Condition33%1/3
$family == 6 and not $AnyEvent::PROTOCOL{"ipv6"}
!ll&&!rl&&r
200
MC/DC0%0/2
$family == 6 and not $AnyEvent::PROTOCOL{"ipv6"}
$family == 6 not $AnyEvent::PROTOCOL{"ipv6"}
Truth table33%1/3
A and not(B)
A= $family == 6B= not $AnyEvent::PROTOCOL{"ipv6"}
ABresult
0X0
100
111
748  
749 2 ▶ $family ||= 4 unless $AnyEvent::PROTOCOL{ipv6};
Branch: 1 branch
BranchTrueFalseTotal
unless $AnyEvent::PROTOCOL{"ipv6"} 0 2 2
Condition0%0/2
$family ||= 4
l!l
00
MC/DC0%0/1
$family ||= 4
$family
Truth table0%0/2
A
A= $family
Aresult
11
00
750 2 ▶ $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4};
Branch: 1 branch
BranchTrueFalseTotal
unless $AnyEvent::PROTOCOL{"ipv4"} 0 2 2
Condition0%0/2
$family ||= 6
l!l
00
MC/DC0%0/1
$family ||= 6
$family
Truth table0%0/2
A
A= $family
Aresult
11
00
751  
752 2 ▶ $proto ||= "tcp";
Condition50%1/2
$proto ||= "tcp"
l!l
02
MC/DC0%0/1
$proto ||= "tcp"
$proto
Truth table50%1/2
A
A= $proto
Aresult
11
00
753 2 ▶ $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'udp' ? : 0 2 2
Condition33%1/3
$type ||= $proto eq "udp" ? 2 : 1
l!l&&r!l&&!r
020
MC/DC0%0/2
$type ||= $proto eq "udp" ? 2 : 1
$type $proto eq "udp" ? 2 : 1
Truth table33%1/3
A ||= B
A= $typeB= $proto eq "udp" ? 2 : 1
ABresult
1X1
011
000
754  
755 2 ▶ my $proton = AnyEvent::Socket::getprotobyname $proto
Branch: 1 branch
BranchTrueFalseTotal
my $proton = &AnyEvent::Socket::getprotobyname($proto) or Carp::croak("${proto}: protocol unknown") 2 0 2
756 or Carp::croak "$proto: protocol unknown";
757  
758 2 my $port;
759  
760 2 ▶ if ($service =~ /^(\S+)=(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($service =~ /^(\S+)=(\d+)$/) { } 0 2 2
elsif ($service =~ /^\d+$/) { } 2 0 2
761 0 ($service, $port) = ($1, $2);
762 } elsif ($service =~ /^\d+$/) {
763 2 ($service, $port) = (undef, $service);
764 } else {
765 0 ▶ $port = (getservbyname $service, $proto)[2]
Branch: 1 branch
BranchTrueFalseTotal
$port = ()[2] or Carp::croak("$service/${proto}: service unknown") 0 0 0
766 or Carp::croak "$service/$proto: service unknown";
767 }
768  
769 # resolve a records / provide sockaddr structures
770 2 ▶ my $resolve = sub {
Subroutine __ANON__: called CC=9 SCAR=27.8
771 2 my @target = @_;
772  
773 2 my @res;
774 2 ▶ my $cv = AE::cv {
Subroutine __ANON__: called CC=2 SCAR=14.0
775 $cb->(
776 map $_->[2],
777 sort {
778 0 ▶ $AnyEvent::PROTOCOL{$b->[1]} <=> $AnyEvent::PROTOCOL{$a->[1]}
Condition0%0/3
$AnyEvent::PROTOCOL{$AnyEvent::Socket::b->[1]} <=> $AnyEvent::PROTOCOL{$AnyEvent::Socket::a->[1]} || $AnyEvent::Socket::a->[0] <=> $AnyEvent::Socket::b->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$AnyEvent::PROTOCOL{$AnyEvent::Socket::b->[1]} <=> $AnyEvent::PROTOCOL{$AnyEvent::Socket::a->[1]} || $AnyEvent::Socket::a->[0] <=> $AnyEvent::Socket::b->[0]
$AnyEvent::PROTOCOL{$AnyEvent::Socket::b->[1]} <=> $AnyEvent::PROTOCOL{$AnyEvent::Socket::a->[1]} $AnyEvent::Socket::a->[0] <=> $AnyEvent::Socket::b->[0]
Truth table0%0/3
A || B
A= $AnyEvent::PROTOCOL{$AnyEvent::Socket::b->[1]} <=> $AnyEvent::PROTOCOL{$AnyEvent::Socket::a->[1]}B= $AnyEvent::Socket::a->[0] <=> $AnyEvent::Socket::b->[0]
ABresult
1X1
011
000
779 or $a->[0] <=> $b->[0]
780 }
781 @res
782 )
783 2 };
784  
785 2 $cv->begin;
786 2 for my $idx (0 .. $#target) {
787 2 my ($node, $port) = @{ $target[$idx] };
788  
789 2 ▶ if (my $noden = parse_address $node) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $noden = &parse_address($node)) { } 0 2 2
790 0 my $af = address_family $noden;
791  
792 0 ▶ if ($af == AF_INET && $family != 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 2 and $family != 6) 0 0 0
Condition0%0/3
$af == 2 and $family != 6
!ll&&!rl&&r
000
MC/DC0%0/2
$af == 2 and $family != 6
$af == 2 $family != 6
Truth table0%0/3
A and B
A= $af == 2B= $family != 6
ABresult
0X0
100
111
793 0 push @res, [$idx, "ipv4", [AF_INET, $type, $proton,
794 pack_sockaddr $port, $noden]]
795 }
796  
797 0 ▶ if ($af == AF_INET6 && $family != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 10 and $family != 4) 0 0 0
Condition0%0/3
$af == 10 and $family != 4
!ll&&!rl&&r
000
MC/DC0%0/2
$af == 10 and $family != 4
$af == 10 $family != 4
Truth table0%0/3
A and B
A= $af == 10B= $family != 4
ABresult
0X0
100
111
798 0 push @res, [$idx, "ipv6", [AF_INET6, $type, $proton,
799 pack_sockaddr $port, $noden]]
800 }
801 } else {
802 2 $node =~ y/A-Z/a-z/;
803  
804 # a records
805 2 ▶ if ($family != 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family != 6) 2 0 2
806 2 $cv->begin;
807 2 ▶ AnyEvent::DNS::a $node, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
808 push @res, [$idx, "ipv4", [AF_INET, $type, $proton, pack_sockaddr $port, parse_ipv4 $_]]
809 2 for @_;
810  
811 # dns takes precedence over hosts
812 2 ▶ _load_hosts_unless {
Subroutine __ANON__: called CC=2 SCAR=7.2
813 push @res,
814 map [$idx, "ipv4", [AF_INET, $type, $proton, pack_sockaddr $port, $_]],
815 2 ▶ @{ ($HOSTS{$node} || [])->[0] };
Condition50%1/2
$AnyEvent::Socket::HOSTS{$node} || []
l!l
20
MC/DC0%0/1
$AnyEvent::Socket::HOSTS{$node} || []
$AnyEvent::Socket::HOSTS{$node}
Truth table50%1/2
A
A= $AnyEvent::Socket::HOSTS{$node}
Aresult
11
00
816 2 } $cv, @_;
817 2 };
818 }
819  
820 # aaaa records
821 2 ▶ if ($family != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family != 4) 2 0 2
822 2 $cv->begin;
823 2 ▶ AnyEvent::DNS::aaaa $node, sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
824 push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, pack_sockaddr $port, parse_ipv6 $_]]
825 2 for @_;
826  
827 2 ▶ _load_hosts_unless {
Subroutine __ANON__: called CC=2 SCAR=7.2
828 push @res,
829 map [$idx + 0.5, "ipv6", [AF_INET6, $type, $proton, pack_sockaddr $port, $_]],
830 2 ▶ @{ ($HOSTS{$node} || [])->[1] }
Condition50%1/2
$AnyEvent::Socket::HOSTS{$node} || []
l!l
20
MC/DC0%0/1
$AnyEvent::Socket::HOSTS{$node} || []
$AnyEvent::Socket::HOSTS{$node}
Truth table50%1/2
A
A= $AnyEvent::Socket::HOSTS{$node}
Aresult
11
00
831 2 } $cv, @_;
832 2 };
833 }
834 }
835 }
836 2 $cv->end;
837 2 };
838  
839 2 ▶ $node = AnyEvent::Util::idn_to_ascii $node
Branch: 1 branch
BranchTrueFalseTotal
if $node =~ /[^\x00-\x7f]/ 0 2 2
840 if $node =~ /[^\x00-\x7f]/;
841  
842 # try srv records, if applicable
843 2 ▶ if ($node eq "localhost") {
Branch: 2 branches
BranchTrueFalseTotal
if ($node eq 'localhost') { } 0 2 2
elsif (defined $service and not &parse_address($node)) { } 0 2 2
844 0 $resolve->(["127.0.0.1", $port], ["::1", $port]);
845 ▶ } elsif (defined $service && !parse_address $node) {
Condition33%1/3
defined $service and not &parse_address($node)
!ll&&!rl&&r
200
MC/DC0%0/2
defined $service and not &parse_address($node)
defined $service not &parse_address($node)
Truth table33%1/3
A and not(B)
A= defined $serviceB= not &parse_address($node)
ABresult
0X0
100
111
846 0 ▶ AnyEvent::DNS::srv $service, $proto, $node, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
847 0 my (@srv) = @_;
848  
849 0 ▶ if (@srv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@srv) { } 0 0 0
850 # the only srv record has "." ("" here) => abort
851 0 ▶ $srv[0][2] ne "" || $#srv
Branch: 1 branch
BranchTrueFalseTotal
$srv[0][2] ne "" or $#srv or return &$cb() 0 0 0
Condition0%0/3
$srv[0][2] ne "" or $#srv
l!l&&r!l&&!r
000
MC/DC0%0/2
$srv[0][2] ne "" or $#srv
$srv[0][2] ne "" $#srv
Truth table0%0/3
A or B
A= $srv[0][2] ne ""B= $#srv
ABresult
1X1
011
000
852 or return $cb->();
853  
854 # use srv records then
855 0 $resolve->(
856 map ["$_->[3].", $_->[2]],
857 grep $_->[3] ne ".",
858 @srv
859 );
860 } else {
861 # no srv records, continue traditionally
862 0 $resolve->([$node, $port]);
863 }
864 0 };
865 } else {
866 # most common case
867 2 $resolve->([$node, $port]);
868 }
869 }
870  
871 =item $guard = tcp_connect $host, $service, $connect_cb[, $prepare_cb]
872  
873 This is a convenience function that creates a TCP socket and makes a
874 100% non-blocking connect to the given C<$host> (which can be a DNS/IDN
875 hostname or a textual IP address, or the string C<unix/> for UNIX domain
876 sockets) and C<$service> (which can be a numeric port number or a service
877 name, or a C<servicename=portnumber> string, or the pathname to a UNIX
878 domain socket).
879  
880 If both C<$host> and C<$port> are names, then this function will use SRV
881 records to locate the real target(s).
882  
883 In either case, it will create a list of target hosts (e.g. for multihomed
884 hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to
885 each in turn.
886  
887 After the connection is established, then the C<$connect_cb> will be
888 invoked with the socket file handle (in non-blocking mode) as first, and
889 the peer host (as a textual IP address) and peer port as second and third
890 arguments, respectively. The fourth argument is a code reference that you
891 can call if, for some reason, you don't like this connection, which will
892 cause C<tcp_connect> to try the next one (or call your callback without
893 any arguments if there are no more connections). In most cases, you can
894 simply ignore this argument.
895  
896 $cb->($filehandle, $host, $port, $retry)
897  
898 If the connect is unsuccessful, then the C<$connect_cb> will be invoked
899 without any arguments and C<$!> will be set appropriately (with C<ENXIO>
900 indicating a DNS resolution failure).
901  
902 The callback will I<never> be invoked before C<tcp_connect> returns, even
903 if C<tcp_connect> was able to connect immediately (e.g. on unix domain
904 sockets).
905  
906 The file handle is perfect for being plugged into L<AnyEvent::Handle>, but
907 can be used as a normal perl file handle as well.
908  
909 Unless called in void context, C<tcp_connect> returns a guard object that
910 will automatically cancel the connection attempt when it gets destroyed
911 - in which case the callback will not be invoked. Destroying it does not
912 do anything to the socket after the connect was successful - you cannot
913 "uncall" a callback that has been invoked already.
914  
915 Sometimes you need to "prepare" the socket before connecting, for example,
916 to C<bind> it to some port, or you want a specific connect timeout that
917 is lower than your kernel's default timeout. In this case you can specify
918 a second callback, C<$prepare_cb>. It will be called with the file handle
919 in not-yet-connected state as only argument and must return the connection
920 timeout value (or C<0>, C<undef> or the empty list to indicate the default
921 timeout is to be used).
922  
923 Note to the poor Microsoft Windows users: Windows (of course) doesn't
924 correctly signal connection errors, so unless your event library works
925 around this, failed connections will simply hang. The only event libraries
926 that handle this condition correctly are L<EV> and L<Glib>. Additionally,
927 AnyEvent works around this bug with L<Event> and in its pure-perl
928 backend. All other libraries cannot correctly handle this condition. To
929 lessen the impact of this windows bug, a default timeout of 30 seconds
930 will be imposed on windows. Cygwin is not affected.
931  
932 Simple Example: connect to localhost on port 22.
933  
934 tcp_connect localhost => 22, sub {
935 my $fh = shift
936 or die "unable to connect: $!";
937 # do something
938 };
939  
940 Complex Example: connect to www.google.com on port 80 and make a simple
941 GET request without much error handling. Also limit the connection timeout
942 to 15 seconds.
943  
944 tcp_connect "www.google.com", "http",
945 sub {
946 my ($fh) = @_
947 or die "unable to connect: $!";
948  
949 my $handle; # avoid direct assignment so on_eof has it in scope.
950 $handle = new AnyEvent::Handle
951 fh => $fh,
952 on_error => sub {
953 AE::log error => $_[2];
954 $_[0]->destroy;
955 },
956 on_eof => sub {
957 $handle->destroy; # destroy handle
958 AE::log info => "Done.";
959 };
960  
961 $handle->push_write ("GET / HTTP/1.0\015\012\015\012");
962  
963 $handle->push_read (line => "\015\012\015\012", sub {
964 my ($handle, $line) = @_;
965  
966 # print response header
967 print "HEADER\n$line\n\nBODY\n";
968  
969 $handle->on_read (sub {
970 # print response body
971 print $_[0]->rbuf;
972 $_[0]->rbuf = "";
973 });
974 });
975 }, sub {
976 my ($fh) = @_;
977 # could call $fh->bind etc. here
978  
979 15
980 };
981  
982 Example: connect to a UNIX domain socket.
983  
984 tcp_connect "unix/", "/tmp/.X11-unix/X0", sub {
985 ...
986 }
987  
988 =cut
989  
990 0 ▶sub tcp_connect($$$;$) {
Subroutine tcp_connect: not called CC=2 SCAR=17.9
Pod documented
991 0 my ($host, $port, $connect, $prepare) = @_;
992  
993 # see http://cr.yp.to/docs/connect.html for some tricky aspects
994 # also http://advogato.org/article/672.html
995  
996 0 my %state = ( fh => undef );
997  
998 # name/service to type/sockaddr resolution
999 0 ▶ resolve_sockaddr $host, $port, 0, 0, undef, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1000 0 my @target = @_;
1001  
1002 0 ▶ $state{next} = sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
1003 0 ▶ return unless exists $state{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $state{"fh"} 0 0 0
1004  
1005 0 my $errno = $!;
1006 my $target = shift @target
1007 0 ▶ or return AE::postpone {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1008 0 ▶ return unless exists $state{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $state{"fh"} 0 0 0
1009 0 %state = ();
1010 0 $! = $errno;
1011 0 $connect->();
1012 0 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
my $target = shift @target or return &AE::postpone(sub { return unless exists $state{"fh"}; %state = (); $! = $errno; &$connect(); } ) 0 0 0
1013  
1014 0 my ($domain, $type, $proto, $sockaddr) = @$target;
1015  
1016 # socket creation
1017 socket $state{fh}, $domain, $type, $proto
1018 0 ▶ or return $state{next}();
Branch: 1 branch
BranchTrueFalseTotal
socket $state{"fh"}, $domain, $type, $proto or return $state{"next"}->() 0 0 0
1019  
1020 0 AnyEvent::fh_unblock $state{fh};
1021
1022 0 ▶ my $timeout = $prepare && $prepare->($state{fh});
Condition0%0/3
$prepare && &$prepare($state{'fh'})
!ll&&!rl&&r
000
MC/DC0%0/2
$prepare && &$prepare($state{'fh'})
$prepare &$prepare($state{'fh'})
Truth table0%0/3
A && B
A= $prepareB= &$prepare($state{'fh'})
ABresult
0X0
100
111
1023  
1024 0 $timeout ||= 30 if AnyEvent::WIN32;
1025  
1026 0 ▶ $state{to} = AE::timer $timeout, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1027 0 $! = Errno::ETIMEDOUT;
1028 0 $state{next}();
1029 0 ▶ } if $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout 0 0 0
1030  
1031 # now connect
1032 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (connect $state{'fh'}, $sockaddr or ($! == 115 or $! == 11 or $! == -1e+99 or $! == -1e+99)) { } 0 0 0
Condition0%0/12
connect $state{"fh"}, $sockaddr or ($! == 115 or $! == 11 or $! == "-1e+99" or $! == "-1e+99")
l!l&&r!l&&!r
000
$! == 115 or $! == 11 or $! == "-1e+99" or $! == "-1e+99"
l!l&&r!l&&!r
000
$! == 115 or $! == 11 or $! == "-1e+99"
l!l&&r!l&&!r
000
$! == 115 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/5
connect $state{"fh"}, $sockaddr or ($! == 115 or $! == 11 or $! == "-1e+99" or $! == "-1e+99")
connect $state{"fh"}, $sockaddr $! == 115 $! == 11 $! == "-1e+99" $! == "-1e+99"
Truth table0%0/6
A or B or C or D or E
A= connect $state{"fh"}, $sockaddrB= $! == 115C= $! == 11D= $! == "-1e+99"E= $! == "-1e+99"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
1033 (connect $state{fh}, $sockaddr)
1034 || ($! == Errno::EINPROGRESS # POSIX
1035 || $! == Errno::EWOULDBLOCK
1036 # WSAEINPROGRESS intentionally not checked - it means something else entirely
1037 || $! == AnyEvent::Util::WSAEINVAL # not convinced, but doesn't hurt
1038 || $! == AnyEvent::Util::WSAEWOULDBLOCK)
1039 ) {
1040 0 ▶ $state{ww} = AE::io $state{fh}, 1, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1041 # we are connected, or maybe there was an error
1042 0 ▶ if (my $sin = getpeername $state{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sin = getpeername $state{'fh'}) { } 0 0 0
1043 0 my ($port, $host) = unpack_sockaddr $sin;
1044  
1045 0 delete $state{ww}; delete $state{to};
1046  
1047 0 ▶ my $guard = guard { %state = () };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1048  
1049 0 ▶ $connect->(delete $state{fh}, format_address $host, $port, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1050 0 $guard->cancel;
1051 0 $state{next}();
1052 0 });
1053 } else {
1054 0 ▶ if ($! == Errno::ENOTCONN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 107) 0 0 0
1055 # dummy read to fetch real error code if !cygwin
1056 0 sysread $state{fh}, my $buf, 1;
1057  
1058 # cygwin 1.5 continously reports "ready' but never delivers
1059 # an error with getpeername or sysread.
1060 # cygwin 1.7 only reports readyness *once*, but is otherwise
1061 # the same, which is actually more broken.
1062 # Work around both by using unportable SO_ERROR for cygwin.
1063 0 $! = (unpack "l", getsockopt $state{fh}, Socket::SOL_SOCKET(), Socket::SO_ERROR()) || Errno::EAGAIN
1064 if AnyEvent::CYGWIN && $! == Errno::EAGAIN;
1065 }
1066  
1067 0 ▶ return if $! == Errno::EAGAIN; # skip spurious wake-ups
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 0 0 0
1068  
1069 0 delete $state{ww}; delete $state{to};
1070  
1071 0 $state{next}();
1072 }
1073 0 };
1074 } else {
1075 0 $state{next}();
1076 }
1077 0 };
1078  
1079 0 $! = Errno::ENXIO;
1080 0 $state{next}();
1081 0 };
1082  
1083 0 ▶ defined wantarray && guard { %state = () }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1084 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray and &guard(sub { %state = (); } ) 0 0 0
1085  
1086 =item $guard = tcp_server $host, $service, $accept_cb[, $prepare_cb]
1087  
1088 Create and bind a stream socket to the given host address and port, set
1089 the SO_REUSEADDR flag (if applicable) and call C<listen>. Unlike the name
1090 implies, this function can also bind on UNIX domain sockets.
1091  
1092 For internet sockets, C<$host> must be an IPv4 or IPv6 address (or
1093 C<undef>, in which case it binds either to C<0> or to C<::>, depending
1094 on whether IPv4 or IPv6 is the preferred protocol, and maybe to both in
1095 future versions, as applicable).
1096  
1097 To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6
1098 wildcard address, use C<::>.
1099  
1100 The port is specified by C<$service>, which must be either a service name
1101 or a numeric port number (or C<0> or C<undef>, in which case an ephemeral
1102 port will be used).
1103  
1104 For UNIX domain sockets, C<$host> must be C<unix/> and C<$service> must be
1105 the absolute pathname of the socket. This function will try to C<unlink>
1106 the socket before it tries to bind to it, and will try to unlink it after
1107 it stops using it. See SECURITY CONSIDERATIONS, below.
1108  
1109 For each new connection that could be C<accept>ed, call the C<<
1110 $accept_cb->($fh, $host, $port) >> with the file handle (in non-blocking
1111 mode) as first, and the peer host and port as second and third arguments
1112 (see C<tcp_connect> for details).
1113  
1114 Croaks on any errors it can detect before the listen.
1115  
1116 In non-void context, this function returns a guard object whose lifetime
1117 it tied to the TCP server: If the object gets destroyed, the server will
1118 be stopped and the listening socket will be cleaned up/unlinked (already
1119 accepted connections will not be affected).
1120  
1121 When called in void-context, AnyEvent will keep the listening socket alive
1122 internally. In this case, there is no guarantee that the listening socket
1123 will be cleaned up or unlinked.
1124  
1125 In all cases, when the function returns to the caller, the socket is bound
1126 and in listening state.
1127  
1128 If you need more control over the listening socket, you can provide a
1129 C<< $prepare_cb->($fh, $host, $port) >>, which is called just before the
1130 C<listen ()> call, with the listen file handle as first argument, and IP
1131 address and port number of the local socket endpoint as second and third
1132 arguments.
1133  
1134 It should return the length of the listen queue (or C<0> for the default).
1135  
1136 Note to IPv6 users: RFC-compliant behaviour for IPv6 sockets listening on
1137 C<::> is to bind to both IPv6 and IPv4 addresses by default on dual-stack
1138 hosts. Unfortunately, only GNU/Linux seems to implement this properly, so
1139 if you want both IPv4 and IPv6 listening sockets you should create the
1140 IPv6 socket first and then attempt to bind on the IPv4 socket, but ignore
1141 any C<EADDRINUSE> errors.
1142  
1143 Example: bind on some TCP port on the local machine and tell each client
1144 to go away.
1145  
1146 tcp_server undef, undef, sub {
1147 my ($fh, $host, $port) = @_;
1148  
1149 syswrite $fh, "The internet is full, $host:$port. Go away!\015\012";
1150 }, sub {
1151 my ($fh, $thishost, $thisport) = @_;
1152 AE::log info => "Bound to $thishost, port $thisport.";
1153 };
1154  
1155 Example: bind a server on a unix domain socket.
1156  
1157 tcp_server "unix/", "/tmp/mydir/mysocket", sub {
1158 my ($fh) = @_;
1159 };
1160  
1161 =item $guard = AnyEvent::Socket::tcp_bind $host, $service, $done_cb[, $prepare_cb]
1162  
1163 Same as C<tcp_server>, except it doesn't call C<accept> in a loop for you
1164 but simply passes the listen socket to the C<$done_cb>. This is useful
1165 when you want to have a convenient set up for your listen socket, but want
1166 to do the C<accept>'ing yourself, for example, in another process.
1167  
1168 In case of an error, C<tcp_bind> either croaks, or passes C<undef> to the
1169 C<$done_cb>.
1170  
1171 In non-void context, a guard will be returned. It will clean up/unlink the
1172 listening socket when destroyed. In void context, no automatic clean up
1173 might be performed.
1174  
1175 =cut
1176  
1177 0 ▶sub _tcp_bind($$$;$) {
Subroutine _tcp_bind: not called CC=20 SCAR=60.4
1178 0 my ($host, $service, $done, $prepare) = @_;
1179  
1180 0 ▶ $host = $AnyEvent::PROTOCOL{ipv4} < $AnyEvent::PROTOCOL{ipv6} && AF_INET6
Branch: 2 branches
BranchTrueFalseTotal
unless defined $host 0 0 0
$AnyEvent::PROTOCOL{'ipv4'} < $AnyEvent::PROTOCOL{'ipv6'} && 10 ? : 0 0 0
Condition0%0/2
$AnyEvent::PROTOCOL{"ipv4"} < $AnyEvent::PROTOCOL{"ipv6"} and 10
!ll
00
MC/DC0%0/1
$AnyEvent::PROTOCOL{"ipv4"} < $AnyEvent::PROTOCOL{"ipv6"} and 10
$AnyEvent::PROTOCOL{"ipv4"} < $AnyEvent::PROTOCOL{"ipv6"}
Truth table0%0/2
A
A= $AnyEvent::PROTOCOL{"ipv4"} < $AnyEvent::PROTOCOL{"ipv6"}
Aresult
00
11
1181 ? "::" : "0"
1182 unless defined $host;
1183  
1184 0 ▶ my $ipn = parse_address $host
Branch: 1 branch
BranchTrueFalseTotal
my $ipn = &parse_address($host) or Carp::croak("tcp_bind: cannot parse '${host}' as host address") 0 0 0
1185 or Carp::croak "tcp_bind: cannot parse '$host' as host address";
1186  
1187 0 my $af = address_family $ipn;
1188  
1189 0 my %state;
1190  
1191 # win32 perl is too stupid to get this right :/
1192 0 Carp::croak "tcp_bind: AF_UNIX address family not supported on win32"
1193 if AnyEvent::WIN32 && $af == AF_UNIX;
1194  
1195 0 ▶ socket my $fh, $af, SOCK_STREAM, 0
Branch: 1 branch
BranchTrueFalseTotal
socket my $fh, $af, 1, 0 or Carp::croak("tcp_bind: $!") 0 0 0
1196 or Carp::croak "tcp_bind: $!";
1197  
1198 0 $state{fh} = $fh;
1199  
1200 0 ▶ if ($af == AF_INET || $af == AF_INET6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($af == 2 or $af == 10) { } 0 0 0
elsif ($af == 1) { } 0 0 0
Condition0%0/3
$af == 2 or $af == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$af == 2 or $af == 10
$af == 2 $af == 10
Truth table0%0/3
A or B
A= $af == 2B= $af == 10
ABresult
1X1
011
000
1201 0 ▶ setsockopt $fh, SOL_SOCKET, SO_REUSEADDR, 1
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $fh, 1, 2, 1 or Carp::croak("tcp_bind: so_reuseaddr: $!") 0 0 0
1202 or Carp::croak "tcp_bind: so_reuseaddr: $!"
1203 unless AnyEvent::WIN32; # work around windows bug
1204  
1205 0 ▶ unless ($service =~ /^\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service =~ /^\d*$/) 0 0 0
1206 0 ▶ $service = (getservbyname $service, "tcp")[2]
Branch: 1 branch
BranchTrueFalseTotal
$service = (getservbyname $service, "tcp")[2] or Carp::croak("tcp_bind: unknown service '${service}'") 0 0 0
1207 or Carp::croak "tcp_bind: unknown service '$service'"
1208 }
1209 } elsif ($af == AF_UNIX) {
1210 0 unlink $service;
1211 }
1212  
1213 0 ▶ bind $fh, pack_sockaddr $service, $ipn
Branch: 1 branch
BranchTrueFalseTotal
bind $fh, &pack_sockaddr($service, $ipn) or Carp::croak("tcp_bind: $!") 0 0 0
1214 or Carp::croak "tcp_bind: $!";
1215  
1216 0 ▶ if ($af == AF_UNIX and defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($af == 1 and defined wantarray) 0 0 0
Condition0%0/3
$af == 1 and defined wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
$af == 1 and defined wantarray
$af == 1 defined wantarray
Truth table0%0/3
A and B
A= $af == 1B= defined wantarray
ABresult
0X0
100
111
1217 # this is racy, but is not designed to be foolproof, just best-effort
1218 0 my $ino = (lstat $service)[1];
1219 0 ▶ $state{unlink} = guard {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1220 0 ▶ unlink $service
Branch: 1 branch
BranchTrueFalseTotal
if (lstat $service)[1] == $ino 0 0 0
1221 if (lstat $service)[1] == $ino;
1222 0 };
1223 }
1224  
1225 0 AnyEvent::fh_unblock $fh;
1226  
1227 0 my $len;
1228  
1229 0 ▶ if ($prepare) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prepare) 0 0 0
1230 0 my ($service, $host) = unpack_sockaddr getsockname $fh;
1231 0 ▶ $len = $prepare && $prepare->($fh, format_address $host, $service);
Condition0%0/3
$prepare && &$prepare($fh, &format_address($host), $service)
!ll&&!rl&&r
000
MC/DC0%0/2
$prepare && &$prepare($fh, &format_address($host), $service)
$prepare &$prepare($fh, &format_address($host), $service)
Truth table0%0/3
A && B
A= $prepareB= &$prepare($fh, &format_address($host), $service)
ABresult
0X0
100
111
1232 }
1233
1234 0 ▶ $len ||= 128;
Condition0%0/2
$len ||= 128
l!l
00
MC/DC0%0/1
$len ||= 128
$len
Truth table0%0/2
A
A= $len
Aresult
11
00
1235  
1236 0 ▶ listen $fh, $len
Branch: 1 branch
BranchTrueFalseTotal
listen $fh, $len or Carp::croak("tcp_bind: $!") 0 0 0
1237 or Carp::croak "tcp_bind: $!";
1238  
1239 0 $done->(\%state);
1240  
1241 defined wantarray
1242 0 ▶ ? guard { %state = () } # clear fh, unlink
Subroutine __ANON__: not called CC=1 SCAR=6.9
1243 : ()
1244 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
1245  
1246 0 ▶sub tcp_bind($$$;$) {
Subroutine tcp_bind: not called CC=1 SCAR=6.9
Pod documented
1247 0 my ($host, $service, $done, $prepare) = @_;
1248  
1249 0 ▶ _tcp_bind $host, $service, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1250 0 $done->(delete shift->{fh});
1251 0 }, $prepare
1252 }
1253  
1254 0 ▶sub tcp_server($$$;$) {
Subroutine tcp_server: not called CC=1 SCAR=6.9
Pod documented
1255 0 my ($host, $service, $accept, $prepare) = @_;
1256  
1257 0 ▶ _tcp_bind $host, $service, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1258 0 my $rstate = shift;
1259  
1260 0 ▶ $rstate->{aw} = AE::io $rstate->{fh}, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1261 # this closure keeps $state alive
1262 0 ▶ while ($rstate->{fh} && (my $peer = accept my $fh, $rstate->{fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rstate->{"fh"} and my $peer = accept(my $fh, $rstate->{"fh"})) 0 0 0
Condition0%0/3
$rstate->{"fh"} and my $peer = accept(my $fh, $rstate->{"fh"})
!ll&&!rl&&r
000
MC/DC0%0/2
$rstate->{"fh"} and my $peer = accept(my $fh, $rstate->{"fh"})
$rstate->{"fh"} my $peer = accept(my $fh, $rstate->{"fh"})
Truth table0%0/3
A and B
A= $rstate->{"fh"}B= my $peer = accept(my $fh, $rstate->{"fh"})
ABresult
0X0
100
111
1263 0 AnyEvent::fh_unblock $fh; # POSIX requires inheritance, the outside world does not
1264  
1265 0 my ($service, $host) = unpack_sockaddr $peer;
1266 0 $accept->($fh, format_address $host, $service);
1267 }
1268 0 };
1269 0 }, $prepare
1270 }
1271  
1272 =item tcp_nodelay $fh, $enable
1273  
1274 Enables (or disables) the C<TCP_NODELAY> socket option (also known as
1275 Nagle's algorithm). Returns false on error, true otherwise.
1276  
1277 =cut
1278  
1279 0 ▶sub tcp_nodelay($$) {
Subroutine tcp_nodelay: not called CC=1 SCAR=6.9
Pod documented
1280 0 my $onoff = int ! ! $_[1];
1281  
1282 0 setsockopt $_[0], Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), $onoff
1283 }
1284  
1285 =item tcp_congestion $fh, $algorithm
1286  
1287 Sets the tcp congestion avoidance algorithm (via the C<TCP_CONGESTION>
1288 socket option). The default is OS-specific, but is usually
1289 C<reno>. Typical other available choices include C<cubic>, C<lp>, C<bic>,
1290 C<highspeed>, C<htcp>, C<hybla>, C<illinois>, C<scalable>, C<vegas>,
1291 C<veno>, C<westwood> and C<yeah>.
1292  
1293 =cut
1294  
1295 0 ▶sub tcp_congestion($$) {
Subroutine tcp_congestion: not called CC=1 SCAR=6.9
Pod documented
1296 0 defined TCP_CONGESTION
1297 ? setsockopt $_[0], Socket::IPPROTO_TCP (), TCP_CONGESTION, "$_[1]"
1298 : undef
1299 }
1300  
1301 =back
1302  
1303 =head1 SECURITY CONSIDERATIONS
1304  
1305 This module is quite powerful, with with power comes the ability to abuse
1306 as well: If you accept "hostnames" and ports from untrusted sources,
1307 then note that this can be abused to delete files (host=C<unix/>). This
1308 is not really a problem with this module, however, as blindly accepting
1309 any address and protocol and trying to bind a server or connect to it is
1310 harmful in general.
1311  
1312 =head1 AUTHOR
1313  
1314 Marc Lehmann <schmorp@schmorp.de>
1315 http://anyevent.schmorp.de
1316  
1317 =cut
1318  
1319 5 1
1320  
« blib/lib/AnyEvent/Handle.pm ↑ Summary blib/lib/AnyEvent/Debug.pm »