From: Daniel Berger Date: 2006-12-01T04:47:05+09:00 Subject: Re: cidr.rb: port of Perl's Net::CIDR v0.11 available Jos Backus wrote: >> r issue, and probably not worth the API breakage. >> > > It eases the transition from Perl, as in my case. If you mean something like > the following then that would be great: > > lizzy:/tmp% cat r > class Ping > def self.create(type, *args) > case type > when :foo > FooPing.new > when :bar > BarPing.new > end > end > end > > class FooPing < Ping > def ping(host) > puts "#{self.class}: pinging #{host}" > end > end > > class BarPing < Ping > def ping(host) > puts "#{self.class}: pinging #{host}" > end > end > > pinger = Ping.create(:bar) > pinger.ping("myhost") > lizzy:/tmp% ruby r > BarPing: pinging myhost > lizzy:/tmp% > > Let me know what I can do to help move this along. > > Well, you could start by porting the ping_icmp() function from Ping.pm to Ruby. I started working on it but I got stuck and I need to work on other things. Below is what I've got so far: require 'net/ping' module Net class Ping::ICMP < Ping ICMP_ECHOREPLY = 0 ICMP_ECHO = 8 ICMP_SUBCODE = 0 ICMP_FLAGS = 0 ICMP_PORT = 0 def initialize(*args) super(args) raise 'requires root privileges' if Process.euid > 0 # unsure @seq = 0 @data_size = 0 @data = "" end def checksum(msg) length = msg.length num_short = length / 2 check = 0 msg.unpack("n#{num_short}").each do |short| check += short end if length % 2 > 0 check += msg[length-1, 1].unpack('C') << 8 end check = (check >> 16) + (check & 0xffff) return (~((check >> 16) + check) & 0xffff) end def ping socket = Socket.new( Socket::PF_INET, Socket::SOCK_RAW, Socket::IPPROTO_ICMP ) @seq = (@seq + 1) % 65536 checksum = 0 pstring = "C2 n3 A" << @data_size.to_s pid = Process.pid msg = [ICMP_ECHO, ICMP_SUBCODE, checksum, pid, @seq, @data].pack(pstring) # TODO: finish end end end Regards, Dan