2005-06-02 07:52:17 +00:00
|
|
|
require 'socket'
|
|
|
|
require 'resolv'
|
2005-09-30 05:59:44 +00:00
|
|
|
require 'rex/exceptions'
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Base class for all sockets.
|
|
|
|
#
|
|
|
|
###
|
2005-09-27 05:31:48 +00:00
|
|
|
module Socket
|
2005-06-02 07:52:17 +00:00
|
|
|
|
2005-06-03 04:51:51 +00:00
|
|
|
module Comm
|
|
|
|
end
|
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/socket/parameters'
|
2005-07-24 20:53:54 +00:00
|
|
|
require 'rex/socket/tcp'
|
|
|
|
require 'rex/socket/tcp_server'
|
2005-09-30 05:59:44 +00:00
|
|
|
|
|
|
|
require 'rex/socket/comm'
|
2005-07-24 20:53:54 +00:00
|
|
|
require 'rex/socket/comm/local'
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-30 05:48:45 +00:00
|
|
|
require 'rex/socket/switch_board'
|
2005-10-27 04:14:02 +00:00
|
|
|
require 'rex/socket/subnet_walker'
|
2006-08-12 08:31:38 +00:00
|
|
|
require 'rex/socket/range_walker'
|
2005-06-03 04:51:51 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Factory methods
|
|
|
|
#
|
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Create a socket instance using the supplied parameter hash.
|
|
|
|
#
|
2005-06-04 06:19:42 +00:00
|
|
|
def self.create(opts = {})
|
2005-06-02 07:52:17 +00:00
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts))
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Create a socket using the supplied Rex::Socket::Parameter instance.
|
|
|
|
#
|
2005-06-02 07:52:17 +00:00
|
|
|
def self.create_param(param)
|
|
|
|
return param.comm.create(param)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Create a TCP socket using the supplied parameter hash.
|
|
|
|
#
|
2005-06-04 06:19:42 +00:00
|
|
|
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-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Create a TCP server socket using the supplied parameter hash.
|
|
|
|
#
|
|
|
|
def self.create_tcp_server(opts = {})
|
2005-06-03 07:13:15 +00:00
|
|
|
return create_tcp(opts.merge('Server' => true))
|
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Create a UDP socket using the supplied parameter hash.
|
|
|
|
#
|
2005-06-04 06:19:42 +00:00
|
|
|
def self.create_udp(opts = {})
|
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts.merge('Proto' => 'udp')))
|
|
|
|
end
|
|
|
|
|
2008-07-22 19:03:59 +00:00
|
|
|
#
|
|
|
|
# Create a IP socket using the supplied parameter hash.
|
|
|
|
#
|
|
|
|
def self.create_ip(opts = {})
|
|
|
|
return create_param(Rex::Socket::Parameters.from_hash(opts.merge('Proto' => 'ip')))
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Serialization
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2007-04-30 00:11:49 +00:00
|
|
|
|
|
|
|
# Cache our IPv6 support flag
|
|
|
|
@@support_ipv6 = nil
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
|
|
|
# Determine whether we support IPv6
|
2009-12-08 18:32:26 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.support_ipv6?
|
2008-12-26 07:11:49 +00:00
|
|
|
return @@support_ipv6 if not @@support_ipv6.nil?
|
|
|
|
|
|
|
|
@@support_ipv6 = false
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-30 00:11:49 +00:00
|
|
|
if (::Socket.const_defined?('AF_INET6'))
|
|
|
|
begin
|
|
|
|
s = ::Socket.new(::Socket::AF_INET6, ::Socket::SOCK_DGRAM, ::Socket::IPPROTO_UDP)
|
|
|
|
s.close
|
2008-12-26 07:11:49 +00:00
|
|
|
@@support_ipv6 = true
|
2007-04-30 00:13:26 +00:00
|
|
|
rescue
|
2007-04-30 00:11:49 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2008-12-26 07:11:49 +00:00
|
|
|
return @@support_ipv6
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
#
|
|
|
|
# Determine whether this is an IPv4 address
|
2009-12-08 18:32:26 +00:00
|
|
|
#
|
2006-06-08 21:02:58 +00:00
|
|
|
def self.is_ipv4?(addr)
|
2006-06-15 20:53:20 +00:00
|
|
|
res = Rex::Socket.getaddress(addr)
|
2006-06-08 21:02:58 +00:00
|
|
|
res.match(/:/) ? false : true
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2006-06-08 21:02:58 +00:00
|
|
|
#
|
|
|
|
# Determine whether this is an IPv6 address
|
2009-12-08 18:32:26 +00:00
|
|
|
#
|
2006-06-08 21:02:58 +00:00
|
|
|
def self.is_ipv6?(addr)
|
2006-06-15 20:53:20 +00:00
|
|
|
res = Rex::Socket.getaddress(addr)
|
2006-06-08 21:02:58 +00:00
|
|
|
res.match(/:/) ? true : false
|
|
|
|
end
|
|
|
|
|
2006-06-15 20:53:20 +00:00
|
|
|
#
|
2009-12-08 18:32:26 +00:00
|
|
|
# Checks to see if the supplied address is a dotted quad.
|
2006-06-15 20:53:20 +00:00
|
|
|
#
|
|
|
|
def self.dotted_ip?(addr)
|
2007-04-29 23:43:39 +00:00
|
|
|
# Assume anything with a colon is IPv6
|
|
|
|
return true if (support_ipv6? and addr =~ /:/)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
# Otherwise assume this is IPv4
|
2006-06-15 22:41:57 +00:00
|
|
|
(addr =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))$/) ? true : false
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Wrapper for Resolv.getaddress that takes special care to see if the
|
|
|
|
# supplied address is already a dotted quad, for instance. This is
|
|
|
|
# necessary to prevent calls to gethostbyaddr (which occurs on windows).
|
|
|
|
# These calls can be quite slow.
|
|
|
|
#
|
|
|
|
def self.getaddress(addr)
|
2008-11-09 22:59:21 +00:00
|
|
|
begin
|
|
|
|
dotted_ip?(addr) ? addr : Resolv.getaddress(addr)
|
|
|
|
rescue ::ArgumentError # Win32 bug
|
|
|
|
nil
|
|
|
|
end
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Wrapper for Socket.gethostbyname which takes into account whether or not
|
|
|
|
# an IP address is supplied. If it is, then reverse DNS resolution does
|
|
|
|
# not occur. This is done in order to prevent delays, such as would occur
|
|
|
|
# on Windows.
|
|
|
|
#
|
|
|
|
def self.gethostbyname(host)
|
2007-04-29 23:43:39 +00:00
|
|
|
if (dotted_ip?(host))
|
|
|
|
if (is_ipv4?(host))
|
|
|
|
return [ host, host, 2, host.split('.').map{ |c| c.to_i }.pack("C4") ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
::Socket.gethostbyname(host)
|
2006-06-15 20:53:20 +00:00
|
|
|
end
|
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
#
|
|
|
|
# Create a sockaddr structure using the supplied IP address, port, and
|
|
|
|
# address family
|
|
|
|
#
|
2006-06-08 21:02:58 +00:00
|
|
|
def self.to_sockaddr(ip, port)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-05-01 04:47:53 +00:00
|
|
|
if (ip == '::ffff:0.0.0.0')
|
|
|
|
ip = support_ipv6?() ? '::' : '0.0.0.0'
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ::Socket.pack_sockaddr_in(port, ip)
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2005-06-04 06:19:42 +00:00
|
|
|
#
|
|
|
|
# Returns the address family, host, and port of the supplied sockaddr as
|
|
|
|
# [ af, host, port ]
|
|
|
|
#
|
|
|
|
def self.from_sockaddr(saddr)
|
2007-03-22 22:15:26 +00:00
|
|
|
port, host = ::Socket::unpack_sockaddr_in(saddr)
|
2007-04-29 23:43:39 +00:00
|
|
|
af = ::Socket::AF_INET
|
|
|
|
if (support_ipv6?() and is_ipv6?(host))
|
|
|
|
af = ::Socket::AF_INET6
|
|
|
|
end
|
2007-03-22 22:15:26 +00:00
|
|
|
return [ af, host, port ]
|
2005-06-04 06:19:42 +00:00
|
|
|
end
|
|
|
|
|
2005-07-08 14:53:12 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Resolves a host to raw network-byte order.
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
2005-07-08 14:53:12 +00:00
|
|
|
def self.resolv_nbo(host)
|
2006-06-15 20:53:20 +00:00
|
|
|
self.gethostbyname(Rex::Socket.getaddress(host))[3]
|
2005-07-08 14:53:12 +00:00
|
|
|
end
|
|
|
|
|
2005-10-27 04:14:02 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to a network-byte order ruby integer.
|
|
|
|
#
|
2005-09-30 05:43:06 +00:00
|
|
|
def self.resolv_nbo_i(host)
|
2007-04-29 23:43:39 +00:00
|
|
|
addr_ntoi(resolv_nbo(host))
|
2005-09-30 05:43:06 +00:00
|
|
|
end
|
|
|
|
|
2005-10-27 04:14:02 +00:00
|
|
|
#
|
|
|
|
# Resolves a host to a dotted address.
|
|
|
|
#
|
|
|
|
def self.resolv_to_dotted(host)
|
2007-04-29 23:43:39 +00:00
|
|
|
addr_ntoa(addr_aton(host))
|
2005-10-27 04:14:02 +00:00
|
|
|
end
|
|
|
|
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Converts a ascii address into an integer
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.addr_atoi(addr)
|
|
|
|
resolv_nbo_i(addr)
|
|
|
|
end
|
2005-09-30 05:43:06 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
#
|
|
|
|
# Converts an integer address into ascii
|
|
|
|
#
|
|
|
|
def self.addr_itoa(addr, v6=false)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
nboa = addr_iton(addr, v6)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
|
|
|
# IPv4
|
2007-04-29 23:43:39 +00:00
|
|
|
if (addr < 0x100000000 and not v6)
|
|
|
|
nboa.unpack('C4').join('.')
|
|
|
|
# IPv6
|
|
|
|
else
|
|
|
|
nboa.unpack('n8').map{ |c| "%.4x" % c }.join(":")
|
2009-12-08 18:32:26 +00:00
|
|
|
end
|
2005-09-30 05:43:06 +00:00
|
|
|
end
|
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Converts a ascii address to network byte order
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.addr_aton(addr)
|
|
|
|
resolv_nbo(addr)
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# Converts a network byte order address to ascii
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.addr_ntoa(addr)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
|
|
|
# IPv4
|
2007-04-29 23:43:39 +00:00
|
|
|
if (addr.length == 4)
|
2008-07-23 17:39:02 +00:00
|
|
|
return addr.unpack('C4').join('.')
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
# IPv6
|
|
|
|
if (addr.length == 16)
|
|
|
|
return addr.unpack('n8').map{ |c| "%.4x" % c }.join(":")
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
|
|
|
raise RuntimeError, "Invalid address format"
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2007-04-29 23:43:39 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Converts a network byte order address to an integer
|
|
|
|
#
|
|
|
|
def self.addr_ntoi(addr)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
bits = addr.unpack("N*")
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
if (bits.length == 1)
|
|
|
|
return bits[0]
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
if (bits.length == 4)
|
|
|
|
val = 0
|
|
|
|
bits.each_index { |i| val += ( bits[i] << (96 - (i * 32)) ) }
|
|
|
|
return val
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
raise RuntimeError, "Invalid address format"
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Converts an integer into a network byte order address
|
|
|
|
#
|
|
|
|
def self.addr_iton(addr, v6=false)
|
|
|
|
if(addr < 0x100000000 and not v6)
|
|
|
|
return [addr].pack('N')
|
|
|
|
else
|
|
|
|
w = []
|
|
|
|
w[0] = (addr >> 96) & 0xffffffff
|
|
|
|
w[1] = (addr >> 64) & 0xffffffff
|
|
|
|
w[2] = (addr >> 32) & 0xffffffff
|
|
|
|
w[3] = addr & 0xffffffff
|
2009-12-08 18:32:26 +00:00
|
|
|
return w.pack('N4')
|
2007-04-29 23:43:39 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2006-08-12 08:31:38 +00:00
|
|
|
#
|
|
|
|
# Converts a CIDR subnet into an array (base, bcast)
|
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
def self.cidr_crack(cidr, v6=false)
|
2006-08-12 08:31:38 +00:00
|
|
|
tmp = cidr.split('/')
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2008-08-23 07:05:38 +00:00
|
|
|
tst,scope = tmp[0].split("%",2)
|
|
|
|
scope = "%" + scope if scope
|
|
|
|
scope ||= ""
|
|
|
|
|
|
|
|
addr = addr_atoi(tst)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
bits = 32
|
|
|
|
mask = 0
|
|
|
|
use6 = false
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-05-01 04:54:18 +00:00
|
|
|
if (addr > 0xffffffff or v6 or cidr =~ /:/)
|
2007-04-29 23:43:39 +00:00
|
|
|
use6 = true
|
|
|
|
bits = 128
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
mask = (2 ** bits) - (2 ** (bits - tmp[1].to_i))
|
2006-08-12 08:31:38 +00:00
|
|
|
base = addr & mask
|
2007-04-29 23:43:39 +00:00
|
|
|
|
|
|
|
stop = base + (2 ** (bits - tmp[1].to_i)) - 1
|
2009-12-08 18:32:26 +00:00
|
|
|
return [self.addr_itoa(base, use6) + scope, self.addr_itoa(stop, use6) + scope]
|
2006-08-12 08:31:38 +00:00
|
|
|
end
|
2007-04-29 23:43:39 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Converts a netmask (255.255.255.240) into a bitmask (28). This is the
|
|
|
|
# lame kid way of doing it.
|
|
|
|
#
|
|
|
|
def self.net2bitmask(netmask)
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
nmask = resolv_nbo(netmask)
|
|
|
|
imask = addr_ntoi(nmask)
|
|
|
|
bits = 32
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
if (imask > 0xffffffff)
|
|
|
|
bits = 128
|
|
|
|
end
|
|
|
|
|
|
|
|
0.upto(bits-1) do |bit|
|
|
|
|
p = 2 ** bit
|
|
|
|
return (bits - bit) if ((imask & p) == p)
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-04-29 23:43:39 +00:00
|
|
|
0
|
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
|
|
|
# Converts a bitmask (28) into a netmask (255.255.255.240)
|
2007-04-29 23:43:39 +00:00
|
|
|
# TODO: IPv6 (use is ambiguous right now)
|
2005-09-30 05:43:06 +00:00
|
|
|
#
|
|
|
|
def self.bit2netmask(bitmask)
|
|
|
|
[ (~((2 ** (32 - bitmask)) - 1)) & 0xffffffff ].pack('N').unpack('CCCC').join('.')
|
|
|
|
end
|
|
|
|
|
2008-12-20 20:34:05 +00:00
|
|
|
#
|
|
|
|
# Converts a port specification like "80,21-23,443" into a sorted,
|
|
|
|
# unique array of valid port numbers like [21,22,23,80,443]
|
|
|
|
#
|
|
|
|
def self.portspec_crack(pspec)
|
|
|
|
ports = []
|
|
|
|
|
|
|
|
# Build ports array from port specification
|
|
|
|
pspec.split(/,/).each do |item|
|
|
|
|
start, stop = item.split(/-/).map { |p| p.to_i }
|
|
|
|
|
|
|
|
start ||= 0
|
|
|
|
stop ||= item.match(/-/) ? 65535 : start
|
|
|
|
|
|
|
|
start, stop = stop, start if stop < start
|
|
|
|
|
|
|
|
start.upto(stop) { |p| ports << p }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sort, and remove dups and invalid ports
|
|
|
|
ports.sort.uniq.delete_if { |p| p < 0 or p > 65535 }
|
|
|
|
end
|
|
|
|
|
2005-11-24 00:08:09 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Utility class methods
|
|
|
|
#
|
|
|
|
##
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-11-24 00:08:09 +00:00
|
|
|
def self.source_address(dest='1.2.3.4')
|
2006-01-30 21:57:24 +00:00
|
|
|
begin
|
2008-07-21 03:32:51 +00:00
|
|
|
s = self.create_udp(
|
2006-01-30 21:57:24 +00:00
|
|
|
'PeerHost' => dest,
|
|
|
|
'PeerPort' => 31337
|
2008-07-21 03:32:51 +00:00
|
|
|
)
|
|
|
|
r = s.getsockname[1]
|
|
|
|
s.close
|
|
|
|
return r
|
2006-01-30 21:57:24 +00:00
|
|
|
rescue ::Exception
|
|
|
|
return '127.0.0.1'
|
|
|
|
end
|
2005-11-24 00:08:09 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2007-01-21 20:21:58 +00:00
|
|
|
def self.socket_pair
|
|
|
|
begin
|
|
|
|
pair = ::Socket.pair(::Socket::AF_UNIX, ::Socket::SOCK_STREAM, 0)
|
|
|
|
|
|
|
|
# Windows does not support Socket.pair, so we emulate it
|
|
|
|
rescue ::NotImplementedError
|
|
|
|
srv = TCPServer.new('localhost', 0)
|
|
|
|
rsock = TCPSocket.new(srv.addr[3], srv.addr[1])
|
|
|
|
lsock = srv.accept
|
|
|
|
srv.close
|
|
|
|
[lsock, rsock]
|
2009-12-08 18:32:26 +00:00
|
|
|
end
|
2007-01-21 20:21:58 +00:00
|
|
|
end
|
2009-12-08 18:32:26 +00:00
|
|
|
|
2005-11-24 00:08:09 +00:00
|
|
|
|
2005-06-02 07:52:17 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Class initialization
|
|
|
|
#
|
|
|
|
##
|
2005-06-03 07:37:15 +00:00
|
|
|
|
2005-09-27 05:31:48 +00:00
|
|
|
#
|
|
|
|
# Initialize general socket parameters.
|
|
|
|
#
|
|
|
|
def initsock(params = nil)
|
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
|
2005-11-24 18:50:33 +00:00
|
|
|
self.context = params.context || {}
|
2007-04-29 23:43:39 +00:00
|
|
|
self.ipv = params.v6 ? 6 : 4
|
2005-06-03 07:37:15 +00:00
|
|
|
end
|
2005-06-02 07:52:17 +00:00
|
|
|
end
|
|
|
|
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# By default, all sockets are themselves selectable file descriptors.
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
def fd
|
|
|
|
self
|
2005-06-03 07:13:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# Returns local connection information.
|
2005-06-03 07:13:15 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
def getsockname
|
2009-12-08 18:32:26 +00:00
|
|
|
Socket.from_sockaddr(super)
|
2005-06-03 07:13:15 +00:00
|
|
|
end
|
|
|
|
|
2005-07-16 08:12:58 +00:00
|
|
|
#
|
2005-09-27 05:31:48 +00:00
|
|
|
# Wrapper around getsockname
|
2005-07-16 08:12:58 +00:00
|
|
|
#
|
|
|
|
def getlocalname
|
2005-09-27 05:31:48 +00:00
|
|
|
getsockname
|
2005-07-16 08:12:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Return peer connection information.
|
|
|
|
#
|
|
|
|
def getpeername
|
2005-09-27 05:31:48 +00:00
|
|
|
return Socket.from_sockaddr(super)
|
2005-07-16 08:12:58 +00:00
|
|
|
end
|
|
|
|
|
2006-08-15 04:07:25 +00:00
|
|
|
#
|
|
|
|
# Returns a string that indicates the type of the socket, such as 'tcp'.
|
|
|
|
#
|
|
|
|
def type?
|
|
|
|
raise NotImplementedError, "Socket type is not supported."
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The peer host of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :peerhost
|
|
|
|
#
|
|
|
|
# The peer port of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :peerport
|
|
|
|
#
|
|
|
|
# The local host of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :localhost
|
|
|
|
#
|
|
|
|
# The local port of the connected socket.
|
|
|
|
#
|
|
|
|
attr_reader :localport
|
2005-11-24 18:50:33 +00:00
|
|
|
#
|
2007-04-29 23:43:39 +00:00
|
|
|
# The IP version of the socket
|
|
|
|
#
|
|
|
|
attr_reader :ipv
|
|
|
|
#
|
2005-11-24 18:50:33 +00:00
|
|
|
# Contextual information that describes the source and other
|
|
|
|
# instance-specific attributes. This comes from the param.context
|
|
|
|
# attribute.
|
|
|
|
#
|
|
|
|
attr_reader :context
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_writer :peerhost, :peerport, :localhost, :localport # :nodoc:
|
2005-11-24 18:50:33 +00:00
|
|
|
attr_writer :context # :nodoc:
|
2007-04-29 23:43:39 +00:00
|
|
|
attr_writer :ipv # :nodoc:
|
2005-06-02 07:52:17 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2005-09-29 20:18:24 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Globalized socket constants
|
|
|
|
#
|
|
|
|
SHUT_RDWR = ::Socket::SHUT_RDWR
|
|
|
|
SHUT_RD = ::Socket::SHUT_RD
|
2008-11-09 22:59:21 +00:00
|
|
|
SHUT_WR = ::Socket::SHUT_WR
|
2009-12-08 18:32:26 +00:00
|
|
|
|