Class: PacketGen::PcapNG::EPB

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

Overview

EPB represents a Enhanced Packet Block (EPB) of a pcapng file.

EPB Definition

Int32   :type           Default: 0x00000006
Int32   :block_len
Int32   :interface_id
Int32   :tsh (timestamp high)
Int32   :tsl (timestamp low)
Int32   :cap_len
Int32   :orig_len
String  :data
String  :options
Int32   :block_len2

Author:

  • Sylvain Daubert

Constant Summary collapse

MIN_SIZE =

Minimum EPB size

8 * 4

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

Returns a new instance of EPB.

Parameters:

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

Options Hash (options):

  • :endian (:little, :big)

    set block endianness

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

    block total length

  • :interface_id (Integer)

    specifies the interface this packet comes from

  • :tsh (Integer)

    timestamp (high nibbles)

  • :tsl (Integer)

    timestamp (low nibbles)

  • :cap_len (Integer)

    number of octets captured from the packet

  • :orig_len (Integer)

    actual length of the packet when it was transmitted on the network

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

    block total length



74
75
76
77
78
79
# File 'lib/packetgen/pcapng/epb.rb', line 74

def initialize(options={})
  super
  endianness(options[:endian] || :little)
  recalc_block_len
  self.type = options[:type] || PcapNG::EPB_TYPE.to_i
end

Instance Attribute Details

#cap_lenInteger

32-bit capture length

Returns:

  • (Integer)


48
# File 'lib/packetgen/pcapng/epb.rb', line 48

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

#dataTypes::String

Returns:



55
# File 'lib/packetgen/pcapng/epb.rb', line 55

define_field_before :block_len2, :data, Types::String

#endian:little, :big

Returns:

  • (:little, :big)


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

def endian
  @endian
end

#interfaceIPB

Returns:

  • (IPB)


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

def interface
  @interface
end

#interface_idInteger

32-bit interface ID

Returns:

  • (Integer)


36
# File 'lib/packetgen/pcapng/epb.rb', line 36

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

#optionsTypes::String

Returns:



58
# File 'lib/packetgen/pcapng/epb.rb', line 58

define_field_before :block_len2, :options, Types::String

#orig_lenInteger

32-bit original length

Returns:

  • (Integer)


52
# File 'lib/packetgen/pcapng/epb.rb', line 52

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

#tshInteger

high 32-bit timestamp value

Returns:

  • (Integer)


40
# File 'lib/packetgen/pcapng/epb.rb', line 40

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

#tslInteger

low 32-bit imestamp value

Returns:

  • (Integer)


44
# File 'lib/packetgen/pcapng/epb.rb', line 44

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

Instance Method Details

#read(str_or_io) ⇒ self

Reads a String or a IO to populate the object

Parameters:

  • str_or_io (::String, IO)

Returns:

  • (self)


84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/packetgen/pcapng/epb.rb', line 84

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

  %i[type block_len interface_id tsh tsl cap_len orig_len].each do |attr|
    self[attr].read io.read(self[attr].sz)
  end
  self[:data].read io.read(self.cap_len)
  read_options(io)
  read_blocklen2_and_check(io)

  self
end

#timestampTime

Return timestamp as a Time object

Returns:

  • (Time)


100
101
102
# File 'lib/packetgen/pcapng/epb.rb', line 100

def timestamp
  Time.at((self.tsh << 32 | self.tsl) * ts_resol)
end

#timestamp=(time) ⇒ Time

Set timestamp from a Time object

Parameters:

  • time (Time)

Returns:

  • (Time)

    time



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

def timestamp=(time)
  tstamp = (time.to_r / ts_resol).to_i
  self.tsh = (tstamp & 0xffffffff00000000) >> 32
  self.tsl = tstamp & 0xffffffff
  time
end

#to_sString

Return the object as a String

Returns:

  • (String)


116
117
118
119
120
# File 'lib/packetgen/pcapng/epb.rb', line 116

def to_s
  pad_field :data, :options
  recalc_block_len
  super
end