Class: PacketGen::Types::TLV Deprecated

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

Overview

Deprecated.

Use AbstractTLV instead.

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

Since:

  • 3.1.0 deprecated

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fieldable

#format_inspect, #sz, #to_s, #type_name

Methods inherited from Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

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.

  • :v (Class)

    String subclass for :value attribute. Default: String.

Since:

  • 3.1.0 deprecated


56
57
58
59
60
61
# File 'lib/packetgen/types/tlv.rb', line 56

def initialize(options={})
  Deprecation.deprecated_class(self.class, AbstractTLV)
  super
  initialize_types(options)
  initialize_values(options)
end

Instance Attribute Details

#lengthInteger

Returns:

  • (Integer)

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

define_field :length, Int8

#typeInteger

Returns:

  • (Integer)

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

define_field :type, Int8

#valueObject

Get value

Returns:

  • (Object)

    depend on value type

Since:

  • 3.1.0 deprecated


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

define_field :value, String

Instance Method Details

#human_typeString

Return human readable type, if TYPES is defined

Returns:

Since:

  • 3.1.0 deprecated


117
118
119
120
121
122
123
124
125
# File 'lib/packetgen/types/tlv.rb', line 117

def human_type
  if 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)

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

define_field :type, Int8

#read(str) ⇒ Fields

Populate object from a binary string

Parameters:

Returns:

Since:

  • 3.1.0 deprecated


66
67
68
69
70
71
72
73
74
# File 'lib/packetgen/types/tlv.rb', line 66

def read(str)
  idx = 0
  self[:type].read str[idx, self[:type].sz]
  idx += self[:type].sz
  self[:length].read str[idx, self[:length].sz]
  idx += self[:length].sz
  self[:value].read str[idx, self.length]
  self
end

#to_humanString

Returns:

Since:

  • 3.1.0 deprecated


128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/packetgen/types/tlv.rb', line 128

def to_human
  name = self.class.to_s.gsub(/.*::/, '')
  @typestr ||= if human_types?
                 types = self.class::TYPES.values
                 "%-#{types.max_by(&:length).size}s"
               else
                 '%s'
               end
  lenstr = "%-#{(2**self[:length].nbits - 1).to_s.size}u"
  "#{name} type:#{@typestr} length:#{lenstr} value:#{value.inspect}" % [human_type,
                                                                        length]
end