Class: PacketFu::PcapNG::SPB

Inherits:
Struct
  • Object
show all
Includes:
Block, StructFu
Defined in:
lib/packetfu/pcapng/spb.rb

Overview

Pcapng::SPB represents a Section Simple Packet Block (SPB) of a pcapng file.

Pcapng::SPB Definition

Int32   :type           Default: 0x00000003
Int32   :block_len
Int32   :orig_len
String  :data
Int32   :block_len2

Constant Summary collapse

MIN_SIZE =
4*4

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Block

#pad_field, #recalc_block_len

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ SPB

Returns a new instance of SPB.



22
23
24
25
26
27
# File 'lib/packetfu/pcapng/spb.rb', line 22

def initialize(args={})
  @endian = set_endianness(args[:endian] || :little)
  init_fields(args)
  super(args[:type], args[:block_len], args[:orig_len], args[:data],
        args[:block_len2])
end

Instance Attribute Details

#block_lenObject

Returns the value of attribute block_len

Returns:

  • (Object)

    the current value of block_len



14
15
16
# File 'lib/packetfu/pcapng/spb.rb', line 14

def block_len
  @block_len
end

#block_len2Object

Returns the value of attribute block_len2

Returns:

  • (Object)

    the current value of block_len2



14
15
16
# File 'lib/packetfu/pcapng/spb.rb', line 14

def block_len2
  @block_len2
end

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



14
15
16
# File 'lib/packetfu/pcapng/spb.rb', line 14

def data
  @data
end

#endianObject

Returns the value of attribute endian.



17
18
19
# File 'lib/packetfu/pcapng/spb.rb', line 17

def endian
  @endian
end

#interfaceObject

Returns the value of attribute interface.



18
19
20
# File 'lib/packetfu/pcapng/spb.rb', line 18

def interface
  @interface
end

#orig_lenObject

Returns the value of attribute orig_len

Returns:

  • (Object)

    the current value of orig_len



14
15
16
# File 'lib/packetfu/pcapng/spb.rb', line 14

def orig_len
  @orig_len
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



14
15
16
# File 'lib/packetfu/pcapng/spb.rb', line 14

def type
  @type
end

Instance Method Details

#has_options?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/packetfu/pcapng/spb.rb', line 39

def has_options?
  false
end

#init_fields(args = {}) ⇒ Object

Used by #initialize to set the initial fields



30
31
32
33
34
35
36
37
# File 'lib/packetfu/pcapng/spb.rb', line 30

def init_fields(args={})
  args[:type]  = @int32.new(args[:type] || PcapNG::SPB_TYPE.to_i)
  args[:block_len] = @int32.new(args[:block_len] || MIN_SIZE)
  args[:orig_len] = @int32.new(args[:orig_len] || 0)
  args[:data] = StructFu::String.new(args[:data] || '')
  args[:block_len2] = @int32.new(args[:block_len2] || MIN_SIZE)
  args
end

#read(str_or_io) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/packetfu/pcapng/spb.rb', line 43

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

  self[:type].read io.read(4)
  self[:block_len].read io.read(4)
  self[:orig_len].read io.read(4)
  # Take care of IDB snaplen
  # CAUTION: snaplen == 0 -> no capture limit
  if interface and interface.snaplen.to_i > 0
    data_len = [self[:orig_len].to_i, interface.snaplen.to_i].min
  else
    data_len = self[:orig_len].to_i
  end
  data_pad_len = (4 - (data_len % 4)) % 4
  self[:data].read io.read(data_len)
  io.read data_pad_len
  self[:block_len2].read io.read(4)

  unless self[:block_len].to_i == self[:block_len2].to_i
    raise InvalidFileError, 'Incoherency in Simple Packet Block'
  end

  self
end

#to_sObject

Return the object as a String



74
75
76
77
78
# File 'lib/packetfu/pcapng/spb.rb', line 74

def to_s
  pad_field :data
  recalc_block_len
  to_a.map(&:to_s).join
end