module Rex::Socket::Udp

This class provides methods for interacting with a UDP socket.

Public Class Methods

create(hash = {}) click to toggle source

Creates the client using the supplied hash.

# File lib/rex/socket/udp.rb, line 21
def self.create(hash = {})
  hash['Proto'] = 'udp'
  # If we have are to bind to a LocalHost we must be a Server to avail of pivoting.
  # Rex::Socket::Parameters will subsequently turn off the sever flag after the correct
  # comm has been chosen.
  if( hash['LocalHost'] )
    hash['Server'] = true
  end
  self.create_param(Rex::Socket::Parameters.from_hash(hash))
end
create_param(param) click to toggle source

Wrapper around the base socket class’ creation method that automatically sets the parameter’s protocol to UDP.

# File lib/rex/socket/udp.rb, line 36
def self.create_param(param)
  param.proto = 'udp'
  Rex::Socket.create_param(param)
end

Public Instance Methods

def_read_timeout() click to toggle source

The default number of seconds to wait for a read operation to timeout.

# File lib/rex/socket/udp.rb, line 155
def def_read_timeout
  10
end
get(timeout=nil) click to toggle source

Calls recvfrom and only returns the data

# File lib/rex/socket/udp.rb, line 147
def get(timeout=nil)
  data, saddr, sport = recvfrom(65535, timeout)
  return data
end
put(gram)
Alias for: write
read(length = 65535) click to toggle source

Read a datagram from the UDP socket.

# File lib/rex/socket/udp.rb, line 63
def read(length = 65535)
  if length < 0
    length = 65535
  end
  return sysread(length)
end
recvfrom(length = 65535, timeout=def_read_timeout) click to toggle source

Receives a datagram and returns the data and host:port of the requestor as [ data, host, port ].

# File lib/rex/socket/udp.rb, line 122
def recvfrom(length = 65535, timeout=def_read_timeout)

  begin
    if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
        (rv[0]) and (rv[0][0] == fd)
       )
        data, saddr    = recvfrom_nonblock(length)
        af, host, port = Rex::Socket.from_sockaddr(saddr)

        return [ data, host, port ]
    else
      return [ '', nil, nil ]
    end
  rescue ::Timeout::Error
    return [ '', nil, nil ]
  rescue ::Interrupt
    raise $!
  rescue ::Exception
    return [ '', nil, nil ]
  end
end
sendto(gram, peerhost, peerport, flags = 0) click to toggle source

Sends a datagram to the supplied host:port with optional flags.

# File lib/rex/socket/udp.rb, line 99
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 = Rex::Socket.getaddress(peerhost, true)
    if peerhost[0,7].downcase != '::ffff:'
      peerhost = '::ffff:' + peerhost
    end
  end

  begin
    send(gram, flags, Rex::Socket.to_sockaddr(peerhost, peerport))
  rescue  ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
    return nil
  end

end
timed_read(length = 65535, timeout=def_read_timeout) click to toggle source

Read a datagram from the UDP socket with a timeout

# File lib/rex/socket/udp.rb, line 73
def timed_read(length = 65535, timeout=def_read_timeout)
  begin
    if ((rv = ::IO.select([ fd ], nil, nil, timeout)) and
        (rv[0]) and (rv[0][0] == fd)
       )
        return read(length)
    else
      return ''
    end
  rescue Exception
    return ''
  end
end
type?() click to toggle source
# File lib/rex/socket/udp.rb, line 159
def type?
  return 'udp'
end
write(gram) click to toggle source

Write the supplied datagram to the connected UDP socket.

# File lib/rex/socket/udp.rb, line 50
def write(gram)
  begin
    return syswrite(gram)
  rescue  ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
    return nil
  end
end
Also aliased as: put