2005-05-31 12:56:36 +00:00
|
|
|
module Rex
|
|
|
|
module IO
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Stream
|
|
|
|
# ------
|
|
|
|
#
|
|
|
|
# This mixin is an abstract representation of a streaming connection.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Stream
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Abstract methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Set the stream to blocking or non-blocking
|
|
|
|
#
|
|
|
|
def blocking=(tf)
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Check to see if the stream is blocking or non-blocking
|
|
|
|
#
|
|
|
|
def blocking
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes data to the stream.
|
|
|
|
#
|
|
|
|
def write(buf, opts = {})
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Reads data from the stream.
|
|
|
|
#
|
|
|
|
def read(length = nil, opts = {})
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Shuts down the stream for reading, writing, or both.
|
|
|
|
#
|
|
|
|
def shutdown(how = SW_BOTH)
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Closes the stream and allows for resource cleanup
|
|
|
|
#
|
|
|
|
def close
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
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)
|
|
|
|
super
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the file descriptor that can be polled via select, if any.
|
|
|
|
#
|
|
|
|
def poll_fd
|
2005-06-03 07:13:15 +00:00
|
|
|
super
|
2005-05-31 12:56:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Common methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes data to the stream
|
|
|
|
#
|
|
|
|
def <<(buf)
|
|
|
|
return write(buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes to the stream, optionally timing out after a period of time
|
|
|
|
#
|
|
|
|
def timed_write(buf, wait = def_write_timeout, opts = {})
|
|
|
|
if (wait and wait > 0)
|
|
|
|
timeout(wait) {
|
|
|
|
return write(buf, opts)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return write(buf, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Reads from the stream, optionally timing out after a period of time
|
|
|
|
#
|
|
|
|
def timed_read(length = nil, wait = def_read_timeout, opts = {})
|
|
|
|
if (wait and wait > 0)
|
|
|
|
timeout(wait) {
|
|
|
|
return read(length, opts)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return read(length, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Write the full contents of the supplied buffer
|
|
|
|
#
|
|
|
|
def put(buf, opts = {})
|
|
|
|
send_buf = buf.dup()
|
|
|
|
send_len = send_buf.length
|
|
|
|
wait = opts['Timeout'] || 0
|
|
|
|
|
|
|
|
# Keep writing until our send length drops to zero
|
|
|
|
while (send_len > 0)
|
|
|
|
curr_len = timed_write(send_buf, wait, opts)
|
|
|
|
send_len -= curr_len
|
|
|
|
send_buf.slice!(0, curr_len)
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read as much data as possible from the pipe
|
|
|
|
#
|
|
|
|
def get(timeout = def_read_timeout, ltimeout = def_read_loop_timeout, opts = {})
|
|
|
|
# No data in the first place? bust.
|
2005-06-03 07:13:15 +00:00
|
|
|
if (!has_read_data?(timeout))
|
2005-05-31 12:56:36 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
buf = ""
|
|
|
|
lps = 0
|
|
|
|
|
|
|
|
# Keep looping until there is no more data to be gotten..
|
2005-06-03 07:13:15 +00:00
|
|
|
while (has_read_data?(ltimeout))
|
2005-06-03 05:21:49 +00:00
|
|
|
temp = read(def_block_size)
|
2005-05-31 12:56:36 +00:00
|
|
|
|
|
|
|
break if (temp.empty?)
|
|
|
|
|
|
|
|
buf += temp
|
|
|
|
lps += 1
|
|
|
|
|
|
|
|
break if (lps >= def_max_loops)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return the entire buffer we read in
|
|
|
|
return buf
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Defaults
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
def def_write_timeout
|
|
|
|
return 10
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_read_timeout
|
|
|
|
return 10
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_read_loop_timeout
|
|
|
|
return 0.5
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_max_loops
|
|
|
|
return 1024
|
|
|
|
end
|
|
|
|
|
|
|
|
def def_block_size
|
|
|
|
return 16384
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end end
|