ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
Revision: 1.111
Committed: Mon Aug 10 13:36:00 2009 UTC (14 years, 10 months ago) by elmex
Branch: MAIN
Changes since 1.110: +1 -1 lines
Log Message:
fixed wrong documentation.

File Contents

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