blib/lib/Net/Ping.pm

filter: Statementstmt 13.0% 123 / 946 Branchbran 0.3% 2 / 606 Conditioncond 2.4% 9 / 361 MC/DCmcdc 0.0% 0 / 201 Subroutinesub 37.9% 30 / 79 Podpod 100% 36 / 36 totaltotal 8.9% 200 / 2229 CC 429 SCAR 75.2
CC
429
Cov
7%
new
8010.0
ping_icmp
1892.0
ack
1722.0
CRAP 1840.8
↑ Summary
1 package Net::Ping;
2  
3 7 require 5.002;
4 7 require Exporter;
5  
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
8 $def_timeout $def_proto $def_factor $def_family
9 7 $max_datasize $pingstring $hires $source_verify $syn_forking);
10 7 ▶use Fcntl qw( F_GETFL F_SETFL O_NONBLOCK );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Socket 2.007;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Socket qw( SOCK_DGRAM SOCK_STREAM SOCK_RAW AF_INET PF_INET IPPROTO_TCP
Subroutine BEGIN: called CC=1 SCAR=0.0
13 SOL_SOCKET SO_ERROR SO_BROADCAST
14 IPPROTO_IP IP_TOS IP_TTL
15 7 inet_ntoa inet_aton getnameinfo sockaddr_in );
16 7 ▶use POSIX qw( ENOTCONN ECONNREFUSED ECONNRESET EINPROGRESS EWOULDBLOCK EAGAIN
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 WNOHANG );
18 7 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 @ISA = qw(Exporter);
23 7 @EXPORT = qw(pingecho);
24 7 @EXPORT_OK = qw(wakeonlan);
25 7 $VERSION = "2.77";
26  
27 # Globals
28  
29 7 $def_timeout = 5; # Default timeout to wait for a reply
30 7 $def_proto = "tcp"; # Default protocol to use for pinging
31 7 $def_factor = 1.2; # Default exponential backoff rate.
32 7 $def_family = AF_INET; # Default family.
33 7 $max_datasize = 65535; # Maximum data bytes. recommended: 1472 (Ethernet MTU: 1500)
34 # The data we exchange with the server for the stream protocol
35 7 $pingstring = "pingschwingping!\n";
36 7 $source_verify = 1; # Default is to verify source endpoint
37 7 $syn_forking = 0;
38  
39 # Constants
40  
41 7 ▶my $AF_INET6 = eval { Socket::AF_INET6() } || 30;
Condition50%1/2
eval { do { 10 } } || 30
l!l
70
MC/DC0%0/1
eval { do { 10 } } || 30
eval { do { 10 } }
Truth table50%1/2
A
A= eval { do { 10 } }
Aresult
11
00
42 7 my $AF_UNSPEC = eval { Socket::AF_UNSPEC() };
43 7 ▶my $AI_NUMERICHOST = eval { Socket::AI_NUMERICHOST() } || 4;
Condition50%1/2
eval { do { 4 } } || 4
l!l
70
MC/DC0%0/1
eval { do { 4 } } || 4
eval { do { 4 } }
Truth table50%1/2
A
A= eval { do { 4 } }
Aresult
11
00
44 7 ▶my $NI_NUMERICHOST = eval { Socket::NI_NUMERICHOST() } || 2;
Condition50%1/2
eval { do { 1 } } || 2
l!l
70
MC/DC0%0/1
eval { do { 1 } } || 2
eval { do { 1 } }
Truth table50%1/2
A
A= eval { do { 1 } }
Aresult
11
00
45 7 ▶my $IPPROTO_IPV6 = eval { Socket::IPPROTO_IPV6() } || 41;
Condition50%1/2
eval { do { 41 } } || 41
l!l
70
MC/DC0%0/1
eval { do { 41 } } || 41
eval { do { 41 } }
Truth table50%1/2
A
A= eval { do { 41 } }
Aresult
11
00
46 7 ▶my $NIx_NOSERV = eval { Socket::NIx_NOSERV() } || 2;
Condition50%1/2
eval { do { 2 } } || 2
l!l
70
MC/DC0%0/1
eval { do { 2 } } || 2
eval { do { 2 } }
Truth table50%1/2
A
A= eval { do { 2 } }
Aresult
11
00
47 #my $IPV6_HOPLIMIT = eval { Socket::IPV6_HOPLIMIT() }; # ping6 -h 0-255
48 7 my $qr_family = qr/^(?:(?:(:?ip)?v?(?:4|6))|${\AF_INET}|$AF_INET6)$/;
49 7 my $qr_family4 = qr/^(?:(?:(:?ip)?v?4)|${\AF_INET})$/;
50 7 my $Socket_VERSION = eval $Socket::VERSION;
51  
52 7 ▶if ($^O =~ /Win32/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win32/i) 0 7 7
53 # Hack to avoid this Win32 spewage:
54 # Your vendor has not defined POSIX macro ECONNREFUSED
55 0 my @pairs = (ECONNREFUSED => 10061, # "Unknown Error" Special Win32 Response?
56 ENOTCONN => 10057,
57 ECONNRESET => 10054,
58 EINPROGRESS => 10036,
59 EWOULDBLOCK => 10035,
60 );
61 0 ▶ while (my $name = shift @pairs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift @pairs) 0 0 0
62 0 my $value = shift @pairs;
63 # When defined, these all are non-zero
64 0 ▶ unless (eval $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval $name) 0 0 0
65 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 0 ▶ *{$name} = defined prototype \&{$name} ? sub () {$value} : sub {$value};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined prototype \&{$name;} ? : 0 0 0
67 }
68 }
69 # $syn_forking = 1; # XXX possibly useful in < Win2K ?
70 };
71  
72 # Description: The pingecho() subroutine is provided for backward
73 # compatibility with the original Net::Ping. It accepts a host
74 # name/IP and an optional timeout in seconds. Create a tcp ping
75 # object and try pinging the host. The result of the ping is returned.
76  
77 sub pingecho
78 0 ▶{
Subroutine pingecho: not called CC=1 SCAR=6.9
Pod documented
79 0 my ($host, # Name or IP number of host to ping
80 $timeout # Optional timeout in seconds
81 ) = @_;
82 0 my ($p); # A ping object
83  
84 0 $p = Net::Ping->new("tcp", $timeout);
85 0 $p->ping($host); # Going out of scope closes the connection
86 }
87  
88 # Description: The new() method creates a new ping object. Optional
89 # parameters may be specified for the protocol to use, the timeout in
90 # seconds and the size in bytes of additional data which should be
91 # included in the packet.
92 # After the optional parameters are checked, the data is constructed
93 # and a socket is opened if appropriate. The object is returned.
94  
95 sub new
96 0 ▶{
Subroutine new: not called CC=89 SCAR=89.9
Pod documented
97 0 my ($this,
98 $proto, # Optional protocol to use for pinging
99 $timeout, # Optional timeout in seconds
100 $data_size, # Optional additional bytes of data
101 $device, # Optional device to use
102 $tos, # Optional ToS to set
103 $ttl, # Optional TTL to set
104 $family, # Optional address family (AF_INET)
105 ) = @_;
106 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
107 0 my $self = {};
108 0 my ($cnt, # Count through data bytes
109 $min_datasize # Minimum data bytes required
110 );
111  
112 0 bless($self, $class);
113 0 ▶ if (ref $proto eq 'HASH') { # support named args
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto eq "HASH") 0 0 0
114 0 for my $k (qw(proto timeout data_size device tos ttl family
115 gateway host port bind retrans pingstring source_verify
116 econnrefused dontfrag
117 IPV6_USE_MIN_MTU IPV6_RECVPATHMTU IPV6_HOPLIMIT))
118 {
119 0 ▶ if (exists $proto->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $proto->{$k}) 0 0 0
120 0 $self->{$k} = $proto->{$k};
121 # some are still globals
122 0 ▶ if ($k eq 'pingstring') { $pingstring = $proto->{$k} }
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "pingstring") 0 0 0
123 0 ▶ if ($k eq 'source_verify') { $source_verify = $proto->{$k} }
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "source_verify") 0 0 0
124 # and some are local
125 0 ▶ $timeout = $proto->{$k} if ($k eq 'timeout');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "timeout" 0 0 0
126 0 ▶ $data_size = $proto->{$k} if ($k eq 'data_size');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "data_size" 0 0 0
127 0 ▶ $device = $proto->{$k} if ($k eq 'device');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "device" 0 0 0
128 0 ▶ $tos = $proto->{$k} if ($k eq 'tos');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "tos" 0 0 0
129 0 ▶ $ttl = $proto->{$k} if ($k eq 'ttl');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "ttl" 0 0 0
130 0 ▶ $family = $proto->{$k} if ($k eq 'family');
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "family" 0 0 0
131 0 delete $proto->{$k};
132 }
133 }
134 0 ▶ if (%$proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$proto) 0 0 0
135 0 croak("Invalid named argument: ",join(" ",keys (%$proto)));
136 }
137 0 $proto = $self->{'proto'};
138 }
139  
140 0 ▶ $proto = $def_proto unless $proto; # Determine the protocol
Branch: 1 branch
BranchTrueFalseTotal
unless $proto 0 0 0
141 0 ▶ croak('Protocol for ping must be "icmp", "icmpv6", "udp", "tcp", "syn", "stream" or "external"')
Branch: 1 branch
BranchTrueFalseTotal
unless $proto =~ /^(icmp|icmpv6|udp|tcp|syn|stream|external)$/ 0 0 0
142 unless $proto =~ m/^(icmp|icmpv6|udp|tcp|syn|stream|external)$/;
143 0 $self->{proto} = $proto;
144  
145 0 ▶ $timeout = $def_timeout unless defined $timeout; # Determine the timeout
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
146 0 ▶ croak("Default timeout for ping must be greater than 0 seconds")
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
147 if $timeout <= 0;
148 0 $self->{timeout} = $timeout;
149  
150 0 $self->{device} = $device;
151  
152 0 $self->{tos} = $tos;
153  
154 0 ▶ if ($self->{'host'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"host"}) 0 0 0
155 0 my $host = $self->{'host'};
156 0 ▶ my $ip = $self->_resolv($host) or
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $self->_resolv($host) or carp("could not resolve host $host") 0 0 0
157 carp("could not resolve host $host");
158 0 $self->{host} = $ip;
159 0 $self->{family} = $ip->{family};
160 }
161  
162 0 ▶ if ($self->{bind}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bind'}) { } 0 0 0
163 0 my $addr = $self->{bind};
164 0 ▶ my $ip = $self->_resolv($addr)
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $self->_resolv($addr) or carp("could not resolve local addr $addr") 0 0 0
165 or carp("could not resolve local addr $addr");
166 0 $self->{local_addr} = $ip;
167 } else {
168 0 $self->{local_addr} = undef; # Don't bind by default
169 }
170  
171 0 ▶ if ($self->{proto} eq 'icmp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proto"} eq "icmp") 0 0 0
172 0 ▶ croak('TTL must be from 0 to 255')
Branch: 1 branch
BranchTrueFalseTotal
if $ttl and $ttl < 0 || $ttl > 255 0 0 0
Condition0%0/6
$ttl and $ttl < 0 || $ttl > 255
!ll&&!rl&&r
000
$ttl < 0 or $ttl > 255
l!l&&r!l&&!r
000
MC/DC0%0/3
$ttl and $ttl < 0 || $ttl > 255
$ttl $ttl < 0 $ttl > 255
Truth table0%0/4
A and B or C
A= $ttlB= $ttl < 0C= $ttl > 255
ABCresult
0XX0
1000
11X1
1011
173 if ($ttl && ($ttl < 0 || $ttl > 255));
174 0 $self->{ttl} = $ttl;
175 }
176  
177 0 ▶ if ($family) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family) { } 0 0 0
178 0 ▶ if ($family =~ $qr_family) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /$qr_family/) { } 0 0 0
179 0 ▶ if ($family =~ $qr_family4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /$qr_family4/) { } 0 0 0
180 0 $self->{family} = AF_INET;
181 } else {
182 0 $self->{family} = $AF_INET6;
183 }
184 } else {
185 0 croak('Family must be "ipv4" or "ipv6"')
186 }
187 } else {
188 0 ▶ if ($self->{proto} eq 'icmpv6') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'proto'} eq 'icmpv6') { } 0 0 0
189 0 $self->{family} = $AF_INET6;
190 } else {
191 0 $self->{family} = $def_family;
192 }
193 }
194  
195 0 ▶ $min_datasize = ($proto eq "udp") ? 1 : 0; # Determine data size
Branch: 1 branch
BranchTrueFalseTotal
$proto eq 'udp' ? : 0 0 0
196 0 ▶ $data_size = $min_datasize unless defined($data_size) && $proto ne "tcp";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data_size and $proto ne "tcp" 0 0 0
Condition0%0/3
defined $data_size and $proto ne "tcp"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data_size and $proto ne "tcp"
defined $data_size $proto ne "tcp"
Truth table0%0/3
A and B
A= defined $data_sizeB= $proto ne "tcp"
ABresult
0X0
100
111
197 # allow for fragmented packets if data_size>1472 (MTU 1500)
198 0 ▶ croak("Data for ping must be from $min_datasize to $max_datasize bytes")
Branch: 1 branch
BranchTrueFalseTotal
if $data_size < $min_datasize or $data_size > $Net::Ping::max_datasize 0 0 0
Condition0%0/3
$data_size < $min_datasize or $data_size > $Net::Ping::max_datasize
l!l&&r!l&&!r
000
MC/DC0%0/2
$data_size < $min_datasize or $data_size > $Net::Ping::max_datasize
$data_size < $min_datasize $data_size > $Net::Ping::max_datasize
Truth table0%0/3
A or B
A= $data_size < $min_datasizeB= $data_size > $Net::Ping::max_datasize
ABresult
1X1
011
000
199 if ($data_size < $min_datasize) || ($data_size > $max_datasize);
200 0 ▶ $data_size-- if $self->{proto} eq "udp"; # We provide the first byte
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"proto"} eq "udp" 0 0 0
201 0 $self->{data_size} = $data_size;
202  
203 0 $self->{data} = ""; # Construct data bytes
204 0 ▶ for ($cnt = 0; $cnt < $self->{data_size}; $cnt++)
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt < $self->{"data_size"}) 0 0 0
205 {
206 0 $self->{data} .= chr($cnt % 256);
207 }
208  
209 # Default exponential backoff rate
210 0 ▶ $self->{retrans} = $def_factor unless exists $self->{retrans};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"retrans"} 0 0 0
211 # Default Connection refused behavior
212 0 ▶ $self->{econnrefused} = undef unless exists $self->{econnrefused};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"econnrefused"} 0 0 0
213  
214 0 $self->{seq} = 0; # For counting packets
215 0 ▶ if ($self->{proto} eq "udp") # Open a socket
Branch: 5 branches
BranchTrueFalseTotal
if ($self->{'proto'} eq 'udp') { } 0 0 0
elsif ($self->{'proto'} eq 'icmp') { } 0 0 0
elsif ($self->{'proto'} eq 'icmpv6') { } 0 0 0
elsif ($self->{'proto'} eq 'tcp' or $self->{'proto'} eq 'stream') { } 0 0 0
elsif ($self->{'proto'} eq 'syn') { } 0 0 0
216 {
217 0 ▶ $self->{proto_num} = eval { (getprotobyname('udp'))[2] } ||
Condition0%0/2
eval { do { (getprotobyname 'udp')[2] } } || croak(q[Can't udp protocol by name])
l!l
00
MC/DC0%0/1
eval { do { (getprotobyname 'udp')[2] } } || croak(q[Can't udp protocol by name])
eval { do { (getprotobyname 'udp')[2] } }
Truth table0%0/2
A
A= eval { do { (getprotobyname 'udp')[2] } }
Aresult
11
00
218 croak("Can't udp protocol by name");
219 $self->{port_num} = $self->{port}
220 0 ▶ || (getservbyname('echo', 'udp'))[2]
Condition0%0/5
$self->{'port'} || (getservbyname 'echo', 'udp')[2] || croak(q[Can't get udp echo port by name])
l!l
00
$self->{"port"} or (getservbyname "echo", "udp")[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'port'} || (getservbyname 'echo', 'udp')[2] || croak(q[Can't get udp echo port by name])
$self->{"port"} (getservbyname "echo", "udp")[2]
Truth table0%0/3
A or B
A= $self->{"port"}B= (getservbyname "echo", "udp")[2]
ABresult
1X1
011
000
221 || croak("Can't get udp echo port by name");
222 0 $self->{fh} = FileHandle->new();
223 socket($self->{fh}, PF_INET, SOCK_DGRAM,
224 0 ▶ $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, 2, 2, $self->{"proto_num"} or croak("udp socket error - $!") 0 0 0
225 croak("udp socket error - $!");
226 0 $self->_setopts();
227 }
228 elsif ($self->{proto} eq "icmp")
229 {
230 0 ▶ croak("icmp ping requires root privilege") if !_isroot() and $^O ne "linux";
Branch: 1 branch
BranchTrueFalseTotal
if not _isroot() and $^O ne "linux" 0 0 0
Condition0%0/3
not _isroot() and $^O ne "linux"
!ll&&!rl&&r
000
MC/DC0%0/2
not _isroot() and $^O ne "linux"
not _isroot() $^O ne "linux"
Truth table0%0/3
not(A) and B
A= not _isroot()B= $^O ne "linux"
ABresult
0X0
100
111
231 0 ▶ $self->{proto_num} = eval { (getprotobyname('icmp'))[2] } ||
Condition0%0/2
eval { do { (getprotobyname 'icmp')[2] } } || croak(q[Can't get icmp protocol by name])
l!l
00
MC/DC0%0/1
eval { do { (getprotobyname 'icmp')[2] } } || croak(q[Can't get icmp protocol by name])
eval { do { (getprotobyname 'icmp')[2] } }
Truth table0%0/2
A
A= eval { do { (getprotobyname 'icmp')[2] } }
Aresult
11
00
232 croak("Can't get icmp protocol by name");
233 0 $self->{pid} = $$ & 0xffff; # Save lower 16 bits of pid
234 0 $self->{fh} = FileHandle->new();
235 0 ▶ if ($^O eq "linux" and !_isroot()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'linux' and not _isroot()) { } 0 0 0
Condition0%0/3
$^O eq "linux" and not _isroot()
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "linux" and not _isroot()
$^O eq "linux" not _isroot()
Truth table0%0/3
A and not(B)
A= $^O eq "linux"B= not _isroot()
ABresult
0X0
100
111
236 0 ▶ socket($self->{fh}, PF_INET, SOCK_DGRAM, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, 2, 2, $self->{"proto_num"} or croak("icmp socket error - $!") 0 0 0
237 croak("icmp socket error - $!");
238 } else {
239 0 ▶ socket($self->{fh}, PF_INET, SOCK_RAW, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, 2, 3, $self->{"proto_num"} or croak("icmp socket error - $!") 0 0 0
240 croak("icmp socket error - $!");
241 }
242 0 $self->_setopts();
243 0 ▶ if ($self->{'ttl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ttl"}) 0 0 0
244 0 ▶ setsockopt($self->{fh}, IPPROTO_IP, IP_TTL, pack("I*", $self->{'ttl'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, 0, 2, pack("I*", $self->{"ttl"}) or croak(qq[error configuring ttl to $self->{"ttl"} $!]) 0 0 0
245 or croak "error configuring ttl to $self->{'ttl'} $!";
246 }
247 }
248 elsif ($self->{proto} eq "icmpv6")
249 {
250 #croak("icmpv6 ping requires root privilege") if !_isroot();
251 croak("Wrong family $self->{family} for icmpv6 protocol")
252 0 ▶ if $self->{family} and $self->{family} != $AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"family"} and $self->{"family"} != $AF_INET6 0 0 0
Condition0%0/3
$self->{"family"} and $self->{"family"} != $AF_INET6
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"family"} and $self->{"family"} != $AF_INET6
$self->{"family"} $self->{"family"} != $AF_INET6
Truth table0%0/3
A and B
A= $self->{"family"}B= $self->{"family"} != $AF_INET6
ABresult
0X0
100
111
253 0 $self->{family} = $AF_INET6;
254 0 ▶ $self->{proto_num} = eval { (getprotobyname('ipv6-icmp'))[2] } ||
Condition0%0/2
eval { do { (getprotobyname 'ipv6-icmp')[2] } } || croak(q[Can't get ipv6-icmp protocol by name])
l!l
00
MC/DC0%0/1
eval { do { (getprotobyname 'ipv6-icmp')[2] } } || croak(q[Can't get ipv6-icmp protocol by name])
eval { do { (getprotobyname 'ipv6-icmp')[2] } }
Truth table0%0/2
A
A= eval { do { (getprotobyname 'ipv6-icmp')[2] } }
Aresult
11
00
255 croak("Can't get ipv6-icmp protocol by name"); # 58
256 0 $self->{pid} = $$ & 0xffff; # Save lower 16 bits of pid
257 0 $self->{fh} = FileHandle->new();
258 0 ▶ if ($^O eq 'linux' and !_isroot()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'linux' and not _isroot()) { } 0 0 0
Condition0%0/3
$^O eq "linux" and not _isroot()
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "linux" and not _isroot()
$^O eq "linux" not _isroot()
Truth table0%0/3
A and not(B)
A= $^O eq "linux"B= not _isroot()
ABresult
0X0
100
111
259 0 ▶ socket($self->{fh}, $AF_INET6, SOCK_DGRAM, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, $AF_INET6, 2, $self->{"proto_num"} or croak("icmp socket error - $!") 0 0 0
260 croak("icmp socket error - $!");
261 } else {
262 0 ▶ socket($self->{fh}, $AF_INET6, SOCK_RAW, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, $AF_INET6, 3, $self->{"proto_num"} or croak("icmp socket error - $!") 0 0 0
263 croak("icmp socket error - $!");
264 }
265 0 $self->_setopts();
266 0 ▶ if ($self->{'gateway'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"gateway"}) 0 0 0
267 0 my $g = $self->{gateway};
268 0 ▶ my $ip = $self->_resolv($g)
Branch: 1 branch
BranchTrueFalseTotal
my $ip = $self->_resolv($g) or croak("nonexistent gateway $g") 0 0 0
269 or croak("nonexistent gateway $g");
270 0 ▶ $self->{family} eq $AF_INET6
Branch: 1 branch
BranchTrueFalseTotal
$self->{"family"} eq $AF_INET6 or croak("gateway requires the AF_INET6 family") 0 0 0
271 or croak("gateway requires the AF_INET6 family");
272 0 ▶ $ip->{family} eq $AF_INET6
Branch: 1 branch
BranchTrueFalseTotal
$ip->{"family"} eq $AF_INET6 or croak("gateway address needs to be IPv6") 0 0 0
273 or croak("gateway address needs to be IPv6");
274 0 ▶ my $IPV6_NEXTHOP = eval { Socket::IPV6_NEXTHOP() } || 48; # IPV6_3542NEXTHOP, or 21
Condition0%0/2
eval { do { Socket::IPV6_NEXTHOP() } } || 48
l!l
00
MC/DC0%0/1
eval { do { Socket::IPV6_NEXTHOP() } } || 48
eval { do { Socket::IPV6_NEXTHOP() } }
Truth table0%0/2
A
A= eval { do { Socket::IPV6_NEXTHOP() } }
Aresult
11
00
275 0 ▶ setsockopt($self->{fh}, $IPPROTO_IPV6, $IPV6_NEXTHOP, _pack_sockaddr_in($ip))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $IPPROTO_IPV6, $IPV6_NEXTHOP, &_pack_sockaddr_in($ip) or croak("error configuring gateway to $g NEXTHOP $!") 0 0 0
276 or croak "error configuring gateway to $g NEXTHOP $!";
277 }
278 0 ▶ if (exists $self->{IPV6_USE_MIN_MTU}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"IPV6_USE_MIN_MTU"}) 0 0 0
279 0 ▶ my $IPV6_USE_MIN_MTU = eval { Socket::IPV6_USE_MIN_MTU() } || 42;
Condition0%0/2
eval { do { Socket::IPV6_USE_MIN_MTU() } } || 42
l!l
00
MC/DC0%0/1
eval { do { Socket::IPV6_USE_MIN_MTU() } } || 42
eval { do { Socket::IPV6_USE_MIN_MTU() } }
Truth table0%0/2
A
A= eval { do { Socket::IPV6_USE_MIN_MTU() } }
Aresult
11
00
280 setsockopt($self->{fh}, $IPPROTO_IPV6, $IPV6_USE_MIN_MTU,
281 0 ▶ pack("I*", $self->{'IPV6_USE_MIN_MT'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $IPPROTO_IPV6, $IPV6_USE_MIN_MTU, pack("I*", $self->{"IPV6_USE_MIN_MT"}) or croak("error configuring IPV6_USE_MIN_MT} $!") 0 0 0
282 or croak "error configuring IPV6_USE_MIN_MT} $!";
283 }
284 0 ▶ if (exists $self->{IPV6_RECVPATHMTU}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"IPV6_RECVPATHMTU"}) 0 0 0
285 0 ▶ my $IPV6_RECVPATHMTU = eval { Socket::IPV6_RECVPATHMTU() } || 43;
Condition0%0/2
eval { do { Socket::IPV6_RECVPATHMTU() } } || 43
l!l
00
MC/DC0%0/1
eval { do { Socket::IPV6_RECVPATHMTU() } } || 43
eval { do { Socket::IPV6_RECVPATHMTU() } }
Truth table0%0/2
A
A= eval { do { Socket::IPV6_RECVPATHMTU() } }
Aresult
11
00
286 setsockopt($self->{fh}, $IPPROTO_IPV6, $IPV6_RECVPATHMTU,
287 0 ▶ pack("I*", $self->{'RECVPATHMTU'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $IPPROTO_IPV6, $IPV6_RECVPATHMTU, pack("I*", $self->{"RECVPATHMTU"}) or croak("error configuring IPV6_RECVPATHMTU $!") 0 0 0
288 or croak "error configuring IPV6_RECVPATHMTU $!";
289 }
290 0 ▶ if ($self->{'tos'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tos"}) 0 0 0
291 0 ▶ my $proto = $self->{family} == AF_INET ? IPPROTO_IP : $IPPROTO_IPV6;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'family'} == 2 ? : 0 0 0
292 0 ▶ setsockopt($self->{fh}, $proto, IP_TOS, pack("I*", $self->{'tos'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $proto, 1, pack("I*", $self->{"tos"}) or croak(qq[error configuring tos to $self->{"tos"} $!]) 0 0 0
293 or croak "error configuring tos to $self->{'tos'} $!";
294 }
295 0 ▶ if ($self->{'ttl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ttl"}) 0 0 0
296 0 ▶ my $proto = $self->{family} == AF_INET ? IPPROTO_IP : $IPPROTO_IPV6;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'family'} == 2 ? : 0 0 0
297 0 ▶ setsockopt($self->{fh}, $proto, IP_TTL, pack("I*", $self->{'ttl'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $proto, 2, pack("I*", $self->{"ttl"}) or croak(qq[error configuring ttl to $self->{"ttl"} $!]) 0 0 0
298 or croak "error configuring ttl to $self->{'ttl'} $!";
299 }
300 }
301 ▶ elsif ($self->{proto} eq "tcp" || $self->{proto} eq "stream")
Condition0%0/3
$self->{"proto"} eq "tcp" or $self->{"proto"} eq "stream"
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"proto"} eq "tcp" or $self->{"proto"} eq "stream"
$self->{"proto"} eq "tcp" $self->{"proto"} eq "stream"
Truth table0%0/3
A or B
A= $self->{"proto"} eq "tcp"B= $self->{"proto"} eq "stream"
ABresult
1X1
011
000
302 {
303 0 ▶ $self->{proto_num} = eval { (getprotobyname('tcp'))[2] } ||
Condition0%0/2
eval { do { (getprotobyname 'tcp')[2] } } || croak(q[Can't get tcp protocol by name])
l!l
00
MC/DC0%0/1
eval { do { (getprotobyname 'tcp')[2] } } || croak(q[Can't get tcp protocol by name])
eval { do { (getprotobyname 'tcp')[2] } }
Truth table0%0/2
A
A= eval { do { (getprotobyname 'tcp')[2] } }
Aresult
11
00
304 croak("Can't get tcp protocol by name");
305 $self->{port_num} = $self->{port}
306 0 ▶ || (getservbyname('echo', 'tcp'))[2]
Condition0%0/5
$self->{'port'} || (getservbyname 'echo', 'tcp')[2] || croak(q[Can't get tcp echo port by name])
l!l
00
$self->{"port"} or (getservbyname "echo", "tcp")[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'port'} || (getservbyname 'echo', 'tcp')[2] || croak(q[Can't get tcp echo port by name])
$self->{"port"} (getservbyname "echo", "tcp")[2]
Truth table0%0/3
A or B
A= $self->{"port"}B= (getservbyname "echo", "tcp")[2]
ABresult
1X1
011
000
307 || croak("Can't get tcp echo port by name");
308 0 $self->{fh} = FileHandle->new();
309 }
310 elsif ($self->{proto} eq "syn")
311 {
312 0 ▶ $self->{proto_num} = eval { (getprotobyname('tcp'))[2] } ||
Condition0%0/2
eval { do { (getprotobyname 'tcp')[2] } } || croak(q[Can't get tcp protocol by name])
l!l
00
MC/DC0%0/1
eval { do { (getprotobyname 'tcp')[2] } } || croak(q[Can't get tcp protocol by name])
eval { do { (getprotobyname 'tcp')[2] } }
Truth table0%0/2
A
A= eval { do { (getprotobyname 'tcp')[2] } }
Aresult
11
00
313 croak("Can't get tcp protocol by name");
314 0 ▶ $self->{port_num} = (getservbyname('echo', 'tcp'))[2] ||
Condition0%0/2
(getservbyname 'echo', 'tcp')[2] || croak(q[Can't get tcp echo port by name])
l!l
00
MC/DC0%0/1
(getservbyname 'echo', 'tcp')[2] || croak(q[Can't get tcp echo port by name])
(getservbyname 'echo', 'tcp')[2]
Truth table0%0/2
A
A= (getservbyname 'echo', 'tcp')[2]
Aresult
11
00
315 croak("Can't get tcp echo port by name");
316 0 ▶ if ($syn_forking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Ping::syn_forking) { } 0 0 0
317 0 $self->{fork_rd} = FileHandle->new();
318 0 $self->{fork_wr} = FileHandle->new();
319 0 pipe($self->{fork_rd}, $self->{fork_wr});
320 0 $self->{fh} = FileHandle->new();
321 0 $self->{good} = {};
322 0 $self->{bad} = {};
323 } else {
324 0 $self->{wbits} = "";
325 0 $self->{bad} = {};
326 }
327 0 $self->{syn} = {};
328 0 $self->{stop_time} = 0;
329 }
330  
331 0 return($self);
332 }
333  
334 # Description: Set the local IP address from which pings will be sent.
335 # For ICMP, UDP and TCP pings, just saves the address to be used when
336 # the socket is opened. Returns non-zero if successful; croaks on error.
337 sub bind
338 0 ▶{
Subroutine bind: not called CC=10 SCAR=47.0
Pod documented
339 0 my ($self,
340 $local_addr # Name or IP number of local interface
341 ) = @_;
342 0 my ($ip, # Hash of addr (string), addr_in (packed), family
343 $h # resolved hash
344 );
345  
346 0 ▶ croak("Usage: \$p->bind(\$local_addr)") unless @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 0 0
347 croak("already bound") if defined($self->{local_addr}) &&
348 0 ▶ ($self->{proto} eq "udp" || $self->{proto} eq "icmp");
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"local_addr"} and $self->{"proto"} eq "udp" || $self->{"proto"} eq "icmp" 0 0 0
Condition0%0/6
defined $self->{"local_addr"} and $self->{"proto"} eq "udp" || $self->{"proto"} eq "icmp"
!ll&&!rl&&r
000
$self->{"proto"} eq "udp" or $self->{"proto"} eq "icmp"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $self->{"local_addr"} and $self->{"proto"} eq "udp" || $self->{"proto"} eq "icmp"
defined $self->{"local_addr"} $self->{"proto"} eq "udp" $self->{"proto"} eq "icmp"
Truth table0%0/4
A and B or C
A= defined $self->{"local_addr"}B= $self->{"proto"} eq "udp"C= $self->{"proto"} eq "icmp"
ABCresult
0XX0
1000
11X1
1011
349  
350 0 $ip = $self->_resolv($local_addr);
351 0 ▶ carp("nonexistent local address $local_addr") unless defined($ip);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 0 0 0
352 0 $self->{local_addr} = $ip;
353  
354 0 ▶ if (($self->{proto} ne "udp") &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proto"} ne "udp" and $self->{"proto"} ne "icmp" and $self->{"proto"} ne "tcp" and $self->{"proto"} ne "syn") 0 0 0
Condition0%0/9
$self->{"proto"} ne "udp" and $self->{"proto"} ne "icmp" and $self->{"proto"} ne "tcp" and $self->{"proto"} ne "syn"
!ll&&!rl&&r
000
$self->{"proto"} ne "udp" and $self->{"proto"} ne "icmp" and $self->{"proto"} ne "tcp"
!ll&&!rl&&r
000
$self->{"proto"} ne "udp" and $self->{"proto"} ne "icmp"
!ll&&!rl&&r
000
MC/DC0%0/4
$self->{"proto"} ne "udp" and $self->{"proto"} ne "icmp" and $self->{"proto"} ne "tcp" and $self->{"proto"} ne "syn"
$self->{"proto"} ne "udp" $self->{"proto"} ne "icmp" $self->{"proto"} ne "tcp" $self->{"proto"} ne "syn"
Truth table0%0/5
A and B and C and D
A= $self->{"proto"} ne "udp"B= $self->{"proto"} ne "icmp"C= $self->{"proto"} ne "tcp"D= $self->{"proto"} ne "syn"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
355 ($self->{proto} ne "icmp") &&
356 ($self->{proto} ne "tcp") &&
357 ($self->{proto} ne "syn"))
358 {
359 0 croak("Unknown protocol \"$self->{proto}\" in bind()");
360 }
361  
362 0 return 1;
363 }
364  
365 # Description: A select() wrapper that compensates for platform
366 # peculiarities.
367 sub mselect
368 0 ▶{
Subroutine mselect: not called CC=9 SCAR=45.0
Pod documented
369 0 ▶ if ($_[3] > 0 and $^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3] > 0 and $^O eq 'MSWin32') { } 0 0 0
Condition0%0/3
$_[3] > 0 and $^O eq "MSWin32"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[3] > 0 and $^O eq "MSWin32"
$_[3] > 0 $^O eq "MSWin32"
Truth table0%0/3
A and B
A= $_[3] > 0B= $^O eq "MSWin32"
ABresult
0X0
100
111
370 # On windows, select() doesn't process the message loop,
371 # but sleep() will, allowing alarm() to interrupt the latter.
372 # So we chop up the timeout into smaller pieces and interleave
373 # select() and sleep() calls.
374 0 my $t = $_[3];
375 0 my $gran = 0.5; # polling granularity in seconds
376 0 my @args = @_;
377 0 while (1) {
378 0 ▶ $gran = $t if $gran > $t;
Branch: 1 branch
BranchTrueFalseTotal
if $gran > $t 0 0 0
379 0 my $nfound = select($_[0], $_[1], $_[2], $gran);
380 0 ▶ undef $nfound if $nfound == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $nfound == -1 0 0 0
381 0 $t -= $gran;
382 0 ▶ return $nfound if $nfound or !defined($nfound) or $t <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nfound or not defined $nfound or $t <= 0 0 0 0
Condition0%0/6
$nfound or not defined $nfound or $t <= 0
l!l&&r!l&&!r
000
$nfound or not defined $nfound
l!l&&r!l&&!r
000
MC/DC0%0/3
$nfound or not defined $nfound or $t <= 0
$nfound not defined $nfound $t <= 0
Truth table0%0/4
A or not(B) or C
A= $nfoundB= not defined $nfoundC= $t <= 0
ABCresult
1XX1
01X1
0011
0000
383  
384 0 sleep(0);
385 0 ($_[0], $_[1], $_[2]) = @args;
386 }
387 }
388 else {
389 0 my $nfound = select($_[0], $_[1], $_[2], $_[3]);
390 0 ▶ undef $nfound if $nfound == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $nfound == -1 0 0 0
391 0 return $nfound;
392 }
393 }
394  
395 # Description: Allow UDP source endpoint comparison to be
396 # skipped for those remote interfaces that do
397 # not response from the same endpoint.
398  
399 sub source_verify
400 0 ▶{
Subroutine source_verify: not called CC=4 SCAR=30.0
Pod documented
401 0 my $self = shift;
402 0 ▶ $source_verify = 1 unless defined
Branch: 2 branches
BranchTrueFalseTotal
unless defined($Net::Ping::source_verify = defined $self && ref $self ? shift() : $self) 0 0 0
defined $self && ref $self ? : 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
403 ($source_verify = ((defined $self) && (ref $self)) ? shift() : $self);
404 }
405  
406 # Description: Set whether or not the connect
407 # behavior should enforce remote service
408 # availability as well as reachability.
409  
410 sub service_check
411 0 ▶{
Subroutine service_check: not called CC=2 SCAR=17.9
Pod documented
412 0 my $self = shift;
413 $self->{econnrefused} = 1 unless defined
414 0 ▶ ($self->{econnrefused} = shift());
Branch: 1 branch
BranchTrueFalseTotal
unless defined($self->{"econnrefused"} = shift()) 0 0 0
415 }
416  
417 sub tcp_service_check
418 0 ▶{
Subroutine tcp_service_check: not called CC=1 SCAR=6.9
Pod documented
419 0 service_check(@_);
420 }
421  
422 # Description: Set exponential backoff for retransmission.
423 # Should be > 1 to retain exponential properties.
424 # If set to 0, retransmissions are disabled.
425  
426 sub retrans
427 0 ▶{
Subroutine retrans: not called CC=1 SCAR=6.9
Pod documented
428 0 my $self = shift;
429 0 $self->{retrans} = shift;
430 }
431  
432 0 ▶sub _IsAdminUser {
Subroutine _IsAdminUser: not called CC=5 SCAR=34.0
433 0 ▶ return unless $^O eq 'MSWin32' or $^O eq "cygwin";
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" or $^O eq "cygwin" 0 0 0
Condition0%0/3
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "cygwin"
$^O eq "MSWin32" $^O eq "cygwin"
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "cygwin"
ABresult
1X1
011
000
434 0 ▶ return unless eval { require Win32 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require Win32 } } 0 0 0
435 0 ▶ return unless defined &Win32::IsAdminUser;
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Win32::IsAdminUser 0 0 0
436 0 return Win32::IsAdminUser();
437 }
438  
439 4 ▶sub _isroot {
Subroutine _isroot: called CC=9 SCAR=37.0
440 4 ▶ if (($> and $^O ne 'VMS' and $^O ne 'cygwin')
Branch: 1 branch
BranchTrueFalseTotal
if ($> and $^O ne 'VMS' and $^O ne 'cygwin' or $^O eq 'MSWin32' || $^O eq 'cygwin' and not _IsAdminUser() or $^O eq 'VMS' and `write sys\$output f\$privilege("SYSPRV")` =~ /FALSE/) { } 4 0 4
Condition19%4/21
$> and $^O ne "VMS" and $^O ne "cygwin" or $^O eq "MSWin32" || $^O eq "cygwin" and not _IsAdminUser() or $^O eq "VMS" and `write sys\$output f\$privilege("SYSPRV")` =~ /FALSE/
l!l&&r!l&&!r
400
$> and $^O ne "VMS" and $^O ne "cygwin" or $^O eq "MSWin32" || $^O eq "cygwin" and not _IsAdminUser()
l!l&&r!l&&!r
400
$> and $^O ne "VMS" and $^O ne "cygwin"
!ll&&!rl&&r
004
$> and $^O ne "VMS"
!ll&&!rl&&r
004
$^O eq "MSWin32" || $^O eq "cygwin" and not _IsAdminUser()
!ll&&!rl&&r
000
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
000
$^O eq "VMS" and `write sys\$output f\$privilege("SYSPRV")` =~ /FALSE/
!ll&&!rl&&r
000
MC/DC0%0/8
$> and $^O ne "VMS" and $^O ne "cygwin" or $^O eq "MSWin32" || $^O eq "cygwin" and not _IsAdminUser() or $^O eq "VMS" and `write sys\$output f\$privilege("SYSPRV")` =~ /FALSE/
$> $^O ne "VMS" $^O ne "cygwin" $^O eq "MSWin32" $^O eq "cygwin" not _IsAdminUser() $^O eq "VMS" `write sys\$output f\$privilege("SYSPRV")` =~ /FALSE/
Truth table2%1/34
A and B and C or D or E and not(F) or G and H
A= $>B= $^O ne "VMS"C= $^O ne "cygwin"D= $^O eq "MSWin32"E= $^O eq "cygwin"F= not _IsAdminUser()G= $^O eq "VMS"H= `write sys\$output f\$privilege("SYSPRV")` =~ /FALSE/
ABCDEFGHresult
111XXXXX1
0XX1X1XX1
0XX011XX1
10X1X1XX1
10X011XX1
1101X1XX1
110011XX1
0XX00X111
0XX1X0111
0XX010111
10X00X111
10X1X0111
10X010111
11000X111
1101X0111
110010111
0XX00X0X0
0XX00X100
0XX1X00X0
0XX1X0100
0XX0100X0
0XX010100
10X00X0X0
10X00X100
10X1X00X0
10X1X0100
10X0100X0
10X010100
11000X0X0
11000X100
1101X00X0
1101X0100
1100100X0
110010100
441 or (($^O eq 'MSWin32' or $^O eq 'cygwin')
442 and !_IsAdminUser())
443 or ($^O eq 'VMS'
444 and (`write sys\$output f\$privilege("SYSPRV")` =~ m/FALSE/))) {
445 4 return 0;
446 }
447 else {
448 0 return 1;
449 }
450 }
451  
452 # Description: Sets ipv6 reachability
453 # REACHCONF was removed in RFC3542, ping6 -R supports it. requires root.
454  
455 sub IPV6_REACHCONF
456 0 ▶{
Subroutine IPV6_REACHCONF: not called CC=4 SCAR=30.0
Pod documented
457 0 my $self = shift;
458 0 my $on = shift;
459 0 ▶ if ($on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on) { } 0 0 0
460 0 my $reachconf = eval { Socket::IPV6_REACHCONF() };
461 0 ▶ if (!$reachconf) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reachconf) 0 0 0
462 0 carp "IPV6_REACHCONF not supported on this platform";
463 0 return 0;
464 }
465 0 ▶ if (!_isroot()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_isroot()) 0 0 0
466 0 carp "IPV6_REACHCONF requires root permissions";
467 0 return 0;
468 }
469 0 $self->{IPV6_REACHCONF} = 1;
470 }
471 else {
472 0 return $self->{IPV6_REACHCONF};
473 }
474 }
475  
476 # Description: set it on or off.
477  
478 sub IPV6_USE_MIN_MTU
479 0 ▶{
Subroutine IPV6_USE_MIN_MTU: not called CC=5 SCAR=34.0
Pod documented
480 0 my $self = shift;
481 0 my $on = shift;
482 0 ▶ if (defined $on) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on) { } 0 0 0
483 0 ▶ my $IPV6_USE_MIN_MTU = eval { Socket::IPV6_USE_MIN_MTU() } || 43;
Condition0%0/2
eval { do { Socket::IPV6_USE_MIN_MTU() } } || 43
l!l
00
MC/DC0%0/1
eval { do { Socket::IPV6_USE_MIN_MTU() } } || 43
eval { do { Socket::IPV6_USE_MIN_MTU() } }
Truth table0%0/2
A
A= eval { do { Socket::IPV6_USE_MIN_MTU() } }
Aresult
11
00
484 #if (!$IPV6_USE_MIN_MTU) {
485 # carp "IPV6_USE_MIN_MTU not supported on this platform";
486 # return 0;
487 #}
488 0 ▶ $self->{IPV6_USE_MIN_MTU} = $on ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 0 0 0
489 setsockopt($self->{fh}, $IPPROTO_IPV6, $IPV6_USE_MIN_MTU,
490 0 ▶ pack("I*", $self->{'IPV6_USE_MIN_MT'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $IPPROTO_IPV6, $IPV6_USE_MIN_MTU, pack("I*", $self->{"IPV6_USE_MIN_MT"}) or croak("error configuring IPV6_USE_MIN_MT} $!") 0 0 0
491 or croak "error configuring IPV6_USE_MIN_MT} $!";
492 }
493 else {
494 0 return $self->{IPV6_USE_MIN_MTU};
495 }
496 }
497  
498 # Description: notify an according MTU
499  
500 sub IPV6_RECVPATHMTU
501 0 ▶{
Subroutine IPV6_RECVPATHMTU: not called CC=4 SCAR=30.0
Pod documented
502 0 my $self = shift;
503 0 my $on = shift;
504 0 ▶ if ($on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on) { } 0 0 0
505 0 ▶ my $IPV6_RECVPATHMTU = eval { Socket::IPV6_RECVPATHMTU() } || 43;
Condition0%0/2
eval { do { Socket::IPV6_RECVPATHMTU() } } || 43
l!l
00
MC/DC0%0/1
eval { do { Socket::IPV6_RECVPATHMTU() } } || 43
eval { do { Socket::IPV6_RECVPATHMTU() } }
Truth table0%0/2
A
A= eval { do { Socket::IPV6_RECVPATHMTU() } }
Aresult
11
00
506 #if (!$RECVPATHMTU) {
507 # carp "IPV6_RECVPATHMTU not supported on this platform";
508 # return 0;
509 #}
510 0 $self->{IPV6_RECVPATHMTU} = 1;
511 setsockopt($self->{fh}, $IPPROTO_IPV6, $IPV6_RECVPATHMTU,
512 0 ▶ pack("I*", $self->{'IPV6_RECVPATHMTU'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, $IPPROTO_IPV6, $IPV6_RECVPATHMTU, pack("I*", $self->{"IPV6_RECVPATHMTU"}) or croak("error configuring IPV6_RECVPATHMTU} $!") 0 0 0
513 or croak "error configuring IPV6_RECVPATHMTU} $!";
514 }
515 else {
516 0 return $self->{IPV6_RECVPATHMTU};
517 }
518 }
519  
520 # Description: allows the module to use milliseconds as returned by
521 # the Time::HiRes module
522  
523 7 $hires = 1;
524 sub hires
525 0 ▶{
Subroutine hires: not called CC=4 SCAR=30.0
Pod documented
526 0 my $self = shift;
527 0 ▶ $hires = 1 unless defined
Branch: 2 branches
BranchTrueFalseTotal
unless defined($Net::Ping::hires = defined $self && ref $self ? shift() : $self) 0 0 0
defined $self && ref $self ? : 0 0 0
Condition0%0/3
defined $self and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self and ref $self
defined $self ref $self
Truth table0%0/3
A and B
A= defined $selfB= ref $self
ABresult
0X0
100
111
528 ($hires = ((defined $self) && (ref $self)) ? shift() : $self);
529 }
530  
531 sub time
532 0 ▶{
Subroutine time: not called CC=2 SCAR=17.9
Pod documented
533 0 ▶ return $hires ? Time::HiRes::time() : CORE::time();
Branch: 1 branch
BranchTrueFalseTotal
$Net::Ping::hires ? : 0 0 0
534 }
535  
536 # Description: Sets or clears the O_NONBLOCK flag on a file handle.
537 sub socket_blocking_mode
538 0 ▶{
Subroutine socket_blocking_mode: not called CC=8 SCAR=42.8
Pod documented
539 0 my ($self,
540 $fh, # the file handle whose flags are to be modified
541 $block) = @_; # if true then set the blocking
542 # mode (clear O_NONBLOCK), otherwise
543 # set the non-blocking mode (set O_NONBLOCK)
544  
545 0 my $flags;
546 0 ▶ if ($^O eq 'MSWin32' || $^O eq 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "VMS") 0 0 0
Condition0%0/3
$^O eq "MSWin32" or $^O eq "VMS"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "VMS"
$^O eq "MSWin32" $^O eq "VMS"
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "VMS"
ABresult
1X1
011
000
547 # FIONBIO enables non-blocking sockets on windows and vms.
548 # FIONBIO is (0x80000000|(4<<16)|(ord('f')<<8)|126), as per winsock.h, ioctl.h
549 0 my $f = 0x8004667e;
550 0 ▶ my $v = pack("L", $block ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 0 0 0
551 0 ▶ ioctl($fh, $f, $v) or croak("ioctl failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
ioctl $fh, $f, $v or croak("ioctl failed: $!") 0 0 0
552 0 return;
553 }
554 0 ▶ if ($flags = fcntl($fh, F_GETFL, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags = fcntl($fh, 3, 0)) { } 0 0 0
555 0 ▶ $flags = $block ? ($flags & ~O_NONBLOCK) : ($flags | O_NONBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
$block ? : 0 0 0
556 0 ▶ if (!fcntl($fh, F_SETFL, $flags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not fcntl($fh, 4, $flags)) 0 0 0
557 0 croak("fcntl F_SETFL: $!");
558 }
559 } else {
560 0 croak("fcntl F_GETFL: $!");
561 }
562 }
563  
564 # Description: Ping a host name or IP number with an optional timeout.
565 # First lookup the host, and return undef if it is not found. Otherwise
566 # perform the specific ping method based on the protocol. Return the
567 # result of the ping.
568  
569 sub ping
570 0 ▶{
Subroutine ping: not called CC=19 SCAR=59.4
Pod documented
571 0 my ($self,
572 $host, # Name or IP number of host to ping
573 $timeout, # Seconds after which ping times out
574 $family, # Address family
575 ) = @_;
576 0 my ($ip, # Hash of addr (string), addr_in (packed), family
577 $ret, # The return value
578 $ping_time, # When ping began
579 );
580  
581 0 ▶ $host = $self->{host} if !defined $host and $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $host and $self->{"host"} 0 0 0
Condition0%0/3
not defined $host and $self->{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $host and $self->{"host"}
not defined $host $self->{"host"}
Truth table0%0/3
not(A) and B
A= not defined $hostB= $self->{"host"}
ABresult
0X0
100
111
582 0 ▶ croak("Usage: \$p->ping([ \$host [, \$timeout [, \$family]]])") if @_ > 4 or !$host;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 4 or not $host 0 0 0
Condition0%0/3
@_ > 4 or not $host
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ > 4 or not $host
@_ > 4 not $host
Truth table0%0/3
A or not(B)
A= @_ > 4B= not $host
ABresult
1X1
011
000
583 0 ▶ $timeout = $self->{timeout} unless $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout 0 0 0
584 0 ▶ croak("Timeout must be greater than 0 seconds") if $timeout <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 0 0
585  
586 0 ▶ if ($family) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family) { } 0 0 0
587 0 ▶ if ($family =~ $qr_family) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /$qr_family/) { } 0 0 0
588 0 ▶ if ($family =~ $qr_family4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /$qr_family4/) { } 0 0 0
589 0 $self->{family_local} = AF_INET;
590 } else {
591 0 $self->{family_local} = $AF_INET6;
592 }
593 } else {
594 0 croak('Family must be "ipv4" or "ipv6"')
595 }
596 } else {
597 0 $self->{family_local} = $self->{family};
598 }
599
600 0 $ip = $self->_resolv($host);
601 0 ▶ return () unless defined($ip); # Does host exist?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip 0 0 0
602  
603 # Dispatch to the appropriate routine.
604 0 $ping_time = &time();
605 0 ▶ if ($self->{proto} eq "external") {
Branch: 7 branches
BranchTrueFalseTotal
if ($self->{'proto'} eq 'external') { } 0 0 0
elsif ($self->{'proto'} eq 'udp') { } 0 0 0
elsif ($self->{'proto'} eq 'icmp') { } 0 0 0
elsif ($self->{'proto'} eq 'icmpv6') { } 0 0 0
elsif ($self->{'proto'} eq 'tcp') { } 0 0 0
elsif ($self->{'proto'} eq 'stream') { } 0 0 0
elsif ($self->{'proto'} eq 'syn') { } 0 0 0
606 0 $ret = $self->ping_external($ip, $timeout);
607 }
608 elsif ($self->{proto} eq "udp") {
609 0 $ret = $self->ping_udp($ip, $timeout);
610 }
611 elsif ($self->{proto} eq "icmp") {
612 0 $ret = $self->ping_icmp($ip, $timeout);
613 }
614 elsif ($self->{proto} eq "icmpv6") {
615 0 $ret = $self->ping_icmpv6($ip, $timeout);
616 }
617 elsif ($self->{proto} eq "tcp") {
618 0 $ret = $self->ping_tcp($ip, $timeout);
619 }
620 elsif ($self->{proto} eq "stream") {
621 0 $ret = $self->ping_stream($ip, $timeout);
622 }
623 elsif ($self->{proto} eq "syn") {
624 0 $ret = $self->ping_syn($host, $ip, $ping_time, $ping_time+$timeout);
625 } else {
626 0 croak("Unknown protocol \"$self->{proto}\" in ping()");
627 }
628  
629 0 ▶ return wantarray ? ($ret, &time() - $ping_time, $self->ntop($ip)) : $ret;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
630 }
631  
632 # Uses Net::Ping::External to do an external ping.
633 0 ▶sub ping_external {
Subroutine ping_external: not called CC=8 SCAR=42.8
Pod documented
634 0 my ($self,
635 $ip, # Hash of addr (string), addr_in (packed), family
636 $timeout, # Seconds after which ping times out
637 $family
638 ) = @_;
639  
640 0 ▶ $ip = $self->{host} if !defined $ip and $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip and $self->{"host"} 0 0 0
Condition0%0/3
not defined $ip and $self->{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ip and $self->{"host"}
not defined $ip $self->{"host"}
Truth table0%0/3
not(A) and B
A= not defined $ipB= $self->{"host"}
ABresult
0X0
100
111
641 0 ▶ $timeout = $self->{timeout} if !defined $timeout and $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and $self->{"timeout"} 0 0 0
Condition0%0/3
not defined $timeout and $self->{"timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $timeout and $self->{"timeout"}
not defined $timeout $self->{"timeout"}
Truth table0%0/3
not(A) and B
A= not defined $timeoutB= $self->{"timeout"}
ABresult
0X0
100
111
642 my @addr = exists $ip->{addr_in}
643 ? ('ip' => $ip->{addr_in})
644 0 ▶ : ('host' => $ip->{host});
Branch: 1 branch
BranchTrueFalseTotal
exists $ip->{'addr_in'} ? : 0 0 0
645  
646 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { local(@INC) = @INC; pop @INC if $INC[-1] eq "."; require Net::Ping::External } } or croak("Protocol \"external\" not supported on your system: Net::Ping::External not found") 0 0 0
647 0 local @INC = @INC;
648 0 ▶ pop @INC if $INC[-1] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 0 0
649 0 require Net::Ping::External;
650 } or croak('Protocol "external" not supported on your system: Net::Ping::External not found');
651 0 return Net::Ping::External::ping(@addr, timeout => $timeout,
652 family => $family);
653 }
654  
655 # h2ph "asm/socket.h"
656 # require "asm/socket.ph";
657 7 ▶use constant SO_BINDTODEVICE => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
658 7 ▶use constant ICMP_ECHOREPLY => 0; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
659 7 ▶use constant ICMPv6_ECHOREPLY => 129; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
660 7 ▶use constant ICMP_UNREACHABLE => 3; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
661 7 ▶use constant ICMPv6_UNREACHABLE => 1; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
662 7 ▶use constant ICMPv6_NI_REPLY => 140; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
663 7 ▶use constant ICMP_ECHO => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
664 7 ▶use constant ICMPv6_ECHO => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
665 7 ▶use constant ICMP_TIME_EXCEEDED => 11; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
666 7 ▶use constant ICMP_PARAMETER_PROBLEM => 12; # ICMP packet types
Subroutine BEGIN: called CC=1 SCAR=0.0
667 7 ▶use constant ICMP_TIMESTAMP => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
668 7 ▶use constant ICMP_TIMESTAMP_REPLY => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
669 7 ▶use constant ICMP_STRUCT => "C2 n3 A"; # Structure of a minimal ICMP packet
Subroutine BEGIN: called CC=1 SCAR=0.0
670 7 ▶use constant ICMP_TIMESTAMP_STRUCT => "C2 n3 N3"; # Structure of a minimal timestamp ICMP packet
Subroutine BEGIN: called CC=1 SCAR=0.0
671 7 ▶use constant SUBCODE => 0; # No ICMP subcode for ECHO and ECHOREPLY
Subroutine BEGIN: called CC=1 SCAR=0.0
672 7 ▶use constant ICMP_FLAGS => 0; # No special flags for send or recv
Subroutine BEGIN: called CC=1 SCAR=0.0
673 7 ▶use constant ICMP_PORT => 0; # No port with ICMP
Subroutine BEGIN: called CC=1 SCAR=0.0
674 7 ▶use constant IP_MTU_DISCOVER => 10; # linux only
Subroutine BEGIN: called CC=1 SCAR=0.0
675  
676 sub message_type
677 0 ▶{
Subroutine message_type: not called CC=5 SCAR=34.0
Pod documented
678 0 my ($self,
679 $type
680 ) = @_;
681  
682 croak "Setting message type only supported on 'icmp' protocol"
683 0 ▶ unless $self->{proto} eq 'icmp';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"proto"} eq "icmp" 0 0 0
684  
685 0 ▶ return $self->{message_type} || 'echo'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 0 0
Condition0%0/2
$self->{'message_type'} || 'echo'
l!l
00
MC/DC0%0/1
$self->{'message_type'} || 'echo'
$self->{'message_type'}
Truth table0%0/2
A
A= $self->{'message_type'}
Aresult
11
00
686 unless defined($type);
687  
688 0 ▶ croak "Supported icmp message type are limited to 'echo' and 'timestamp': '$type' not supported"
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^echo|timestamp$/i 0 0 0
689 unless $type =~ /^echo|timestamp$/i;
690  
691 0 $self->{message_type} = lc($type);
692 }
693  
694 sub ping_icmp
695 0 ▶{
Subroutine ping_icmp: not called CC=43 SCAR=75.5
Pod documented
696 0 my ($self,
697 $ip, # Hash of addr (string), addr_in (packed), family
698 $timeout # Seconds after which ping times out
699 ) = @_;
700  
701 0 my ($saddr, # sockaddr_in with port and ip
702 $checksum, # Checksum of ICMP packet
703 $msg, # ICMP packet to send
704 $len_msg, # Length of $msg
705 $rbits, # Read bits, filehandles for reading
706 $nfound, # Number of ready filehandles found
707 $finish_time, # Time ping should be finished
708 $done, # set to 1 when we are done
709 $ret, # Return value
710 $recv_msg, # Received message including IP header
711 $recv_msg_len, # Length of recevied message, less any additional data
712 $from_saddr, # sockaddr_in of sender
713 $from_port, # Port packet was sent from
714 $from_ip, # Packed IP of sender
715 $timestamp_msg, # ICMP timestamp message type
716 $from_type, # ICMP type
717 $from_subcode, # ICMP subcode
718 $from_chk, # ICMP packet checksum
719 $from_pid, # ICMP packet id
720 $from_seq, # ICMP packet sequence
721 $from_msg # ICMP message
722 );
723  
724 0 ▶ $ip = $self->{host} if !defined $ip and $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip and $self->{"host"} 0 0 0
Condition0%0/3
not defined $ip and $self->{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ip and $self->{"host"}
not defined $ip $self->{"host"}
Truth table0%0/3
not(A) and B
A= not defined $ipB= $self->{"host"}
ABresult
0X0
100
111
725 0 ▶ $timeout = $self->{timeout} if !defined $timeout and $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and $self->{"timeout"} 0 0 0
Condition0%0/3
not defined $timeout and $self->{"timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $timeout and $self->{"timeout"}
not defined $timeout $self->{"timeout"}
Truth table0%0/3
not(A) and B
A= not defined $timeoutB= $self->{"timeout"}
ABresult
0X0
100
111
726 0 ▶ $timestamp_msg = $self->{message_type} && $self->{message_type} eq 'timestamp' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'message_type'} && $self->{'message_type'} eq 'timestamp' ? : 0 0 0
Condition0%0/3
$self->{"message_type"} and $self->{"message_type"} eq "timestamp"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"message_type"} and $self->{"message_type"} eq "timestamp"
$self->{"message_type"} $self->{"message_type"} eq "timestamp"
Truth table0%0/3
A and B
A= $self->{"message_type"}B= $self->{"message_type"} eq "timestamp"
ABresult
0X0
100
111
727  
728 0 ▶ if ($^O eq 'linux' and !_isroot()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'linux' and not _isroot()) { } 0 0 0
Condition0%0/3
$^O eq "linux" and not _isroot()
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "linux" and not _isroot()
$^O eq "linux" not _isroot()
Truth table0%0/3
A and not(B)
A= $^O eq "linux"B= not _isroot()
ABresult
0X0
100
111
729 0 ▶ socket($self->{fh}, $ip->{family}, SOCK_DGRAM, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, $ip->{"family"}, 2, $self->{"proto_num"} or croak("icmp socket error - $!") 0 0 0
730 croak("icmp socket error - $!");
731 } else {
732 0 ▶ socket($self->{fh}, $ip->{family}, SOCK_RAW, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, $ip->{"family"}, 3, $self->{"proto_num"} or croak("icmp socket error - $!") 0 0 0
733 croak("icmp socket error - $!");
734 }
735  
736 0 ▶ if (defined $self->{local_addr} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))) 0 0 0
Condition0%0/3
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
defined $self->{"local_addr"} not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
Truth table0%0/3
A and not(B)
A= defined $self->{"local_addr"}B= not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
ABresult
0X0
100
111
737 !CORE::bind($self->{fh}, _pack_sockaddr_in(0, $self->{local_addr}))) {
738 0 croak("icmp bind error - $!");
739 }
740 0 $self->_setopts();
741  
742 0 $self->{seq} = ($self->{seq} + 1) % 65536; # Increment sequence
743 0 $checksum = 0; # No checksum for starters
744 0 ▶ if ($ip->{family} == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->{'family'} == 2) { } 0 0 0
745 0 ▶ if ($timestamp_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timestamp_msg) { } 0 0 0
746 $msg = pack(ICMP_TIMESTAMP_STRUCT, ICMP_TIMESTAMP, SUBCODE,
747 0 $checksum, $self->{pid}, $self->{seq}, 0, 0, 0);
748 } else {
749 $msg = pack(ICMP_STRUCT . $self->{data_size}, ICMP_ECHO, SUBCODE,
750 0 $checksum, $self->{pid}, $self->{seq}, $self->{data});
751 }
752 } else {
753 # how to get SRC
754 0 my $pseudo_header = pack('a16a16Nnn', $ip->{addr_in}, $ip->{addr_in}, 8+length($self->{data}), 0, 0x003a);
755 $msg = pack(ICMP_STRUCT . $self->{data_size}, ICMPv6_ECHO, SUBCODE,
756 0 $checksum, $self->{pid}, $self->{seq}, $self->{data});
757 0 $msg = $pseudo_header.$msg
758 }
759 0 $checksum = Net::Ping->checksum($msg);
760 0 ▶ if ($ip->{family} == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip->{'family'} == 2) { } 0 0 0
761 0 ▶ if ($timestamp_msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timestamp_msg) { } 0 0 0
762 $msg = pack(ICMP_TIMESTAMP_STRUCT, ICMP_TIMESTAMP, SUBCODE,
763 0 $checksum, $self->{pid}, $self->{seq}, 0, 0, 0);
764 } else {
765 $msg = pack(ICMP_STRUCT . $self->{data_size}, ICMP_ECHO, SUBCODE,
766 0 $checksum, $self->{pid}, $self->{seq}, $self->{data});
767 }
768 } else {
769 $msg = pack(ICMP_STRUCT . $self->{data_size}, ICMPv6_ECHO, SUBCODE,
770 0 $checksum, $self->{pid}, $self->{seq}, $self->{data});
771 }
772 0 $len_msg = length($msg);
773 0 $saddr = _pack_sockaddr_in(ICMP_PORT, $ip);
774 0 $self->{from_ip} = undef;
775 0 $self->{from_type} = undef;
776 0 $self->{from_subcode} = undef;
777 0 send($self->{fh}, $msg, ICMP_FLAGS, $saddr); # Send the message
778  
779 0 $rbits = "";
780 0 vec($rbits, $self->{fh}->fileno(), 1) = 1;
781 0 $ret = 0;
782 0 $done = 0;
783 0 $finish_time = &time() + $timeout; # Must be done by this time
784 0 ▶ while (!$done && $timeout > 0) # Keep trying if we have time
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and $timeout > 0) 0 0 0
Condition0%0/3
not $done and $timeout > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $done and $timeout > 0
not $done $timeout > 0
Truth table0%0/3
not(A) and B
A= not $doneB= $timeout > 0
ABresult
0X0
100
111
785 {
786 0 $nfound = mselect((my $rout=$rbits), undef, undef, $timeout); # Wait for packet
787 0 $timeout = $finish_time - &time(); # Get remaining time
788 0 ▶ if (!defined($nfound)) # Hmm, a strange error
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $nfound) { } 0 0 0
elsif ($nfound) { } 0 0 0
789 {
790 0 $ret = undef;
791 0 $done = 1;
792 }
793 elsif ($nfound) # Got a packet from somewhere
794 {
795 0 $recv_msg = "";
796 0 $from_pid = -1;
797 0 $from_seq = -1;
798 0 $from_saddr = recv($self->{fh}, $recv_msg, 1500, ICMP_FLAGS);
799 0 $recv_msg_len = length($recv_msg) - length($self->{data});
800 0 ($from_port, $from_ip) = _unpack_sockaddr_in($from_saddr, $ip->{family});
801 # ICMP echo includes the header and ICMPv6 doesn't.
802 # IPv4 length($recv_msg) is 28 (20 header + 8 payload)
803 # while IPv6 length is only 8 (sans header).
804 0 ▶ my $off = ($ip->{family} == AF_INET) ? 20 : 0; # payload offset
Branch: 1 branch
BranchTrueFalseTotal
$ip->{'family'} == 2 ? : 0 0 0
805 0 ($from_type, $from_subcode) = unpack("C2", substr($recv_msg, $off, 2));
806 0 ▶ if ($from_type == ICMP_TIMESTAMP_REPLY) {
Branch: 3 branches
BranchTrueFalseTotal
if ($from_type == 14) { } 0 0 0
elsif ($from_type == 0 or $from_type == 129) { } 0 0 0
elsif ($from_type == 140) { } 0 0 0
807 0 ▶ ($from_pid, $from_seq) = unpack("n3", substr($recv_msg, $off + 4, 4))
Branch: 1 branch
BranchTrueFalseTotal
if length $recv_msg >= $off + 8 0 0 0
808 if length $recv_msg >= $off + 8;
809 ▶ } elsif ($from_type == ICMP_ECHOREPLY || $from_type == ICMPv6_ECHOREPLY) {
Condition0%0/3
$from_type == 0 or $from_type == 129
l!l&&r!l&&!r
000
MC/DC0%0/2
$from_type == 0 or $from_type == 129
$from_type == 0 $from_type == 129
Truth table0%0/3
A or B
A= $from_type == 0B= $from_type == 129
ABresult
1X1
011
000
810 #warn "ICMP_ECHOREPLY: ", $ip->{family}, " ",$recv_msg, ":", length($recv_msg);
811 0 ▶ ($from_pid, $from_seq) = unpack("n2", substr($recv_msg, $off + 4, 4))
Branch: 1 branch
BranchTrueFalseTotal
if $recv_msg_len == $off + 8 0 0 0
812 if $recv_msg_len == $off + 8;
813 } elsif ($from_type == ICMPv6_NI_REPLY) {
814 ($from_pid, $from_seq) = unpack("n2", substr($recv_msg, 4, 4))
815 0 ▶ if ($ip->{family} == $AF_INET6 && length $recv_msg == 8);
Branch: 1 branch
BranchTrueFalseTotal
if $ip->{"family"} == $AF_INET6 and length $recv_msg == 8 0 0 0
Condition0%0/3
$ip->{"family"} == $AF_INET6 and length $recv_msg == 8
!ll&&!rl&&r
000
MC/DC0%0/2
$ip->{"family"} == $AF_INET6 and length $recv_msg == 8
$ip->{"family"} == $AF_INET6 length $recv_msg == 8
Truth table0%0/3
A and B
A= $ip->{"family"} == $AF_INET6B= length $recv_msg == 8
ABresult
0X0
100
111
816 } else {
817 #warn "ICMP: ", $from_type, " ",$ip->{family}, " ",$recv_msg, ":", length($recv_msg);
818 0 ▶ ($from_pid, $from_seq) = unpack("n2", substr($recv_msg, $off + 32, 4))
Branch: 1 branch
BranchTrueFalseTotal
if length $recv_msg >= $off + 36 0 0 0
819 if length $recv_msg >= $off + 36;
820 }
821 0 $self->{from_ip} = $from_ip;
822 0 $self->{from_type} = $from_type;
823 0 $self->{from_subcode} = $from_subcode;
824 0 ▶ next if ($from_pid != $self->{pid});
Branch: 1 branch
BranchTrueFalseTotal
if $from_pid != $self->{"pid"} 0 0 0
825 0 ▶ next if ($from_seq != $self->{seq});
Branch: 1 branch
BranchTrueFalseTotal
if $from_seq != $self->{"seq"} 0 0 0
826 0 ▶ if (! $source_verify || ($self->ntop($from_ip) eq $self->ntop($ip))) { # Does the packet check out?
Branch: 1 branch
BranchTrueFalseTotal
if (not $Net::Ping::source_verify or $self->ntop($from_ip) eq $self->ntop($ip)) 0 0 0
Condition0%0/3
not $Net::Ping::source_verify or $self->ntop($from_ip) eq $self->ntop($ip)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $Net::Ping::source_verify or $self->ntop($from_ip) eq $self->ntop($ip)
not $Net::Ping::source_verify $self->ntop($from_ip) eq $self->ntop($ip)
Truth table0%0/3
not(A) or B
A= not $Net::Ping::source_verifyB= $self->ntop($from_ip) eq $self->ntop($ip)
ABresult
1X1
011
000
827 0 ▶ if (!$timestamp_msg && (($from_type == ICMP_ECHOREPLY) || ($from_type == ICMPv6_ECHOREPLY))) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $timestamp_msg and $from_type == 0 || $from_type == 129) { } 0 0 0
elsif ($timestamp_msg and $from_type == 14) { } 0 0 0
elsif ($from_type == 3 or $from_type == 1) { } 0 0 0
elsif ($from_type == 11) { } 0 0 0
Condition0%0/6
not $timestamp_msg and $from_type == 0 || $from_type == 129
!ll&&!rl&&r
000
$from_type == 0 or $from_type == 129
l!l&&r!l&&!r
000
MC/DC0%0/3
not $timestamp_msg and $from_type == 0 || $from_type == 129
not $timestamp_msg $from_type == 0 $from_type == 129
Truth table0%0/4
not(A) and B or C
A= not $timestamp_msgB= $from_type == 0C= $from_type == 129
ABCresult
0XX0
1000
11X1
1011
828 0 $ret = 1;
829 0 $done = 1;
830 ▶ } elsif ($timestamp_msg && $from_type == ICMP_TIMESTAMP_REPLY) {
Condition0%0/3
$timestamp_msg and $from_type == 14
!ll&&!rl&&r
000
MC/DC0%0/2
$timestamp_msg and $from_type == 14
$timestamp_msg $from_type == 14
Truth table0%0/3
A and B
A= $timestamp_msgB= $from_type == 14
ABresult
0X0
100
111
831 0 $ret = 1;
832 0 $done = 1;
833 ▶ } elsif (($from_type == ICMP_UNREACHABLE) || ($from_type == ICMPv6_UNREACHABLE)) {
Condition0%0/3
$from_type == 3 or $from_type == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$from_type == 3 or $from_type == 1
$from_type == 3 $from_type == 1
Truth table0%0/3
A or B
A= $from_type == 3B= $from_type == 1
ABresult
1X1
011
000
834 0 $done = 1;
835 } elsif ($from_type == ICMP_TIME_EXCEEDED) {
836 0 $ret = 0;
837 0 $done = 1;
838 }
839 }
840 } else { # Oops, timed out
841 0 $done = 1;
842 }
843 }
844 0 return $ret;
845 }
846  
847 sub ping_icmpv6
848 0 ▶{
Subroutine ping_icmpv6: not called CC=1 SCAR=6.9
Pod documented
849 0 shift->ping_icmp(@_);
850 }
851  
852 0 ▶sub icmp_result {
Subroutine icmp_result: not called CC=5 SCAR=34.0
Pod documented
853 0 my ($self) = @_;
854 0 ▶ my $addr = $self->{from_ip} || "";
Condition0%0/2
$self->{'from_ip'} || ''
l!l
00
MC/DC0%0/1
$self->{'from_ip'} || ''
$self->{'from_ip'}
Truth table0%0/2
A
A= $self->{'from_ip'}
Aresult
11
00
855 0 ▶ $addr = "\0\0\0\0" unless 4 == length $addr;
Branch: 1 branch
BranchTrueFalseTotal
unless 4 == length $addr 0 0 0
856 0 ▶ return ($self->ntop($addr),($self->{from_type} || 0), ($self->{from_subcode} || 0));
Condition0%0/4
$self->{'from_type'} || 0
l!l
00
$self->{'from_subcode'} || 0
l!l
00
MC/DC0%0/1
$self->{'from_type'} || 0
$self->{'from_type'}
MC/DC0%0/1
$self->{'from_subcode'} || 0
$self->{'from_subcode'}
Truth table0%0/2
A
A= $self->{'from_type'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'from_subcode'}
Aresult
11
00
857 }
858  
859 # Description: Do a checksum on the message. Basically sum all of
860 # the short words and fold the high order bits into the low order bits.
861  
862 sub checksum
863 0 ▶{
Subroutine checksum: not called CC=3 SCAR=24.8
Pod documented
864 0 my ($class,
865 $msg # The message to checksum
866 ) = @_;
867 0 my ($len_msg, # Length of the message
868 $num_short, # The number of short words in the message
869 $short, # One short word
870 $chk # The checksum
871 );
872  
873 0 $len_msg = length($msg);
874 0 $num_short = int($len_msg / 2);
875 0 $chk = 0;
876 0 foreach $short (unpack("n$num_short", $msg))
877 {
878 0 $chk += $short;
879 } # Add the odd byte in
880 0 ▶ $chk += (unpack("C", substr($msg, $len_msg - 1, 1)) << 8) if $len_msg % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $len_msg % 2 0 0 0
881 0 $chk = ($chk >> 16) + ($chk & 0xffff); # Fold high into low
882 0 return(~(($chk >> 16) + $chk) & 0xffff); # Again and complement
883 }
884  
885  
886 # Description: Perform a tcp echo ping. Since a tcp connection is
887 # host specific, we have to open and close each connection here. We
888 # can't just leave a socket open. Because of the robust nature of
889 # tcp, it will take a while before it gives up trying to establish a
890 # connection. Therefore, we use select() on a non-blocking socket to
891 # check against our timeout. No data bytes are actually
892 # sent since the successful establishment of a connection is proof
893 # enough of the reachability of the remote host. Also, tcp is
894 # expensive and doesn't need our help to add to the overhead.
895  
896 sub ping_tcp
897 0 ▶{
Subroutine ping_tcp: not called CC=7 SCAR=40.3
Pod documented
898 0 my ($self,
899 $ip, # Hash of addr (string), addr_in (packed), family
900 $timeout # Seconds after which ping times out
901 ) = @_;
902 0 my ($ret # The return value
903 );
904  
905 0 ▶ $ip = $self->{host} if !defined $ip and $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip and $self->{"host"} 0 0 0
Condition0%0/3
not defined $ip and $self->{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ip and $self->{"host"}
not defined $ip $self->{"host"}
Truth table0%0/3
not(A) and B
A= not defined $ipB= $self->{"host"}
ABresult
0X0
100
111
906 0 ▶ $timeout = $self->{timeout} if !defined $timeout and $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and $self->{"timeout"} 0 0 0
Condition0%0/3
not defined $timeout and $self->{"timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $timeout and $self->{"timeout"}
not defined $timeout $self->{"timeout"}
Truth table0%0/3
not(A) and B
A= not defined $timeoutB= $self->{"timeout"}
ABresult
0X0
100
111
907  
908 0 $! = 0;
909 0 $ret = $self -> tcp_connect( $ip, $timeout);
910 0 ▶ if (!$self->{econnrefused} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"econnrefused"} and $! == 111) 0 0 0
Condition0%0/3
not $self->{"econnrefused"} and $! == 111
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"econnrefused"} and $! == 111
not $self->{"econnrefused"} $! == 111
Truth table0%0/3
not(A) and B
A= not $self->{"econnrefused"}B= $! == 111
ABresult
0X0
100
111
911 $! == ECONNREFUSED) {
912 0 $ret = 1; # "Connection refused" means reachable
913 }
914 0 $self->{fh}->close();
915 0 return $ret;
916 }
917  
918 sub tcp_connect
919 0 ▶{
Subroutine tcp_connect: not called CC=14 SCAR=53.5
Pod documented
920 0 my ($self,
921 $ip, # Hash of addr (string), addr_in (packed), family
922 $timeout # Seconds after which connect times out
923 ) = @_;
924 0 my ($saddr); # Packed IP and Port
925  
926 0 ▶ $ip = $self->{host} if !defined $ip and $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ip and $self->{"host"} 0 0 0
Condition0%0/3
not defined $ip and $self->{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ip and $self->{"host"}
not defined $ip $self->{"host"}
Truth table0%0/3
not(A) and B
A= not defined $ipB= $self->{"host"}
ABresult
0X0
100
111
927 0 ▶ $timeout = $self->{timeout} if !defined $timeout and $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and $self->{"timeout"} 0 0 0
Condition0%0/3
not defined $timeout and $self->{"timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $timeout and $self->{"timeout"}
not defined $timeout $self->{"timeout"}
Truth table0%0/3
not(A) and B
A= not defined $timeoutB= $self->{"timeout"}
ABresult
0X0
100
111
928  
929 0 $saddr = _pack_sockaddr_in($self->{port_num}, $ip);
930  
931 0 my $ret = 0; # Default to unreachable
932  
933 0 ▶ my $do_socket = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
934 0 ▶ socket($self->{fh}, $ip->{family}, SOCK_STREAM, $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, $ip->{"family"}, 1, $self->{"proto_num"} or croak("tcp socket error - $!") 0 0 0
935 croak("tcp socket error - $!");
936 0 ▶ if (defined $self->{local_addr} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))) 0 0 0
Condition0%0/3
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
defined $self->{"local_addr"} not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
Truth table0%0/3
A and not(B)
A= defined $self->{"local_addr"}B= not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
ABresult
0X0
100
111
937 !CORE::bind($self->{fh}, _pack_sockaddr_in(0, $self->{local_addr}))) {
938 0 croak("tcp bind error - $!");
939 }
940 0 $self->_setopts();
941 0 };
942 0 ▶ my $do_connect = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
943 0 $self->{ip} = $ip->{addr_in};
944 # ECONNREFUSED is 10061 on MSWin32. If we pass it as child error through $?,
945 # we'll get (10061 & 255) = 77, so we cannot check it in the parent process.
946 0 ▶ return ($ret = connect($self->{fh}, $saddr) || ($! == ECONNREFUSED && !$self->{econnrefused}));
Condition0%0/6
connect($self->{'fh'}, $saddr) || $! == 111 && !$self->{'econnrefused'}
l!l&&r!l&&!r
000
$! == 111 and not $self->{"econnrefused"}
!ll&&!rl&&r
000
MC/DC0%0/3
connect($self->{'fh'}, $saddr) || $! == 111 && !$self->{'econnrefused'}
connect($self->{'fh'}, $saddr) $! == 111 not $self->{"econnrefused"}
Truth table0%0/4
A || B and not(C)
A= connect($self->{'fh'}, $saddr)B= $! == 111C= not $self->{"econnrefused"}
ABCresult
1XX1
0111
00X0
0100
947 0 };
948 0 ▶ my $do_connect_nb = sub {
Subroutine __ANON__: not called CC=19 SCAR=59.4
949 # Set O_NONBLOCK property on filehandle
950 0 $self->socket_blocking_mode($self->{fh}, 0);
951  
952 # start the connection attempt
953 0 ▶ if (!connect($self->{fh}, $saddr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not connect($self->{'fh'}, $saddr)) { } 0 0 0
954 0 ▶ if ($! == ECONNREFUSED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! == 111) { } 0 0 0
elsif ($! != 115 and $^O ne 'MSWin32' || $! != 11) { } 0 0 0
955 0 ▶ $ret = 1 unless $self->{econnrefused};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"econnrefused"} 0 0 0
956 ▶ } elsif ($! != EINPROGRESS && ($^O ne 'MSWin32' || $! != EWOULDBLOCK)) {
Condition0%0/6
$! != 115 and $^O ne "MSWin32" || $! != 11
!ll&&!rl&&r
000
$^O ne "MSWin32" or $! != 11
l!l&&r!l&&!r
000
MC/DC0%0/3
$! != 115 and $^O ne "MSWin32" || $! != 11
$! != 115 $^O ne "MSWin32" $! != 11
Truth table0%0/4
A and B or C
A= $! != 115B= $^O ne "MSWin32"C= $! != 11
ABCresult
0XX0
1000
11X1
1011
957 # EINPROGRESS is the expected error code after a connect()
958 # on a non-blocking socket. But if the kernel immediately
959 # determined that this connect() will never work,
960 # Simply respond with "unreachable" status.
961 # (This can occur on some platforms with errno
962 # EHOSTUNREACH or ENETUNREACH.)
963 0 return 0;
964 } else {
965 # Got the expected EINPROGRESS.
966 # Just wait for connection completion...
967 0 my ($wbits, $wout, $wexc);
968 0 $wout = $wexc = $wbits = "";
969 0 vec($wbits, $self->{fh}->fileno, 1) = 1;
970  
971 0 ▶ my $nfound = mselect(undef,
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
972 ($wout = $wbits),
973 ($^O eq 'MSWin32' ? ($wexc = $wbits) : undef),
974 $timeout);
975 0 ▶ warn("select: $!") unless defined $nfound;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nfound 0 0 0
976  
977 0 ▶ if ($nfound && vec($wout, $self->{fh}->fileno, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound and vec $wout, $self->{'fh'}->fileno, 1) { } 0 0 0
Condition0%0/3
$nfound and vec $wout, $self->{"fh"}->fileno, 1
!ll&&!rl&&r
000
MC/DC0%0/2
$nfound and vec $wout, $self->{"fh"}->fileno, 1
$nfound vec $wout, $self->{"fh"}->fileno, 1
Truth table0%0/3
A and B
A= $nfoundB= vec $wout, $self->{"fh"}->fileno, 1
ABresult
0X0
100
111
978 # the socket is ready for writing so the connection
979 # attempt completed. test whether the connection
980 # attempt was successful or not
981  
982 0 ▶ if (getpeername($self->{fh})) {
Branch: 1 branch
BranchTrueFalseTotal
if (getpeername $self->{'fh'}) { } 0 0 0
983 # Connection established to remote host
984 0 $ret = 1;
985 } else {
986 # TCP ACK will never come from this host
987 # because there was an error connecting.
988  
989 # This should set $! to the correct error.
990 0 my $char;
991 0 sysread($self->{fh},$char,1);
992 0 ▶ $! = ECONNREFUSED if ($! == EAGAIN && $^O =~ /cygwin/i);
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 and $^O =~ /cygwin/i 0 0 0
Condition0%0/3
$! == 11 and $^O =~ /cygwin/i
!ll&&!rl&&r
000
MC/DC0%0/2
$! == 11 and $^O =~ /cygwin/i
$! == 11 $^O =~ /cygwin/i
Truth table0%0/3
A and B
A= $! == 11B= $^O =~ /cygwin/i
ABresult
0X0
100
111
993  
994 $ret = 1 if (!$self->{econnrefused}
995 0 ▶ && $! == ECONNREFUSED);
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"econnrefused"} and $! == 111 0 0 0
Condition0%0/3
not $self->{"econnrefused"} and $! == 111
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"econnrefused"} and $! == 111
not $self->{"econnrefused"} $! == 111
Truth table0%0/3
not(A) and B
A= not $self->{"econnrefused"}B= $! == 111
ABresult
0X0
100
111
996 }
997 } else {
998 # the connection attempt timed out (or there were connect
999 # errors on Windows)
1000 0 ▶ if ($^O =~ 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/) 0 0 0
1001 # If the connect will fail on a non-blocking socket,
1002 # winsock reports ECONNREFUSED as an exception, and we
1003 # need to fetch the socket-level error code via getsockopt()
1004 # instead of using the thread-level error code that is in $!.
1005 0 ▶ if ($nfound && vec($wexc, $self->{fh}->fileno, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound and vec $wexc, $self->{"fh"}->fileno, 1) 0 0 0
Condition0%0/3
$nfound and vec $wexc, $self->{"fh"}->fileno, 1
!ll&&!rl&&r
000
MC/DC0%0/2
$nfound and vec $wexc, $self->{"fh"}->fileno, 1
$nfound vec $wexc, $self->{"fh"}->fileno, 1
Truth table0%0/3
A and B
A= $nfoundB= vec $wexc, $self->{"fh"}->fileno, 1
ABresult
0X0
100
111
1006 0 $! = unpack("i", getsockopt($self->{fh}, SOL_SOCKET,
1007 SO_ERROR));
1008 }
1009 }
1010 }
1011 }
1012 } else {
1013 # Connection established to remote host
1014 0 $ret = 1;
1015 }
1016  
1017 # Unset O_NONBLOCK property on filehandle
1018 0 $self->socket_blocking_mode($self->{fh}, 1);
1019 0 $self->{ip} = $ip->{addr_in};
1020 0 return $ret;
1021 0 };
1022  
1023 0 ▶ if ($syn_forking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Ping::syn_forking) { } 0 0 0
1024 # Buggy Winsock API doesn't allow nonblocking connect.
1025 # Hence, if our OS is Windows, we need to create a separate
1026 # process to do the blocking connect attempt.
1027 # XXX Above comments are not true at least for Win2K, where
1028 # nonblocking connect works.
1029  
1030 0 $| = 1; # Clear buffer prior to fork to prevent duplicate flushing.
1031 0 $self->{'tcp_chld'} = fork;
1032 0 ▶ if (!$self->{'tcp_chld'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"tcp_chld"}) 0 0 0
1033 0 ▶ if (!defined $self->{'tcp_chld'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"tcp_chld"}) 0 0 0
1034 # Fork did not work
1035 0 warn "Fork error: $!";
1036 0 return 0;
1037 }
1038 0 &{ $do_socket }();
1039  
1040 # Try a slow blocking connect() call
1041 # and report the status to the parent.
1042 0 ▶ if ( &{ $do_connect }() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&{$do_connect;}()) { } 0 0 0
1043 0 $self->{fh}->close();
1044 # No error
1045 0 exit 0;
1046 } else {
1047 # Pass the error status to the parent
1048 # Make sure that $! <= 255
1049 0 ▶ exit($! <= 255 ? $! : 255);
Branch: 1 branch
BranchTrueFalseTotal
$! <= 255 ? : 0 0 0
1050 }
1051 }
1052  
1053 0 &{ $do_socket }();
1054  
1055 0 my $patience = &time() + $timeout;
1056  
1057 0 my ($child, $child_errno);
1058 0 $? = 0; $child_errno = 0;
1059 # Wait up to the timeout
1060 # And clean off the zombie
1061 do {
1062 0 $child = waitpid($self->{'tcp_chld'}, &WNOHANG());
1063 0 $child_errno = $? >> 8;
1064 0 select(undef, undef, undef, 0.1);
1065 0 ▶ } while &time() < $patience && $child != $self->{'tcp_chld'};
Branch: 1 branch
BranchTrueFalseTotal
&time() < $patience and $child != $self->{"tcp_chld"} and do { do { $child = waitpid($self->{"tcp_chld"}, &WNOHANG()); $child_errno = $? >> 8; select undef, undef, undef, "0.1" } } 0 0 0
Condition0%0/3
&time() < $patience and $child != $self->{"tcp_chld"}
!ll&&!rl&&r
000
MC/DC0%0/2
&time() < $patience and $child != $self->{"tcp_chld"}
&time() < $patience $child != $self->{"tcp_chld"}
Truth table0%0/3
A and B
A= &time() < $patienceB= $child != $self->{"tcp_chld"}
ABresult
0X0
100
111
1066  
1067 0 ▶ if ($child == $self->{'tcp_chld'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child == $self->{'tcp_chld'}) { } 0 0 0
1068 0 ▶ if ($self->{proto} eq "stream") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proto"} eq "stream") 0 0 0
1069 # We need the socket connected here, in parent
1070 # Should be safe to connect because the child finished
1071 # within the timeout
1072 0 &{ $do_connect }();
1073 }
1074 # $ret cannot be set by the child process
1075 0 $ret = !$child_errno;
1076 } else {
1077 # Time must have run out.
1078 # Put that choking client out of its misery
1079 0 kill "KILL", $self->{'tcp_chld'};
1080 # Clean off the zombie
1081 0 waitpid($self->{'tcp_chld'}, 0);
1082 0 $ret = 0;
1083 }
1084 0 delete $self->{'tcp_chld'};
1085 0 $! = $child_errno;
1086 } else {
1087 # Otherwise don't waste the resources to fork
1088  
1089 0 &{ $do_socket }();
1090  
1091 0 &{ $do_connect_nb }();
1092 }
1093  
1094 0 return $ret;
1095 }
1096  
1097 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
1098 0 my $self = shift;
1099 0 ▶ if ($self->{'proto'} && ($self->{'proto'} eq 'tcp') && $self->{'tcp_chld'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proto"} and $self->{"proto"} eq "tcp" and $self->{"tcp_chld"}) 0 0 0
Condition0%0/6
$self->{"proto"} and $self->{"proto"} eq "tcp" and $self->{"tcp_chld"}
!ll&&!rl&&r
000
$self->{"proto"} and $self->{"proto"} eq "tcp"
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"proto"} and $self->{"proto"} eq "tcp" and $self->{"tcp_chld"}
$self->{"proto"} $self->{"proto"} eq "tcp" $self->{"tcp_chld"}
Truth table0%0/4
A and B and C
A= $self->{"proto"}B= $self->{"proto"} eq "tcp"C= $self->{"tcp_chld"}
ABCresult
0XX0
10X0
1100
1111
1100 # Put that choking client out of its misery
1101 0 kill "KILL", $self->{'tcp_chld'};
1102 # Clean off the zombie
1103 0 waitpid($self->{'tcp_chld'}, 0);
1104 }
1105 }
1106  
1107 # This writes the given string to the socket and then reads it
1108 # back. It returns 1 on success, 0 on failure.
1109 sub tcp_echo
1110 0 ▶{
Subroutine tcp_echo: not called CC=5 SCAR=34.0
Pod documented
1111 0 my ($self, $timeout, $pingstring) = @_;
1112  
1113 0 ▶ $timeout = $self->{timeout} if !defined $timeout and $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout and $self->{"timeout"} 0 0 0
Condition0%0/3
not defined $timeout and $self->{"timeout"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $timeout and $self->{"timeout"}
not defined $timeout $self->{"timeout"}
Truth table0%0/3
not(A) and B
A= not defined $timeoutB= $self->{"timeout"}
ABresult
0X0
100
111
1114 0 ▶ $pingstring = $self->{pingstring} if !defined $pingstring and $self->{pingstring};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $pingstring and $self->{"pingstring"} 0 0 0
Condition0%0/3
not defined $pingstring and $self->{"pingstring"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $pingstring and $self->{"pingstring"}
not defined $pingstring $self->{"pingstring"}
Truth table0%0/3
not(A) and B
A= not defined $pingstringB= $self->{"pingstring"}
ABresult
0X0
100
111
1115  
1116 0 my $ret = undef;
1117 0 my $time = &time();
1118 0 my $wrstr = $pingstring;
1119 0 my $rdstr = "";
1120  
1121 0 eval <<'EOM';
1122 do {
1123 my $rin = "";
1124 vec($rin, $self->{fh}->fileno(), 1) = 1;
1125  
1126 my $rout = undef;
1127 if($wrstr) {
1128 $rout = "";
1129 vec($rout, $self->{fh}->fileno(), 1) = 1;
1130 }
1131  
1132 if(mselect($rin, $rout, undef, ($time + $timeout) - &time())) {
1133  
1134 if($rout && vec($rout,$self->{fh}->fileno(),1)) {
1135 my $num = syswrite($self->{fh}, $wrstr, length $wrstr);
1136 if($num) {
1137 # If it was a partial write, update and try again.
1138 $wrstr = substr($wrstr,$num);
1139 } else {
1140 # There was an error.
1141 $ret = 0;
1142 }
1143 }
1144  
1145 if(vec($rin,$self->{fh}->fileno(),1)) {
1146 my $reply;
1147 if(sysread($self->{fh},$reply,length($pingstring)-length($rdstr))) {
1148 $rdstr .= $reply;
1149 $ret = 1 if $rdstr eq $pingstring;
1150 } else {
1151 # There was an error.
1152 $ret = 0;
1153 }
1154 }
1155  
1156 }
1157 } until &time() > ($time + $timeout) || defined($ret);
1158 EOM
1159  
1160 0 return $ret;
1161 }
1162  
1163 # Description: Perform a stream ping. If the tcp connection isn't
1164 # already open, it opens it. It then sends some data and waits for
1165 # a reply. It leaves the stream open on exit.
1166  
1167 sub ping_stream
1168 0 ▶{
Subroutine ping_stream: not called CC=4 SCAR=30.0
Pod documented
1169 0 my ($self,
1170 $ip, # Hash of addr (string), addr_in (packed), family
1171 $timeout # Seconds after which ping times out
1172 ) = @_;
1173  
1174 # Open the stream if it's not already open
1175 0 ▶ if(!defined $self->{fh}->fileno()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}->fileno) 0 0 0
1176 0 ▶ $self->tcp_connect($ip, $timeout) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->tcp_connect($ip, $timeout) or return 0 0 0 0
1177 }
1178  
1179 croak "tried to switch servers while stream pinging"
1180 0 ▶ if $self->{ip} ne $ip->{addr_in};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ip"} ne $ip->{"addr_in"} 0 0 0
1181  
1182 0 return $self->tcp_echo($timeout, $pingstring);
1183 }
1184  
1185 # Description: opens the stream. You would do this if you want to
1186 # separate the overhead of opening the stream from the first ping.
1187  
1188 sub open
1189 0 ▶{
Subroutine open: not called CC=9 SCAR=45.0
Pod documented
1190 0 my ($self,
1191 $host, # Host or IP address
1192 $timeout, # Seconds after which open times out
1193 $family
1194 ) = @_;
1195 0 my $ip; # Hash of addr (string), addr_in (packed), family
1196 0 ▶ $host = $self->{host} unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
1197  
1198 0 ▶ if ($family) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family) { } 0 0 0
1199 0 ▶ if ($family =~ $qr_family) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /$qr_family/) { } 0 0 0
1200 0 ▶ if ($family =~ $qr_family4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /$qr_family4/) { } 0 0 0
1201 0 $self->{family_local} = AF_INET;
1202 } else {
1203 0 $self->{family_local} = $AF_INET6;
1204 }
1205 } else {
1206 0 croak('Family must be "ipv4" or "ipv6"')
1207 }
1208 } else {
1209 0 $self->{family_local} = $self->{family};
1210 }
1211  
1212 0 ▶ $timeout = $self->{timeout} unless $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout 0 0 0
1213 0 $ip = $self->_resolv($host);
1214  
1215 0 ▶ if ($self->{proto} eq "stream") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proto"} eq "stream") 0 0 0
1216 0 ▶ if (defined($self->{fh}->fileno())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'fh'}->fileno) { } 0 0 0
1217 0 croak("socket is already open");
1218 } else {
1219 0 ▶ return () unless $ip;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip 0 0 0
1220 0 $self->tcp_connect($ip, $timeout);
1221 }
1222 }
1223 }
1224  
1225 0 ▶sub _dontfrag {
Subroutine _dontfrag: not called CC=5 SCAR=34.0
1226 0 my $self = shift;
1227 # bsd solaris
1228 0 my $IP_DONTFRAG = eval { Socket::IP_DONTFRAG() };
1229 0 ▶ if ($IP_DONTFRAG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IP_DONTFRAG) 0 0 0
1230 0 my $i = 1;
1231 0 ▶ setsockopt($self->{fh}, IPPROTO_IP, $IP_DONTFRAG, pack("I*", $i))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, 0, $IP_DONTFRAG, pack("I*", $i) or croak("error configuring IP_DONTFRAG $!") 0 0 0
1232 or croak "error configuring IP_DONTFRAG $!";
1233 # Linux needs more: Path MTU Discovery as defined in RFC 1191
1234 # For non SOCK_STREAM sockets it is the user's responsibility to packetize
1235 # the data in MTU sized chunks and to do the retransmits if necessary.
1236 # The kernel will reject packets that are bigger than the known path
1237 # MTU if this flag is set (with EMSGSIZE).
1238 0 ▶ if ($^O eq 'linux') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux") 0 0 0
1239 0 my $i = 2; # IP_PMTUDISC_DO
1240 0 ▶ setsockopt($self->{fh}, IPPROTO_IP, IP_MTU_DISCOVER, pack("I*", $i))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, 0, 10, pack("I*", $i) or croak("error configuring IP_MTU_DISCOVER $!") 0 0 0
1241 or croak "error configuring IP_MTU_DISCOVER $!";
1242 }
1243 }
1244 }
1245  
1246 # SO_BINDTODEVICE + IP_TOS
1247 0 ▶sub _setopts {
Subroutine _setopts: not called CC=6 SCAR=37.4
1248 0 my $self = shift;
1249 0 ▶ if ($self->{'device'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"device"}) 0 0 0
1250 0 ▶ setsockopt($self->{fh}, SOL_SOCKET, SO_BINDTODEVICE, pack("Z*", $self->{'device'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, 1, 25, pack("Z*", $self->{"device"}) or croak(qq[error binding to device $self->{"device"} $!]) 0 0 0
1251 or croak "error binding to device $self->{'device'} $!";
1252 }
1253 0 ▶ if ($self->{'tos'}) { # need to re-apply ToS (RT #6706)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tos"}) 0 0 0
1254 0 ▶ setsockopt($self->{fh}, IPPROTO_IP, IP_TOS, pack("I*", $self->{'tos'}))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $self->{"fh"}, 0, 1, pack("I*", $self->{"tos"}) or croak(qq[error applying tos to $self->{"tos"} $!]) 0 0 0
1255 or croak "error applying tos to $self->{'tos'} $!";
1256 }
1257 0 ▶ if ($self->{'dontfrag'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dontfrag"}) 0 0 0
1258 0 $self->_dontfrag;
1259 }
1260 }
1261  
1262  
1263 # Description: Perform a udp echo ping. Construct a message of
1264 # at least the one-byte sequence number and any additional data bytes.
1265 # Send the message out and wait for a message to come back. If we
1266 # get a message, make sure all of its parts match. If they do, we are
1267 # done. Otherwise go back and wait for the message until we run out
1268 # of time. Return the result of our efforts.
1269  
1270 7 ▶use constant UDP_FLAGS => 0; # Nothing special on send or recv
Subroutine BEGIN: called CC=1 SCAR=0.0
1271 sub ping_udp
1272 0 ▶{
Subroutine ping_udp: not called CC=28 SCAR=67.0
Pod documented
1273 0 my ($self,
1274 $ip, # Hash of addr (string), addr_in (packed), family
1275 $timeout # Seconds after which ping times out
1276 ) = @_;
1277  
1278 0 my ($saddr, # sockaddr_in with port and ip
1279 $ret, # The return value
1280 $msg, # Message to be echoed
1281 $finish_time, # Time ping should be finished
1282 $flush, # Whether socket needs to be disconnected
1283 $connect, # Whether socket needs to be connected
1284 $done, # Set to 1 when we are done pinging
1285 $rbits, # Read bits, filehandles for reading
1286 $nfound, # Number of ready filehandles found
1287 $from_saddr, # sockaddr_in of sender
1288 $from_msg, # Characters echoed by $host
1289 $from_port, # Port message was echoed from
1290 $from_ip # Packed IP number of sender
1291 );
1292  
1293 0 $saddr = _pack_sockaddr_in($self->{port_num}, $ip);
1294 0 $self->{seq} = ($self->{seq} + 1) % 256; # Increment sequence
1295 0 $msg = chr($self->{seq}) . $self->{data}; # Add data if any
1296  
1297 socket($self->{fh}, $ip->{family}, SOCK_DGRAM,
1298 0 ▶ $self->{proto_num}) ||
Branch: 1 branch
BranchTrueFalseTotal
socket $self->{"fh"}, $ip->{"family"}, 2, $self->{"proto_num"} or croak("udp socket error - $!") 0 0 0
1299 croak("udp socket error - $!");
1300  
1301 0 ▶ if (defined $self->{local_addr} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))) 0 0 0
Condition0%0/3
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
defined $self->{"local_addr"} not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
Truth table0%0/3
A and not(B)
A= defined $self->{"local_addr"}B= not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
ABresult
0X0
100
111
1302 !CORE::bind($self->{fh}, _pack_sockaddr_in(0, $self->{local_addr}))) {
1303 0 croak("udp bind error - $!");
1304 }
1305  
1306 0 $self->_setopts();
1307  
1308 0 ▶ if ($self->{connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'connected'}) { } 0 0 0
1309 0 ▶ if ($self->{connected} ne $saddr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"connected"} ne $saddr) 0 0 0
1310 # Still connected to wrong destination.
1311 # Need to flush out the old one.
1312 0 $flush = 1;
1313 }
1314 } else {
1315 # Not connected yet.
1316 # Need to connect() before send()
1317 0 $connect = 1;
1318 }
1319  
1320 # Have to connect() and send() instead of sendto()
1321 # in order to pick up on the ECONNREFUSED setting
1322 # from recv() or double send() errno as utilized in
1323 # the concept by rdw @ perlmonks. See:
1324 # http://perlmonks.thepen.com/42898.html
1325 0 ▶ if ($flush) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flush) 0 0 0
1326 # Need to socket() again to flush the descriptor
1327 # This will disconnect from the old saddr.
1328 socket($self->{fh}, $ip->{family}, SOCK_DGRAM,
1329 0 $self->{proto_num});
1330 0 $self->_setopts();
1331 }
1332 # Connect the socket if it isn't already connected
1333 # to the right destination.
1334 0 ▶ if ($flush || $connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flush or $connect) 0 0 0
Condition0%0/3
$flush or $connect
l!l&&r!l&&!r
000
MC/DC0%0/2
$flush or $connect
$flush $connect
Truth table0%0/3
A or B
A= $flushB= $connect
ABresult
1X1
011
000
1335 0 connect($self->{fh}, $saddr); # Tie destination to socket
1336 0 $self->{connected} = $saddr;
1337 }
1338 0 send($self->{fh}, $msg, UDP_FLAGS); # Send it
1339  
1340 0 $rbits = "";
1341 0 vec($rbits, $self->{fh}->fileno(), 1) = 1;
1342 0 $ret = 0; # Default to unreachable
1343 0 $done = 0;
1344 0 my $retrans = 0.01;
1345 0 my $factor = $self->{retrans};
1346 0 $finish_time = &time() + $timeout; # Ping needs to be done by then
1347 0 ▶ while (!$done && $timeout > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and $timeout > 0) 0 0 0
Condition0%0/3
not $done and $timeout > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $done and $timeout > 0
not $done $timeout > 0
Truth table0%0/3
not(A) and B
A= not $doneB= $timeout > 0
ABresult
0X0
100
111
1348 {
1349 0 ▶ if ($factor > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($factor > 1) 0 0 0
1350 {
1351 0 ▶ $timeout = $retrans if $timeout > $retrans;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout > $retrans 0 0 0
1352 0 $retrans*= $factor; # Exponential backoff
1353 }
1354 0 $nfound = mselect((my $rout=$rbits), undef, undef, $timeout); # Wait for response
1355 0 my $why = $!;
1356 0 $timeout = $finish_time - &time(); # Get remaining time
1357  
1358 0 ▶ if (!defined($nfound)) # Hmm, a strange error
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $nfound) { } 0 0 0
elsif ($nfound) { } 0 0 0
elsif ($timeout <= 0) { } 0 0 0
1359 {
1360 0 $ret = undef;
1361 0 $done = 1;
1362 }
1363 elsif ($nfound) # A packet is waiting
1364 {
1365 0 $from_msg = "";
1366 0 $from_saddr = recv($self->{fh}, $from_msg, 1500, UDP_FLAGS);
1367 0 ▶ if (!$from_saddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $from_saddr) { } 0 0 0
1368 # For example an unreachable host will make recv() fail.
1369 0 ▶ if (!$self->{econnrefused} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"econnrefused"} and $! == 111 || $! == 104) 0 0 0
Condition0%0/6
not $self->{"econnrefused"} and $! == 111 || $! == 104
!ll&&!rl&&r
000
$! == 111 or $! == 104
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->{"econnrefused"} and $! == 111 || $! == 104
not $self->{"econnrefused"} $! == 111 $! == 104
Truth table0%0/4
not(A) and B or C
A= not $self->{"econnrefused"}B= $! == 111C= $! == 104
ABCresult
0XX0
1000
11X1
1011
1370 ($! == ECONNREFUSED ||
1371 $! == ECONNRESET)) {
1372 # "Connection refused" means reachable
1373 # Good, continue
1374 0 $ret = 1;
1375 }
1376 0 $done = 1;
1377 } else {
1378 0 ($from_port, $from_ip) = _unpack_sockaddr_in($from_saddr, $ip->{family});
1379 0 ▶ my $addr_in = ref($ip) eq "HASH" ? $ip->{addr_in} : $ip;
Branch: 1 branch
BranchTrueFalseTotal
ref $ip eq 'HASH' ? : 0 0 0
1380 0 ▶ if (!$source_verify ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $Net::Ping::source_verify or $from_ip eq $addr_in and $from_port == $self->{"port_num"} and $from_msg eq $msg) 0 0 0
Condition0%0/9
not $Net::Ping::source_verify or $from_ip eq $addr_in and $from_port == $self->{"port_num"} and $from_msg eq $msg
l!l&&r!l&&!r
000
$from_ip eq $addr_in and $from_port == $self->{"port_num"} and $from_msg eq $msg
!ll&&!rl&&r
000
$from_ip eq $addr_in and $from_port == $self->{"port_num"}
!ll&&!rl&&r
000
MC/DC0%0/4
not $Net::Ping::source_verify or $from_ip eq $addr_in and $from_port == $self->{"port_num"} and $from_msg eq $msg
not $Net::Ping::source_verify $from_ip eq $addr_in $from_port == $self->{"port_num"} $from_msg eq $msg
Truth table0%0/5
not(A) or B and C and D
A= not $Net::Ping::source_verifyB= $from_ip eq $addr_inC= $from_port == $self->{"port_num"}D= $from_msg eq $msg
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1381 (($from_ip eq $addr_in) && # Does the packet check out?
1382 ($from_port == $self->{port_num}) &&
1383 ($from_msg eq $msg)))
1384 {
1385 0 $ret = 1; # It's a winner
1386 0 $done = 1;
1387 }
1388 }
1389 }
1390 elsif ($timeout <= 0) # Oops, timed out
1391 {
1392 0 $done = 1;
1393 }
1394 else
1395 {
1396 # Send another in case the last one dropped
1397 0 ▶ if (send($self->{fh}, $msg, UDP_FLAGS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (send $self->{'fh'}, $msg, 0) { } 0 0 0
1398 # Another send worked? The previous udp packet
1399 # must have gotten lost or is still in transit.
1400 # Hopefully this new packet will arrive safely.
1401 } else {
1402 0 ▶ if (!$self->{econnrefused} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"econnrefused"} and $! == 111) 0 0 0
Condition0%0/3
not $self->{"econnrefused"} and $! == 111
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"econnrefused"} and $! == 111
not $self->{"econnrefused"} $! == 111
Truth table0%0/3
not(A) and B
A= not $self->{"econnrefused"}B= $! == 111
ABresult
0X0
100
111
1403 $! == ECONNREFUSED) {
1404 # "Connection refused" means reachable
1405 # Good, continue
1406 0 $ret = 1;
1407 }
1408 0 $done = 1;
1409 }
1410 }
1411 }
1412 0 return $ret;
1413 }
1414  
1415 # Description: Send a TCP SYN packet to host specified.
1416 sub ping_syn
1417 0 ▶{
Subroutine ping_syn: not called CC=10 SCAR=47.0
Pod documented
1418 0 my $self = shift;
1419 0 my $host = shift;
1420 0 my $ip = shift;
1421 0 my $start_time = shift;
1422 0 my $stop_time = shift;
1423  
1424 0 ▶ if ($syn_forking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Ping::syn_forking) 0 0 0
1425 0 return $self->ping_syn_fork($host, $ip, $start_time, $stop_time);
1426 }
1427  
1428 0 my $fh = FileHandle->new();
1429 0 my $saddr = _pack_sockaddr_in($self->{port_num}, $ip);
1430  
1431 # Create TCP socket
1432 0 ▶ if (!socket ($fh, $ip->{family}, SOCK_STREAM, $self->{proto_num})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (socket $fh, $ip->{"family"}, 1, $self->{"proto_num"}) 0 0 0
1433 0 croak("tcp socket error - $!");
1434 }
1435  
1436 0 ▶ if (defined $self->{local_addr} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_addr"} and not CORE::bind($fh, &_pack_sockaddr_in(0, $self->{"local_addr"}))) 0 0 0
Condition0%0/3
defined $self->{"local_addr"} and not CORE::bind($fh, &_pack_sockaddr_in(0, $self->{"local_addr"}))
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"local_addr"} and not CORE::bind($fh, &_pack_sockaddr_in(0, $self->{"local_addr"}))
defined $self->{"local_addr"} not CORE::bind($fh, &_pack_sockaddr_in(0, $self->{"local_addr"}))
Truth table0%0/3
A and not(B)
A= defined $self->{"local_addr"}B= not CORE::bind($fh, &_pack_sockaddr_in(0, $self->{"local_addr"}))
ABresult
0X0
100
111
1437 !CORE::bind($fh, _pack_sockaddr_in(0, $self->{local_addr}))) {
1438 0 croak("tcp bind error - $!");
1439 }
1440  
1441 0 $self->_setopts();
1442 # Set O_NONBLOCK property on filehandle
1443 0 $self->socket_blocking_mode($fh, 0);
1444  
1445 # Attempt the non-blocking connect
1446 # by just sending the TCP SYN packet
1447 0 ▶ if (connect($fh, $saddr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect $fh, $saddr) { } 0 0 0
1448 # Non-blocking, yet still connected?
1449 # Must have connected very quickly,
1450 # or else it wasn't very non-blocking.
1451 #warn "WARNING: Nonblocking connect connected anyway? ($^O)";
1452 } else {
1453 # Error occurred connecting.
1454 0 ▶ if ($! == EINPROGRESS || ($^O eq 'MSWin32' && $! == EWOULDBLOCK)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 115 or $^O eq 'MSWin32' and $! == 11) { } 0 0 0
Condition0%0/6
$! == 115 or $^O eq "MSWin32" and $! == 11
l!l&&r!l&&!r
000
$^O eq "MSWin32" and $! == 11
!ll&&!rl&&r
000
MC/DC0%0/3
$! == 115 or $^O eq "MSWin32" and $! == 11
$! == 115 $^O eq "MSWin32" $! == 11
Truth table0%0/4
A or B and C
A= $! == 115B= $^O eq "MSWin32"C= $! == 11
ABCresult
1XX1
0111
00X0
0100
1455 # The connection is just still in progress.
1456 # This is the expected condition.
1457 } else {
1458 # Just save the error and continue on.
1459 # The ack() can check the status later.
1460 0 $self->{bad}->{$host} = $!;
1461 }
1462 }
1463  
1464 0 my $entry = [ $host, $ip, $fh, $start_time, $stop_time, $self->{port_num} ];
1465 0 $self->{syn}->{$fh->fileno} = $entry;
1466 0 ▶ if ($self->{stop_time} < $stop_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stop_time"} < $stop_time) 0 0 0
1467 0 $self->{stop_time} = $stop_time;
1468 }
1469 0 vec($self->{wbits}, $fh->fileno, 1) = 1;
1470  
1471 0 return 1;
1472 }
1473  
1474 0 ▶sub ping_syn_fork {
Subroutine ping_syn_fork: not called CC=7 SCAR=40.3
Pod documented
1475 0 my ($self, $host, $ip, $start_time, $stop_time) = @_;
1476  
1477 # Buggy Winsock API doesn't allow nonblocking connect.
1478 # Hence, if our OS is Windows, we need to create a separate
1479 # process to do the blocking connect attempt.
1480 0 my $pid = fork();
1481 0 ▶ if (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) { } 0 0 0
1482 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
1483 # Parent process
1484 0 my $entry = [ $host, $ip, $pid, $start_time, $stop_time ];
1485 0 $self->{syn}->{$pid} = $entry;
1486 0 ▶ if ($self->{stop_time} < $stop_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stop_time"} < $stop_time) 0 0 0
1487 0 $self->{stop_time} = $stop_time;
1488 }
1489 } else {
1490 # Child process
1491 0 my $saddr = _pack_sockaddr_in($self->{port_num}, $ip);
1492  
1493 # Create TCP socket
1494 0 ▶ if (!socket ($self->{fh}, $ip->{family}, SOCK_STREAM, $self->{proto_num})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (socket $self->{"fh"}, $ip->{"family"}, 1, $self->{"proto_num"}) 0 0 0
1495 0 croak("tcp socket error - $!");
1496 }
1497  
1498 0 ▶ if (defined $self->{local_addr} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))) 0 0 0
Condition0%0/3
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"local_addr"} and not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
defined $self->{"local_addr"} not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
Truth table0%0/3
A and not(B)
A= defined $self->{"local_addr"}B= not CORE::bind($self->{"fh"}, &_pack_sockaddr_in(0, $self->{"local_addr"}))
ABresult
0X0
100
111
1499 !CORE::bind($self->{fh}, _pack_sockaddr_in(0, $self->{local_addr}))) {
1500 0 croak("tcp bind error - $!");
1501 }
1502  
1503 0 $self->_setopts();
1504  
1505 0 $!=0;
1506 # Try to connect (could take a long time)
1507 0 connect($self->{fh}, $saddr);
1508 # Notify parent of connect error status
1509 0 my $err = $!+0;
1510 0 my $wrstr = "$$ $err";
1511 # Force to 16 chars including \n
1512 0 $wrstr .= " "x(15 - length $wrstr). "\n";
1513 0 syswrite($self->{fork_wr}, $wrstr, length $wrstr);
1514 0 exit;
1515 }
1516 } else {
1517 # fork() failed?
1518 0 die "fork: $!";
1519 }
1520 0 return 1;
1521 }
1522  
1523 # Description: Wait for TCP ACK from host specified
1524 # from ping_syn above. If no host is specified, wait
1525 # for TCP ACK from any of the hosts in the SYN queue.
1526 sub ack
1527 0 ▶{
Subroutine ack: not called CC=41 SCAR=74.5
Pod documented
1528 0 my $self = shift;
1529  
1530 0 ▶ if ($self->{proto} eq "syn") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proto"} eq "syn") 0 0 0
1531 0 ▶ if ($syn_forking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Ping::syn_forking) 0 0 0
1532 0 my @answer = $self->ack_unfork(shift);
1533 0 ▶ return wantarray ? @answer : $answer[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1534 }
1535 0 my $wbits = "";
1536 0 my $stop_time = 0;
1537 0 ▶ if (my $host = shift or $self->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host = shift() or $self->{'host'}) { } 0 0 0
Condition0%0/3
my $host = shift() or $self->{"host"}
l!l&&r!l&&!r
000
MC/DC0%0/2
my $host = shift() or $self->{"host"}
my $host = shift() $self->{"host"}
Truth table0%0/3
A or B
A= my $host = shift()B= $self->{"host"}
ABresult
1X1
011
000
1538 # Host passed as arg or as option to new
1539 0 ▶ $host = $self->{host} unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
1540 0 ▶ if (exists $self->{bad}->{$host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"bad"}{$host}) 0 0 0
1541 0 ▶ if (!$self->{econnrefused} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'econnrefused'} and $self->{'bad'}{$host} and ($! = 111) > 0 and $self->{'bad'}{$host} eq "$!") { } 0 0 0
Condition0%0/9
not $self->{"econnrefused"} and $self->{"bad"}{$host} and ($! = 111) > 0 and $self->{"bad"}{$host} eq "$!"
!ll&&!rl&&r
000
not $self->{"econnrefused"} and $self->{"bad"}{$host} and ($! = 111) > 0
!ll&&!rl&&r
000
not $self->{"econnrefused"} and $self->{"bad"}{$host}
!ll&&!rl&&r
000
MC/DC0%0/4
not $self->{"econnrefused"} and $self->{"bad"}{$host} and ($! = 111) > 0 and $self->{"bad"}{$host} eq "$!"
not $self->{"econnrefused"} $self->{"bad"}{$host} ($! = 111) > 0 $self->{"bad"}{$host} eq "$!"
Truth table0%0/5
not(A) and B and C and D
A= not $self->{"econnrefused"}B= $self->{"bad"}{$host}C= ($! = 111) > 0D= $self->{"bad"}{$host} eq "$!"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1542 $self->{bad}->{ $host } &&
1543 (($! = ECONNREFUSED)>0) &&
1544 $self->{bad}->{ $host } eq "$!") {
1545 # "Connection refused" means reachable
1546 # Good, continue
1547 } else {
1548 # ECONNREFUSED means no good
1549 0 return ();
1550 }
1551 }
1552 0 my $host_fd = undef;
1553 0 foreach my $fd (keys %{ $self->{syn} }) {
1554 0 my $entry = $self->{syn}->{$fd};
1555 0 ▶ if ($entry->[0] eq $host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->[0] eq $host) 0 0 0
1556 0 $host_fd = $fd;
1557 0 ▶ $stop_time = $entry->[4]
Condition0%0/2
$entry->[4] || croak("Corrupted SYN entry for [$host]")
l!l
00
MC/DC0%0/1
$entry->[4] || croak("Corrupted SYN entry for [$host]")
$entry->[4]
Truth table0%0/2
A
A= $entry->[4]
Aresult
11
00
1558 || croak("Corrupted SYN entry for [$host]");
1559 0 last;
1560 }
1561 }
1562 0 ▶ croak("ack called on [$host] without calling ping first!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host_fd 0 0 0
1563 unless defined $host_fd;
1564 0 vec($wbits, $host_fd, 1) = 1;
1565 } else {
1566 # No $host passed so scan all hosts
1567 # Use the latest stop_time
1568 0 $stop_time = $self->{stop_time};
1569 # Use all the bits
1570 0 $wbits = $self->{wbits};
1571 }
1572  
1573 0 ▶ while ($wbits !~ /^\0*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wbits =~ /^\0*\z/) 0 0 0
1574 0 my $timeout = $stop_time - &time();
1575 # Force a minimum of 10 ms timeout.
1576 0 ▶ $timeout = 0.01 if $timeout <= 0.01;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= "0.01" 0 0 0
1577  
1578 0 my $winner_fd = undef;
1579 0 my $wout = $wbits;
1580 0 my $fd = 0;
1581 # Do "bad" fds from $wbits first
1582 0 ▶ while ($wout !~ /^\0*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wout =~ /^\0*\z/) 0 0 0
1583 0 ▶ if (vec($wout, $fd, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $wout, $fd, 1) 0 0 0
1584 # Wipe it from future scanning.
1585 0 vec($wout, $fd, 1) = 0;
1586 0 ▶ if (my $entry = $self->{syn}->{$fd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->{"syn"}{$fd}) 0 0 0
1587 0 ▶ if ($self->{bad}->{ $entry->[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bad"}{$entry->[0]}) 0 0 0
1588 0 $winner_fd = $fd;
1589 0 last;
1590 }
1591 }
1592 }
1593 0 $fd++;
1594 }
1595  
1596 0 ▶ if (defined($winner_fd) or my $nfound = mselect(undef, ($wout=$wbits), undef, $timeout)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $winner_fd or my $nfound = mselect(undef, $wout = $wbits, undef, $timeout)) { } 0 0 0
elsif (defined $nfound) { } 0 0 0
Condition0%0/3
defined $winner_fd or my $nfound = mselect(undef, $wout = $wbits, undef, $timeout)
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $winner_fd or my $nfound = mselect(undef, $wout = $wbits, undef, $timeout)
defined $winner_fd my $nfound = mselect(undef, $wout = $wbits, undef, $timeout)
Truth table0%0/3
A or B
A= defined $winner_fdB= my $nfound = mselect(undef, $wout = $wbits, undef, $timeout)
ABresult
1X1
011
000
1597 0 ▶ if (defined $winner_fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $winner_fd) { } 0 0 0
1598 0 $fd = $winner_fd;
1599 } else {
1600 # Done waiting for one of the ACKs
1601 0 $fd = 0;
1602 # Determine which one
1603 0 ▶ while ($wout !~ /^\0*\z/ &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($wout =~ /^\0*\z/ or vec $wout, $fd, 1) 0 0 0
Condition0%0/3
$wout =~ /^\0*\z/ or vec $wout, $fd, 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$wout =~ /^\0*\z/ or vec $wout, $fd, 1
$wout =~ /^\0*\z/ vec $wout, $fd, 1
Truth table0%0/3
A or B
A= $wout =~ /^\0*\z/B= vec $wout, $fd, 1
ABresult
1X1
011
000
1604 !vec($wout, $fd, 1)) {
1605 0 $fd++;
1606 }
1607 }
1608 0 ▶ if (my $entry = $self->{syn}->{$fd}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->{'syn'}{$fd}) { } 0 0 0
1609 # Wipe it from future scanning.
1610 0 delete $self->{syn}->{$fd};
1611 0 vec($self->{wbits}, $fd, 1) = 0;
1612 0 vec($wbits, $fd, 1) = 0;
1613 0 ▶ if (!$self->{econnrefused} &&
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->{'econnrefused'} and $self->{'bad'}{$entry->[0]} and ($! = 111) > 0 and $self->{'bad'}{$entry->[0]} eq "$!") { } 0 0 0
elsif (getpeername $entry->[2]) { } 0 0 0
Condition0%0/9
not $self->{"econnrefused"} and $self->{"bad"}{$entry->[0]} and ($! = 111) > 0 and $self->{"bad"}{$entry->[0]} eq "$!"
!ll&&!rl&&r
000
not $self->{"econnrefused"} and $self->{"bad"}{$entry->[0]} and ($! = 111) > 0
!ll&&!rl&&r
000
not $self->{"econnrefused"} and $self->{"bad"}{$entry->[0]}
!ll&&!rl&&r
000
MC/DC0%0/4
not $self->{"econnrefused"} and $self->{"bad"}{$entry->[0]} and ($! = 111) > 0 and $self->{"bad"}{$entry->[0]} eq "$!"
not $self->{"econnrefused"} $self->{"bad"}{$entry->[0]} ($! = 111) > 0 $self->{"bad"}{$entry->[0]} eq "$!"
Truth table0%0/5
not(A) and B and C and D
A= not $self->{"econnrefused"}B= $self->{"bad"}{$entry->[0]}C= ($! = 111) > 0D= $self->{"bad"}{$entry->[0]} eq "$!"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1614 $self->{bad}->{ $entry->[0] } &&
1615 (($! = ECONNREFUSED)>0) &&
1616 $self->{bad}->{ $entry->[0] } eq "$!") {
1617 # "Connection refused" means reachable
1618 # Good, continue
1619 } elsif (getpeername($entry->[2])) {
1620 # Connection established to remote host
1621 # Good, continue
1622 } else {
1623 # TCP ACK will never come from this host
1624 # because there was an error connecting.
1625  
1626 # This should set $! to the correct error.
1627 0 my $char;
1628 0 sysread($entry->[2],$char,1);
1629 # Store the excuse why the connection failed.
1630 0 $self->{bad}->{$entry->[0]} = $!;
1631 0 ▶ if (!$self->{econnrefused} &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'econnrefused'} and $! == 111 || $! == 11 && $^O =~ /cygwin/i) { } 0 0 0
Condition0%0/9
not $self->{"econnrefused"} and $! == 111 || $! == 11 && $^O =~ /cygwin/i
!ll&&!rl&&r
000
$! == 111 or $! == 11 and $^O =~ /cygwin/i
l!l&&r!l&&!r
000
$! == 11 and $^O =~ /cygwin/i
!ll&&!rl&&r
000
MC/DC0%0/4
not $self->{"econnrefused"} and $! == 111 || $! == 11 && $^O =~ /cygwin/i
not $self->{"econnrefused"} $! == 111 $! == 11 $^O =~ /cygwin/i
Truth table0%0/5
not(A) and B or C and D
A= not $self->{"econnrefused"}B= $! == 111C= $! == 11D= $^O =~ /cygwin/i
ABCDresult
0XXX0
100X0
10100
11XX1
10111
1632 (($! == ECONNREFUSED) ||
1633 ($! == EAGAIN && $^O =~ /cygwin/i))) {
1634 # "Connection refused" means reachable
1635 # Good, continue
1636 } else {
1637 # No good, try the next socket...
1638 0 next;
1639 }
1640 }
1641 # Everything passed okay, return the answer
1642 return wantarray ?
1643 0 ▶ ($entry->[0], &time() - $entry->[3], $self->ntop($entry->[1]), $entry->[5])
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1644 : $entry->[0];
1645 } else {
1646 0 warn "Corrupted SYN entry: unknown fd [$fd] ready!";
1647 0 vec($wbits, $fd, 1) = 0;
1648 0 vec($self->{wbits}, $fd, 1) = 0;
1649 }
1650 } elsif (defined $nfound) {
1651 # Timed out waiting for ACK
1652 0 foreach my $fd (keys %{ $self->{syn} }) {
1653 0 ▶ if (vec($wbits, $fd, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $wbits, $fd, 1) 0 0 0
1654 0 my $entry = $self->{syn}->{$fd};
1655 0 $self->{bad}->{$entry->[0]} = "Timed out";
1656 0 vec($wbits, $fd, 1) = 0;
1657 0 vec($self->{wbits}, $fd, 1) = 0;
1658 0 delete $self->{syn}->{$fd};
1659 }
1660 }
1661 } else {
1662 # Weird error occurred with select()
1663 0 warn("select: $!");
1664 0 $self->{syn} = {};
1665 0 $wbits = "";
1666 }
1667 }
1668 }
1669 0 return ();
1670 }
1671  
1672 0 ▶sub ack_unfork {
Subroutine ack_unfork: not called CC=19 SCAR=59.4
Pod documented
1673 0 my ($self,$host) = @_;
1674 0 my $stop_time = $self->{stop_time};
1675 0 ▶ if ($host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 0 0 0
1676 # Host passed as arg
1677 0 ▶ if (my $entry = $self->{good}->{$host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->{"good"}{$host}) 0 0 0
1678 0 delete $self->{good}->{$host};
1679 0 return ($entry->[0], &time() - $entry->[3], $self->ntop($entry->[1]));
1680 }
1681 }
1682  
1683 0 my $rbits = "";
1684 0 my $timeout;
1685  
1686 0 ▶ if (keys %{ $self->{syn} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{'syn'};}) { } 0 0 0
1687 # Scan all hosts that are left
1688 0 vec($rbits, fileno($self->{fork_rd}), 1) = 1;
1689 0 $timeout = $stop_time - &time();
1690 # Force a minimum of 10 ms timeout.
1691 0 ▶ $timeout = 0.01 if $timeout < 0.01;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < "0.01" 0 0 0
1692 } else {
1693 # No hosts left to wait for
1694 0 $timeout = 0;
1695 }
1696  
1697 0 ▶ if ($timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout > 0) 0 0 0
1698 0 my $nfound;
1699 0 ▶ while ( keys %{ $self->{syn} } and
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$self{"syn"};} and $nfound = mselect(my $rout = $rbits, undef, undef, $timeout)) 0 0 0
Condition0%0/3
keys %{$$self{"syn"};} and $nfound = mselect(my $rout = $rbits, undef, undef, $timeout)
!ll&&!rl&&r
000
MC/DC0%0/2
keys %{$$self{"syn"};} and $nfound = mselect(my $rout = $rbits, undef, undef, $timeout)
keys %{$$self{"syn"};} $nfound = mselect(my $rout = $rbits, undef, undef, $timeout)
Truth table0%0/3
A and B
A= keys %{$$self{"syn"};}B= $nfound = mselect(my $rout = $rbits, undef, undef, $timeout)
ABresult
0X0
100
111
1700 $nfound = mselect((my $rout=$rbits), undef, undef, $timeout)) {
1701 # Done waiting for one of the ACKs
1702 0 ▶ if (!sysread($self->{fork_rd}, $_, 16)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysread $self->{"fork_rd"}, $_, 16) 0 0 0
1703 # Socket closed, which means all children are done.
1704 0 return ();
1705 }
1706 0 my ($pid, $how) = split;
1707 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 0 0 0
1708 # Flush the zombie
1709 0 waitpid($pid, 0);
1710 0 ▶ if (my $entry = $self->{syn}->{$pid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->{'syn'}{$pid}) { } 0 0 0
1711 # Connection attempt to remote host is done
1712 0 delete $self->{syn}->{$pid};
1713 0 ▶ if (!$how || # If there was no error connecting
Branch: 1 branch
BranchTrueFalseTotal
if (not $how or not $self->{"econnrefused"} and $how == 111) 0 0 0
Condition0%0/6
not $how or not $self->{"econnrefused"} and $how == 111
l!l&&r!l&&!r
000
not $self->{"econnrefused"} and $how == 111
!ll&&!rl&&r
000
MC/DC0%0/3
not $how or not $self->{"econnrefused"} and $how == 111
not $how not $self->{"econnrefused"} $how == 111
Truth table0%0/4
not(A) or not(B) and C
A= not $howB= not $self->{"econnrefused"}C= $how == 111
ABCresult
1XX1
0111
00X0
0100
1714 (!$self->{econnrefused} &&
1715 $how == ECONNREFUSED)) { # "Connection refused" means reachable
1716 0 ▶ if ($host && $entry->[0] ne $host) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host and $entry->[0] ne $host) 0 0 0
Condition0%0/3
$host and $entry->[0] ne $host
!ll&&!rl&&r
000
MC/DC0%0/2
$host and $entry->[0] ne $host
$host $entry->[0] ne $host
Truth table0%0/3
A and B
A= $hostB= $entry->[0] ne $host
ABresult
0X0
100
111
1717 # A good connection, but not the host we need.
1718 # Move it from the "syn" hash to the "good" hash.
1719 0 $self->{good}->{$entry->[0]} = $entry;
1720 # And wait for the next winner
1721 0 next;
1722 }
1723 0 return ($entry->[0], &time() - $entry->[3], $self->ntop($entry->[1]));
1724 }
1725 } else {
1726 # Should never happen
1727 0 die "Unknown ping from pid [$pid]";
1728 }
1729 } else {
1730 0 die "Empty response from status socket?";
1731 }
1732 }
1733 0 ▶ if (defined $nfound) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nfound) { } 0 0 0
1734 # Timed out waiting for ACK status
1735 } else {
1736 # Weird error occurred with select()
1737 0 warn("select: $!");
1738 }
1739 }
1740 0 ▶ if (my @synners = keys %{ $self->{syn} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@synners) = keys %{$$self{"syn"};}) 0 0 0
1741 # Kill all the synners
1742 0 kill 9, @synners;
1743 0 foreach my $pid (@synners) {
1744 # Wait for the deaths to finish
1745 # Then flush off the zombie
1746 0 waitpid($pid, 0);
1747 }
1748 }
1749 0 $self->{syn} = {};
1750 0 return ();
1751 }
1752  
1753 # Description: Tell why the ack() failed
1754 0 ▶sub nack {
Subroutine nack: not called CC=3 SCAR=24.8
Pod documented
1755 0 my $self = shift;
1756 0 ▶ my $host = shift || croak('Usage> nack($failed_ack_host)');
Condition0%0/2
shift() || croak('Usage> nack($failed_ack_host)')
l!l
00
MC/DC0%0/1
shift() || croak('Usage> nack($failed_ack_host)')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1757 0 ▶ return $self->{bad}->{$host} || undef;
Condition0%0/2
$self->{'bad'}{$host} || undef
l!l
00
MC/DC0%0/1
$self->{'bad'}{$host} || undef
$self->{'bad'}{$host}
Truth table0%0/2
A
A= $self->{'bad'}{$host}
Aresult
11
00
1758 }
1759  
1760 # Description: Close the connection.
1761  
1762 sub close
1763 0 ▶{
Subroutine close: not called CC=4 SCAR=30.0
Pod documented
1764 0 my ($self) = @_;
1765  
1766 0 ▶ if ($self->{proto} eq "syn") {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'proto'} eq 'syn') { } 0 0 0
elsif ($self->{'proto'} eq 'tcp') { } 0 0 0
elsif ($self->{'proto'} eq 'external') { } 0 0 0
1767 0 delete $self->{syn};
1768 } elsif ($self->{proto} eq "tcp") {
1769 # The connection will already be closed
1770 } elsif ($self->{proto} eq "external") {
1771 # Nothing to close
1772 } else {
1773 0 $self->{fh}->close();
1774 }
1775 }
1776  
1777 0 ▶sub port_number {
Subroutine port_number: not called CC=2 SCAR=17.9
Pod documented
1778 0 my $self = shift;
1779 0 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1780 0 $self->{port_num} = shift @_;
1781 0 $self->service_check(1);
1782 }
1783 0 return $self->{port_num};
1784 }
1785  
1786 0 ▶sub ntop {
Subroutine ntop: not called CC=2 SCAR=17.9
Pod documented
1787 0 my($self, $ip) = @_;
1788  
1789 # Vista doesn't define a inet_ntop. It has InetNtop instead.
1790 # Not following ANSI... priceless. getnameinfo() is defined
1791 # for Windows 2000 and later, so that may be the choice.
1792  
1793 # Any port will work, even undef, but this will work for now.
1794 # Socket warns when undef is passed in, but it still works.
1795 0 my $port = getservbyname('echo', 'udp');
1796 0 my $sockaddr = _pack_sockaddr_in($port, $ip);
1797 0 my ($error, $address) = getnameinfo($sockaddr, $NI_NUMERICHOST);
1798 0 ▶ croak $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
1799 0 return $address;
1800 }
1801  
1802 0 ▶sub wakeonlan {
Subroutine wakeonlan: not called CC=5 SCAR=34.0
Pod documented
1803 0 my ($mac_addr, $host, $port) = @_;
1804  
1805 # use the discard service if $port not passed in
1806 0 ▶ if (! defined $host) { $host = '255.255.255.255' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 0 0 0
1807 0 ▶ if (! defined $port || $port !~ /^\d+$/ ) { $port = 9 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port and $port =~ /^\d+$/) 0 0 0
Condition0%0/3
defined $port and $port =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $port and $port =~ /^\d+$/
defined $port $port =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $portB= $port =~ /^\d+$/
ABresult
0X0
100
111
1808  
1809 0 require IO::Socket::INET;
1810 0 ▶ my $sock = IO::Socket::INET->new(Proto=>'udp') || return undef;
Condition0%0/2
'IO::Socket::INET'->new('Proto', 'udp') || (return undef)
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new('Proto', 'udp') || (return undef)
'IO::Socket::INET'->new('Proto', 'udp')
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Proto', 'udp')
Aresult
11
00
1811  
1812 0 my $ip_addr = inet_aton($host);
1813 0 my $sock_addr = sockaddr_in($port, $ip_addr);
1814 0 $mac_addr =~ s/://g;
1815 0 my $packet = pack('C6H*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, $mac_addr x 16);
1816  
1817 0 setsockopt($sock, SOL_SOCKET, SO_BROADCAST, 1);
1818 0 send($sock, $packet, 0, $sock_addr);
1819 0 $sock->close;
1820  
1821 0 return 1;
1822 }
1823  
1824 ########################################################
1825 # DNS hostname resolution
1826 # return:
1827 # $h->{name} = host - as passed in
1828 # $h->{host} = host - as passed in without :port
1829 # $h->{port} = OPTIONAL - if :port, then value of port
1830 # $h->{addr} = resolved numeric address
1831 # $h->{addr_in} = aton/pton result
1832 # $h->{family} = AF_INET/6
1833 ############################
1834 0 ▶sub _resolv {
Subroutine _resolv: not called CC=23 SCAR=63.1
1835 0 my ($self,
1836 $name,
1837 ) = @_;
1838  
1839 0 my %h;
1840 0 $h{name} = $name;
1841 0 my $family = $self->{family};
1842  
1843 0 ▶ if (defined($self->{family_local})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"family_local"}) 0 0 0
1844 $family = $self->{family_local}
1845 0 }
1846  
1847 # START - host:port
1848 0 my $cnt = 0;
1849  
1850 # Count ":"
1851 0 ▶ $cnt++ while ($name =~ m/:/g);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /:/g and do { ++$cnt } 0 0 0
1852  
1853 # 0 = hostname or IPv4 address
1854 0 ▶ if ($cnt == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($cnt == 0) { } 0 0 0
elsif ($cnt == 1) { } 0 0 0
elsif ($cnt >= 2) { } 0 0 0
1855 0 $h{host} = $name
1856 # 1 = IPv4 address with port
1857 } elsif ($cnt == 1) {
1858 0 ($h{host}, $h{port}) = split /:/, $name
1859 # >=2 = IPv6 address
1860 } elsif ($cnt >= 2) {
1861 #IPv6 with port - [2001::1]:port
1862 0 ▶ if ($name =~ /^\[.*\]:\d{1,5}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\[.*\]:\d{1,5}$/) { } 0 0 0
1863 0 ($h{host}, $h{port}) = split /:([^:]+)$/, $name # split after last :
1864 # IPv6 without port
1865 } else {
1866 0 $h{host} = $name
1867 }
1868 }
1869  
1870 # Clean up host
1871 0 $h{host} =~ s/\[//g;
1872 0 $h{host} =~ s/\]//g;
1873 # Clean up port
1874 0 ▶ if (defined($h{port}) && (($h{port} !~ /^\d{1,5}$/) || ($h{port} < 1) || ($h{port} > 65535))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h{"port"} and !($h{"port"} =~ /^\d{1,5}$/) || $h{"port"} < 1 || $h{"port"} > 65535) 0 0 0
Condition0%0/9
defined $h{"port"} and !($h{"port"} =~ /^\d{1,5}$/) || $h{"port"} < 1 || $h{"port"} > 65535
!ll&&!rl&&r
000
not $h{"port"} =~ /^\d{1,5}$/ or $h{"port"} < 1 or $h{"port"} > 65535
l!l&&r!l&&!r
000
not $h{"port"} =~ /^\d{1,5}$/ or $h{"port"} < 1
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $h{"port"} and !($h{"port"} =~ /^\d{1,5}$/) || $h{"port"} < 1 || $h{"port"} > 65535
defined $h{"port"} not $h{"port"} =~ /^\d{1,5}$/ $h{"port"} < 1 $h{"port"} > 65535
Truth table0%0/5
A and not(B) or C or D
A= defined $h{"port"}B= not $h{"port"} =~ /^\d{1,5}$/C= $h{"port"} < 1D= $h{"port"} > 65535
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1875 0 croak("Invalid port `$h{port}' in `$name'");
1876 0 return undef;
1877 }
1878 # END - host:port
1879  
1880 # address check
1881 # new way
1882 0 ▶ if ($Socket_VERSION > 1.94) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Socket_VERSION > 1.94) { } 0 0 0
1883 0 my %hints = (
1884 family => $AF_UNSPEC,
1885 protocol => IPPROTO_TCP,
1886 flags => $AI_NUMERICHOST
1887 );
1888  
1889 # numeric address, return
1890 0 my ($err, @getaddr) = Socket::getaddrinfo($h{host}, undef, \%hints);
1891 0 ▶ if (defined($getaddr[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $getaddr[0]) 0 0 0
1892 0 $h{addr} = $h{host};
1893 0 $h{family} = $getaddr[0]->{family};
1894 0 ▶ if ($h{family} == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{'family'} == 2) { } 0 0 0
1895 0 (undef, $h{addr_in}, undef, undef) = Socket::unpack_sockaddr_in $getaddr[0]->{addr};
1896 } else {
1897 0 (undef, $h{addr_in}, undef, undef) = Socket::unpack_sockaddr_in6 $getaddr[0]->{addr};
1898 }
1899 0 return \%h
1900 }
1901 # old way
1902 } else {
1903 # numeric address, return
1904 0 my $ret = gethostbyname($h{host});
1905 0 ▶ if (defined($ret) && (_inet_ntoa($ret) eq $h{host})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret and _inet_ntoa($ret) eq $h{"host"}) 0 0 0
Condition0%0/3
defined $ret and _inet_ntoa($ret) eq $h{"host"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ret and _inet_ntoa($ret) eq $h{"host"}
defined $ret _inet_ntoa($ret) eq $h{"host"}
Truth table0%0/3
A and B
A= defined $retB= _inet_ntoa($ret) eq $h{"host"}
ABresult
0X0
100
111
1906 0 $h{addr} = $h{host};
1907 0 $h{addr_in} = $ret;
1908 0 $h{family} = AF_INET;
1909 0 return \%h
1910 }
1911 }
1912  
1913 # resolve
1914 # new way
1915 0 ▶ if ($Socket_VERSION >= 1.94) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Socket_VERSION >= 1.94) { } 0 0 0
1916 0 my %hints = (
1917 family => $family,
1918 protocol => IPPROTO_TCP
1919 );
1920  
1921 0 my ($err, @getaddr) = Socket::getaddrinfo($h{host}, undef, \%hints);
1922 0 ▶ if (defined($getaddr[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $getaddr[0]) { } 0 0 0
1923 0 my ($err, $address) = Socket::getnameinfo($getaddr[0]->{addr}, $NI_NUMERICHOST, $NIx_NOSERV);
1924 0 ▶ if (defined($address)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $address) { } 0 0 0
1925 0 $h{addr} = $address;
1926 0 $h{addr} =~ s/\%(.)*$//; # remove %ifID if IPv6
1927 0 $h{family} = $getaddr[0]->{family};
1928 0 ▶ if ($h{family} == AF_INET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{'family'} == 2) { } 0 0 0
1929 0 (undef, $h{addr_in}, undef, undef) = Socket::unpack_sockaddr_in $getaddr[0]->{addr};
1930 } else {
1931 0 (undef, $h{addr_in}, undef, undef) = Socket::unpack_sockaddr_in6 $getaddr[0]->{addr};
1932 }
1933 0 return \%h;
1934 } else {
1935 0 carp("getnameinfo($getaddr[0]->{addr}) failed - $err");
1936 0 return undef;
1937 }
1938 } else {
1939 0 ▶ warn(sprintf("getaddrinfo($h{host},,%s) failed - $err",
Branch: 1 branch
BranchTrueFalseTotal
$family == 2 ? : 0 0 0
1940 $family == AF_INET ? "AF_INET" : "AF_INET6"));
1941 0 return undef;
1942 }
1943 # old way
1944 } else {
1945 0 ▶ if ($family == $AF_INET6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family == $AF_INET6) 0 0 0
1946 0 croak("Socket >= 1.94 required for IPv6 - found Socket $Socket::VERSION");
1947 0 return undef;
1948 }
1949  
1950 0 my @gethost = gethostbyname($h{host});
1951 0 ▶ if (defined($gethost[4])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gethost[4]) { } 0 0 0
1952 0 $h{addr} = inet_ntoa($gethost[4]);
1953 0 $h{addr_in} = $gethost[4];
1954 0 $h{family} = AF_INET;
1955 0 return \%h
1956 } else {
1957 0 carp("gethostbyname($h{host}) failed - $^E");
1958 0 return undef;
1959 }
1960 }
1961 0 return undef;
1962 }
1963  
1964 0 ▶sub _pack_sockaddr_in($$) {
Subroutine _pack_sockaddr_in: not called CC=3 SCAR=24.8
1965 0 my ($port,
1966 $ip,
1967 ) = @_;
1968  
1969 0 ▶ my $addr = ref($ip) eq "HASH" ? $ip->{addr_in} : $ip;
Branch: 1 branch
BranchTrueFalseTotal
ref $ip eq 'HASH' ? : 0 0 0
1970 0 ▶ if (length($addr) <= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $addr <= 4) { } 0 0 0
1971 0 return Socket::pack_sockaddr_in($port, $addr);
1972 } else {
1973 0 return Socket::pack_sockaddr_in6($port, $addr);
1974 }
1975 }
1976  
1977 0 ▶sub _unpack_sockaddr_in($;$) {
Subroutine _unpack_sockaddr_in: not called CC=4 SCAR=30.0
1978 0 my ($addr,
1979 $family,
1980 ) = @_;
1981  
1982 0 my ($port, $host);
1983 0 ▶ if ($family == AF_INET || (!defined($family) and length($addr) <= 16 )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family == 2 or not defined $family and length $addr <= 16) { } 0 0 0
Condition0%0/6
$family == 2 or not defined $family and length $addr <= 16
l!l&&r!l&&!r
000
not defined $family and length $addr <= 16
!ll&&!rl&&r
000
MC/DC0%0/3
$family == 2 or not defined $family and length $addr <= 16
$family == 2 not defined $family length $addr <= 16
Truth table0%0/4
A or not(B) and C
A= $family == 2B= not defined $familyC= length $addr <= 16
ABCresult
1XX1
0111
00X0
0100
1984 0 ($port, $host) = Socket::unpack_sockaddr_in($addr);
1985 } else {
1986 0 ($port, $host) = Socket::unpack_sockaddr_in6($addr);
1987 }
1988 0 return $port, $host
1989 }
1990  
1991 0 ▶sub _inet_ntoa {
Subroutine _inet_ntoa: not called CC=3 SCAR=24.8
1992 0 my ($addr
1993 ) = @_;
1994  
1995 0 my $ret;
1996 0 ▶ if ($Socket_VERSION >= 1.94) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Socket_VERSION >= 1.94) { } 0 0 0
1997 0 my ($err, $address) = Socket::getnameinfo($addr, $NI_NUMERICHOST);
1998 0 ▶ if (defined($address)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $address) { } 0 0 0
1999 0 $ret = $address;
2000 } else {
2001 0 carp("getnameinfo($addr) failed - $err");
2002 }
2003 } else {
2004 0 $ret = inet_ntoa($addr)
2005 }
2006
2007 0 return $ret
2008 }
2009  
2010 7 1;
2011 __END__
↑ Summary