<commit_before>from termcolor import colored
import ofp_dissector_v10
from ofp_parser_v10 import get_action
import ofp_cli # NO_COLOR variable

def red(string):
    if ofp_cli.NO_COLOR == True:
        return string
    return colored(string, 'red')


def green(string):
    if ofp_cli.NO_COLOR == True:
        return string
    return colored(string, 'green')


def blue(string):
    if ofp_cli.NO_COLOR == True:
        return string
    return colored(string, 'blue')


def eth_addr(a):
    mac = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]),
                                             ord(a[3]), ord(a[4]), ord(a[5]))
    return mac


def datapath_id(a):
    dpid = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]),
                                                        ord(a[2]), ord(a[3]),
                                                        ord(a[4]), ord(a[5]),
                                                        ord(a[6]), ord(a[7]))
    return dpid


def print_headers(print_options, date, getlen, caplen, eth, ip, tcp):
    if print_options['min'] == 1:
        print_minimal(date, getlen, ip, tcp)
    else:
        print_layer1(date, getlen, caplen)
        print_layer2(eth)
        print_layer3(ip)
        print_tcp(tcp)


def print_minimal(date, getlen, ip, tcp):
    string = '%s %s:%s -> %s:%s Size: %s Bytes'
    print string % (date, blue(ip['s_addr']), blue(tcp['source_port']),
                    blue(ip['d_addr']), blue(tcp['dest_port']), getlen)


def print_layer1(date, getlen, caplen):
    print '%s: captured %d bytes, truncated to %d bytes' % (date, getlen, caplen)


def print_layer2(eth):
    print ('Destination MAC: %s Source MAC: %s Protocol: %s' %
           (eth_addr(eth['dst_mac']), eth_addr(eth['src_mac']),
            hex(eth['protocol'])))


def print_vlan(vlan):
    print 'Prio: %s CFI: %s VID: %s' % (vlan['prio'], vlan['cfi'], red(vlan['vid']))


def print_layer3(ip):
    print (('IP Version: %d IP Header Length: %d TTL: %d Protocol: %d '
           'Source Address: %s Destination Address: %s') %
           (ip['version'], (ip['ihl'] * 4), ip['ttl'], ip['protocol'],
            blue(ip['s_addr']), blue(ip['d_addr'])))


def print_tcp(tcp):
    print ('TCP Source Port: %s Dest Port: %s Sequence Number: %s '
           'Acknowledgement: %s TCP header length: %s Flags: (CWR: %s '
           'ECE: %s URG: %s ACK: %s PSH: %s RST: %s SYN: %s FYN: %s' %
           (tcp['source_port'], tcp['dest_port'], tcp['sequence'],
            tcp['acknowledgement'], (tcp['length']), tcp['flag_cwr'],
            tcp['flag_ece'], tcp['flag_urg'], tcp['flag_ack'], tcp['flag_psh'],
            tcp['flag_rst'], tcp['flag_syn'], tcp['flag_fyn']))


def print_openflow_header(of):
    version = ofp_dissector_v10.get_ofp_version(of['version'])
    name_version = '%s(%s)' % (version, of['version'])
    if version == '1.0':
        name = ofp_dissector_v10.get_ofp_type(of['type'])
        name_type = '%s(%s)' % (name, of['type'])
    else:
        name_type = '%s' % (of['type'])

    print ('OpenFlow Version: %s Type: %s Length: %s  XID: %s' %
           (name_version, name_type, of['length'], red(of['xid'])))


def print_of_hello(of_xid):
    print '%s OpenFlow Hello' % of_xid


def print_of_error(of_xid, nameCode, typeCode):
    print '%s OpenFlow Error - Type: %s Code: %s' % (of_xid, red(nameCode), red(typeCode))


def print_of_feature_req(of_xid):
    print '%s OpenFlow Feature Request' % of_xid


def print_of_getconfig_req(of_xid):
    print '%s OpenFlow GetConfig Request' % of_xid


def print_of_feature_res(of_xid, f_res):
    print '%s OpenFlow Feature Reply' % of_xid
    dpid = datapath_id(f_res['datapath_id'])
    print ('%s FeatureRes - datapath_id: %s n_buffers: %s n_tbls: %s, pad: %s'
           % (of_xid, green(dpid), f_res['n_buffers'], f_res['n_tbls'],
           f_res['pad']))


def print_of_feature_res_caps_and_actions(of_xid, caps, actions):
    print ('%s FeatureRes - Capabilities:' % of_xid),
    for i in caps:
        print ofp_dissector_v10.get_feature_res_capabilities(i),
    print
    print ('%s FeatureRes - Actions:' % of_xid),
    for i in actions:
        print ofp_dissector_v10.get_feature_res_actions(i),
    print


def print_of_feature_res_ports(of_xid, ports):
    print ('%s FeatureRes - port_id: %s hw_addr: %s name: %s' % (of_xid,
           green(ports['port_id']), green(ports['hw_addr']),
           green(ports['name'])))
    print ('%s FeatureRes - config:' % of_xid),
    for i in ports['config']:
        print ofp_dissector_v10.get_phy_config(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - state:' % of_xid),
    for i in ports['state']:
        print ofp_dissector_v10.get_phy_state(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - curr:' % of_xid),
    for i in ports['curr']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - advertised:' % of_xid),
    for i in ports['advertised']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - supported:' % of_xid),
    for i in ports['supported']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - peer:' % of_xid),
    for i in ports['peer']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0'


def print_ofp_match(xid, ofmatch):
    print ('%s OpenFlow Match -' % (xid)),
    for K in ofmatch:
        print ("%s: %s" % (K, green(ofmatch[K]))),
    print


def print_ofp_body(xid, ofbody):
    string = ('%s OpenFlow Body - Cookie: %s Command: %s Idle/Hard Timeouts: '
             '%s/%s Priority: %s Buffer ID: %s Out Port: %s Flags: %s')
    command = green(ofp_dissector_v10.get_ofp_command(ofbody['command']))
    flags = green(ofp_dissector_v10.get_ofp_flags(ofbody['flags']))

    print string % (xid, ofbody['cookie'], command, ofbody['idle_timeout'],
                    ofbody['hard_timeout'], ofbody['priority'],
                    ofbody['buffer_id'], ofbody['out_port'], flags)


def print_ofp_flow_removed(xid, ofrem):
    string = ('%s OpenFlow Body - Cookie: %s  Priority: %s Pad: %s '
              'Duration Secs/NSecs: %s/%s Idle Timeout: %s Pad2/Pad3: %s/%s'
              ' Packet Count: %s Byte Count: %s')

    print string % (xid, ofrem['cookie'], ofrem['priority'], ofrem['reason'],
                    ofrem['pad'], ofrem['duration_sec'], ofrem['duration_nsec'],
                    ofrem['idle_timeout'], ofrem['pad2'], ofrem['pad3'],
                    ofrem['packet_count'], ofrem['byte_count'])


def print_ofp_action(xid, action_type, length, payload):
    if action_type == 0:
        port, max_len = get_action(action_type, length, payload)

        port = str('CONTROLLER(65533)' if port == 65533 else port)
        print ('%s OpenFlow Action - Type: %s Length: %s Port: %s Max Length: %s' %
               (xid, green('OUTPUT'), length, green(port), max_len))
        return 'output:' + port

    elif action_type == 1:
        vlan, pad = get_action(action_type, length, payload)
        print ('%s OpenFlow Action - Type: %s Length: %s VLAN ID: %s Pad: %s' %
               (xid, green('SetVLANID'), length, green(str(vlan)), pad))
        return 'mod_vlan_vid:' + str(vlan)
<commit_msg>Fixed FlowRemoved printing<commit_after>from termcolor import colored
import ofp_dissector_v10
from ofp_parser_v10 import get_action
import ofp_cli # NO_COLOR variable

def red(string):
    if ofp_cli.NO_COLOR == True:
        return string
    return colored(string, 'red')


def green(string):
    if ofp_cli.NO_COLOR == True:
        return string
    return colored(string, 'green')


def blue(string):
    if ofp_cli.NO_COLOR == True:
        return string
    return colored(string, 'blue')


def eth_addr(a):
    mac = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]),
                                             ord(a[3]), ord(a[4]), ord(a[5]))
    return mac


def datapath_id(a):
    dpid = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]),
                                                        ord(a[2]), ord(a[3]),
                                                        ord(a[4]), ord(a[5]),
                                                        ord(a[6]), ord(a[7]))
    return dpid


def print_headers(print_options, date, getlen, caplen, eth, ip, tcp):
    if print_options['min'] == 1:
        print_minimal(date, getlen, ip, tcp)
    else:
        print_layer1(date, getlen, caplen)
        print_layer2(eth)
        print_layer3(ip)
        print_tcp(tcp)


def print_minimal(date, getlen, ip, tcp):
    string = '%s %s:%s -> %s:%s Size: %s Bytes'
    print string % (date, blue(ip['s_addr']), blue(tcp['source_port']),
                    blue(ip['d_addr']), blue(tcp['dest_port']), getlen)


def print_layer1(date, getlen, caplen):
    print '%s: captured %d bytes, truncated to %d bytes' % (date, getlen, caplen)


def print_layer2(eth):
    print ('Destination MAC: %s Source MAC: %s Protocol: %s' %
           (eth_addr(eth['dst_mac']), eth_addr(eth['src_mac']),
            hex(eth['protocol'])))


def print_vlan(vlan):
    print 'Prio: %s CFI: %s VID: %s' % (vlan['prio'], vlan['cfi'], red(vlan['vid']))


def print_layer3(ip):
    print (('IP Version: %d IP Header Length: %d TTL: %d Protocol: %d '
           'Source Address: %s Destination Address: %s') %
           (ip['version'], (ip['ihl'] * 4), ip['ttl'], ip['protocol'],
            blue(ip['s_addr']), blue(ip['d_addr'])))


def print_tcp(tcp):
    print ('TCP Source Port: %s Dest Port: %s Sequence Number: %s '
           'Acknowledgement: %s TCP header length: %s Flags: (CWR: %s '
           'ECE: %s URG: %s ACK: %s PSH: %s RST: %s SYN: %s FYN: %s' %
           (tcp['source_port'], tcp['dest_port'], tcp['sequence'],
            tcp['acknowledgement'], (tcp['length']), tcp['flag_cwr'],
            tcp['flag_ece'], tcp['flag_urg'], tcp['flag_ack'], tcp['flag_psh'],
            tcp['flag_rst'], tcp['flag_syn'], tcp['flag_fyn']))


def print_openflow_header(of):
    version = ofp_dissector_v10.get_ofp_version(of['version'])
    name_version = '%s(%s)' % (version, of['version'])
    if version == '1.0':
        name = ofp_dissector_v10.get_ofp_type(of['type'])
        name_type = '%s(%s)' % (name, of['type'])
    else:
        name_type = '%s' % (of['type'])

    print ('OpenFlow Version: %s Type: %s Length: %s  XID: %s' %
           (name_version, name_type, of['length'], red(of['xid'])))


def print_of_hello(of_xid):
    print '%s OpenFlow Hello' % of_xid


def print_of_error(of_xid, nameCode, typeCode):
    print '%s OpenFlow Error - Type: %s Code: %s' % (of_xid, red(nameCode), red(typeCode))


def print_of_feature_req(of_xid):
    print '%s OpenFlow Feature Request' % of_xid


def print_of_getconfig_req(of_xid):
    print '%s OpenFlow GetConfig Request' % of_xid


def print_of_feature_res(of_xid, f_res):
    print '%s OpenFlow Feature Reply' % of_xid
    dpid = datapath_id(f_res['datapath_id'])
    print ('%s FeatureRes - datapath_id: %s n_buffers: %s n_tbls: %s, pad: %s'
           % (of_xid, green(dpid), f_res['n_buffers'], f_res['n_tbls'],
           f_res['pad']))


def print_of_feature_res_caps_and_actions(of_xid, caps, actions):
    print ('%s FeatureRes - Capabilities:' % of_xid),
    for i in caps:
        print ofp_dissector_v10.get_feature_res_capabilities(i),
    print
    print ('%s FeatureRes - Actions:' % of_xid),
    for i in actions:
        print ofp_dissector_v10.get_feature_res_actions(i),
    print


def print_of_feature_res_ports(of_xid, ports):
    print ('%s FeatureRes - port_id: %s hw_addr: %s name: %s' % (of_xid,
           green(ports['port_id']), green(ports['hw_addr']),
           green(ports['name'])))
    print ('%s FeatureRes - config:' % of_xid),
    for i in ports['config']:
        print ofp_dissector_v10.get_phy_config(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - state:' % of_xid),
    for i in ports['state']:
        print ofp_dissector_v10.get_phy_state(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - curr:' % of_xid),
    for i in ports['curr']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - advertised:' % of_xid),
    for i in ports['advertised']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - supported:' % of_xid),
    for i in ports['supported']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0',
    print
    print ('%s FeatureRes - peer:' % of_xid),
    for i in ports['peer']:
        print ofp_dissector_v10.get_phy_feature(i),
    else:
        print '0'


def print_ofp_match(xid, ofmatch):
    print ('%s OpenFlow Match -' % (xid)),
    for K in ofmatch:
        print ("%s: %s" % (K, green(ofmatch[K]))),
    print


def print_ofp_body(xid, ofbody):
    string = ('%s OpenFlow Body - Cookie: %s Command: %s Idle/Hard Timeouts: '
             '%s/%s Priority: %s Buffer ID: %s Out Port: %s Flags: %s')
    command = green(ofp_dissector_v10.get_ofp_command(ofbody['command']))
    flags = green(ofp_dissector_v10.get_ofp_flags(ofbody['flags']))

    print string % (xid, ofbody['cookie'], command, ofbody['idle_timeout'],
                    ofbody['hard_timeout'], ofbody['priority'],
                    ofbody['buffer_id'], ofbody['out_port'], flags)


def print_ofp_flow_removed(xid, ofrem):
    string = ('%s OpenFlow Body - Cookie: %s Priority: %s Reason: %s Pad: %s '
              'Duration Secs/NSecs: %s/%s Idle Timeout: %s Pad2/Pad3: %s/%s'
              ' Packet Count: %s Byte Count: %s')

    print string % (xid, ofrem['cookie'], ofrem['priority'], red(ofrem['reason']),
                    ofrem['pad'], ofrem['duration_sec'], ofrem['duration_nsec'],
                    ofrem['idle_timeout'], ofrem['pad2'], ofrem['pad3'],
                    ofrem['packet_count'], ofrem['byte_count'])


def print_ofp_action(xid, action_type, length, payload):
    if action_type == 0:
        port, max_len = get_action(action_type, length, payload)

        port = str('CONTROLLER(65533)' if port == 65533 else port)
        print ('%s OpenFlow Action - Type: %s Length: %s Port: %s Max Length: %s' %
               (xid, green('OUTPUT'), length, green(port), max_len))
        return 'output:' + port

    elif action_type == 1:
        vlan, pad = get_action(action_type, length, payload)
        print ('%s OpenFlow Action - Type: %s Length: %s VLAN ID: %s Pad: %s' %
               (xid, green('SetVLANID'), length, green(str(vlan)), pad))
        return 'mod_vlan_vid:' + str(vlan)

{'fraction_non_alphanumeric': 0.1626060034997981, 'fraction_numerical': 0.011845470453627676, 'mean_word_length': 2.7886282508924016, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3346407', 'n_tokens_mistral': 5454, 'n_tokens_neox': 5288, 'n_words': 1266}