Class: PacketGen::PcapNG::SHB
- Inherits:
-
Block
- Object
- Types::Fields
- Block
- PacketGen::PcapNG::SHB
- 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
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_lenvalue for undefined length 0xffffffff_ffffffff
Instance Attribute Summary collapse
- #endian ⇒ :little, :big
-
#interfaces ⇒ Array<IDB>
readonly
Get interfaces for this section.
-
#magic ⇒ Integer
32-bit magic number.
- #options ⇒ Types::String
-
#section_len ⇒ Integer
64-bit section length.
-
#unknown_blocks ⇒ Array<UnknownBlock>
readonly
Get unsupported blocks given in pcapng file as raw data.
-
#ver_major ⇒ Integer
16-bit minor version number.
Attributes inherited from Block
Instance Method Summary collapse
-
#<<(idb) ⇒ self
Add a IDB to this section.
-
#initialize(options = {}) ⇒ SHB
constructor
A new instance of SHB.
-
#read(str_or_io) ⇒ self
Reads a String or a IO to populate the object.
-
#to_s ⇒ String
Return the object as a String.
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
Instance Attribute Details
#endian ⇒ :little, :big
24 25 26 |
# File 'lib/packetgen/pcapng/shb.rb', line 24 def endian @endian end |
#interfaces ⇒ Array<IDB> (readonly)
Get interfaces for this section
27 28 29 |
# File 'lib/packetgen/pcapng/shb.rb', line 27 def interfaces @interfaces end |
#magic ⇒ Integer
32-bit magic number
47 |
# File 'lib/packetgen/pcapng/shb.rb', line 47 define_field_before :block_len2, :magic, Types::Int32, default: MAGIC_INT32 |
#options ⇒ Types::String
63 |
# File 'lib/packetgen/pcapng/shb.rb', line 63 define_field_before :block_len2, :options, Types::String |
#section_len ⇒ Integer
64-bit section length
59 60 |
# File 'lib/packetgen/pcapng/shb.rb', line 59 define_field_before :block_len2, :section_len, Types::Int64, default: SECTION_LEN_UNDEFINED |
#unknown_blocks ⇒ Array<UnknownBlock> (readonly)
Get unsupported blocks given in pcapng file as raw data
30 31 32 |
# File 'lib/packetgen/pcapng/shb.rb', line 30 def unknown_blocks @unknown_blocks end |
Instance Method Details
#<<(idb) ⇒ self
Add a IDB to this section
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
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_s ⇒ String
Return the object as a 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 |