ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
Revision: 1.79
Committed: Tue Jun 23 23:37:32 2009 UTC (15 years ago) by root
Branch: MAIN
CVS Tags: rel-4_412
Changes since 1.78: +1 -1 lines
Log Message:
4.412

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