Class: PacketGen::Types::TLV

Inherits:
Fields
  • Object
show all
Defined in:
lib/packetgen/types/tlv.rb

Overview

Class to handle Type-Length-Value attributes

TLV fields handles three subfields:

  • a tag/type field (defaults: Int8 type),

  • a length field (defaults: Int8 type),

  • a value field (defaults: String type).

#initialize supports options to change tag and length type. By example, to declare a TLV using Int16:

define_field :tlv, PacketGen::Types::TLV, builder: ->(obj) { PacketGen::Types::TLV.new(t: PacketGen::Types::Int16, l: PacketGen::Types::Int16) }

Subclasses

A subclass may defined a constant hash TYPES. This hash defines human readable types versus their integer values. Hash keys are integer values, and hash values are types as strings.

If TYPES is defined, a subclass may:

Author:

  • Sylvain Daubert

Direct Known Subclasses

Header::DNS::Option, Header::Dot11::Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Fields

#[], #[]=, #body=, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, #force_binary, inherited, #inspect, #read, #sz, #to_h, #to_s

Constructor Details

#initialize(options = {}) ⇒ TLV

Returns a new instance of TLV.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :type (Integer)
  • :length (Integer)
  • :value (String)
  • :t (Class)

    Int subclass for :type attribute. Default: Int8.

  • :l (Class)

    Int subclass for :length attribute. Default: Int8.



44
45
46
47
48
49
50
51
52
# File 'lib/packetgen/types/tlv.rb', line 44

def initialize(options={})
  super
  self[:type] = options[:t].new(type) if options[:t]
  self[:length] = options[:l].new(length) if options[:l]
  self[:value] = String.new('', length_from: self[:length])
  self.type = options[:type] if options[:type]
  self.value = options[:value] if options[:value]
  self.length = options[:length] if options[:length]
end

Instance Attribute Details

#lengthInteger

Returns:

  • (Integer)


30
# File 'lib/packetgen/types/tlv.rb', line 30

define_field :length, Int8

#typeInteger

Returns:

  • (Integer)


27
# File 'lib/packetgen/types/tlv.rb', line 27

define_field :type, Int8

#valueString

Returns:



33
34
# File 'lib/packetgen/types/tlv.rb', line 33

define_field :value, String,
builder: ->(tlv) { String.new('', length_from: tlv[:length]) }

Instance Method Details

#human_typeString

Return human readable type, if TYPES is defined

Returns:



87
88
89
90
91
92
93
94
95
# File 'lib/packetgen/types/tlv.rb', line 87

def human_type
  if has_human_types?
    htype = self.class::TYPES[type]
    htype = type if htype.nil?
    htype.to_s
  else
    type.to_s
  end
end

#old_type=Integer

Returns:

  • (Integer)


55
# File 'lib/packetgen/types/tlv.rb', line 55

define_field :type, Int8

#to_humanString

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/packetgen/types/tlv.rb', line 98

def to_human
  name = self.class.to_s.gsub(/.*::/, '')
  @typestr ||= if has_human_types?
                 types = self.class::TYPES.values
                 "%-#{types.max { |a,b| a.length <=> b.length }.size}s"
               else
                 "%s"
               end
  @lenstr ||= "%-#{(2**(self[:length].width*8) - 1).to_s.size}u"
  "#{name} type:#@typestr length:#@lenstr value:#{value.inspect}" % [human_type,
                                                                     length]
end