Module: PacketGen::Headerable

Included in:
PacketGen::Header::ASN1Base, PacketGen::Header::Base
Defined in:
lib/packetgen/headerable.rb

Overview

This mixin module defines minimal API for a class to act as a header in Packet.

Some others methods may optionally be defined by a Headerable object:

  • #calc_length to calculate length from content. It should be defined if header as a length attribute.

  • #calc_checksum to calculate checksum. It should be defined if header as a checksum attriibute.

  • #reply to invert fields in header for a reply.

Author:

  • Sylvain Daubert

  • LemonTree55

Since:

  • 3.0.2

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ void

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.

This method returns an undefined value.

Extend klass with ClassMethods.

Parameters:

  • klass (Class)

Since:

  • 3.0.2



41
42
43
# File 'lib/packetgen/headerable.rb', line 41

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#added_to_packet(packet) ⇒ void

This method is abstract.

This base method does nothing but may be overriden by subclasses.

This method returns an undefined value.

This method is called when a header is added to a packet.

Parameters:

  • packet (Packet)

    packet to which self is added

Since:

  • 3.0.2



87
# File 'lib/packetgen/headerable.rb', line 87

def added_to_packet(packet) end

#method_nameString

return header method name

Returns:

  • (String)

Since:

  • 3.0.2



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

def method_name
  return @method_name if defined? @method_name

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

#packetPacket?

Reference on packet which owns this header

Returns:

Since:

  • 3.0.2



69
70
71
# File 'lib/packetgen/headerable.rb', line 69

def packet
  @packet ||= nil
end

#packet=(packet) ⇒ Packet

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.

Set packet to which this header belongs to

Parameters:

Returns:

Since:

  • 3.0.2



77
78
79
80
81
# File 'lib/packetgen/headerable.rb', line 77

def packet=(packet)
  @packet = packet
  added_to_packet(packet)
  @packet
end

#parse?Boolean

This method is abstract.

Should be redefined by subclasses. This method should check invariant attributes from header.

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

Returns:

  • (Boolean)

Since:

  • 3.0.2



63
64
65
# File 'lib/packetgen/headerable.rb', line 63

def parse?
  true
end

#protocol_nameString

Return header protocol name

Returns:

  • (String)

Since:

  • 3.0.2



47
48
49
# File 'lib/packetgen/headerable.rb', line 47

def protocol_name
  self.class.protocol_name
end

#read(str) ⇒ self

This method is abstract.

This method MUST be redefined by subclasses.

Populate headerable object from a binary string.

Parameters:

  • str (String)

Returns:

  • (self)

Raises:

  • (NotImplementedError)

Since:

  • 3.0.2



94
95
96
97
98
99
# File 'lib/packetgen/headerable.rb', line 94

def read(str)
  # Do not call super and rescue NoMethodError: too slow
  raise NotImplementedError, "#{self.class} should implement #read" if method(:read).super_method.nil?

  super
end

#to_sString

This method is abstract.

This method MUST be redefined by subclasses.

Generate binary string from header

Returns:

  • (String)

Raises:

  • (NotImplementedError)

Since:

  • 3.0.2



105
106
107
108
109
# File 'lib/packetgen/headerable.rb', line 105

def to_s
  raise NotImplementedError, "#{self.class} should implement #to_s" if method(:to_s).super_method.nil?

  super
end