Class: PacketGen::PcapNG::SHB

Inherits:
Block show all
Defined in:
lib/packetgen/pcapng/shb.rb

Overview

SHB represents a Section Header Block (SHB) of a pcapng file.

SHB Definition

Int32   :type           Default: 0x0A0D0D0A
Int32   :block_len
Int32   :magic          Default: 0x1A2B3C4D  # :big is 0x4D3C2C1A
Int16   :ver_major      Default: 1
Int16   :ver_minor      Default: 0
Int64   :section_len
String  :options        Default: ''
Int32   :block_len2

Author:

  • Sylvain Daubert

Constant Summary collapse

MAGIC_INT32 =

Magic value to retrieve SHB

0x1A2B3C4D
MAGIC_LITTLE =

Magic value (little endian version)

[MAGIC_INT32].pack('V')
MAGIC_BIG =

Magic value (big endian version)

[MAGIC_INT32].pack('N')
MIN_SIZE =

Minimum SHB size

7 * 4
SECTION_LEN_UNDEFINED =

section_len value for undefined length

0xffffffff_ffffffff

Instance Attribute Summary collapse

Attributes inherited from Block

#block_len, #type

Instance Method Summary collapse

Methods inherited from Block

#options?, #pad_field, #recalc_block_len

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, update_field

Constructor Details

#initialize(options = {}) ⇒ SHB

Returns a new instance of SHB.

Parameters:

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

Options Hash (options):

  • :endian (:little, :big)

    set block endianness

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

    block total length

  • :magic (Integer)

    magic number to distinguish little endian sessions and big endian ones

  • :ver_major (Integer)

    number of the current major version of the format

  • :ver_minor (Integer)

    number of the current minor version of the format

  • :section_len (Integer)

    length of following section, excluding he SHB itself

  • :options (::String)
  • :block_len2 (Integer)

    block total length



80
81
82
83
84
85
86
87
# File 'lib/packetgen/pcapng/shb.rb', line 80

def initialize(options={})
  super
  @interfaces = []
  @unknown_blocks = []
  endianness(options[:endian] || :little)
  recalc_block_len
  self.type = options[:type] || PcapNG::SHB_TYPE.to_i
end

Instance Attribute Details

#endian:little, :big

Returns:

  • (:little, :big)


25
26
27
# File 'lib/packetgen/pcapng/shb.rb', line 25

def endian
  @endian
end

#interfacesArray<IDB> (readonly)

Get interfaces for this section

Returns:



28
29
30
# File 'lib/packetgen/pcapng/shb.rb', line 28

def interfaces
  @interfaces
end

#magicInteger

32-bit magic number

Returns:

  • (Integer)


48
# File 'lib/packetgen/pcapng/shb.rb', line 48

define_field_before :block_len2, :magic, Types::Int32, default: MAGIC_INT32

#optionsTypes::String

Returns:



64
# File 'lib/packetgen/pcapng/shb.rb', line 64

define_field_before :block_len2, :options, Types::String

#section_lenInteger

64-bit section length

Returns:

  • (Integer)


60
61
# File 'lib/packetgen/pcapng/shb.rb', line 60

define_field_before :block_len2, :section_len, Types::Int64,
default: SECTION_LEN_UNDEFINED

#unknown_blocksArray<UnknownBlock> (readonly)

Get unsupported blocks given in pcapng file as raw data

Returns:



31
32
33
# File 'lib/packetgen/pcapng/shb.rb', line 31

def unknown_blocks
  @unknown_blocks
end

#ver_majorInteger

16-bit minor version number

Returns:

  • (Integer)


52
# File 'lib/packetgen/pcapng/shb.rb', line 52

define_field_before :block_len2, :ver_major, Types::Int16, default: 1

Instance Method Details

#<<(idb) ⇒ self

Add a IDB to this section

Parameters:

Returns:

  • (self)


111
112
113
114
115
# File 'lib/packetgen/pcapng/shb.rb', line 111

def <<(idb)
  @interfaces << idb
  idb.section = self
  self
end

#add_unknown_block(block) ⇒ Object



128
129
130
131
# File 'lib/packetgen/pcapng/shb.rb', line 128

def add_unknown_block(block)
  self.unknown_blocks << block
  block.section = self
end

#read(str_or_io) ⇒ self

Reads a String or a IO to populate the object

Parameters:

  • str_or_io (::String, IO)

Returns:

  • (self)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/packetgen/pcapng/shb.rb', line 92

def read(str_or_io)
  io = to_io(str_or_io)
  return self if io.eof?

  self[:type].read check_shb(io)
  %i[block_len magic ver_major ver_minor section_len].each do |attr|
    self[attr].read io.read(self[attr].sz)
  end
  handle_magic_and_check(self[:magic].to_s)

  read_options(io)
  read_blocklen2_and_check(io)

  self
end

#to_sString

Return the object as a String

Returns:

  • (String)


119
120
121
122
123
124
125
126
# File 'lib/packetgen/pcapng/shb.rb', line 119

def to_s
  body = @interfaces.map(&:to_s).join
  self.section_len = body.size unless self.section_len == SECTION_LEN_UNDEFINED

  pad_field :options
  recalc_block_len
  super + body
end