ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
Revision: 1.102
Committed: Thu Jul 30 16:39:19 2009 UTC (14 years, 11 months ago) by root
Branch: MAIN
Changes since 1.101: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 AnyEvent::Socket - useful IPv4 and IPv6 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 use Carp ();
39 use Errno ();
40 use Socket qw(AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM SOL_SOCKET SO_REUSEADDR);
41
42 use AnyEvent (); BEGIN { AnyEvent::common_sense }
43 use AnyEvent::Util qw(guard fh_nonblocking AF_INET6);
44 use AnyEvent::DNS ();
45
46 use base 'Exporter';
47
48 our @EXPORT = qw(
49 getprotobyname
50 parse_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 our $VERSION = 4.881;
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 sub parse_ipv4($) {
73 $_[0] =~ /^ (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* )
74 (?:\. (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) ){0,3}$/x
75 or return undef;
76
77 @_ = map /^0/ ? oct : $_, split /\./, $_[0];
78
79 # check leading parts against range
80 return undef if grep $_ >= 256, @_[0 .. @_ - 2];
81
82 # check trailing part against range
83 return undef if $_[-1] >= 2 ** (8 * (4 - $#_));
84
85 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 =cut
103
104 sub parse_ipv6($) {
105 # quick test to avoid longer processing
106 my $n = $_[0] =~ y/://;
107 return undef if $n < 2 || $n > 8;
108
109 my ($h, $t) = split /::/, $_[0], 2;
110
111 unless (defined $t) {
112 ($h, $t) = (undef, $h);
113 }
114
115 my @h = split /:/, $h;
116 my @t = split /:/, $t;
117
118 # check for ipv4 tail
119 if (@t && $t[-1]=~ /\./) {
120 return undef if $n > 6;
121
122 my $ipn = parse_ipv4 pop @t
123 or return undef;
124
125 push @t, map +(sprintf "%x", $_), unpack "nn", $ipn;
126 }
127
128 # no :: then we need to have exactly 8 components
129 return undef unless @h + @t == 8 || $_[0] =~ /::/;
130
131 # now check all parts for validity
132 return undef if grep !/^[0-9a-fA-F]{1,4}$/, @h, @t;
133
134 # now pad...
135 push @h, 0 while @h + @t < 8;
136
137 # and done
138 pack "n*", map hex, @h, @t
139 }
140
141 sub parse_unix($) {
142 $_[0] eq "unix/"
143 ? pack "S", AF_UNIX
144 : undef
145
146 }
147
148 =item $ipn = parse_address $ip
149
150 Combines C<parse_ipv4> and C<parse_ipv6> in one function. The address
151 here refers to the host address (not socket address) in network form
152 (binary).
153
154 If the C<$text> is C<unix/>, then this function returns a special token
155 recognised by the other functions in this module to mean "UNIX domain
156 socket".
157
158 If the C<$text> to parse is a mapped IPv4 in IPv6 address (:ffff::<ipv4>),
159 then it will be treated as an IPv4 address. If you don't want that, you
160 have to call C<parse_ipv4> and/or C<parse_ipv6> manually.
161
162 =item $ipn = AnyEvent::Socket::aton $ip
163
164 Same as C<parse_address>, but not exported (think C<Socket::inet_aton> but
165 I<without> name resolution).
166
167 =cut
168
169 sub parse_address($) {
170 for (&parse_ipv6) {
171 if ($_) {
172 s/^\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff//;
173 return $_;
174 } else {
175 return &parse_ipv4 || &parse_unix
176 }
177 }
178 }
179
180 *aton = \&parse_address;
181
182 =item ($name, $aliases, $proto) = getprotobyname $name
183
184 Works like the builtin function of the same name, except it tries hard to
185 work even on broken platforms (well, that's windows), where getprotobyname
186 is traditionally very unreliable.
187
188 =cut
189
190 # microsoft can't even get getprotobyname working (the etc/protocols file
191 # gets lost fairly often on windows), so we have to hardcode some common
192 # protocol numbers ourselves.
193 our %PROTO_BYNAME;
194
195 $PROTO_BYNAME{tcp} = Socket::IPPROTO_TCP () if defined &Socket::IPPROTO_TCP;
196 $PROTO_BYNAME{udp} = Socket::IPPROTO_UDP () if defined &Socket::IPPROTO_UDP;
197 $PROTO_BYNAME{icmp} = Socket::IPPROTO_ICMP() if defined &Socket::IPPROTO_ICMP;
198
199 sub getprotobyname($) {
200 my $name = lc shift;
201
202 defined (my $proton = $PROTO_BYNAME{$name} || (getprotobyname $name)[2])
203 or return;
204
205 ($name, uc $name, $proton)
206 }
207
208 =item ($host, $service) = parse_hostport $string[, $default_service]
209
210 Splitting a string of the form C<hostname:port> is a common
211 problem. Unfortunately, just splitting on the colon makes it hard to
212 specify IPv6 addresses and doesn't support the less common but well
213 standardised C<[ip literal]> syntax.
214
215 This function tries to do this job in a better way, it supports the
216 following formats, where C<port> can be a numerical port number of a
217 service name, or a C<name=port> string, and the C< port> and C<:port>
218 parts are optional. Also, everywhere where an IP address is supported
219 a hostname or unix domain socket address is also supported (see
220 C<parse_unix>).
221
222 hostname:port e.g. "www.linux.org", "www.x.de:443", "www.x.de:https=443"
223 ipv4:port e.g. "198.182.196.56", "127.1:22"
224 ipv6 e.g. "::1", "affe::1"
225 [ipv4or6]:port e.g. "[::1]", "[10.0.1]:80"
226 [ipv4or6] port e.g. "[127.0.0.1]", "[www.x.org] 17"
227 ipv4or6 port e.g. "::1 443", "10.0.0.1 smtp"
228
229 It also supports defaulting the service name in a simple way by using
230 C<$default_service> if no service was detected. If neither a service was
231 detected nor a default was specified, then this function returns the
232 empty list. The same happens when a parse error was detected, such as a
233 hostname with a colon in it (the function is rather conservative, though).
234
235 Example:
236
237 print join ",", parse_hostport "localhost:443";
238 # => "localhost,443"
239
240 print join ",", parse_hostport "localhost", "https";
241 # => "localhost,https"
242
243 print join ",", parse_hostport "[::1]";
244 # => "," (empty list)
245
246 =cut
247
248 sub parse_hostport($;$) {
249 my ($host, $port);
250
251 for ("$_[0]") { # work on a copy, just in case, and also reset pos
252
253 # parse host, special cases: "ipv6" or "ipv6 port"
254 unless (
255 ($host) = /^\s* ([0-9a-fA-F:]*:[0-9a-fA-F:]*:[0-9a-fA-F\.:]*)/xgc
256 and parse_ipv6 $host
257 ) {
258 /^\s*/xgc;
259
260 if (/^ \[ ([^\[\]]+) \]/xgc) {
261 $host = $1;
262 } elsif (/^ ([^\[\]:\ ]+) /xgc) {
263 $host = $1;
264 } else {
265 return;
266 }
267 }
268
269 # parse port
270 if (/\G (?:\s+|:) ([^:[:space:]]+) \s*$/xgc) {
271 $port = $1;
272 } elsif (/\G\s*$/gc && length $_[1]) {
273 $port = $_[1];
274 } else {
275 return;
276 }
277 }
278
279 # hostnames must not contain :'s
280 return if $host =~ /:/ && !parse_ipv6 $host;
281
282 ($host, $port)
283 }
284
285 =item $sa_family = address_family $ipn
286
287 Returns the address family/protocol-family (AF_xxx/PF_xxx, in one value :)
288 of the given host address in network format.
289
290 =cut
291
292 sub address_family($) {
293 4 == length $_[0]
294 ? AF_INET
295 : 16 == length $_[0]
296 ? AF_INET6
297 : unpack "S", $_[0]
298 }
299
300 =item $text = format_ipv4 $ipn
301
302 Expects a four octet string representing a binary IPv4 address and returns
303 its textual format. Rarely used, see C<format_address> for a nicer
304 interface.
305
306 =item $text = format_ipv6 $ipn
307
308 Expects a sixteen octet string representing a binary IPv6 address and
309 returns its textual format. Rarely used, see C<format_address> for a
310 nicer interface.
311
312 =item $text = format_address $ipn
313
314 Covnvert a host address in network format (e.g. 4 octets for IPv4 or 16
315 octets for IPv6) and convert it into textual form.
316
317 Returns C<unix/> for UNIX domain sockets.
318
319 This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>,
320 except it automatically detects the address type.
321
322 Returns C<undef> if it cannot detect the type.
323
324 If the C<$ipn> is a mapped IPv4 in IPv6 address (:ffff::<ipv4>), then just
325 the contained IPv4 address will be returned. If you do not want that, you
326 have to call C<format_ipv6> manually.
327
328 =item $text = AnyEvent::Socket::ntoa $ipn
329
330 Same as format_address, but not exported (think C<inet_ntoa>).
331
332 =cut
333
334 sub format_ipv4($) {
335 join ".", unpack "C4", $_[0]
336 }
337
338 sub format_ipv6($) {
339 if (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 eq $_[0]) {
340 return "::";
341 } elsif (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1 eq $_[0]) {
342 return "::1";
343 } elsif (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr $_[0], 0, 12) {
344 # v4compatible
345 return "::" . format_ipv4 substr $_[0], 12;
346 } elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) {
347 # v4mapped
348 return "::ffff:" . format_ipv4 substr $_[0], 12;
349 } elsif (v0.0.0.0.0.0.0.0.255.255.0.0 eq substr $_[0], 0, 12) {
350 # v4translated
351 return "::ffff:0:" . format_ipv4 substr $_[0], 12;
352 } else {
353 my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0];
354
355 # this is rather sucky, I admit
356 $ip =~ s/^0:(?:0:)*(0$)?/::/
357 or $ip =~ s/(:0){7}$/::/ or $ip =~ s/(:0){7}/:/
358 or $ip =~ s/(:0){6}$/::/ or $ip =~ s/(:0){6}/:/
359 or $ip =~ s/(:0){5}$/::/ or $ip =~ s/(:0){5}/:/
360 or $ip =~ s/(:0){4}$/::/ or $ip =~ s/(:0){4}/:/
361 or $ip =~ s/(:0){3}$/::/ or $ip =~ s/(:0){3}/:/
362 or $ip =~ s/(:0){2}$/::/ or $ip =~ s/(:0){2}/:/
363 or $ip =~ s/(:0){1}$/::/ or $ip =~ s/(:0){1}/:/;
364 return $ip
365 }
366 }
367
368 sub format_address($) {
369 my $af = address_family $_[0];
370 if ($af == AF_INET) {
371 return &format_ipv4;
372 } elsif ($af == AF_INET6) {
373 return (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12)
374 ? format_ipv4 substr $_[0], 12
375 : &format_ipv6;
376 } elsif ($af == AF_UNIX) {
377 return "unix/"
378 } else {
379 return undef
380 }
381 }
382
383 *ntoa = \&format_address;
384
385 =item inet_aton $name_or_address, $cb->(@addresses)
386
387 Works similarly to its Socket counterpart, except that it uses a
388 callback. Also, if a host has only an IPv6 address, this might be passed
389 to the callback instead (use the length to detect this - 4 for IPv4, 16
390 for IPv6).
391
392 Unlike the L<Socket> function of the same name, you can get multiple IPv4
393 and IPv6 addresses as result (and maybe even other adrdess types).
394
395 =cut
396
397 sub inet_aton {
398 my ($name, $cb) = @_;
399
400 if (my $ipn = &parse_ipv4) {
401 $cb->($ipn);
402 } elsif (my $ipn = &parse_ipv6) {
403 $cb->($ipn);
404 } elsif ($name eq "localhost") { # rfc2606 et al.
405 $cb->(v127.0.0.1, v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1);
406 } else {
407 require AnyEvent::DNS;
408
409 # simple, bad suboptimal algorithm
410 AnyEvent::DNS::a ($name, sub {
411 if (@_) {
412 $cb->(map +(parse_ipv4 $_), @_);
413 } else {
414 $cb->();
415 #AnyEvent::DNS::aaaa ($name, $cb); need inet_pton
416 }
417 });
418 }
419 }
420
421 BEGIN {
422 *sockaddr_family = $Socket::VERSION >= 1.75
423 ? \&Socket::sockaddr_family
424 : # for 5.6.x, we need to do something much more horrible
425 (Socket::pack_sockaddr_in 0x5555, "\x55\x55\x55\x55"
426 | eval { Socket::pack_sockaddr_un "U" }) =~ /^\x00/
427 ? sub { unpack "xC", $_[0] }
428 : sub { unpack "S" , $_[0] };
429 }
430
431 # check for broken platforms with extra field in sockaddr structure
432 # kind of a rfc vs. bsd issue, as usual (ok, normally it's a
433 # unix vs. bsd issue, a iso C vs. bsd issue or simply a
434 # correctness vs. bsd issue.)
435 my $pack_family = 0x55 == sockaddr_family ("\x55\x55")
436 ? "xC" : "S";
437
438 =item $sa = AnyEvent::Socket::pack_sockaddr $service, $host
439
440 Pack the given port/host combination into a binary sockaddr
441 structure. Handles both IPv4 and IPv6 host addresses, as well as UNIX
442 domain sockets (C<$host> == C<unix/> and C<$service> == absolute
443 pathname).
444
445 =cut
446
447 sub pack_sockaddr($$) {
448 my $af = address_family $_[1];
449
450 if ($af == AF_INET) {
451 Socket::pack_sockaddr_in $_[0], $_[1]
452 } elsif ($af == AF_INET6) {
453 pack "$pack_family nL a16 L",
454 AF_INET6,
455 $_[0], # port
456 0, # flowinfo
457 $_[1], # addr
458 0 # scope id
459 } elsif ($af == AF_UNIX) {
460 Socket::pack_sockaddr_un $_[0]
461 } else {
462 Carp::croak "pack_sockaddr: invalid host";
463 }
464 }
465
466 =item ($service, $host) = AnyEvent::Socket::unpack_sockaddr $sa
467
468 Unpack the given binary sockaddr structure (as used by bind, getpeername
469 etc.) into a C<$service, $host> combination.
470
471 For IPv4 and IPv6, C<$service> is the port number and C<$host> the host
472 address in network format (binary).
473
474 For UNIX domain sockets, C<$service> is the absolute pathname and C<$host>
475 is a special token that is understood by the other functions in this
476 module (C<format_address> converts it to C<unix/>).
477
478 =cut
479
480 sub unpack_sockaddr($) {
481 my $af = sockaddr_family $_[0];
482
483 if ($af == AF_INET) {
484 Socket::unpack_sockaddr_in $_[0]
485 } elsif ($af == AF_INET6) {
486 unpack "x2 n x4 a16", $_[0]
487 } elsif ($af == AF_UNIX) {
488 ((Socket::unpack_sockaddr_un $_[0]), pack "S", AF_UNIX)
489 } else {
490 Carp::croak "unpack_sockaddr: unsupported protocol family $af";
491 }
492 }
493
494 =item resolve_sockaddr $node, $service, $proto, $family, $type, $cb->([$family, $type, $proto, $sockaddr], ...)
495
496 Tries to resolve the given nodename and service name into protocol families
497 and sockaddr structures usable to connect to this node and service in a
498 protocol-independent way. It works remotely similar to the getaddrinfo
499 posix function.
500
501 For internet addresses, C<$node> is either an IPv4 or IPv6 address or an
502 internet hostname, and C<$service> is either a service name (port name
503 from F</etc/services>) or a numerical port number. If both C<$node> and
504 C<$service> are names, then SRV records will be consulted to find the real
505 service, otherwise they will be used as-is. If you know that the service
506 name is not in your services database, then you can specify the service in
507 the format C<name=port> (e.g. C<http=80>).
508
509 For UNIX domain sockets, C<$node> must be the string C<unix/> and
510 C<$service> must be the absolute pathname of the socket. In this case,
511 C<$proto> will be ignored.
512
513 C<$proto> must be a protocol name, currently C<tcp>, C<udp> or
514 C<sctp>. The default is currently C<tcp>, but in the future, this function
515 might try to use other protocols such as C<sctp>, depending on the socket
516 type and any SRV records it might find.
517
518 C<$family> must be either C<0> (meaning any protocol is OK), C<4> (use
519 only IPv4) or C<6> (use only IPv6). The default is influenced by
520 C<$ENV{PERL_ANYEVENT_PROTOCOLS}>.
521
522 C<$type> must be C<SOCK_STREAM>, C<SOCK_DGRAM> or C<SOCK_SEQPACKET> (or
523 C<undef> in which case it gets automatically chosen to be C<SOCK_STREAM>
524 unless C<$proto> is C<udp>).
525
526 The callback will receive zero or more array references that contain
527 C<$family, $type, $proto> for use in C<socket> and a binary
528 C<$sockaddr> for use in C<connect> (or C<bind>).
529
530 The application should try these in the order given.
531
532 Example:
533
534 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... };
535
536 =cut
537
538 sub resolve_sockaddr($$$$$$) {
539 my ($node, $service, $proto, $family, $type, $cb) = @_;
540
541 if ($node eq "unix/") {
542 return $cb->() if $family || $service !~ /^\//; # no can do
543
544 return $cb->([AF_UNIX, defined $type ? $type : SOCK_STREAM, 0, Socket::pack_sockaddr_un $service]);
545 }
546
547 unless (AF_INET6) {
548 $family != 6
549 or return $cb->();
550
551 $family = 4;
552 }
553
554 $cb->() if $family == 4 && !$AnyEvent::PROTOCOL{ipv4};
555 $cb->() if $family == 6 && !$AnyEvent::PROTOCOL{ipv6};
556
557 $family ||= 4 unless $AnyEvent::PROTOCOL{ipv6};
558 $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4};
559
560 $proto ||= "tcp";
561 $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM;
562
563 my $proton = getprotobyname $proto
564 or Carp::croak "$proto: protocol unknown";
565
566 my $port;
567
568 if ($service =~ /^(\S+)=(\d+)$/) {
569 ($service, $port) = ($1, $2);
570 } elsif ($service =~ /^\d+$/) {
571 ($service, $port) = (undef, $service);
572 } else {
573 $port = (getservbyname $service, $proto)[2]
574 or Carp::croak "$service/$proto: service unknown";
575 }
576
577 my @target = [$node, $port];
578
579 # resolve a records / provide sockaddr structures
580 my $resolve = sub {
581 my @res;
582 my $cv = AnyEvent->condvar (cb => sub {
583 $cb->(
584 map $_->[2],
585 sort {
586 $AnyEvent::PROTOCOL{$b->[1]} <=> $AnyEvent::PROTOCOL{$a->[1]}
587 or $a->[0] <=> $b->[0]
588 }
589 @res
590 )
591 });
592
593 $cv->begin;
594 for my $idx (0 .. $#target) {
595 my ($node, $port) = @{ $target[$idx] };
596
597 if (my $noden = parse_address $node) {
598 my $af = address_family $noden;
599
600 if ($af == AF_INET && $family != 6) {
601 push @res, [$idx, "ipv4", [AF_INET, $type, $proton,
602 pack_sockaddr $port, $noden]]
603 }
604
605 if ($af == AF_INET6 && $family != 4) {
606 push @res, [$idx, "ipv6", [AF_INET6, $type, $proton,
607 pack_sockaddr $port, $noden]]
608 }
609 } else {
610 # ipv4
611 if ($family != 6) {
612 $cv->begin;
613 AnyEvent::DNS::a $node, sub {
614 push @res, [$idx, "ipv4", [AF_INET, $type, $proton,
615 pack_sockaddr $port, parse_ipv4 $_]]
616 for @_;
617 $cv->end;
618 };
619 }
620
621 # ipv6
622 if ($family != 4) {
623 $cv->begin;
624 AnyEvent::DNS::aaaa $node, sub {
625 push @res, [$idx, "ipv6", [AF_INET6, $type, $proton,
626 pack_sockaddr $port, parse_ipv6 $_]]
627 for @_;
628 $cv->end;
629 };
630 }
631 }
632 }
633 $cv->end;
634 };
635
636 # try srv records, if applicable
637 if ($node eq "localhost") {
638 @target = (["127.0.0.1", $port], ["::1", $port]);
639 &$resolve;
640 } elsif (defined $service && !parse_address $node) {
641 AnyEvent::DNS::srv $service, $proto, $node, sub {
642 my (@srv) = @_;
643
644 # no srv records, continue traditionally
645 @srv
646 or return &$resolve;
647
648 # the only srv record has "." ("" here) => abort
649 $srv[0][2] ne "" || $#srv
650 or return $cb->();
651
652 # use srv records then
653 @target = map ["$_->[3].", $_->[2]],
654 grep $_->[3] ne ".",
655 @srv;
656
657 &$resolve;
658 };
659 } else {
660 &$resolve;
661 }
662 }
663
664 =item $guard = tcp_connect $host, $service, $connect_cb[, $prepare_cb]
665
666 This is a convenience function that creates a TCP socket and makes a 100%
667 non-blocking connect to the given C<$host> (which can be a hostname or
668 a textual IP address, or the string C<unix/> for UNIX domain sockets)
669 and C<$service> (which can be a numeric port number or a service name,
670 or a C<servicename=portnumber> string, or the pathname to a UNIX domain
671 socket).
672
673 If both C<$host> and C<$port> are names, then this function will use SRV
674 records to locate the real target(s).
675
676 In either case, it will create a list of target hosts (e.g. for multihomed
677 hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to
678 each in turn.
679
680 If the connect is successful, then the C<$connect_cb> will be invoked with
681 the socket file handle (in non-blocking mode) as first and the peer host
682 (as a textual IP address) and peer port as second and third arguments,
683 respectively. The fourth argument is a code reference that you can call
684 if, for some reason, you don't like this connection, which will cause
685 C<tcp_connect> to try the next one (or call your callback without any
686 arguments if there are no more connections). In most cases, you can simply
687 ignore this argument.
688
689 $cb->($filehandle, $host, $port, $retry)
690
691 If the connect is unsuccessful, then the C<$connect_cb> will be invoked
692 without any arguments and C<$!> will be set appropriately (with C<ENXIO>
693 indicating a DNS resolution failure).
694
695 The file handle is perfect for being plugged into L<AnyEvent::Handle>, but
696 can be used as a normal perl file handle as well.
697
698 Unless called in void context, C<tcp_connect> returns a guard object that
699 will automatically abort connecting when it gets destroyed (it does not do
700 anything to the socket after the connect was successful).
701
702 Sometimes you need to "prepare" the socket before connecting, for example,
703 to C<bind> it to some port, or you want a specific connect timeout that
704 is lower than your kernel's default timeout. In this case you can specify
705 a second callback, C<$prepare_cb>. It will be called with the file handle
706 in not-yet-connected state as only argument and must return the connection
707 timeout value (or C<0>, C<undef> or the empty list to indicate the default
708 timeout is to be used).
709
710 Note that the socket could be either a IPv4 TCP socket or an IPv6 TCP
711 socket (although only IPv4 is currently supported by this module).
712
713 Note to the poor Microsoft Windows users: Windows (of course) doesn't
714 correctly signal connection errors, so unless your event library works
715 around this, failed connections will simply hang. The only event libraries
716 that handle this condition correctly are L<EV> and L<Glib>. Additionally,
717 AnyEvent works around this bug with L<Event> and in its pure-perl
718 backend. All other libraries cannot correctly handle this condition. To
719 lessen the impact of this windows bug, a default timeout of 30 seconds
720 will be imposed on windows. Cygwin is not affected.
721
722 Simple Example: connect to localhost on port 22.
723
724 tcp_connect localhost => 22, sub {
725 my $fh = shift
726 or die "unable to connect: $!";
727 # do something
728 };
729
730 Complex Example: connect to www.google.com on port 80 and make a simple
731 GET request without much error handling. Also limit the connection timeout
732 to 15 seconds.
733
734 tcp_connect "www.google.com", "http",
735 sub {
736 my ($fh) = @_
737 or die "unable to connect: $!";
738
739 my $handle; # avoid direct assignment so on_eof has it in scope.
740 $handle = new AnyEvent::Handle
741 fh => $fh,
742 on_error => sub {
743 warn "error $_[2]\n";
744 $_[0]->destroy;
745 },
746 on_eof => sub {
747 $handle->destroy; # destroy handle
748 warn "done.\n";
749 };
750
751 $handle->push_write ("GET / HTTP/1.0\015\012\015\012");
752
753 $handle->push_read_line ("\015\012\015\012", sub {
754 my ($handle, $line) = @_;
755
756 # print response header
757 print "HEADER\n$line\n\nBODY\n";
758
759 $handle->on_read (sub {
760 # print response body
761 print $_[0]->rbuf;
762 $_[0]->rbuf = "";
763 });
764 });
765 }, sub {
766 my ($fh) = @_;
767 # could call $fh->bind etc. here
768
769 15
770 };
771
772 Example: connect to a UNIX domain socket.
773
774 tcp_connect "unix/", "/tmp/.X11-unix/X0", sub {
775 ...
776 }
777
778 =cut
779
780 sub tcp_connect($$$;$) {
781 my ($host, $port, $connect, $prepare) = @_;
782
783 # see http://cr.yp.to/docs/connect.html for some background
784 # also http://advogato.org/article/672.html
785
786 my %state = ( fh => undef );
787
788 # name/service to type/sockaddr resolution
789 resolve_sockaddr $host, $port, 0, 0, undef, sub {
790 my @target = @_;
791
792 $state{next} = sub {
793 return unless exists $state{fh};
794
795 my $target = shift @target
796 or return (%state = (), $connect->());
797
798 my ($domain, $type, $proto, $sockaddr) = @$target;
799
800 # socket creation
801 socket $state{fh}, $domain, $type, $proto
802 or return $state{next}();
803
804 fh_nonblocking $state{fh}, 1;
805
806 my $timeout = $prepare && $prepare->($state{fh});
807
808 $timeout ||= 30 if AnyEvent::WIN32;
809
810 $state{to} = AnyEvent->timer (after => $timeout, cb => sub {
811 $! = Errno::ETIMEDOUT;
812 $state{next}();
813 }) if $timeout;
814
815 # called when the connect was successful, which,
816 # in theory, could be the case immediately (but never is in practise)
817 $state{connected} = sub {
818 # we are connected, or maybe there was an error
819 if (my $sin = getpeername $state{fh}) {
820 my ($port, $host) = unpack_sockaddr $sin;
821
822 delete $state{ww}; delete $state{to};
823
824 my $guard = guard { %state = () };
825
826 $connect->(delete $state{fh}, format_address $host, $port, sub {
827 $guard->cancel;
828 $state{next}();
829 });
830 } else {
831 # dummy read to fetch real error code
832 sysread $state{fh}, my $buf, 1 if $! == Errno::ENOTCONN;
833
834 return if $! == Errno::EAGAIN; # skip spurious wake-ups
835
836 delete $state{ww}; delete $state{to};
837
838 $state{next}();
839 }
840 };
841
842 # now connect
843 if (connect $state{fh}, $sockaddr) {
844 $state{connected}->();
845 } elsif ($! == Errno::EINPROGRESS # POSIX
846 || $! == Errno::EWOULDBLOCK
847 # WSAEINPROGRESS intentionally not checked - it means something else entirely
848 || $! == AnyEvent::Util::WSAEINVAL # not convinced, but doesn't hurt
849 || $! == AnyEvent::Util::WSAEWOULDBLOCK) {
850 $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $state{connected});
851 } else {
852 $state{next}();
853 }
854 };
855
856 $! = Errno::ENXIO;
857 $state{next}();
858 };
859
860 defined wantarray && guard { %state = () }
861 }
862
863 =item $guard = tcp_server $host, $service, $accept_cb[, $prepare_cb]
864
865 Create and bind a stream socket to the given host, and port, set the
866 SO_REUSEADDR flag (if applicable) and call C<listen>. Unlike the name
867 implies, this function can also bind on UNIX domain sockets.
868
869 For internet sockets, C<$host> must be an IPv4 or IPv6 address (or
870 C<undef>, in which case it binds either to C<0> or to C<::>, depending
871 on whether IPv4 or IPv6 is the preferred protocol, and maybe to both in
872 future versions, as applicable).
873
874 To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6
875 wildcard address, use C<::>.
876
877 The port is specified by C<$service>, which must be either a service name or
878 a numeric port number (or C<0> or C<undef>, in which case an ephemeral
879 port will be used).
880
881 For UNIX domain sockets, C<$host> must be C<unix/> and C<$service> must be
882 the absolute pathname of the socket. This function will try to C<unlink>
883 the socket before it tries to bind to it. See SECURITY CONSIDERATIONS,
884 below.
885
886 For each new connection that could be C<accept>ed, call the C<<
887 $accept_cb->($fh, $host, $port) >> with the file handle (in non-blocking
888 mode) as first and the peer host and port as second and third arguments
889 (see C<tcp_connect> for details).
890
891 Croaks on any errors it can detect before the listen.
892
893 If called in non-void context, then this function returns a guard object
894 whose lifetime it tied to the TCP server: If the object gets destroyed,
895 the server will be stopped (but existing accepted connections will
896 continue).
897
898 If you need more control over the listening socket, you can provide a
899 C<< $prepare_cb->($fh, $host, $port) >>, which is called just before the
900 C<listen ()> call, with the listen file handle as first argument, and IP
901 address and port number of the local socket endpoint as second and third
902 arguments.
903
904 It should return the length of the listen queue (or C<0> for the default).
905
906 Note to IPv6 users: RFC-compliant behaviour for IPv6 sockets listening on
907 C<::> is to bind to both IPv6 and IPv4 addresses by default on dual-stack
908 hosts. Unfortunately, only GNU/Linux seems to implement this properly, so
909 if you want both IPv4 and IPv6 listening sockets you should create the
910 IPv6 socket first and then attempt to bind on the IPv4 socket, but ignore
911 any C<EADDRINUSE> errors.
912
913 Example: bind on some TCP port on the local machine and tell each client
914 to go away.
915
916 tcp_server undef, undef, sub {
917 my ($fh, $host, $port) = @_;
918
919 syswrite $fh, "The internet is full, $host:$port. Go away!\015\012";
920 }, sub {
921 my ($fh, $thishost, $thisport) = @_;
922 warn "bound to $thishost, port $thisport\n";
923 };
924
925 Example: bind a server on a unix domain socket.
926
927 tcp_server "unix/", "/tmp/mydir/mysocket", sub {
928 my ($fh) = @_;
929 };
930
931 =cut
932
933 sub tcp_server($$$;$) {
934 my ($host, $service, $accept, $prepare) = @_;
935
936 $host = $AnyEvent::PROTOCOL{ipv4} < $AnyEvent::PROTOCOL{ipv6} && AF_INET6
937 ? "::" : "0"
938 unless defined $host;
939
940 my $ipn = parse_address $host
941 or Carp::croak "AnyEvent::Socket::tcp_server: cannot parse '$host' as host address";
942
943 my $af = address_family $ipn;
944
945 my %state;
946
947 # win32 perl is too stupid to get this right :/
948 Carp::croak "tcp_server/socket: address family not supported"
949 if AnyEvent::WIN32 && $af == AF_UNIX;
950
951 socket $state{fh}, $af, SOCK_STREAM, 0
952 or Carp::croak "tcp_server/socket: $!";
953
954 if ($af == AF_INET || $af == AF_INET6) {
955 setsockopt $state{fh}, SOL_SOCKET, SO_REUSEADDR, 1
956 or Carp::croak "tcp_server/so_reuseaddr: $!"
957 unless AnyEvent::WIN32; # work around windows bug
958
959 unless ($service =~ /^\d*$/) {
960 $service = (getservbyname $service, "tcp")[2]
961 or Carp::croak "$service: service unknown"
962 }
963 } elsif ($af == AF_UNIX) {
964 unlink $service;
965 }
966
967 bind $state{fh}, pack_sockaddr $service, $ipn
968 or Carp::croak "bind: $!";
969
970 fh_nonblocking $state{fh}, 1;
971
972 my $len;
973
974 if ($prepare) {
975 my ($service, $host) = unpack_sockaddr getsockname $state{fh};
976 $len = $prepare && $prepare->($state{fh}, format_address $host, $service);
977 }
978
979 $len ||= 128;
980
981 listen $state{fh}, $len
982 or Carp::croak "listen: $!";
983
984 $state{aw} = AnyEvent->io (fh => $state{fh}, poll => 'r', cb => sub {
985 # this closure keeps $state alive
986 while (my $peer = accept my $fh, $state{fh}) {
987 fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not
988
989 my ($service, $host) = unpack_sockaddr $peer;
990 $accept->($fh, format_address $host, $service);
991 }
992 });
993
994 defined wantarray
995 ? guard { %state = () } # clear fh and watcher, which breaks the circular dependency
996 : ()
997 }
998
999 1;
1000
1001 =back
1002
1003 =head1 SECURITY CONSIDERATIONS
1004
1005 This module is quite powerful, with with power comes the ability to abuse
1006 as well: If you accept "hostnames" and ports from untrusted sources,
1007 then note that this can be abused to delete files (host=C<unix/>). This
1008 is not really a problem with this module, however, as blindly accepting
1009 any address and protocol and trying to bind a server or connect to it is
1010 harmful in general.
1011
1012 =head1 AUTHOR
1013
1014 Marc Lehmann <schmorp@schmorp.de>
1015 http://home.schmorp.de/
1016
1017 =cut
1018