2005-06-02 07:52:17 +00:00
|
|
|
require 'Rex/Socket'
|
|
|
|
require 'Rex/Socket/Tcp'
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Local
|
|
|
|
# -----
|
|
|
|
#
|
|
|
|
# Local communication class factory.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Rex::Socket::Comm::Local
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates an instance of a socket using the supplied parameters.
|
|
|
|
#
|
|
|
|
def self.create(param)
|
|
|
|
case param.proto
|
|
|
|
when 'tcp'
|
2005-06-03 04:51:51 +00:00
|
|
|
return create_by_type(param, ::Socket::SOCK_STREAM, ::Socket::IPPROTO_TCP)
|
|
|
|
when 'udp'
|
|
|
|
return create_by_type(param, ::Socket::SOCK_DGRAM, ::Socket::IPPROTO_UDP)
|
2005-06-02 07:52:17 +00:00
|
|
|
else
|
|
|
|
raise RuntimeError, "Unsupported protocol: #{param.proto}", caller # FIXME EXCEPTION
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Creates a TCP socket
|
|
|
|
#
|
2005-06-03 04:51:51 +00:00
|
|
|
def self.create_by_type(param, type, proto = 0)
|
2005-06-02 07:52:17 +00:00
|
|
|
# Create the raw TCP socket
|
2005-06-03 04:51:51 +00:00
|
|
|
sock = ::Socket.new(::Socket::AF_INET, type, proto)
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
# Bind to a given local address and/or port if they are supplied
|
|
|
|
if (param.localhost || param.localport)
|
2005-06-03 04:51:51 +00:00
|
|
|
sock.bind(Rex::Socket.to_sockaddr(param.localhost, param.localport))
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If a server TCP instance is being created...
|
|
|
|
if (param.server?)
|
|
|
|
sock.listen(32)
|
|
|
|
|
|
|
|
return sock if (param.bare?)
|
|
|
|
|
|
|
|
return Rex::Socket::TcpServer.new(sock)
|
|
|
|
# Otherwise, if we're creating a client...
|
|
|
|
else
|
2005-06-03 04:51:51 +00:00
|
|
|
# If we were supplied with host information
|
|
|
|
if (param.peerhost)
|
|
|
|
sock.connect(Rex::Socket.to_sockaddr(param.peerhost, param.peerport))
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
return sock if (param.bare?)
|
|
|
|
|
|
|
|
return Rex::Socket::Tcp.new(sock)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|