| 1 | package MojoX::Ping; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = 0.512; | ||
| 7 | use base 'Mojo::Base'; | ||
| 8 | |||
| 9 | use Mojo::IOLoop; | ||
| 10 | use Socket qw/SOCK_RAW/; | ||
| 11 | use Time::HiRes 'time'; | ||
| 12 | use IO::Socket::INET qw/sockaddr_in inet_aton/; | ||
| 13 | use List::Util (); | ||
| 14 | require Carp; | ||
| 15 | |||
| 16 | __PACKAGE__->attr(ioloop => sub { Mojo::IOLoop->singleton }); | ||
| 17 | __PACKAGE__->attr(interval => 0.2); | ||
| 18 | __PACKAGE__->attr(timeout => 5); | ||
| 19 | __PACKAGE__->attr('error'); | ||
| 20 | |||
| 21 | my $ICMP_PING = 'ccnnnA*'; | ||
| 22 | |||
| 23 | my $ICMP_ECHOREPLY = 0; # Echo Reply | ||
| 24 | my $ICMP_DEST_UNREACH = 3; # Destination Unreachable | ||
| 25 | my $ICMP_SOURCE_QUENCH = 4; # Source Quench | ||
| 26 | my $ICMP_REDIRECT = 5; # Redirect (change route) | ||
| 27 | my $ICMP_ECHO = 8; # Echo Request | ||
| 28 | my $ICMP_TIME_EXCEEDED = 11; # Time Exceeded | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $class = shift; | ||
| 32 | |||
| 33 | my $self = $class->SUPER::new(@_); | ||
| 34 | |||
| 35 | # Create RAW socket | ||
| 36 | my $socket = IO::Socket::INET->new( | ||
| 37 | Proto => 'icmp', | ||
| 38 | Type => SOCK_RAW, | ||
| 39 | Blocking => 0 | ||
| 40 | ) or Carp::croak "Unable to create icmp socket : $!"; | ||
| 41 | |||
| 42 | $self->{_socket} = $socket; | ||
| 43 | |||
| 44 | # Create Poll object | ||
| 45 | $self->ioloop->iowatcher->watch( | ||
| 46 | $socket, | ||
| 47 | sub { $self->_on_read }, | ||
| 48 | sub { $self->_send_requests } | ||
| 49 | ); | ||
| 50 | |||
| 51 | $self->ioloop->iowatcher->change($socket, 1, 1); | ||
| 52 | |||
| 53 | # Ping tasks | ||
| 54 | $self->{_tasks} = []; | ||
| 55 | $self->{_tasks_out} = []; | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub ping { | ||
| 61 | my ($self, $host, $times, $cb) = @_; | ||
| 62 | |||
| 63 | my $socket = $self->{_socket}; | ||
| 64 | |||
| 65 | my $ip = inet_aton($host); | ||
| 66 | |||
| 67 | my $request = { | ||
| 68 | host => $host, | ||
| 69 | times => $times, | ||
| 70 | results => [], | ||
| 71 | cb => $cb, | ||
| 72 | identifier => int(rand 0x10000), | ||
| 73 | destination => scalar sockaddr_in(0, $ip), | ||
| 74 | }; | ||
| 75 | |||
| 76 | push @{$self->{_tasks}}, $request; | ||
| 77 | |||
| 78 | push @{$self->{_tasks_out}}, $request; | ||
| 79 | |||
| 80 | $self->ioloop->iowatcher->change($socket, 1, 1); | ||
| 81 | |||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub start { | ||
| 86 | my ($self) = @_; | ||
| 87 | $self->ioloop->start; | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _send_requests { | ||
| 93 | my $self = shift; | ||
| 94 | |||
| 95 | foreach my $request (@{$self->{_tasks_out}}) { | ||
| 96 | $self->_send_request($request); | ||
| 97 | } | ||
| 98 | |||
| 99 | $self->{_tasks_out} = []; | ||
| 100 | $self->ioloop->iowatcher->change($self->{_socket}, 1, 0); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _on_read { | ||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | my $socket = $self->{_socket}; | ||
| 107 | $socket->sysread(my $chunk, 4194304, 0); | ||
| 108 | |||
| 109 | my $icmp_msg = substr $chunk, 20; | ||
| 110 | |||
| 111 | my ($type, $identifier, $sequence, $data); | ||
| 112 | |||
| 113 | $type = unpack 'c', $icmp_msg; | ||
| 114 | |||
| 115 | if ($type == $ICMP_ECHOREPLY) { | ||
| 116 | ($type, $identifier, $sequence, $data) = | ||
| 117 | (unpack $ICMP_PING, $icmp_msg)[0, 3, 4, 5]; | ||
| 118 | } | ||
| 119 | elsif ($type == $ICMP_DEST_UNREACH || $type == $ICMP_TIME_EXCEEDED) { | ||
| 120 | ($identifier, $sequence) = unpack('nn', substr($chunk, 52)); | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | |||
| 124 | # Don't mind | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | # Find our task | ||
| 129 | my $request = | ||
| 130 | List::Util::first { $identifier == $_->{identifier} } | ||
| 131 | @{$self->{_tasks}}; | ||
| 132 | |||
| 133 | return unless $request; | ||
| 134 | |||
| 135 | # Is it response to our latest message? | ||
| 136 | return unless $sequence == @{$request->{results}} + 1; | ||
| 137 | |||
| 138 | if ($type == $ICMP_ECHOREPLY) { | ||
| 139 | |||
| 140 | # Check data | ||
| 141 | if ($data eq $request->{data}) { | ||
| 142 | $self->_store_result($request, 'OK'); | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | $self->_store_result($request, 'MALFORMED'); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | elsif ($type == $ICMP_DEST_UNREACH) { | ||
| 149 | $self->_store_result($request, 'DEST_UNREACH'); | ||
| 150 | } | ||
| 151 | elsif ($type == $ICMP_TIME_EXCEEDED) { | ||
| 152 | $self->_store_result($request, 'TIMEOUT'); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _store_result { | ||
| 157 | my ($self, $request, $result) = @_; | ||
| 158 | |||
| 159 | my $results = $request->{results}; | ||
| 160 | |||
| 161 | # Clear request specific data | ||
| 162 | $self->ioloop->drop(delete $request->{timer}) if exists $request->{timer}; | ||
| 163 | |||
| 164 | push @$results, [$result, time - $request->{start}]; | ||
| 165 | |||
| 166 | if (@$results == $request->{times} || $result eq 'ERROR') { | ||
| 167 | |||
| 168 | # Cleanup | ||
| 169 | my $tasks = $self->{_tasks}; | ||
| 170 | for my $i (0 .. scalar @$tasks) { | ||
| 171 | if ($tasks->[$i] == $request) { | ||
| 172 | splice @$tasks, $i, 1; | ||
| 173 | last; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | # Testing done | ||
| 178 | $request->{cb}->($self, $results); | ||
| 179 | |||
| 180 | undef $request; | ||
| 181 | } | ||
| 182 | |||
| 183 | # Perform another check | ||
| 184 | else { | ||
| 185 | |||
| 186 | # Setup interval timer before next request | ||
| 187 | $self->ioloop->timer( | ||
| 188 | $self->interval => sub { | ||
| 189 | push @{$self->{_tasks_out}}, $request; | ||
| 190 | $self->ioloop->iowatcher->change($self->{_socket}, 1, 1); | ||
| 191 | } | ||
| 192 | ); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | sub _send_request { | ||
| 197 | my ($self, $request) = @_; | ||
| 198 | |||
| 199 | my $checksum = 0x0000; | ||
| 200 | my $identifier = $request->{identifier}; | ||
| 201 | my $sequence = @{$request->{results}} + 1; | ||
| 202 | my $data = 'abcdef'; | ||
| 203 | |||
| 204 | my $msg = pack $ICMP_PING, | ||
| 205 | $ICMP_ECHO, 0x00, $checksum, | ||
| 206 | $identifier, $sequence, $data; | ||
| 207 | |||
| 208 | $checksum = $self->_icmp_checksum($msg); | ||
| 209 | |||
| 210 | $msg = pack $ICMP_PING, | ||
| 211 | 0x08, 0x00, $checksum, | ||
| 212 | $identifier, $sequence, $data; | ||
| 213 | |||
| 214 | $request->{data} = $data; | ||
| 215 | |||
| 216 | $request->{start} = time; | ||
| 217 | |||
| 218 | $request->{timer} = $self->ioloop->timer( | ||
| 219 | $self->timeout => sub { | ||
| 220 | my ($loop) = @_; | ||
| 221 | $self->_store_result($request, 'TIMEOUT'); | ||
| 222 | } | ||
| 223 | ); | ||
| 224 | |||
| 225 | my $socket = $self->{_socket}; | ||
| 226 | |||
| 227 | $socket->send($msg, 0, $request->{destination}) or die "$!"; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub _icmp_checksum { | ||
| 231 | my ($self, $msg) = @_; | ||
| 232 | |||
| 233 | my $res = 0; | ||
| 234 | foreach my $int (unpack "n*", $msg) { | ||
| 235 | $res += $int; | ||
| 236 | } | ||
| 237 | |||
| 238 | # Add possible odd byte | ||
| 239 | $res += unpack('C', substr($msg, -1, 1)) << 8 | ||
| 240 | if length($msg) % 2; | ||
| 241 | |||
| 242 | # Fold high into low | ||
| 243 | $res = ($res >> 16) + ($res & 0xffff); | ||
| 244 | |||
| 245 | # Two times | ||
| 246 | $res = ($res >> 16) + ($res & 0xffff); | ||
| 247 | |||
| 248 | return ~$res; | ||
| 249 | } | ||
| 250 | |||
| 251 | 1; | ||
| 252 | __END__ |