AnyEvent-FastPing
view release on metacpan or search on metacpan
FastPing.pm view on Meta::CPAN
other pinger objects and other generators, but this doesn't help against
malicious replies.
Note that very high packet rates can overwhelm your process, causing
replies to be dropped (configure your kernel with long receive queues for
raw sockets if this is a problem).
Example: register a callback which simply dumps the received data.
use AnyEvent::Socket;
$pinger->on_recv (sub {
for (@{ $_[0] }) {
printf "%s %g\n", (AnyEvent::Socket::format_address $_->[0]), $_->[1];
}
});
Example: a single ping reply with payload of 1 from C<::1> gets passed
like this:
[
[ "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1", 0.000280141830444336 ]
]
Example: ping replies for C<127.0.0.1> and C<127.0.0.2>:
[
[ "\177\0\0\1", 0.00015711784362793 ],
[ "\177\0\0\2", 0.00090184211731 ]
]
=item $pinger->on_idle ($callback->())
Registers a callback to be called when the pinger becomes I<idle>, that
is, it has been started, has exhausted all ping ranges and waited for
the C<max_rtt> time. An idle pinger is also stopped, so the callback can
instantly add new ranges, if it so desires.
=cut
sub on_idle {
$IDLE_CB[ &id ] = $_[1];
}
=item $pinger->interval ($seconds)
Configures the minimum interval between packet sends for this pinger - the
pinger will not send packets faster than this rate (or actually 1 / rate),
even if individual ranges have a lower interval.
A value of C<0> selects the fastest possible speed (currently no faster
than 1_000_000 packets/s).
=item $pinger->max_rtt ($seconds)
If your idle callback were called instantly after all ranges were
exhausted and you destroyed the object inside (which is common), then
there would be no chance to receive some replies, as there would be no
time of the packet to travel over the network.
This can be fixed by starting a timer in the idle callback, or more simply
by selecting a suitable C<max_rtt> value, which should be the maximum time
you allow a ping packet to travel to its destination and back.
The pinger thread automatically waits for this amount of time before becoming idle.
The default is currently C<0.5> seconds, which is usually plenty.
=item $pinger->add_range ($lo, $hi[, $interval])
Ping the IPv4 (or IPv6, but see below) address range, starting at binary
address C<$lo> and ending at C<$hi> (both C<$lo> and C<$hi> will be
pinged), generating no more than one ping per C<$interval> seconds (or as
fast as possible if omitted).
You can convert IP addresses from text to binary form by
using C<AnyEvent::Util::parse_address>, C<Socket::inet_aton>,
C<Socket6::inet_pton> or any other method that you like :)
The algorithm to select the next address is O(log n) on the number of
ranges, so even a large number of ranges (many thousands) is manageable.
No storage is allocated per address.
Note that, while IPv6 addresses are currently supported, the usefulness of
this option is extremely limited and might be gone in future versions - if
you want to ping a number of IPv6 hosts, better specify them individually
using the C<add_hosts> method.
=item $pinger->add_hosts ([$host...], $interval, $interleave)
Similar to C<add_range>, but uses a list of single addresses instead. The
list is specified as an array reference as first argument. Each entry in
the array should be a binary host address, either IPv4 or IPv6. If all
addresses are IPv4 addresses, then a compact IPv4-only format will be used
to store the list internally.
Minimum C<$interval> is the same as for C<add_range> and can be left out.
C<$interlave> specifies an increment between addresses: often address
lists are generated in a way that results in clustering - first all
addresses from one subnet, then from the next, and so on. To avoid this,
you can specify an interleave factor. If it is C<1> (the default), then
every address is pinged in the order specified. If it is C<2>, then only
every second address will be pinged in the first round, followed by a
second round with the others. Higher factors will create C<$interleave>
runs of addresses spaced C<$interleave> indices in the list.
The special value C<0> selects a (hopefully) suitable interleave factor
automatically - currently C<256> for lists with less than 65536 addresses,
and the square root of the list length otherwise.
=item $pinger->start
Start the pinger, unless it is running already. While a pinger is running
you must not modify the pinger. If you want to change a parameter, you
have to C<stop> the pinger first.
The pinger will automatically stop when destroyed.
=item $pinger->stop
( run in 0.689 second using v1.01-cache-2.11-cpan-7e98afdb40f )