2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/socket/tcp'
|
|
|
|
require 'rex/io/stream_server'
|
2005-06-03 07:13:15 +00:00
|
|
|
|
2005-06-03 07:32:17 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# TcpServer
|
|
|
|
# ---------
|
|
|
|
#
|
|
|
|
# This class provides methods for interacting with a TCP server. It
|
|
|
|
# implements the StreamServer IO interface.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
###
|
2005-06-03 07:13:15 +00:00
|
|
|
class Rex::Socket::TcpServer < Rex::Socket
|
|
|
|
include Rex::IO::StreamServer
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
# Creates the server using the supplied hash
|
|
|
|
#
|
|
|
|
def self.create(hash)
|
|
|
|
self.create_param(Rex::Socket::Parameters.from_hash(hash))
|
|
|
|
end
|
|
|
|
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
|
|
|
# Wrapper around the base class' creation method that automatically sets
|
|
|
|
# the parameter's protocol to TCP and sets the server flag to true
|
|
|
|
#
|
|
|
|
def self.create_param(param)
|
|
|
|
param.proto = 'tcp'
|
|
|
|
param.server = true
|
|
|
|
|
|
|
|
super(param)
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Class initialization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# StreamServer mixin implementation
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Accepts a child connection
|
|
|
|
#
|
|
|
|
def accept(opts = {})
|
2005-09-23 06:08:04 +00:00
|
|
|
t = Rex::Socket::Tcp.new(self.sock.accept[0])
|
|
|
|
|
|
|
|
if (t)
|
|
|
|
pn = t.getpeername
|
|
|
|
|
|
|
|
t.peerhost = pn[1]
|
|
|
|
t.peerport = pn[2]
|
|
|
|
end
|
|
|
|
|
|
|
|
t
|
2005-06-03 07:13:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns whether or not one or more client connections are pending
|
|
|
|
# acceptance
|
|
|
|
#
|
|
|
|
def pending_client?(timeout = nil)
|
|
|
|
timeout = timeout.to_i if (timeout)
|
|
|
|
|
2005-07-17 00:52:47 +00:00
|
|
|
return (Rex::ThreadSafe.select([ poll_fd ], nil, nil, timeout) != nil)
|
2005-06-03 07:13:15 +00:00
|
|
|
end
|
|
|
|
end
|