2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
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
|
2010-10-16 21:50:03 +00:00
|
|
|
block_size = 32768
|
2013-03-08 00:16:57 +00:00
|
|
|
|
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
|
2011-04-21 17:17:26 +00:00
|
|
|
data = buf[total_sent, block_size]
|
2010-09-13 14:39:17 +00:00
|
|
|
sent = fd.write_nonblock( data )
|
2010-02-06 17:59:25 +00:00
|
|
|
if sent > 0
|
|
|
|
total_sent += sent
|
|
|
|
end
|
2006-01-07 18:59:04 +00:00
|
|
|
end
|
2011-04-30 07:59:33 +00:00
|
|
|
rescue ::Errno::EAGAIN, ::Errno::EWOULDBLOCK
|
|
|
|
# Sleep for a half a second, or until we can write again
|
|
|
|
Rex::ThreadSafe.select( nil, [ fd ], nil, 0.5 )
|
|
|
|
# Decrement the block size to handle full sendQs better
|
|
|
|
block_size = 1024
|
|
|
|
# Try to write the data again
|
|
|
|
retry
|
|
|
|
rescue ::IOError, ::Errno::EPIPE
|
2011-05-03 03:59:02 +00:00
|
|
|
return nil
|
2005-11-24 19:38:36 +00:00
|
|
|
end
|
2013-03-08 00:16:57 +00:00
|
|
|
|
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 = {})
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2005-11-24 19:38:36 +00:00
|
|
|
begin
|
2013-03-08 00:16:57 +00:00
|
|
|
return fd.read_nonblock( length )
|
2011-04-30 07:59:33 +00:00
|
|
|
rescue ::Errno::EAGAIN, ::Errno::EWOULDBLOCK
|
|
|
|
# Sleep for a half a second, or until we can read again
|
|
|
|
Rex::ThreadSafe.select( [ fd ], nil, nil, 0.5 )
|
|
|
|
# Decrement the block size to handle full sendQs better
|
|
|
|
retry
|
|
|
|
rescue ::IOError, ::Errno::EPIPE
|
2011-05-03 03:59:02 +00:00
|
|
|
return nil
|
2005-11-24 19:38:36 +00:00
|
|
|
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)
|
2010-07-09 17:58:59 +00:00
|
|
|
|
|
|
|
# Allow a timeout of "0" that waits almost indefinitely for input, this
|
|
|
|
# mimics the behavior of Rex::ThreadSafe.select() and fixes some corner
|
|
|
|
# cases of unintentional no-wait timeouts.
|
|
|
|
timeout = 3600 if (timeout and timeout == 0)
|
|
|
|
|
2005-11-24 18:50:33 +00:00
|
|
|
begin
|
2010-07-01 14:49:49 +00:00
|
|
|
if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
|
|
|
|
(rv[0]) and
|
|
|
|
(rv[0][0] == fd))
|
|
|
|
true
|
2005-11-24 18:50:33 +00:00
|
|
|
else
|
2010-07-01 14:49:49 +00:00
|
|
|
false
|
2005-11-24 18:50:33 +00:00
|
|
|
end
|
2011-01-11 08:47:42 +00:00
|
|
|
rescue ::Errno::EBADF, ::Errno::ENOTSOCK
|
2011-03-23 23:15:11 +00:00
|
|
|
raise ::EOFError
|
2008-11-11 05:39:23 +00:00
|
|
|
rescue StreamClosedError, ::IOError, ::EOFError, ::Errno::EPIPE
|
2011-05-03 03:59:02 +00:00
|
|
|
# Return false if the socket is dead
|
|
|
|
return false
|
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-03-27 15:44:33 +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
|
2011-05-13 06:37:15 +00:00
|
|
|
data = read(bsize)
|
|
|
|
raise EOFError if data.nil?
|
|
|
|
data
|
2005-11-16 17:56:07 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2008-11-11 05:11:40 +00:00
|
|
|
end end
|
2009-11-02 18:14:57 +00:00
|
|
|
|