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.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bettercap/network/packet_queue.rb', line 22

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

Instance Method Details

#push(packet) ⇒ Object

Push a packet to the queue.



36
37
38
39
# File 'lib/bettercap/network/packet_queue.rb', line 36

def push(packet)
  @queue.push(packet)
  @ctx.memory.optimize! if ( @queue.size == 1 )
end

#stopObject

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



53
54
55
56
57
58
# File 'lib/bettercap/network/packet_queue.rb', line 53

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

#wait_empty(timeout) ⇒ Object

Wait for the packet queue to be empty.



42
43
44
45
46
47
48
49
50
# File 'lib/bettercap/network/packet_queue.rb', line 42

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