class Pcap::Pcaplet

Attributes

capture[R]

Public Class Methods

new(args = nil) click to toggle source
# File lib/pcaplet.rb, line 23
def initialize(args = nil)
  if args
    ARGV[0,0] = args.split(%r\s+/)
  end
  @device = nil
  @rfile = nil
  @count = -1
  @snaplen = 68
  @log_packets = false
  @duplicated = nil

  opts = OptionParser.new do |opts|
    opts.on('-d') {$DEBUG = true}
    opts.on('-v') {$VERBOSE = true}
    opts.on('-n') {Pcap.convert = false}
    opts.on('-i IFACE') {|s| @device = s}
    opts.on('-r FILE') {|s| @rfile = s}
    opts.on('-c COUNT', OptionParser::DecimalInteger) {|i| @count = i}
    opts.on('-s LEN', OptionParser::DecimalInteger) {|i| @snaplen = i}
    opts.on('-l') { @log_packets = true }
  end
  begin
    opts.parse!
  rescue
    usage(1)
  end

  @filter = ARGV.join(' ')

  # check option consistency
  usage(1) if @device && @rfile
  if !@device and !@rfile
    @device = Pcap.lookupdev
  end

  # open
  begin
    if @device
      @capture = Capture.open_live(@device, @snaplen)
    elsif @rfile
      if @rfile !~ %r\.gz$/
        @capture = Capture.open_offline(@rfile)
      else
        $stdin = IO.popen("gzip -dc < #@rfile", 'r')
        @capture = Capture.open_offline('-')
      end
    end
    @capture.setfilter(@filter)
  rescue PcapError, ArgumentError
    $stdout.flush
    $stderr.puts $!
    exit(1)
  end
end

Public Instance Methods

add_filter(f) click to toggle source
# File lib/pcaplet.rb, line 80
def add_filter(f)
  if @filter == nil || @filter =~ %r^\s*$/  # if empty
    @filter = f
  else
    f = f.source if f.is_a? Filter
    @filter = "( #{@filter} ) and ( #{f} )"
  end
  @capture.setfilter(@filter)
end
close() click to toggle source
# File lib/pcaplet.rb, line 121
def close
  @capture.close
end
each(&block) click to toggle source
Alias for: each_packet
each_packet(&block) click to toggle source
# File lib/pcaplet.rb, line 90
def each_packet(&block)
  begin
    @duplicated ||= (RUBY_PLATFORM =~ %rlinux/ && @device == "lo")
    if !@duplicated
      @capture.loop(@count, &block)
    else
      flip = true
      @capture.loop(@count) do |pkt|
        flip = (! flip)
        next if flip

        block.call pkt
      end
    end
  rescue Exception => e
    $stderr.puts "exception when looping over each packet loop: #{e.inspect}"
    raise
  ensure
    # print statistics if live
    if @device && @log_packets
      stat = @capture.stats
      if stat
        $stderr.print("#{stat.recv} packets received by filter\n");
        $stderr.print("#{stat.drop} packets dropped by kernel\n");
      end
    end
  end
end
Also aliased as: each
usage(status, msg = nil) click to toggle source
# File lib/pcaplet.rb, line 17
def usage(status, msg = nil)
  $stderr.puts msg if msg
  pcaplet_usage
  exit(status)
end