<commit_before>#!/usr/bin/env python
import sys
import time
import zmq
import csv
import thread

sys.path.insert(0,'../../../Engine/libraries/netip/python/')
sys.path.insert(0,'../../../ryu/ryu/')

from netip import *
from ofproto import ofproto_parser
from ofproto import ofproto_common
from ofproto import ofproto_v1_0, ofproto_v1_0_parser
from ofproto import ofproto_v1_2, ofproto_v1_2_parser
from ofproto import ofproto_v1_3, ofproto_v1_3_parser
from ofproto import ofproto_v1_4, ofproto_v1_4_parser
from ofproto import ofproto_v1_5, ofproto_v1_5_parser

from lib.mac import haddr_to_bin
from lib.packet import packet
from lib.packet import ethernet
from lib.packet import ether_types
from datetime import datetime


class Module(object):
   origin=""
   destination=""
   length=0
   max_length=0
   min_length=0
   counter=0
   def __init__(self, origin, destination, length, max_length, min_length, counter):
      self.origin = origin
      self.destination = destination
      self.length = length
      self.max_length = max_length
      self.min_length = min_length
      self.counter = counter


class Address(object):
   dst = "00:00:00:00:00:00"
   src = "00:00:00:00:00:00"
   def __init__(self, src, dst, counter, timestamp, diff):
      self.src = src
      self.dst = dst
      self.counter = counter
      self.timestamp = timestamp
      self.diff = diff


def module_identification(module_list, src_field, length):
   if src_field.startswith("0_", 0, 2) == True:
      origin = src_field[2:]
      destination = "core"

   elif src_field.startswith("1_", 0, 2) == True:
      origin = src_field[2:]
      destination = "core"

   elif src_field.startswith("2_", 0, 2) == True:
      origin = "core"
      destination = src_field[2:]

   elif src_field.startswith("3_", 0, 2) == True:
      origin = "core"
      destination = src_field[2:]

   if not module_list:
      module = Module(origin, destination, length, length, length, 1)
      module_list.append(module)
   else:
      validate = False
      for modules in module_list:
         if (modules.origin == origin and destination == modules.destination):
            modules.length = modules.length + length
            if modules.max_length < length:
               modules.max_length = length
            if modules.min_length > length:
               modules.min_length = length
            modules.counter = modules.counter + 1
            validate = True
            break
            
      if validate != True:
         module = Module(origin, destination, length, length, length, 1)
         module_list.append(module)


def print_module_list (module_list):
   for i in module_list:
      print('\033[1;34m%r has sent to %r %r messages. Average length of messages %r Bytes, maximum message size %r Bytes, minimum message size %r Bytes.\033[1;m')% (i.origin.replace("'", ""), i.destination.replace("'", ""), i.counter, i.length/i.counter , i.max_length, i.min_length)


def msg_parser (msg):
   (netide_version, netide_msg_type, netide_msg_len, netide_xid, netide_mod_id, netide_datapath) = NetIDEOps.netIDE_decode_header(msg)
   message_data = msg[NetIDEOps.NetIDE_Header_Size:]
   ret = bytearray(message_data)
   if len(ret) >= ofproto_common.OFP_HEADER_SIZE:
      (version, msg_type, msg_len, xid) = ofproto_parser.header(ret)
      msg_decoded = ofproto_parser.msg(netide_datapath, version, msg_type, msg_len, xid, ret)
      return(msg_decoded, len(ret))
   else:
      return("message", len(ret))


def loop_detection (msg, timestamp, address_list):
   position = str(msg).find("OFPPacketIn", 0, len(str(msg)))
   is_in_list = False
   FMT = '%H:%M:%S'

   if position != -1:
      pkt = packet.Packet(msg.data)
      eth = pkt.get_protocol(ethernet.ethernet)
      #if eth.ethertype == ether_types.ETH_TYPE_ARP:
      dst = eth.dst
      src = eth.src
      if not address_list: #and dst == 'ff:ff:ff:ff:ff:ff':
         address = Address(src, dst, 1, timestamp, 0)
         address_list.append(address)
                        
      else:
         for addr in address_list:
            if addr.src == src and addr.dst == dst:
               is_in_list = True
               addr.counter = addr.counter + 1
               delta_time = datetime.strptime(timestamp, FMT) - datetime.strptime(addr.timestamp, FMT)
               if delta_time.seconds == 0:
                  addr.diff = 1
               else:
                  addr.diff= delta_time.seconds
               break
         if is_in_list == False: #and dst == 'ff:ff:ff:ff:ff:ff':
               address = Address(src, dst, 1, timestamp, 0)
               address_list.append(address)


def loop_detection_two(address_list):
   for addr in address_list:
      if addr.counter / addr.diff  >= 50:
         return True


def print_list_address(address_list):
   for addr in address_list:
      print('MAC origen: %r, MAC destino: %r, Counter:%d, Timestamp: %r, Diff: %d')%(addr.src, addr.dst, addr.counter, addr.timestamp, addr.diff)


def IDE_connection(message):
   context = zmq.Context()
   publisher = context.socket(zmq.PUB)
   publisher.bind("tcp://127.0.0.1:5560")
   netip_message = NetIDEOps.netIDE_encode('NETIDE_MGMT', 0, 0, 0, message)
   time.sleep(1)
   publisher.send(netip_message);

def receive_messages(module_list, address_list):
   context = zmq.Context()
   socket = context.socket(zmq.SUB)
   socket.connect("tcp://localhost:5557")
   socket.setsockopt(zmq.SUBSCRIBE, "")
<commit_msg>Verificator binding to all addresses<commit_after>#!/usr/bin/env python
import sys
import time
import zmq
import csv
import thread

sys.path.insert(0,'../../../Engine/libraries/netip/python/')
sys.path.insert(0,'../../../ryu/ryu/')

from netip import *
from ofproto import ofproto_parser
from ofproto import ofproto_common
from ofproto import ofproto_v1_0, ofproto_v1_0_parser
from ofproto import ofproto_v1_2, ofproto_v1_2_parser
from ofproto import ofproto_v1_3, ofproto_v1_3_parser
from ofproto import ofproto_v1_4, ofproto_v1_4_parser
from ofproto import ofproto_v1_5, ofproto_v1_5_parser

from lib.mac import haddr_to_bin
from lib.packet import packet
from lib.packet import ethernet
from lib.packet import ether_types
from datetime import datetime


class Module(object):
   origin=""
   destination=""
   length=0
   max_length=0
   min_length=0
   counter=0
   def __init__(self, origin, destination, length, max_length, min_length, counter):
      self.origin = origin
      self.destination = destination
      self.length = length
      self.max_length = max_length
      self.min_length = min_length
      self.counter = counter


class Address(object):
   dst = "00:00:00:00:00:00"
   src = "00:00:00:00:00:00"
   def __init__(self, src, dst, counter, timestamp, diff):
      self.src = src
      self.dst = dst
      self.counter = counter
      self.timestamp = timestamp
      self.diff = diff


def module_identification(module_list, src_field, length):
   if src_field.startswith("0_", 0, 2) == True:
      origin = src_field[2:]
      destination = "core"

   elif src_field.startswith("1_", 0, 2) == True:
      origin = src_field[2:]
      destination = "core"

   elif src_field.startswith("2_", 0, 2) == True:
      origin = "core"
      destination = src_field[2:]

   elif src_field.startswith("3_", 0, 2) == True:
      origin = "core"
      destination = src_field[2:]

   if not module_list:
      module = Module(origin, destination, length, length, length, 1)
      module_list.append(module)
   else:
      validate = False
      for modules in module_list:
         if (modules.origin == origin and destination == modules.destination):
            modules.length = modules.length + length
            if modules.max_length < length:
               modules.max_length = length
            if modules.min_length > length:
               modules.min_length = length
            modules.counter = modules.counter + 1
            validate = True
            break
            
      if validate != True:
         module = Module(origin, destination, length, length, length, 1)
         module_list.append(module)


def print_module_list (module_list):
   for i in module_list:
      print('\033[1;34m%r has sent to %r %r messages. Average length of messages %r Bytes, maximum message size %r Bytes, minimum message size %r Bytes.\033[1;m')% (i.origin.replace("'", ""), i.destination.replace("'", ""), i.counter, i.length/i.counter , i.max_length, i.min_length)


def msg_parser (msg):
   (netide_version, netide_msg_type, netide_msg_len, netide_xid, netide_mod_id, netide_datapath) = NetIDEOps.netIDE_decode_header(msg)
   message_data = msg[NetIDEOps.NetIDE_Header_Size:]
   ret = bytearray(message_data)
   if len(ret) >= ofproto_common.OFP_HEADER_SIZE:
      (version, msg_type, msg_len, xid) = ofproto_parser.header(ret)
      msg_decoded = ofproto_parser.msg(netide_datapath, version, msg_type, msg_len, xid, ret)
      return(msg_decoded, len(ret))
   else:
      return("message", len(ret))


def loop_detection (msg, timestamp, address_list):
   position = str(msg).find("OFPPacketIn", 0, len(str(msg)))
   is_in_list = False
   FMT = '%H:%M:%S'

   if position != -1:
      pkt = packet.Packet(msg.data)
      eth = pkt.get_protocol(ethernet.ethernet)
      #if eth.ethertype == ether_types.ETH_TYPE_ARP:
      dst = eth.dst
      src = eth.src
      if not address_list: #and dst == 'ff:ff:ff:ff:ff:ff':
         address = Address(src, dst, 1, timestamp, 0)
         address_list.append(address)
                        
      else:
         for addr in address_list:
            if addr.src == src and addr.dst == dst:
               is_in_list = True
               addr.counter = addr.counter + 1
               delta_time = datetime.strptime(timestamp, FMT) - datetime.strptime(addr.timestamp, FMT)
               if delta_time.seconds == 0:
                  addr.diff = 1
               else:
                  addr.diff= delta_time.seconds
               break
         if is_in_list == False: #and dst == 'ff:ff:ff:ff:ff:ff':
               address = Address(src, dst, 1, timestamp, 0)
               address_list.append(address)


def loop_detection_two(address_list):
   for addr in address_list:
      if addr.counter / addr.diff  >= 50:
         return True


def print_list_address(address_list):
   for addr in address_list:
      print('MAC origen: %r, MAC destino: %r, Counter:%d, Timestamp: %r, Diff: %d')%(addr.src, addr.dst, addr.counter, addr.timestamp, addr.diff)


def IDE_connection(message):
   context = zmq.Context()
   publisher = context.socket(zmq.PUB)
   publisher.bind("tcp://*:5560")
   netip_message = NetIDEOps.netIDE_encode('NETIDE_MGMT', 0, 0, 0, message)
   time.sleep(1)
   publisher.send(netip_message);

def receive_messages(module_list, address_list):
   context = zmq.Context()
   socket = context.socket(zmq.SUB)
   socket.connect("tcp://localhost:5557")
   socket.setsockopt(zmq.SUBSCRIBE, "")

{'fraction_non_alphanumeric': 0.1133973479652492, 'fraction_numerical': 0.019204389574759947, 'mean_word_length': 2.933812949640288, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '913001', 'n_tokens_mistral': 3686, 'n_tokens_neox': 3537, 'n_words': 854}