Class: Pcapz::Capture

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

Instance Method Summary collapse

Constructor Details

#initialize(interface = Interfacez.default) ⇒ Capture

Returns a new instance of Capture.



3
4
5
6
7
8
# File 'lib/pcapz/capture.rb', line 3

def initialize(interface = Interfacez.default)
  @interface   = interface
  @buffer_size = 0
  @file        = nil
  configure_bpf_dev
end

Instance Method Details

#fileObject



41
42
43
# File 'lib/pcapz/capture.rb', line 41

def file
  @file
end

#packetsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pcapz/capture.rb', line 10

def packets
  return nil if @file.closed?
  if block_given?
    loop do
      buffer = @file.read_nonblock(@buffer_size)
      while buffer.size > 0
        datalen = buffer.slice(12,4).unpack('L')[0]
        hdrlen  = buffer.slice(16,2).unpack('v')[0]
        size    = (datalen+hdrlen) + 3 & ~3
        pkt = buffer.slice!(0,size)[hdrlen..-1] 
        yield pkt unless pkt.nil?
      end
    end
  else
    packets = []
    buffer = @file.read_nonblock(@buffer_size)
    while buffer.size > 0
      size, hdrlen = header_decode(buffer)
      pkt = buffer.slice!(0,size)[hdrlen..-1] 
      packets << pkt unless pkt.nil?
    end
    return packets
  end
  return true
rescue EOFError, Errno::EAGAIN
  retry
rescue IO::WaitReadable
  IO.select([@file])
  retry
end

#stop!Object



45
46
47
48
49
# File 'lib/pcapz/capture.rb', line 45

def stop!
  return nil if stopped?
  @file.close
  stopped?
end

#stopped?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/pcapz/capture.rb', line 51

def stopped?
  @file.closed?
end