Class: Arpoon::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/arpoon/packet.rb

Constant Summary collapse

Operations =
{
  1 => :request,
  2 => :reply
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface = nil, source, destination, operation, sender_hw, sender_ip, target_hw, target_ip) ⇒ Packet

Returns a new instance of Packet.



64
65
66
67
68
69
70
71
72
73
# File 'lib/arpoon/packet.rb', line 64

def initialize (interface = nil, source, destination, operation, sender_hw, sender_ip, target_hw, target_ip)
  @interface   = interface
  @source      = source
  @destination = destination

  @operation = operation.downcase

  @sender = Struct.new(:mac, :ip).new(sender_hw, sender_ip)
  @target = Struct.new(:mac, :ip).new(target_hw, target_ip)
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



62
63
64
# File 'lib/arpoon/packet.rb', line 62

def destination
  @destination
end

#interfaceObject (readonly)

Returns the value of attribute interface.



62
63
64
# File 'lib/arpoon/packet.rb', line 62

def interface
  @interface
end

#senderObject (readonly)

Returns the value of attribute sender.



62
63
64
# File 'lib/arpoon/packet.rb', line 62

def sender
  @sender
end

#sourceObject (readonly)

Returns the value of attribute source.



62
63
64
# File 'lib/arpoon/packet.rb', line 62

def source
  @source
end

#targetObject (readonly)

Returns the value of attribute target.



62
63
64
# File 'lib/arpoon/packet.rb', line 62

def target
  @target
end

Class Method Details

.unpack(data, interface = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/arpoon/packet.rb', line 22

def self.unpack (data, interface = nil)
  source      = HWAddr.new(data[0, 6].unpack('C6'))
  destination = HWAddr.new(data[6, 6].unpack('C6'))

  unless data[12, 2].unpack('n').first == 0x0806
    raise ArgumentError, 'the passed data is not an ARP packet'
  end

  unless data[16, 2].unpack('n').first == 0x0800
    raise ArgumentError, 'the passed data is not using the IP protocol'
  end

  hw_size = data[18, 1].unpack('C').first
  pr_size = data[19, 1].unpack('C').first

  if hw_size != 6
    raise ArgumentError, "#{hw_size} is an unsupported size"
  end

  opcode = data[20, 2].unpack('n').first

  if pr_size == 4
    sender_hw = HWAddr.new(data[22, 6].unpack('C6'))
    sender_ip = IPAddr.new_ntoh(data[28, 4])

    target_hw = HWAddr.new(data[32, 6].unpack('C6'))
    target_ip = IPAddr.new_ntoh(data[38, 4])
  elsif pr_size == 16
    sender_hw = HWAddr.new(data[22, 6].unpack('C6'))
    sender_ip = IPAddr.new_ntoh(data[28, 16])

    target_hw = HWAddr.new(data[44, 6].unpack('C6'))
    target_ip = IPAddr.new_ntoh(data[50, 16])
  else
    raise ArgumentError, "#{pr_size} is an unsupported protocol size"
  end

  new(interface, source, destination, Operations[opcode], sender_hw, sender_ip, target_hw, target_ip)
end

Instance Method Details

#reply?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/arpoon/packet.rb', line 79

def reply?
  @operation == :reply
end

#request?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/arpoon/packet.rb', line 75

def request?
  @operation == :request
end

#to_symObject



83
84
85
# File 'lib/arpoon/packet.rb', line 83

def to_sym
  @operation
end