lib/netchk/icmp.rb in netchk-0.0.1.beta vs lib/netchk/icmp.rb in netchk-0.0.1
- old
+ new
@@ -1,15 +1,17 @@
+# rubocop:disable Style/FrozenStringLiteralComment:
require 'net/ping/ping'
if File::ALT_SEPARATOR
require 'win32/security'
end
# The Net module serves as a namespace only.
module Netchk
-
- # The Net::Ping::ICMP class encapsulates an icmp ping.
+ # Modified version of Net::Ping::ICMP that does
+ # not check for root privileges and uses a DGRAM
+ # socket instead of a raw socket.
class ICMP < ::Net::Ping
ICMP_ECHOREPLY = 0 # Echo reply
ICMP_ECHO = 8 # Echo request
ICMP_SUBCODE = 0
@@ -22,27 +24,19 @@
# default size is 56.
#
attr_reader :data_size
# Creates and returns a new Ping::ICMP object. This is similar to its
- # superclass constructor, but must be created with root privileges (on
- # UNIX systems), and the port value is ignored.
- #
- def initialize(host=nil, port=nil, timeout=5)
- if File::ALT_SEPARATOR
- unless Win32::Security.elevated_security?
- raise 'requires elevated security'
- end
- end
-
+ # superclass constructor, and the port value is ignored.
+ def initialize(host = nil, port = nil, timeout = 5)
@seq = 0
@bind_port = 0
@bind_host = nil
@data_size = 56
@data = ''
- 0.upto(@data_size){ |n| @data << (n % 256).chr }
+ 0.upto(@data_size) { |n| @data << (n % 256).chr }
@ping_id = (Thread.current.object_id ^ Process.pid) & 0xffff
super(host, port, timeout)
@port = nil # This value is not used in ICMP pings.
@@ -51,11 +45,11 @@
# Sets the number of bytes sent in the ping method.
#
def data_size=(size)
@data_size = size
@data = ''
- 0.upto(size){ |n| @data << (n % 256).chr }
+ 0.upto(size) { |n| @data << (n % 256).chr }
end
# Associates the local end of the socket connection with the given
# +host+ and +port+. The default port is 0.
#
@@ -103,11 +97,11 @@
start_time = Time.now
socket.send(msg, 0, saddr) # Send the message
begin
- Timeout.timeout(@timeout){
+ Timeout.timeout(@timeout) {
while true
io_array = select([socket], nil, nil, timeout)
if io_array.nil? || io_array[0].empty?
raise Timeout::Error if io_array.nil?
@@ -147,26 +141,26 @@
@duration = Time.now - start_time if bool
end
private
- # Perform a checksum on the message. This is the sum of all the short
- # words and it folds the high order bits into the low order bits.
- #
- def checksum(msg)
- length = msg.length
- num_short = length / 2
- check = 0
+ # Perform a checksum on the message. This is the sum of all the short
+ # words and it folds the high order bits into the low order bits.
+ #
+ def checksum(msg)
+ length = msg.length
+ num_short = length / 2
+ check = 0
- msg.unpack("n#{num_short}").each do |short|
- check += short
- end
+ msg.unpack("n#{num_short}").each do |short|
+ check += short
+ end
- if length % 2 > 0
- check += msg[length-1, 1].unpack('C').first << 8
- end
+ if length % 2 > 0
+ check += msg[length - 1, 1].unpack('C').first << 8
+ end
- check = (check >> 16) + (check & 0xffff)
- return (~((check >> 16) + check) & 0xffff)
- end
+ check = (check >> 16) + (check & 0xffff)
+ return (~((check >> 16) + check) & 0xffff)
+ end
end
end