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
|
2015-08-15 13:10:14 +00:00
|
|
|
self.mutex = Mutex.new
|
|
|
|
self.cond = ConditionVariable.new
|
2013-08-30 21:28:33 +00:00
|
|
|
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)
|
|
|
|
if (self.completion_routine)
|
2015-11-16 18:40:13 +00:00
|
|
|
self.response = response
|
2013-08-30 21:28:33 +00:00
|
|
|
self.completion_routine.call(response, self.completion_param)
|
|
|
|
else
|
2015-11-16 18:40:13 +00:00
|
|
|
self.mutex.synchronize do
|
|
|
|
self.response = response
|
2015-08-15 13:10:14 +00:00
|
|
|
self.cond.signal
|
2015-11-16 18:40:13 +00:00
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
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)
|
2015-08-15 13:10:14 +00:00
|
|
|
interval = nil if interval and interval == -1
|
2015-11-16 18:40:13 +00:00
|
|
|
self.mutex.synchronize do
|
|
|
|
if self.response.nil?
|
|
|
|
self.cond.wait(self.mutex, interval)
|
|
|
|
end
|
|
|
|
end
|
2013-08-30 21:28:33 +00:00
|
|
|
return self.response
|
|
|
|
end
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2015-08-15 13:10:14 +00:00
|
|
|
attr_accessor :rid, :mutex, :cond, :response # :nodoc:
|
2013-08-30 21:28:33 +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
|
2009-11-02 18:14:57 +00:00
|
|
|
|