2005-06-02 07:52:17 +00:00
|
|
|
require 'socket'
|
|
|
|
require 'resolv'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Socket
|
|
|
|
# ------
|
|
|
|
#
|
|
|
|
# Base class for all sockets.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Socket
|
|
|
|
|
2005-06-03 04:51:51 +00:00
|
|
|
module Comm
|
|
|
|
end
|
|
|
|
|
|
|
|
require 'Rex/Socket/Parameters'
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory methods
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
def self.create(opts)
|
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_param(param)
|
|
|
|
return param.comm.create(param)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_tcp(opts)
|
2005-06-03 04:51:51 +00:00
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts.merge('Proto' => 'tcp')))
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
2005-06-03 07:13:15 +00:00
|
|
|
|
|
|
|
def self.create_tcp_server(opts)
|
|
|
|
return create_tcp(opts.merge('Server' => true))
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Serialization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a sockaddr structure using the supplied IP address, port, and
|
|
|
|
# address family
|
|
|
|
#
|
|
|
|
def self.to_sockaddr(ip, port, af = ::Socket::AF_INET)
|
|
|
|
ip = "0.0.0.0" unless ip
|
|
|
|
ip = Resolv.getaddress(ip)
|
|
|
|
data = [ af, port.to_i ] + ip.split('.').collect { |o| o.to_i } + [ "" ]
|
|
|
|
|
|
|
|
return data.pack('snCCCCa8')
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
|
|
|
# Class initialization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-06-03 07:37:15 +00:00
|
|
|
def initialize(sock, params = nil)
|
2005-06-02 07:52:17 +00:00
|
|
|
self.sock = sock
|
2005-06-03 07:37:15 +00:00
|
|
|
|
|
|
|
if (params)
|
|
|
|
self.peerhost = params.peerhost
|
|
|
|
self.peerport = params.peerport
|
|
|
|
self.localhost = params.localhost
|
|
|
|
self.localport = params.localport
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
|
|
|
# Closes the associated socket
|
|
|
|
#
|
|
|
|
def close
|
|
|
|
self.sock.close if (self.sock)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the sock context that was supplied to the constructor as the
|
|
|
|
# default poll_fd
|
|
|
|
#
|
|
|
|
def poll_fd
|
|
|
|
return self.sock
|
|
|
|
end
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
attr_reader :sock
|
2005-06-03 07:37:15 +00:00
|
|
|
attr_reader :peerhost, :peerport, :localhost, :localport
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_writer :sock
|
2005-06-03 07:37:15 +00:00
|
|
|
attr_writer :peerhost, :peerport, :localhost, :localport
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|