Class: Spcap::File

Inherits:
Object
  • Object
show all
Defined in:
lib/spcap/file.rb

Constant Summary collapse

MagicNumber =

File format : File header

4 Magic number
2,2  Major version  Minor version
4  Time zone offset always set to 0
4  Time stamp accuracy always set to 0
4  Snapshot length
4  Link-layer header type
["A1B2C3D4"].pack("H*")

Instance Method Summary collapse

Constructor Details

#initialize(istream) ⇒ File

Returns a new instance of File.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spcap/file.rb', line 14

def initialize(istream)
  @istream = istream
  magic_number = istream.read(4)
  if magic_number == MagicNumber
    @unpack_16 = "n"
    @unpack_32 = "N"
  else
    @unpack_16 = "v"
    @unpack_32 = "V"
  end
  @major_version, @minor_version = read16, read16
  @istream.read(8) # flush unused  time_zone_offset_always_0, timestamp_accuracy_always_0,
  @snapshot_length = read32
  @linklayer_header_type = read32
end

Instance Method Details

#eachObject

Packets header

4 Time stamp, seconds value
4 Time stamp, microseconds value
4 Length of captured packet data
4 Un-truncated length of the packet data


38
39
40
41
42
43
44
45
46
# File 'lib/spcap/file.rb', line 38

def each
  until(@istream.eof?)
    time = Time.at(read32,read32)
    caplen = read32
    len = read32
    raw_data = @istream.read(caplen)
    yield Packet.new(time,raw_data,len,@linklayer_header_type)
  end
end

#read16Object



30
# File 'lib/spcap/file.rb', line 30

def read16 ; @istream.read(2).unpack(@unpack_16).first ; end

#read32Object



32
# File 'lib/spcap/file.rb', line 32

def read32 ; @istream.read(4).unpack(@unpack_32).first ; end