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


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

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)

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

def endian
  @endian
end

16-bit link type

Returns:

  • (Integer)

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

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

#optionsTypes::String

Returns:


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

define_field_before :block_len2, :options, Types::String

#packetsArray<EPB,SPB>

Returns:


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

def packets
  @packets
end

#reservedInteger

16-bit reserved field

Returns:

  • (Integer)

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

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

#sectionSHB

Returns:


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

def section
  @section
end

#snaplenInteger

32-bit snap length

Returns:

  • (Integer)

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

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

Instance Method Details

#<<(xpb) ⇒ self

Add a xPB to this section

Parameters:

Returns:

  • (self)

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

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)

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

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)

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

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)

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

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