Class: PacketFu::Timestamp

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/pcap.rb

Overview

The Timestamp class defines how Timestamps appear in libpcap files.

Header Definition

Symbol  :endian  Default: :little
Int32   :sec
Int32   :usec

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ Timestamp

Returns a new instance of Timestamp


110
111
112
113
114
# File 'lib/packetfu/pcap.rb', line 110

def initialize(args={})
  set_endianness(args[:endian] ||= :little)
  init_fields(args)
  super(args[:endian], args[:sec], args[:usec])
end

Instance Attribute Details

#endianObject

Returns the value of attribute endian

Returns:

  • (Object)

    the current value of endian


107
108
109
# File 'lib/packetfu/pcap.rb', line 107

def endian
  @endian
end

#secObject

Returns the value of attribute sec

Returns:

  • (Object)

    the current value of sec


107
108
109
# File 'lib/packetfu/pcap.rb', line 107

def sec
  @sec
end

#usecObject

Returns the value of attribute usec

Returns:

  • (Object)

    the current value of usec


107
108
109
# File 'lib/packetfu/pcap.rb', line 107

def usec
  @usec
end

Instance Method Details

#init_fields(args = {}) ⇒ Object

Called by initialize to set the initial fields.


117
118
119
120
121
# File 'lib/packetfu/pcap.rb', line 117

def init_fields(args={})
  args[:sec] = @int32.new(args[:sec])
  args[:usec] = @int32.new(args[:usec])
  return args
end

#read(str) ⇒ Object

Reads a string to populate the object.


129
130
131
132
133
134
135
# File 'lib/packetfu/pcap.rb', line 129

def read(str)
  force_binary(str)
  return self if str.nil?
  self[:sec].read str[0,4]
  self[:usec].read str[4,4]
  self
end

#to_sObject

Returns the object in string form.


124
125
126
# File 'lib/packetfu/pcap.rb', line 124

def to_s
  self.to_a[1,2].map {|x| x.to_s}.join
end