Class: PacketFu::ARPHeader

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/protos/arp/header.rb

Overview

ARPHeader is a complete ARP struct, used in ARPPacket.

ARP is used to discover the machine address of nearby devices.

See www.networksorcery.com/enp/protocol/arp.htm for details.

Header Definition

Int16   :arp_hw          Default: 1       # Ethernet
Int16   :arp_proto,      Default: 0x8000  # IP
Int8    :arp_hw_len,     Default: 6
Int8    :arp_proto_len,  Default: 4
Int16   :arp_opcode,     Default: 1       # 1: Request, 2: Reply, 3: Request-Reverse, 4: Reply-Reverse
EthMac  :arp_src_mac                      # From eth.rb
Octets  :arp_src_ip                       # From ip.rb
EthMac  :arp_dst_mac                      # From eth.rb
Octets  :arp_dst_ip                       # From ip.rb
String  :body

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ ARPHeader

Returns a new instance of ARPHeader.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/packetfu/protos/arp/header.rb', line 28

def initialize(args={})
  src_mac = args[:arp_src_mac] || (args[:config][:eth_src] if args[:config])
  src_ip_bin = args[:arp_src_ip]   || (args[:config][:ip_src_bin] if args[:config])

  super( 
    Int16.new(args[:arp_hw] || 1), 
    Int16.new(args[:arp_proto] ||0x0800),
    Int8.new(args[:arp_hw_len] || 6), 
    Int8.new(args[:arp_proto_len] || 4), 
    Int16.new(args[:arp_opcode] || 1),
    EthMac.new.read(src_mac),
    Octets.new.read(src_ip_bin),
    EthMac.new.read(args[:arp_dst_mac]),
    Octets.new.read(args[:arp_dst_ip]),
    StructFu::String.new.read(args[:body])
  )
end

Instance Attribute Details

#arp_dst_ipObject

Getter for the ARP destination IP address.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_dst_ip
  @arp_dst_ip
end

#arp_dst_macObject

Setter for the ARP destination MAC address.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_dst_mac
  @arp_dst_mac
end

#arp_hwObject

Getter for the ARP hardware type.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_hw
  @arp_hw
end

#arp_hw_lenObject

Getter for the ARP hardware type length.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_hw_len
  @arp_hw_len
end

#arp_opcodeObject

Getter for the ARP opcode.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_opcode
  @arp_opcode
end

#arp_protoObject

Getter for the ARP protocol.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_proto
  @arp_proto
end

#arp_proto_lenObject

Getter for the ARP protocol length.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_proto_len
  @arp_proto_len
end

#arp_src_ipObject

Setter for the ARP source IP address.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_src_ip
  @arp_src_ip
end

#arp_src_macObject

Getter for the ARP source MAC address.



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def arp_src_mac
  @arp_src_mac
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



21
22
23
# File 'lib/packetfu/protos/arp/header.rb', line 21

def body
  @body
end

Instance Method Details

#arp_daddr_ipObject Also known as: arp_dst_ip_readable

Get a more readable destination IP address.



145
146
147
# File 'lib/packetfu/protos/arp/header.rb', line 145

def arp_daddr_ip
  self[:arp_dst_ip].to_x
end

#arp_daddr_ip=(addr) ⇒ Object

Set a more readable destination IP address.



140
141
142
# File 'lib/packetfu/protos/arp/header.rb', line 140

def arp_daddr_ip=(addr)
  self[:arp_dst_ip].read_quad(addr)
end

#arp_daddr_macObject Also known as: arp_dst_mac_readable

Get a more readable source MAC address.



125
126
127
# File 'lib/packetfu/protos/arp/header.rb', line 125

def arp_daddr_mac
  EthHeader.str2mac(self[:arp_dst_mac].to_s)
end

#arp_daddr_mac=(mac) ⇒ Object

Set the destination MAC address in a more readable way.



118
119
120
121
122
# File 'lib/packetfu/protos/arp/header.rb', line 118

def arp_daddr_mac=(mac)
  mac = EthHeader.mac2str(mac)
  self[:arp_dst_mac].read(mac)
  self.arp_dst_mac
end

#arp_proto_readableObject



156
157
158
# File 'lib/packetfu/protos/arp/header.rb', line 156

def arp_proto_readable
  "0x%04x" % arp_proto
end

#arp_saddr_ipObject Also known as: arp_src_ip_readable

Get a more readable source IP address.



135
136
137
# File 'lib/packetfu/protos/arp/header.rb', line 135

def arp_saddr_ip
  self[:arp_src_ip].to_x
end

#arp_saddr_ip=(addr) ⇒ Object

Set a more readable source IP address.



130
131
132
# File 'lib/packetfu/protos/arp/header.rb', line 130

def arp_saddr_ip=(addr)
  self[:arp_src_ip].read_quad(addr)
end

#arp_saddr_macObject Also known as: arp_src_mac_readable

Get a more readable source MAC address.



113
114
115
# File 'lib/packetfu/protos/arp/header.rb', line 113

def arp_saddr_mac
  EthHeader.str2mac(self[:arp_src_mac].to_s)
end

#arp_saddr_mac=(mac) ⇒ Object

Set the source MAC address in a more readable way.



106
107
108
109
110
# File 'lib/packetfu/protos/arp/header.rb', line 106

def arp_saddr_mac=(mac)
  mac = EthHeader.mac2str(mac)
  self[:arp_src_mac].read(mac)
  self.arp_src_mac
end

#read(str) ⇒ Object

Reads a string to populate the object.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/packetfu/protos/arp/header.rb', line 52

def read(str)
  force_binary(str)
  return self if str.nil?
  self[:arp_hw].read(str[0,2])
  self[:arp_proto].read(str[2,2])
  self[:arp_hw_len].read(str[4,1])
  self[:arp_proto_len].read(str[5,1])
  self[:arp_opcode].read(str[6,2])
  self[:arp_src_mac].read(str[8,6])
  self[:arp_src_ip].read(str[14,4])
  self[:arp_dst_mac].read(str[18,6])
  self[:arp_dst_ip].read(str[24,4])
  self[:body].read(str[28,str.size])
  self
end

#to_sObject

Returns the object in string form.



47
48
49
# File 'lib/packetfu/protos/arp/header.rb', line 47

def to_s
  self.to_a.map {|x| x.to_s}.join
end