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

Comparing AnyEvent-FastPing/FastPing.pm (file contents):
Revision 1.1 by root, Sun Apr 27 15:43:51 2008 UTC vs.
Revision 1.9 by root, Sun Jan 16 11:48:15 2011 UTC

6 6
7 use AnyEvent::FastPing; 7 use AnyEvent::FastPing;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module was written for a single purpose only: sending ICMP EHCO 11This module was written for a single purpose only: sending ICMP ECHO
12REQUEST packets as quickly as possible to a large number of hosts 12REQUEST packets as quickly as possible to a large number of hosts
13(thousands to millions). 13(thousands to millions).
14 14
15It employs a sending thread and is fully event-driven (using AnyEvent), so 15It employs a sending thread and is fully event-driven (using AnyEvent), so
16you have to run an event model supported by AnyEvent to use this module. 16you have to run an event model supported by AnyEvent to use this module.
21 21
22=cut 22=cut
23 23
24package AnyEvent::FastPing; 24package AnyEvent::FastPing;
25 25
26use strict; 26use common::sense;
27no warnings;
28 27
29use AnyEvent; 28use AnyEvent;
30 29
31BEGIN { 30BEGIN {
32 our $VERSION = '1.0'; 31 our $VERSION = '1.14';
33 our @ISA = qw(Exporter); 32 our @ISA = qw(Exporter);
34 33
35 require Exporter; 34 require Exporter;
36 #Exporter::export_ok_tags (keys %EXPORT_TAGS); 35 #Exporter::export_ok_tags (keys %EXPORT_TAGS);
37 36
43 42
44our $THR_REQ_FH; open $THR_REQ_FH, ">&=$THR_REQ_FD" or die "FATAL: cannot fdopen"; 43our $THR_REQ_FH; open $THR_REQ_FH, ">&=$THR_REQ_FD" or die "FATAL: cannot fdopen";
45our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen"; 44our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen";
46 45
47our $THR_REQ_W; 46our $THR_REQ_W;
48our $THR_RES_W = AnyEvent->io (fh => $THR_RES_FH, poll => 'r', cb => sub { 47our $THR_RES_W = AE::io $THR_RES_FH, 0, sub {
49 my $sv = _read_res 48 my $sv = _read_res
50 or return; 49 or return;
51 50
52 $sv->(); 51 $sv->();
53}); 52};
54 53
55our $THR_REQ_BUF; 54our $THR_REQ_BUF;
56 55
57sub _send_req($) { 56sub _send_req($) {
58 $THR_REQ_BUF .= $_[0]; 57 $THR_REQ_BUF .= $_[0];
59 58
60 $THR_REQ_W ||= AnyEvent->io (fh => $THR_REQ_FH, poll => 'w', cb => sub { 59 $THR_REQ_W ||= AE::io $THR_REQ_FH, 1, sub {
61 my $len = syswrite $THR_REQ_FH, $THR_REQ_BUF; 60 my $len = syswrite $THR_REQ_FH, $THR_REQ_BUF;
62 substr $THR_REQ_BUF, 0, $len, ""; 61 substr $THR_REQ_BUF, 0, $len, "";
63 62
64 undef $THR_REQ_W unless length $THR_REQ_BUF; 63 undef $THR_REQ_W unless length $THR_REQ_BUF;
65 }); 64 };
66} 65}
67 66
68=item AnyEvent::FastPing::ipv4_supported 67=item AnyEvent::FastPing::ipv4_supported
69 68
70Returns true if IPv4 is supported in this module and on this system. 69Returns true if IPv4 is supported in this module and on this system.
91range C<interval> is the minimum time in seconds between pings to the 90range C<interval> is the minimum time in seconds between pings to the
92given range. If omitted, defaults to C<$send_interval>. 91given range. If omitted, defaults to C<$send_interval>.
93 92
94The C<$send_interval> is the minimum interval between sending any two 93The C<$send_interval> is the minimum interval between sending any two
95packets and is a way to make an overall rate limit. If omitted, pings will 94packets and is a way to make an overall rate limit. If omitted, pings will
96be send as fast as possible. 95be sent as fast as possible.
97 96
98The C<$payload> is a 32 bit unsigned integer given as the ICMP ECHO 97The C<$payload> is a 32 bit unsigned integer given as the ICMP ECHO
99REQUEST ident and sequence numbers (in unspecified order :). 98REQUEST ident and sequence numbers (in unspecified order :).
100 99
101The request will be queued and all requests will be served by a background 100The request will be queued and all requests will be served by a background
145sub icmp_ping($$$&) { 144sub icmp_ping($$$&) {
146 _send_req _req_icmp_ping @_; 145 _send_req _req_icmp_ping @_;
147} 146}
148 147
149our $ICMP4_FH; 148our $ICMP4_FH;
150our $ICMP4_W = (open $ICMP4_FH, "<&=$ICMP4_FD") && AnyEvent->io (fh => $ICMP4_FH, poll => 'r', cb => \&_recv_icmp4); 149our $ICMP4_W = (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4;
151our $ICMP6_FH; 150our $ICMP6_FH;
152our $ICMP6_W = (open $ICMP6_FH, "<&=$ICMP6_FD") && AnyEvent->io (fh => $ICMP6_FH, poll => 'r', cb => \&_recv_icmp6); 151our $ICMP6_W = (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6;
153 152
154=item AnyEvent::FastPing::register_cb \&cb 153=item AnyEvent::FastPing::register_cb \&cb
155 154
156Register a callback that is called for every received ping reply 155Register a callback that is called for every received ping reply
157(regardless of whether a ping is still in process or not and regardless of 156(regardless of whether a ping is still in process or not and regardless of
158whether the reply is actually a reply to a ping sent earlier). 157whether the reply is actually a reply to a ping sent earlier).
159 158
160The code reference gets a single parameter - an arrayref with an 159The code reference gets a single parameter - an arrayref with an
161entry for each received packet (replies are beign batched for greater 160entry for each received packet (replies are being batched for greater
162efficiency). Each packet is represented by an arrayref with three members: 161efficiency). Each packet is represented by an arrayref with three members:
163the source address (an octet string of either 4 (IPv4) or 16 (IPv6) octets 162the source address (an octet string of either 4 (IPv4) or 16 (IPv6) octets
164length), the payload as passed to C<icmp_ping> and the round trip time in 163length), the payload as passed to C<icmp_ping> and the round trip time in
165seconds. 164seconds.
165
166Example: register a callback which simply dumps the received data. Since
167the coderef is created on the fly via sub, it would be hard to unregister
168this callback again :)
169
170 AnyEvent::FastPing::register_cb sub {
171 for (@{$_[0]}) {
172 printf "%s %d %g\n",
173 (4 == length $_->[0] ? inet_ntoa $_->[0] : Socket6::inet_ntop (&AF_INET6, $_->[0])),
174 $_->[2],
175 $_->[1];
176 }
177 };
166 178
167Example: a single ping reply with payload of 1 from C<::1> gets passed 179Example: a single ping reply with payload of 1 from C<::1> gets passed
168like this: 180like this:
169 181
170 [ [ 182 [ [
196 208
197=cut 209=cut
198 210
199our @CB; 211our @CB;
200 212
201sub register_cb(&) { 213sub register_cb($) {
202 push @CB, $_[0]; 214 push @CB, $_[0];
203} 215}
204 216
205sub unregister_cb($) { 217sub unregister_cb($) {
206 @CB = grep $_ != $_[0], @CB; 218 @CB = grep $_ != $_[0], @CB;
210 222
211=back 223=back
212 224
213=head1 AUTHOR 225=head1 AUTHOR
214 226
215 Marc Lehmann <schmorp@schmorp.de> 227 Marc Lehmann <schmorp@schmorp.de>
216 http://home.schmorp.de/ 228 http://home.schmorp.de/
217 229
218=head1 AUTHOR 230=head1 LICENSE
219 231
220 This software is distributed under the GENERAL PUBLIC LICENSE, version 2 232 This software is distributed under the GENERAL PUBLIC LICENSE, version 2
221 or any later version or, at your option, the Artistic License. 233 or any later version or, at your option, the Artistic License.
222 234
223=cut 235=cut
224 236

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines