2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-04-10 04:13:08 +00:00
|
|
|
require 'timeout'
|
|
|
|
require 'thread'
|
|
|
|
|
2005-04-10 02:05:43 +00:00
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class handles waiting for a response to a given request
|
2005-11-15 05:22:13 +00:00
|
|
|
# and the subsequent response association.
|
2005-04-10 02:05:43 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
class PacketResponseWaiter
|
2005-04-12 05:37:11 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Initializes a response waiter instance for the supplied request
|
2005-11-15 05:22:13 +00:00
|
|
|
# identifier.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def initialize(rid, completion_routine = nil, completion_param = nil)
|
2005-04-15 07:03:33 +00:00
|
|
|
self.rid = rid.dup
|
2005-04-10 02:05:43 +00:00
|
|
|
self.response = nil
|
|
|
|
|
|
|
|
if (completion_routine)
|
|
|
|
self.completion_routine = completion_routine
|
|
|
|
self.completion_param = completion_param
|
|
|
|
else
|
2009-06-14 21:20:30 +00:00
|
|
|
self.done = false
|
2005-04-10 02:05:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Checks to see if this waiter instance is waiting for the supplied
|
2005-11-15 05:22:13 +00:00
|
|
|
# packet based on its request identifier.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def waiting_for?(packet)
|
|
|
|
return (packet.rid == rid)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Notifies the waiter that the supplied response packet has arrived.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def notify(response)
|
|
|
|
self.response = response
|
|
|
|
|
|
|
|
if (self.completion_routine)
|
|
|
|
self.completion_routine(self.completion_param, response)
|
|
|
|
else
|
2009-06-14 21:20:30 +00:00
|
|
|
self.done = true
|
2005-04-10 02:05:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Waits for a given time interval for the response packet to arrive.
|
|
|
|
#
|
2005-04-10 02:05:43 +00:00
|
|
|
def wait(interval)
|
2005-04-10 04:13:08 +00:00
|
|
|
begin
|
|
|
|
timeout(interval) {
|
2009-06-14 21:20:30 +00:00
|
|
|
while(not self.done)
|
|
|
|
select(nil, nil, nil, 0.1)
|
|
|
|
end
|
2005-04-10 04:13:08 +00:00
|
|
|
}
|
2007-03-01 07:47:40 +00:00
|
|
|
rescue Timeout::Error
|
2005-04-10 04:13:08 +00:00
|
|
|
self.response = nil
|
|
|
|
end
|
2005-04-10 02:05:43 +00:00
|
|
|
|
|
|
|
return self.response
|
|
|
|
end
|
|
|
|
|
2009-06-14 21:20:30 +00:00
|
|
|
attr_accessor :rid, :done, :response # :nodoc:
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :completion_routine, :completion_param # :nodoc:
|
2005-04-10 02:05:43 +00:00
|
|
|
end
|
|
|
|
|
2009-06-14 21:20:30 +00:00
|
|
|
end; end; end
|