Files
metasploit-gs/lib/rex/socket/udp.rb
T

150 lines
2.8 KiB
Ruby
Raw Normal View History

2005-07-09 21:18:49 +00:00
require 'rex/socket'
2005-06-04 06:19:42 +00:00
###
#
# This class provides methods for interacting with a UDP socket.
#
###
2005-09-27 05:31:48 +00:00
module Rex::Socket::Udp
include Rex::Socket
2005-06-04 06:19:42 +00:00
##
#
# Factory
#
##
2005-09-27 05:31:48 +00:00
#
2005-11-15 05:22:13 +00:00
# Creates the client using the supplied hash.
2005-09-27 05:31:48 +00:00
#
def self.create(hash = {})
self.create_param(Rex::Socket::Parameters.from_hash(hash))
end
2005-06-04 06:19:42 +00:00
#
# Wrapper around the base socket class' creation method that automatically
2005-11-15 05:22:13 +00:00
# sets the parameter's protocol to UDP.
2005-06-04 06:19:42 +00:00
#
def self.create_param(param)
param.proto = 'udp'
2005-09-27 05:31:48 +00:00
Rex::Socket.create_param(param)
2005-06-04 06:19:42 +00:00
end
##
#
# UDP connected state methods
#
##
#
2005-11-15 05:22:13 +00:00
# Write the supplied datagram to the connected UDP socket.
2005-06-04 06:19:42 +00:00
#
def write(gram)
begin
return syswrite(gram)
2008-11-11 07:41:07 +00:00
rescue ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
return nil
end
2005-06-04 06:19:42 +00:00
end
2009-02-05 21:49:50 +00:00
alias put write
2005-11-26 11:16:36 +00:00
2005-06-04 06:19:42 +00:00
#
2005-11-15 05:22:13 +00:00
# Read a datagram from the UDP socket.
2005-06-04 06:19:42 +00:00
#
def read(length = 65535)
2006-01-27 05:33:08 +00:00
if length < 0
length = 65535
end
2005-09-27 05:31:48 +00:00
return sysread(length)
2005-06-04 06:19:42 +00:00
end
2005-11-26 11:16:36 +00:00
#
# Read a datagram from the UDP socket with a timeout
#
def timed_read(length = 65535, timeout=def_read_timeout)
begin
if ((rv = Kernel.select([ fd ], nil, nil, timeout)) and
(rv[0]) and (rv[0][0] == fd)
)
return read(length)
else
return ''
end
rescue Exception
return ''
end
end
2005-06-04 06:19:42 +00:00
#alias send write
#alias recv read
##
#
# UDP non-connected state methods
#
##
#
2005-11-15 05:22:13 +00:00
# Sends a datagram to the supplied host:port with optional flags.
2005-06-04 06:19:42 +00:00
#
def sendto(gram, peerhost, peerport, flags = 0)
# Catch unconnected IPv6 sockets talking to IPv4 addresses
peer = Rex::Socket.resolv_nbo(peerhost)
if (peer.length == 4 and self.ipv == 6)
peerhost = '::ffff:' + Rex::Socket.getaddress(peerhost)
end
begin
send(gram, flags, Rex::Socket.to_sockaddr(peerhost, peerport))
2008-11-11 07:41:07 +00:00
rescue ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
return nil
end
2005-06-04 06:19:42 +00:00
end
#
# Receives a datagram and returns the data and host:port of the requestor
2005-11-15 05:22:13 +00:00
# as [ data, host, port ].
2005-06-04 06:19:42 +00:00
#
2005-11-26 11:16:36 +00:00
def recvfrom(length = 65535, timeout=def_read_timeout)
begin
if ((rv = Kernel.select([ fd ], nil, nil, timeout)) and
(rv[0]) and (rv[0][0] == fd)
)
data, saddr = super(length)
af, host, port = Rex::Socket.from_sockaddr(saddr)
2005-06-04 06:19:42 +00:00
2005-11-26 11:16:36 +00:00
return [ data, host, port ]
else
return [ '', nil, nil ]
end
rescue Exception
return [ '', nil, nil ]
end
2005-06-04 06:19:42 +00:00
end
2005-11-26 11:16:36 +00:00
#
# Calls recvfrom and only returns the data
#
def get(timeout=nil)
data, saddr, sport = recvfrom(65535, timeout)
return data
end
#
# The default number of seconds to wait for a read operation to timeout.
#
def def_read_timeout
10
end
2005-06-04 06:19:42 +00:00
2006-01-27 05:33:08 +00:00
def type?
return 'udp'
end
2005-12-13 06:08:40 +00:00
end