Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Net::Ping patch #2987

Closed
p5pRT opened this issue Dec 14, 2000 · 5 comments
Closed

Net::Ping patch #2987

p5pRT opened this issue Dec 14, 2000 · 5 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 14, 2000

Migrated from rt.perl.org#4909 (status was 'resolved')

Searchable as RT4909$

@p5pRT
Copy link
Author

p5pRT commented Dec 14, 2000

From gellyfish@gellyfish.com

Hello,

With Net​::Ping, when using the default udp 'ping', undef may be returned by
recv() where there is no udp echo service on the pinged host - this
was being passed unchecked to sockaddr_in resulting in the error :

Use of uninitialized value in subroutine entry at
/usr/lib/perl5/5.6.0/i686-linux/Socket.pm line 311.
Bad arg length for Socket​::unpack_sockaddr_in, length is 0, should be 16 at
/usr/lib/perl5/5.6.0/i686-linux/Socket.pm line 311.

which is confusing to the user of the module and unnecessary. The following
patch tests for this condition , it also makes explicit that the absence
of an echo service on a pinged host (for udp and tcp 'ping') is a possible
reason for a host being reported as unreachable.

The patch is against version 2.02 of the module as distributed with 5.6.0 .

Inline Patch
--- Ping.pm~	Thu Dec 14 07:12:10 2000
+++ Ping.pm	Thu Dec 14 07:56:22 2000
@@ -369,14 +369,20 @@
         elsif ($nfound)         # A packet is waiting
         {
             $from_msg = "";
-            $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags);
-            ($from_port, $from_ip) = sockaddr_in($from_saddr);
-            if (($from_ip eq $ip) &&        # Does the packet check out?
-                ($from_port == $self->{"port_num"}) &&
-                ($from_msg eq $msg))
+            if( $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags) )
             {
-                $ret = 1;       # It's a winner
-                $done = 1;
+               ($from_port, $from_ip) = sockaddr_in($from_saddr);
+               if (($from_ip eq $ip) &&        # Does the packet check out?
+                   ($from_port == $self->{"port_num"}) &&
+                   ($from_msg eq $msg))
+               {
+                   $ret = 1;       # It's a winner
+                   $done = 1;
+               }
+            }
+            else # probably no UDP echo service running.  But persist.
+            {
+               $done = 0;
             }
         }
         else                    # Oops, timed out
@@ -454,6 +460,11 @@
 received from the remote host and the received packet contains the
 same data as the packet that was sent, the remote host is considered
 reachable.  This protocol does not require any special privileges.
+
+It should be borne in mind that, for both tcp and udp ping, a host
+will be reported as unreachable if if not is not running the
+appropriate echo service.  For Unix-like systems see L<inetd(8)> for
+more information.
 
 If the "icmp" protocol is specified, the ping() method sends an icmp
 echo message to the remote host, which is what the UNIX ping program



/J\ \-\- Jonathan Stowe | \ | This space for rent   |

@p5pRT
Copy link
Author

p5pRT commented Dec 14, 2000

From gellyfish@gellyfish.com

Ping.diff
--- Ping.pm~	Thu Dec 14 07:12:10 2000
+++ Ping.pm	Thu Dec 14 07:56:22 2000
@@ -369,14 +369,20 @@
         elsif ($nfound)         # A packet is waiting
         {
             $from_msg = "";
-            $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags);
-            ($from_port, $from_ip) = sockaddr_in($from_saddr);
-            if (($from_ip eq $ip) &&        # Does the packet check out?
-                ($from_port == $self->{"port_num"}) &&
-                ($from_msg eq $msg))
+            if( $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags) )
             {
-                $ret = 1;       # It's a winner
-                $done = 1;
+               ($from_port, $from_ip) = sockaddr_in($from_saddr);
+               if (($from_ip eq $ip) &&        # Does the packet check out?
+                   ($from_port == $self->{"port_num"}) &&
+                   ($from_msg eq $msg))
+               {
+                   $ret = 1;       # It's a winner
+                   $done = 1;
+               }
+            }
+            else # probably no UDP echo service running.  But persist.
+            {
+               $done = 0;
             }
         }
         else                    # Oops, timed out
@@ -454,6 +460,11 @@
 received from the remote host and the received packet contains the
 same data as the packet that was sent, the remote host is considered
 reachable.  This protocol does not require any special privileges.
+
+It should be borne in mind that, for both tcp and udp ping, a host
+will be reported as unreachable if if not is not running the
+appropriate echo service.  For Unix-like systems see L<inetd(8)> for
+more information.
 
 If the "icmp" protocol is specified, the ping() method sends an icmp
 echo message to the remote host, which is what the UNIX ping program

@p5pRT
Copy link
Author

p5pRT commented Dec 14, 2000

From @jhi

Something very similar to your patch had already gone in to the
development version of Perl. Here's how the code looks nowadays​:

  elsif ($nfound) # A packet is waiting
  {
  $from_msg = "";
  $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags)
  or last; # For example an unreachable host will make recv() fai\
l.
  ($from_port, $from_ip) = sockaddr_in($from_saddr);
  if (($from_ip eq $ip) && # Does the packet check out?
  ($from_port == $self->{"port_num"}) &&
  ($from_msg eq $msg))
  {
  $ret = 1; # It's a winner
  $done = 1;
  }
  }
  else # Oops, timed out
  {
  $done = 1;
  }

On Thu, Dec 14, 2000 at 08​:28​:14AM +0000, Jonathan Stowe wrote​:

Hello,

With Net​::Ping, when using the default udp 'ping', undef may be returned by
recv() where there is no udp echo service on the pinged host - this
was being passed unchecked to sockaddr_in resulting in the error :

Use of uninitialized value in subroutine entry at
/usr/lib/perl5/5.6.0/i686-linux/Socket.pm line 311.
Bad arg length for Socket​::unpack_sockaddr_in, length is 0, should be 16 at
/usr/lib/perl5/5.6.0/i686-linux/Socket.pm line 311.

which is confusing to the user of the module and unnecessary. The following
patch tests for this condition , it also makes explicit that the absence
of an echo service on a pinged host (for udp and tcp 'ping') is a possible
reason for a host being reported as unreachable.

The patch is against version 2.02 of the module as distributed with 5.6.0 .

--- Ping.pm~ Thu Dec 14 07​:12​:10 2000
+++ Ping.pm Thu Dec 14 07​:56​:22 2000
@​@​ -369,14 +369,20 @​@​
elsif ($nfound) # A packet is waiting
{
$from_msg = "";
- $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags);
- ($from_port, $from_ip) = sockaddr_in($from_saddr);
- if (($from_ip eq $ip) && # Does the packet check out?
- ($from_port == $self->{"port_num"}) &&
- ($from_msg eq $msg))
+ if( $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags) )
{
- $ret = 1; # It's a winner
- $done = 1;
+ ($from_port, $from_ip) = sockaddr_in($from_saddr);
+ if (($from_ip eq $ip) && # Does the packet check out?
+ ($from_port == $self->{"port_num"}) &&
+ ($from_msg eq $msg))
+ {
+ $ret = 1; # It's a winner
+ $done = 1;
+ }
+ }
+ else # probably no UDP echo service running. But persist.
+ {
+ $done = 0;
}
}
else # Oops, timed out
@​@​ -454,6 +460,11 @​@​
received from the remote host and the received packet contains the
same data as the packet that was sent, the remote host is considered
reachable. This protocol does not require any special privileges.
+
+It should be borne in mind that, for both tcp and udp ping, a host
+will be reported as unreachable if if not is not running the
+appropriate echo service. For Unix-like systems see L<inetd(8)> for
+more information.

If the "icmp" protocol is specified, the ping() method sends an icmp
echo message to the remote host, which is what the UNIX ping program

/J\
--
Jonathan Stowe |
<http​://www.gellyfish.com> | This space for rent
|

--- Ping.pm~ Thu Dec 14 07​:12​:10 2000
+++ Ping.pm Thu Dec 14 07​:56​:22 2000
@​@​ -369,14 +369,20 @​@​
elsif ($nfound) # A packet is waiting
{
$from_msg = "";
- $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags);
- ($from_port, $from_ip) = sockaddr_in($from_saddr);
- if (($from_ip eq $ip) && # Does the packet check out?
- ($from_port == $self->{"port_num"}) &&
- ($from_msg eq $msg))
+ if( $from_saddr = recv($self->{"fh"}, $from_msg, 1500, $flags) )
{
- $ret = 1; # It's a winner
- $done = 1;
+ ($from_port, $from_ip) = sockaddr_in($from_saddr);
+ if (($from_ip eq $ip) && # Does the packet check out?
+ ($from_port == $self->{"port_num"}) &&
+ ($from_msg eq $msg))
+ {
+ $ret = 1; # It's a winner
+ $done = 1;
+ }
+ }
+ else # probably no UDP echo service running. But persist.
+ {
+ $done = 0;
}
}
else # Oops, timed out
@​@​ -454,6 +460,11 @​@​
received from the remote host and the received packet contains the
same data as the packet that was sent, the remote host is considered
reachable. This protocol does not require any special privileges.
+
+It should be borne in mind that, for both tcp and udp ping, a host
+will be reported as unreachable if if not is not running the
+appropriate echo service. For Unix-like systems see L<inetd(8)> for
+more information.

If the "icmp" protocol is specified, the ping() method sends an icmp
echo message to the remote host, which is what the UNIX ping program

@p5pRT
Copy link
Author

p5pRT commented Dec 15, 2000

From [Unknown Contact. See original ticket]

On Thu, 14 Dec 2000, Jarkko Hietaniemi wrote​:

Something very similar to your patch had already gone in to the
development version of Perl. Here's how the code looks nowadays​:

    elsif \($nfound\)         \# A packet is waiting
    \{
        $from\_msg = "";
        $from\_saddr = recv\($self\->\{"fh"\}\, $from\_msg\, 1500\, $flags\)
            or last; \# For example an unreachable host will make recv\(\) 

I must be going blind - I looked at the latest cut of the development track
and thought it was the same as the 5.6.0 version.

/J\

@p5pRT
Copy link
Author

p5pRT commented Dec 15, 2000

From @jhi

On Fri, Dec 15, 2000 at 09​:36​:05AM +0000, Jonathan Stowe wrote​:

On Thu, 14 Dec 2000, Jarkko Hietaniemi wrote​:

Something very similar to your patch had already gone in to the
development version of Perl. Here's how the code looks nowadays​:

    elsif \($nfound\)         \# A packet is waiting
    \{
        $from\_msg = "";
        $from\_saddr = recv\($self\->\{"fh"\}\, $from\_msg\, 1500\, $flags\)
            or last; \# For example an unreachable host will make recv\(\) 

I must be going blind - I looked at the latest cut of the development track
and thought it was the same as the 5.6.0 version.

The doc snippet was a good addition, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant