xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/Net/Ping.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage Net::Ping;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gaterequire 5.002;
4*0Sstevel@tonic-gaterequire Exporter;
5*0Sstevel@tonic-gate
6*0Sstevel@tonic-gateuse strict;
7*0Sstevel@tonic-gateuse vars qw(@ISA @EXPORT $VERSION
8*0Sstevel@tonic-gate            $def_timeout $def_proto $def_factor
9*0Sstevel@tonic-gate            $max_datasize $pingstring $hires $source_verify $syn_forking);
10*0Sstevel@tonic-gateuse Fcntl qw( F_GETFL F_SETFL O_NONBLOCK );
11*0Sstevel@tonic-gateuse Socket qw( SOCK_DGRAM SOCK_STREAM SOCK_RAW PF_INET SOL_SOCKET SO_ERROR
12*0Sstevel@tonic-gate               inet_aton inet_ntoa sockaddr_in );
13*0Sstevel@tonic-gateuse POSIX qw( ENOTCONN ECONNREFUSED ECONNRESET EINPROGRESS EWOULDBLOCK EAGAIN WNOHANG );
14*0Sstevel@tonic-gateuse FileHandle;
15*0Sstevel@tonic-gateuse Carp;
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate@ISA = qw(Exporter);
18*0Sstevel@tonic-gate@EXPORT = qw(pingecho);
19*0Sstevel@tonic-gate$VERSION = "2.31";
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gatesub SOL_IP { 0; };
22*0Sstevel@tonic-gatesub IP_TOS { 1; };
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate# Constants
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate$def_timeout = 5;           # Default timeout to wait for a reply
27*0Sstevel@tonic-gate$def_proto = "tcp";         # Default protocol to use for pinging
28*0Sstevel@tonic-gate$def_factor = 1.2;          # Default exponential backoff rate.
29*0Sstevel@tonic-gate$max_datasize = 1024;       # Maximum data bytes in a packet
30*0Sstevel@tonic-gate# The data we exchange with the server for the stream protocol
31*0Sstevel@tonic-gate$pingstring = "pingschwingping!\n";
32*0Sstevel@tonic-gate$source_verify = 1;         # Default is to verify source endpoint
33*0Sstevel@tonic-gate$syn_forking = 0;
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gateif ($^O =~ /Win32/i) {
36*0Sstevel@tonic-gate  # Hack to avoid this Win32 spewage:
37*0Sstevel@tonic-gate  # Your vendor has not defined POSIX macro ECONNREFUSED
38*0Sstevel@tonic-gate  *ECONNREFUSED = sub {10061;}; # "Unknown Error" Special Win32 Response?
39*0Sstevel@tonic-gate  *ENOTCONN     = sub {10057;};
40*0Sstevel@tonic-gate  *ECONNRESET   = sub {10054;};
41*0Sstevel@tonic-gate  *EINPROGRESS  = sub {10036;};
42*0Sstevel@tonic-gate  *EWOULDBLOCK  = sub {10035;};
43*0Sstevel@tonic-gate#  $syn_forking = 1;    # XXX possibly useful in < Win2K ?
44*0Sstevel@tonic-gate};
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate# h2ph "asm/socket.h"
47*0Sstevel@tonic-gate# require "asm/socket.ph";
48*0Sstevel@tonic-gatesub SO_BINDTODEVICE {25;}
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate# Description:  The pingecho() subroutine is provided for backward
51*0Sstevel@tonic-gate# compatibility with the original Net::Ping.  It accepts a host
52*0Sstevel@tonic-gate# name/IP and an optional timeout in seconds.  Create a tcp ping
53*0Sstevel@tonic-gate# object and try pinging the host.  The result of the ping is returned.
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gatesub pingecho
56*0Sstevel@tonic-gate{
57*0Sstevel@tonic-gate  my ($host,              # Name or IP number of host to ping
58*0Sstevel@tonic-gate      $timeout            # Optional timeout in seconds
59*0Sstevel@tonic-gate      ) = @_;
60*0Sstevel@tonic-gate  my ($p);                # A ping object
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate  $p = Net::Ping->new("tcp", $timeout);
63*0Sstevel@tonic-gate  $p->ping($host);        # Going out of scope closes the connection
64*0Sstevel@tonic-gate}
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate# Description:  The new() method creates a new ping object.  Optional
67*0Sstevel@tonic-gate# parameters may be specified for the protocol to use, the timeout in
68*0Sstevel@tonic-gate# seconds and the size in bytes of additional data which should be
69*0Sstevel@tonic-gate# included in the packet.
70*0Sstevel@tonic-gate#   After the optional parameters are checked, the data is constructed
71*0Sstevel@tonic-gate# and a socket is opened if appropriate.  The object is returned.
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gatesub new
74*0Sstevel@tonic-gate{
75*0Sstevel@tonic-gate  my ($this,
76*0Sstevel@tonic-gate      $proto,             # Optional protocol to use for pinging
77*0Sstevel@tonic-gate      $timeout,           # Optional timeout in seconds
78*0Sstevel@tonic-gate      $data_size,         # Optional additional bytes of data
79*0Sstevel@tonic-gate      $device,            # Optional device to use
80*0Sstevel@tonic-gate      $tos,               # Optional ToS to set
81*0Sstevel@tonic-gate      ) = @_;
82*0Sstevel@tonic-gate  my  $class = ref($this) || $this;
83*0Sstevel@tonic-gate  my  $self = {};
84*0Sstevel@tonic-gate  my ($cnt,               # Count through data bytes
85*0Sstevel@tonic-gate      $min_datasize       # Minimum data bytes required
86*0Sstevel@tonic-gate      );
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate  bless($self, $class);
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate  $proto = $def_proto unless $proto;          # Determine the protocol
91*0Sstevel@tonic-gate  croak('Protocol for ping must be "icmp", "udp", "tcp", "syn", "stream", or "external"')
92*0Sstevel@tonic-gate    unless $proto =~ m/^(icmp|udp|tcp|syn|stream|external)$/;
93*0Sstevel@tonic-gate  $self->{"proto"} = $proto;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate  $timeout = $def_timeout unless $timeout;    # Determine the timeout
96*0Sstevel@tonic-gate  croak("Default timeout for ping must be greater than 0 seconds")
97*0Sstevel@tonic-gate    if $timeout <= 0;
98*0Sstevel@tonic-gate  $self->{"timeout"} = $timeout;
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate  $self->{"device"} = $device;
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate  $self->{"tos"} = $tos;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate  $min_datasize = ($proto eq "udp") ? 1 : 0;  # Determine data size
105*0Sstevel@tonic-gate  $data_size = $min_datasize unless defined($data_size) && $proto ne "tcp";
106*0Sstevel@tonic-gate  croak("Data for ping must be from $min_datasize to $max_datasize bytes")
107*0Sstevel@tonic-gate    if ($data_size < $min_datasize) || ($data_size > $max_datasize);
108*0Sstevel@tonic-gate  $data_size-- if $self->{"proto"} eq "udp";  # We provide the first byte
109*0Sstevel@tonic-gate  $self->{"data_size"} = $data_size;
110*0Sstevel@tonic-gate
111*0Sstevel@tonic-gate  $self->{"data"} = "";                       # Construct data bytes
112*0Sstevel@tonic-gate  for ($cnt = 0; $cnt < $self->{"data_size"}; $cnt++)
113*0Sstevel@tonic-gate  {
114*0Sstevel@tonic-gate    $self->{"data"} .= chr($cnt % 256);
115*0Sstevel@tonic-gate  }
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate  $self->{"local_addr"} = undef;              # Don't bind by default
118*0Sstevel@tonic-gate  $self->{"retrans"} = $def_factor;           # Default exponential backoff rate
119*0Sstevel@tonic-gate  $self->{"econnrefused"} = undef;            # Default Connection refused behavior
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate  $self->{"seq"} = 0;                         # For counting packets
122*0Sstevel@tonic-gate  if ($self->{"proto"} eq "udp")              # Open a socket
123*0Sstevel@tonic-gate  {
124*0Sstevel@tonic-gate    $self->{"proto_num"} = (getprotobyname('udp'))[2] ||
125*0Sstevel@tonic-gate      croak("Can't udp protocol by name");
126*0Sstevel@tonic-gate    $self->{"port_num"} = (getservbyname('echo', 'udp'))[2] ||
127*0Sstevel@tonic-gate      croak("Can't get udp echo port by name");
128*0Sstevel@tonic-gate    $self->{"fh"} = FileHandle->new();
129*0Sstevel@tonic-gate    socket($self->{"fh"}, PF_INET, SOCK_DGRAM,
130*0Sstevel@tonic-gate           $self->{"proto_num"}) ||
131*0Sstevel@tonic-gate             croak("udp socket error - $!");
132*0Sstevel@tonic-gate    if ($self->{'device'}) {
133*0Sstevel@tonic-gate      setsockopt($self->{"fh"}, SOL_SOCKET, SO_BINDTODEVICE(), pack("Z*", $self->{'device'}))
134*0Sstevel@tonic-gate        or croak "error binding to device $self->{'device'} $!";
135*0Sstevel@tonic-gate    }
136*0Sstevel@tonic-gate    if ($self->{'tos'}) {
137*0Sstevel@tonic-gate      setsockopt($self->{"fh"}, SOL_IP, IP_TOS(), pack("I*", $self->{'tos'}))
138*0Sstevel@tonic-gate        or croak "error configuring tos to $self->{'tos'} $!";
139*0Sstevel@tonic-gate    }
140*0Sstevel@tonic-gate  }
141*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "icmp")
142*0Sstevel@tonic-gate  {
143*0Sstevel@tonic-gate    croak("icmp ping requires root privilege") if ($> and $^O ne 'VMS' and $^O ne 'cygwin');
144*0Sstevel@tonic-gate    $self->{"proto_num"} = (getprotobyname('icmp'))[2] ||
145*0Sstevel@tonic-gate      croak("Can't get icmp protocol by name");
146*0Sstevel@tonic-gate    $self->{"pid"} = $$ & 0xffff;           # Save lower 16 bits of pid
147*0Sstevel@tonic-gate    $self->{"fh"} = FileHandle->new();
148*0Sstevel@tonic-gate    socket($self->{"fh"}, PF_INET, SOCK_RAW, $self->{"proto_num"}) ||
149*0Sstevel@tonic-gate      croak("icmp socket error - $!");
150*0Sstevel@tonic-gate    if ($self->{'device'}) {
151*0Sstevel@tonic-gate      setsockopt($self->{"fh"}, SOL_SOCKET, SO_BINDTODEVICE(), pack("Z*", $self->{'device'}))
152*0Sstevel@tonic-gate        or croak "error binding to device $self->{'device'} $!";
153*0Sstevel@tonic-gate    }
154*0Sstevel@tonic-gate    if ($self->{'tos'}) {
155*0Sstevel@tonic-gate      setsockopt($self->{"fh"}, SOL_IP, IP_TOS(), pack("I*", $self->{'tos'}))
156*0Sstevel@tonic-gate        or croak "error configuring tos to $self->{'tos'} $!";
157*0Sstevel@tonic-gate    }
158*0Sstevel@tonic-gate  }
159*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "tcp" || $self->{"proto"} eq "stream")
160*0Sstevel@tonic-gate  {
161*0Sstevel@tonic-gate    $self->{"proto_num"} = (getprotobyname('tcp'))[2] ||
162*0Sstevel@tonic-gate      croak("Can't get tcp protocol by name");
163*0Sstevel@tonic-gate    $self->{"port_num"} = (getservbyname('echo', 'tcp'))[2] ||
164*0Sstevel@tonic-gate      croak("Can't get tcp echo port by name");
165*0Sstevel@tonic-gate    $self->{"fh"} = FileHandle->new();
166*0Sstevel@tonic-gate  }
167*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "syn")
168*0Sstevel@tonic-gate  {
169*0Sstevel@tonic-gate    $self->{"proto_num"} = (getprotobyname('tcp'))[2] ||
170*0Sstevel@tonic-gate      croak("Can't get tcp protocol by name");
171*0Sstevel@tonic-gate    $self->{"port_num"} = (getservbyname('echo', 'tcp'))[2] ||
172*0Sstevel@tonic-gate      croak("Can't get tcp echo port by name");
173*0Sstevel@tonic-gate    if ($syn_forking) {
174*0Sstevel@tonic-gate      $self->{"fork_rd"} = FileHandle->new();
175*0Sstevel@tonic-gate      $self->{"fork_wr"} = FileHandle->new();
176*0Sstevel@tonic-gate      pipe($self->{"fork_rd"}, $self->{"fork_wr"});
177*0Sstevel@tonic-gate      $self->{"fh"} = FileHandle->new();
178*0Sstevel@tonic-gate      $self->{"good"} = {};
179*0Sstevel@tonic-gate      $self->{"bad"} = {};
180*0Sstevel@tonic-gate    } else {
181*0Sstevel@tonic-gate      $self->{"wbits"} = "";
182*0Sstevel@tonic-gate      $self->{"bad"} = {};
183*0Sstevel@tonic-gate    }
184*0Sstevel@tonic-gate    $self->{"syn"} = {};
185*0Sstevel@tonic-gate    $self->{"stop_time"} = 0;
186*0Sstevel@tonic-gate  }
187*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "external")
188*0Sstevel@tonic-gate  {
189*0Sstevel@tonic-gate    # No preliminary work needs to be done.
190*0Sstevel@tonic-gate  }
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate  return($self);
193*0Sstevel@tonic-gate}
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate# Description: Set the local IP address from which pings will be sent.
196*0Sstevel@tonic-gate# For ICMP and UDP pings, this calls bind() on the already-opened socket;
197*0Sstevel@tonic-gate# for TCP pings, just saves the address to be used when the socket is
198*0Sstevel@tonic-gate# opened.  Returns non-zero if successful; croaks on error.
199*0Sstevel@tonic-gatesub bind
200*0Sstevel@tonic-gate{
201*0Sstevel@tonic-gate  my ($self,
202*0Sstevel@tonic-gate      $local_addr         # Name or IP number of local interface
203*0Sstevel@tonic-gate      ) = @_;
204*0Sstevel@tonic-gate  my ($ip                 # Packed IP number of $local_addr
205*0Sstevel@tonic-gate      );
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate  croak("Usage: \$p->bind(\$local_addr)") unless @_ == 2;
208*0Sstevel@tonic-gate  croak("already bound") if defined($self->{"local_addr"}) &&
209*0Sstevel@tonic-gate    ($self->{"proto"} eq "udp" || $self->{"proto"} eq "icmp");
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate  $ip = inet_aton($local_addr);
212*0Sstevel@tonic-gate  croak("nonexistent local address $local_addr") unless defined($ip);
213*0Sstevel@tonic-gate  $self->{"local_addr"} = $ip; # Only used if proto is tcp
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gate  if ($self->{"proto"} eq "udp" || $self->{"proto"} eq "icmp")
216*0Sstevel@tonic-gate  {
217*0Sstevel@tonic-gate  CORE::bind($self->{"fh"}, sockaddr_in(0, $ip)) ||
218*0Sstevel@tonic-gate    croak("$self->{'proto'} bind error - $!");
219*0Sstevel@tonic-gate  }
220*0Sstevel@tonic-gate  elsif (($self->{"proto"} ne "tcp") && ($self->{"proto"} ne "syn"))
221*0Sstevel@tonic-gate  {
222*0Sstevel@tonic-gate    croak("Unknown protocol \"$self->{proto}\" in bind()");
223*0Sstevel@tonic-gate  }
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate  return 1;
226*0Sstevel@tonic-gate}
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate# Description: A select() wrapper that compensates for platform
229*0Sstevel@tonic-gate# peculiarities.
230*0Sstevel@tonic-gatesub mselect
231*0Sstevel@tonic-gate{
232*0Sstevel@tonic-gate    if ($_[3] > 0 and $^O eq 'MSWin32') {
233*0Sstevel@tonic-gate	# On windows, select() doesn't process the message loop,
234*0Sstevel@tonic-gate	# but sleep() will, allowing alarm() to interrupt the latter.
235*0Sstevel@tonic-gate	# So we chop up the timeout into smaller pieces and interleave
236*0Sstevel@tonic-gate	# select() and sleep() calls.
237*0Sstevel@tonic-gate	my $t = $_[3];
238*0Sstevel@tonic-gate	my $gran = 0.5;  # polling granularity in seconds
239*0Sstevel@tonic-gate	my @args = @_;
240*0Sstevel@tonic-gate	while (1) {
241*0Sstevel@tonic-gate	    $gran = $t if $gran > $t;
242*0Sstevel@tonic-gate	    my $nfound = select($_[0], $_[1], $_[2], $gran);
243*0Sstevel@tonic-gate	    $t -= $gran;
244*0Sstevel@tonic-gate	    return $nfound if $nfound or !defined($nfound) or $t <= 0;
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate	    sleep(0);
247*0Sstevel@tonic-gate	    ($_[0], $_[1], $_[2]) = @args;
248*0Sstevel@tonic-gate	}
249*0Sstevel@tonic-gate    }
250*0Sstevel@tonic-gate    else {
251*0Sstevel@tonic-gate	return select($_[0], $_[1], $_[2], $_[3]);
252*0Sstevel@tonic-gate    }
253*0Sstevel@tonic-gate}
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate# Description: Allow UDP source endpoint comparision to be
256*0Sstevel@tonic-gate#              skipped for those remote interfaces that do
257*0Sstevel@tonic-gate#              not response from the same endpoint.
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gatesub source_verify
260*0Sstevel@tonic-gate{
261*0Sstevel@tonic-gate  my $self = shift;
262*0Sstevel@tonic-gate  $source_verify = 1 unless defined
263*0Sstevel@tonic-gate    ($source_verify = ((defined $self) && (ref $self)) ? shift() : $self);
264*0Sstevel@tonic-gate}
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate# Description: Set whether or not the connect
267*0Sstevel@tonic-gate# behavior should enforce remote service
268*0Sstevel@tonic-gate# availability as well as reachability.
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gatesub service_check
271*0Sstevel@tonic-gate{
272*0Sstevel@tonic-gate  my $self = shift;
273*0Sstevel@tonic-gate  $self->{"econnrefused"} = 1 unless defined
274*0Sstevel@tonic-gate    ($self->{"econnrefused"} = shift());
275*0Sstevel@tonic-gate}
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gatesub tcp_service_check
278*0Sstevel@tonic-gate{
279*0Sstevel@tonic-gate  service_check(@_);
280*0Sstevel@tonic-gate}
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate# Description: Set exponential backoff for retransmission.
283*0Sstevel@tonic-gate# Should be > 1 to retain exponential properties.
284*0Sstevel@tonic-gate# If set to 0, retransmissions are disabled.
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gatesub retrans
287*0Sstevel@tonic-gate{
288*0Sstevel@tonic-gate  my $self = shift;
289*0Sstevel@tonic-gate  $self->{"retrans"} = shift;
290*0Sstevel@tonic-gate}
291*0Sstevel@tonic-gate
292*0Sstevel@tonic-gate# Description: allows the module to use milliseconds as returned by
293*0Sstevel@tonic-gate# the Time::HiRes module
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gate$hires = 0;
296*0Sstevel@tonic-gatesub hires
297*0Sstevel@tonic-gate{
298*0Sstevel@tonic-gate  my $self = shift;
299*0Sstevel@tonic-gate  $hires = 1 unless defined
300*0Sstevel@tonic-gate    ($hires = ((defined $self) && (ref $self)) ? shift() : $self);
301*0Sstevel@tonic-gate  require Time::HiRes if $hires;
302*0Sstevel@tonic-gate}
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gatesub time
305*0Sstevel@tonic-gate{
306*0Sstevel@tonic-gate  return $hires ? Time::HiRes::time() : CORE::time();
307*0Sstevel@tonic-gate}
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gate# Description: Sets or clears the O_NONBLOCK flag on a file handle.
310*0Sstevel@tonic-gatesub socket_blocking_mode
311*0Sstevel@tonic-gate{
312*0Sstevel@tonic-gate  my ($self,
313*0Sstevel@tonic-gate      $fh,              # the file handle whose flags are to be modified
314*0Sstevel@tonic-gate      $block) = @_;     # if true then set the blocking
315*0Sstevel@tonic-gate                        # mode (clear O_NONBLOCK), otherwise
316*0Sstevel@tonic-gate                        # set the non-blocking mode (set O_NONBLOCK)
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate  my $flags;
319*0Sstevel@tonic-gate  if ($^O eq 'MSWin32' || $^O eq 'VMS') {
320*0Sstevel@tonic-gate      # FIONBIO enables non-blocking sockets on windows and vms.
321*0Sstevel@tonic-gate      # FIONBIO is (0x80000000|(4<<16)|(ord('f')<<8)|126), as per winsock.h, ioctl.h
322*0Sstevel@tonic-gate      my $f = 0x8004667e;
323*0Sstevel@tonic-gate      my $v = pack("L", $block ? 0 : 1);
324*0Sstevel@tonic-gate      ioctl($fh, $f, $v) or croak("ioctl failed: $!");
325*0Sstevel@tonic-gate      return;
326*0Sstevel@tonic-gate  }
327*0Sstevel@tonic-gate  if ($flags = fcntl($fh, F_GETFL, 0)) {
328*0Sstevel@tonic-gate    $flags = $block ? ($flags & ~O_NONBLOCK) : ($flags | O_NONBLOCK);
329*0Sstevel@tonic-gate    if (!fcntl($fh, F_SETFL, $flags)) {
330*0Sstevel@tonic-gate      croak("fcntl F_SETFL: $!");
331*0Sstevel@tonic-gate    }
332*0Sstevel@tonic-gate  } else {
333*0Sstevel@tonic-gate    croak("fcntl F_GETFL: $!");
334*0Sstevel@tonic-gate  }
335*0Sstevel@tonic-gate}
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate# Description: Ping a host name or IP number with an optional timeout.
338*0Sstevel@tonic-gate# First lookup the host, and return undef if it is not found.  Otherwise
339*0Sstevel@tonic-gate# perform the specific ping method based on the protocol.  Return the
340*0Sstevel@tonic-gate# result of the ping.
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gatesub ping
343*0Sstevel@tonic-gate{
344*0Sstevel@tonic-gate  my ($self,
345*0Sstevel@tonic-gate      $host,              # Name or IP number of host to ping
346*0Sstevel@tonic-gate      $timeout,           # Seconds after which ping times out
347*0Sstevel@tonic-gate      ) = @_;
348*0Sstevel@tonic-gate  my ($ip,                # Packed IP number of $host
349*0Sstevel@tonic-gate      $ret,               # The return value
350*0Sstevel@tonic-gate      $ping_time,         # When ping began
351*0Sstevel@tonic-gate      );
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate  croak("Usage: \$p->ping(\$host [, \$timeout])") unless @_ == 2 || @_ == 3;
354*0Sstevel@tonic-gate  $timeout = $self->{"timeout"} unless $timeout;
355*0Sstevel@tonic-gate  croak("Timeout must be greater than 0 seconds") if $timeout <= 0;
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate  $ip = inet_aton($host);
358*0Sstevel@tonic-gate  return () unless defined($ip);      # Does host exist?
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate  # Dispatch to the appropriate routine.
361*0Sstevel@tonic-gate  $ping_time = &time();
362*0Sstevel@tonic-gate  if ($self->{"proto"} eq "external") {
363*0Sstevel@tonic-gate    $ret = $self->ping_external($ip, $timeout);
364*0Sstevel@tonic-gate  }
365*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "udp") {
366*0Sstevel@tonic-gate    $ret = $self->ping_udp($ip, $timeout);
367*0Sstevel@tonic-gate  }
368*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "icmp") {
369*0Sstevel@tonic-gate    $ret = $self->ping_icmp($ip, $timeout);
370*0Sstevel@tonic-gate  }
371*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "tcp") {
372*0Sstevel@tonic-gate    $ret = $self->ping_tcp($ip, $timeout);
373*0Sstevel@tonic-gate  }
374*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "stream") {
375*0Sstevel@tonic-gate    $ret = $self->ping_stream($ip, $timeout);
376*0Sstevel@tonic-gate  }
377*0Sstevel@tonic-gate  elsif ($self->{"proto"} eq "syn") {
378*0Sstevel@tonic-gate    $ret = $self->ping_syn($host, $ip, $ping_time, $ping_time+$timeout);
379*0Sstevel@tonic-gate  } else {
380*0Sstevel@tonic-gate    croak("Unknown protocol \"$self->{proto}\" in ping()");
381*0Sstevel@tonic-gate  }
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate  return wantarray ? ($ret, &time() - $ping_time, inet_ntoa($ip)) : $ret;
384*0Sstevel@tonic-gate}
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate# Uses Net::Ping::External to do an external ping.
387*0Sstevel@tonic-gatesub ping_external {
388*0Sstevel@tonic-gate  my ($self,
389*0Sstevel@tonic-gate      $ip,                # Packed IP number of the host
390*0Sstevel@tonic-gate      $timeout            # Seconds after which ping times out
391*0Sstevel@tonic-gate     ) = @_;
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate  eval { require Net::Ping::External; }
394*0Sstevel@tonic-gate    or croak('Protocol "external" not supported on your system: Net::Ping::External not found');
395*0Sstevel@tonic-gate  return Net::Ping::External::ping(ip => $ip, timeout => $timeout);
396*0Sstevel@tonic-gate}
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gateuse constant ICMP_ECHOREPLY => 0; # ICMP packet types
399*0Sstevel@tonic-gateuse constant ICMP_ECHO      => 8;
400*0Sstevel@tonic-gateuse constant ICMP_STRUCT    => "C2 n3 A";  # Structure of a minimal ICMP packet
401*0Sstevel@tonic-gateuse constant SUBCODE        => 0; # No ICMP subcode for ECHO and ECHOREPLY
402*0Sstevel@tonic-gateuse constant ICMP_FLAGS     => 0; # No special flags for send or recv
403*0Sstevel@tonic-gateuse constant ICMP_PORT      => 0; # No port with ICMP
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gatesub ping_icmp
406*0Sstevel@tonic-gate{
407*0Sstevel@tonic-gate  my ($self,
408*0Sstevel@tonic-gate      $ip,                # Packed IP number of the host
409*0Sstevel@tonic-gate      $timeout            # Seconds after which ping times out
410*0Sstevel@tonic-gate      ) = @_;
411*0Sstevel@tonic-gate
412*0Sstevel@tonic-gate  my ($saddr,             # sockaddr_in with port and ip
413*0Sstevel@tonic-gate      $checksum,          # Checksum of ICMP packet
414*0Sstevel@tonic-gate      $msg,               # ICMP packet to send
415*0Sstevel@tonic-gate      $len_msg,           # Length of $msg
416*0Sstevel@tonic-gate      $rbits,             # Read bits, filehandles for reading
417*0Sstevel@tonic-gate      $nfound,            # Number of ready filehandles found
418*0Sstevel@tonic-gate      $finish_time,       # Time ping should be finished
419*0Sstevel@tonic-gate      $done,              # set to 1 when we are done
420*0Sstevel@tonic-gate      $ret,               # Return value
421*0Sstevel@tonic-gate      $recv_msg,          # Received message including IP header
422*0Sstevel@tonic-gate      $from_saddr,        # sockaddr_in of sender
423*0Sstevel@tonic-gate      $from_port,         # Port packet was sent from
424*0Sstevel@tonic-gate      $from_ip,           # Packed IP of sender
425*0Sstevel@tonic-gate      $from_type,         # ICMP type
426*0Sstevel@tonic-gate      $from_subcode,      # ICMP subcode
427*0Sstevel@tonic-gate      $from_chk,          # ICMP packet checksum
428*0Sstevel@tonic-gate      $from_pid,          # ICMP packet id
429*0Sstevel@tonic-gate      $from_seq,          # ICMP packet sequence
430*0Sstevel@tonic-gate      $from_msg           # ICMP message
431*0Sstevel@tonic-gate      );
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gate  $self->{"seq"} = ($self->{"seq"} + 1) % 65536; # Increment sequence
434*0Sstevel@tonic-gate  $checksum = 0;                          # No checksum for starters
435*0Sstevel@tonic-gate  $msg = pack(ICMP_STRUCT . $self->{"data_size"}, ICMP_ECHO, SUBCODE,
436*0Sstevel@tonic-gate              $checksum, $self->{"pid"}, $self->{"seq"}, $self->{"data"});
437*0Sstevel@tonic-gate  $checksum = Net::Ping->checksum($msg);
438*0Sstevel@tonic-gate  $msg = pack(ICMP_STRUCT . $self->{"data_size"}, ICMP_ECHO, SUBCODE,
439*0Sstevel@tonic-gate              $checksum, $self->{"pid"}, $self->{"seq"}, $self->{"data"});
440*0Sstevel@tonic-gate  $len_msg = length($msg);
441*0Sstevel@tonic-gate  $saddr = sockaddr_in(ICMP_PORT, $ip);
442*0Sstevel@tonic-gate  $self->{"from_ip"} = undef;
443*0Sstevel@tonic-gate  $self->{"from_type"} = undef;
444*0Sstevel@tonic-gate  $self->{"from_subcode"} = undef;
445*0Sstevel@tonic-gate  send($self->{"fh"}, $msg, ICMP_FLAGS, $saddr); # Send the message
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gate  $rbits = "";
448*0Sstevel@tonic-gate  vec($rbits, $self->{"fh"}->fileno(), 1) = 1;
449*0Sstevel@tonic-gate  $ret = 0;
450*0Sstevel@tonic-gate  $done = 0;
451*0Sstevel@tonic-gate  $finish_time = &time() + $timeout;      # Must be done by this time
452*0Sstevel@tonic-gate  while (!$done && $timeout > 0)          # Keep trying if we have time
453*0Sstevel@tonic-gate  {
454*0Sstevel@tonic-gate    $nfound = mselect((my $rout=$rbits), undef, undef, $timeout); # Wait for packet
455*0Sstevel@tonic-gate    $timeout = $finish_time - &time();    # Get remaining time
456*0Sstevel@tonic-gate    if (!defined($nfound))                # Hmm, a strange error
457*0Sstevel@tonic-gate    {
458*0Sstevel@tonic-gate      $ret = undef;
459*0Sstevel@tonic-gate      $done = 1;
460*0Sstevel@tonic-gate    }
461*0Sstevel@tonic-gate    elsif ($nfound)                     # Got a packet from somewhere
462*0Sstevel@tonic-gate    {
463*0Sstevel@tonic-gate      $recv_msg = "";
464*0Sstevel@tonic-gate      $from_pid = -1;
465*0Sstevel@tonic-gate      $from_seq = -1;
466*0Sstevel@tonic-gate      $from_saddr = recv($self->{"fh"}, $recv_msg, 1500, ICMP_FLAGS);
467*0Sstevel@tonic-gate      ($from_port, $from_ip) = sockaddr_in($from_saddr);
468*0Sstevel@tonic-gate      ($from_type, $from_subcode) = unpack("C2", substr($recv_msg, 20, 2));
469*0Sstevel@tonic-gate      if ($from_type == ICMP_ECHOREPLY) {
470*0Sstevel@tonic-gate        ($from_pid, $from_seq) = unpack("n3", substr($recv_msg, 24, 4))
471*0Sstevel@tonic-gate          if length $recv_msg >= 28;
472*0Sstevel@tonic-gate      } else {
473*0Sstevel@tonic-gate        ($from_pid, $from_seq) = unpack("n3", substr($recv_msg, 52, 4))
474*0Sstevel@tonic-gate          if length $recv_msg >= 56;
475*0Sstevel@tonic-gate      }
476*0Sstevel@tonic-gate      $self->{"from_ip"} = $from_ip;
477*0Sstevel@tonic-gate      $self->{"from_type"} = $from_type;
478*0Sstevel@tonic-gate      $self->{"from_subcode"} = $from_subcode;
479*0Sstevel@tonic-gate      if (($from_pid == $self->{"pid"}) && # Does the packet check out?
480*0Sstevel@tonic-gate          ($from_seq == $self->{"seq"})) {
481*0Sstevel@tonic-gate        if ($from_type == ICMP_ECHOREPLY){
482*0Sstevel@tonic-gate          $ret = 1;
483*0Sstevel@tonic-gate        }
484*0Sstevel@tonic-gate        $done = 1;
485*0Sstevel@tonic-gate      }
486*0Sstevel@tonic-gate    } else {     # Oops, timed out
487*0Sstevel@tonic-gate      $done = 1;
488*0Sstevel@tonic-gate    }
489*0Sstevel@tonic-gate  }
490*0Sstevel@tonic-gate  return $ret;
491*0Sstevel@tonic-gate}
492*0Sstevel@tonic-gate
493*0Sstevel@tonic-gatesub icmp_result {
494*0Sstevel@tonic-gate  my ($self) = @_;
495*0Sstevel@tonic-gate  my $ip = $self->{"from_ip"} || "";
496*0Sstevel@tonic-gate  $ip = "\0\0\0\0" unless 4 == length $ip;
497*0Sstevel@tonic-gate  return (inet_ntoa($ip),($self->{"from_type"} || 0), ($self->{"from_subcode"} || 0));
498*0Sstevel@tonic-gate}
499*0Sstevel@tonic-gate
500*0Sstevel@tonic-gate# Description:  Do a checksum on the message.  Basically sum all of
501*0Sstevel@tonic-gate# the short words and fold the high order bits into the low order bits.
502*0Sstevel@tonic-gate
503*0Sstevel@tonic-gatesub checksum
504*0Sstevel@tonic-gate{
505*0Sstevel@tonic-gate  my ($class,
506*0Sstevel@tonic-gate      $msg            # The message to checksum
507*0Sstevel@tonic-gate      ) = @_;
508*0Sstevel@tonic-gate  my ($len_msg,       # Length of the message
509*0Sstevel@tonic-gate      $num_short,     # The number of short words in the message
510*0Sstevel@tonic-gate      $short,         # One short word
511*0Sstevel@tonic-gate      $chk            # The checksum
512*0Sstevel@tonic-gate      );
513*0Sstevel@tonic-gate
514*0Sstevel@tonic-gate  $len_msg = length($msg);
515*0Sstevel@tonic-gate  $num_short = int($len_msg / 2);
516*0Sstevel@tonic-gate  $chk = 0;
517*0Sstevel@tonic-gate  foreach $short (unpack("n$num_short", $msg))
518*0Sstevel@tonic-gate  {
519*0Sstevel@tonic-gate    $chk += $short;
520*0Sstevel@tonic-gate  }                                           # Add the odd byte in
521*0Sstevel@tonic-gate  $chk += (unpack("C", substr($msg, $len_msg - 1, 1)) << 8) if $len_msg % 2;
522*0Sstevel@tonic-gate  $chk = ($chk >> 16) + ($chk & 0xffff);      # Fold high into low
523*0Sstevel@tonic-gate  return(~(($chk >> 16) + $chk) & 0xffff);    # Again and complement
524*0Sstevel@tonic-gate}
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate# Description:  Perform a tcp echo ping.  Since a tcp connection is
528*0Sstevel@tonic-gate# host specific, we have to open and close each connection here.  We
529*0Sstevel@tonic-gate# can't just leave a socket open.  Because of the robust nature of
530*0Sstevel@tonic-gate# tcp, it will take a while before it gives up trying to establish a
531*0Sstevel@tonic-gate# connection.  Therefore, we use select() on a non-blocking socket to
532*0Sstevel@tonic-gate# check against our timeout.  No data bytes are actually
533*0Sstevel@tonic-gate# sent since the successful establishment of a connection is proof
534*0Sstevel@tonic-gate# enough of the reachability of the remote host.  Also, tcp is
535*0Sstevel@tonic-gate# expensive and doesn't need our help to add to the overhead.
536*0Sstevel@tonic-gate
537*0Sstevel@tonic-gatesub ping_tcp
538*0Sstevel@tonic-gate{
539*0Sstevel@tonic-gate  my ($self,
540*0Sstevel@tonic-gate      $ip,                # Packed IP number of the host
541*0Sstevel@tonic-gate      $timeout            # Seconds after which ping times out
542*0Sstevel@tonic-gate      ) = @_;
543*0Sstevel@tonic-gate  my ($ret                # The return value
544*0Sstevel@tonic-gate      );
545*0Sstevel@tonic-gate
546*0Sstevel@tonic-gate  $! = 0;
547*0Sstevel@tonic-gate  $ret = $self -> tcp_connect( $ip, $timeout);
548*0Sstevel@tonic-gate  if (!$self->{"econnrefused"} &&
549*0Sstevel@tonic-gate      $! == ECONNREFUSED) {
550*0Sstevel@tonic-gate    $ret = 1;  # "Connection refused" means reachable
551*0Sstevel@tonic-gate  }
552*0Sstevel@tonic-gate  $self->{"fh"}->close();
553*0Sstevel@tonic-gate  return $ret;
554*0Sstevel@tonic-gate}
555*0Sstevel@tonic-gate
556*0Sstevel@tonic-gatesub tcp_connect
557*0Sstevel@tonic-gate{
558*0Sstevel@tonic-gate  my ($self,
559*0Sstevel@tonic-gate      $ip,                # Packed IP number of the host
560*0Sstevel@tonic-gate      $timeout            # Seconds after which connect times out
561*0Sstevel@tonic-gate      ) = @_;
562*0Sstevel@tonic-gate  my ($saddr);            # Packed IP and Port
563*0Sstevel@tonic-gate
564*0Sstevel@tonic-gate  $saddr = sockaddr_in($self->{"port_num"}, $ip);
565*0Sstevel@tonic-gate
566*0Sstevel@tonic-gate  my $ret = 0;            # Default to unreachable
567*0Sstevel@tonic-gate
568*0Sstevel@tonic-gate  my $do_socket = sub {
569*0Sstevel@tonic-gate    socket($self->{"fh"}, PF_INET, SOCK_STREAM, $self->{"proto_num"}) ||
570*0Sstevel@tonic-gate      croak("tcp socket error - $!");
571*0Sstevel@tonic-gate    if (defined $self->{"local_addr"} &&
572*0Sstevel@tonic-gate        !CORE::bind($self->{"fh"}, sockaddr_in(0, $self->{"local_addr"}))) {
573*0Sstevel@tonic-gate      croak("tcp bind error - $!");
574*0Sstevel@tonic-gate    }
575*0Sstevel@tonic-gate    if ($self->{'device'}) {
576*0Sstevel@tonic-gate      setsockopt($self->{"fh"}, SOL_SOCKET, SO_BINDTODEVICE(), pack("Z*", $self->{'device'}))
577*0Sstevel@tonic-gate        or croak("error binding to device $self->{'device'} $!");
578*0Sstevel@tonic-gate    }
579*0Sstevel@tonic-gate    if ($self->{'tos'}) {
580*0Sstevel@tonic-gate      setsockopt($self->{"fh"}, SOL_IP, IP_TOS(), pack("I*", $self->{'tos'}))
581*0Sstevel@tonic-gate        or croak "error configuring tos to $self->{'tos'} $!";
582*0Sstevel@tonic-gate    }
583*0Sstevel@tonic-gate  };
584*0Sstevel@tonic-gate  my $do_connect = sub {
585*0Sstevel@tonic-gate    $self->{"ip"} = $ip;
586*0Sstevel@tonic-gate    # ECONNREFUSED is 10061 on MSWin32. If we pass it as child error through $?,
587*0Sstevel@tonic-gate    # we'll get (10061 & 255) = 77, so we cannot check it in the parent process.
588*0Sstevel@tonic-gate    return ($ret = connect($self->{"fh"}, $saddr) || ($! == ECONNREFUSED && !$self->{"econnrefused"}));
589*0Sstevel@tonic-gate  };
590*0Sstevel@tonic-gate  my $do_connect_nb = sub {
591*0Sstevel@tonic-gate    # Set O_NONBLOCK property on filehandle
592*0Sstevel@tonic-gate    $self->socket_blocking_mode($self->{"fh"}, 0);
593*0Sstevel@tonic-gate
594*0Sstevel@tonic-gate    # start the connection attempt
595*0Sstevel@tonic-gate    if (!connect($self->{"fh"}, $saddr)) {
596*0Sstevel@tonic-gate      if ($! == ECONNREFUSED) {
597*0Sstevel@tonic-gate        $ret = 1 unless $self->{"econnrefused"};
598*0Sstevel@tonic-gate      } elsif ($! != EINPROGRESS && ($^O ne 'MSWin32' || $! != EWOULDBLOCK)) {
599*0Sstevel@tonic-gate        # EINPROGRESS is the expected error code after a connect()
600*0Sstevel@tonic-gate        # on a non-blocking socket.  But if the kernel immediately
601*0Sstevel@tonic-gate        # determined that this connect() will never work,
602*0Sstevel@tonic-gate        # Simply respond with "unreachable" status.
603*0Sstevel@tonic-gate        # (This can occur on some platforms with errno
604*0Sstevel@tonic-gate        # EHOSTUNREACH or ENETUNREACH.)
605*0Sstevel@tonic-gate        return 0;
606*0Sstevel@tonic-gate      } else {
607*0Sstevel@tonic-gate        # Got the expected EINPROGRESS.
608*0Sstevel@tonic-gate        # Just wait for connection completion...
609*0Sstevel@tonic-gate        my ($wbits, $wout, $wexc);
610*0Sstevel@tonic-gate        $wout = $wexc = $wbits = "";
611*0Sstevel@tonic-gate        vec($wbits, $self->{"fh"}->fileno, 1) = 1;
612*0Sstevel@tonic-gate
613*0Sstevel@tonic-gate        my $nfound = mselect(undef,
614*0Sstevel@tonic-gate			    ($wout = $wbits),
615*0Sstevel@tonic-gate			    ($^O eq 'MSWin32' ? ($wexc = $wbits) : undef),
616*0Sstevel@tonic-gate			    $timeout);
617*0Sstevel@tonic-gate        warn("select: $!") unless defined $nfound;
618*0Sstevel@tonic-gate
619*0Sstevel@tonic-gate        if ($nfound && vec($wout, $self->{"fh"}->fileno, 1)) {
620*0Sstevel@tonic-gate          # the socket is ready for writing so the connection
621*0Sstevel@tonic-gate          # attempt completed. test whether the connection
622*0Sstevel@tonic-gate          # attempt was successful or not
623*0Sstevel@tonic-gate
624*0Sstevel@tonic-gate          if (getpeername($self->{"fh"})) {
625*0Sstevel@tonic-gate            # Connection established to remote host
626*0Sstevel@tonic-gate            $ret = 1;
627*0Sstevel@tonic-gate          } else {
628*0Sstevel@tonic-gate            # TCP ACK will never come from this host
629*0Sstevel@tonic-gate            # because there was an error connecting.
630*0Sstevel@tonic-gate
631*0Sstevel@tonic-gate            # This should set $! to the correct error.
632*0Sstevel@tonic-gate            my $char;
633*0Sstevel@tonic-gate            sysread($self->{"fh"},$char,1);
634*0Sstevel@tonic-gate            $! = ECONNREFUSED if ($! == EAGAIN && $^O =~ /cygwin/i);
635*0Sstevel@tonic-gate
636*0Sstevel@tonic-gate            $ret = 1 if (!$self->{"econnrefused"}
637*0Sstevel@tonic-gate                         && $! == ECONNREFUSED);
638*0Sstevel@tonic-gate          }
639*0Sstevel@tonic-gate        } else {
640*0Sstevel@tonic-gate          # the connection attempt timed out (or there were connect
641*0Sstevel@tonic-gate	  # errors on Windows)
642*0Sstevel@tonic-gate	  if ($^O =~ 'MSWin32') {
643*0Sstevel@tonic-gate	      # If the connect will fail on a non-blocking socket,
644*0Sstevel@tonic-gate	      # winsock reports ECONNREFUSED as an exception, and we
645*0Sstevel@tonic-gate	      # need to fetch the socket-level error code via getsockopt()
646*0Sstevel@tonic-gate	      # instead of using the thread-level error code that is in $!.
647*0Sstevel@tonic-gate	      if ($nfound && vec($wexc, $self->{"fh"}->fileno, 1)) {
648*0Sstevel@tonic-gate		  $! = unpack("i", getsockopt($self->{"fh"}, SOL_SOCKET,
649*0Sstevel@tonic-gate			                      SO_ERROR));
650*0Sstevel@tonic-gate	      }
651*0Sstevel@tonic-gate	  }
652*0Sstevel@tonic-gate        }
653*0Sstevel@tonic-gate      }
654*0Sstevel@tonic-gate    } else {
655*0Sstevel@tonic-gate      # Connection established to remote host
656*0Sstevel@tonic-gate      $ret = 1;
657*0Sstevel@tonic-gate    }
658*0Sstevel@tonic-gate
659*0Sstevel@tonic-gate    # Unset O_NONBLOCK property on filehandle
660*0Sstevel@tonic-gate    $self->socket_blocking_mode($self->{"fh"}, 1);
661*0Sstevel@tonic-gate    $self->{"ip"} = $ip;
662*0Sstevel@tonic-gate    return $ret;
663*0Sstevel@tonic-gate  };
664*0Sstevel@tonic-gate
665*0Sstevel@tonic-gate  if ($syn_forking) {
666*0Sstevel@tonic-gate    # Buggy Winsock API doesn't allow nonblocking connect.
667*0Sstevel@tonic-gate    # Hence, if our OS is Windows, we need to create a separate
668*0Sstevel@tonic-gate    # process to do the blocking connect attempt.
669*0Sstevel@tonic-gate    # XXX Above comments are not true at least for Win2K, where
670*0Sstevel@tonic-gate    # nonblocking connect works.
671*0Sstevel@tonic-gate
672*0Sstevel@tonic-gate    $| = 1; # Clear buffer prior to fork to prevent duplicate flushing.
673*0Sstevel@tonic-gate    $self->{'tcp_chld'} = fork;
674*0Sstevel@tonic-gate    if (!$self->{'tcp_chld'}) {
675*0Sstevel@tonic-gate      if (!defined $self->{'tcp_chld'}) {
676*0Sstevel@tonic-gate        # Fork did not work
677*0Sstevel@tonic-gate        warn "Fork error: $!";
678*0Sstevel@tonic-gate        return 0;
679*0Sstevel@tonic-gate      }
680*0Sstevel@tonic-gate      &{ $do_socket }();
681*0Sstevel@tonic-gate
682*0Sstevel@tonic-gate      # Try a slow blocking connect() call
683*0Sstevel@tonic-gate      # and report the status to the parent.
684*0Sstevel@tonic-gate      if ( &{ $do_connect }() ) {
685*0Sstevel@tonic-gate        $self->{"fh"}->close();
686*0Sstevel@tonic-gate        # No error
687*0Sstevel@tonic-gate        exit 0;
688*0Sstevel@tonic-gate      } else {
689*0Sstevel@tonic-gate        # Pass the error status to the parent
690*0Sstevel@tonic-gate        # Make sure that $! <= 255
691*0Sstevel@tonic-gate        exit($! <= 255 ? $! : 255);
692*0Sstevel@tonic-gate      }
693*0Sstevel@tonic-gate    }
694*0Sstevel@tonic-gate
695*0Sstevel@tonic-gate    &{ $do_socket }();
696*0Sstevel@tonic-gate
697*0Sstevel@tonic-gate    my $patience = &time() + $timeout;
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gate    my ($child, $child_errno);
700*0Sstevel@tonic-gate    $? = 0; $child_errno = 0;
701*0Sstevel@tonic-gate    # Wait up to the timeout
702*0Sstevel@tonic-gate    # And clean off the zombie
703*0Sstevel@tonic-gate    do {
704*0Sstevel@tonic-gate      $child = waitpid($self->{'tcp_chld'}, &WNOHANG());
705*0Sstevel@tonic-gate      $child_errno = $? >> 8;
706*0Sstevel@tonic-gate      select(undef, undef, undef, 0.1);
707*0Sstevel@tonic-gate    } while &time() < $patience && $child != $self->{'tcp_chld'};
708*0Sstevel@tonic-gate
709*0Sstevel@tonic-gate    if ($child == $self->{'tcp_chld'}) {
710*0Sstevel@tonic-gate      if ($self->{"proto"} eq "stream") {
711*0Sstevel@tonic-gate        # We need the socket connected here, in parent
712*0Sstevel@tonic-gate        # Should be safe to connect because the child finished
713*0Sstevel@tonic-gate        # within the timeout
714*0Sstevel@tonic-gate        &{ $do_connect }();
715*0Sstevel@tonic-gate      }
716*0Sstevel@tonic-gate      # $ret cannot be set by the child process
717*0Sstevel@tonic-gate      $ret = !$child_errno;
718*0Sstevel@tonic-gate    } else {
719*0Sstevel@tonic-gate      # Time must have run out.
720*0Sstevel@tonic-gate      # Put that choking client out of its misery
721*0Sstevel@tonic-gate      kill "KILL", $self->{'tcp_chld'};
722*0Sstevel@tonic-gate      # Clean off the zombie
723*0Sstevel@tonic-gate      waitpid($self->{'tcp_chld'}, 0);
724*0Sstevel@tonic-gate      $ret = 0;
725*0Sstevel@tonic-gate    }
726*0Sstevel@tonic-gate    delete $self->{'tcp_chld'};
727*0Sstevel@tonic-gate    $! = $child_errno;
728*0Sstevel@tonic-gate  } else {
729*0Sstevel@tonic-gate    # Otherwise don't waste the resources to fork
730*0Sstevel@tonic-gate
731*0Sstevel@tonic-gate    &{ $do_socket }();
732*0Sstevel@tonic-gate
733*0Sstevel@tonic-gate    &{ $do_connect_nb }();
734*0Sstevel@tonic-gate  }
735*0Sstevel@tonic-gate
736*0Sstevel@tonic-gate  return $ret;
737*0Sstevel@tonic-gate}
738*0Sstevel@tonic-gate
739*0Sstevel@tonic-gatesub DESTROY {
740*0Sstevel@tonic-gate  my $self = shift;
741*0Sstevel@tonic-gate  if ($self->{'proto'} eq 'tcp' &&
742*0Sstevel@tonic-gate      $self->{'tcp_chld'}) {
743*0Sstevel@tonic-gate    # Put that choking client out of its misery
744*0Sstevel@tonic-gate    kill "KILL", $self->{'tcp_chld'};
745*0Sstevel@tonic-gate    # Clean off the zombie
746*0Sstevel@tonic-gate    waitpid($self->{'tcp_chld'}, 0);
747*0Sstevel@tonic-gate  }
748*0Sstevel@tonic-gate}
749*0Sstevel@tonic-gate
750*0Sstevel@tonic-gate# This writes the given string to the socket and then reads it
751*0Sstevel@tonic-gate# back.  It returns 1 on success, 0 on failure.
752*0Sstevel@tonic-gatesub tcp_echo
753*0Sstevel@tonic-gate{
754*0Sstevel@tonic-gate  my $self = shift;
755*0Sstevel@tonic-gate  my $timeout = shift;
756*0Sstevel@tonic-gate  my $pingstring = shift;
757*0Sstevel@tonic-gate
758*0Sstevel@tonic-gate  my $ret = undef;
759*0Sstevel@tonic-gate  my $time = &time();
760*0Sstevel@tonic-gate  my $wrstr = $pingstring;
761*0Sstevel@tonic-gate  my $rdstr = "";
762*0Sstevel@tonic-gate
763*0Sstevel@tonic-gate  eval <<'EOM';
764*0Sstevel@tonic-gate    do {
765*0Sstevel@tonic-gate      my $rin = "";
766*0Sstevel@tonic-gate      vec($rin, $self->{"fh"}->fileno(), 1) = 1;
767*0Sstevel@tonic-gate
768*0Sstevel@tonic-gate      my $rout = undef;
769*0Sstevel@tonic-gate      if($wrstr) {
770*0Sstevel@tonic-gate        $rout = "";
771*0Sstevel@tonic-gate        vec($rout, $self->{"fh"}->fileno(), 1) = 1;
772*0Sstevel@tonic-gate      }
773*0Sstevel@tonic-gate
774*0Sstevel@tonic-gate      if(mselect($rin, $rout, undef, ($time + $timeout) - &time())) {
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate        if($rout && vec($rout,$self->{"fh"}->fileno(),1)) {
777*0Sstevel@tonic-gate          my $num = syswrite($self->{"fh"}, $wrstr, length $wrstr);
778*0Sstevel@tonic-gate          if($num) {
779*0Sstevel@tonic-gate            # If it was a partial write, update and try again.
780*0Sstevel@tonic-gate            $wrstr = substr($wrstr,$num);
781*0Sstevel@tonic-gate          } else {
782*0Sstevel@tonic-gate            # There was an error.
783*0Sstevel@tonic-gate            $ret = 0;
784*0Sstevel@tonic-gate          }
785*0Sstevel@tonic-gate        }
786*0Sstevel@tonic-gate
787*0Sstevel@tonic-gate        if(vec($rin,$self->{"fh"}->fileno(),1)) {
788*0Sstevel@tonic-gate          my $reply;
789*0Sstevel@tonic-gate          if(sysread($self->{"fh"},$reply,length($pingstring)-length($rdstr))) {
790*0Sstevel@tonic-gate            $rdstr .= $reply;
791*0Sstevel@tonic-gate            $ret = 1 if $rdstr eq $pingstring;
792*0Sstevel@tonic-gate          } else {
793*0Sstevel@tonic-gate            # There was an error.
794*0Sstevel@tonic-gate            $ret = 0;
795*0Sstevel@tonic-gate          }
796*0Sstevel@tonic-gate        }
797*0Sstevel@tonic-gate
798*0Sstevel@tonic-gate      }
799*0Sstevel@tonic-gate    } until &time() > ($time + $timeout) || defined($ret);
800*0Sstevel@tonic-gateEOM
801*0Sstevel@tonic-gate
802*0Sstevel@tonic-gate  return $ret;
803*0Sstevel@tonic-gate}
804*0Sstevel@tonic-gate
805*0Sstevel@tonic-gate
806*0Sstevel@tonic-gate
807*0Sstevel@tonic-gate
808*0Sstevel@tonic-gate# Description: Perform a stream ping.  If the tcp connection isn't
809*0Sstevel@tonic-gate# already open, it opens it.  It then sends some data and waits for
810*0Sstevel@tonic-gate# a reply.  It leaves the stream open on exit.
811*0Sstevel@tonic-gate
812*0Sstevel@tonic-gatesub ping_stream
813*0Sstevel@tonic-gate{
814*0Sstevel@tonic-gate  my ($self,
815*0Sstevel@tonic-gate      $ip,                # Packed IP number of the host
816*0Sstevel@tonic-gate      $timeout            # Seconds after which ping times out
817*0Sstevel@tonic-gate      ) = @_;
818*0Sstevel@tonic-gate
819*0Sstevel@tonic-gate  # Open the stream if it's not already open
820*0Sstevel@tonic-gate  if(!defined $self->{"fh"}->fileno()) {
821*0Sstevel@tonic-gate    $self->tcp_connect($ip, $timeout) or return 0;
822*0Sstevel@tonic-gate  }
823*0Sstevel@tonic-gate
824*0Sstevel@tonic-gate  croak "tried to switch servers while stream pinging"
825*0Sstevel@tonic-gate    if $self->{"ip"} ne $ip;
826*0Sstevel@tonic-gate
827*0Sstevel@tonic-gate  return $self->tcp_echo($timeout, $pingstring);
828*0Sstevel@tonic-gate}
829*0Sstevel@tonic-gate
830*0Sstevel@tonic-gate# Description: opens the stream.  You would do this if you want to
831*0Sstevel@tonic-gate# separate the overhead of opening the stream from the first ping.
832*0Sstevel@tonic-gate
833*0Sstevel@tonic-gatesub open
834*0Sstevel@tonic-gate{
835*0Sstevel@tonic-gate  my ($self,
836*0Sstevel@tonic-gate      $host,              # Host or IP address
837*0Sstevel@tonic-gate      $timeout            # Seconds after which open times out
838*0Sstevel@tonic-gate      ) = @_;
839*0Sstevel@tonic-gate
840*0Sstevel@tonic-gate  my ($ip);               # Packed IP number of the host
841*0Sstevel@tonic-gate  $ip = inet_aton($host);
842*0Sstevel@tonic-gate  $timeout = $self->{"timeout"} unless $timeout;
843*0Sstevel@tonic-gate
844*0Sstevel@tonic-gate  if($self->{"proto"} eq "stream") {
845*0Sstevel@tonic-gate    if(defined($self->{"fh"}->fileno())) {
846*0Sstevel@tonic-gate      croak("socket is already open");
847*0Sstevel@tonic-gate    } else {
848*0Sstevel@tonic-gate      $self->tcp_connect($ip, $timeout);
849*0Sstevel@tonic-gate    }
850*0Sstevel@tonic-gate  }
851*0Sstevel@tonic-gate}
852*0Sstevel@tonic-gate
853*0Sstevel@tonic-gate
854*0Sstevel@tonic-gate# Description:  Perform a udp echo ping.  Construct a message of
855*0Sstevel@tonic-gate# at least the one-byte sequence number and any additional data bytes.
856*0Sstevel@tonic-gate# Send the message out and wait for a message to come back.  If we
857*0Sstevel@tonic-gate# get a message, make sure all of its parts match.  If they do, we are
858*0Sstevel@tonic-gate# done.  Otherwise go back and wait for the message until we run out
859*0Sstevel@tonic-gate# of time.  Return the result of our efforts.
860*0Sstevel@tonic-gate
861*0Sstevel@tonic-gateuse constant UDP_FLAGS => 0; # Nothing special on send or recv
862*0Sstevel@tonic-gatesub ping_udp
863*0Sstevel@tonic-gate{
864*0Sstevel@tonic-gate  my ($self,
865*0Sstevel@tonic-gate      $ip,                # Packed IP number of the host
866*0Sstevel@tonic-gate      $timeout            # Seconds after which ping times out
867*0Sstevel@tonic-gate      ) = @_;
868*0Sstevel@tonic-gate
869*0Sstevel@tonic-gate  my ($saddr,             # sockaddr_in with port and ip
870*0Sstevel@tonic-gate      $ret,               # The return value
871*0Sstevel@tonic-gate      $msg,               # Message to be echoed
872*0Sstevel@tonic-gate      $finish_time,       # Time ping should be finished
873*0Sstevel@tonic-gate      $flush,             # Whether socket needs to be disconnected
874*0Sstevel@tonic-gate      $connect,           # Whether socket needs to be connected
875*0Sstevel@tonic-gate      $done,              # Set to 1 when we are done pinging
876*0Sstevel@tonic-gate      $rbits,             # Read bits, filehandles for reading
877*0Sstevel@tonic-gate      $nfound,            # Number of ready filehandles found
878*0Sstevel@tonic-gate      $from_saddr,        # sockaddr_in of sender
879*0Sstevel@tonic-gate      $from_msg,          # Characters echoed by $host
880*0Sstevel@tonic-gate      $from_port,         # Port message was echoed from
881*0Sstevel@tonic-gate      $from_ip            # Packed IP number of sender
882*0Sstevel@tonic-gate      );
883*0Sstevel@tonic-gate
884*0Sstevel@tonic-gate  $saddr = sockaddr_in($self->{"port_num"}, $ip);
885*0Sstevel@tonic-gate  $self->{"seq"} = ($self->{"seq"} + 1) % 256;    # Increment sequence
886*0Sstevel@tonic-gate  $msg = chr($self->{"seq"}) . $self->{"data"};   # Add data if any
887*0Sstevel@tonic-gate
888*0Sstevel@tonic-gate  if ($self->{"connected"}) {
889*0Sstevel@tonic-gate    if ($self->{"connected"} ne $saddr) {
890*0Sstevel@tonic-gate      # Still connected to wrong destination.
891*0Sstevel@tonic-gate      # Need to flush out the old one.
892*0Sstevel@tonic-gate      $flush = 1;
893*0Sstevel@tonic-gate    }
894*0Sstevel@tonic-gate  } else {
895*0Sstevel@tonic-gate    # Not connected yet.
896*0Sstevel@tonic-gate    # Need to connect() before send()
897*0Sstevel@tonic-gate    $connect = 1;
898*0Sstevel@tonic-gate  }
899*0Sstevel@tonic-gate
900*0Sstevel@tonic-gate  # Have to connect() and send() instead of sendto()
901*0Sstevel@tonic-gate  # in order to pick up on the ECONNREFUSED setting
902*0Sstevel@tonic-gate  # from recv() or double send() errno as utilized in
903*0Sstevel@tonic-gate  # the concept by rdw @ perlmonks.  See:
904*0Sstevel@tonic-gate  # http://perlmonks.thepen.com/42898.html
905*0Sstevel@tonic-gate  if ($flush) {
906*0Sstevel@tonic-gate    # Need to socket() again to flush the descriptor
907*0Sstevel@tonic-gate    # This will disconnect from the old saddr.
908*0Sstevel@tonic-gate    socket($self->{"fh"}, PF_INET, SOCK_DGRAM,
909*0Sstevel@tonic-gate           $self->{"proto_num"});
910*0Sstevel@tonic-gate  }
911*0Sstevel@tonic-gate  # Connect the socket if it isn't already connected
912*0Sstevel@tonic-gate  # to the right destination.
913*0Sstevel@tonic-gate  if ($flush || $connect) {
914*0Sstevel@tonic-gate    connect($self->{"fh"}, $saddr);               # Tie destination to socket
915*0Sstevel@tonic-gate    $self->{"connected"} = $saddr;
916*0Sstevel@tonic-gate  }
917*0Sstevel@tonic-gate  send($self->{"fh"}, $msg, UDP_FLAGS);           # Send it
918*0Sstevel@tonic-gate
919*0Sstevel@tonic-gate  $rbits = "";
920*0Sstevel@tonic-gate  vec($rbits, $self->{"fh"}->fileno(), 1) = 1;
921*0Sstevel@tonic-gate  $ret = 0;                   # Default to unreachable
922*0Sstevel@tonic-gate  $done = 0;
923*0Sstevel@tonic-gate  my $retrans = 0.01;
924*0Sstevel@tonic-gate  my $factor = $self->{"retrans"};
925*0Sstevel@tonic-gate  $finish_time = &time() + $timeout;       # Ping needs to be done by then
926*0Sstevel@tonic-gate  while (!$done && $timeout > 0)
927*0Sstevel@tonic-gate  {
928*0Sstevel@tonic-gate    if ($factor > 1)
929*0Sstevel@tonic-gate    {
930*0Sstevel@tonic-gate      $timeout = $retrans if $timeout > $retrans;
931*0Sstevel@tonic-gate      $retrans*= $factor; # Exponential backoff
932*0Sstevel@tonic-gate    }
933*0Sstevel@tonic-gate    $nfound  = mselect((my $rout=$rbits), undef, undef, $timeout); # Wait for response
934*0Sstevel@tonic-gate    my $why = $!;
935*0Sstevel@tonic-gate    $timeout = $finish_time - &time();   # Get remaining time
936*0Sstevel@tonic-gate
937*0Sstevel@tonic-gate    if (!defined($nfound))  # Hmm, a strange error
938*0Sstevel@tonic-gate    {
939*0Sstevel@tonic-gate      $ret = undef;
940*0Sstevel@tonic-gate      $done = 1;
941*0Sstevel@tonic-gate    }
942*0Sstevel@tonic-gate    elsif ($nfound)         # A packet is waiting
943*0Sstevel@tonic-gate    {
944*0Sstevel@tonic-gate      $from_msg = "";
945*0Sstevel@tonic-gate      $from_saddr = recv($self->{"fh"}, $from_msg, 1500, UDP_FLAGS);
946*0Sstevel@tonic-gate      if (!$from_saddr) {
947*0Sstevel@tonic-gate        # For example an unreachable host will make recv() fail.
948*0Sstevel@tonic-gate        if (!$self->{"econnrefused"} &&
949*0Sstevel@tonic-gate            ($! == ECONNREFUSED ||
950*0Sstevel@tonic-gate             $! == ECONNRESET)) {
951*0Sstevel@tonic-gate          # "Connection refused" means reachable
952*0Sstevel@tonic-gate          # Good, continue
953*0Sstevel@tonic-gate          $ret = 1;
954*0Sstevel@tonic-gate        }
955*0Sstevel@tonic-gate        $done = 1;
956*0Sstevel@tonic-gate      } else {
957*0Sstevel@tonic-gate        ($from_port, $from_ip) = sockaddr_in($from_saddr);
958*0Sstevel@tonic-gate        if (!$source_verify ||
959*0Sstevel@tonic-gate            (($from_ip eq $ip) &&        # Does the packet check out?
960*0Sstevel@tonic-gate             ($from_port == $self->{"port_num"}) &&
961*0Sstevel@tonic-gate             ($from_msg eq $msg)))
962*0Sstevel@tonic-gate        {
963*0Sstevel@tonic-gate          $ret = 1;       # It's a winner
964*0Sstevel@tonic-gate          $done = 1;
965*0Sstevel@tonic-gate        }
966*0Sstevel@tonic-gate      }
967*0Sstevel@tonic-gate    }
968*0Sstevel@tonic-gate    elsif ($timeout <= 0)              # Oops, timed out
969*0Sstevel@tonic-gate    {
970*0Sstevel@tonic-gate      $done = 1;
971*0Sstevel@tonic-gate    }
972*0Sstevel@tonic-gate    else
973*0Sstevel@tonic-gate    {
974*0Sstevel@tonic-gate      # Send another in case the last one dropped
975*0Sstevel@tonic-gate      if (send($self->{"fh"}, $msg, UDP_FLAGS)) {
976*0Sstevel@tonic-gate        # Another send worked?  The previous udp packet
977*0Sstevel@tonic-gate        # must have gotten lost or is still in transit.
978*0Sstevel@tonic-gate        # Hopefully this new packet will arrive safely.
979*0Sstevel@tonic-gate      } else {
980*0Sstevel@tonic-gate        if (!$self->{"econnrefused"} &&
981*0Sstevel@tonic-gate            $! == ECONNREFUSED) {
982*0Sstevel@tonic-gate          # "Connection refused" means reachable
983*0Sstevel@tonic-gate          # Good, continue
984*0Sstevel@tonic-gate          $ret = 1;
985*0Sstevel@tonic-gate        }
986*0Sstevel@tonic-gate        $done = 1;
987*0Sstevel@tonic-gate      }
988*0Sstevel@tonic-gate    }
989*0Sstevel@tonic-gate  }
990*0Sstevel@tonic-gate  return $ret;
991*0Sstevel@tonic-gate}
992*0Sstevel@tonic-gate
993*0Sstevel@tonic-gate# Description: Send a TCP SYN packet to host specified.
994*0Sstevel@tonic-gatesub ping_syn
995*0Sstevel@tonic-gate{
996*0Sstevel@tonic-gate  my $self = shift;
997*0Sstevel@tonic-gate  my $host = shift;
998*0Sstevel@tonic-gate  my $ip = shift;
999*0Sstevel@tonic-gate  my $start_time = shift;
1000*0Sstevel@tonic-gate  my $stop_time = shift;
1001*0Sstevel@tonic-gate
1002*0Sstevel@tonic-gate  if ($syn_forking) {
1003*0Sstevel@tonic-gate    return $self->ping_syn_fork($host, $ip, $start_time, $stop_time);
1004*0Sstevel@tonic-gate  }
1005*0Sstevel@tonic-gate
1006*0Sstevel@tonic-gate  my $fh = FileHandle->new();
1007*0Sstevel@tonic-gate  my $saddr = sockaddr_in($self->{"port_num"}, $ip);
1008*0Sstevel@tonic-gate
1009*0Sstevel@tonic-gate  # Create TCP socket
1010*0Sstevel@tonic-gate  if (!socket ($fh, PF_INET, SOCK_STREAM, $self->{"proto_num"})) {
1011*0Sstevel@tonic-gate    croak("tcp socket error - $!");
1012*0Sstevel@tonic-gate  }
1013*0Sstevel@tonic-gate
1014*0Sstevel@tonic-gate  if (defined $self->{"local_addr"} &&
1015*0Sstevel@tonic-gate      !CORE::bind($fh, sockaddr_in(0, $self->{"local_addr"}))) {
1016*0Sstevel@tonic-gate    croak("tcp bind error - $!");
1017*0Sstevel@tonic-gate  }
1018*0Sstevel@tonic-gate
1019*0Sstevel@tonic-gate  if ($self->{'device'}) {
1020*0Sstevel@tonic-gate    setsockopt($fh, SOL_SOCKET, SO_BINDTODEVICE(), pack("Z*", $self->{'device'}))
1021*0Sstevel@tonic-gate      or croak("error binding to device $self->{'device'} $!");
1022*0Sstevel@tonic-gate  }
1023*0Sstevel@tonic-gate  if ($self->{'tos'}) {
1024*0Sstevel@tonic-gate    setsockopt($fh, SOL_IP, IP_TOS(), pack("I*", $self->{'tos'}))
1025*0Sstevel@tonic-gate      or croak "error configuring tos to $self->{'tos'} $!";
1026*0Sstevel@tonic-gate  }
1027*0Sstevel@tonic-gate  # Set O_NONBLOCK property on filehandle
1028*0Sstevel@tonic-gate  $self->socket_blocking_mode($fh, 0);
1029*0Sstevel@tonic-gate
1030*0Sstevel@tonic-gate  # Attempt the non-blocking connect
1031*0Sstevel@tonic-gate  # by just sending the TCP SYN packet
1032*0Sstevel@tonic-gate  if (connect($fh, $saddr)) {
1033*0Sstevel@tonic-gate    # Non-blocking, yet still connected?
1034*0Sstevel@tonic-gate    # Must have connected very quickly,
1035*0Sstevel@tonic-gate    # or else it wasn't very non-blocking.
1036*0Sstevel@tonic-gate    #warn "WARNING: Nonblocking connect connected anyway? ($^O)";
1037*0Sstevel@tonic-gate  } else {
1038*0Sstevel@tonic-gate    # Error occurred connecting.
1039*0Sstevel@tonic-gate    if ($! == EINPROGRESS || ($^O eq 'MSWin32' && $! == EWOULDBLOCK)) {
1040*0Sstevel@tonic-gate      # The connection is just still in progress.
1041*0Sstevel@tonic-gate      # This is the expected condition.
1042*0Sstevel@tonic-gate    } else {
1043*0Sstevel@tonic-gate      # Just save the error and continue on.
1044*0Sstevel@tonic-gate      # The ack() can check the status later.
1045*0Sstevel@tonic-gate      $self->{"bad"}->{$host} = $!;
1046*0Sstevel@tonic-gate    }
1047*0Sstevel@tonic-gate  }
1048*0Sstevel@tonic-gate
1049*0Sstevel@tonic-gate  my $entry = [ $host, $ip, $fh, $start_time, $stop_time ];
1050*0Sstevel@tonic-gate  $self->{"syn"}->{$fh->fileno} = $entry;
1051*0Sstevel@tonic-gate  if ($self->{"stop_time"} < $stop_time) {
1052*0Sstevel@tonic-gate    $self->{"stop_time"} = $stop_time;
1053*0Sstevel@tonic-gate  }
1054*0Sstevel@tonic-gate  vec($self->{"wbits"}, $fh->fileno, 1) = 1;
1055*0Sstevel@tonic-gate
1056*0Sstevel@tonic-gate  return 1;
1057*0Sstevel@tonic-gate}
1058*0Sstevel@tonic-gate
1059*0Sstevel@tonic-gatesub ping_syn_fork {
1060*0Sstevel@tonic-gate  my ($self, $host, $ip, $start_time, $stop_time) = @_;
1061*0Sstevel@tonic-gate
1062*0Sstevel@tonic-gate  # Buggy Winsock API doesn't allow nonblocking connect.
1063*0Sstevel@tonic-gate  # Hence, if our OS is Windows, we need to create a separate
1064*0Sstevel@tonic-gate  # process to do the blocking connect attempt.
1065*0Sstevel@tonic-gate  my $pid = fork();
1066*0Sstevel@tonic-gate  if (defined $pid) {
1067*0Sstevel@tonic-gate    if ($pid) {
1068*0Sstevel@tonic-gate      # Parent process
1069*0Sstevel@tonic-gate      my $entry = [ $host, $ip, $pid, $start_time, $stop_time ];
1070*0Sstevel@tonic-gate      $self->{"syn"}->{$pid} = $entry;
1071*0Sstevel@tonic-gate      if ($self->{"stop_time"} < $stop_time) {
1072*0Sstevel@tonic-gate        $self->{"stop_time"} = $stop_time;
1073*0Sstevel@tonic-gate      }
1074*0Sstevel@tonic-gate    } else {
1075*0Sstevel@tonic-gate      # Child process
1076*0Sstevel@tonic-gate      my $saddr = sockaddr_in($self->{"port_num"}, $ip);
1077*0Sstevel@tonic-gate
1078*0Sstevel@tonic-gate      # Create TCP socket
1079*0Sstevel@tonic-gate      if (!socket ($self->{"fh"}, PF_INET, SOCK_STREAM, $self->{"proto_num"})) {
1080*0Sstevel@tonic-gate        croak("tcp socket error - $!");
1081*0Sstevel@tonic-gate      }
1082*0Sstevel@tonic-gate
1083*0Sstevel@tonic-gate      if (defined $self->{"local_addr"} &&
1084*0Sstevel@tonic-gate          !CORE::bind($self->{"fh"}, sockaddr_in(0, $self->{"local_addr"}))) {
1085*0Sstevel@tonic-gate        croak("tcp bind error - $!");
1086*0Sstevel@tonic-gate      }
1087*0Sstevel@tonic-gate
1088*0Sstevel@tonic-gate      if ($self->{'device'}) {
1089*0Sstevel@tonic-gate        setsockopt($self->{"fh"}, SOL_SOCKET, SO_BINDTODEVICE(), pack("Z*", $self->{'device'}))
1090*0Sstevel@tonic-gate          or croak("error binding to device $self->{'device'} $!");
1091*0Sstevel@tonic-gate      }
1092*0Sstevel@tonic-gate      if ($self->{'tos'}) {
1093*0Sstevel@tonic-gate        setsockopt($self->{"fh"}, SOL_IP, IP_TOS(), pack("I*", $self->{'tos'}))
1094*0Sstevel@tonic-gate          or croak "error configuring tos to $self->{'tos'} $!";
1095*0Sstevel@tonic-gate      }
1096*0Sstevel@tonic-gate
1097*0Sstevel@tonic-gate      $!=0;
1098*0Sstevel@tonic-gate      # Try to connect (could take a long time)
1099*0Sstevel@tonic-gate      connect($self->{"fh"}, $saddr);
1100*0Sstevel@tonic-gate      # Notify parent of connect error status
1101*0Sstevel@tonic-gate      my $err = $!+0;
1102*0Sstevel@tonic-gate      my $wrstr = "$$ $err";
1103*0Sstevel@tonic-gate      # Force to 16 chars including \n
1104*0Sstevel@tonic-gate      $wrstr .= " "x(15 - length $wrstr). "\n";
1105*0Sstevel@tonic-gate      syswrite($self->{"fork_wr"}, $wrstr, length $wrstr);
1106*0Sstevel@tonic-gate      exit;
1107*0Sstevel@tonic-gate    }
1108*0Sstevel@tonic-gate  } else {
1109*0Sstevel@tonic-gate    # fork() failed?
1110*0Sstevel@tonic-gate    die "fork: $!";
1111*0Sstevel@tonic-gate  }
1112*0Sstevel@tonic-gate  return 1;
1113*0Sstevel@tonic-gate}
1114*0Sstevel@tonic-gate
1115*0Sstevel@tonic-gate# Description: Wait for TCP ACK from host specified
1116*0Sstevel@tonic-gate# from ping_syn above.  If no host is specified, wait
1117*0Sstevel@tonic-gate# for TCP ACK from any of the hosts in the SYN queue.
1118*0Sstevel@tonic-gatesub ack
1119*0Sstevel@tonic-gate{
1120*0Sstevel@tonic-gate  my $self = shift;
1121*0Sstevel@tonic-gate
1122*0Sstevel@tonic-gate  if ($self->{"proto"} eq "syn") {
1123*0Sstevel@tonic-gate    if ($syn_forking) {
1124*0Sstevel@tonic-gate      my @answer = $self->ack_unfork(shift);
1125*0Sstevel@tonic-gate      return wantarray ? @answer : $answer[0];
1126*0Sstevel@tonic-gate    }
1127*0Sstevel@tonic-gate    my $wbits = "";
1128*0Sstevel@tonic-gate    my $stop_time = 0;
1129*0Sstevel@tonic-gate    if (my $host = shift) {
1130*0Sstevel@tonic-gate      # Host passed as arg
1131*0Sstevel@tonic-gate      if (exists $self->{"bad"}->{$host}) {
1132*0Sstevel@tonic-gate        if (!$self->{"econnrefused"} &&
1133*0Sstevel@tonic-gate            $self->{"bad"}->{ $host } &&
1134*0Sstevel@tonic-gate            (($! = ECONNREFUSED)>0) &&
1135*0Sstevel@tonic-gate            $self->{"bad"}->{ $host } eq "$!") {
1136*0Sstevel@tonic-gate          # "Connection refused" means reachable
1137*0Sstevel@tonic-gate          # Good, continue
1138*0Sstevel@tonic-gate        } else {
1139*0Sstevel@tonic-gate          # ECONNREFUSED means no good
1140*0Sstevel@tonic-gate          return ();
1141*0Sstevel@tonic-gate        }
1142*0Sstevel@tonic-gate      }
1143*0Sstevel@tonic-gate      my $host_fd = undef;
1144*0Sstevel@tonic-gate      foreach my $fd (keys %{ $self->{"syn"} }) {
1145*0Sstevel@tonic-gate        my $entry = $self->{"syn"}->{$fd};
1146*0Sstevel@tonic-gate        if ($entry->[0] eq $host) {
1147*0Sstevel@tonic-gate          $host_fd = $fd;
1148*0Sstevel@tonic-gate          $stop_time = $entry->[4]
1149*0Sstevel@tonic-gate            || croak("Corrupted SYN entry for [$host]");
1150*0Sstevel@tonic-gate          last;
1151*0Sstevel@tonic-gate        }
1152*0Sstevel@tonic-gate      }
1153*0Sstevel@tonic-gate      croak("ack called on [$host] without calling ping first!")
1154*0Sstevel@tonic-gate        unless defined $host_fd;
1155*0Sstevel@tonic-gate      vec($wbits, $host_fd, 1) = 1;
1156*0Sstevel@tonic-gate    } else {
1157*0Sstevel@tonic-gate      # No $host passed so scan all hosts
1158*0Sstevel@tonic-gate      # Use the latest stop_time
1159*0Sstevel@tonic-gate      $stop_time = $self->{"stop_time"};
1160*0Sstevel@tonic-gate      # Use all the bits
1161*0Sstevel@tonic-gate      $wbits = $self->{"wbits"};
1162*0Sstevel@tonic-gate    }
1163*0Sstevel@tonic-gate
1164*0Sstevel@tonic-gate    while ($wbits !~ /^\0*\z/) {
1165*0Sstevel@tonic-gate      my $timeout = $stop_time - &time();
1166*0Sstevel@tonic-gate      # Force a minimum of 10 ms timeout.
1167*0Sstevel@tonic-gate      $timeout = 0.01 if $timeout <= 0.01;
1168*0Sstevel@tonic-gate
1169*0Sstevel@tonic-gate      my $winner_fd = undef;
1170*0Sstevel@tonic-gate      my $wout = $wbits;
1171*0Sstevel@tonic-gate      my $fd = 0;
1172*0Sstevel@tonic-gate      # Do "bad" fds from $wbits first
1173*0Sstevel@tonic-gate      while ($wout !~ /^\0*\z/) {
1174*0Sstevel@tonic-gate        if (vec($wout, $fd, 1)) {
1175*0Sstevel@tonic-gate          # Wipe it from future scanning.
1176*0Sstevel@tonic-gate          vec($wout, $fd, 1) = 0;
1177*0Sstevel@tonic-gate          if (my $entry = $self->{"syn"}->{$fd}) {
1178*0Sstevel@tonic-gate            if ($self->{"bad"}->{ $entry->[0] }) {
1179*0Sstevel@tonic-gate              $winner_fd = $fd;
1180*0Sstevel@tonic-gate              last;
1181*0Sstevel@tonic-gate            }
1182*0Sstevel@tonic-gate          }
1183*0Sstevel@tonic-gate        }
1184*0Sstevel@tonic-gate        $fd++;
1185*0Sstevel@tonic-gate      }
1186*0Sstevel@tonic-gate
1187*0Sstevel@tonic-gate      if (defined($winner_fd) or my $nfound = mselect(undef, ($wout=$wbits), undef, $timeout)) {
1188*0Sstevel@tonic-gate        if (defined $winner_fd) {
1189*0Sstevel@tonic-gate          $fd = $winner_fd;
1190*0Sstevel@tonic-gate        } else {
1191*0Sstevel@tonic-gate          # Done waiting for one of the ACKs
1192*0Sstevel@tonic-gate          $fd = 0;
1193*0Sstevel@tonic-gate          # Determine which one
1194*0Sstevel@tonic-gate          while ($wout !~ /^\0*\z/ &&
1195*0Sstevel@tonic-gate                 !vec($wout, $fd, 1)) {
1196*0Sstevel@tonic-gate            $fd++;
1197*0Sstevel@tonic-gate          }
1198*0Sstevel@tonic-gate        }
1199*0Sstevel@tonic-gate        if (my $entry = $self->{"syn"}->{$fd}) {
1200*0Sstevel@tonic-gate          # Wipe it from future scanning.
1201*0Sstevel@tonic-gate          delete $self->{"syn"}->{$fd};
1202*0Sstevel@tonic-gate          vec($self->{"wbits"}, $fd, 1) = 0;
1203*0Sstevel@tonic-gate          vec($wbits, $fd, 1) = 0;
1204*0Sstevel@tonic-gate          if (!$self->{"econnrefused"} &&
1205*0Sstevel@tonic-gate              $self->{"bad"}->{ $entry->[0] } &&
1206*0Sstevel@tonic-gate              (($! = ECONNREFUSED)>0) &&
1207*0Sstevel@tonic-gate              $self->{"bad"}->{ $entry->[0] } eq "$!") {
1208*0Sstevel@tonic-gate            # "Connection refused" means reachable
1209*0Sstevel@tonic-gate            # Good, continue
1210*0Sstevel@tonic-gate          } elsif (getpeername($entry->[2])) {
1211*0Sstevel@tonic-gate            # Connection established to remote host
1212*0Sstevel@tonic-gate            # Good, continue
1213*0Sstevel@tonic-gate          } else {
1214*0Sstevel@tonic-gate            # TCP ACK will never come from this host
1215*0Sstevel@tonic-gate            # because there was an error connecting.
1216*0Sstevel@tonic-gate
1217*0Sstevel@tonic-gate            # This should set $! to the correct error.
1218*0Sstevel@tonic-gate            my $char;
1219*0Sstevel@tonic-gate            sysread($entry->[2],$char,1);
1220*0Sstevel@tonic-gate            # Store the excuse why the connection failed.
1221*0Sstevel@tonic-gate            $self->{"bad"}->{$entry->[0]} = $!;
1222*0Sstevel@tonic-gate            if (!$self->{"econnrefused"} &&
1223*0Sstevel@tonic-gate                (($! == ECONNREFUSED) ||
1224*0Sstevel@tonic-gate                 ($! == EAGAIN && $^O =~ /cygwin/i))) {
1225*0Sstevel@tonic-gate              # "Connection refused" means reachable
1226*0Sstevel@tonic-gate              # Good, continue
1227*0Sstevel@tonic-gate            } else {
1228*0Sstevel@tonic-gate              # No good, try the next socket...
1229*0Sstevel@tonic-gate              next;
1230*0Sstevel@tonic-gate            }
1231*0Sstevel@tonic-gate          }
1232*0Sstevel@tonic-gate          # Everything passed okay, return the answer
1233*0Sstevel@tonic-gate          return wantarray ?
1234*0Sstevel@tonic-gate            ($entry->[0], &time() - $entry->[3], inet_ntoa($entry->[1]))
1235*0Sstevel@tonic-gate            : $entry->[0];
1236*0Sstevel@tonic-gate        } else {
1237*0Sstevel@tonic-gate          warn "Corrupted SYN entry: unknown fd [$fd] ready!";
1238*0Sstevel@tonic-gate          vec($wbits, $fd, 1) = 0;
1239*0Sstevel@tonic-gate          vec($self->{"wbits"}, $fd, 1) = 0;
1240*0Sstevel@tonic-gate        }
1241*0Sstevel@tonic-gate      } elsif (defined $nfound) {
1242*0Sstevel@tonic-gate        # Timed out waiting for ACK
1243*0Sstevel@tonic-gate        foreach my $fd (keys %{ $self->{"syn"} }) {
1244*0Sstevel@tonic-gate          if (vec($wbits, $fd, 1)) {
1245*0Sstevel@tonic-gate            my $entry = $self->{"syn"}->{$fd};
1246*0Sstevel@tonic-gate            $self->{"bad"}->{$entry->[0]} = "Timed out";
1247*0Sstevel@tonic-gate            vec($wbits, $fd, 1) = 0;
1248*0Sstevel@tonic-gate            vec($self->{"wbits"}, $fd, 1) = 0;
1249*0Sstevel@tonic-gate            delete $self->{"syn"}->{$fd};
1250*0Sstevel@tonic-gate          }
1251*0Sstevel@tonic-gate        }
1252*0Sstevel@tonic-gate      } else {
1253*0Sstevel@tonic-gate        # Weird error occurred with select()
1254*0Sstevel@tonic-gate        warn("select: $!");
1255*0Sstevel@tonic-gate        $self->{"syn"} = {};
1256*0Sstevel@tonic-gate        $wbits = "";
1257*0Sstevel@tonic-gate      }
1258*0Sstevel@tonic-gate    }
1259*0Sstevel@tonic-gate  }
1260*0Sstevel@tonic-gate  return ();
1261*0Sstevel@tonic-gate}
1262*0Sstevel@tonic-gate
1263*0Sstevel@tonic-gatesub ack_unfork {
1264*0Sstevel@tonic-gate  my ($self,$host) = @_;
1265*0Sstevel@tonic-gate  my $stop_time = $self->{"stop_time"};
1266*0Sstevel@tonic-gate  if ($host) {
1267*0Sstevel@tonic-gate    # Host passed as arg
1268*0Sstevel@tonic-gate    if (my $entry = $self->{"good"}->{$host}) {
1269*0Sstevel@tonic-gate      delete $self->{"good"}->{$host};
1270*0Sstevel@tonic-gate      return ($entry->[0], &time() - $entry->[3], inet_ntoa($entry->[1]));
1271*0Sstevel@tonic-gate    }
1272*0Sstevel@tonic-gate  }
1273*0Sstevel@tonic-gate
1274*0Sstevel@tonic-gate  my $rbits = "";
1275*0Sstevel@tonic-gate  my $timeout;
1276*0Sstevel@tonic-gate
1277*0Sstevel@tonic-gate  if (keys %{ $self->{"syn"} }) {
1278*0Sstevel@tonic-gate    # Scan all hosts that are left
1279*0Sstevel@tonic-gate    vec($rbits, fileno($self->{"fork_rd"}), 1) = 1;
1280*0Sstevel@tonic-gate    $timeout = $stop_time - &time();
1281*0Sstevel@tonic-gate    # Force a minimum of 10 ms timeout.
1282*0Sstevel@tonic-gate    $timeout = 0.01 if $timeout < 0.01;
1283*0Sstevel@tonic-gate  } else {
1284*0Sstevel@tonic-gate    # No hosts left to wait for
1285*0Sstevel@tonic-gate    $timeout = 0;
1286*0Sstevel@tonic-gate  }
1287*0Sstevel@tonic-gate
1288*0Sstevel@tonic-gate  if ($timeout > 0) {
1289*0Sstevel@tonic-gate    my $nfound;
1290*0Sstevel@tonic-gate    while ( keys %{ $self->{"syn"} } and
1291*0Sstevel@tonic-gate           $nfound = mselect((my $rout=$rbits), undef, undef, $timeout)) {
1292*0Sstevel@tonic-gate      # Done waiting for one of the ACKs
1293*0Sstevel@tonic-gate      if (!sysread($self->{"fork_rd"}, $_, 16)) {
1294*0Sstevel@tonic-gate        # Socket closed, which means all children are done.
1295*0Sstevel@tonic-gate        return ();
1296*0Sstevel@tonic-gate      }
1297*0Sstevel@tonic-gate      my ($pid, $how) = split;
1298*0Sstevel@tonic-gate      if ($pid) {
1299*0Sstevel@tonic-gate        # Flush the zombie
1300*0Sstevel@tonic-gate        waitpid($pid, 0);
1301*0Sstevel@tonic-gate        if (my $entry = $self->{"syn"}->{$pid}) {
1302*0Sstevel@tonic-gate          # Connection attempt to remote host is done
1303*0Sstevel@tonic-gate          delete $self->{"syn"}->{$pid};
1304*0Sstevel@tonic-gate          if (!$how || # If there was no error connecting
1305*0Sstevel@tonic-gate              (!$self->{"econnrefused"} &&
1306*0Sstevel@tonic-gate               $how == ECONNREFUSED)) {  # "Connection refused" means reachable
1307*0Sstevel@tonic-gate            if ($host && $entry->[0] ne $host) {
1308*0Sstevel@tonic-gate              # A good connection, but not the host we need.
1309*0Sstevel@tonic-gate              # Move it from the "syn" hash to the "good" hash.
1310*0Sstevel@tonic-gate              $self->{"good"}->{$entry->[0]} = $entry;
1311*0Sstevel@tonic-gate              # And wait for the next winner
1312*0Sstevel@tonic-gate              next;
1313*0Sstevel@tonic-gate            }
1314*0Sstevel@tonic-gate            return ($entry->[0], &time() - $entry->[3], inet_ntoa($entry->[1]));
1315*0Sstevel@tonic-gate          }
1316*0Sstevel@tonic-gate        } else {
1317*0Sstevel@tonic-gate          # Should never happen
1318*0Sstevel@tonic-gate          die "Unknown ping from pid [$pid]";
1319*0Sstevel@tonic-gate        }
1320*0Sstevel@tonic-gate      } else {
1321*0Sstevel@tonic-gate        die "Empty response from status socket?";
1322*0Sstevel@tonic-gate      }
1323*0Sstevel@tonic-gate    }
1324*0Sstevel@tonic-gate    if (defined $nfound) {
1325*0Sstevel@tonic-gate      # Timed out waiting for ACK status
1326*0Sstevel@tonic-gate    } else {
1327*0Sstevel@tonic-gate      # Weird error occurred with select()
1328*0Sstevel@tonic-gate      warn("select: $!");
1329*0Sstevel@tonic-gate    }
1330*0Sstevel@tonic-gate  }
1331*0Sstevel@tonic-gate  if (my @synners = keys %{ $self->{"syn"} }) {
1332*0Sstevel@tonic-gate    # Kill all the synners
1333*0Sstevel@tonic-gate    kill 9, @synners;
1334*0Sstevel@tonic-gate    foreach my $pid (@synners) {
1335*0Sstevel@tonic-gate      # Wait for the deaths to finish
1336*0Sstevel@tonic-gate      # Then flush off the zombie
1337*0Sstevel@tonic-gate      waitpid($pid, 0);
1338*0Sstevel@tonic-gate    }
1339*0Sstevel@tonic-gate  }
1340*0Sstevel@tonic-gate  $self->{"syn"} = {};
1341*0Sstevel@tonic-gate  return ();
1342*0Sstevel@tonic-gate}
1343*0Sstevel@tonic-gate
1344*0Sstevel@tonic-gate# Description:  Tell why the ack() failed
1345*0Sstevel@tonic-gatesub nack {
1346*0Sstevel@tonic-gate  my $self = shift;
1347*0Sstevel@tonic-gate  my $host = shift || croak('Usage> nack($failed_ack_host)');
1348*0Sstevel@tonic-gate  return $self->{"bad"}->{$host} || undef;
1349*0Sstevel@tonic-gate}
1350*0Sstevel@tonic-gate
1351*0Sstevel@tonic-gate# Description:  Close the connection.
1352*0Sstevel@tonic-gate
1353*0Sstevel@tonic-gatesub close
1354*0Sstevel@tonic-gate{
1355*0Sstevel@tonic-gate  my ($self) = @_;
1356*0Sstevel@tonic-gate
1357*0Sstevel@tonic-gate  if ($self->{"proto"} eq "syn") {
1358*0Sstevel@tonic-gate    delete $self->{"syn"};
1359*0Sstevel@tonic-gate  } elsif ($self->{"proto"} eq "tcp") {
1360*0Sstevel@tonic-gate    # The connection will already be closed
1361*0Sstevel@tonic-gate  } else {
1362*0Sstevel@tonic-gate    $self->{"fh"}->close();
1363*0Sstevel@tonic-gate  }
1364*0Sstevel@tonic-gate}
1365*0Sstevel@tonic-gate
1366*0Sstevel@tonic-gate
1367*0Sstevel@tonic-gate1;
1368*0Sstevel@tonic-gate__END__
1369*0Sstevel@tonic-gate
1370*0Sstevel@tonic-gate=head1 NAME
1371*0Sstevel@tonic-gate
1372*0Sstevel@tonic-gateNet::Ping - check a remote host for reachability
1373*0Sstevel@tonic-gate
1374*0Sstevel@tonic-gate=head1 SYNOPSIS
1375*0Sstevel@tonic-gate
1376*0Sstevel@tonic-gate    use Net::Ping;
1377*0Sstevel@tonic-gate
1378*0Sstevel@tonic-gate    $p = Net::Ping->new();
1379*0Sstevel@tonic-gate    print "$host is alive.\n" if $p->ping($host);
1380*0Sstevel@tonic-gate    $p->close();
1381*0Sstevel@tonic-gate
1382*0Sstevel@tonic-gate    $p = Net::Ping->new("icmp");
1383*0Sstevel@tonic-gate    $p->bind($my_addr); # Specify source interface of pings
1384*0Sstevel@tonic-gate    foreach $host (@host_array)
1385*0Sstevel@tonic-gate    {
1386*0Sstevel@tonic-gate        print "$host is ";
1387*0Sstevel@tonic-gate        print "NOT " unless $p->ping($host, 2);
1388*0Sstevel@tonic-gate        print "reachable.\n";
1389*0Sstevel@tonic-gate        sleep(1);
1390*0Sstevel@tonic-gate    }
1391*0Sstevel@tonic-gate    $p->close();
1392*0Sstevel@tonic-gate
1393*0Sstevel@tonic-gate    $p = Net::Ping->new("tcp", 2);
1394*0Sstevel@tonic-gate    # Try connecting to the www port instead of the echo port
1395*0Sstevel@tonic-gate    $p->{port_num} = getservbyname("http", "tcp");
1396*0Sstevel@tonic-gate    while ($stop_time > time())
1397*0Sstevel@tonic-gate    {
1398*0Sstevel@tonic-gate        print "$host not reachable ", scalar(localtime()), "\n"
1399*0Sstevel@tonic-gate            unless $p->ping($host);
1400*0Sstevel@tonic-gate        sleep(300);
1401*0Sstevel@tonic-gate    }
1402*0Sstevel@tonic-gate    undef($p);
1403*0Sstevel@tonic-gate
1404*0Sstevel@tonic-gate    # Like tcp protocol, but with many hosts
1405*0Sstevel@tonic-gate    $p = Net::Ping->new("syn");
1406*0Sstevel@tonic-gate    $p->{port_num} = getservbyname("http", "tcp");
1407*0Sstevel@tonic-gate    foreach $host (@host_array) {
1408*0Sstevel@tonic-gate      $p->ping($host);
1409*0Sstevel@tonic-gate    }
1410*0Sstevel@tonic-gate    while (($host,$rtt,$ip) = $p->ack) {
1411*0Sstevel@tonic-gate      print "HOST: $host [$ip] ACKed in $rtt seconds.\n";
1412*0Sstevel@tonic-gate    }
1413*0Sstevel@tonic-gate
1414*0Sstevel@tonic-gate    # High precision syntax (requires Time::HiRes)
1415*0Sstevel@tonic-gate    $p = Net::Ping->new();
1416*0Sstevel@tonic-gate    $p->hires();
1417*0Sstevel@tonic-gate    ($ret, $duration, $ip) = $p->ping($host, 5.5);
1418*0Sstevel@tonic-gate    printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration)
1419*0Sstevel@tonic-gate      if $ret;
1420*0Sstevel@tonic-gate    $p->close();
1421*0Sstevel@tonic-gate
1422*0Sstevel@tonic-gate    # For backward compatibility
1423*0Sstevel@tonic-gate    print "$host is alive.\n" if pingecho($host);
1424*0Sstevel@tonic-gate
1425*0Sstevel@tonic-gate=head1 DESCRIPTION
1426*0Sstevel@tonic-gate
1427*0Sstevel@tonic-gateThis module contains methods to test the reachability of remote
1428*0Sstevel@tonic-gatehosts on a network.  A ping object is first created with optional
1429*0Sstevel@tonic-gateparameters, a variable number of hosts may be pinged multiple
1430*0Sstevel@tonic-gatetimes and then the connection is closed.
1431*0Sstevel@tonic-gate
1432*0Sstevel@tonic-gateYou may choose one of six different protocols to use for the
1433*0Sstevel@tonic-gateping. The "tcp" protocol is the default. Note that a live remote host
1434*0Sstevel@tonic-gatemay still fail to be pingable by one or more of these protocols. For
1435*0Sstevel@tonic-gateexample, www.microsoft.com is generally alive but not "icmp" pingable.
1436*0Sstevel@tonic-gate
1437*0Sstevel@tonic-gateWith the "tcp" protocol the ping() method attempts to establish a
1438*0Sstevel@tonic-gateconnection to the remote host's echo port.  If the connection is
1439*0Sstevel@tonic-gatesuccessfully established, the remote host is considered reachable.  No
1440*0Sstevel@tonic-gatedata is actually echoed.  This protocol does not require any special
1441*0Sstevel@tonic-gateprivileges but has higher overhead than the "udp" and "icmp" protocols.
1442*0Sstevel@tonic-gate
1443*0Sstevel@tonic-gateSpecifying the "udp" protocol causes the ping() method to send a udp
1444*0Sstevel@tonic-gatepacket to the remote host's echo port.  If the echoed packet is
1445*0Sstevel@tonic-gatereceived from the remote host and the received packet contains the
1446*0Sstevel@tonic-gatesame data as the packet that was sent, the remote host is considered
1447*0Sstevel@tonic-gatereachable.  This protocol does not require any special privileges.
1448*0Sstevel@tonic-gateIt should be borne in mind that, for a udp ping, a host
1449*0Sstevel@tonic-gatewill be reported as unreachable if it is not running the
1450*0Sstevel@tonic-gateappropriate echo service.  For Unix-like systems see L<inetd(8)>
1451*0Sstevel@tonic-gatefor more information.
1452*0Sstevel@tonic-gate
1453*0Sstevel@tonic-gateIf the "icmp" protocol is specified, the ping() method sends an icmp
1454*0Sstevel@tonic-gateecho message to the remote host, which is what the UNIX ping program
1455*0Sstevel@tonic-gatedoes.  If the echoed message is received from the remote host and
1456*0Sstevel@tonic-gatethe echoed information is correct, the remote host is considered
1457*0Sstevel@tonic-gatereachable.  Specifying the "icmp" protocol requires that the program
1458*0Sstevel@tonic-gatebe run as root or that the program be setuid to root.
1459*0Sstevel@tonic-gate
1460*0Sstevel@tonic-gateIf the "external" protocol is specified, the ping() method attempts to
1461*0Sstevel@tonic-gateuse the C<Net::Ping::External> module to ping the remote host.
1462*0Sstevel@tonic-gateC<Net::Ping::External> interfaces with your system's default C<ping>
1463*0Sstevel@tonic-gateutility to perform the ping, and generally produces relatively
1464*0Sstevel@tonic-gateaccurate results. If C<Net::Ping::External> if not installed on your
1465*0Sstevel@tonic-gatesystem, specifying the "external" protocol will result in an error.
1466*0Sstevel@tonic-gate
1467*0Sstevel@tonic-gateIf the "syn" protocol is specified, the ping() method will only
1468*0Sstevel@tonic-gatesend a TCP SYN packet to the remote host then immediately return.
1469*0Sstevel@tonic-gateIf the syn packet was sent successfully, it will return a true value,
1470*0Sstevel@tonic-gateotherwise it will return false.  NOTE: Unlike the other protocols,
1471*0Sstevel@tonic-gatethe return value does NOT determine if the remote host is alive or
1472*0Sstevel@tonic-gatenot since the full TCP three-way handshake may not have completed
1473*0Sstevel@tonic-gateyet.  The remote host is only considered reachable if it receives
1474*0Sstevel@tonic-gatea TCP ACK within the timeout specifed.  To begin waiting for the
1475*0Sstevel@tonic-gateACK packets, use the ack() method as explained below.  Use the
1476*0Sstevel@tonic-gate"syn" protocol instead the "tcp" protocol to determine reachability
1477*0Sstevel@tonic-gateof multiple destinations simultaneously by sending parallel TCP
1478*0Sstevel@tonic-gateSYN packets.  It will not block while testing each remote host.
1479*0Sstevel@tonic-gatedemo/fping is provided in this distribution to demonstrate the
1480*0Sstevel@tonic-gate"syn" protocol as an example.
1481*0Sstevel@tonic-gateThis protocol does not require any special privileges.
1482*0Sstevel@tonic-gate
1483*0Sstevel@tonic-gate=head2 Functions
1484*0Sstevel@tonic-gate
1485*0Sstevel@tonic-gate=over 4
1486*0Sstevel@tonic-gate
1487*0Sstevel@tonic-gate=item Net::Ping->new([$proto [, $def_timeout [, $bytes [, $device [, $tos ]]]]]);
1488*0Sstevel@tonic-gate
1489*0Sstevel@tonic-gateCreate a new ping object.  All of the parameters are optional.  $proto
1490*0Sstevel@tonic-gatespecifies the protocol to use when doing a ping.  The current choices
1491*0Sstevel@tonic-gateare "tcp", "udp", "icmp", "stream", "syn", or "external".
1492*0Sstevel@tonic-gateThe default is "tcp".
1493*0Sstevel@tonic-gate
1494*0Sstevel@tonic-gateIf a default timeout ($def_timeout) in seconds is provided, it is used
1495*0Sstevel@tonic-gatewhen a timeout is not given to the ping() method (below).  The timeout
1496*0Sstevel@tonic-gatemust be greater than 0 and the default, if not specified, is 5 seconds.
1497*0Sstevel@tonic-gate
1498*0Sstevel@tonic-gateIf the number of data bytes ($bytes) is given, that many data bytes
1499*0Sstevel@tonic-gateare included in the ping packet sent to the remote host. The number of
1500*0Sstevel@tonic-gatedata bytes is ignored if the protocol is "tcp".  The minimum (and
1501*0Sstevel@tonic-gatedefault) number of data bytes is 1 if the protocol is "udp" and 0
1502*0Sstevel@tonic-gateotherwise.  The maximum number of data bytes that can be specified is
1503*0Sstevel@tonic-gate1024.
1504*0Sstevel@tonic-gate
1505*0Sstevel@tonic-gateIf $device is given, this device is used to bind the source endpoint
1506*0Sstevel@tonic-gatebefore sending the ping packet.  I beleive this only works with
1507*0Sstevel@tonic-gatesuperuser privileges and with udp and icmp protocols at this time.
1508*0Sstevel@tonic-gate
1509*0Sstevel@tonic-gateIf $tos is given, this ToS is configured into the soscket.
1510*0Sstevel@tonic-gate
1511*0Sstevel@tonic-gate=item $p->ping($host [, $timeout]);
1512*0Sstevel@tonic-gate
1513*0Sstevel@tonic-gatePing the remote host and wait for a response.  $host can be either the
1514*0Sstevel@tonic-gatehostname or the IP number of the remote host.  The optional timeout
1515*0Sstevel@tonic-gatemust be greater than 0 seconds and defaults to whatever was specified
1516*0Sstevel@tonic-gatewhen the ping object was created.  Returns a success flag.  If the
1517*0Sstevel@tonic-gatehostname cannot be found or there is a problem with the IP number, the
1518*0Sstevel@tonic-gatesuccess flag returned will be undef.  Otherwise, the success flag will
1519*0Sstevel@tonic-gatebe 1 if the host is reachable and 0 if it is not.  For most practical
1520*0Sstevel@tonic-gatepurposes, undef and 0 and can be treated as the same case.  In array
1521*0Sstevel@tonic-gatecontext, the elapsed time as well as the string form of the ip the
1522*0Sstevel@tonic-gatehost resolved to are also returned.  The elapsed time value will
1523*0Sstevel@tonic-gatebe a float, as retuned by the Time::HiRes::time() function, if hires()
1524*0Sstevel@tonic-gatehas been previously called, otherwise it is returned as an integer.
1525*0Sstevel@tonic-gate
1526*0Sstevel@tonic-gate=item $p->source_verify( { 0 | 1 } );
1527*0Sstevel@tonic-gate
1528*0Sstevel@tonic-gateAllows source endpoint verification to be enabled or disabled.
1529*0Sstevel@tonic-gateThis is useful for those remote destinations with multiples
1530*0Sstevel@tonic-gateinterfaces where the response may not originate from the same
1531*0Sstevel@tonic-gateendpoint that the original destination endpoint was sent to.
1532*0Sstevel@tonic-gateThis only affects udp and icmp protocol pings.
1533*0Sstevel@tonic-gate
1534*0Sstevel@tonic-gateThis is enabled by default.
1535*0Sstevel@tonic-gate
1536*0Sstevel@tonic-gate=item $p->service_check( { 0 | 1 } );
1537*0Sstevel@tonic-gate
1538*0Sstevel@tonic-gateSet whether or not the connect behavior should enforce
1539*0Sstevel@tonic-gateremote service availability as well as reachability.  Normally,
1540*0Sstevel@tonic-gateif the remote server reported ECONNREFUSED, it must have been
1541*0Sstevel@tonic-gatereachable because of the status packet that it reported.
1542*0Sstevel@tonic-gateWith this option enabled, the full three-way tcp handshake
1543*0Sstevel@tonic-gatemust have been established successfully before it will
1544*0Sstevel@tonic-gateclaim it is reachable.  NOTE:  It still does nothing more
1545*0Sstevel@tonic-gatethan connect and disconnect.  It does not speak any protocol
1546*0Sstevel@tonic-gate(i.e., HTTP or FTP) to ensure the remote server is sane in
1547*0Sstevel@tonic-gateany way.  The remote server CPU could be grinding to a halt
1548*0Sstevel@tonic-gateand unresponsive to any clients connecting, but if the kernel
1549*0Sstevel@tonic-gatethrows the ACK packet, it is considered alive anyway.  To
1550*0Sstevel@tonic-gatereally determine if the server is responding well would be
1551*0Sstevel@tonic-gateapplication specific and is beyond the scope of Net::Ping.
1552*0Sstevel@tonic-gateFor udp protocol, enabling this option demands that the
1553*0Sstevel@tonic-gateremote server replies with the same udp data that it was sent
1554*0Sstevel@tonic-gateas defined by the udp echo service.
1555*0Sstevel@tonic-gate
1556*0Sstevel@tonic-gateThis affects the "udp", "tcp", and "syn" protocols.
1557*0Sstevel@tonic-gate
1558*0Sstevel@tonic-gateThis is disabled by default.
1559*0Sstevel@tonic-gate
1560*0Sstevel@tonic-gate=item $p->tcp_service_check( { 0 | 1 } );
1561*0Sstevel@tonic-gate
1562*0Sstevel@tonic-gateDepricated method, but does the same as service_check() method.
1563*0Sstevel@tonic-gate
1564*0Sstevel@tonic-gate=item $p->hires( { 0 | 1 } );
1565*0Sstevel@tonic-gate
1566*0Sstevel@tonic-gateCauses this module to use Time::HiRes module, allowing milliseconds
1567*0Sstevel@tonic-gateto be returned by subsequent calls to ping().
1568*0Sstevel@tonic-gate
1569*0Sstevel@tonic-gateThis is disabled by default.
1570*0Sstevel@tonic-gate
1571*0Sstevel@tonic-gate=item $p->bind($local_addr);
1572*0Sstevel@tonic-gate
1573*0Sstevel@tonic-gateSets the source address from which pings will be sent.  This must be
1574*0Sstevel@tonic-gatethe address of one of the interfaces on the local host.  $local_addr
1575*0Sstevel@tonic-gatemay be specified as a hostname or as a text IP address such as
1576*0Sstevel@tonic-gate"192.168.1.1".
1577*0Sstevel@tonic-gate
1578*0Sstevel@tonic-gateIf the protocol is set to "tcp", this method may be called any
1579*0Sstevel@tonic-gatenumber of times, and each call to the ping() method (below) will use
1580*0Sstevel@tonic-gatethe most recent $local_addr.  If the protocol is "icmp" or "udp",
1581*0Sstevel@tonic-gatethen bind() must be called at most once per object, and (if it is
1582*0Sstevel@tonic-gatecalled at all) must be called before the first call to ping() for that
1583*0Sstevel@tonic-gateobject.
1584*0Sstevel@tonic-gate
1585*0Sstevel@tonic-gate=item $p->open($host);
1586*0Sstevel@tonic-gate
1587*0Sstevel@tonic-gateWhen you are using the "stream" protocol, this call pre-opens the
1588*0Sstevel@tonic-gatetcp socket.  It's only necessary to do this if you want to
1589*0Sstevel@tonic-gateprovide a different timeout when creating the connection, or
1590*0Sstevel@tonic-gateremove the overhead of establishing the connection from the
1591*0Sstevel@tonic-gatefirst ping.  If you don't call C<open()>, the connection is
1592*0Sstevel@tonic-gateautomatically opened the first time C<ping()> is called.
1593*0Sstevel@tonic-gateThis call simply does nothing if you are using any protocol other
1594*0Sstevel@tonic-gatethan stream.
1595*0Sstevel@tonic-gate
1596*0Sstevel@tonic-gate=item $p->ack( [ $host ] );
1597*0Sstevel@tonic-gate
1598*0Sstevel@tonic-gateWhen using the "syn" protocol, use this method to determine
1599*0Sstevel@tonic-gatethe reachability of the remote host.  This method is meant
1600*0Sstevel@tonic-gateto be called up to as many times as ping() was called.  Each
1601*0Sstevel@tonic-gatecall returns the host (as passed to ping()) that came back
1602*0Sstevel@tonic-gatewith the TCP ACK.  The order in which the hosts are returned
1603*0Sstevel@tonic-gatemay not necessarily be the same order in which they were
1604*0Sstevel@tonic-gateSYN queued using the ping() method.  If the timeout is
1605*0Sstevel@tonic-gatereached before the TCP ACK is received, or if the remote
1606*0Sstevel@tonic-gatehost is not listening on the port attempted, then the TCP
1607*0Sstevel@tonic-gateconnection will not be established and ack() will return
1608*0Sstevel@tonic-gateundef.  In list context, the host, the ack time, and the
1609*0Sstevel@tonic-gatedotted ip string will be returned instead of just the host.
1610*0Sstevel@tonic-gateIf the optional $host argument is specified, the return
1611*0Sstevel@tonic-gatevalue will be partaining to that host only.
1612*0Sstevel@tonic-gateThis call simply does nothing if you are using any protocol
1613*0Sstevel@tonic-gateother than syn.
1614*0Sstevel@tonic-gate
1615*0Sstevel@tonic-gate=item $p->nack( $failed_ack_host );
1616*0Sstevel@tonic-gate
1617*0Sstevel@tonic-gateThe reason that host $failed_ack_host did not receive a
1618*0Sstevel@tonic-gatevalid ACK.  Useful to find out why when ack( $fail_ack_host )
1619*0Sstevel@tonic-gatereturns a false value.
1620*0Sstevel@tonic-gate
1621*0Sstevel@tonic-gate=item $p->close();
1622*0Sstevel@tonic-gate
1623*0Sstevel@tonic-gateClose the network connection for this ping object.  The network
1624*0Sstevel@tonic-gateconnection is also closed by "undef $p".  The network connection is
1625*0Sstevel@tonic-gateautomatically closed if the ping object goes out of scope (e.g. $p is
1626*0Sstevel@tonic-gatelocal to a subroutine and you leave the subroutine).
1627*0Sstevel@tonic-gate
1628*0Sstevel@tonic-gate=item pingecho($host [, $timeout]);
1629*0Sstevel@tonic-gate
1630*0Sstevel@tonic-gateTo provide backward compatibility with the previous version of
1631*0Sstevel@tonic-gateNet::Ping, a pingecho() subroutine is available with the same
1632*0Sstevel@tonic-gatefunctionality as before.  pingecho() uses the tcp protocol.  The
1633*0Sstevel@tonic-gatereturn values and parameters are the same as described for the ping()
1634*0Sstevel@tonic-gatemethod.  This subroutine is obsolete and may be removed in a future
1635*0Sstevel@tonic-gateversion of Net::Ping.
1636*0Sstevel@tonic-gate
1637*0Sstevel@tonic-gate=back
1638*0Sstevel@tonic-gate
1639*0Sstevel@tonic-gate=head1 NOTES
1640*0Sstevel@tonic-gate
1641*0Sstevel@tonic-gateThere will be less network overhead (and some efficiency in your
1642*0Sstevel@tonic-gateprogram) if you specify either the udp or the icmp protocol.  The tcp
1643*0Sstevel@tonic-gateprotocol will generate 2.5 times or more traffic for each ping than
1644*0Sstevel@tonic-gateeither udp or icmp.  If many hosts are pinged frequently, you may wish
1645*0Sstevel@tonic-gateto implement a small wait (e.g. 25ms or more) between each ping to
1646*0Sstevel@tonic-gateavoid flooding your network with packets.
1647*0Sstevel@tonic-gate
1648*0Sstevel@tonic-gateThe icmp protocol requires that the program be run as root or that it
1649*0Sstevel@tonic-gatebe setuid to root.  The other protocols do not require special
1650*0Sstevel@tonic-gateprivileges, but not all network devices implement tcp or udp echo.
1651*0Sstevel@tonic-gate
1652*0Sstevel@tonic-gateLocal hosts should normally respond to pings within milliseconds.
1653*0Sstevel@tonic-gateHowever, on a very congested network it may take up to 3 seconds or
1654*0Sstevel@tonic-gatelonger to receive an echo packet from the remote host.  If the timeout
1655*0Sstevel@tonic-gateis set too low under these conditions, it will appear that the remote
1656*0Sstevel@tonic-gatehost is not reachable (which is almost the truth).
1657*0Sstevel@tonic-gate
1658*0Sstevel@tonic-gateReachability doesn't necessarily mean that the remote host is actually
1659*0Sstevel@tonic-gatefunctioning beyond its ability to echo packets.  tcp is slightly better
1660*0Sstevel@tonic-gateat indicating the health of a system than icmp because it uses more
1661*0Sstevel@tonic-gateof the networking stack to respond.
1662*0Sstevel@tonic-gate
1663*0Sstevel@tonic-gateBecause of a lack of anything better, this module uses its own
1664*0Sstevel@tonic-gateroutines to pack and unpack ICMP packets.  It would be better for a
1665*0Sstevel@tonic-gateseparate module to be written which understands all of the different
1666*0Sstevel@tonic-gatekinds of ICMP packets.
1667*0Sstevel@tonic-gate
1668*0Sstevel@tonic-gate=head1 INSTALL
1669*0Sstevel@tonic-gate
1670*0Sstevel@tonic-gateThe latest source tree is available via cvs:
1671*0Sstevel@tonic-gate
1672*0Sstevel@tonic-gate  cvs -z3 -q -d :pserver:anonymous@cvs.roobik.com.:/usr/local/cvsroot/freeware checkout Net-Ping
1673*0Sstevel@tonic-gate  cd Net-Ping
1674*0Sstevel@tonic-gate
1675*0Sstevel@tonic-gateThe tarball can be created as follows:
1676*0Sstevel@tonic-gate
1677*0Sstevel@tonic-gate  perl Makefile.PL ; make ; make dist
1678*0Sstevel@tonic-gate
1679*0Sstevel@tonic-gateThe latest Net::Ping release can be found at CPAN:
1680*0Sstevel@tonic-gate
1681*0Sstevel@tonic-gate  $CPAN/modules/by-module/Net/
1682*0Sstevel@tonic-gate
1683*0Sstevel@tonic-gate1) Extract the tarball
1684*0Sstevel@tonic-gate
1685*0Sstevel@tonic-gate  gtar -zxvf Net-Ping-xxxx.tar.gz
1686*0Sstevel@tonic-gate  cd Net-Ping-xxxx
1687*0Sstevel@tonic-gate
1688*0Sstevel@tonic-gate2) Build:
1689*0Sstevel@tonic-gate
1690*0Sstevel@tonic-gate  make realclean
1691*0Sstevel@tonic-gate  perl Makefile.PL
1692*0Sstevel@tonic-gate  make
1693*0Sstevel@tonic-gate  make test
1694*0Sstevel@tonic-gate
1695*0Sstevel@tonic-gate3) Install
1696*0Sstevel@tonic-gate
1697*0Sstevel@tonic-gate  make install
1698*0Sstevel@tonic-gate
1699*0Sstevel@tonic-gateOr install it RPM Style:
1700*0Sstevel@tonic-gate
1701*0Sstevel@tonic-gate  rpm -ta SOURCES/Net-Ping-xxxx.tar.gz
1702*0Sstevel@tonic-gate
1703*0Sstevel@tonic-gate  rpm -ih RPMS/noarch/perl-Net-Ping-xxxx.rpm
1704*0Sstevel@tonic-gate
1705*0Sstevel@tonic-gate=head1 BUGS
1706*0Sstevel@tonic-gate
1707*0Sstevel@tonic-gateFor a list of known issues, visit:
1708*0Sstevel@tonic-gate
1709*0Sstevel@tonic-gatehttps://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Ping
1710*0Sstevel@tonic-gate
1711*0Sstevel@tonic-gateTo report a new bug, visit:
1712*0Sstevel@tonic-gate
1713*0Sstevel@tonic-gatehttps://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Ping
1714*0Sstevel@tonic-gate
1715*0Sstevel@tonic-gate=head1 AUTHORS
1716*0Sstevel@tonic-gate
1717*0Sstevel@tonic-gate  Current maintainer:
1718*0Sstevel@tonic-gate    bbb@cpan.org (Rob Brown)
1719*0Sstevel@tonic-gate
1720*0Sstevel@tonic-gate  External protocol:
1721*0Sstevel@tonic-gate    colinm@cpan.org (Colin McMillen)
1722*0Sstevel@tonic-gate
1723*0Sstevel@tonic-gate  Stream protocol:
1724*0Sstevel@tonic-gate    bronson@trestle.com (Scott Bronson)
1725*0Sstevel@tonic-gate
1726*0Sstevel@tonic-gate  Original pingecho():
1727*0Sstevel@tonic-gate    karrer@bernina.ethz.ch (Andreas Karrer)
1728*0Sstevel@tonic-gate    pmarquess@bfsec.bt.co.uk (Paul Marquess)
1729*0Sstevel@tonic-gate
1730*0Sstevel@tonic-gate  Original Net::Ping author:
1731*0Sstevel@tonic-gate    mose@ns.ccsn.edu (Russell Mosemann)
1732*0Sstevel@tonic-gate
1733*0Sstevel@tonic-gate=head1 COPYRIGHT
1734*0Sstevel@tonic-gate
1735*0Sstevel@tonic-gateCopyright (c) 2002-2003, Rob Brown.  All rights reserved.
1736*0Sstevel@tonic-gate
1737*0Sstevel@tonic-gateCopyright (c) 2001, Colin McMillen.  All rights reserved.
1738*0Sstevel@tonic-gate
1739*0Sstevel@tonic-gateThis program is free software; you may redistribute it and/or
1740*0Sstevel@tonic-gatemodify it under the same terms as Perl itself.
1741*0Sstevel@tonic-gate
1742*0Sstevel@tonic-gate$Id: Ping.pm,v 1.86 2003/06/27 21:31:07 rob Exp $
1743*0Sstevel@tonic-gate
1744*0Sstevel@tonic-gate=cut
1745