[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to make net-ping thread safe?

Laurent Julliard

3/15/2008 5:29:00 PM

Hi,

I'm using the excellent net-ping gem (v 1.2.2 on Ruby 1.8.6 Linux box)
from Daniel Berger to ping a number of machines on a local area network.

I recently refactored my code to use threads and I'm having troubles
with Net::Ping::ICMP. I'm trying to ping several machines, each one in a
separate thread and I'm seing strange things like when one machine is on
all three machines are reported on (although two are off). When I run my
program with one thread only the on/off state shows as expected.

So I'm wondering if the Net::Ping::ICMP#ping method is thread safe?
Daniel said he made no attempt to make sure that the code is and
usggested that I post this question here.

The code for the method Net::Ping::ICMP#ping is here:
http://www.koders.com/ruby/fidE6256BC790B8AD197544CE26287B5C7D3200E4C8.aspx...

(line 61)

Here is also a simplified version of my own code:

-----------------------------------------------

require 'rubygems'
require 'net/ping/icmp'

WORK_STATIONS = ['192.168.1.1', '192.168.1.2', '192.168.1.3']
threads = []
ping_objects = []

WORK_STATIONS.each do |ip|
ping_objects << Net::Ping::ICMP.new(ip)
threads << Thread.new(ip, ping_objects.last) do |ip, p|
puts "Monitoring #{ip}..."
loop do
puts "#{ip} is #{p.ping ? 'on' : 'off'}"
sleep 2
end
end
end

threads.each { |th| th.join }

------------------------------------------------

Any advice either to change my own script or improve the net-ping code
is welcome.

Thanks for your help!

Laurent


6 Answers

Joel VanderWerf

3/15/2008 7:54:00 PM

0

Laurent Julliard wrote:
> Hi,
>
> I'm using the excellent net-ping gem (v 1.2.2 on Ruby 1.8.6 Linux box)
> from Daniel Berger to ping a number of machines on a local area network.
>
> I recently refactored my code to use threads and I'm having troubles
> with Net::Ping::ICMP. I'm trying to ping several machines, each one in a
> separate thread and I'm seing strange things like when one machine is on
> all three machines are reported on (although two are off). When I run my
> program with one thread only the on/off state shows as expected.
>
> So I'm wondering if the Net::Ping::ICMP#ping method is thread safe?
> Daniel said he made no attempt to make sure that the code is and
> usggested that I post this question here.
>
> The code for the method Net::Ping::ICMP#ping is here:
> http://www.koders.com/ruby/fidE6256BC790B8AD197544CE26287B5C7D3200E4C8.aspx...
>
>
> (line 61)
>
> Here is also a simplified version of my own code:
>
> -----------------------------------------------
>
> require 'rubygems'
> require 'net/ping/icmp'
>
> WORK_STATIONS = ['192.168.1.1', '192.168.1.2', '192.168.1.3']
> threads = []
> ping_objects = []
>
> WORK_STATIONS.each do |ip|
> ping_objects << Net::Ping::ICMP.new(ip)
> threads << Thread.new(ip, ping_objects.last) do |ip, p|
> puts "Monitoring #{ip}..."
> loop do
> puts "#{ip} is #{p.ping ? 'on' : 'off'}"
> sleep 2
> end
> end
> end
>
> threads.each { |th| th.join }
>
> ------------------------------------------------
>
> Any advice either to change my own script or improve the net-ping code
> is welcome.
>
> Thanks for your help!
>
> Laurent
>

In icmp.rb, the problem seems to be that it uses process ID as the ICMP
packet ID, so it is impossible to tell which ECHO REPLY packet
corresponds to which request (if multiple requests come from one process).

Quoting http://tools.ietf.org/h...:

The identifier and sequence number may be used by the echo sender
to aid in matching the replies with the requests. For example,
the identifier might be used like a port in TCP or UDP to identify
a session, and the sequence number might be incremented on each
request sent. The destination returns these same values in the
reply.

Maybe it is standard practice to use pid, so that multiple ICMP clients
(e.g. /bin/ping) never step on each other. In that case, I guess icmp.rb
could fork{sleep} for each Ping instance and use the child pid as a
conflict-free identifier.[1]

However, watching tcpdump as /bin/ping is running on linux, it seems
that the ID might be a local port rather than process id. Maybe ping is
just binding a socket to 0 to ask the OS for a unique port.

Ruby's icmp should do whatever /bin/ping does.

You could work around this by filtering the response by sender host,
*if* you assume that different threads never ping the same host.

Or you could just shell out to ping (which is what I always do).

[1] http://www.ping127001.com/pingpage... uses PID.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

3/15/2008 8:14:00 PM

0

Joel VanderWerf wrote:
> In icmp.rb, the problem seems to be that it uses process ID as the ICMP
> packet ID, so it is impossible to tell which ECHO REPLY packet
> corresponds to which request (if multiple requests come from one process).

On second thought, the problem could be solved in icmp.rb by using the
sequence number to separate requests, and keeping the sequence number in
an ICMP class var instead of instance vars. Here's a patch that seems to
make your original code work correctly (detecting some hosts up and some
down).

---
/usr/local/lib/ruby/gems/1.8/gems/net-ping-1.2.2/lib/net/ping/icmp.rb
2008-01-23 19:46:14.000000000 -0800
+++ net/ping/icmp.rb 2008-03-15 13:09:55.000000000 -0700
@@ -24,7 +24,6 @@
def initialize(host=nil, port=nil, timeout=5)
raise 'requires root privileges' if Process.euid > 0

- @seq = 0
@bind_port = 0
@bind_host = nil
@data_size = 56
@@ -37,6 +36,11 @@
super(host, port, timeout)
@port = nil # This value is not used in ICMP pings.
end
+
+ @seq = 0
+ def self.next_seq
+ @seq = (@seq + 1) % 65536
+ end

# Sets the number of bytes sent in the ping method.
#
@@ -73,14 +77,14 @@
socket.bind(saddr)
end

- @seq = (@seq + 1) % 65536
+ seq = ICMP.next_seq
pstring = 'C2 n3 A' << @data_size.to_s
timeout = @timeout

checksum = 0
- msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq,
@data].pack(pstring)
+ msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, seq,
@data].pack(pstring)
checksum = checksum(msg)
- msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq,
@data].pack(pstring)
+ msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, seq,
@data].pack(pstring)

start_time = Time.now

@@ -101,7 +105,7 @@
end

pid = nil
- seq = nil
+ rcv_seq = nil

data, sender = socket.recvfrom(1500)
port, host = Socket.unpack_sockaddr_in(sender)
@@ -110,15 +114,15 @@
case type
when ICMP_ECHOREPLY
if data.length >= 28
- pid, seq = data[24, 4].unpack('n3')
+ pid, rcv_seq = data[24, 4].unpack('n3')
end
else
if data.length > 56
- pid, seq = data[52, 4].unpack('n3')
+ pid, rcv_seq = data[52, 4].unpack('n3')
end
end

- if pid == @pid && seq == @seq && type == ICMP_ECHOREPLY
+ if pid == @pid && rcv_seq == seq && type == ICMP_ECHOREPLY
bool = true
end
}


--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel VanderWerf

3/15/2008 8:21:00 PM

0


Forgot to make the previous patch thread safe:

--- net/ping/icmp.rb.bck 2008-03-15 13:19:42.000000000 -0700
+++ net/ping/icmp.rb 2008-03-15 13:19:42.000000000 -0700
@@ -1,5 +1,6 @@
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'ping'
+require 'thread'

module Net
class Ping::ICMP < Ping
@@ -38,8 +39,11 @@
end

@seq = 0
+ @seq_mutex = Mutex.new
def self.next_seq
- @seq = (@seq + 1) % 65536
+ @seq_mutex.synchronize do
+ @seq = (@seq + 1) % 65536
+ end
end

# Sets the number of bytes sent in the ping method.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Daniel Berger

3/15/2008 9:32:00 PM

0



On Mar 15, 11:28 am, Laurent Julliard <laur...@moldus.org> wrote:
> Hi,
>
> I'm using the excellent net-ping gem (v 1.2.2 on Ruby 1.8.6 Linux box)
> from Daniel Berger to ping a number of machines on a local area network.
>
> I recently refactored my code to use threads and I'm having troubles
> with Net::Ping::ICMP. I'm trying to ping several machines, each one in a
> separate thread and I'm seing strange things like when one machine is on
> all three machines are reported on (although two are off). When I run my
> program with one thread only the on/off state shows as expected.
>
> So I'm wondering if the Net::Ping::ICMP#ping method is thread safe?

<snip>

Hi Laurent,

Please apply Joel's patches and let me know how it works. If all is
well I'll apply the patches and put out another release.

BTW, thanks Joel!

Regards,

Dan

Laurent Julliard

3/16/2008 8:12:00 AM

0

Joel VanderWerf wrote:
> Joel VanderWerf wrote:
>> In icmp.rb, the problem seems to be that it uses process ID as the
>> ICMP packet ID, so it is impossible to tell which ECHO REPLY packet
>> corresponds to which request (if multiple requests come from one
>> process).
>
> On second thought, the problem could be solved in icmp.rb by using the
> sequence number to separate requests, and keeping the sequence number in
> an ICMP class var instead of instance vars. Here's a patch that seems to
> make your original code work correctly (detecting some hosts up and some
> down).
>

Joel,

Fist of all thanks for catching this!

Your recommendation is to turn the sequence number into a class variable
so shouldn't the sequence number really be named @@seq rather than @seq
if we want the sequence number to be unique across multiple instances of
ICMP objects?

And if this is so, one of the consequence is that for a given ICMP
instance chances are that the seq field in the icmp packet will not
contain consecutive values. I don't know if this is a problem wrt to the
ICMP specifications or not.

Don't you think an alternative would be not to use the PID as the packet
identifier but rather a class variable of our own that we would
increment each time a new ICMP instance is created?

Thanks again for your comments.

Laurent



> ---
> /usr/local/lib/ruby/gems/1.8/gems/net-ping-1.2.2/lib/net/ping/icmp.rb
> 2008-01-23 19:46:14.000000000 -0800
> +++ net/ping/icmp.rb 2008-03-15 13:09:55.000000000 -0700
> @@ -24,7 +24,6 @@
> def initialize(host=nil, port=nil, timeout=5)
> raise 'requires root privileges' if Process.euid > 0
>
> - @seq = 0
> @bind_port = 0
> @bind_host = nil
> @data_size = 56
> @@ -37,6 +36,11 @@
> super(host, port, timeout)
> @port = nil # This value is not used in ICMP pings.
> end
> +
> + @seq = 0
> + def self.next_seq
> + @seq = (@seq + 1) % 65536
> + end
>
> # Sets the number of bytes sent in the ping method.
> #
> @@ -73,14 +77,14 @@
> socket.bind(saddr)
> end
>
> - @seq = (@seq + 1) % 65536
> + seq = ICMP.next_seq
> pstring = 'C2 n3 A' << @data_size.to_s
> timeout = @timeout
>
> checksum = 0
> - msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq,
> @data].pack(pstring)
> + msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, seq,
> @data].pack(pstring)
> checksum = checksum(msg)
> - msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, @seq,
> @data].pack(pstring)
> + msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, @pid, seq,
> @data].pack(pstring)
>
> start_time = Time.now
>
> @@ -101,7 +105,7 @@
> end
>
> pid = nil
> - seq = nil
> + rcv_seq = nil
>
> data, sender = socket.recvfrom(1500)
> port, host = Socket.unpack_sockaddr_in(sender)
> @@ -110,15 +114,15 @@
> case type
> when ICMP_ECHOREPLY
> if data.length >= 28
> - pid, seq = data[24, 4].unpack('n3')
> + pid, rcv_seq = data[24, 4].unpack('n3')
> end
> else
> if data.length > 56
> - pid, seq = data[52, 4].unpack('n3')
> + pid, rcv_seq = data[52, 4].unpack('n3')
> end
> end
>
> - if pid == @pid && seq == @seq && type == ICMP_ECHOREPLY
> + if pid == @pid && rcv_seq == seq && type == ICMP_ECHOREPLY
> bool = true
> end
> }
>
>


Jean-François Trân

3/16/2008 5:05:00 PM

0

2008/3/16, Laurent Julliard <laurent@moldus.org>:

> Your recommendation is to turn the sequence number into a class
> variable so shouldn't the sequence number really be named @@seq
> rather than @seq if we want the sequence number to be unique across
> multiple instances of ICMP objects?

If I am correct, Joel is using a ICMP class instance variable instead
of a class variable. So in the patch, @seq is not a var of an ICMP
object but an instance variable of ICMP class (as an object :) ).

-- Jean-Fran=E7ois.