ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
Revision: 1.54
Committed: Thu Jul 17 15:21:02 2008 UTC (15 years, 11 months ago) by root
Branch: MAIN
Changes since 1.53: +78 -1 lines
Log Message:
*** empty log message ***

File Contents

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