2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-04-10 04:13:08 +00:00
|
|
|
require 'timeout'
|
2009-11-02 18:14:57 +00:00
|
|
|
require 'thread'
|
2005-04-10 04:13:08 +00:00
|
|
|
|
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
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Initializes a response waiter instance for the supplied request
|
|
|
|
# identifier.
|
|
|
|
#
|
|
|
|
def initialize(rid, completion_routine = nil, completion_param = nil)
|
|
|
|
self.rid = rid.dup
|
|
|
|
self.response = nil
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
if (completion_routine)
|
|
|
|
self.completion_routine = completion_routine
|
|
|
|
self.completion_param = completion_param
|
|
|
|
else
|
|
|
|
self.done = false
|
|
|
|
end
|
|
|
|
end
|
2010-09-13 20:57:38 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Checks to see if this waiter instance is waiting for the supplied
|
|
|
|
# packet based on its request identifier.
|
|
|
|
#
|
|
|
|
def waiting_for?(packet)
|
|
|
|
return (packet.rid == rid)
|
|
|
|
end
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Notifies the waiter that the supplied response packet has arrived.
|
|
|
|
#
|
|
|
|
def notify(response)
|
|
|
|
self.response = response
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
if (self.completion_routine)
|
|
|
|
self.completion_routine.call(response, self.completion_param)
|
|
|
|
else
|
|
|
|
self.done = true
|
|
|
|
end
|
|
|
|
end
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
#
|
|
|
|
# Waits for a given time interval for the response packet to arrive.
|
|
|
|
# If the interval is -1 we can wait forever.
|
|
|
|
#
|
|
|
|
def wait(interval)
|
|
|
|
if( interval and interval == -1 )
|
|
|
|
while(not self.done)
|
|
|
|
::IO.select(nil, nil, nil, 0.1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Timeout.timeout(interval) {
|
|
|
|
while(not self.done)
|
|
|
|
::IO.select(nil, nil, nil, 0.1)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rescue Timeout::Error
|
|
|
|
self.response = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return self.response
|
|
|
|
end
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
attr_accessor :rid, :done, :response # :nodoc:
|
|
|
|
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
|
2009-11-02 18:14:57 +00:00
|
|
|
|