Ping/ICMP.pm

filter: Statementstmt 59.6% 77 / 129 Branchbran 5.2% 2 / 38 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 77.7% 21 / 27 Podpod 50.0% 1 / 2 totaltotal 47.6% 103 / 216 CC 25 SCAR 42.5
CC
25
Cov
45%
__ANON__
210.0
ping
65.2
__ANON__
6.0
CRAP 70.1
↑ Summary Ping.pm »
1 package Net::Async::Ping::ICMP;
2 1 $Net::Async::Ping::ICMP::VERSION = '0.001001';
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings NONFATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POSIX 'ECONNREFUSED';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::Ping;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Async::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Socket qw( SOCK_RAW SOCK_DGRAM AF_INET NI_NUMERICHOST inet_aton pack_sockaddr_in unpack_sockaddr_in getnameinfo inet_ntop);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use constant ICMP_ECHOREPLY => 0; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant ICMP_UNREACHABLE => 3; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant ICMP_ECHO => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant ICMP_TIME_EXCEEDED => 11; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant ICMP_PARAMETER_PROBLEM => 12; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant ICMP_STRUCT => "C2 n3 A"; # Structure of a minimal ICMP packet
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant SUBCODE => 0; # No ICMP subcode for ECHO and ECHOREPLY
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant ICMP_FLAGS => 0; # No special flags for send or recv
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant ICMP_PORT => 0; # No port with ICMP
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 extends 'IO::Async::Notifier';
26  
27 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 has default_timeout => (
30 is => 'ro',
31 default => 5,
32 );
33  
34 1 has bind => ( is => 'rw' );
35  
36 1 has _pid => (
37 is => 'lazy',
38 );
39  
40 sub _build__pid
41 0 ▶{ my $self = shift;
Subroutine _build__pid: not called CC=1 SCAR=6.9
42 0 $$ & 0xffff;
43 }
44  
45 1 has seq => (
46 is => 'ro',
47 default => 1,
48 );
49  
50 # Whether to try and use ping sockets. This option used in tests
51 # to force normal ping to be used
52 1 has use_ping_socket => (
53 is => 'ro',
54 default => 1,
55 );
56  
57 # Overrides method in IO::Async::Notifier to allow specific options in this class
58 sub configure_unknown
59 1 ▶{ my $self = shift;
Subroutine configure_unknown: called CC=3 SCAR=12.5
Pod documented
60 1 my %params = @_;
61 1 delete $params{$_} foreach qw/default_timeout bind seq use_ping_socket/;
62 1 ▶ return unless keys %params;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %params 1 0 1
63 0 my $class = ref $self;
64 0 croak "Unrecognised configuration keys for $class - " . join( " ", keys %params );
65  
66 }
67  
68 sub ping {
69 1 ▶ my $self = shift;
Subroutine ping: called CC=9 SCAR=41.8
Pod undocumented
70 # Maintain compat with old API
71 1 my $legacy = ref $_[0] eq 'IO::Async::Loop::Poll';
72 1 ▶ my $loop = $legacy ? shift : $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
$legacy ? : 0 1 1
73  
74 1 my ($host, $timeout) = @_;
75 1 ▶ $timeout //= $self->default_timeout;
Condition33%1/3
$timeout //= $self->default_timeout
l!l&&r!l&&!r
010
MC/DC0%0/2
$timeout //= $self->default_timeout
$timeout $self->default_timeout
Truth table33%1/3
A //= B
A= $timeoutB= $self->default_timeout
ABresult
1X1
011
000
76  
77 1 my $t0 = [Time::HiRes::gettimeofday];
78  
79 1 my $fh = IO::Socket->new;
80 1 ▶ my $proto_num = (getprotobyname('icmp'))[2] ||
Condition33%1/3
(getprotobyname 'icmp')[2] || croak(q[Can't get icmp protocol by name])
l!l&&r!l&&!r
001
MC/DC0%0/2
(getprotobyname 'icmp')[2] || croak(q[Can't get icmp protocol by name])
(getprotobyname 'icmp')[2] croak(q[Can't get icmp protocol by name])
Truth table33%1/3
A || B
A= (getprotobyname 'icmp')[2]B= croak(q[Can't get icmp protocol by name])
ABresult
1X1
011
000
81 croak("Can't get icmp protocol by name");
82 # Let's try a ping socket (unprivileged ping) first. See
83 # https://lwn.net/Articles/422330/
84 0 my ($ping_socket, $ident);
85 0 ▶ if ($self->use_ping_socket && socket($fh, AF_INET, SOCK_DGRAM, $proto_num))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_ping_socket and socket $fh, 2, 2, $proto_num) { } 0 0 0
Condition0%0/3
$self->use_ping_socket and socket $fh, 2, 2, $proto_num
!ll&&!rl&&r
000
MC/DC0%0/2
$self->use_ping_socket and socket $fh, 2, 2, $proto_num
$self->use_ping_socket socket $fh, 2, 2, $proto_num
Truth table0%0/3
A and B
A= $self->use_ping_socketB= socket $fh, 2, 2, $proto_num
ABresult
0X0
100
111
86 {
87 0 $ping_socket = 1;
88 0 ($ident) = unpack_sockaddr_in getsockname($fh);
89 }
90 else {
91 0 ▶ socket($fh, AF_INET, SOCK_RAW, $proto_num) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $fh, 2, 3, $proto_num or croak("Unable to create ICMP socket ($!). Are you running as root?" . ' If not, and your system supports ping sockets, try setting' . " /proc/sys/net/ipv4/ping_group_range") 0 0 0
92 croak("Unable to create ICMP socket ($!). Are you running as root?"
93 ." If not, and your system supports ping sockets, try setting"
94 ." /proc/sys/net/ipv4/ping_group_range");
95 0 $ident = $self->_pid;
96 }
97  
98 0 ▶ if ($self->bind)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bind) 0 0 0
99 {
100 0 my $bind = pack_sockaddr_in 0, inet_aton $self->bind;
101 0 ▶ bind $fh, $bind
Branch: 1 branch
BranchTrueFalseTotal
CORE::bind $fh, $bind or croak('Failed to bind to ' . $self->bind) 0 0 0
102 or croak "Failed to bind to ".$self->bind;
103 }
104  
105 $loop->resolver->getaddrinfo(
106 host => $host,
107 protocol => $proto_num,
108 family => AF_INET,
109 )->then( sub {
110  
111 0 ▶ my $saddr = $_[0]->{addr};
Subroutine __ANON__: not called CC=2 SCAR=17.9
112 0 my $f = $loop->new_future;
113  
114 my $socket = IO::Async::Socket->new(
115 handle => $fh,
116 on_recv_error => sub {
117 0 ▶ my ( $self, $errno ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 $f->fail('Receive error');
119 },
120 0 );
121  
122 my $on_recv = $self->_capture_weakself(
123 sub {
124 0 ▶ my $ping = shift or return; # weakref, may have disappeared
Subroutine __ANON__: not called CC=14 SCAR=53.5
Branch: 1 branch
BranchTrueFalseTotal
my $ping = shift() or return 0 0 0
125 0 my ( $self, $recv_msg, $from_saddr ) = @_;
126  
127 0 my $from_pid = -1;
128 0 my $from_seq = -1;
129 0 my ($from_port, $from_ip) = unpack_sockaddr_in($from_saddr);
130 0 ▶ my $offset = $ping_socket ? 0 : 20; # No offset needed for ping sockets
Branch: 1 branch
BranchTrueFalseTotal
$ping_socket ? : 0 0 0
131 0 my ($from_type, $from_subcode) = unpack("C2", substr($recv_msg, $offset, 2));
132  
133 0 ▶ if ($from_type == ICMP_ECHOREPLY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_type == 0) { } 0 0 0
134 0 ▶ ($from_pid, $from_seq) = unpack("n3", substr($recv_msg, $offset + 4, 4))
Branch: 1 branch
BranchTrueFalseTotal
if length $recv_msg >= $offset + 8 0 0 0
135 if length $recv_msg >= $offset + 8;
136 } else {
137 0 ▶ ($from_pid, $from_seq) = unpack("n3", substr($recv_msg, $offset + 32, 4))
Branch: 1 branch
BranchTrueFalseTotal
if length $recv_msg >= $offset + 36 0 0 0
138 if length $recv_msg >= $offset + 36;
139 }
140  
141 # Not needed for ping socket - kernel handles this for us
142 0 ▶ return if !$ping_socket && $from_pid != $ping->_pid;
Branch: 1 branch
BranchTrueFalseTotal
if not $ping_socket and $from_pid != $ping->_pid 0 0 0
Condition0%0/3
not $ping_socket and $from_pid != $ping->_pid
!ll&&!rl&&r
000
MC/DC0%0/2
not $ping_socket and $from_pid != $ping->_pid
not $ping_socket $from_pid != $ping->_pid
Truth table0%0/3
not(A) and B
A= not $ping_socketB= $from_pid != $ping->_pid
ABresult
0X0
100
111
143 0 ▶ return if $from_seq != $ping->seq;
Branch: 1 branch
BranchTrueFalseTotal
if $from_seq != $ping->seq 0 0 0
144 0 ▶ if ($from_type == ICMP_ECHOREPLY) {
Branch: 3 branches
BranchTrueFalseTotal
if ($from_type == 0) { } 0 0 0
elsif ($from_type == 3) { } 0 0 0
elsif ($from_type == 11) { } 0 0 0
145 0 my $ip = unpack_sockaddr_in($saddr);
146 0 ▶ return if inet_ntop(AF_INET, $from_ip) ne inet_ntop(AF_INET, $ip); # Does the packet check out?
Branch: 1 branch
BranchTrueFalseTotal
if inet_ntop(2, $from_ip) ne inet_ntop(2, $ip) 0 0 0
147 0 $f->done;
148 } elsif ($from_type == ICMP_UNREACHABLE) {
149 0 $f->fail('ICMP Unreachable');
150 } elsif ($from_type == ICMP_TIME_EXCEEDED) {
151 0 $f->fail('ICMP Timeout');
152 }
153 0 ▶ $legacy ? $loop->remove($socket) : $ping->remove_child($socket);
Branch: 1 branch
BranchTrueFalseTotal
$legacy ? : 0 0 0
154 },
155 0 );
156  
157 0 $socket->configure(on_recv => $on_recv);
158 0 ▶ $legacy ? $loop->add($socket) : $self->add_child($socket);
Branch: 1 branch
BranchTrueFalseTotal
$legacy ? : 0 0 0
159 0 $socket->send( $self->_msg($ident), ICMP_FLAGS, $saddr );
160  
161 Future->wait_any(
162 $f,
163 $loop->timeout_future(after => $timeout)
164 )
165 ->then(
166 0 ▶ sub { Future->done(Time::HiRes::tv_interval($t0)) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 )
168 0 });
169 }
170  
171 sub _msg
172 0 ▶{ my ($self, $ident) = @_;
Subroutine _msg: not called CC=1 SCAR=6.9
173 # data_size to be implemented later
174 0 my $data_size = 0;
175 0 my $data = '';
176 0 my $checksum = 0;
177 0 my $msg = pack(ICMP_STRUCT . $data_size, ICMP_ECHO, SUBCODE,
178 $checksum, $ident, $self->seq, $data);
179 0 $checksum = Net::Ping->checksum($msg);
180 0 $msg = pack(ICMP_STRUCT . $data_size, ICMP_ECHO, SUBCODE,
181 $checksum, $ident, $self->seq, $data);
182 0 return $msg;
183 }
184  
185 1 1;
186  
187 __END__
↑ Summary Ping.pm »