… | |
… | |
26 | use common::sense; |
26 | use common::sense; |
27 | |
27 | |
28 | use AnyEvent; |
28 | use AnyEvent; |
29 | |
29 | |
30 | BEGIN { |
30 | BEGIN { |
31 | our $VERSION = '1.15'; |
31 | our $VERSION = '2.0'; |
32 | our @ISA = qw(Exporter); |
32 | our @ISA = qw(Exporter); |
33 | |
33 | |
34 | require Exporter; |
34 | require Exporter; |
35 | #Exporter::export_ok_tags (keys %EXPORT_TAGS); |
35 | #Exporter::export_ok_tags (keys %EXPORT_TAGS); |
36 | |
36 | |
37 | require XSLoader; |
37 | require XSLoader; |
38 | XSLoader::load (__PACKAGE__, $VERSION); |
38 | XSLoader::load (__PACKAGE__, $VERSION); |
39 | } |
39 | } |
40 | |
40 | |
41 | our ($THR_REQ_FD, $THR_RES_FD, $ICMP4_FD, $ICMP6_FD); |
41 | our ($THR_RES_FD, $ICMP4_FD, $ICMP6_FD); |
42 | |
42 | |
43 | our $THR_REQ_FH; open $THR_REQ_FH, ">&=$THR_REQ_FD" or die "FATAL: cannot fdopen"; |
|
|
44 | our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen"; |
43 | our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen"; |
45 | |
44 | |
46 | our $THR_REQ_W; |
45 | our $ICMP4_FH; our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; |
|
|
46 | our $ICMP6_FH; our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; |
|
|
47 | |
|
|
48 | =item AnyEvent::FastPing::ipv4_supported |
|
|
49 | |
|
|
50 | Returns true if IPv4 is supported in this module and on this system. |
|
|
51 | |
|
|
52 | =item AnyEvent::FastPing::ipv6_supported |
|
|
53 | |
|
|
54 | Returns true if IPv6 is supported in this module and on this system. |
|
|
55 | |
|
|
56 | =item AnyEvent::FastPing::icmp4_pktsize |
|
|
57 | |
|
|
58 | Returns the number of bytes each IPv4 ping packet has. |
|
|
59 | |
|
|
60 | =item AnyEvent::FastPing::icmp6_pktsize |
|
|
61 | |
|
|
62 | Returns the number of bytes each IPv4 ping packet has. |
|
|
63 | |
|
|
64 | =cut |
|
|
65 | |
|
|
66 | sub new { |
|
|
67 | my ($klass) = @_; |
|
|
68 | |
|
|
69 | _new $klass, (rand 65536), (rand 65536), (rand 65536) |
|
|
70 | } |
|
|
71 | |
|
|
72 | our @IDLE_CB; |
|
|
73 | |
|
|
74 | sub DESTROY { |
|
|
75 | undef $IDLE_CB[ &id ]; |
|
|
76 | &_free; |
|
|
77 | } |
|
|
78 | |
|
|
79 | sub on_idle { |
|
|
80 | $IDLE_CB[ &id ] = $_[1]; |
|
|
81 | } |
|
|
82 | |
47 | our $THR_RES_W = AE::io $THR_RES_FH, 0, sub { |
83 | our $THR_RES_W = AE::io $THR_RES_FH, 0, sub { |
48 | my $sv = _read_res |
84 | sysread $THR_RES_FH, my $buf, 8; |
49 | or return; |
|
|
50 | |
85 | |
51 | $sv->(); |
86 | for my $id (unpack "S*", $buf) { |
|
|
87 | _stop_id $id; |
|
|
88 | ($IDLE_CB[$id] || sub { })->(); |
|
|
89 | } |
52 | }; |
90 | }; |
53 | |
91 | |
54 | our $THR_REQ_BUF; |
92 | for(1..10) { |
55 | |
93 | my $p = new AnyEvent::FastPing;#d# |
56 | sub _send_req($) { |
94 | $p->interval (0); |
57 | $THR_REQ_BUF .= $_[0]; |
95 | $p->max_rtt (0.5); |
58 | |
96 | #$p->add_range (v127.0.0.1, v127.255.255.254, 0); |
59 | $THR_REQ_W ||= AE::io $THR_REQ_FH, 1, sub { |
97 | $p->add_range (v1.0.0.1, v1.255.255.254, 0); |
60 | my $len = syswrite $THR_REQ_FH, $THR_REQ_BUF; |
98 | $p->on_idle (my $cv = AE::cv); |
61 | substr $THR_REQ_BUF, 0, $len, ""; |
99 | my $cnt; |
62 | |
100 | $p->on_recv (sub { |
63 | undef $THR_REQ_W unless length $THR_REQ_BUF; |
101 | $cnt++; |
64 | }; |
102 | }); |
|
|
103 | $p->start; |
|
|
104 | $cv->recv; |
|
|
105 | warn $cnt; |
65 | } |
106 | } |
66 | |
|
|
67 | =item AnyEvent::FastPing::ipv4_supported |
|
|
68 | |
|
|
69 | Returns true if IPv4 is supported in this module and on this system. |
|
|
70 | |
|
|
71 | =item AnyEvent::FastPing::ipv6_supported |
|
|
72 | |
|
|
73 | Returns true if IPv6 is supported in this module and on this system. |
|
|
74 | |
|
|
75 | =item AnyEvent::FastPing::icmp4_pktsize |
|
|
76 | |
|
|
77 | Returns the number of bytes each IPv4 ping packet has. |
|
|
78 | |
|
|
79 | =item AnyEvent::FastPing::icmp6_pktsize |
|
|
80 | |
|
|
81 | Returns the number of bytes each IPv4 ping packet has. |
|
|
82 | |
107 | |
83 | =item AnyEvent::FastPing::icmp_ping [ranges...], $send_interval, $payload, \&callback |
108 | =item AnyEvent::FastPing::icmp_ping [ranges...], $send_interval, $payload, \&callback |
84 | |
109 | |
85 | Ping the given IPv4 address ranges. Each range is an arrayref of the |
110 | Ping the given IPv4 address ranges. Each range is an arrayref of the |
86 | form C<[lo, hi, interval]>, where C<lo> and C<hi> are octet strings with |
111 | form C<[lo, hi, interval]>, where C<lo> and C<hi> are octet strings with |
… | |
… | |
140 | $done->wait; |
165 | $done->wait; |
141 | |
166 | |
142 | =cut |
167 | =cut |
143 | |
168 | |
144 | sub icmp_ping($$$&) { |
169 | sub icmp_ping($$$&) { |
145 | _send_req _req_icmp_ping @_; |
170 | # _send_req _req_icmp_ping @_; |
146 | } |
171 | } |
147 | |
|
|
148 | our $ICMP4_FH; our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; |
|
|
149 | our $ICMP6_FH; our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; |
|
|
150 | |
172 | |
151 | =item AnyEvent::FastPing::register_cb \&cb |
173 | =item AnyEvent::FastPing::register_cb \&cb |
152 | |
174 | |
153 | Register a callback that is called for every received ping reply |
175 | Register a callback that is called for every received ping reply |
154 | (regardless of whether a ping is still in process or not and regardless of |
176 | (regardless of whether a ping is still in process or not and regardless of |