AnyEvent
view release on metacpan or search on metacpan
lib/AnyEvent/Socket.pm view on Meta::CPAN
=head1 NAME
AnyEvent::Socket - useful IPv4 and IPv6 stuff. also unix domain sockets. and stuff.
=head1 SYNOPSIS
use AnyEvent::Socket;
tcp_connect "gameserver.deliantra.net", 13327, sub {
my ($fh) = @_
or die "gameserver.deliantra.net connect failed: $!";
# enjoy your filehandle
};
# a simple tcp server
tcp_server undef, 8888, sub {
my ($fh, $host, $port) = @_;
syswrite $fh, "The internet is full, $host:$port. Go away!\015\012";
};
=head1 DESCRIPTION
This module implements various utility functions for handling internet
protocol addresses and sockets, in an as transparent and simple way as
possible.
All functions documented without C<AnyEvent::Socket::> prefix are exported
by default.
=over 4
=cut
package AnyEvent::Socket;
use Carp ();
use Errno ();
use Socket qw(AF_INET AF_UNIX SOCK_STREAM SOCK_DGRAM SOL_SOCKET SO_REUSEADDR);
use AnyEvent (); BEGIN { AnyEvent::common_sense }
use AnyEvent::Util qw(guard AF_INET6);
use AnyEvent::DNS ();
use base 'Exporter';
our @EXPORT = qw(
getprotobyname
parse_hostport format_hostport
parse_ipv4 parse_ipv6
parse_ip parse_address
format_ipv4 format_ipv6
format_ip format_address
address_family
inet_aton
tcp_server
tcp_connect
);
our $VERSION = $AnyEvent::VERSION;
=item $ipn = parse_ipv4 $dotted_quad
Tries to parse the given dotted quad IPv4 address and return it in
octet form (or undef when it isn't in a parsable format). Supports all
forms specified by POSIX (e.g. C<10.0.0.1>, C<10.1>, C<10.0x020304>,
C<0x12345678> or C<0377.0377.0377.0377>).
=cut
sub parse_ipv4($) {
$_[0] =~ /^ (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* )
(?:\. (?: 0x[0-9a-fA-F]+ | 0[0-7]* | [1-9][0-9]* ) ){0,3}$/x
or return undef;
@_ = map /^0/ ? oct : $_, split /\./, $_[0];
# check leading parts against range
return undef if grep $_ >= 256, @_[0 .. @_ - 2];
# check trailing part against range
return undef if $_[-1] >= 2 ** (8 * (4 - $#_));
pack "N", (pop)
+ ($_[0] << 24)
( run in 1.328 second using v1.01-cache-2.11-cpan-39bf76dae61 )