#! /usr/bin/env python2
# vim: set fenc=utf8 ts=4 sw=4 et :
#

import sys
import os
import shutil
import time
import signal
from threading import Thread
from Queue import Queue, Empty
from scapy.all import *

#MAC Addr of the flow generator 
FLOWGEN_MAC = "b4:be:b1:6b:00:b5"
#Timeout const in seconds
FLOW_TIMEOUT = 10
MAX_PKT = 100
running = True

class Flow:
    def __init__(self, dst):
        self.pkts = []
        self.dst_hash = dst
        self.last_pkt = time.time()
    def write(self):
        if len(self.pkts) == 1:
            print("Only init pkt recived, flow with dst_hash: {} not written".format(self.dst_hash))
            return

        i = 0
        pktdump = PcapWriter(interface+"/"+self.dst_hash, append=True, sync=True)
        for pkt in self.pkts:
            pktdump.write(pkt)
            i += 1
        pktdump.close()
        print("Flow with dst_hash: {} written, included {} packets".format(self.dst_hash,i))


def packet_handler(pkt):
   #check for ether 
   if type(pkt) is Ether:
       #check dict
       dst = str(pkt.dst)
       if dst not in flows:
           sendp(Ether(dst=FLOWGEN_MAC, src=dst)/Raw("ENTE"),iface=interface,verbose=False)
           f = Flow(dst)
           flows[dst] = f
       flows[dst].pkts.append(pkt)
       flows[dst].last_pkt = time.time()

def flow_handler():
    to_remove = []
    act_time = time.time()
    for dst in flows:
       #check timeout
       if act_time - flows[dst].last_pkt > FLOW_TIMEOUT:
           flows[dst].write()
           to_remove.append(dst)

    for dst in to_remove:
        del flows[dst]


def threaded_sniff_target(q):
    while (running):
        sniff(iface=interface, filter="ether src host {0}".format(FLOWGEN_MAC), prn = lambda x : q.put(x), timeout=1)

def threaded_handler(q):
  while (running):
    try:
      pkt = q.get(timeout = 1)
      packet_handler(pkt)
    except Empty:
      #no new pkts so we have time to handle the flows
      flow_handler()
      print("Amount of active flows: {}".format(len(flows)))


def print_usage():
    print("Usage: ./fworker interface")
    sys.exit(1)

def SIGINT_handler(x,y):
    global running
    running = False

if __name__ == "__main__":
    q = Queue.Queue(maxsize=MAX_PKT)

    flows={}    
    dicto={}
    if len(sys.argv) != 2:
        print_usage()
    else:
        interface = str(sys.argv[1])


    if os.path.exists(interface):
        print("dir exists, clear dir")
        shutil.rmtree(interface)
    
    os.mkdir(interface)
    print("Start sniffing on Port "+interface)
    sniffer = Thread(target = threaded_sniff_target, args = (q,))
    sniffer.start()
    handler = Thread(target = threaded_handler, args = (q,))
    handler.start()

    signal.signal(signal.SIGINT, SIGINT_handler)
    signal.signal(signal.SIGTERM, SIGINT_handler)

    while running:
        signal.pause()

@@ -117,16 +117,30 @@
  signal%0A
+import struct%0A
 from thr
@@ -357,16 +357,496 @@
 = True%0A%0A
+class PcapPacket:%0A    def __init__(self, payload):%0A        self.caplen = len(payload)%0A        self.wirelen = self.caplen%0A        t = time.time()%0A        it = int(t)%0A        self.sec = it%0A        self.usec = int(round((t-it)*1000000))%0A        self.payload = payload%0A    def write(self):%0A        sys.stdout.write(struct.pack(%22IIII%22,self.sec, self.usec, self.caplen, self.wirelen))%0A        sys.stdout.write(str(self.payload))%0A        sys.stdout.flush()%0A        #print(self.payload)%0A%0A
 class Fl
@@ -1026,24 +1026,25 @@
             
+#
 print(%22Only 
@@ -1144,104 +1144,8 @@
 rn%0A%0A
-        i = 0%0A        pktdump = PcapWriter(interface+%22/%22+self.dst_hash, append=True, sync=True)%0A
     
@@ -1189,22 +1189,15 @@
  pkt
-dump
 .write(
-pkt
 )%0A  
@@ -1206,51 +1206,9 @@
     
- 
-   i += 1%0A        pktdump.close()%0A        
+#
 prin
@@ -1392,25 +1392,81 @@
     
-dst = str(pkt.dst
+pcap = PcapPacket(pkt)%0A       dst = str(pkt.dst)%0A       src = str(pkt.src
 )%0A  
@@ -1518,27 +1518,19 @@
 her(dst=
-FLOWGEN_MAC
+src
 , src=ds
@@ -1657,18 +1657,19 @@
 append(p
-kt
+cap
 )%0A      
@@ -2362,24 +2362,25 @@
 ler()%0A      
+#
 print(%22Amoun
@@ -2651,21 +2651,8 @@
    %0A
-    dicto=%7B%7D%0A
     
@@ -2783,24 +2783,25 @@
 e):%0A        
+#
 print(%22dir e
@@ -2881,24 +2881,25 @@
 erface)%0A    
+#
 print(%22Start
@@ -2925,24 +2925,156 @@
 +interface)%0A
+    #global hdr%0A    hdr = struct.pack(%22IHHIIII%22, 0xa1b2c3d4L, 2, 4, 0, 0, MTU, 1)%0A    sys.stdout.write(hdr)%0A    sys.stdout.flush()%0A%0A
     sniffer 

Add pcap output support