Class: PacketGen::Capture
- Inherits:
-
Object
- Object
- PacketGen::Capture
- Defined in:
- lib/packetgen/capture.rb
Overview
Capture packets from wire
Instance Attribute Summary collapse
-
#iface ⇒ String
readonly
Get interface name.
-
#packets ⇒ Array<Packets>
readonly
Get captured packets.
-
#raw_packets ⇒ Array<String>
readonly
Get captured packet raw data.
Instance Method Summary collapse
-
#initialize(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil) ⇒ Capture
constructor
A new instance of Capture.
-
#start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil) {|packet| ... } ⇒ Object
Start capture.
-
#stop ⇒ void
Stop capture.
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.
52 53 54 55 56 57 58 |
# 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 = [] iface, max, timeout, filter, promisc, parse, snaplen, monitor end |
Instance Attribute Details
#iface ⇒ String (readonly)
Get interface name
32 33 34 |
# File 'lib/packetgen/capture.rb', line 32 def iface @iface end |
#packets ⇒ Array<Packets> (readonly)
Get captured packets.
24 25 26 |
# File 'lib/packetgen/capture.rb', line 24 def packets @packets end |
#raw_packets ⇒ Array<String> (readonly)
Get captured packet raw data.
28 29 30 |
# File 'lib/packetgen/capture.rb', line 28 def raw_packets @raw_packets end |
Instance Method Details
#start(iface: nil, max: nil, timeout: nil, filter: nil, promisc: false, parse: true, snaplen: nil, monitor: nil) {|packet| ... } ⇒ Object
Start capture
68 69 70 71 72 73 74 75 76 77 78 |
# 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) 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 ⇒ 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.
85 86 87 |
# File 'lib/packetgen/capture.rb', line 85 def stop cap_thread.kill end |