ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
Revision: 1.104
Committed: Sat Aug 1 09:14:54 2009 UTC (14 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-4_9
Changes since 1.103: +1 -1 lines
Log Message:
4.9

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