Class: BetterCap::Network::PacketQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/network/packet_queue.rb

Overview

This class is responsible for sending various network packets.

Instance Method Summary collapse

Constructor Details

#initialize(iface, packet_throttle = 0.0, nworkers = 4) ⇒ PacketQueue

Initialize the PacketQueue, it will spawn nworkers thread and will send packets to the iface network interface. If packet_throttle is different than 0.0, it will be used as a delay between each packet to be sent.


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bettercap/network/packet_queue.rb', line 18

def initialize( iface, packet_throttle = 0.0, nworkers = 4 )
  @iface    = iface
  @nworkers = nworkers
  @throttle = packet_throttle;
  @running  = true
  @stream   = Pcap.open_live( iface, 0xffff, false , 1 )
  @mutex    = Mutex.new
  @udp      = UDPSocket.new
  @queue    = Queue.new
  @workers  = (0...nworkers).map { ::Thread.new { worker } }
end

Instance Method Details

#push(packet) ⇒ Object

Push a packet to the queue.


31
32
33
# File 'lib/bettercap/network/packet_queue.rb', line 31

def push(packet)
  @queue.push(packet)
end

#stopObject

Notify the queue to stop and wait for every worker to finish.


47
48
49
50
51
# File 'lib/bettercap/network/packet_queue.rb', line 47

def stop
  @running = false
  @nworkers.times { push(nil) }
  @workers.map(&:join)
end

#wait_empty(timeout) ⇒ Object

Wait for the packet queue to be empty.


36
37
38
39
40
41
42
43
44
# File 'lib/bettercap/network/packet_queue.rb', line 36

def wait_empty( timeout )
  begin
    Timeout::timeout(timeout) {
      while !@queue.empty?
        sleep 0.5
      end
    }
  rescue; end
end