Class: PacketFu::LLDPHeader

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

Overview

LLDPHeader is a complete LLDP struct, used in LLDPPacket.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ LLDPHeader

Returns a new instance of LLDPHeader.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/packetfu/protos/lldp/header.rb', line 8

def initialize(args={})
  src_mac = (args[:lldp_port_id] if :lldp_port_id_type == 3) || (args[:config][:eth_src] if args[:config])
  src_ip_bin = (args[:lldp_address] if :lldp_address_type == 1) || (args[:config][:ip_src_bin] if args[:config])

  super(Int8.new(args[:lldp_chassis_id_type] || 4),
  StructFu::String.new.read(:lldp_chassis_id),
  Int8.new(args[:lldp_port_id_type] || 3),
  EthMac.new.read(src_mac),
  Int16.new(args[:lldp_ttl] || 120),
  StructFu::String.new.read(:lldp_port_description) || "",
  StructFu::String.new.read(:lldp_system_name) || "",
  StructFu::String.new.read(:lldp_system_description) || "",
  Int16.new(args[:lldp_capabilty] || 0x0080),
  Int16.new(args[:lldp_enabled_capability] || 0x0080),
  Int8.new(args[:lldp_address_type] || 1),
  StructFu::String.new.read(:lldp_address) || src_ip_bin,
  Int8.new(args[:lldp_interface_type] || 2),
  Int32.new(args[:lldp_interface]),
  StructFu::String.new.read(:lldp_oid) || ""
  )
end

Instance Attribute Details

#lldp_address=(value) ⇒ Object

Sets the attribute lldp_address

Parameters:

  • value (Object)

    the value to set the attribute lldp_address to.

Returns:

  • (Object)

    the newly set value



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_address=(value)
  @lldp_address = value
end

#lldp_address_typeObject

Getter for the LLDP address type.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_address_type
  @lldp_address_type
end

#lldp_capabiltyObject

Returns the value of attribute lldp_capabilty

Returns:

  • (Object)

    the current value of lldp_capabilty



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_capabilty
  @lldp_capabilty
end

#lldp_chassis_id=(i) ⇒ Object

Setter for the LLDP chassis id.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_chassis_id=(value)
  @lldp_chassis_id = value
end

#lldp_chassis_id_typeObject

Getter for the LLDP chassis id type.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_chassis_id_type
  @lldp_chassis_id_type
end

#lldp_enabled_capabilityObject

Returns the value of attribute lldp_enabled_capability

Returns:

  • (Object)

    the current value of lldp_enabled_capability



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_enabled_capability
  @lldp_enabled_capability
end

#lldp_interfaceObject

Getter for the LLDP interface type.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_interface
  @lldp_interface
end

#lldp_interface_typeObject

Getter for the LLDP interface type.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_interface_type
  @lldp_interface_type
end

#lldp_oidObject

Getter for the LLDP oid type.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_oid
  @lldp_oid
end

#lldp_port_descriptionObject

Getter for the LLDP port description.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_port_description
  @lldp_port_description
end

#lldp_port_idObject

Returns the value of attribute lldp_port_id

Returns:

  • (Object)

    the current value of lldp_port_id



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_port_id
  @lldp_port_id
end

#lldp_port_id_typeObject

Getter for the LLDP port id type.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_port_id_type
  @lldp_port_id_type
end

#lldp_system_descriptionObject

Getter for the LLDP system description.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_system_description
  @lldp_system_description
end

#lldp_system_nameObject

Getter for the LLDP system name.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_system_name
  @lldp_system_name
end

#lldp_ttlObject

Getter for the LLDP ttl.



5
6
7
# File 'lib/packetfu/protos/lldp/header.rb', line 5

def lldp_ttl
  @lldp_ttl
end

Instance Method Details

#lldp_address_type_readableObject



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/packetfu/protos/lldp/header.rb', line 223

def lldp_address_type_readable
  case lldp_address_type
  when 1
    "IPv4"
  when 2
    "IPv6"
  when 6
    "MAC"
  else
    lldp_address_type
  end
end

#lldp_capabilty_readableObject



236
237
238
# File 'lib/packetfu/protos/lldp/header.rb', line 236

def lldp_capabilty_readable
  "0x%04x" % lldp_capabilty
end

#lldp_chassis_id_readableObject

Getter for the LLDP chassis id .



127
128
129
130
131
132
133
# File 'lib/packetfu/protos/lldp/header.rb', line 127

def lldp_chassis_id_readable()
  if self[:lldp_chassis_id_type].to_i == 4
    return EthHeader.str2mac(self[:lldp_chassis_id].to_s)
  else
    return self[:lldp_chassis_id].to_s
  end
end

#lldp_enabled_capability_readableObject



240
241
242
# File 'lib/packetfu/protos/lldp/header.rb', line 240

def lldp_enabled_capability_readable
  "0x%04x" % lldp_enabled_capability
end

#lldp_port_id_readableObject

Getter for the LLDP port id.



141
142
143
144
145
146
147
148
# File 'lib/packetfu/protos/lldp/header.rb', line 141

def lldp_port_id_readable()
  #if mac addr
  if self[:lldp_port_id_type].to_i == 3
    return EthHeader.str2mac(self[:lldp_port_id].to_s)
  else
    return self[:lldp_port_id].to_s
  end
end

#lldp_saddr_ipObject Also known as: lldp_address

Get a more readable source IP address.



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/packetfu/protos/lldp/header.rb', line 209

def lldp_saddr_ip
  #ipv4 or ipv6
  if (self[:lldp_address_type].to_i == 1) or (self[:lldp_address_type].to_i == 2)
    begin
      IPAddr::ntop(self[:lldp_address])
    rescue
      self[:lldp_address]
    end
  elsif  self[:lldp_address_type].to_i == 6
    #mac
    EthHeader.str2mac(self[:lldp_address].to_s)
  end
end

#lldp_saddr_ip=(addr) ⇒ Object

Set a more readable source IP address.



203
204
205
206
# File 'lib/packetfu/protos/lldp/header.rb', line 203

def lldp_saddr_ip=(addr)
  self[:lldp_address_type] = 1
  self[:lldp_address].read_quad(addr)
end

#lldp_saddr_macObject Also known as: lldp_chassis_id

Get a more readable source MAC address.



198
199
200
# File 'lib/packetfu/protos/lldp/header.rb', line 198

def lldp_saddr_mac
  EthHeader.str2mac(self[:lldp_port_id].to_s)
end

#lldp_saddr_mac=(mac) ⇒ Object

Set the source MAC address in a more readable way.



151
152
153
154
155
156
# File 'lib/packetfu/protos/lldp/header.rb', line 151

def lldp_saddr_mac=(mac)
  mac = EthHeader.mac2str(mac)
  self[:lldp_port_id_type] = 3
  self[:lldp_port_id].read(mac)
  self.lldp_port_id
end

#read(str) ⇒ Object

Reads a string to populate the object.



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/packetfu/protos/lldp/header.rb', line 36

def read(str)
  force_binary(str)
  return self if str.nil?
  index = 0
  #check for lldp pdu end
  while (str[index,2] != "\x00\x00") && (index+2 < str.size)
    tlv_known = false
    #chassis subtype
    if str[index,1] == "\x02"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_chassis_id_type].read(str[index+2,1])
      self[:lldp_chassis_id].read(str[index+3, tlv_length - 1])
      index += tlv_length + 2
    end
    #port subtype
    if str[index,1] == "\x04"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_port_id_type].read(str[index+2,1])
      self[:lldp_port_id].read(str[index+3, tlv_length - 1])
      index += tlv_length + 2
    end
    #ttl subtype
    if str[index,1] == "\x06"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_ttl].read(str[index+2, tlv_length])
      index += tlv_length + 2
    end
    #port description
    if str[index,1] == "\x08"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_port_description].read(str[index+2, tlv_length])
      index += tlv_length + 2
    end
    #system name
    if str[index,1] == "\x0a"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_system_name].read(str[index+2, tlv_length])
      index += tlv_length + 2
    end
    #system description
    if str[index,1] == "\x0c"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_system_description].read(str[index+2, tlv_length])
      index += tlv_length + 2
    end
    #system capabilities
    if str[index,1] == "\x0e"
      tlv_known = true
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      self[:lldp_capabilty].read(str[index+2, 2])
      self[:lldp_enabled_capability].read(str[index+4, 2])
      index += tlv_length + 2
    end
    #management address
    if str[index,1] == "\x10"
      tlv_known = true
      tlv_length = str[index + 1,1].unpack("U*").join.to_i
      addr_length = str[index + 2, 1].unpack("U*").join.to_i
      self[:lldp_address_type].read(str[index + 3, 1])
      self[:lldp_address].read(str[index + 4,addr_length - 1])
      self[:lldp_interface_type].read(str[index + addr_length + 3, 1].unpack("U*").join.to_i)
      self[:lldp_interface].read(str[index + addr_length + 4, 4])
      oid_string_length = str[index + addr_length + 8, 1].unpack("U*").join.to_i
      if oid_string_length > 0
        self[:lldp_oid].read(str[index + addr_length + 9, oid_string_length])
      end
      index += tlv_length + 2
    end

    #if tlv type is unknown jump over it
    unless tlv_known
      tlv_length = str[index+1,1].unpack("U*").join.to_i
      index += tlv_length + 2
    end
  end
  self
end

#to_sObject

Returns the object in string form.



31
32
33
# File 'lib/packetfu/protos/lldp/header.rb', line 31

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