Class: PacketGen::Types::TLV Deprecated
- Includes:
- Fieldable
- Defined in:
- lib/packetgen/types/tlv.rb
Overview
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:
-
print human readable type using #human_type,
-
set type as String with #type=.
Instance Attribute Summary collapse
- #length ⇒ Integer
- #type ⇒ Integer
-
#value ⇒ Object
Get
value
.
Instance Method Summary collapse
-
#human_type ⇒ String
Return human readable type, if TYPES is defined.
-
#initialize(options = {}) ⇒ TLV
constructor
A new instance of TLV.
- #old_type= ⇒ Integer
-
#read(str) ⇒ Fields
Populate object from a binary string.
- #to_human ⇒ String
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.
57 58 59 60 61 62 |
# File 'lib/packetgen/types/tlv.rb', line 57 def initialize(={}) Deprecation.deprecated_class(self.class, AbstractTLV) super initialize_types() initialize_values() end |
Instance Attribute Details
#value ⇒ Object
Get value
45 |
# File 'lib/packetgen/types/tlv.rb', line 45 define_field :value, String |
Instance Method Details
#human_type ⇒ String
Return human readable type, if TYPES is defined
118 119 120 121 122 123 124 125 126 |
# File 'lib/packetgen/types/tlv.rb', line 118 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 |
#read(str) ⇒ Fields
Populate object from a binary string
67 68 69 70 71 72 73 74 75 |
# File 'lib/packetgen/types/tlv.rb', line 67 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_human ⇒ String
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/packetgen/types/tlv.rb', line 129 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 |