2005-09-22 04:53:46 +00:00
|
|
|
require 'rex/sync/thread_safe'
|
2005-07-24 20:53:54 +00:00
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
module Rex
|
|
|
|
module IO
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# This mixin is an abstract representation of a streaming connection. Streams
|
|
|
|
# extend classes that must implement the following methods:
|
|
|
|
#
|
|
|
|
# syswrite(buffer)
|
|
|
|
# sysread(length)
|
|
|
|
# shutdown(how)
|
|
|
|
# close
|
|
|
|
# peerinfo
|
|
|
|
# localinfo
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
module Stream
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Abstract methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
2006-01-07 18:59:04 +00:00
|
|
|
# This method writes the supplied buffer to the stream. This method
|
|
|
|
# intelligent reduces the size of supplied buffers so that ruby doesn't get
|
|
|
|
# into a potential global thread blocking state when used on blocking
|
|
|
|
# sockets. That is, this method will send the supplied buffer in chunks
|
|
|
|
# of, at most, 32768 bytes.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
def write(buf, opts = {})
|
2010-02-06 17:59:25 +00:00
|
|
|
total_sent = 0
|
|
|
|
total_length = buf.length
|
2005-11-24 19:38:36 +00:00
|
|
|
begin
|
2010-02-06 17:59:25 +00:00
|
|
|
while( total_sent < total_length )
|
|
|
|
s = Rex::ThreadSafe.select( nil, [ fd ], nil, 0.2 )
|
|
|
|
if( s == nil || s[0] == nil )
|
|
|
|
next
|
|
|
|
end
|
|
|
|
data = buf[0, 32768]
|
|
|
|
sent = fd.syswrite( data )
|
|
|
|
if sent > 0
|
|
|
|
total_sent += sent
|
|
|
|
buf[0, sent] = ""
|
|
|
|
end
|
2006-01-07 18:59:04 +00:00
|
|
|
end
|
2008-11-11 05:39:23 +00:00
|
|
|
rescue ::IOError, ::Errno::EPIPE
|
2005-11-24 19:52:12 +00:00
|
|
|
return nil if (fd.abortive_close == true)
|
2005-11-24 19:38:36 +00:00
|
|
|
raise $!
|
|
|
|
end
|
2010-02-06 17:59:25 +00:00
|
|
|
total_sent
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method reads data of the supplied length from the stream.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
def read(length = nil, opts = {})
|
2008-09-24 04:41:51 +00:00
|
|
|
# XXX handle length being nil
|
2005-11-24 19:38:36 +00:00
|
|
|
begin
|
2010-03-10 06:43:46 +00:00
|
|
|
fd.readpartial(length)
|
2008-11-11 05:39:23 +00:00
|
|
|
rescue ::IOError, ::EOFError, ::Errno::EPIPE
|
2006-04-30 19:49:27 +00:00
|
|
|
return nil if (fd.abortive_close == true)
|
2005-11-24 19:38:36 +00:00
|
|
|
raise $!
|
|
|
|
end
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-06-03 07:13:15 +00:00
|
|
|
# Polls the stream to see if there is any read data available. Returns
|
|
|
|
# true if data is available for reading, otherwise false is returned.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
2005-06-03 07:13:15 +00:00
|
|
|
def has_read_data?(timeout = nil)
|
2005-11-24 18:50:33 +00:00
|
|
|
begin
|
2006-01-07 18:59:04 +00:00
|
|
|
if ((rv = Rex::ThreadSafe.select([ fd ], nil, nil, timeout)) and
|
2005-11-24 18:50:33 +00:00
|
|
|
(rv[0]) and
|
|
|
|
(rv[0][0] == fd))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2008-11-11 05:39:23 +00:00
|
|
|
rescue StreamClosedError, ::IOError, ::EOFError, ::Errno::EPIPE
|
2005-11-24 18:50:33 +00:00
|
|
|
# If the thing that lead to the closure was an abortive close, then
|
|
|
|
# don't raise the stream closed error.
|
|
|
|
return false if (fd.abortive_close == true)
|
|
|
|
|
|
|
|
raise $!
|
2005-09-27 05:31:48 +00:00
|
|
|
end
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method returns the selectable file descriptor, or self by default.
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def fd
|
2005-09-27 05:31:48 +00:00
|
|
|
self
|
2010-02-06 17:59:25 +00:00
|
|
|
end
|
2005-07-16 08:12:58 +00:00
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Common methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method writes the supplied buffer to the stream by calling the write
|
|
|
|
# routine.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
def <<(buf)
|
2005-06-03 22:56:36 +00:00
|
|
|
return write(buf.to_s)
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2006-01-30 16:37:41 +00:00
|
|
|
#
|
|
|
|
# This method calls get_once() to read pending data from the socket
|
|
|
|
#
|
|
|
|
def >>
|
|
|
|
get_once
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method writes to the stream, optionally timing out after a period of
|
|
|
|
# time.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
def timed_write(buf, wait = def_write_timeout, opts = {})
|
|
|
|
if (wait and wait > 0)
|
2009-11-02 18:14:57 +00:00
|
|
|
Timeout.timeout(wait) {
|
2005-09-16 07:50:04 +00:00
|
|
|
return write(buf, opts)
|
|
|
|
}
|
2005-05-31 12:56:36 +00:00
|
|
|
else
|
|
|
|
return write(buf, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method reads from the stream, optionally timing out after a period
|
|
|
|
# of time.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
def timed_read(length = nil, wait = def_read_timeout, opts = {})
|
|
|
|
if (wait and wait > 0)
|
2009-11-02 18:14:57 +00:00
|
|
|
Timeout.timeout(wait) {
|
2005-09-16 07:50:04 +00:00
|
|
|
return read(length, opts)
|
|
|
|
}
|
2005-05-31 12:56:36 +00:00
|
|
|
else
|
|
|
|
return read(length, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method writes the full contents of the supplied buffer, optionally
|
|
|
|
# with a timeout.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
|
|
|
def put(buf, opts = {})
|
2005-11-23 03:22:17 +00:00
|
|
|
return 0 if (buf == nil or buf.length == 0)
|
|
|
|
|
2006-01-28 07:12:35 +00:00
|
|
|
send_len = buf.length
|
|
|
|
send_idx = 0
|
2005-05-31 12:56:36 +00:00
|
|
|
wait = opts['Timeout'] || 0
|
|
|
|
|
|
|
|
# Keep writing until our send length drops to zero
|
2006-01-28 07:12:35 +00:00
|
|
|
while (send_idx < send_len)
|
|
|
|
curr_len = timed_write(buf[send_idx, buf.length-send_idx], wait, opts)
|
2005-11-24 19:52:12 +00:00
|
|
|
|
|
|
|
# If the write operation failed due to an IOError, then we fail.
|
|
|
|
return buf.length - send_len if (curr_len == nil)
|
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
send_len -= curr_len
|
2006-01-28 07:12:35 +00:00
|
|
|
send_idx += curr_len
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2005-11-23 03:22:17 +00:00
|
|
|
return buf.length - send_len
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
|
2009-11-02 18:14:57 +00:00
|
|
|
#
|
2005-11-16 17:56:07 +00:00
|
|
|
# This method emulates the behavior of Pex::Socket::Recv in MSF2
|
|
|
|
#
|
|
|
|
def get_once(length = -1, timeout = def_read_timeout)
|
|
|
|
|
|
|
|
if (has_read_data?(timeout) == false)
|
|
|
|
return nil
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
bsize = (length == -1) ? def_block_size : length
|
|
|
|
|
|
|
|
begin
|
|
|
|
return read(bsize)
|
|
|
|
rescue Exception
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-11-16 17:56:07 +00:00
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This method reads as much data as it can from the wire given a maximum
|
|
|
|
# timeout.
|
2005-05-31 12:56:36 +00:00
|
|
|
#
|
2005-06-04 07:51:59 +00:00
|
|
|
def get(timeout = nil, ltimeout = def_read_loop_timeout, opts = {})
|
2005-06-03 22:56:36 +00:00
|
|
|
# For those people who are used to being able to use a negative timeout!
|
2005-06-04 06:19:42 +00:00
|
|
|
if (timeout and timeout.to_i < 0)
|
2005-06-03 22:56:36 +00:00
|
|
|
timeout = nil
|
|
|
|
end
|
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
# No data in the first place? bust.
|
2005-07-16 07:32:11 +00:00
|
|
|
if (has_read_data?(timeout) == false)
|
2005-05-31 12:56:36 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
buf = ""
|
|
|
|
lps = 0
|
2005-07-24 20:53:54 +00:00
|
|
|
eof = false
|
2005-05-31 12:56:36 +00:00
|
|
|
|
|
|
|
# Keep looping until there is no more data to be gotten..
|
2005-07-16 07:32:11 +00:00
|
|
|
while (has_read_data?(ltimeout) == true)
|
2005-07-24 20:53:54 +00:00
|
|
|
# Catch EOF errors so that we can handle them properly.
|
|
|
|
begin
|
|
|
|
temp = read(def_block_size)
|
|
|
|
rescue EOFError
|
|
|
|
eof = true
|
|
|
|
end
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
# If we read zero bytes and we had data, then we've hit EOF
|
|
|
|
if (temp and temp.length == 0)
|
2005-07-24 20:53:54 +00:00
|
|
|
eof = true
|
|
|
|
end
|
|
|
|
|
|
|
|
# If we reached EOF and there are no bytes in the buffer we've been
|
|
|
|
# reading into, then throw an EOF error.
|
|
|
|
if (eof)
|
|
|
|
# If we've already read at least some data, then it's time to
|
|
|
|
# break out and let it be processed before throwing an EOFError.
|
|
|
|
if (buf.length > 0)
|
|
|
|
break
|
|
|
|
else
|
|
|
|
raise EOFError
|
|
|
|
end
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
break if (temp == nil or temp.empty? == true)
|
2005-05-31 12:56:36 +00:00
|
|
|
|
|
|
|
buf += temp
|
|
|
|
lps += 1
|
2009-11-02 18:14:57 +00:00
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
break if (lps >= def_max_loops)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the entire buffer we read in
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Defaults
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The default number of seconds to wait for a write operation to timeout.
|
|
|
|
#
|
2005-05-31 12:56:36 +00:00
|
|
|
def def_write_timeout
|
2005-11-15 05:22:13 +00:00
|
|
|
10
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The default number of seconds to wait for a read operation to timeout.
|
|
|
|
#
|
2005-05-31 12:56:36 +00:00
|
|
|
def def_read_timeout
|
2005-11-15 05:22:13 +00:00
|
|
|
10
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# The default number of seconds to wait while in a read loop after read
|
|
|
|
# data has been found.
|
|
|
|
#
|
2005-05-31 12:56:36 +00:00
|
|
|
def def_read_loop_timeout
|
2005-11-16 17:56:07 +00:00
|
|
|
0.1
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The maximum number of read loops to perform before returning to the
|
|
|
|
# caller.
|
|
|
|
#
|
2005-05-31 12:56:36 +00:00
|
|
|
def def_max_loops
|
2005-11-15 05:22:13 +00:00
|
|
|
1024
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The default block size to read in chunks from the wire.
|
|
|
|
#
|
2005-05-31 12:56:36 +00:00
|
|
|
def def_block_size
|
2005-11-15 05:22:13 +00:00
|
|
|
16384
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
2005-11-24 18:50:33 +00:00
|
|
|
#
|
|
|
|
# This flag indicates whether or not an abortive close has been issued.
|
|
|
|
#
|
|
|
|
attr_accessor :abortive_close
|
2010-03-10 06:43:46 +00:00
|
|
|
|
2005-05-31 12:56:36 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-11-11 05:11:40 +00:00
|
|
|
end end
|
2009-11-02 18:14:57 +00:00
|
|
|
|