Ejemplo n.º 1
0
def get_ranges(packet_field, mac_dl=False, integer=False):

    ints = list()
    params = packet_field['value'].split(',')
    if mac_dl == True:
        for param in params: 
            ints.append(mac_to_int(param))
    elif integer == True:
        for param in params:
            ints.append(int(param))
    else:
	for param in params:
	    ints.append(int(param,16)) # all params should be in hexadecimal format
             
    ints.sort()
    ranges = list()
    current_range = list()
    for i,intn in enumerate(ints):
	if not len(current_range) > 0 or current_range[-1] == (intn -1):
	    current_range.append(intn)
        if not (current_range[-1] == (intn -1)) or i == len(ints)-1:
            if mac_dl == True:
                ranges.append((int_to_mac(current_range[0]),int_to_mac(current_range[-1])))
            elif integer == False:
                ranges.append((hex(current_range[0]),hex(current_range[-1])))
	    else:
		ranges.append((current_range[0],current_range[-1]))
	    current_range = []
    return ranges
Ejemplo n.º 2
0
 def get_ranges(packet_field, mac_dl=False, integer=False):
 
     ints = list()
     params = packet_field.split(',')
     if not type(params) == list:
         params = [params]
     if mac_dl == True:
         for param in params: 
             ints.append(mac_to_int(param))
     elif integer == True:
         for param in params:
             try:
                 ints.append(int(param))
             except Exception as e:
                 ints.append(int(param,16))
                 
     else:
 	    for param in params:
 	        ints.append(int(param,16)) # all params should be in hexadecimal format
              
     ints.sort()
     ranges = list()
     current_range = list()
     for i,intn in enumerate(ints):
 	    if not len(current_range) > 0 or current_range[-1] == (intn -1):
 	        current_range.append(intn)
         if not (current_range[-1] == (intn -1)) or i == len(ints)-1:
             if mac_dl == True:
                 ranges.append((int_to_mac(current_range[0]),int_to_mac(current_range[-1])))
             elif integer == False:
                 ranges.append((hex(current_range[0]),hex(current_range[-1])))
 	        else:
 		    ranges.append((current_range[0],current_range[-1]))
 	    current_range = []
     return ranges
Ejemplo n.º 3
0
    def parse_granted_flowspaces(gfs):
        gfs_list=[]
        for fs in gfs:
            fs_dict = dict(
                flowspace=dict(),
                openflow=dict()
            )
            fs_dict['openflow']=[]
            fs_dict['flowspace']=dict(
                                     mac_src_s=int_to_mac(fs.mac_src_s),
                                     mac_src_e=int_to_mac(fs.mac_src_e),
                                     mac_dst_s=int_to_mac(fs.mac_dst_s),
                                     mac_dst_e=int_to_mac(fs.mac_dst_e),
                                     eth_type_s=fs.eth_type_s,
                                     eth_type_e=fs.eth_type_e,
                                     vlan_id_s=fs.vlan_id_s,
                                     vlan_id_e=fs.vlan_id_e,
                                     ip_src_s=int_to_dotted_ip(fs.ip_src_s),
                                     ip_dst_s=int_to_dotted_ip(fs.ip_dst_s),
                                     ip_src_e=int_to_dotted_ip(fs.ip_src_e),
                                     ip_dst_e=int_to_dotted_ip(fs.ip_dst_e),
                                     ip_proto_s=fs.ip_proto_s,
                                     ip_proto_e=fs.ip_proto_e,
                                     tp_src_s=fs.tp_src_s,
                                     tp_dst_s=fs.tp_dst_s,
                                     tp_src_e=fs.tp_src_e,
                                     tp_dst_e=fs.tp_dst_e,
                                 )

            openflow_dict=dict(
                                    dpid=fs.dpid,
                                    direction=fs.direction,
                                    port_number_s=fs.port_number_s,
                                    port_number_e=fs.port_number_e,
                              )
            existing_fs = False
            for prev_dict in gfs_list:
                if fs_dict['flowspace'] == prev_dict['flowspace']:
                    prev_dict['openflow'].append(openflow_dict)
                    existing_fs = True
                    break
            if not existing_fs:
                fs_dict['openflow'].append(openflow_dict)
                gfs_list.append(fs_dict)
        print "----------------------------------------------------"
        import pprint
        pprint.pprint(gfs_list)
        return gfs_list
Ejemplo n.º 4
0
    def parse_granted_flowspaces(gfs):
        gfs_list=[]
        for fs in gfs:
            fs_dict = dict(
                flowspace=dict(),
                openflow=dict()
            )
            fs_dict['openflow']=[]
            fs_dict['flowspace']=dict(
                                     mac_src_s=int_to_mac(fs.mac_src_s),
                                     mac_src_e=int_to_mac(fs.mac_src_e),
                                     mac_dst_s=int_to_mac(fs.mac_dst_s),
                                     mac_dst_e=int_to_mac(fs.mac_dst_e),
                                     eth_type_s=fs.eth_type_s,
                                     eth_type_e=fs.eth_type_e,
                                     vlan_id_s=fs.vlan_id_s,
                                     vlan_id_e=fs.vlan_id_e,
                                     ip_src_s=int_to_dotted_ip(fs.ip_src_s),
                                     ip_dst_s=int_to_dotted_ip(fs.ip_dst_s),
                                     ip_src_e=int_to_dotted_ip(fs.ip_src_e),
                                     ip_dst_e=int_to_dotted_ip(fs.ip_dst_e),
                                     ip_proto_s=fs.ip_proto_s,
                                     ip_proto_e=fs.ip_proto_e,
                                     tp_src_s=fs.tp_src_s,
                                     tp_dst_s=fs.tp_dst_s,
                                     tp_src_e=fs.tp_src_e,
                                     tp_dst_e=fs.tp_dst_e,
                                 )

            openflow_dict=dict(
                                    dpid=fs.dpid,
                                    direction=fs.direction,
                                    port_number_s=fs.port_number_s,
                                    port_number_e=fs.port_number_e,
                              )
            existing_fs = False
            for prev_dict in gfs_list:
                if fs_dict['flowspace'] == prev_dict['flowspace']:
                    prev_dict['openflow'].append(openflow_dict)
                    existing_fs = True
                    break
            if not existing_fs:
                fs_dict['openflow'].append(openflow_dict)
                gfs_list.append(fs_dict)

        return gfs_list
Ejemplo n.º 5
0
 def parse_mac_not_ranged_types(in_strs):
     in_strs = in_strs.split(',')
     if not type(in_strs) == list:
         in_strs = [in_strs]
     output = list()
     for in_str in in_strs:
        mac_int = int_to_mac(in_str) 
        output.append((mac_int,mac_int))
     return output
Ejemplo n.º 6
0
 def __unicode__(self):
     expression = ""
     try:
         if (self.mac_src_s  == self.mac_src_e):
             expression = expression + ("MAC Source Addr: %s, " % (int_to_mac(self.mac_src_s)))
         elif (self.mac_src_s > 0 or self.mac_src_e < 0xFFFFFFFFFFFF):
             expression = expression + ("MAC Source Addr: %s-%s, " % (int_to_mac(self.mac_src_s), int_to_mac(self.mac_src_e)))
         if (self.mac_dst_s  == self.mac_dst_e):
             expression = expression + ("MAC Destination Addr: %s, " % (int_to_mac(self.mac_dst_s)))
         elif (self.mac_dst_s > 0 or self.mac_dst_e < 0xFFFFFFFFFFFF): 
             expression = expression + ("MAC Destination Addr: %s-%s, " % (int_to_mac(self.mac_dst_s), int_to_mac(self.mac_dst_e))) 
         if (self.eth_type_s == self.eth_type_e): 
             expression = expression + ("Eth Type: 0x%x, " % (self.eth_type_s)) 
         elif (self.eth_type_s > 0 or self.eth_type_e < 0xFFFF): 
             expression = expression + ("Eth Type: 0x%x-0x%x, " % (self.eth_type_s,self.eth_type_e)) 
         if (self.vlan_id_s == self.vlan_id_e): 
             expression = expression + ("VLAN id: %d, " % (self.vlan_id_s)) 
         elif (self.vlan_id_s > 0 or self.vlan_id_e < 0xFFF): 
             expression = expression + ("VLAN id: %d-%d, " % (self.vlan_id_s,self.vlan_id_e)) 
         if (self.ip_src_s == self.ip_src_e):    
             expression = expression + ("IP Source Addr: %s, " % (int_to_dotted_ip(self.ip_src_s)))
         elif (self.ip_src_s > 0 or self.ip_src_e < 0xFFFFFFFF):    
             expression = expression + ("IP Source Addr: %s-%s, " % (int_to_dotted_ip(self.ip_src_s), int_to_dotted_ip(self.ip_src_e)))
         if (self.ip_dst_s == self.ip_dst_e):    
             expression = expression + ("IP Destination Addr: %s, " % (int_to_dotted_ip(self.ip_dst_s)))
         elif (self.ip_dst_s > 0 or self.ip_dst_e < 0xFFFFFFFF):    
             expression = expression + ("IP Destination Addr: %s-%s, " % (int_to_dotted_ip(self.ip_dst_s), int_to_dotted_ip(self.ip_dst_e)))
         if (self.ip_proto_s == self.ip_proto_e):    
             expression = expression + ("IP Protocol Number: %s, " % (self.ip_proto_s)) 
         elif (self.ip_proto_s > 0 or self.ip_proto_e < 0xFF):    
             expression = expression + ("IP Protocol Number: %s-%s, " % (self.ip_proto_s, self.ip_proto_e)) 
         if (self.tp_src_s == self.tp_src_e):    
             expression = expression + ("Transport Source Port: %s, " % (self.tp_src_s))
         elif (self.tp_src_s > 0 or self.tp_src_e < 0xFFFF):    
             expression = expression + ("Transport Source Port: %s-%s, " % (self.tp_src_s, self.tp_src_e))
         if (self.tp_dst_s == self.tp_dst_e):    
             expression = expression + ("Transport Destination Port: %s, " % (self.tp_dst_s))
         if (self.tp_dst_s > 0 or self.tp_dst_e < 0xFFFF):    
             expression = expression + ("Transport Destination Port: %s-%s, " % (self.tp_dst_s, self.tp_dst_e))
         if expression == "":
             expression = "All"
     except Exception as e:
         expression = self.stringify()
     return expression
Ejemplo n.º 7
0
 def __unicode__(self):
     expression = ""
     if (self.mac_src_s == self.mac_src_e):
         expression = expression + ("MAC Source Addr: %s, " %
                                    (int_to_mac(self.mac_src_s)))
     elif (self.mac_src_s > 0 or self.mac_src_e < 0xFFFFFFFFFFFF):
         expression = expression + (
             "MAC Source Addr: %s-%s, " %
             (int_to_mac(self.mac_src_s), int_to_mac(self.mac_src_e)))
     if (self.mac_dst_s == self.mac_dst_e):
         expression = expression + ("MAC Destination Addr: %s, " %
                                    (int_to_mac(self.mac_dst_s)))
     elif (self.mac_dst_s > 0 or self.mac_dst_e < 0xFFFFFFFFFFFF):
         expression = expression + (
             "MAC Destination Addr: %s-%s, " %
             (int_to_mac(self.mac_dst_s), int_to_mac(self.mac_dst_e)))
     if (self.eth_type_s == self.eth_type_e):
         expression = expression + ("Eth Type: 0x%x, " % (self.eth_type_s))
     elif (self.eth_type_s > 0 or self.eth_type_e < 0xFFFF):
         expression = expression + ("Eth Type: 0x%x-0x%x, " %
                                    (self.eth_type_s, self.eth_type_e))
     if (self.vlan_id_s == self.vlan_id_e):
         expression = expression + ("VLAN id: %d, " % (self.vlan_id_s))
     elif (self.vlan_id_s > 0 or self.vlan_id_e < 0xFFF):
         expression = expression + ("VLAN id: %d-%d, " %
                                    (self.vlan_id_s, self.vlan_id_e))
     if (self.ip_src_s == self.ip_src_e):
         expression = expression + ("IP Source Addr: %s, " %
                                    (int_to_dotted_ip(self.ip_src_s)))
     elif (self.ip_src_s > 0 or self.ip_src_e < 0xFFFFFFFF):
         expression = expression + ("IP Source Addr: %s-%s, " %
                                    (int_to_dotted_ip(self.ip_src_s),
                                     int_to_dotted_ip(self.ip_src_e)))
     if (self.ip_dst_s == self.ip_dst_e):
         expression = expression + ("IP Destination Addr: %s, " %
                                    (int_to_dotted_ip(self.ip_dst_s)))
     elif (self.ip_dst_s > 0 or self.ip_dst_e < 0xFFFFFFFF):
         expression = expression + ("IP Destination Addr: %s-%s, " %
                                    (int_to_dotted_ip(self.ip_dst_s),
                                     int_to_dotted_ip(self.ip_dst_e)))
     if (self.ip_proto_s == self.ip_proto_e):
         expression = expression + ("IP Protocol Number: %s, " %
                                    (self.ip_proto_s))
     elif (self.ip_proto_s > 0 or self.ip_proto_e < 0xFF):
         expression = expression + ("IP Protocol Number: %s-%s, " %
                                    (self.ip_proto_s, self.ip_proto_e))
     if (self.tp_src_s == self.tp_src_e):
         expression = expression + ("Transport Source Port: %s, " %
                                    (self.tp_src_s))
     elif (self.tp_src_s > 0 or self.tp_src_e < 0xFFFF):
         expression = expression + ("Transport Source Port: %s-%s, " %
                                    (self.tp_src_s, self.tp_src_e))
     if (self.tp_dst_s == self.tp_dst_e):
         expression = expression + ("Transport Destination Port: %s, " %
                                    (self.tp_dst_s))
     if (self.tp_dst_s > 0 or self.tp_dst_e < 0xFFFF):
         expression = expression + ("Transport Destination Port: %s-%s, " %
                                    (self.tp_dst_s, self.tp_dst_e))
     if expression == "":
         expression = "All"
     return expression