# Path: ovizart/modules/traffic/pcap/handler.py
# class Handler:
# 
#     def __init__(self, debug_mode="DEBUG"):
#         self._logger = Logger(log_name="Pcap Handler", log_mode=debug_mode)
#         self._logger.message("Pcap Handler initialized")
#         self._pcap = None
#         self._filter_type = None
#         self._file_pointer = None
# 
#     def open_file(self, pcap_file, mode="rb"):
#         try:
#             self._file_pointer = file(pcap_file, mode)
#             self._logger.set_log_level("DEBUG")
#             self._logger.message(("%s is opened at %s mode") % (pcap_file, mode))
#         except:
#             self._logger.set_log_level("ERROR")
#             self._logger.message("Error at opening pcap file")
# 
#     def open_pcap(self, mode="r"):
#         if mode == "r":
#             self._pcap = dpkt.pcap.Reader(self._file_pointer)
#             self._logger.set_log_level("DEBUG")
#             self._logger.message("pcap reader is created")
#         if mode == "w":
#             self._pcap = dpkt.pcap.Writer(self._file_pointer)
# 
#     def write_pcap(self, buf, ts):
#         self._pcap.writepkt(buf, ts)
# 
#     def close_file(self):
#         self._file_pointer.close()
# 
#     def set_filter_type(self, t):
#         self._filter_type = t
#         self._logger.set_log_level("DEBUG")
#         self._logger.message(("Filter type is set %s") % (t))
# 
#     def get_filter_type(self):
#         return self._filter_type
# 
#     def get_pcap(self):
#         return self._pcap
# 
#     def get_eth(self, buf):
#         eth = dpkt.ethernet.Ethernet(buf)
#         if eth.type == dpkt.ethernet.ETH_TYPE_IP:
#             return eth
#         else:
#             self._logger.set_log_level("ERROR")
#             self._logger.message("No Eth is returned")
#             return False
# 
#     def get_ip(self, eth):
#         ip = eth.data
#         if ip.p == dpkt.ip.IP_PROTO_TCP:
#             return ip
#         else:
#             self._logger.set_log_level("ERROR")
#             self._logger.message("No IP is returned")
#             return False
# 
#     def get_tcp(self, ip):
#         tcp = ip.data
#         #self._logger.message(("TCP is returned %s") % (tcp))
#         return tcp
# 
#     def get_udp(self, ip):
#         udp = ip.data
#         return udp
# 
#     def get_reader(self):
#         return self._pcap
# 
# Path: ovizart/modules/traffic/log/logger.py
# class Logger:
# 
#     def __init__(self, log_name, log_mode):
#         logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
#         self.mode = log_mode
#         self.log = logging.getLogger(log_name)
#         self.log.setLevel(log_mode)
# 
#     def set_log_level(self, level="DEBUG"):
#         self.log.setLevel(level)
# 
#     def message(self, message):
#         if self.mode == "DEBUG":
#             self.log.debug(message)
#         if self.mode == "ERROR":
#             self.log.error(message)
#         if self.mode == "INFO":
#             self.log.info(message)
# 
# Path: ovizart/modules/utils/handler.py
# def generate_random_name(N):
#     return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))


import sys
from ovizart.modules.traffic.pcap.handler import Handler
from ovizart.modules.traffic.log.logger import Logger
from ovizart.modules.utils.handler import generate_random_name
        self.pcap = handler.get_pcap()
        self.pcap_handler = handler
        self._logger = Logger(log_name="Flow Handler", log_mode=debug_mode)
        self._logger.message("Flow Handler initialized")

    def get_tcp_flows(self, filter_type="TCP"):
        flow = dict()
        flow_id = 0
        flow_num = 0
        direction = dict() # 1 is one, 2 is bidirectional, keep the flow numbers as indexes
        index = dict()
        self.pcap_handler.set_filter_type(filter_type)
        for ts, buf in self.pcap:
            eth = self.pcap_handler.get_eth(buf)
            if eth:
                ip = self.pcap_handler.get_ip(eth)
            else:
                continue
            #src_ip = self.ip.src
            #dst_ip = self.ip.dst
            # for human readable ip
            # from socket import inet_ntoa
            # inet_ntoa(dst_ip)
            if self.pcap_handler.get_filter_type() == "TCP":
                if not ip:
                    continue
                tcp = self.pcap_handler.get_tcp(ip)
                forward_index = (ip.src, tcp.sport, ip.dst, tcp.dport)
                backward_index = (ip.dst, tcp.dport, ip.src, tcp.sport)
                if index.has_key(forward_index):

# Path: ovizart/modules/traffic/pcap/handler.py
# class Handler:
# 
#     def __init__(self, debug_mode="DEBUG"):
#         self._logger = Logger(log_name="Pcap Handler", log_mode=debug_mode)
#         self._logger.message("Pcap Handler initialized")
#         self._pcap = None
#         self._filter_type = None
#         self._file_pointer = None
# 
#     def open_file(self, pcap_file, mode="rb"):
#         try:
#             self._file_pointer = file(pcap_file, mode)
#             self._logger.set_log_level("DEBUG")
#             self._logger.message(("%s is opened at %s mode") % (pcap_file, mode))
#         except:
#             self._logger.set_log_level("ERROR")
#             self._logger.message("Error at opening pcap file")
# 
#     def open_pcap(self, mode="r"):
#         if mode == "r":
#             self._pcap = dpkt.pcap.Reader(self._file_pointer)
#             self._logger.set_log_level("DEBUG")
#             self._logger.message("pcap reader is created")
#         if mode == "w":
#             self._pcap = dpkt.pcap.Writer(self._file_pointer)
# 
#     def write_pcap(self, buf, ts):
#         self._pcap.writepkt(buf, ts)
# 
#     def close_file(self):
#         self._file_pointer.close()
# 
#     def set_filter_type(self, t):
#         self._filter_type = t
#         self._logger.set_log_level("DEBUG")
#         self._logger.message(("Filter type is set %s") % (t))
# 
#     def get_filter_type(self):
#         return self._filter_type
# 
#     def get_pcap(self):
#         return self._pcap
# 
#     def get_eth(self, buf):
#         eth = dpkt.ethernet.Ethernet(buf)
#         if eth.type == dpkt.ethernet.ETH_TYPE_IP:
#             return eth
#         else:
#             self._logger.set_log_level("ERROR")
#             self._logger.message("No Eth is returned")
#             return False
# 
#     def get_ip(self, eth):
#         ip = eth.data
#         if ip.p == dpkt.ip.IP_PROTO_TCP:
#             return ip
#         else:
#             self._logger.set_log_level("ERROR")
#             self._logger.message("No IP is returned")
#             return False
# 
#     def get_tcp(self, ip):
#         tcp = ip.data
#         #self._logger.message(("TCP is returned %s") % (tcp))
#         return tcp
# 
#     def get_udp(self, ip):
#         udp = ip.data
#         return udp
# 
#     def get_reader(self):
#         return self._pcap
# 
# Path: ovizart/modules/traffic/log/logger.py
# class Logger:
# 
#     def __init__(self, log_name, log_mode):
#         logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
#         self.mode = log_mode
#         self.log = logging.getLogger(log_name)
#         self.log.setLevel(log_mode)
# 
#     def set_log_level(self, level="DEBUG"):
#         self.log.setLevel(level)
# 
#     def message(self, message):
#         if self.mode == "DEBUG":
#             self.log.debug(message)
#         if self.mode == "ERROR":
#             self.log.error(message)
#         if self.mode == "INFO":
#             self.log.info(message)
# 
# Path: ovizart/modules/utils/handler.py
# def generate_random_name(N):
#     return ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(N))


# Path: ovizart/modules/traffic/flow/handler.py
import sys
from ovizart.modules.traffic.pcap.handler import Handler
from ovizart.modules.traffic.log.logger import Logger
from ovizart.modules.utils.handler import generate_random_name

        self.pcap = handler.get_pcap()
        self.pcap_handler = handler
        self._logger = Logger(log_name="Flow Handler", log_mode=debug_mode)
        self._logger.message("Flow Handler initialized")

    def get_tcp_flows(self, filter_type="TCP"):
        flow = dict()
        flow_id = 0
        flow_num = 0
        direction = dict() # 1 is one, 2 is bidirectional, keep the flow numbers as indexes
        index = dict()
        self.pcap_handler.set_filter_type(filter_type)
        for ts, buf in self.pcap:
            eth = self.pcap_handler.get_eth(buf)
            if eth:
                ip = self.pcap_handler.get_ip(eth)
            else:
                continue
            #src_ip = self.ip.src
            #dst_ip = self.ip.dst
            # for human readable ip
            # from socket import inet_ntoa
            # inet_ntoa(dst_ip)
            if self.pcap_handler.get_filter_type() == "TCP":
                if not ip:
                    continue
                tcp = self.pcap_handler.get_tcp(ip)
                forward_index = (ip.src, tcp.sport, ip.dst, tcp.dport)
                backward_index = (ip.dst, tcp.dport, ip.src, tcp.sport)
                if index.has_key(forward_index):

                    flow_num = index[forward_index]