Class: PacketGen::PcapNG::UnknownBlock

Inherits:
Struct
  • Object
show all
Includes:
Block, StructFu
Defined in:
lib/packetgen/pcapng/unknown_block.rb

Overview

UnknownBlock is used to handle unsupported blocks of a pcapng file.

Constant Summary collapse

MIN_SIZE =

Minimum Iblock size

12

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Block

#pad_field, #recalc_block_len

Methods included from StructFu

#clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(options = {}) ⇒ UnknownBlock

Returns a new instance of UnknownBlock.

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :endian (:little, :big)

    set block endianness

  • :type (Integer)
  • :block_len (Integer)

    block total length

  • :body (::String)
  • :block_len2 (Integer)

    block total length



22
23
24
25
26
# File 'lib/packetgen/pcapng/unknown_block.rb', line 22

def initialize(options={})
  @endian = set_endianness(options[:endian] || :little)
  init_fields(options)
  super(options[:type], options[:block_len], options[:body], options[:block_len2])
end

Instance Attribute Details

#block_lenObject

Returns the value of attribute block_len

Returns:

  • (Object)

    the current value of block_len



5
6
7
# File 'lib/packetgen/pcapng/unknown_block.rb', line 5

def block_len
  @block_len
end

#block_len2Object

Returns the value of attribute block_len2

Returns:

  • (Object)

    the current value of block_len2



5
6
7
# File 'lib/packetgen/pcapng/unknown_block.rb', line 5

def block_len2
  @block_len2
end

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



5
6
7
# File 'lib/packetgen/pcapng/unknown_block.rb', line 5

def body
  @body
end

#endian:little, :big

Returns:

  • (:little, :big)


10
11
12
# File 'lib/packetgen/pcapng/unknown_block.rb', line 10

def endian
  @endian
end

#sectionSHB

Returns:



12
13
14
# File 'lib/packetgen/pcapng/unknown_block.rb', line 12

def section
  @section
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



5
6
7
# File 'lib/packetgen/pcapng/unknown_block.rb', line 5

def type
  @type
end

Instance Method Details

#has_options?false

Has this block option?

Returns:

  • (false)


42
43
44
# File 'lib/packetgen/pcapng/unknown_block.rb', line 42

def has_options?
  false
end

#init_fields(options = {}) ⇒ Hash

Used by #initialize to set the initial fields

Parameters:

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

Returns:

  • (Hash)

    return options

See Also:



32
33
34
35
36
37
38
# File 'lib/packetgen/pcapng/unknown_block.rb', line 32

def init_fields(options={})
  options[:type]  = @int32.new(options[:type] || 0)
  options[:block_len] = @int32.new(options[:block_len] || MIN_SIZE)
  options[:body] = StructFu::String.new(options[:body] || '')
  options[:block_len2] = @int32.new(options[:block_len2] || MIN_SIZE)
  options
end

#read(str_or_io) ⇒ self

Parameters:

  • str_or_io (::String, IO)

Returns:

  • (self)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/packetgen/pcapng/unknown_block.rb', line 49

def read(str_or_io)
  if str_or_io.respond_to? :read
    io = str_or_io
  else
    io = StringIO.new(force_binary(str_or_io.to_s))
  end
  return self if io.eof?

  self[:type].read io.read(4)
  self[:block_len].read io.read(4)
  self[:body].read io.read(self[:block_len].to_i - MIN_SIZE)
  self[:block_len2].read io.read(4)
  
  unless self[:block_len].to_i == self[:block_len2].to_i
    raise InvalidFileError, 'Incoherency in Header Block'
  end

  self
end

#to_sString

Return the object as a String

Returns:



71
72
73
74
75
# File 'lib/packetgen/pcapng/unknown_block.rb', line 71

def to_s
  pad_field :body
  recalc_block_len
  to_a.map(&:to_s).join
end