net-ping 1.7.4-universal-mingw32 → 1.7.5-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/lib/net/ping/tcp.rb CHANGED
@@ -1,102 +1,107 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
-
3
- # The Net module serves as a namespace only.
4
- module Net
5
-
6
- # With a TCP ping simply try to open a connection. If we are successful,
7
- # assume success. In either case close the connection to be polite.
8
- #
9
- class Ping::TCP < Ping
10
- @@service_check = false
11
-
12
- # Returns whether or not Errno::ECONNREFUSED is considered a successful
13
- # ping. The default is false.
14
- #
15
- def self.service_check
16
- @@service_check
17
- end
18
-
19
- # Sets whether or not an Errno::ECONNREFUSED should be considered a
20
- # successful ping.
21
- #
22
- def self.service_check=(bool)
23
- unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
24
- raise ArgumentError, 'argument must be true or false'
25
- end
26
- @@service_check = bool
27
- end
28
-
29
- # This method attempts to ping a host and port using a TCPSocket with
30
- # the host, port and timeout values passed in the constructor. Returns
31
- # true if successful, or false otherwise.
32
- #
33
- # Note that, by default, an Errno::ECONNREFUSED return result will be
34
- # considered a failed ping. See the documentation for the
35
- # Ping::TCP.service_check= method if you wish to change this behavior.
36
- #
37
- def ping(host=@host)
38
- super(host)
39
-
40
- bool = false
41
- start_time = Time.now
42
-
43
- # Failure here most likely means bad host, so just bail.
44
- begin
45
- addr = Socket.getaddrinfo(host, port)
46
- rescue SocketError => err
47
- @exception = err
48
- return false
49
- end
50
-
51
- begin
52
- # Where addr[0][0] is likely AF_INET.
53
- sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
54
-
55
- # This may not be entirely necessary
56
- sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
57
-
58
- begin
59
- # Where addr[0][3] is an IP address
60
- sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
61
- rescue Errno::EINPROGRESS
62
- # No-op, continue below
63
- rescue Exception => err
64
- # Something has gone horribly wrong
65
- @exception = err
66
- return false
67
- end
68
-
69
- resp = IO.select(nil, [sock], nil, timeout)
70
-
71
- # Assume ECONNREFUSED at this point
72
- if resp.nil?
73
- if @@service_check
74
- bool = true
75
- else
76
- @exception = Errno::ECONNREFUSED
77
- end
78
- else
79
- bool = true
80
- end
81
- ensure
82
- sock.close if sock
83
- end
84
-
85
- # There is no duration if the ping failed
86
- @duration = Time.now - start_time if bool
87
-
88
- bool
89
- end
90
-
91
- alias ping? ping
92
- alias pingecho ping
93
-
94
- # Class method aliases. DEPRECATED.
95
- class << self
96
- alias econnrefused service_check
97
- alias econnrefused= service_check=
98
- alias ecr service_check
99
- alias ecr= service_check=
100
- end
101
- end
102
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+
3
+ # The Net module serves as a namespace only.
4
+ module Net
5
+
6
+ # With a TCP ping simply try to open a connection. If we are successful,
7
+ # assume success. In either case close the connection to be polite.
8
+ #
9
+ class Ping::TCP < Ping
10
+ @@service_check = false
11
+
12
+ # Returns whether or not Errno::ECONNREFUSED is considered a successful
13
+ # ping. The default is false.
14
+ #
15
+ def self.service_check
16
+ @@service_check
17
+ end
18
+
19
+ # Sets whether or not an Errno::ECONNREFUSED should be considered a
20
+ # successful ping.
21
+ #
22
+ def self.service_check=(bool)
23
+ unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
24
+ raise ArgumentError, 'argument must be true or false'
25
+ end
26
+ @@service_check = bool
27
+ end
28
+
29
+ # This method attempts to ping a host and port using a TCPSocket with
30
+ # the host, port and timeout values passed in the constructor. Returns
31
+ # true if successful, or false otherwise.
32
+ #
33
+ # Note that, by default, an Errno::ECONNREFUSED return result will be
34
+ # considered a failed ping. See the documentation for the
35
+ # Ping::TCP.service_check= method if you wish to change this behavior.
36
+ #
37
+ def ping(host=@host)
38
+ super(host)
39
+
40
+ bool = false
41
+ start_time = Time.now
42
+
43
+ # Failure here most likely means bad host, so just bail.
44
+ begin
45
+ addr = Socket.getaddrinfo(host, port)
46
+ rescue SocketError => err
47
+ @exception = err
48
+ return false
49
+ end
50
+
51
+ begin
52
+ # Where addr[0][0] is likely AF_INET.
53
+ sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
54
+
55
+ # This may not be entirely necessary
56
+ sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
57
+
58
+ begin
59
+ # Where addr[0][3] is an IP address
60
+ sock.connect_nonblock(Socket.pack_sockaddr_in(port, addr[0][3]))
61
+ rescue Errno::EINPROGRESS
62
+ # No-op, continue below
63
+ rescue Exception => err
64
+ # Something has gone horribly wrong
65
+ @exception = err
66
+ return false
67
+ end
68
+
69
+ resp = IO.select(nil, [sock], nil, timeout)
70
+
71
+ if @@service_check
72
+ bool = true
73
+ elsif resp.nil?
74
+ # Assume ECONNREFUSED at this point
75
+ @exception = Errno::ECONNREFUSED
76
+ else
77
+ sockopt = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR)
78
+
79
+ # Check to see if ECONNREFUSED actually occurred.
80
+ if sockopt.int == Errno::ECONNREFUSED::Errno
81
+ @exception = Errno::ECONNREFUSED
82
+ else
83
+ bool = true
84
+ end
85
+ end
86
+ ensure
87
+ sock.close if sock
88
+ end
89
+
90
+ # There is no duration if the ping failed
91
+ @duration = Time.now - start_time if bool
92
+
93
+ bool
94
+ end
95
+
96
+ alias ping? ping
97
+ alias pingecho ping
98
+
99
+ # Class method aliases. DEPRECATED.
100
+ class << self
101
+ alias econnrefused service_check
102
+ alias econnrefused= service_check=
103
+ alias ecr service_check
104
+ alias ecr= service_check=
105
+ end
106
+ end
107
+ end
data/lib/net/ping/udp.rb CHANGED
@@ -1,119 +1,119 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
-
3
- # The Net module serves as a namespace only.
4
- module Net
5
-
6
- # The Ping::UDP class encapsulates methods for UDP pings.
7
- class Ping::UDP < Ping
8
- @@service_check = true
9
-
10
- # Returns whether or not the connect behavior should enforce remote
11
- # service availability as well as reachability. The default is true.
12
- #
13
- def self.service_check
14
- @@service_check
15
- end
16
-
17
- # Set whether or not the connect behavior should enforce remote
18
- # service availability as well as reachability. If set to false
19
- # then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
20
- # a successful ping, meaning no actual data handshaking is required.
21
- # By default, if either of those errors occurs it is considered a failed
22
- # ping.
23
- #
24
- def self.service_check=(bool)
25
- unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
26
- raise ArgumentError, 'argument must be true or false'
27
- end
28
- @@service_check = bool
29
- end
30
-
31
- # The maximum data size that can be sent in a UDP ping.
32
- MAX_DATA = 64
33
-
34
- # The data to send to the remote host. By default this is 'ping'.
35
- # This should be MAX_DATA size characters or less.
36
- #
37
- attr_reader :data
38
-
39
- # Creates and returns a new Ping::UDP object. This is effectively
40
- # identical to its superclass constructor.
41
- #
42
- def initialize(host=nil, port=nil, timeout=5)
43
- @data = 'ping'
44
-
45
- super(host, port, timeout)
46
-
47
- @bind_host = nil
48
- @bind_port = nil
49
- end
50
-
51
- # Sets the data string sent to the remote host. This value cannot have
52
- # a size greater than MAX_DATA.
53
- #
54
- def data=(string)
55
- if string.size > MAX_DATA
56
- err = "cannot set data string larger than #{MAX_DATA} characters"
57
- raise ArgumentError, err
58
- end
59
-
60
- @data = string
61
- end
62
-
63
- # Associates the local end of the UDP connection with the given +host+
64
- # and +port+. This is essentially a wrapper for UDPSocket#bind.
65
- #
66
- def bind(host, port)
67
- @bind_host = host
68
- @bind_port = port
69
- end
70
-
71
- # Sends a simple text string to the host and checks the return string. If
72
- # the string sent and the string returned are a match then the ping was
73
- # successful and true is returned. Otherwise, false is returned.
74
- #
75
- def ping(host = @host)
76
- super(host)
77
-
78
- bool = false
79
- udp = UDPSocket.open
80
- array = []
81
-
82
- if @bind_host
83
- udp.bind(@bind_host, @bind_port)
84
- end
85
-
86
- start_time = Time.now
87
-
88
- begin
89
- Timeout.timeout(@timeout){
90
- udp.connect(host, @port)
91
- udp.send(@data, 0)
92
- array = udp.recvfrom(MAX_DATA)
93
- }
94
- rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
95
- if @@service_check
96
- @exception = err
97
- else
98
- bool = true
99
- end
100
- rescue Exception => err
101
- @exception = err
102
- else
103
- if array[0] == @data
104
- bool = true
105
- end
106
- ensure
107
- udp.close if udp
108
- end
109
-
110
- # There is no duration if the ping failed
111
- @duration = Time.now - start_time if bool
112
-
113
- bool
114
- end
115
-
116
- alias ping? ping
117
- alias pingecho ping
118
- end
119
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+
3
+ # The Net module serves as a namespace only.
4
+ module Net
5
+
6
+ # The Ping::UDP class encapsulates methods for UDP pings.
7
+ class Ping::UDP < Ping
8
+ @@service_check = true
9
+
10
+ # Returns whether or not the connect behavior should enforce remote
11
+ # service availability as well as reachability. The default is true.
12
+ #
13
+ def self.service_check
14
+ @@service_check
15
+ end
16
+
17
+ # Set whether or not the connect behavior should enforce remote
18
+ # service availability as well as reachability. If set to false
19
+ # then Errno::ECONNREFUSED or Errno::ECONNRESET will be considered
20
+ # a successful ping, meaning no actual data handshaking is required.
21
+ # By default, if either of those errors occurs it is considered a failed
22
+ # ping.
23
+ #
24
+ def self.service_check=(bool)
25
+ unless bool.kind_of?(TrueClass) || bool.kind_of?(FalseClass)
26
+ raise ArgumentError, 'argument must be true or false'
27
+ end
28
+ @@service_check = bool
29
+ end
30
+
31
+ # The maximum data size that can be sent in a UDP ping.
32
+ MAX_DATA = 64
33
+
34
+ # The data to send to the remote host. By default this is 'ping'.
35
+ # This should be MAX_DATA size characters or less.
36
+ #
37
+ attr_reader :data
38
+
39
+ # Creates and returns a new Ping::UDP object. This is effectively
40
+ # identical to its superclass constructor.
41
+ #
42
+ def initialize(host=nil, port=nil, timeout=5)
43
+ @data = 'ping'
44
+
45
+ super(host, port, timeout)
46
+
47
+ @bind_host = nil
48
+ @bind_port = nil
49
+ end
50
+
51
+ # Sets the data string sent to the remote host. This value cannot have
52
+ # a size greater than MAX_DATA.
53
+ #
54
+ def data=(string)
55
+ if string.size > MAX_DATA
56
+ err = "cannot set data string larger than #{MAX_DATA} characters"
57
+ raise ArgumentError, err
58
+ end
59
+
60
+ @data = string
61
+ end
62
+
63
+ # Associates the local end of the UDP connection with the given +host+
64
+ # and +port+. This is essentially a wrapper for UDPSocket#bind.
65
+ #
66
+ def bind(host, port)
67
+ @bind_host = host
68
+ @bind_port = port
69
+ end
70
+
71
+ # Sends a simple text string to the host and checks the return string. If
72
+ # the string sent and the string returned are a match then the ping was
73
+ # successful and true is returned. Otherwise, false is returned.
74
+ #
75
+ def ping(host = @host)
76
+ super(host)
77
+
78
+ bool = false
79
+ udp = UDPSocket.open
80
+ array = []
81
+
82
+ if @bind_host
83
+ udp.bind(@bind_host, @bind_port)
84
+ end
85
+
86
+ start_time = Time.now
87
+
88
+ begin
89
+ Timeout.timeout(@timeout){
90
+ udp.connect(host, @port)
91
+ udp.send(@data, 0)
92
+ array = udp.recvfrom(MAX_DATA)
93
+ }
94
+ rescue Errno::ECONNREFUSED, Errno::ECONNRESET => err
95
+ if @@service_check
96
+ @exception = err
97
+ else
98
+ bool = true
99
+ end
100
+ rescue Exception => err
101
+ @exception = err
102
+ else
103
+ if array[0] == @data
104
+ bool = true
105
+ end
106
+ ensure
107
+ udp.close if udp
108
+ end
109
+
110
+ # There is no duration if the ping failed
111
+ @duration = Time.now - start_time if bool
112
+
113
+ bool
114
+ end
115
+
116
+ alias ping? ping
117
+ alias pingecho ping
118
+ end
119
+ end
data/lib/net/ping/wmi.rb CHANGED
@@ -1,118 +1,118 @@
1
- require File.join(File.dirname(__FILE__), 'ping')
2
- require 'win32ole'
3
-
4
- # The Net module serves as a namespace only.
5
- #
6
- module Net
7
-
8
- # The Ping::WMI class encapsulates the Win32_PingStatus WMI class for
9
- # MS Windows.
10
- #
11
- class Ping::WMI < Ping
12
-
13
- PingStatus = Struct.new(
14
- 'PingStatus',
15
- :address,
16
- :buffer_size,
17
- :no_fragmentation,
18
- :primary_address_resolution_status,
19
- :protocol_address,
20
- :protocol_address_resolved,
21
- :record_route,
22
- :reply_inconsistency,
23
- :reply_size,
24
- :resolve_address_names,
25
- :response_time,
26
- :response_time_to_live,
27
- :route_record,
28
- :route_record_resolved,
29
- :source_route,
30
- :source_route_type,
31
- :status_code,
32
- :timeout,
33
- :timestamp_record,
34
- :timestamp_record_address,
35
- :timestamp_record_address_resolved,
36
- :timestamp_route,
37
- :time_to_live,
38
- :type_of_service
39
- )
40
-
41
- # Unlike the ping method for other Ping subclasses, this version returns
42
- # a PingStatus struct which contains various bits of information about
43
- # the results of the ping itself, such as response time.
44
- #
45
- # In addition, this version allows you to pass certain options that are
46
- # then passed on to the underlying WQL query. See the MSDN documentation
47
- # on Win32_PingStatus for details.
48
- #
49
- # Examples:
50
- #
51
- # # Ping with no options
52
- # Ping::WMI.ping('www.perl.com')
53
- #
54
- # # Ping with options
55
- # Ping::WMI.ping('www.perl.com', :BufferSize => 64, :NoFragmentation => true)
56
- #--
57
- # The PingStatus struct is a wrapper for the Win32_PingStatus WMI class.
58
- #
59
- def ping(host = @host, options = {})
60
- super(host)
61
-
62
- lhost = Socket.gethostname
63
-
64
- cs = "winmgmts:{impersonationLevel=impersonate}!//#{lhost}/root/cimv2"
65
- wmi = WIN32OLE.connect(cs)
66
-
67
- query = "select * from win32_pingstatus where address = '#{host}'"
68
-
69
- unless options.empty?
70
- options.each{ |key, value|
71
- if value.is_a?(String)
72
- query << " and #{key} = '#{value}'"
73
- else
74
- query << " and #{key} = #{value}"
75
- end
76
- }
77
- end
78
-
79
- status = Struct::PingStatus.new
80
-
81
- wmi.execquery(query).each{ |obj|
82
- status.address = obj.Address
83
- status.buffer_size = obj.BufferSize
84
- status.no_fragmentation = obj.NoFragmentation
85
- status.primary_address_resolution_status = obj.PrimaryAddressResolutionStatus
86
- status.protocol_address = obj.ProtocolAddress
87
- status.protocol_address_resolved = obj.ProtocolAddressResolved
88
- status.record_route = obj.RecordRoute
89
- status.reply_inconsistency = obj.ReplyInconsistency
90
- status.reply_size = obj.ReplySize
91
- status.resolve_address_names = obj.ResolveAddressNames
92
- status.response_time = obj.ResponseTime
93
- status.response_time_to_live = obj.ResponseTimeToLive
94
- status.route_record = obj.RouteRecord
95
- status.route_record_resolved = obj.RouteRecordResolved
96
- status.source_route = obj.SourceRoute
97
- status.source_route_type = obj.SourceRouteType
98
- status.status_code = obj.StatusCode
99
- status.timeout = obj.Timeout
100
- status.timestamp_record = obj.TimeStampRecord
101
- status.timestamp_record_address = obj.TimeStampRecordAddress
102
- status.timestamp_record_address_resolved = obj.TimeStampRecordAddressResolved
103
- status.timestamp_route = obj.TimeStampRoute
104
- status.time_to_live = obj.TimeToLive
105
- status.type_of_service = obj.TypeOfService
106
- }
107
-
108
- status.freeze # Read-only data
109
- end
110
-
111
- # Unlike Net::Ping::WMI#ping, this method returns true or false to
112
- # indicate whether or not the ping was successful.
113
- #
114
- def ping?(host = @host, options = {})
115
- ping(host, options).status_code == 0
116
- end
117
- end
118
- end
1
+ require File.join(File.dirname(__FILE__), 'ping')
2
+ require 'win32ole'
3
+
4
+ # The Net module serves as a namespace only.
5
+ #
6
+ module Net
7
+
8
+ # The Ping::WMI class encapsulates the Win32_PingStatus WMI class for
9
+ # MS Windows.
10
+ #
11
+ class Ping::WMI < Ping
12
+
13
+ PingStatus = Struct.new(
14
+ 'PingStatus',
15
+ :address,
16
+ :buffer_size,
17
+ :no_fragmentation,
18
+ :primary_address_resolution_status,
19
+ :protocol_address,
20
+ :protocol_address_resolved,
21
+ :record_route,
22
+ :reply_inconsistency,
23
+ :reply_size,
24
+ :resolve_address_names,
25
+ :response_time,
26
+ :response_time_to_live,
27
+ :route_record,
28
+ :route_record_resolved,
29
+ :source_route,
30
+ :source_route_type,
31
+ :status_code,
32
+ :timeout,
33
+ :timestamp_record,
34
+ :timestamp_record_address,
35
+ :timestamp_record_address_resolved,
36
+ :timestamp_route,
37
+ :time_to_live,
38
+ :type_of_service
39
+ )
40
+
41
+ # Unlike the ping method for other Ping subclasses, this version returns
42
+ # a PingStatus struct which contains various bits of information about
43
+ # the results of the ping itself, such as response time.
44
+ #
45
+ # In addition, this version allows you to pass certain options that are
46
+ # then passed on to the underlying WQL query. See the MSDN documentation
47
+ # on Win32_PingStatus for details.
48
+ #
49
+ # Examples:
50
+ #
51
+ # # Ping with no options
52
+ # Ping::WMI.ping('www.perl.com')
53
+ #
54
+ # # Ping with options
55
+ # Ping::WMI.ping('www.perl.com', :BufferSize => 64, :NoFragmentation => true)
56
+ #--
57
+ # The PingStatus struct is a wrapper for the Win32_PingStatus WMI class.
58
+ #
59
+ def ping(host = @host, options = {})
60
+ super(host)
61
+
62
+ lhost = Socket.gethostname
63
+
64
+ cs = "winmgmts:{impersonationLevel=impersonate}!//#{lhost}/root/cimv2"
65
+ wmi = WIN32OLE.connect(cs)
66
+
67
+ query = "select * from win32_pingstatus where address = '#{host}'"
68
+
69
+ unless options.empty?
70
+ options.each{ |key, value|
71
+ if value.is_a?(String)
72
+ query << " and #{key} = '#{value}'"
73
+ else
74
+ query << " and #{key} = #{value}"
75
+ end
76
+ }
77
+ end
78
+
79
+ status = Struct::PingStatus.new
80
+
81
+ wmi.execquery(query).each{ |obj|
82
+ status.address = obj.Address
83
+ status.buffer_size = obj.BufferSize
84
+ status.no_fragmentation = obj.NoFragmentation
85
+ status.primary_address_resolution_status = obj.PrimaryAddressResolutionStatus
86
+ status.protocol_address = obj.ProtocolAddress
87
+ status.protocol_address_resolved = obj.ProtocolAddressResolved
88
+ status.record_route = obj.RecordRoute
89
+ status.reply_inconsistency = obj.ReplyInconsistency
90
+ status.reply_size = obj.ReplySize
91
+ status.resolve_address_names = obj.ResolveAddressNames
92
+ status.response_time = obj.ResponseTime
93
+ status.response_time_to_live = obj.ResponseTimeToLive
94
+ status.route_record = obj.RouteRecord
95
+ status.route_record_resolved = obj.RouteRecordResolved
96
+ status.source_route = obj.SourceRoute
97
+ status.source_route_type = obj.SourceRouteType
98
+ status.status_code = obj.StatusCode
99
+ status.timeout = obj.Timeout
100
+ status.timestamp_record = obj.TimeStampRecord
101
+ status.timestamp_record_address = obj.TimeStampRecordAddress
102
+ status.timestamp_record_address_resolved = obj.TimeStampRecordAddressResolved
103
+ status.timestamp_route = obj.TimeStampRoute
104
+ status.time_to_live = obj.TimeToLive
105
+ status.type_of_service = obj.TypeOfService
106
+ }
107
+
108
+ status.freeze # Read-only data
109
+ end
110
+
111
+ # Unlike Net::Ping::WMI#ping, this method returns true or false to
112
+ # indicate whether or not the ping was successful.
113
+ #
114
+ def ping?(host = @host, options = {})
115
+ ping(host, options).status_code == 0
116
+ end
117
+ end
118
+ end