ruby-pcap icon indicating copy to clipboard operation
ruby-pcap copied to clipboard

pcap.rb dump function is async ?

Open yanzou opened this issue 10 years ago • 3 comments

hi, I was using this gem for parsing and filtering pcap, then I came across a problem:

header = ::Pcap::Capture.open_dead(parser.datalink, parser.snaplen)
dumper = ::Pcap::Dumper.open(header, new_file)
  #do some thing

#dump to new file
dumper.dump(pkt) if dumper

file_size = get_file_size(new_file) #  file_size ----------> got 12kb

but, after this when I look at the file in the file system, it became 16 kb

So , my question is the dumper.dump() function is async ? If it is async, can I got complete event ?

yanzou avatar Jun 26 '15 11:06 yanzou

Sorry for the late reply. I suspect that you need to close the dumper to ensure that the data is flushed to disk.

ahobson avatar Jul 03 '15 12:07 ahobson

@ahobson no, I DO close the dumper, I will post my code later

yanzou avatar Jul 07 '15 02:07 yanzou

 begin
         # some  codes...

          parser = ::Pcap::Capture.open_offline(origin_file)
          parser.setfilter(filter)        

          header = ::Pcap::Capture.open_dead(parser.datalink, parser.snaplen)
          dumper = ::Pcap::Dumper.open(header, new_file)
          dumper.dump(pkt) if dumper.present?        

          # some other codes....

 rescue ::Pcap::PcapError => ex
        p ex.backtrace
        throw "PcapError: Parsing pcap file failed. Please check pcap file."
 rescue Exception => ex
        p ex.backtrace
        throw ex
 ensure
        begin
          parser.close if parser
          dumper.close if dumper
          header.close if header
        rescue Exception => ex
          p ex.message, ex.backtrace
        end
 end

yanzou avatar Aug 25 '15 07:08 yanzou