class PacketGen::Capture

Capture packets from wire @author Sylvain Daubert @author Kent 'picat' Gruber

Attributes

cap_thread[R]
filter[R]
iface[R]

Get interface name @return [String]

monitor[R]
packets[R]

Get captured packets. @return [Array<Packets>]

promisc[R]
raw_packets[R]

Get captured packet raw data. @return [Array<String>]

snaplen[R]

Public Class Methods

new(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil) click to toggle source

@param [String] iface interface on which capture

packets on. Default: Use default interface lookup. If no interface found,
use loopback one.

@param [Integer] max maximum number of packets to capture. @param [Integer] timeout maximum number of seconds before end

of capture. Default: +nil+ (no timeout)

@param [String] filter bpf filter @param [Boolean] promisc (default: false) @param [Boolean] parse parse raw data to generate packets before

yielding.  Default: +true+

@param [Integer] snaplen maximum number of bytes to capture for

each packet.

@param [Boolean] monitor enable or disable monitor mode on interface (if supported by iface). @since 2.0.0 remove old 1.x API @since 3.0.0 arguments are kwargs and no more a hash @since 3.1.5 add monitor argument @author Sylvain Daubert @author optix2000 - add monitor argument

# File lib/packetgen/capture.rb, line 52
def initialize(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil)
  @iface = iface || PacketGen.default_iface || PacketGen.loopback_iface

  @packets     = []
  @raw_packets = []
  set_options iface, max, timeout, filter, promisc, parse, snaplen, monitor
end

Public Instance Methods

start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil, &block) click to toggle source

Start capture @see {#initialize} for parameters @yieldparam [Packet,String] packet if a block is given, yield each

captured packet (Packet or raw data String, depending on +:parse+ option)

@since 3.0.0 arguments are kwargs and no more a hash @since 3.1.5 add monitor argument @author Sylvain Daubert @author optix2000 - add monitor argument

# File lib/packetgen/capture.rb, line 68
def start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil, &block)
  set_options iface, max, timeout, filter, promisc, parse, snaplen, monitor

  @cap_thread = Thread.new do
    PCAPRUBWrapper.capture(**capture_args) do |packet_data|
      add_packet(packet_data, &block)
      break if defined?(@max) && (raw_packets.size >= @max)
    end
  end
  cap_thread.join(@timeout)
end
stop() click to toggle source

Stop capture. Should be used from another thread, as {#start} blocks.

BEWARE: multiple capture should not be started in different threads. No effort has been made to make Capture nor PacketGen thread-safe. @return [void]

# File lib/packetgen/capture.rb, line 85
def stop
  cap_thread.kill
end

Private Instance Methods

add_packet(data) { |data| ... } click to toggle source
# File lib/packetgen/capture.rb, line 122
def add_packet(data, &block)
  raw_packets << data
  if @parse
    begin
      packet = Packet.parse(data)
    rescue ParseError
      packet = UnknownPacket.new.parse(data)
    end
    packets << packet
    block&.call(packet)
  elsif block
    yield data
  end
end
capture_args() click to toggle source

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/ParameterLists

# File lib/packetgen/capture.rb, line 109
def capture_args
  h = { iface: iface, filter: filter, monitor: monitor }
  h[:snaplen] = snaplen unless snaplen.nil?
  h[:promisc] = promisc unless promisc.nil?
  h
end
filter_on(pcap) click to toggle source
# File lib/packetgen/capture.rb, line 116
def filter_on(pcap)
  return if filter.nil? || filter.empty?

  PCAPRUBWrapper.filter_on(pcap: pcap, filter: filter)
end
set_options(iface, max, timeout, filter, promisc, parse, snaplen, monitor) click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/packetgen/capture.rb, line 94
def set_options(iface, max, timeout, filter, promisc, parse, snaplen, monitor)
  @max = max if max
  @filter = filter unless filter.nil?
  @timeout = timeout unless timeout.nil?
  @promisc = promisc unless promisc.nil?
  @snaplen = snaplen unless snaplen.nil?
  @parse = parse unless parse.nil?
  @iface = iface unless iface.nil?
  @monitor = monitor unless monitor.nil?
end