… | |
… | |
56 | inet_aton |
56 | inet_aton |
57 | tcp_server |
57 | tcp_server |
58 | tcp_connect |
58 | tcp_connect |
59 | ); |
59 | ); |
60 | |
60 | |
61 | our $VERSION = '1.0'; |
61 | our $VERSION = 4.1; |
62 | |
62 | |
63 | =item $ipn = parse_ipv4 $dotted_quad |
63 | =item $ipn = parse_ipv4 $dotted_quad |
64 | |
64 | |
65 | Tries to parse the given dotted quad IPv4 address and return it in |
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 |
66 | octet form (or undef when it isn't in a parsable format). Supports all |
… | |
… | |
196 | sub format_address($) { |
196 | sub format_address($) { |
197 | my $af = address_family $_[0]; |
197 | my $af = address_family $_[0]; |
198 | if ($af == AF_INET) { |
198 | if ($af == AF_INET) { |
199 | return join ".", unpack "C4", $_[0] |
199 | return join ".", unpack "C4", $_[0] |
200 | } elsif ($af == AF_INET6) { |
200 | } elsif ($af == AF_INET6) { |
|
|
201 | if (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0 eq $_[0]) { |
|
|
202 | return "::"; |
|
|
203 | } elsif (v0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1 eq $_[0]) { |
|
|
204 | return "::1"; |
201 | if (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr $_[0], 0, 12) { |
205 | } elsif (v0.0.0.0.0.0.0.0.0.0.0.0 eq substr $_[0], 0, 12) { |
202 | # v4compatible |
206 | # v4compatible |
203 | return "::" . format_address substr $_[0], 12; |
207 | return "::" . format_address substr $_[0], 12; |
204 | } elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) { |
208 | } elsif (v0.0.0.0.0.0.0.0.0.0.255.255 eq substr $_[0], 0, 12) { |
205 | # v4mapped |
209 | # v4mapped |
206 | return "::ffff:" . format_address substr $_[0], 12; |
210 | return "::ffff:" . format_address substr $_[0], 12; |
… | |
… | |
208 | # v4translated |
212 | # v4translated |
209 | return "::ffff:0:" . format_address substr $_[0], 12; |
213 | return "::ffff:0:" . format_address substr $_[0], 12; |
210 | } else { |
214 | } else { |
211 | my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0]; |
215 | my $ip = sprintf "%x:%x:%x:%x:%x:%x:%x:%x", unpack "n8", $_[0]; |
212 | |
216 | |
|
|
217 | # this is rather sucky, I admit |
213 | $ip =~ s/^0:(?:0:)*(0$)?/::/ |
218 | $ip =~ s/^0:(?:0:)*(0$)?/::/ |
214 | or $ip =~ s/(:0)+$/::/ |
219 | or $ip =~ s/(:0){7}$/::/ or $ip =~ s/(:0){7}/:/ |
215 | or $ip =~ s/(:0)+/:/; |
220 | or $ip =~ s/(:0){6}$/::/ or $ip =~ s/(:0){6}/:/ |
|
|
221 | or $ip =~ s/(:0){5}$/::/ or $ip =~ s/(:0){5}/:/ |
|
|
222 | or $ip =~ s/(:0){4}$/::/ or $ip =~ s/(:0){4}/:/ |
|
|
223 | or $ip =~ s/(:0){3}$/::/ or $ip =~ s/(:0){3}/:/ |
|
|
224 | or $ip =~ s/(:0){2}$/::/ or $ip =~ s/(:0){2}/:/ |
|
|
225 | or $ip =~ s/(:0){1}$/::/ or $ip =~ s/(:0){1}/:/; |
216 | return $ip |
226 | return $ip |
217 | } |
227 | } |
218 | } elsif ($af == AF_UNIX) { |
228 | } elsif ($af == AF_UNIX) { |
219 | return "unix/" |
229 | return "unix/" |
220 | } else { |
230 | } else { |
… | |
… | |
262 | |
272 | |
263 | # check for broken platforms with extra field in sockaddr structure |
273 | # 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 |
274 | # 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 |
275 | # unix vs. bsd issue, a iso C vs. bsd issue or simply a |
266 | # correctness vs. bsd issue. |
276 | # correctness vs. bsd issue. |
267 | my $pack_family = 0x55 == Socket::sockaddr_family "\x55\x55" |
277 | my $pack_family = (0x55 == Socket::sockaddr_family "\x55\x55") |
268 | ? "xC" : "S"; |
278 | ? "xC" : "S"; |
269 | |
279 | |
270 | =item $sa = AnyEvent::Socket::pack_sockaddr $service, $host |
280 | =item $sa = AnyEvent::Socket::pack_sockaddr $service, $host |
271 | |
281 | |
272 | Pack the given port/host combination into a binary sockaddr |
282 | Pack the given port/host combination into a binary sockaddr |
… | |
… | |
424 | $cv->begin; |
434 | $cv->begin; |
425 | for my $idx (0 .. $#target) { |
435 | for my $idx (0 .. $#target) { |
426 | my ($node, $port) = @{ $target[$idx] }; |
436 | my ($node, $port) = @{ $target[$idx] }; |
427 | |
437 | |
428 | if (my $noden = parse_address $node) { |
438 | if (my $noden = parse_address $node) { |
|
|
439 | my $af = address_family $noden; |
|
|
440 | |
429 | if (4 == length $noden && $family != 6) { |
441 | if ($af == AF_INET && $family != 6) { |
430 | push @res, [$idx, "ipv4", [AF_INET, $type, $proton, |
442 | push @res, [$idx, "ipv4", [AF_INET, $type, $proton, |
431 | pack_sockaddr $port, $noden]] |
443 | pack_sockaddr $port, $noden]] |
432 | } |
444 | } |
433 | |
445 | |
434 | if (16 == length $noden && $family != 4) { |
446 | if ($af == AF_INET6 && $family != 4) { |
435 | push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, |
447 | push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, |
436 | pack_sockaddr $port, $noden]] |
448 | pack_sockaddr $port, $noden]] |
437 | } |
449 | } |
438 | } else { |
450 | } else { |
439 | # ipv4 |
451 | # ipv4 |
440 | if ($family != 6) { |
452 | if ($family != 6) { |
441 | $cv->begin; |
453 | $cv->begin; |
442 | a $node, sub { |
454 | AnyEvent::DNS::a $node, sub { |
443 | push @res, [$idx, "ipv4", [AF_INET, $type, $proton, |
455 | push @res, [$idx, "ipv4", [AF_INET, $type, $proton, |
444 | pack_sockaddr $port, parse_ipv4 $_]] |
456 | pack_sockaddr $port, parse_ipv4 $_]] |
445 | for @_; |
457 | for @_; |
446 | $cv->end; |
458 | $cv->end; |
447 | }; |
459 | }; |
448 | } |
460 | } |
449 | |
461 | |
450 | # ipv6 |
462 | # ipv6 |
451 | if ($family != 4) { |
463 | if ($family != 4) { |
452 | $cv->begin; |
464 | $cv->begin; |
453 | aaaa $node, sub { |
465 | AnyEvent::DNS::aaaa $node, sub { |
454 | push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, |
466 | push @res, [$idx, "ipv6", [AF_INET6, $type, $proton, |
455 | pack_sockaddr $port, parse_ipv6 $_]] |
467 | pack_sockaddr $port, parse_ipv6 $_]] |
456 | for @_; |
468 | for @_; |
457 | $cv->end; |
469 | $cv->end; |
458 | }; |
470 | }; |
… | |
… | |
465 | # try srv records, if applicable |
477 | # try srv records, if applicable |
466 | if ($node eq "localhost") { |
478 | if ($node eq "localhost") { |
467 | @target = (["127.0.0.1", $port], ["::1", $port]); |
479 | @target = (["127.0.0.1", $port], ["::1", $port]); |
468 | &$resolve; |
480 | &$resolve; |
469 | } elsif (defined $service && !parse_address $node) { |
481 | } elsif (defined $service && !parse_address $node) { |
470 | srv $service, $proto, $node, sub { |
482 | AnyEvent::DNS::srv $service, $proto, $node, sub { |
471 | my (@srv) = @_; |
483 | my (@srv) = @_; |
472 | |
484 | |
473 | # no srv records, continue traditionally |
485 | # no srv records, continue traditionally |
474 | @srv |
486 | @srv |
475 | or return &$resolve; |
487 | or return &$resolve; |
476 | |
488 | |
477 | # only srv record has "." => abort |
489 | # the only srv record has "." ("" here) => abort |
478 | $srv[0][2] ne "." || $#srv |
490 | $srv[0][2] ne "" || $#srv |
479 | or return $cb->(); |
491 | or return $cb->(); |
480 | |
492 | |
481 | # use srv records then |
493 | # use srv records then |
482 | @target = map ["$_->[3].", $_->[2]], |
494 | @target = map ["$_->[3].", $_->[2]], |
483 | grep $_->[3] ne ".", |
495 | grep $_->[3] ne ".", |
… | |
… | |
691 | Create and bind a stream socket to the given host, and port, set the |
703 | Create and bind a stream socket to the given host, and port, set the |
692 | SO_REUSEADDR flag (if applicable) and call C<listen>. Unlike the name |
704 | SO_REUSEADDR flag (if applicable) and call C<listen>. Unlike the name |
693 | implies, this function can also bind on UNIX domain sockets. |
705 | implies, this function can also bind on UNIX domain sockets. |
694 | |
706 | |
695 | For internet sockets, C<$host> must be an IPv4 or IPv6 address (or |
707 | For internet sockets, C<$host> must be an IPv4 or IPv6 address (or |
696 | C<undef>, in which case it binds either to C<0> or to C<::>, depending on |
708 | C<undef>, in which case it binds either to C<0> or to C<::>, depending |
697 | whether IPv4 or IPv6 is the preferred protocol). |
709 | on whether IPv4 or IPv6 is the preferred protocol, and maybe to both in |
|
|
710 | future versions, as applicable). |
698 | |
711 | |
699 | To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6 |
712 | To bind to the IPv4 wildcard address, use C<0>, to bind to the IPv6 |
700 | wildcard address, use C<::>. |
713 | wildcard address, use C<::>. |
701 | |
714 | |
702 | The port is specified by C<$service>, which must be either a service name or |
715 | The port is specified by C<$service>, which must be either a service name or |
… | |
… | |
726 | address and port number of the local socket endpoint as second and third |
739 | address and port number of the local socket endpoint as second and third |
727 | arguments. |
740 | arguments. |
728 | |
741 | |
729 | It should return the length of the listen queue (or C<0> for the default). |
742 | It should return the length of the listen queue (or C<0> for the default). |
730 | |
743 | |
|
|
744 | Note to IPv6 users: RFC-compliant behaviour for IPv6 sockets listening on |
|
|
745 | C<::> is to bind to both IPv6 and IPv4 addresses by default on dual-stack |
|
|
746 | hosts. Unfortunately, only GNU/Linux seems to implement this properly, so |
|
|
747 | if you want both IPv4 and IPv6 listening sockets you should create the |
|
|
748 | IPv6 socket first and then attempt to bind on the IPv4 socket, but ignore |
|
|
749 | any C<EADDRINUSE> errors. |
|
|
750 | |
731 | Example: bind on some TCP port on the local machine and tell each client |
751 | Example: bind on some TCP port on the local machine and tell each client |
732 | to go away. |
752 | to go away. |
733 | |
753 | |
734 | tcp_server undef, undef, sub { |
754 | tcp_server undef, undef, sub { |
735 | my ($fh, $host, $port) = @_; |
755 | my ($fh, $host, $port) = @_; |
… | |
… | |
754 | |
774 | |
755 | my $af = address_family $ipn; |
775 | my $af = address_family $ipn; |
756 | |
776 | |
757 | my %state; |
777 | my %state; |
758 | |
778 | |
|
|
779 | # win32 perl is too stupid to get this right :/ |
|
|
780 | Carp::croak "tcp_server/socket: address family not supported" |
|
|
781 | if AnyEvent::WIN32 && $af == AF_UNIX; |
|
|
782 | |
759 | socket $state{fh}, $af, SOCK_STREAM, 0 |
783 | socket $state{fh}, $af, SOCK_STREAM, 0 |
760 | or Carp::croak "socket: $!"; |
784 | or Carp::croak "tcp_server/socket: $!"; |
761 | |
785 | |
762 | if ($af == AF_INET || $af == AF_INET6) { |
786 | if ($af == AF_INET || $af == AF_INET6) { |
763 | setsockopt $state{fh}, SOL_SOCKET, SO_REUSEADDR, 1 |
787 | setsockopt $state{fh}, SOL_SOCKET, SO_REUSEADDR, 1 |
764 | or Carp::croak "so_reuseaddr: $!" |
788 | or Carp::croak "tcp_server/so_reuseaddr: $!" |
765 | unless !AnyEvent::WIN32; # work around windows bug |
789 | unless AnyEvent::WIN32; # work around windows bug |
766 | |
790 | |
767 | unless ($service =~ /^\d*$/) { |
791 | unless ($service =~ /^\d*$/) { |
768 | $service = (getservbyname $service, "tcp")[2] |
792 | $service = (getservbyname $service, "tcp")[2] |
769 | or Carp::croak "$service: service unknown" |
793 | or Carp::croak "$service: service unknown" |
770 | } |
794 | } |
… | |
… | |
791 | |
815 | |
792 | $state{aw} = AnyEvent->io (fh => $state{fh}, poll => 'r', cb => sub { |
816 | $state{aw} = AnyEvent->io (fh => $state{fh}, poll => 'r', cb => sub { |
793 | # this closure keeps $state alive |
817 | # this closure keeps $state alive |
794 | while (my $peer = accept my $fh, $state{fh}) { |
818 | while (my $peer = accept my $fh, $state{fh}) { |
795 | fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not |
819 | fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not |
|
|
820 | |
796 | my ($service, $host) = unpack_sockaddr $peer; |
821 | my ($service, $host) = unpack_sockaddr $peer; |
797 | $accept->($fh, format_address $host, $service); |
822 | $accept->($fh, format_address $host, $service); |
798 | } |
823 | } |
799 | }); |
824 | }); |
800 | |
825 | |
… | |
… | |
805 | |
830 | |
806 | 1; |
831 | 1; |
807 | |
832 | |
808 | =back |
833 | =back |
809 | |
834 | |
|
|
835 | =head1 SECURITY CONSIDERATIONS |
|
|
836 | |
|
|
837 | This module is quite powerful, with with power comes the ability to abuse |
|
|
838 | as well: If you accept "hostnames" and ports from untrusted sources, |
|
|
839 | then note that this can be abused to delete files (host=C<unix/>). This |
|
|
840 | is not really a problem with this module, however, as blindly accepting |
|
|
841 | any address and protocol and trying to bind a server or connect to it is |
|
|
842 | harmful in general. |
|
|
843 | |
810 | =head1 AUTHOR |
844 | =head1 AUTHOR |
811 | |
845 | |
812 | Marc Lehmann <schmorp@schmorp.de> |
846 | Marc Lehmann <schmorp@schmorp.de> |
813 | http://home.schmorp.de/ |
847 | http://home.schmorp.de/ |
814 | |
848 | |