Class: BetterCap::Network::PacketQueue
- Inherits:
-
Object
- Object
- BetterCap::Network::PacketQueue
- Defined in:
- lib/bettercap/network/packet_queue.rb
Overview
This class is responsible for sending various network packets.
Instance Method Summary collapse
-
#initialize(iface, packet_throttle = 0.0, nworkers = 4) ⇒ PacketQueue
constructor
Initialize the PacketQueue, it will spawn
nworkers
thread and will send packets to theiface
network interface. -
#push(packet) ⇒ Object
Push a packet to the queue.
-
#stop ⇒ Object
Notify the queue to stop and wait for every worker to finish.
-
#wait_empty(timeout) ⇒ Object
Wait for the packet queue to be empty.
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 48 |
# File 'lib/bettercap/network/packet_queue.rb', line 45 def push(packet) @queue.push(packet) @ctx.memory.optimize! if ( @queue.size == 1 ) end |
#stop ⇒ Object
Notify the queue to stop and wait for every worker to finish.
62 63 64 65 66 67 |
# File 'lib/bettercap/network/packet_queue.rb', line 62 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.
51 52 53 54 55 56 57 58 59 |
# File 'lib/bettercap/network/packet_queue.rb', line 51 def wait_empty( timeout ) begin Timeout::timeout(timeout) { while !@queue.empty? sleep 0.5 end } rescue; end end |