2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/socket/tcp'
|
|
|
|
require 'rex/socket/ssl_tcp'
|
|
|
|
require 'rex/socket/udp'
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Local communication class factory.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Rex::Socket::Comm::Local
|
|
|
|
|
2005-09-30 05:59:44 +00:00
|
|
|
include Rex::Socket::Comm
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
#
|
|
|
|
# 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
|
2005-06-04 20:38:49 +00:00
|
|
|
raise Rex::UnsupportedProtocol.new(param.proto), caller
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-06-03 07:32:17 +00:00
|
|
|
# Creates a socket
|
2005-06-02 07:52:17 +00:00
|
|
|
#
|
2005-06-03 04:51:51 +00:00
|
|
|
def self.create_by_type(param, type, proto = 0)
|
2005-06-03 07:32:17 +00:00
|
|
|
# Create the 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-04 20:38:49 +00:00
|
|
|
begin
|
|
|
|
if (param.server?)
|
|
|
|
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, 1)
|
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
|
|
|
|
|
2005-06-04 20:38:49 +00:00
|
|
|
sock.bind(Rex::Socket.to_sockaddr(param.localhost, param.localport))
|
|
|
|
rescue Errno::EADDRINUSE
|
|
|
|
raise Rex::AddressInUse.new(param.localhost, param.localport), caller
|
|
|
|
end
|
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?)
|
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
sock.extend(Rex::Socket::TcpServer)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
|
|
|
|
return sock
|
2005-06-02 07:52:17 +00:00
|
|
|
# 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)
|
2005-06-04 20:38:49 +00:00
|
|
|
begin
|
|
|
|
sock.connect(Rex::Socket.to_sockaddr(param.peerhost, param.peerport))
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
raise Rex::ConnectionRefused.new(param.peerhost, param.peerport), caller
|
|
|
|
end
|
2005-06-03 04:51:51 +00:00
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
return sock if (param.bare?)
|
|
|
|
|
2005-06-03 07:32:17 +00:00
|
|
|
case param.proto
|
|
|
|
when 'tcp'
|
2005-06-03 22:51:09 +00:00
|
|
|
klass = Rex::Socket::Tcp
|
|
|
|
|
|
|
|
if (param.ssl)
|
|
|
|
klass = Rex::Socket::SslTcp
|
|
|
|
end
|
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
sock.extend(klass)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
|
|
|
|
return sock
|
2005-06-04 06:19:42 +00:00
|
|
|
when 'udp'
|
2005-09-27 05:31:48 +00:00
|
|
|
sock.extend(Rex::Socket::Udp)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
|
|
|
|
return sock
|
2005-06-03 07:32:17 +00:00
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|