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