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
34
35
36
37
38
39
40
41
42
# 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
  @queue    = Queue.new
  @workers  = (0...nworkers).map { ::Thread.new { worker } }
  @ctx      = Context.get

  begin
    @udp = UDPSocket.new
  rescue Errno::EMFILE
    Logger.warn "It looks like another process is using a lot of UDP file descriptors" \
                "and the operating system is denying resources to me, I'll try again in one second."

    sleep 1.0
    retry
  end
end

Instance Method Details

#push(packet) ⇒ Object

Push a packet to the queue.



45
46
47
# File 'lib/bettercap/network/packet_queue.rb', line 45

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

#stopObject

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



60
61
62
63
64
65
# File 'lib/bettercap/network/packet_queue.rb', line 60

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.



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

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