Class: Spcap::Scopy

Inherits:
Stream show all
Defined in:
lib/spcap/scopy.rb

Constant Summary collapse

CURRENT_OUT_FILENAME =
'pcapcopy.tmp'
FILENAME_EXTENTION =
'.pcap'

Constants inherited from Stream

Spcap::Stream::MagicNumber

Instance Method Summary collapse

Methods inherited from Stream

#close, #each, #eof?, #read16, #read32

Constructor Details

#initialize(istream, opt = {}) ⇒ Scopy

Returns a new instance of Scopy.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/spcap/scopy.rb', line 5

def initialize(istream,opt = {})
  @tmpname = CURRENT_OUT_FILENAME
  @wpath = opt[:wpath] || ''
  @prefix = opt[:prefix] || ''
  @limit = opt[:limit] || 10000
  @counter = 0
  @ostream = new_file
  super(istream)
  packing = @unpack_16 + @unpack_16 + @unpack_32 + @unpack_32 + @unpack_32 + @unpack_32
  @header = @magic_number + [@major_version, @minor_version, 0, 0, @snapshot_length, 
    @linklayer_header_type].pack(packing)
end

Instance Method Details

#backup_nameObject



26
27
28
# File 'lib/spcap/scopy.rb', line 26

def backup_name
  @prefix + Time.now.strftime("%Y%m%d%H%M%S%6N") + FILENAME_EXTENTION
end

#finalizeObject



36
37
38
# File 'lib/spcap/scopy.rb', line 36

def finalize
  @ostream.close
end

#new_fileObject



24
# File 'lib/spcap/scopy.rb', line 24

def new_file ; File.new(File.expand_path(backup_name,@wpath),"w") ; end

#nextObject



40
41
42
43
44
45
46
47
48
# File 'lib/spcap/scopy.rb', line 40

def next
  pkt = super
  @counter += 1
    if @counter == @limit
      switch_out
      @counter = 0
    end
  return pkt
end

#read(size) ⇒ Object



18
19
20
21
22
# File 'lib/spcap/scopy.rb', line 18

def read(size)
  buf = @istream.read(size)
  @ostream.write(buf)
  return buf
end

#switch_outObject



30
31
32
33
34
# File 'lib/spcap/scopy.rb', line 30

def switch_out
  @ostream.close
  @ostream = new_file
  @ostream.write(@header)
end