Module: Rex::Socket::Ip

Includes:
Rex::Socket
Defined in:
lib/rex/socket/ip.rb

Overview

This class provides methods for interacting with a IP socket.

Constant Summary

Constants included from Rex::Socket

MATCH_IPV4, MATCH_IPV4_PRIVATE, MATCH_IPV6

Instance Attribute Summary

Attributes included from Rex::Socket

#context, #ipv, #localhost, #localport, #peerhost, #peerport

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rex::Socket

addr_atoc, addr_atoi, addr_atoi_list, addr_aton, addr_ctoa, addr_itoa, addr_iton, addr_ntoa, addr_ntoi, bit2netmask, cidr_crack, compress_address, create_ip, create_tcp, create_tcp_server, create_udp, dotted_ip?, eth_aton, eth_ntoa, #fd, from_sockaddr, getaddress, getaddresses, gethostbyname, #getlocalname, #getpeername, #getsockname, #initsock, ipv6_link_address, ipv6_mac, is_internal?, is_ipv4?, is_ipv6?, net2bitmask, portlist_to_portspec, portspec_crack, portspec_to_portlist, resolv_nbo, resolv_nbo_i, resolv_nbo_i_list, resolv_nbo_list, resolv_to_dotted, source_address, support_ipv6?, tcp_socket_pair, to_sockaddr, udp_socket_pair

Class Method Details

.create(hash = {}) ⇒ Object

Creates the client using the supplied hash.



22
23
24
25
# File 'lib/rex/socket/ip.rb', line 22

def self.create(hash = {})
  hash['Proto'] = 'ip'
  self.create_param(Rex::Socket::Parameters.from_hash(hash))
end

.create_param(param) ⇒ Object

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



31
32
33
34
# File 'lib/rex/socket/ip.rb', line 31

def self.create_param(param)
  param.proto = 'ip'
  Rex::Socket.create_param(param)
end

Instance Method Details

#def_read_timeoutObject

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



123
124
125
# File 'lib/rex/socket/ip.rb', line 123

def def_read_timeout
  10
end

#get(timeout = nil) ⇒ Object

Calls recvfrom and only returns the data



115
116
117
118
# File 'lib/rex/socket/ip.rb', line 115

def get(timeout=nil)
  data, saddr = recvfrom(65535, timeout)
  return data
end

#read(length = 65535) ⇒ Object

Read a datagram from the IP socket.

Raises:



54
55
56
# File 'lib/rex/socket/ip.rb', line 54

def read(length = 65535)
  raise RuntimeError, "IP sockets must use recvfrom(), not read()"
end

#recvfrom(length = 65535, timeout = def_read_timeout) ⇒ Object

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rex/socket/ip.rb', line 95

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    = super(length)
        af, host       = Rex::Socket.from_sockaddr(saddr)

        return [ data, host ]
    else
      return [ '', nil ]
    end
  rescue Exception
    return [ '', nil ]
  end
end

#sendto(gram, peerhost, flags = 0) ⇒ Object

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rex/socket/ip.rb', line 67

def sendto(gram, peerhost, flags = 0)
  dest = ::Socket.pack_sockaddr_in(0, peerhost)

  # Some BSDs require byteswap for len and offset
  if(
    Rex::Compat.is_freebsd or
    Rex::Compat.is_netbsd or
    Rex::Compat.is_bsdi or
    Rex::Compat.is_macosx
    )
    gram=gram.dup
    # Note that these are *intentionally* host order for BSD support
    gram[2,2]=gram[2,2].unpack("n").pack("s")
    gram[6,2]=gram[6,2].unpack("n").pack("s")
  end

  begin
    send(gram, flags, dest)
  rescue  ::Errno::EHOSTUNREACH,::Errno::ENETDOWN,::Errno::ENETUNREACH,::Errno::ENETRESET,::Errno::EHOSTDOWN,::Errno::EACCES,::Errno::EINVAL,::Errno::EADDRNOTAVAIL
    return nil
  end

end

#type?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rex/socket/ip.rb', line 127

def type?
  return 'ip'
end

#write(gram) ⇒ Object Also known as: put

Write the supplied datagram to the connected IP socket.

Raises:



45
46
47
# File 'lib/rex/socket/ip.rb', line 45

def write(gram)
  raise RuntimeError, "IP sockets must use sendto(), not write()"
end