Class: RawSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/resolver/socks.rb

Overview

:nodoc:

Direct Known Subclasses

UdpRawSocket

Constant Summary collapse

@@id_arr =
[]

Instance Method Summary collapse

Constructor Details

#initialize(src_addr, dest_addr) ⇒ RawSocket

Returns a new instance of RawSocket.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/net/dns/resolver/socks.rb', line 7

def initialize(src_addr, dest_addr)
  # Define socket
  begin
    @socket = Socket.new PF_INET, SOCK_RAW, IPPROTO_RAW
  rescue SystemCallError => e
    raise SystemCallError, "You must be root to use raw sockets! #{e}"
  end

  @socket.setsockopt IPPROTO_IP, IP_HDRINCL, 1

  # Checks addresses
  @src_addr  = check_addr src_addr
  @dest_addr = check_addr dest_addr

  # Source and destination port are zero
  @src_port  = 0
  @dest_port = 0

  # Set correct protocol version in the header
  @version = @dest_addr.ipv4? ? "0100" : "0110"

  # Total lenght: must be overridden by subclasses
  @tot_lenght = 20

  # Protocol: must be overridden by subclasses
  @protocol = 1 # ICMP by default

  # Generate a new id
  # @id = genID
  @id = 1234

  # Generate peer sockaddr
  @to = Socket.pack_sockaddr_in @dest_port, @dest_addr.to_s
end

Instance Method Details

#send(payload = '') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/net/dns/resolver/socks.rb', line 42

def send(payload = '')
  packet = make_ip_header([
    [@version + '0101', 'B8'],          # version, hlen
    [0, 'C'],                           # tos
    [@tot_lenght + payload.size, 'n'],  # total len
    [@id, 'n'],                         # id
    [0, 'n'],                           # flags, offset
    [64, 'C'],                          # ttl
    [@protocol, 'C'],                   # protocol
    [0, 'n'],                           # checksum
    [@src_addr.to_i, 'N'],              # source
    [@dest_addr.to_i, 'N'], # destination
  ])
  packet << make_transport_header(payload.size)
  packet << [payload].pack("a*")
  @socket.send(packet, 0, @to)
end