Class: PacketGen::Header::ASN1Base Abstract

Inherits:
RASN1::Model
  • Object
show all
Includes:
PacketGen::Headerable
Defined in:
lib/packetgen/header/asn1_base.rb

Overview

This class is abstract.

Base class for ASN.1 header types. This class implement minimal Base API to mimic a Base object.

Subclasses may define magic methods:

Author:

  • Sylvain Daubert

Since:

  • 2.0.0

Direct Known Subclasses

SNMP

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #parse?, #protocol_name

Class Method Details

.define_attributes(*attributes) ⇒ void

This method returns an undefined value.

Define some methods from given ASN.1 fields to mimic Base attributes

Parameters:

  • attributes (Array<Symbol>)

Since:

  • 2.0.0



27
28
29
30
31
32
33
# File 'lib/packetgen/header/asn1_base.rb', line 27

def define_attributes(*attributes)
  @attributes = attributes
  attributes.each do |attr|
    class_eval "def #{attr}; @elements[:#{attr}].value; end\n" \
               "def #{attr}=(v); @elements[:#{attr}].value = v; end"
  end
end

.known_headersObject

Since:

  • 2.0.0



35
36
37
# File 'lib/packetgen/header/asn1_base.rb', line 35

def known_headers
  @known_headers ||= {}.freeze
end

Instance Method Details

#inspectString

Common inspect method for ASN.1 headers

Returns:

  • (String)

Since:

  • 2.0.0



57
58
59
60
61
62
63
# File 'lib/packetgen/header/asn1_base.rb', line 57

def inspect
  str = Inspect.dashed_line(self.class, 1)
  self.class.class_eval { @attributes }.each do |attr|
    str << Inspect.inspect_asn1_attribute(attr, self[attr], 1)
  end
  str
end

#read(str) ⇒ ASN1Base

Read a BER string

Parameters:

  • str (String)

Returns:

Since:

  • 2.0.0



46
47
48
49
50
51
52
53
# File 'lib/packetgen/header/asn1_base.rb', line 46

def read(str)
  begin
    parse(str, ber: true)
  rescue RASN1::ASN1Error
    # suppress exception to allow guessing
  end
  self
end