2005-11-15 20:33:55 +00:00
|
|
|
require 'singleton'
|
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-11-15 20:33:55 +00:00
|
|
|
include Singleton
|
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-11-15 05:22:13 +00:00
|
|
|
# Creates a socket using the supplied Parameter instance.
|
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-11-15 20:33:55 +00:00
|
|
|
# Notify handlers of the before socket create event.
|
|
|
|
self.instance.notify_before_socket_create(self, param)
|
|
|
|
|
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)
|
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
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
if (param.bare? == false)
|
|
|
|
case param.proto
|
|
|
|
when 'tcp'
|
|
|
|
klass = Rex::Socket::Tcp
|
|
|
|
|
|
|
|
if (param.ssl)
|
|
|
|
klass = Rex::Socket::SslTcp
|
|
|
|
end
|
|
|
|
|
|
|
|
sock.extend(klass)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
when 'udp'
|
|
|
|
sock.extend(Rex::Socket::Udp)
|
|
|
|
|
|
|
|
sock.initsock(param)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-06-03 22:51:09 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
# Notify handlers that a socket has been created.
|
|
|
|
self.instance.notify_socket_created(self, sock, param)
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
sock
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Registration
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
def self.register_event_handler(handler) # :nodoc:
|
|
|
|
self.instance.register_event_handler(handler)
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
def self.deregister_event_handler(handler) # :nodoc:
|
|
|
|
self.instance.deregister_event_handler(handler)
|
|
|
|
end
|
2005-09-27 05:31:48 +00:00
|
|
|
|
2005-11-15 20:33:55 +00:00
|
|
|
def self.each_event_handler(handler) # :nodoc:
|
|
|
|
self.instance.each_event_handler(handler)
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|