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.
57 58 59 60 61 62 63 64 |
# File 'lib/packetgen/capture.rb', line 57 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
37 38 39 |
# File 'lib/packetgen/capture.rb', line 37 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 |
#timestamps ⇒ Array<Time> (readonly)
Get timestamps associated with #packets and #raw_packets
33 34 35 |
# File 'lib/packetgen/capture.rb', line 33 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
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/packetgen/capture.rb', line 76 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.
93 94 95 |
# File 'lib/packetgen/capture.rb', line 93 def stop cap_thread.kill end |