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 = []
  set_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)


143
144
145
146
# File 'lib/packetgen/pcapng/shb.rb', line 143

def <<(idb)
  @interfaces << idb
  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/packetgen/pcapng/shb.rb', line 91

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

  type_str = io.read(4)
  unless type_str == PcapNG::SHB_TYPE.to_s
    type = type_str.unpack('H*').join
    raise InvalidFileError, "Incorrect type (#{type})for Section Header Block"
  end

  block_len_str = io.read(4)

  magic_str = io.read(4)
  case @endian
  when :little
    case magic_str
    when MAGIC_LITTLE
    when MAGIC_BIG
      force_endianness :big
    else
      raise InvalidFileError, 'Incorrect magic for Section Header Block'
    end
  when :big
    case magic_str
    when MAGIC_BIG
    when MAGIC_LITTLE
      force_endianness :little
    else
      raise InvalidFileError, 'Incorrect magic for Section Header Block'
    end
  end

  self[:type].read type_str
  self[:block_len].read block_len_str
  self[:magic].read magic_str
  self[:ver_major].read io.read(2)
  self[:ver_minor].read io.read(2)
  self[:section_len].read io.read(8)
  self[:options].read io.read(self.block_len - MIN_SIZE)
  self[:block_len2].read io.read(4)

  check_len_coherency
  self
end

#to_sString

Return the object as a String

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
# File 'lib/packetgen/pcapng/shb.rb', line 150

def to_s
  body = @interfaces.map(&:to_s).join
  unless self.section_len == SECTION_LEN_UNDEFINED
    self.section_len = body.size
  end
  pad_field :options
  recalc_block_len
  super + body
end