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


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

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)

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

def endian
  @endian
end

#interfacesArray<IDB> (readonly)

Get interfaces for this section

Returns:


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

def interfaces
  @interfaces
end

#magicInteger

32-bit magic number

Returns:

  • (Integer)

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

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

#optionsTypes::String

Returns:


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

define_field_before :block_len2, :options, Types::String

#section_lenInteger

64-bit section length

Returns:

  • (Integer)

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

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:


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

def unknown_blocks
  @unknown_blocks
end

#ver_majorInteger

16-bit minor version number

Returns:

  • (Integer)

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

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)

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

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

#add_unknown_block(block) ⇒ Object


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

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)

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

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)

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

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