lib/packetgen/pcapng/shb.rb in packetgen-1.2.0 vs lib/packetgen/pcapng/shb.rb in packetgen-1.3.0

- old
+ new

@@ -15,14 +15,11 @@ # Int16 :ver_major Default: 1 # Int16 :ver_minor Default: 0 # Int64 :section_len # String :options Default: '' # Int32 :block_len2 - class SHB < Struct.new(:type, :block_len, :magic, :ver_major, :ver_minor, - :section_len, :options, :block_len2) - include StructFu - include Block + class SHB < Block # @return [:little, :big] attr_accessor :endian # Get interfaces for this section # @return [Array<IDB>] @@ -41,10 +38,31 @@ # Minimum SHB size MIN_SIZE = 7*4 # +section_len+ value for undefined length SECTION_LEN_UNDEFINED = 0xffffffff_ffffffff + # @!attribute magic + # 32-bit magic number + # @return [Integer] + define_field_before :block_len2, :magic, Types::Int32, default: MAGIC_INT32 + # @!attribute ver_major + # 16-bit major version number + # @return [Integer] + define_field_before :block_len2, :ver_major, Types::Int16, default: 1 + # @!attribute ver_major + # 16-bit minor version number + # @return [Integer] + define_field_before :block_len2, :ver_minor, Types::Int16, default: 0 + # @!attribute section_len + # 64-bit section length + # @return [Integer] + define_field_before :block_len2, :section_len, Types::Int64, + default: SECTION_LEN_UNDEFINED + # @!attribute options + # @return [Types::String] + define_field_before :block_len2, :options, Types::String + # @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 @@ -56,34 +74,18 @@ # @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 def initialize(options={}) - @endian = set_endianness(options[:endian] || :little) + super @interfaces = [] @unknown_blocks = [] - init_fields(options) - super(options[:type], options[:block_len], options[:magic], options[:ver_major], - options[:ver_minor], options[:section_len], options[:options], options[:block_len2]) + set_endianness(options[:endian] || :little) + recalc_block_len + self.type = options[:type] || PcapNG::SHB_TYPE.to_i end - # Used by {#initialize} to set the initial fields - # @see #initialize possible options - # @param [Hash] options - # @return [Hash] return +options+ - def init_fields(options={}) - options[:type] = @int32.new(options[:type] || PcapNG::SHB_TYPE.to_i) - options[:block_len] = @int32.new(options[:block_len] || MIN_SIZE) - options[:magic] = @int32.new(options[:magic] || MAGIC_INT32) - options[:ver_major] = @int16.new(options[:ver_major] || 1) - options[:ver_minor] = @int16.new(options[:ver_minor] || 0) - options[:section_len] = @int64.new(options[:section_len] || SECTION_LEN_UNDEFINED) - options[:options] = StructFu::String.new(options[:options] || '') - options[:block_len2] = @int32.new(options[:block_len2] || MIN_SIZE) - options - end - # Reads a String or a IO to populate the object # @param [::String,IO] str_or_io # @return [self] def read(str_or_io) if str_or_io.respond_to? :read @@ -128,14 +130,11 @@ self[:ver_minor].read io.read(2) self[:section_len].read io.read(8) self[:options].read io.read(self[:block_len].to_i - MIN_SIZE) self[:block_len2].read io.read(4) - unless self[:block_len].to_i == self[:block_len2].to_i - raise InvalidFileError, 'Incoherency in Section Header Block' - end - + check_len_coherency self end # Add a IDB to this section # @param [IDB] idb @@ -152,27 +151,24 @@ unless self[:section_len].to_i == SECTION_LEN_UNDEFINED self.section_len.value = body.size end pad_field :options recalc_block_len - to_a.map(&:to_s).join + body + fields.map { |f| @fields[f].to_s }.join + body end private def force_endianness(endian) - set_endianness endian @endian = endian - self[:type] = @int32.new(self[:type].to_i) - self[:block_len] = @int32.new(self[:block_len].to_i) - self[:magic] = @int32.new(self[:magic].to_i) - self[:ver_major] = @int16.new(self[:ver_major].to_i) - self[:ver_minor] = @int16.new(self[:ver_minor].to_i) - self[:section_len] = @int64.new(self[:section_len].to_i) - self[:block_len2] = @int32.new(self[:block_len2].to_i) + self[:type] = Types::Int32.new(self[:type].to_i, endian) + self[:block_len] = Types::Int32.new(self[:block_len].to_i, endian) + self[:magic] = Types::Int32.new(self[:magic].to_i, endian) + self[:ver_major] = Types::Int16.new(self[:ver_major].to_i, endian) + self[:ver_minor] = Types::Int16.new(self[:ver_minor].to_i, endian) + self[:section_len] = Types::Int64.new(self[:section_len].to_i, endian) + self[:block_len2] = Types::Int32.new(self[:block_len2].to_i, endian) end - end - end end