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

File Contents

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