ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Socket.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Socket.pm (file contents):
Revision 1.26 by root, Mon May 26 02:18:41 2008 UTC vs.
Revision 1.39 by root, Thu May 29 00:27:06 2008 UTC

38no warnings; 38no warnings;
39use strict; 39use strict;
40 40
41use Carp (); 41use Carp ();
42use Errno (); 42use Errno ();
43use Socket qw(AF_INET SOCK_STREAM SOCK_DGRAM SOL_SOCKET SO_REUSEADDR); 43use Socket qw(AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM SOL_SOCKET SO_REUSEADDR);
44 44
45use AnyEvent (); 45use AnyEvent ();
46use AnyEvent::Util qw(guard fh_nonblocking AF_INET6); 46use AnyEvent::Util qw(guard fh_nonblocking AF_INET6);
47use AnyEvent::DNS (); 47use AnyEvent::DNS ();
48 48
49use base 'Exporter'; 49use base 'Exporter';
50 50
51our @EXPORT = qw(parse_ipv4 parse_ipv6 parse_ip format_ip inet_aton tcp_server tcp_connect); 51our @EXPORT = qw(
52 parse_ipv4 parse_ipv6
53 parse_ip parse_address
54 format_ip format_address
55 address_family
56 inet_aton
57 tcp_server
58 tcp_connect
59);
52 60
53our $VERSION = '1.0'; 61our $VERSION = '1.0';
54 62
55=item $ipn = parse_ipv4 $dotted_quad 63=item $ipn = parse_ipv4 $dotted_quad
56 64
128 136
129 # and done 137 # and done
130 pack "n*", map hex, @h, @t 138 pack "n*", map hex, @h, @t
131} 139}
132 140
141sub parse_unix($) {
142 $_[0] eq "unix/"
143 ? pack "S", AF_UNIX
144 : undef
145
146}
147
133=item $ipn = parse_ip $text 148=item $ipn = parse_address $text
134 149
135Combines C<parse_ipv4> and C<parse_ipv6> in one function. 150Combines C<parse_ipv4> and C<parse_ipv6> in one function. The address
151here refers to the host address (not socket address) in network form
152(binary).
136 153
137=cut 154If the C<$text> is C<unix/>, then this function returns a special token
155recognised by the other functions in this module to mean "UNIX domain
156socket".
138 157
158=cut
159
139sub parse_ip($) { 160sub parse_address($) {
140 &parse_ipv4 || &parse_ipv6 161 &parse_ipv4 || &parse_ipv6 || &parse_unix
141} 162}
142 163
164*parse_ip =\&parse_address; #d#
165
166=item $sa_family = address_family $ipn
167
168Returns the address family/protocol-family (AF_xxx/PF_xxx, in one value :)
169of the given host address in network format.
170
171=cut
172
173sub address_family($) {
174 4 == length $_[0]
175 ? AF_INET
176 : 16 == length $_[0]
177 ? AF_INET6
178 : unpack "S", $_[0]
179}
180
143=item $text = format_ip $ipn 181=item $text = format_address $ipn
144 182
145Takes either an IPv4 address (4 octets) or and IPv6 address (16 octets) 183Covnvert a host address in network format (e.g. 4 octets for IPv4 or 16
146and converts it into textual form. 184octets for IPv6) and convert it into textual form.
185
186Returns C<unix/> for UNIX domain sockets.
147 187
148This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>, 188This function works similarly to C<inet_ntop AF_INET || AF_INET6, ...>,
149except it automatically detects the address type. 189except it automatically detects the address type.
150 190
151=cut 191Returns C<undef> if it cannot detect the type.
152 192
153sub format_ip; 193=cut
194
195sub format_address;
154sub format_ip($) { 196sub format_address($) {
155 if (4 == length $_[0]) { 197 my $af = address_family $_[0];
198 if ($af == AF_INET) {
156 return join ".", unpack "C4", $_[0] 199 return join ".", unpack "C4", $_[0]
157 } elsif (16 == length $_[0]) { 200 } elsif ($af == AF_INET6) {
201 if (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr $_[0], 0, 12) {
202 # v4compatible
203 return "::" . format_address substr $_[0], 12;
158 if (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) { 204 } elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) {
159 # v4mapped 205 # v4mapped
160 return "::ffff:" . format_ip substr $_[0], 12; 206 return "::ffff:" . format_address substr $_[0], 12;
207 } elsif (v0.0.0.0.0.0.0.0.255.255.0.0 eq substr $_[0], 0, 12) {
208 # v4translated
209 return "::ffff:0:" . format_address substr $_[0], 12;
161 } else { 210 } else {
162 my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0]; 211 my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0];
163 212
164 $ip =~ s/^0:(?:0:)*(0$)?/::/ 213 $ip =~ s/^0:(?:0:)*(0$)?/::/
165 or $ip =~ s/(:0)+$/::/ 214 or $ip =~ s/(:0)+$/::/
166 or $ip =~ s/(:0)+/:/; 215 or $ip =~ s/(:0)+/:/;
167 return $ip 216 return $ip
168 } 217 }
218 } elsif ($af == AF_UNIX) {
219 return "unix/"
169 } else { 220 } else {
170 return undef 221 return undef
171 } 222 }
172} 223}
224
225*format_ip = \&format_address;
173 226
174=item inet_aton $name_or_address, $cb->(@addresses) 227=item inet_aton $name_or_address, $cb->(@addresses)
175 228
176Works similarly to its Socket counterpart, except that it uses a 229Works similarly to its Socket counterpart, except that it uses a
177callback. Also, if a host has only an IPv6 address, this might be passed 230callback. Also, if a host has only an IPv6 address, this might be passed
178to the callback instead (use the length to detect this - 4 for IPv4, 16 231to the callback instead (use the length to detect this - 4 for IPv4, 16
179for IPv6). 232for IPv6).
180 233
181Unlike the L<Socket> function of the same name, you can get multiple IPv4 234Unlike the L<Socket> function of the same name, you can get multiple IPv4
182and IPv6 addresses as result. 235and IPv6 addresses as result (and maybe even other adrdess types).
183 236
184=cut 237=cut
185 238
186sub inet_aton { 239sub inet_aton {
187 my ($name, $cb) = @_; 240 my ($name, $cb) = @_;
205 } 258 }
206 }); 259 });
207 } 260 }
208} 261}
209 262
263# check for broken platforms with extra field in sockaddr structure
264# kind of a rfc vs. bsd issue, as usual (ok, normally it's a
265# unix vs. bsd issue, a iso C vs. bsd issue or simply a
266# correctness vs. bsd issue.
267my $pack_family = (0x55 == Socket::sockaddr_family "\x55\x55")
268 ? "xC" : "S";
269
210=item $sa = AnyEvent::Socket::pack_sockaddr $port, $host 270=item $sa = AnyEvent::Socket::pack_sockaddr $service, $host
211 271
212Pack the given port/host combination into a binary sockaddr structure. Handles 272Pack the given port/host combination into a binary sockaddr
213both IPv4 and IPv6 host addresses. 273structure. Handles both IPv4 and IPv6 host addresses, as well as UNIX
274domain sockets (C<$host> == C<unix/> and C<$service> == absolute
275pathname).
214 276
215=cut 277=cut
216 278
217sub pack_sockaddr($$) { 279sub pack_sockaddr($$) {
218 if (4 == length $_[1]) { 280 my $af = address_family $_[1];
281
282 if ($af == AF_INET) {
219 Socket::pack_sockaddr_in $_[0], $_[1] 283 Socket::pack_sockaddr_in $_[0], $_[1]
220 } elsif (16 == length $_[1]) { 284 } elsif ($af == AF_INET6) {
221 pack "SnL a16 L", 285 pack "$pack_family nL a16 L",
222 AF_INET6, 286 AF_INET6,
223 $_[0], # port 287 $_[0], # port
224 0, # flowinfo 288 0, # flowinfo
225 $_[1], # addr 289 $_[1], # addr
226 0 # scope id 290 0 # scope id
291 } elsif ($af == AF_UNIX) {
292 Socket::pack_sockaddr_un $_[0]
227 } else { 293 } else {
228 Carp::croak "pack_sockaddr: invalid host"; 294 Carp::croak "pack_sockaddr: invalid host";
229 } 295 }
230} 296}
231 297
232=item ($port, $host) = AnyEvent::Socket::unpack_sockaddr $sa 298=item ($service, $host) = AnyEvent::Socket::unpack_sockaddr $sa
233 299
234Unpack the given binary sockaddr structure (as used by bind, getpeername 300Unpack the given binary sockaddr structure (as used by bind, getpeername
235etc.) into a C<$port, $host> combination. 301etc.) into a C<$service, $host> combination.
236 302
237Handles both IPv4 and IPv6 sockaddr structures. 303For IPv4 and IPv6, C<$service> is the port number and C<$host> the host
304address in network format (binary).
305
306For UNIX domain sockets, C<$service> is the absolute pathname and C<$host>
307is a special token that is understood by the other functions in this
308module (C<format_address> converts it to C<unix/>).
238 309
239=cut 310=cut
240 311
241sub unpack_sockaddr($) { 312sub unpack_sockaddr($) {
242 my $af = unpack "S", $_[0]; 313 my $af = Socket::sockaddr_family $_[0];
243 314
244 if ($af == AF_INET) { 315 if ($af == AF_INET) {
245 Socket::unpack_sockaddr_in $_[0] 316 Socket::unpack_sockaddr_in $_[0]
246 } elsif ($af == AF_INET6) { 317 } elsif ($af == AF_INET6) {
247 unpack "x2 n x4 a16", $_[0] 318 unpack "x2 n x4 a16", $_[0]
319 } elsif ($af == AF_UNIX) {
320 ((Socket::unpack_sockaddr_un $_[0]), pack "S", AF_UNIX)
248 } else { 321 } else {
249 Carp::croak "unpack_sockaddr: unsupported protocol family $af"; 322 Carp::croak "unpack_sockaddr: unsupported protocol family $af";
250 } 323 }
251} 324}
252 325
253sub _tcp_port($) { 326=item resolve_sockaddr $node, $service, $proto, $family, $type, $cb->([$family, $type, $proto, $sockaddr], ...)
254 $_[0] =~ /^(\d*)$/ and return $1*1;
255 327
256 (getservbyname $_[0], "tcp")[2] 328Tries to resolve the given nodename and service name into protocol families
329and sockaddr structures usable to connect to this node and service in a
330protocol-independent way. It works remotely similar to the getaddrinfo
331posix function.
332
333For internet addresses, C<$node> is either an IPv4 or IPv6 address or an
334internet hostname, and C<$service> is either a service name (port name
335from F</etc/services>) or a numerical port number. If both C<$node> and
336C<$service> are names, then SRV records will be consulted to find the real
337service, otherwise they will be used as-is. If you know that the service
338name is not in your services database, then you can specify the service in
339the format C<name=port> (e.g. C<http=80>).
340
341For UNIX domain sockets, C<$node> must be the string C<unix/> and
342C<$service> must be the absolute pathname of the socket. In this case,
343C<$proto> will be ignored.
344
345C<$proto> must be a protocol name, currently C<tcp>, C<udp> or
346C<sctp>. The default is currently C<tcp>, but in the future, this function
347might try to use other protocols such as C<sctp>, depending on the socket
348type and any SRV records it might find.
349
350C<$family> must be either C<0> (meaning any protocol is OK), C<4> (use
351only IPv4) or C<6> (use only IPv6). This setting might be influenced by
352C<$ENV{PERL_ANYEVENT_PROTOCOLS}>.
353
354C<$type> must be C<SOCK_STREAM>, C<SOCK_DGRAM> or C<SOCK_SEQPACKET> (or
355C<undef> in which case it gets automatically chosen).
356
357The callback will receive zero or more array references that contain
358C<$family, $type, $proto> for use in C<socket> and a binary
359C<$sockaddr> for use in C<connect> (or C<bind>).
360
361The application should try these in the order given.
362
363Example:
364
365 resolve_sockaddr "google.com", "http", 0, undef, undef, sub { ... };
366
367=cut
368
369sub resolve_sockaddr($$$$$$) {
370 my ($node, $service, $proto, $family, $type, $cb) = @_;
371
372 if ($node eq "unix/") {
373 return $cb->() if $family || !/^\//; # no can do
374
375 return $cb->([AF_UNIX, $type, 0, Socket::pack_sockaddr_un $service]);
376 }
377
378 unless (AF_INET6) {
379 $family != 6
380 or return $cb->();
381
382 $family = 4;
383 }
384
385 $cb->() if $family == 4 && !$AnyEvent::PROTOCOL{ipv4};
386 $cb->() if $family == 6 && !$AnyEvent::PROTOCOL{ipv6};
387
388 $family ||= 4 unless $AnyEvent::PROTOCOL{ipv6};
389 $family ||= 6 unless $AnyEvent::PROTOCOL{ipv4};
390
391 $proto ||= "tcp";
392 $type ||= $proto eq "udp" ? SOCK_DGRAM : SOCK_STREAM;
393
394 my $proton = (getprotobyname $proto)[2]
257 or Carp::croak "$_[0]: service unknown" 395 or Carp::croak "$proto: protocol unknown";
396
397 my $port;
398
399 if ($service =~ /^(\S+)=(\d+)$/) {
400 ($service, $port) = ($1, $2);
401 } elsif ($service =~ /^\d+$/) {
402 ($service, $port) = (undef, $service);
403 } else {
404 $port = (getservbyname $service, $proto)[2]
405 or Carp::croak "$service/$proto: service unknown";
406 }
407
408 my @target = [$node, $port];
409
410 # resolve a records / provide sockaddr structures
411 my $resolve = sub {
412 my @res;
413 my $cv = AnyEvent->condvar (cb => sub {
414 $cb->(
415 map $_->[2],
416 sort {
417 $AnyEvent::PROTOCOL{$b->[1]} <=> $AnyEvent::PROTOCOL{$a->[1]}
418 or $a->[0] <=> $b->[0]
419 }
420 @res
421 )
422 });
423
424 $cv->begin;
425 for my $idx (0 .. $#target) {
426 my ($node, $port) = @{ $target[$idx] };
427
428 if (my $noden = parse_address $node) {
429 if (4 == length $noden && $family != 6) {
430 push @res, [$idx, "ipv4", [AF_INET, $type, $proton,
431 pack_sockaddr $port, $noden]]
432 }
433
434 if (16 == length $noden && $family != 4) {
435 push @res, [$idx, "ipv6", [AF_INET6, $type, $proton,
436 pack_sockaddr $port, $noden]]
437 }
438 } else {
439 # ipv4
440 if ($family != 6) {
441 $cv->begin;
442 AnyEvent::DNS::a $node, sub {
443 push @res, [$idx, "ipv4", [AF_INET, $type, $proton,
444 pack_sockaddr $port, parse_ipv4 $_]]
445 for @_;
446 $cv->end;
447 };
448 }
449
450 # ipv6
451 if ($family != 4) {
452 $cv->begin;
453 AnyEvent::DNS::aaaa $node, sub {
454 push @res, [$idx, "ipv6", [AF_INET6, $type, $proton,
455 pack_sockaddr $port, parse_ipv6 $_]]
456 for @_;
457 $cv->end;
458 };
459 }
460 }
461 }
462 $cv->end;
463 };
464
465 # try srv records, if applicable
466 if ($node eq "localhost") {
467 @target = (["127.0.0.1", $port], ["::1", $port]);
468 &$resolve;
469 } elsif (defined $service && !parse_address $node) {
470 AnyEvent::DNS::srv $service, $proto, $node, sub {
471 my (@srv) = @_;
472
473 # no srv records, continue traditionally
474 @srv
475 or return &$resolve;
476
477 # only srv record has "." => abort
478 $srv[0][2] ne "." || $#srv
479 or return $cb->();
480
481 # use srv records then
482 @target = map ["$_->[3].", $_->[2]],
483 grep $_->[3] ne ".",
484 @srv;
485
486 &$resolve;
487 };
488 } else {
489 &$resolve;
490 }
258} 491}
259 492
260=item $guard = tcp_connect $host, $service, $connect_cb[, $prepare_cb] 493=item $guard = tcp_connect $host, $service, $connect_cb[, $prepare_cb]
261 494
262This is a convenience function that creates a TCP socket and makes a 100% 495This is a convenience function that creates a TCP socket and makes a 100%
263non-blocking connect to the given C<$host> (which can be a hostname or a 496non-blocking connect to the given C<$host> (which can be a hostname or
497a textual IP address, or the string C<unix/> for UNIX domain sockets)
264textual IP address) and C<$service> (which can be a numeric port number or 498and C<$service> (which can be a numeric port number or a service name,
265a service name, or a C<servicename=portnumber> string). 499or a C<servicename=portnumber> string, or the pathname to a UNIX domain
500socket).
266 501
267If both C<$host> and C<$port> are names, then this function will use SRV 502If both C<$host> and C<$port> are names, then this function will use SRV
268records to locate the real target(s). 503records to locate the real target(s).
269 504
270In either case, it will create a list of target hosts (e.g. for multihomed 505In either case, it will create a list of target hosts (e.g. for multihomed
301timeout value (or C<0>, C<undef> or the empty list to indicate the default 536timeout value (or C<0>, C<undef> or the empty list to indicate the default
302timeout is to be used). 537timeout is to be used).
303 538
304Note that the socket could be either a IPv4 TCP socket or an IPv6 TCP 539Note that the socket could be either a IPv4 TCP socket or an IPv6 TCP
305socket (although only IPv4 is currently supported by this module). 540socket (although only IPv4 is currently supported by this module).
541
542Note to the poor Microsoft Windows users: Windows (of course) doesn't
543correctly signal connection errors, so unless your event library works
544around this, failed connections will simply hang. The only event libraries
545that handle this condition correctly are L<EV> and L<Glib>. Additionally,
546AnyEvent works around this bug with L<Event> and in its pure-perl
547backend. All other libraries cannot correctly handle this condition. To
548lessen the impact of this windows bug, a default timeout of 30 seconds
549will be imposed on windows. Cygwin is not affected.
306 550
307Simple Example: connect to localhost on port 22. 551Simple Example: connect to localhost on port 22.
308 552
309 tcp_connect localhost => 22, sub { 553 tcp_connect localhost => 22, sub {
310 my $fh = shift 554 my $fh = shift
348 # could call $fh->bind etc. here 592 # could call $fh->bind etc. here
349 593
350 15 594 15
351 }; 595 };
352 596
597Example: connect to a UNIX domain socket.
598
599 tcp_connect "unix/", "/tmp/.X11-unix/X0", sub {
600 ...
601 }
602
353=cut 603=cut
354 604
355sub tcp_connect($$$;$) { 605sub tcp_connect($$$;$) {
356 my ($host, $port, $connect, $prepare) = @_; 606 my ($host, $port, $connect, $prepare) = @_;
357 607
358 # see http://cr.yp.to/docs/connect.html for some background 608 # see http://cr.yp.to/docs/connect.html for some background
609 # also http://advogato.org/article/672.html
359 610
360 my %state = ( fh => undef ); 611 my %state = ( fh => undef );
361 612
362 # name resolution 613 # name/service to type/sockaddr resolution
363 AnyEvent::DNS::addr $host, $port, 0, 0, 0, sub { 614 resolve_sockaddr $host, $port, 0, 0, 0, sub {
364 my @target = @_; 615 my @target = @_;
365 616
366 $state{next} = sub { 617 $state{next} = sub {
367 return unless exists $state{fh}; 618 return unless exists $state{fh};
368 619
378 socket $state{fh}, $domain, $type, $proto 629 socket $state{fh}, $domain, $type, $proto
379 or return $state{next}(); 630 or return $state{next}();
380 631
381 fh_nonblocking $state{fh}, 1; 632 fh_nonblocking $state{fh}, 1;
382 633
383 # prepare and optional timeout
384 if ($prepare) {
385 my $timeout = $prepare->($state{fh}); 634 my $timeout = $prepare && $prepare->($state{fh});
386 635
636 $timeout ||= 30 if AnyEvent::WIN32;
637
387 $state{to} = AnyEvent->timer (after => $timeout, cb => sub { 638 $state{to} = AnyEvent->timer (after => $timeout, cb => sub {
388 $! = &Errno::ETIMEDOUT; 639 $! = &Errno::ETIMEDOUT;
389 $state{next}(); 640 $state{next}();
390 }) if $timeout; 641 }) if $timeout;
391 }
392 642
393 # called when the connect was successful, which, 643 # called when the connect was successful, which,
394 # in theory, could be the case immediately (but never is in practise) 644 # in theory, could be the case immediately (but never is in practise)
395 my $connected = sub { 645 my $connected = sub {
396 delete $state{ww}; 646 delete $state{ww};
402 652
403 my $guard = guard { 653 my $guard = guard {
404 %state = (); 654 %state = ();
405 }; 655 };
406 656
407 $connect->($state{fh}, format_ip $host, $port, sub { 657 $connect->($state{fh}, format_address $host, $port, sub {
408 $guard->cancel; 658 $guard->cancel;
409 $state{next}(); 659 $state{next}();
410 }); 660 });
411 } else { 661 } else {
412 # dummy read to fetch real error code 662 # dummy read to fetch real error code
416 }; 666 };
417 667
418 # now connect 668 # now connect
419 if (connect $state{fh}, $sockaddr) { 669 if (connect $state{fh}, $sockaddr) {
420 $connected->(); 670 $connected->();
421 } elsif ($! == &Errno::EINPROGRESS || $! == &Errno::EWOULDBLOCK) { # EINPROGRESS is POSIX 671 } elsif ($! == &Errno::EINPROGRESS # POSIX
672 || $! == &Errno::EWOULDBLOCK
673 # WSAEINPROGRESS intentionally not checked - it means something else entirely
674 || $! == AnyEvent::Util::WSAEINVAL # not convinced, but doesn't hurt
675 || $! == AnyEvent::Util::WSAEWOULDBLOCK) {
422 $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $connected); 676 $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $connected);
423 } else { 677 } else {
424 %state = (); 678 $state{next}();
425 $connect->();
426 } 679 }
427 }; 680 };
428 681
429 $! = &Errno::ENXIO; 682 $! = &Errno::ENXIO;
430 $state{next}(); 683 $state{next}();
431 }; 684 };
432 685
433 defined wantarray && guard { %state = () } 686 defined wantarray && guard { %state = () }
434} 687}
435 688
436=item $guard = tcp_server $host, $port, $accept_cb[, $prepare_cb] 689=item $guard = tcp_server $host, $service, $accept_cb[, $prepare_cb]
437 690
438Create and bind a TCP socket to the given host, and port, set the 691Create and bind a stream socket to the given host, and port, set the
439SO_REUSEADDR flag and call C<listen>. 692SO_REUSEADDR flag (if applicable) and call C<listen>. Unlike the name
693implies, this function can also bind on UNIX domain sockets.
440 694
441C<$host> must be an IPv4 or IPv6 address (or C<undef>, in which case it 695For internet sockets, C<$host> must be an IPv4 or IPv6 address (or
442binds either to C<0> or to C<::>, depending on whether IPv4 or IPv6 is the 696C<undef>, in which case it binds either to C<0> or to C<::>, depending
443preferred protocol). 697on whether IPv4 or IPv6 is the preferred protocol, and maybe to both in
698future versions, as applicable).
444 699
445To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6 700To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6
446wildcard address, use C<::>. 701wildcard address, use C<::>.
447 702
448The port is specified by C<$port>, which must be either a service name or 703The port is specified by C<$service>, which must be either a service name or
449a numeric port number (or C<0> or C<undef>, in which case an ephemeral 704a numeric port number (or C<0> or C<undef>, in which case an ephemeral
450port will be used). 705port will be used).
706
707For UNIX domain sockets, C<$host> must be C<unix/> and C<$service> must be
708the absolute pathname of the socket. This function will try to C<unlink>
709the socket before it tries to bind to it. See SECURITY CONSIDERATIONS,
710below.
451 711
452For each new connection that could be C<accept>ed, call the C<< 712For each new connection that could be C<accept>ed, call the C<<
453$accept_cb->($fh, $host, $port) >> with the file handle (in non-blocking 713$accept_cb->($fh, $host, $port) >> with the file handle (in non-blocking
454mode) as first and the peer host and port as second and third arguments 714mode) as first and the peer host and port as second and third arguments
455(see C<tcp_connect> for details). 715(see C<tcp_connect> for details).
467address and port number of the local socket endpoint as second and third 727address and port number of the local socket endpoint as second and third
468arguments. 728arguments.
469 729
470It should return the length of the listen queue (or C<0> for the default). 730It should return the length of the listen queue (or C<0> for the default).
471 731
732Note to IPv6 users: RFC-compliant behaviour for IPv6 sockets listening on
733C<::> is to bind to both IPv6 and IPv4 addresses by default on dual-stack
734hosts. Unfortunately, only GNU/Linux seems to implement this properly, so
735if you want both IPv4 and IPv6 listening sockets you should create the
736IPv6 socket first and then attempt to bind on the IPv4 socket, but ignore
737any C<EADDRINUSE> errors.
738
472Example: bind on some TCP port on the local machine and tell each client 739Example: bind on some TCP port on the local machine and tell each client
473to go away. 740to go away.
474 741
475 tcp_server undef, undef, sub { 742 tcp_server undef, undef, sub {
476 my ($fh, $host, $port) = @_; 743 my ($fh, $host, $port) = @_;
482 }; 749 };
483 750
484=cut 751=cut
485 752
486sub tcp_server($$$;$) { 753sub tcp_server($$$;$) {
487 my ($host, $port, $accept, $prepare) = @_; 754 my ($host, $service, $accept, $prepare) = @_;
488 755
489 $host = $AnyEvent::PROTOCOL{ipv4} < $AnyEvent::PROTOCOL{ipv6} && AF_INET6 756 $host = $AnyEvent::PROTOCOL{ipv4} < $AnyEvent::PROTOCOL{ipv6} && AF_INET6
490 ? "::" : "0" 757 ? "::" : "0"
491 unless defined $host; 758 unless defined $host;
492 759
493 my $ipn = parse_ip $host 760 my $ipn = parse_address $host
494 or Carp::croak "AnyEvent::Socket::tcp_server: cannot parse '$host' as IPv4 or IPv6 address"; 761 or Carp::croak "AnyEvent::Socket::tcp_server: cannot parse '$host' as host address";
495 762
496 my $domain = 4 == length $ipn ? AF_INET : AF_INET6; 763 my $af = address_family $ipn;
497 764
498 my %state; 765 my %state;
499 766
767 # win32 perl is too stupid to get this right :/
768 Carp::croak "tcp_server/socket: address family not supported"
769 if AnyEvent::WIN32 && $af == AF_UNIX;
770
500 socket $state{fh}, $domain, SOCK_STREAM, 0 771 socket $state{fh}, $af, SOCK_STREAM, 0
501 or Carp::croak "socket: $!"; 772 or Carp::croak "tcp_server/socket: $!";
502 773
774 if ($af == AF_INET || $af == AF_INET6) {
503 setsockopt $state{fh}, SOL_SOCKET, SO_REUSEADDR, 1 775 setsockopt $state{fh}, SOL_SOCKET, SO_REUSEADDR, 1
504 or Carp::croak "so_reuseaddr: $!"; 776 or Carp::croak "tcp_server/so_reuseaddr: $!"
777 unless AnyEvent::WIN32; # work around windows bug
505 778
779 unless ($service =~ /^\d*$/) {
780 $service = (getservbyname $service, "tcp")[2]
781 or Carp::croak "$service: service unknown"
782 }
783 } elsif ($af == AF_UNIX) {
784 unlink $service;
785 }
786
506 bind $state{fh}, pack_sockaddr _tcp_port $port, $ipn 787 bind $state{fh}, pack_sockaddr $service, $ipn
507 or Carp::croak "bind: $!"; 788 or Carp::croak "bind: $!";
508 789
509 fh_nonblocking $state{fh}, 1; 790 fh_nonblocking $state{fh}, 1;
510 791
511 my $len; 792 my $len;
512 793
513 if ($prepare) { 794 if ($prepare) {
514 my ($port, $host) = unpack_sockaddr getsockname $state{fh}; 795 my ($service, $host) = unpack_sockaddr getsockname $state{fh};
515 $len = $prepare && $prepare->($state{fh}, format_ip $host, $port); 796 $len = $prepare && $prepare->($state{fh}, format_address $host, $service);
516 } 797 }
517 798
518 $len ||= 128; 799 $len ||= 128;
519 800
520 listen $state{fh}, $len 801 listen $state{fh}, $len
522 803
523 $state{aw} = AnyEvent->io (fh => $state{fh}, poll => 'r', cb => sub { 804 $state{aw} = AnyEvent->io (fh => $state{fh}, poll => 'r', cb => sub {
524 # this closure keeps $state alive 805 # this closure keeps $state alive
525 while (my $peer = accept my $fh, $state{fh}) { 806 while (my $peer = accept my $fh, $state{fh}) {
526 fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not 807 fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not
808
527 my ($port, $host) = unpack_sockaddr $peer; 809 my ($service, $host) = unpack_sockaddr $peer;
528 $accept->($fh, format_ip $host, $port); 810 $accept->($fh, format_address $host, $service);
529 } 811 }
530 }); 812 });
531 813
532 defined wantarray 814 defined wantarray
533 ? guard { %state = () } # clear fh and watcher, which breaks the circular dependency 815 ? guard { %state = () } # clear fh and watcher, which breaks the circular dependency
536 818
5371; 8191;
538 820
539=back 821=back
540 822
823=head1 SECURITY CONSIDERATIONS
824
825This module is quite powerful, with with power comes the ability to abuse
826as well: If you accept "hostnames" and ports from untrusted sources,
827then note that this can be abused to delete files (host=C<unix/>). This
828is not really a problem with this module, however, as blindly accepting
829any address and protocol and trying to bind a server or connect to it is
830harmful in general.
831
541=head1 AUTHOR 832=head1 AUTHOR
542 833
543 Marc Lehmann <schmorp@schmorp.de> 834 Marc Lehmann <schmorp@schmorp.de>
544 http://home.schmorp.de/ 835 http://home.schmorp.de/
545 836

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines