Class: PacketGen::Header::Base Abstract

Inherits:
Types::Fields show all
Defined in:
lib/packetgen/header/base.rb

Overview

This class is abstract.

Base class for all header types

Defined Under Namespace

Classes: Binding, Bindings

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Types::Fields

#[], #[]=, #body=, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, #force_binary, #initialize, #inspect, #read, #sz, #to_h, #to_s

Constructor Details

This class inherits a constructor from PacketGen::Types::Fields

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



140
141
142
# File 'lib/packetgen/header/base.rb', line 140

def packet
  @packet
end

Class Method Details

.bind_header(header_klass, args = {}) ⇒ void

This method returns an undefined value.

Bind a upper header to current class

Header1.bind_header Header2, field1: 43
Header1.bind_header Header2, field1: 43, field2: 43
Header1.bind_header Header2, op: :and, field1: 43, field2: 43
Header1.bind_header Header2, field1: ->(v) { v.nil? ? 128 : v > 127 }

Parameters:

  • header_klass (Class)

    header class to bind to current class

  • args (Hash) (defaults to: {})

    current class fields and their value when header_klass is embedded in current class. Given value may be a lambda, whose alone argument is the value extracted from header field (or nil when lambda is used to set field while adding a header).

    If multiple fields are given, a special key :op may be given to set parse operation on this binding. By default, :op is :or (at least one binding must match to parse it). It also may be set to :and (all bindings must match to parse it).



166
167
168
169
170
171
172
173
# File 'lib/packetgen/header/base.rb', line 166

def self.bind_header(header_klass, args={})
  op = args.delete(:op) || :or
  bindings = Bindings.new(op)
  @known_headers[header_klass] = bindings
  args.each do |key, value|
    bindings << Binding.new(key, value)
  end
end

.inherited(klass) ⇒ void

This method returns an undefined value.

On inheritage, create @known_headers class variable

Parameters:

  • klass (Class)


145
146
147
148
# File 'lib/packetgen/header/base.rb', line 145

def self.inherited(klass)
  super
  klass.class_eval { @known_headers = {} }
end

.known_headersHash

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.

Get knwon headers

Returns:

  • (Hash)

    keys: header classes, values: hashes



178
179
180
# File 'lib/packetgen/header/base.rb', line 178

def self.known_headers
  @known_headers
end

Instance Method Details

#header_id(header) ⇒ Integer

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.

Get header id in packet headers array

Parameters:

Returns:

  • (Integer)

Raises:

  • FormatError header not in a packet



201
202
203
204
205
206
207
208
# File 'lib/packetgen/header/base.rb', line 201

def header_id(header)
  raise FormatError, "header of type #{header.class} not in a packet" if packet.nil?
  id = packet.headers.index(header)
  if id.nil?
    raise FormatError, "header of type #{header.class} not in packet #{packet}"
  end
  id
end

#ip_header(header) ⇒ Header

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.

Get IP or IPv6 previous header from header

Parameters:

Returns:

Raises:

  • FormatError no IP or IPv6 header previous header in packet

  • FormatError header not in a packet



216
217
218
219
220
221
# File 'lib/packetgen/header/base.rb', line 216

def ip_header(header)
  hid = header_id(header)
  iph = packet.headers[0...hid].reverse.find { |h| h.is_a? IP or h.is_a? IPv6 }
  raise FormatError, 'no IP or IPv6 header in packet' if iph.nil?
  iph
end

#parse?Boolean

This method is abstract.

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

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

Returns:

  • (Boolean)


192
193
194
# File 'lib/packetgen/header/base.rb', line 192

def parse?
  true
end

#protocol_nameString

Return header protocol name

Returns:

  • (String)


184
185
186
# File 'lib/packetgen/header/base.rb', line 184

def protocol_name
  self.class.to_s.sub(/.*::/, '')
end