Forum: Ruby How to make net-ping thread safe?

Ea48692d5a74b9f73b507ec5aa764dcb?d=identicon&s=25 Laurent Julliard (Guest)
on 2008-03-15 18:29
(Received via mailing list)
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/fidE6256BC790B8AD197544...

(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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-03-15 20:54
(Received via mailing list)
Laurent Julliard wrote:
>
> Here is also a simplified version of my own code:
> WORK_STATIONS.each do |ip|
> 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/html/rfc792:

       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/ping.html uses PID.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-03-15 21:14
(Received via mailing list)
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
              }
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-03-15 21:22
(Received via mailing list)
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.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2008-03-15 22:34
(Received via mailing list)
On Mar 15, 11:28 am, Laurent Julliard <laur...@moldus.org> wrote:
>
> 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
Ea48692d5a74b9f73b507ec5aa764dcb?d=identicon&s=25 Laurent Julliard (Guest)
on 2008-03-16 09:12
(Received via mailing list)
Joel VanderWerf wrote:
> 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
A99870c1391c39da2089649745965bda?d=identicon&s=25 Jean-François Trân (Guest)
on 2008-03-16 18:05
(Received via mailing list)
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çois.
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (djberg96)
on 2009-08-13 11:15
(Received via mailing list)
Laurent Julliard wrote:
>
> Here is also a simplified version of my own code:
> WORK_STATIONS.each do |ip|
> threads.each { |th| th.join }
Very late reply here.

Odd. When I ran nearly identical code it worked as expected. However,
when applied Joel's patch it didn't work right - it reported all hosts
as off. Not sure why.

That was on OS X 10.4.9 with Ruby 1.8.6-368 btw.

Regards,

Dan
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-08-13 22:42
(Received via mailing list)
Daniel Berger wrote:
...
> Very late reply here.

Hi, Daniel,

Found the original and dusted it off...

http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/rub...

> Odd. When I ran nearly identical code it worked as expected. However,
> when applied Joel's patch it didn't work right - it reported all hosts
> as off. Not sure why.

Will take a look on linux first. (This is not code I've used, but I'm
curious...)

> That was on OS X 10.4.9 with Ruby 1.8.6-368 btw.

I'll try there too.

What were the target hosts? OS X, too?
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (djberg96)
on 2009-08-14 00:02
(Received via mailing list)
On Aug 13, 2:41 pm, Joel VanderWerf <vj...@path.berkeley.edu> wrote:
> http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/rub...?...
> I'll try there too.
>
> What were the target hosts? OS X, too?

Yeah, I just ran it against 192.168.0.1 (exists), 192.168.0.2
(exists), and 192.168.0.3 (does not exist).

Regards,

Dan
B7dcb78b66234d8573a368b0692f4c36?d=identicon&s=25 Leo Stern (lsternlicht)
on 2009-11-15 04:24
Joel VanderWerf wrote:
> 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'
>



This does not work.
B7dcb78b66234d8573a368b0692f4c36?d=identicon&s=25 Leo Stern (lsternlicht)
on 2009-11-15 04:36
>> dog = PingerController.new
=> #<PingerController:0x10232dc20>
>> dog.pingHostThreaded
Monitoring 173.45.228.218...Monitoring 173.45.235.145...Monitoring
192.168.1.3...
Monitoring 127.0.0.1...
Monitoring 24.121.214.12...


173.45.235.145 is off173.45.228.218 is off192.168.1.3 is off127.0.0.1 is
off


24.121.214.12 is off

173.45.228.218 is off24.121.214.12 is off127.0.0.1 is off192.168.1.3 is
off173.45.235.145 is off




24.121.214.12 is off127.0.0.1 is off192.168.1.3 is off173.45.235.145 is
off173.45.228.218 is off




127.0.0.1 is off192.168.1.3 is off173.45.235.145 is off173.45.228.218 is
off24.121.214.12 is off




173.45.228.218 is off24.121.214.12 is off127.0.0.1 is off192.168.1.3 is
off173.45.235.145 is off




^X^CIRB::Abort: abort then interrupt!!
  from /usr/local/lib/ruby/1.8/irb.rb:81:in `irb_abort'
  from /usr/local/lib/ruby/1.8/irb.rb:247:in `signal_handle'
  from /usr/local/lib/ruby/1.8/irb.rb:66:in `start'
  from
/Users/lsternlicht/pinger/app/controllers/pinger_controller.rb:32:in
`call'
  from
/Users/lsternlicht/pinger/app/controllers/pinger_controller.rb:32:in
`join'
  from
/Users/lsternlicht/pinger/app/controllers/pinger_controller.rb:32:in
`pingHostThreaded'
  from
/Users/lsternlicht/pinger/app/controllers/pinger_controller.rb:32:in
`each'
  from
/Users/lsternlicht/pinger/app/controllers/pinger_controller.rb:32:in
`pingHostThreaded'
  from (irb):2
>> Net::Ping::ICMP.new('173.45.235.145')
=> #<Net::Ping::ICMP:0x102301148 @duration=nil, @timeout=5, @port=nil,
@host="173.45.235.145", @bind_port=0, @warning=nil, @pid=26254,
@exception=nil,
@data="\000\001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037
!\"\#$%&'()*+,-./012345678", @bind_host=nil, @data_size=56>
>> Net::Ping::ICMP.new('173.45.228.218')
24.121.214.12 is off127.0.0.1 is off192.168.1.3 is off173.45.235.145 is
off173.45.228.218 is off=> #<Net::Ping::ICMP:0x1022c2f10 @duration=nil,
@timeout=5, @port=nil, @host="173.45.228.218", @bind_port=0,
@warning=nil, @pid=26254, @exception=nil,
@data="\000\001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037
!\"\#$%&'()*+,-./012345678", @bind_host=nil, @data_size=56>
>>


anyone have any ideas about the last object instantiation result?
B7dcb78b66234d8573a368b0692f4c36?d=identicon&s=25 Leo Stern (lsternlicht)
on 2009-11-16 03:50
The gem is not thread-safe. I am using ruby 1.8 and Mac os X 10.6. I
think Joel has the right idea but I got the same "off" result as Daniel.
B7dcb78b66234d8573a368b0692f4c36?d=identicon&s=25 Leo Stern (lsternlicht)
on 2009-12-04 03:10
Jean-François Trân wrote:
> 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�ois.

Why?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-12-04 11:26
(Received via mailing list)
2009/12/4 Leo Stern <lsternlicht@gmail.com>:
>> object but an instance variable of ICMP class (as an object :) ).
>
> Why?

Because @@class variables are broken.  You can find numerous threads
about the topic here.

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.