class PacketGen::PcapNG::SHB
{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
Constants
Attributes
@return [:little, :big]
Get interfaces for this section @return [Array<IDB>]
Get unsupported blocks given in pcapng file as raw data @return [Array<UnknownBlock>]
Public Class Methods
@param [Hash] options @option options [:little, :big] :endian set block endianness @option options [Integer] :type @option options [Integer] :block_len block total length @option options [Integer] :magic magic number to distinguish little endian
sessions and big endian ones
@option options [Integer] :ver_major number of the current major version of
the format
@option options [Integer] :ver_minor number of the current minor version of
the format
@option options [Integer] :section_len length of following section, excluding
he SHB itself
@option options [::String] :options @option options [Integer] :block_len2 block total length
PacketGen::PcapNG::Block::new
# 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
Public Instance Methods
Add a IDB
to this section @param [IDB] idb @return [self]
# File lib/packetgen/pcapng/shb.rb, line 110 def <<(idb) @interfaces << idb idb.section = self self end
# File lib/packetgen/pcapng/shb.rb, line 127 def add_unknown_block(block) self.unknown_blocks << block block.section = self end
Reads a String or a IO to populate the object @param [::String,IO] str_or_io @return [self]
# 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
Return the object as a String @return [String]
# 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
Private Instance Methods
Check io contains a SHB
@param [IO] io @return [String] type string
# File lib/packetgen/pcapng/shb.rb, line 148 def check_shb(io) type_str = io.read(4) return type_str if type_str == PcapNG::SHB_TYPE.to_s type = type_str.unpack('H*').join raise InvalidFileError, "Incorrect type (#{type})for Section Header Block" end
# File lib/packetgen/pcapng/shb.rb, line 134 def force_endianness(endian) @endian = endian %i[type block_len magic block_len2].each do |attr| self[attr] = Types::Int32.new(0, endian).read(self[attr].to_s) end %i[ver_major ver_minor].each do |attr| self[attr] = Types::Int16.new(0, endian).read(self[attr].to_s) end self[:section_len] = Types::Int64.new(0, endian).read(self[:section_len].to_s) end
# File lib/packetgen/pcapng/shb.rb, line 156 def handle_magic_and_check(magic_str) case magic_str when MAGIC_LITTLE force_endianness :little if endian == :big when MAGIC_BIG force_endianness :big if endian == :little else raise InvalidFileError, 'Incorrect magic for Section Header Block' end end
# File lib/packetgen/pcapng/shb.rb, line 167 def read_options(io) self[:options].read io.read(self.block_len - MIN_SIZE) end