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