Class: PacketGen::Capture
- Inherits:
-
Object
- Object
- PacketGen::Capture
- Defined in:
- lib/packetgen/capture.rb
Overview
Capture packets from wire
Constant Summary collapse
- DEFAULT_SNAPLEN =
Default snaplen to use if :snaplen option not defined
0xffff
Instance Attribute Summary collapse
-
#packets ⇒ Array<Packets>
readonly
Get captured packets.
-
#raw_packets ⇒ Array<String>
readonly
Get captured packet raw data.
Instance Method Summary collapse
-
#initialize(iface, options = {}) ⇒ Capture
constructor
A new instance of Capture.
-
#start(options = {}) {|packet| ... } ⇒ Object
Start capture.
-
#stop ⇒ void
Stop capture.
Constructor Details
#initialize(iface, options = {}) ⇒ Capture
Returns a new instance of Capture.
34 35 36 37 38 39 |
# File 'lib/packetgen/capture.rb', line 34 def initialize(iface, ={}) @packets = [] @raw_packets = [] @iface = iface end |
Instance Attribute Details
#packets ⇒ Array<Packets> (readonly)
Get captured packets
17 18 19 |
# File 'lib/packetgen/capture.rb', line 17 def packets @packets end |
#raw_packets ⇒ Array<String> (readonly)
Get captured packet raw data
21 22 23 |
# File 'lib/packetgen/capture.rb', line 21 def raw_packets @raw_packets end |
Instance Method Details
#start(options = {}) {|packet| ... } ⇒ Object
Start capture
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/packetgen/capture.rb', line 45 def start(={}) @pcap = PCAPRUB::Pcap.open_live(@iface, @snaplen, @promisc, 1) set_filter @cap_thread = Thread.new do @pcap.each do |packet_data| @raw_packets << packet_data if @parse packet = Packet.parse(packet_data) @packets << packet yield packet if block_given? else yield packet_data if block_given? end if @max break if @raw_packets.size >= @max end 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.
73 74 75 |
# File 'lib/packetgen/capture.rb', line 73 def stop @cap_thread.kill end |