fh_nonblocking $state{fh}, 1;
my $len;
if ($prepare) {
my ($service, $host) = unpack_sockaddr getsockname $state{fh};
$len = $prepare && $prepare->($state{fh}, format_address $host, $service);
}
$len ||= 128;
listen $state{fh}, $len
or Carp::croak "listen: $!";
$state{aw} = AnyEvent->io (fh => $state{fh}, poll => 'r', cb => sub {
# this closure keeps $state alive
while (my $peer = accept my $fh, $state{fh}) {
fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not
my ($service, $host) = unpack_sockaddr $peer;
$accept->($fh, format_address $host, $service);
}
});
defined wantarray
? guard { %state = () } # clear fh and watcher, which breaks the circular dependency
: ()
}
1;
=back
=head1 SECURITY CONSIDERATIONS
This module is quite powerful, with with power comes the ability to abuse
as well: If you accept "hostnames" and ports from untrusted sources,
then note that this can be abused to delete files (host=C<unix/>). This
is not really a problem with this module, however, as blindly accepting
any address and protocol and trying to bind a server or connect to it is
harmful in general.
=head1 AUTHOR
Marc Lehmann <schmorp@schmorp.de>
http://home.schmorp.de/
=cut
=9=
THE END |