Class: PacketGen::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/packetgen/capture.rb

Overview

Capture packets from wire

Author:

  • Sylvain Daubert

  • Kent 'picat' Gruber

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil) ⇒ Capture

Returns a new instance of Capture.

Author:

  • Sylvain Daubert

  • optix2000 - add monitor argument

Since:

  • 2.0.0 remove old 1.x API

  • 3.0.0 arguments are kwargs and no more a hash

  • 3.1.5 add monitor argument



56
57
58
59
60
61
62
63
# File 'lib/packetgen/capture.rb', line 56

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 = []
  @timestamps = []
  set_options(iface, max, timeout, filter, promisc, parse, snaplen, monitor)
end

Instance Attribute Details

#iface ⇒ String (readonly)

Get interface name



36
37
38
# File 'lib/packetgen/capture.rb', line 36

def iface
  @iface
end

#packets ⇒ Array<Packets> (readonly)

Get captured packets.



23
24
25
# File 'lib/packetgen/capture.rb', line 23

def packets
  @packets
end

#raw_packets ⇒ Array<String> (readonly)

Get captured packet raw data.



27
28
29
# File 'lib/packetgen/capture.rb', line 27

def raw_packets
  @raw_packets
end

#timestamps ⇒ Array<Time> (readonly)

Get timestamps associated with #packets and #raw_packets

Since:

  • 3.3.0



32
33
34
# File 'lib/packetgen/capture.rb', line 32

def timestamps
  @timestamps
end

Instance Method Details

#start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil) {|packet, timestamp| ... } ⇒ Object

Start capture

Yield Parameters:

  • packet (Packet, String) —

    if a block is given, yield each captured packet (Packet or raw data String, depending on :parse option)

  • timestamp (Time) —

    packet timestamp

See Also:

  • for parameters

Author:

  • Sylvain Daubert

  • optix2000 - add monitor argument

Since:

  • 3.0.0 arguments are kwargs and no more a hash

  • 3.1.5 add monitor argument

  • 3.3.0 add packet timestamp as second yield parameter



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/packetgen/capture.rb', line 75

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|
      add_packet(packet, &block)
      break if defined?(@max) && (raw_packets.size >= @max)
    end
  end
  cap_thread.join(@timeout)
end

#stop ⇒ void

This method returns an undefined value.

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.



92
93
94
# File 'lib/packetgen/capture.rb', line 92

def stop
  cap_thread.kill
end