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
  set_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)


96
97
98
99
# File 'lib/packetgen/pcapng/idb.rb', line 96

def <<(xpb)
  @packets << xpb
  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
85
86
87
88
89
90
91
# File 'lib/packetgen/pcapng/idb.rb', line 73

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?

  self[:type].read io.read(4)
  self[:block_len].read io.read(4)
  self[:link_type].read io.read(2)
  self[:reserved].read io.read(2)
  self[:snaplen].read io.read(4)
  self[:options].read io.read(self.block_len - MIN_SIZE)
  self[:block_len2].read io.read(4)

  check_len_coherency
  self
end

#to_sString

Return the object as a String

Returns:

  • (String)


136
137
138
139
140
# File 'lib/packetgen/pcapng/idb.rb', line 136

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)


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
# File 'lib/packetgen/pcapng/idb.rb', line 104

def ts_resol(force: false)
  if @options_decoded && !force
    @ts_resol
  else
    packstr = endian == :little ? 'v' : 'n'
    idx = 0
    options = self[:options]

    while idx < options.length
      opt_code, opt_len = options[idx, 4].unpack("#{packstr}2")
      if opt_code == OPTION_IF_TSRESOL && opt_len == 1
        tsresol = options[idx + 4, 1].unpack('C').first
        @ts_resol = if (tsresol & 0x80).zero?
                      10**-tsresol
                    else
                      2**-(tsresol & 0x7f)
                    end

        @options_decoded = true
        return @ts_resol
      else
        idx += 4 + opt_len
      end
    end

    @options_decoded = true
    @ts_resol = 1E-6 # default value
  end
end