Class: PacketGen::PcapNG::IDB
- Inherits:
-
Block
- Object
- Types::Fields
- Block
- PacketGen::PcapNG::IDB
- 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
Constant Summary collapse
- MIN_SIZE =
Minimum IDB size
5 * 4
- OPTION_IF_TSRESOL =
Option code for if_tsresol option
9
Instance Attribute Summary collapse
- #endian ⇒ :little, :big
-
#link_type ⇒ Integer
16-bit link type.
- #options ⇒ Types::String
- #packets ⇒ Array<EPB,SPB>
-
#reserved ⇒ Integer
16-bit reserved field.
- #section ⇒ SHB
-
#snaplen ⇒ Integer
32-bit snap length.
Attributes inherited from Block
Instance Method Summary collapse
-
#<<(xpb) ⇒ self
Add a xPB to this section.
-
#initialize(options = {}) ⇒ IDB
constructor
A new instance of IDB.
-
#read(str_or_io) ⇒ self
Reads a String or a IO to populate the object.
-
#to_s ⇒ String
Return the object as a String.
-
#ts_resol(force: false) ⇒ Float
Give timestamp resolution for this interface.
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.
61 62 63 64 65 66 67 68 |
# File 'lib/packetgen/pcapng/idb.rb', line 61 def initialize(={}) super set_endianness([:endian] || :little) @packets = [] @options_decoded = false recalc_block_len self.type = [:type] || PcapNG::IDB_TYPE.to_i end |
Instance Attribute Details
#endian ⇒ :little, :big
29 30 31 |
# File 'lib/packetgen/pcapng/idb.rb', line 29 def endian @endian end |
#link_type ⇒ Integer
16-bit link type
38 |
# File 'lib/packetgen/pcapng/idb.rb', line 38 define_field_before :block_len2, :link_type, Types::Int16, default: 1 |
#options ⇒ Types::String
49 |
# File 'lib/packetgen/pcapng/idb.rb', line 49 define_field_before :block_len2, :options, Types::String |
#packets ⇒ Array<EPB,SPB>
33 34 35 |
# File 'lib/packetgen/pcapng/idb.rb', line 33 def packets @packets end |
Instance Method Details
#<<(xpb) ⇒ self
Add a xPB to this section
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
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_s ⇒ String
Return the object as a 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
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 = self[:options] while idx < .length opt_code, opt_len = [idx, 4].unpack("#{packstr}2") if opt_code == OPTION_IF_TSRESOL && opt_len == 1 tsresol = [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 |