# Path: iolibrary.py
# def kill_signal_handler(signal, frame):
#     '''
#     Signal handler for killing the execution.
#     '''
#     print('\nExiting the program per your command.')
#     system('rm -f out.out request.txt iolibrary.pyc hillclimbing.pyc constants.pyc connect.pyc parse.pyc sniff.pyc')
#     if not path.exists('history'):
#         mkdir('history')
#     if path.exists('basic_breach.log'):
#         system('mv basic_breach.log history/')
#     if path.exists('full_breach.log'):
#         system('mv full_breach.log history/')
#     if path.exists('debug.log'):
#         system('mv debug.log history/')
#     if path.exists('attack.log'):
#         system('mv attack.log history/')
#     if path.exists('win_count.log'):
#         system('mv win_count.log history/')
#     if path.exists('sniff.log'):
#         system('mv sniff.log history/')
#     if path.exists('sniff_full.log'):
#         system('mv sniff_full.log history/')
#     sys.exit(0)
# 
# def get_arguments_dict(args_list):
#     '''
#     Parse command line arguments that were given to the program that calls this method.
#     '''
#     parser = argparse.ArgumentParser(description='Parser of breach.py output')
#     parser.add_argument('caller_name', metavar='caller_name', help='The program that called the argument parser.')
#     parser.add_argument('--execute_breach', action='store_true', help='Initiate breach attack via breach.py')
#     args = parser.parse_args(args_list)
# 
#     args_dict = {}
#     args_dict['execute_breach'] = True if args.execute_breach else False
#     return args_dict
# 
# def setup_logger(logger_name, log_file, args_dict, level=logging.DEBUG):
#     '''
#     Logger factory.
#     '''
#     l = logging.getLogger(logger_name)
#     l.setLevel(level)
#     formatter = logging.Formatter('%(asctime)s : %(message)s')
#     fileHandler = logging.FileHandler(log_file)
#     fileHandler.setFormatter(formatter)
#     l.addHandler(fileHandler)
#     if args_dict['log_to_screen']:
#         streamHandler = logging.StreamHandler()
#         streamHandler.setFormatter(formatter)
#         l.addHandler(streamHandler)


import socket
import sys
import signal
import logging
import constants
from struct import *
from datetime import datetime
from iolibrary import kill_signal_handler, get_arguments_dict, setup_logger
                    icmph = unpack(constants.ICMP_HEADER_UNPACK, icmp_header)
                    icmp_type = icmph[0]
                    code = icmph[1]
                    checksum = icmph[2]
                    h_size = constants.ETHERNET_HEADER_LENGTH + iph_length + icmph_length
                    data_size = len(packet) - h_size
                    data = packet[h_size:]

                    self.sniff_full_logger.debug('Type : ' + str(icmp_type) + ' Code : ' + str(code) + ' Checksum : ' + str(checksum) + '\nData : ' + data)

                elif protocol == constants.UDP_TAG:
                    u = iph_length + constants.ETHERNET_HEADER_LENGTH
                    udph_length = 8
                    udp_header = packet[u:u+8]
                    udph = unpack(constants.UDP_HEADER_UNPACK, udp_header)
                    source_port = udph[0]
                    dest_port = udph[1]
                    length = udph[2]
                    checksum = udph[3]
                    h_size = constants.ETHERNET_HEADER_LENGTH + iph_length + udph_length
                    data_size = len(packet) - h_size
                    data = packet[h_size:]

                    self.sniff_full_logger.debug('Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum) + '\nData : ' + data)

                else:
                    self.sniff_full_logger.debug('Unknown Protocol')


if __name__ == '__main__':

# Path: iolibrary.py
# def kill_signal_handler(signal, frame):
#     '''
#     Signal handler for killing the execution.
#     '''
#     print('\nExiting the program per your command.')
#     system('rm -f out.out request.txt iolibrary.pyc hillclimbing.pyc constants.pyc connect.pyc parse.pyc sniff.pyc')
#     if not path.exists('history'):
#         mkdir('history')
#     if path.exists('basic_breach.log'):
#         system('mv basic_breach.log history/')
#     if path.exists('full_breach.log'):
#         system('mv full_breach.log history/')
#     if path.exists('debug.log'):
#         system('mv debug.log history/')
#     if path.exists('attack.log'):
#         system('mv attack.log history/')
#     if path.exists('win_count.log'):
#         system('mv win_count.log history/')
#     if path.exists('sniff.log'):
#         system('mv sniff.log history/')
#     if path.exists('sniff_full.log'):
#         system('mv sniff_full.log history/')
#     sys.exit(0)
# 
# def get_arguments_dict(args_list):
#     '''
#     Parse command line arguments that were given to the program that calls this method.
#     '''
#     parser = argparse.ArgumentParser(description='Parser of breach.py output')
#     parser.add_argument('caller_name', metavar='caller_name', help='The program that called the argument parser.')
#     parser.add_argument('--execute_breach', action='store_true', help='Initiate breach attack via breach.py')
#     args = parser.parse_args(args_list)
# 
#     args_dict = {}
#     args_dict['execute_breach'] = True if args.execute_breach else False
#     return args_dict
# 
# def setup_logger(logger_name, log_file, args_dict, level=logging.DEBUG):
#     '''
#     Logger factory.
#     '''
#     l = logging.getLogger(logger_name)
#     l.setLevel(level)
#     formatter = logging.Formatter('%(asctime)s : %(message)s')
#     fileHandler = logging.FileHandler(log_file)
#     fileHandler.setFormatter(formatter)
#     l.addHandler(fileHandler)
#     if args_dict['log_to_screen']:
#         streamHandler = logging.StreamHandler()
#         streamHandler.setFormatter(formatter)
#         l.addHandler(streamHandler)


# Path: sniff.py
import socket
import sys
import signal
import logging
import constants
from struct import *
from datetime import datetime
from iolibrary import kill_signal_handler, get_arguments_dict, setup_logger

                    icmph = unpack(constants.ICMP_HEADER_UNPACK, icmp_header)
                    icmp_type = icmph[0]
                    code = icmph[1]
                    checksum = icmph[2]
                    h_size = constants.ETHERNET_HEADER_LENGTH + iph_length + icmph_length
                    data_size = len(packet) - h_size
                    data = packet[h_size:]

                    self.sniff_full_logger.debug('Type : ' + str(icmp_type) + ' Code : ' + str(code) + ' Checksum : ' + str(checksum) + '\nData : ' + data)

                elif protocol == constants.UDP_TAG:
                    u = iph_length + constants.ETHERNET_HEADER_LENGTH
                    udph_length = 8
                    udp_header = packet[u:u+8]
                    udph = unpack(constants.UDP_HEADER_UNPACK, udp_header)
                    source_port = udph[0]
                    dest_port = udph[1]
                    length = udph[2]
                    checksum = udph[3]
                    h_size = constants.ETHERNET_HEADER_LENGTH + iph_length + udph_length
                    data_size = len(packet) - h_size
                    data = packet[h_size:]

                    self.sniff_full_logger.debug('Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum) + '\nData : ' + data)

                else:
                    self.sniff_full_logger.debug('Unknown Protocol')


if __name__ == '__main__':

    args_dict = get_arguments_dict(sys.argv)