Class: PacketGen::Header::ASN1Base Abstract

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

Overview

This class is abstract.

Base class for ASN.1 header types. Subclasses may define magic methods:

Author:

  • Sylvain Daubert

Direct Known Subclasses

SNMP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#packetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reference on packet which owns this header



18
19
20
# File 'lib/packetgen/header/asn1_base.rb', line 18

def packet
  @packet
end

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>)


30
31
32
33
34
35
36
# File 'lib/packetgen/header/asn1_base.rb', line 30

def self.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

.protocol_nameString

Give protocol name for this class

Returns:

  • (String)

Since:

  • 2.0.0



23
24
25
# File 'lib/packetgen/header/asn1_base.rb', line 23

def self.protocol_name
  self.new.protocol_name
end

Instance Method Details

#inspectString

Common inspect method for ASN.1 headers

Returns:

  • (String)


77
78
79
80
81
82
83
# File 'lib/packetgen/header/asn1_base.rb', line 77

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

#method_nameString

return header method name

Returns:

  • (String)

Since:

  • 2.0.0



53
54
55
56
57
# File 'lib/packetgen/header/asn1_base.rb', line 53

def method_name
  return @method_name if @method_name

  @method_name = protocol_name.downcase.sub(/::/, '_')
end

#parse?true

Returns:

  • (true)


60
61
62
# File 'lib/packetgen/header/asn1_base.rb', line 60

def parse?
  true
end

#protocol_nameString

Return header protocol name

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
# File 'lib/packetgen/header/asn1_base.rb', line 40

def protocol_name
  return @protocol_name if @protocol_name

  classname = self.class.to_s
  @protocol_name = if classname.start_with?('PacketGen::Header')
                     classname.sub(/.*Header::/, '')
                   else
                     classname.sub(/.*::/, '')
                   end
end

#read(str) ⇒ ASN1Base

Read a BER string

Parameters:

  • str (String)

Returns:



70
71
72
73
# File 'lib/packetgen/header/asn1_base.rb', line 70

def read(str)
  parse(str, ber: true)
  self
end