Class: PacketGen::PcapNG::IDB

Inherits:
Block show all
Defined in:
lib/packetgen/pcapng/idb.rb

Overview

IDB represents a Interface Description Block (IDB) of a pcapng file.

IDB Definition

Int32   :type           Default: 0x00000001
Int32   :block_len
Int16   :link_type      Default: 1
Int16   :reserved       Default: 0
Int64   :snaplen        Default: 0 (no limit)
String  :options
Int32   :block_len2

Author:

  • Sylvain Daubert

Constant Summary collapse

MIN_SIZE =

Minimum IDB size

5 * 4
OPTION_IF_TSRESOL =

Option code for if_tsresol option

9

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 = {}) ⇒ IDB

Returns a new instance of IDB.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :endian (:little, :big)

    set block endianness

  • :type (Integer)
  • :block_len (Integer)

    block total length

  • :link_type (Integer)
  • :reserved (Integer)
  • :snaplen (Integer)

    maximum number of octets captured from each packet

  • :options (::String)
  • :block_len2 (Integer)

    block total length



62
63
64
65
66
67
68
69
# File 'lib/packetgen/pcapng/idb.rb', line 62

def initialize(options={})
  super
  endianness(options[:endian] || :little)
  @packets = []
  @options_decoded = false
  recalc_block_len
  self.type = options[:type] || PcapNG::IDB_TYPE.to_i
end

Instance Attribute Details

#endian:little, :big

Returns:

  • (:little, :big)


30
31
32
# File 'lib/packetgen/pcapng/idb.rb', line 30

def endian
  @endian
end

16-bit link type

Returns:

  • (Integer)


39
# File 'lib/packetgen/pcapng/idb.rb', line 39

define_field_before :block_len2, :link_type, Types::Int16, default: 1

#optionsTypes::String

Returns:



50
# File 'lib/packetgen/pcapng/idb.rb', line 50

define_field_before :block_len2, :options, Types::String

#packetsArray<EPB,SPB>

Returns:



34
35
36
# File 'lib/packetgen/pcapng/idb.rb', line 34

def packets
  @packets
end

#reservedInteger

16-bit reserved field

Returns:

  • (Integer)


43
# File 'lib/packetgen/pcapng/idb.rb', line 43

define_field_before :block_len2, :reserved, Types::Int16, default: 0

#sectionSHB

Returns:



32
33
34
# File 'lib/packetgen/pcapng/idb.rb', line 32

def section
  @section
end

#snaplenInteger

32-bit snap length

Returns:

  • (Integer)


47
# File 'lib/packetgen/pcapng/idb.rb', line 47

define_field_before :block_len2, :snaplen, Types::Int32, default: 0

Instance Method Details

#<<(xpb) ⇒ self

Add a xPB to this section

Parameters:

Returns:

  • (self)


90
91
92
93
94
# File 'lib/packetgen/pcapng/idb.rb', line 90

def <<(xpb)
  @packets << xpb
  xpb.interface = self
  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)


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/packetgen/pcapng/idb.rb', line 74

def read(str_or_io)
  io = to_io(str_or_io)
  return self if io.eof?

  %i[type block_len link_type reserved snaplen].each do |attr|
    self[attr].read io.read(self[attr].sz)
  end
  self[:options].read io.read(self.block_len - MIN_SIZE)
  read_blocklen2_and_check(io)

  self
end

#to_sString

Return the object as a String

Returns:

  • (String)


109
110
111
112
113
# File 'lib/packetgen/pcapng/idb.rb', line 109

def to_s
  pad_field :options
  recalc_block_len
  super << @packets.map(&:to_s).join
end

#ts_resol(force: false) ⇒ Float

Give timestamp resolution for this interface

Parameters:

  • force (Boolean) (defaults to: false)

    if true, force decoding even if already done

Returns:

  • (Float)


99
100
101
102
103
104
105
# File 'lib/packetgen/pcapng/idb.rb', line 99

def ts_resol(force: false)
  if @options_decoded && !force
    @ts_resol
  else
    decode_ts_resol
  end
end