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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#packetPacket?

Reference on packet which owns this header

Returns:

Since:

  • 2.0.0



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

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

Since:

  • 2.0.0



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

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



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

def self.protocol_name
  return @protocol_name if defined? @protocol_name

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

Instance Method Details

#added_to_packet(packet) ⇒ void

This method is abstract.

This method is called when a header is added to a packet. This base method does nothing but may be overriden by subclasses.

This method returns an undefined value.

Parameters:

  • packet (Packet)

    packet to which self is added

Since:

  • 2.1.4



87
# File 'lib/packetgen/header/asn1_base.rb', line 87

def added_to_packet(packet) end

#inspectString

Common inspect method for ASN.1 headers

Returns:

  • (String)

Since:

  • 2.0.0



102
103
104
105
106
107
108
# File 'lib/packetgen/header/asn1_base.rb', line 102

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

#method_nameString

return header method name

Returns:

  • (String)

Since:

  • 2.0.0



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

def method_name
  return @method_name if defined? @method_name

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

#parse?true

Called by Packet#parse when guessing first header to check if header is correct

Returns:

  • (true)

Since:

  • 2.0.0



67
68
69
# File 'lib/packetgen/header/asn1_base.rb', line 67

def parse?
  true
end

#protocol_nameString

Return header protocol name

Returns:

  • (String)

Since:

  • 2.0.0



51
52
53
# File 'lib/packetgen/header/asn1_base.rb', line 51

def protocol_name
  self.class.protocol_name
end

#read(str) ⇒ ASN1Base

Read a BER string

Parameters:

  • str (String)

Returns:

Since:

  • 2.0.0



95
96
97
98
# File 'lib/packetgen/header/asn1_base.rb', line 95

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