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.
-
#timestamps ⇒ Array<Time>
readonly
Get timestamps associated with #packets and #raw_packets.
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, timestamp| ... } ⇒ 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.
58 59 60 61 62 63 64 65 |
# File 'lib/packetgen/capture.rb', line 58 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 = [] iface, max, timeout, filter, promisc, parse, snaplen, monitor end |
Instance Attribute Details
#iface ⇒ String (readonly)
Get interface name
38 39 40 |
# File 'lib/packetgen/capture.rb', line 38 def iface @iface end |
#packets ⇒ Array<Packets> (readonly)
Get captured packets.
25 26 27 |
# File 'lib/packetgen/capture.rb', line 25 def packets @packets end |
#raw_packets ⇒ Array<String> (readonly)
Get captured packet raw data.
29 30 31 |
# File 'lib/packetgen/capture.rb', line 29 def raw_packets @raw_packets end |
#timestamps ⇒ Array<Time> (readonly)
Get timestamps associated with #packets and #raw_packets
34 35 36 |
# File 'lib/packetgen/capture.rb', line 34 def @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
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/packetgen/capture.rb', line 77 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| 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.
94 95 96 |
# File 'lib/packetgen/capture.rb', line 94 def stop cap_thread.kill end |