def test_invite(self): """ """ self.ip.src = utils.random_ip() self.udp.sport = utils.random_port() self.message.uri = 'sip:{0}@{1}'.format(utils.random_data(20), utils.random_data(15)) self.message.method = 'INVITE' self.message.headers = { 'Call-ID': utils.random_tag(), 'CSeq': '0 INVITE', 'From': '"{0}" <sip:{1}@{2}>;tag={3}'.format(utils.random_data(10), utils.random_data(20), utils.random_data(15), utils.random_tag()), 'Max-Forwards': '{0}'.format(utils.random_number(2)), 'To': '<sip:{0}@{1}>'.format(utils.random_data(20), utils.random_data(15)), 'Via': 'SIP/2.0/UDP {0}:{1};branch={2};rport'.format( utils.random_ip(), utils.random_port(), utils.random_tag()), 'Content-Length': '0', 'User-Agent': '{0}'.format(utils.random_data(30)), 'Contact': '<sip:{0}@{1}:{2};transport=UDP>;' 'q=1.00;agentid="{3}";' 'methods="INVITE,NOTIFY,MESSAGE,ACK,BYE,CANCEL";' 'expires={4}'.format(utils.random_data(20), utils.random_ip(), utils.random_data(20), utils.random_tag(), utils.random_number(2)), 'Authorization': 'Digest username="******", ' 'realm="{2}", ' 'nonce="{3}", ' 'uri="sip:{4}", ' 'qop=auth, nc=00000001, ' 'cnonce="{5}", ' 'response="{6}", ' 'opaque=""'.format(utils.random_data(20), utils.random_data(15), utils.random_data(15), utils.random_tag(), utils.random_data(15), utils.random_tag(), utils.random_data(32)) }
def author(controller_path, cfg): ubitname = raw_input("UBIT Name: ") link_costs = [ ('1', '2', 3), ('1', '3', 1), ('1', '4', 7), ('2', '4', 2), ('4', '5', 2), ('4', '3', 1), ('5', '2', 6), ] cntrl_port = utils.random_port() remote_api.init_remote_assignment(cfg, cntrl_port) ROUTERS, ROUTER_ID_MAPPING = utils.gen_topology(link_costs, cntrl_port) for router in ROUTERS: success = False utils.run_cmd([controller_path, '-t', 'topology', '-a', str(router.id), '-o', 'response.pkt']) try: with open('response.pkt', 'r') as f: header = struct.unpack('!4sBBH', f.read(8)) controller_ip, cntrl_code, resp_code, payload_len = utils.parse_response_header(header) payload = f.read(payload_len) os.system('hexdump -C response.pkt') if payload == 'I, '+ubitname+', have read and understood the course academic integrity policy.': success = True except: success = False remote_api.cleanup(cfg) utils.cleanup() print success
def grade_data(controller_path, link_costs, pass_stats, pass_file, pass_fail, expected_path_success, expected_path_fail, init_ttl_success, init_ttl_fail, src, dst, cfg): score = 0.0 cntrl_port = utils.random_port() remote_api.init_remote_assignment(cfg, cntrl_port) ROUTERS, ROUTER_ID_MAPPING = utils.gen_topology(link_costs, cntrl_port, scramble=True) init_ttl = init_ttl_success transfer_id = random.randint(1,100) init_seq_num = random.randint(1,100) filename = 'testfile1' # Move file on src router remote_api.copy_file_to(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[src]), ROUTERS), filename, filename) # Remove file on dst router (if it exists) remote_api.delete_file_from(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[dst]), ROUTERS), 'file-*') # INIT utils.run_cmd([controller_path, '-t', 'topology', '-i', '1', '-o', 'response.pkt']) os.system('rm response.pkt*') #cleanup sleep(2) # Do File Transfer remote_api.run_cmd(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[src]), ROUTERS), 'chmod 777 '+filename) utils.run_cmd([controller_path, '-t', 'topology', '-f', ROUTER_ID_MAPPING[src], ROUTER_ID_MAPPING[dst], str(init_ttl), str(transfer_id), str(init_seq_num), filename, '-o', 'response.pkt']) sleep(8) # Checks File stats/meta expected_path = expected_path_success if check_file_transfer(controller_path, init_ttl, transfer_id, init_seq_num, filename, expected_path, ROUTER_ID_MAPPING): score += pass_stats # Check File itself if compare_files(cfg, filename, utils.get_router_ip(int(ROUTER_ID_MAPPING[dst]), ROUTERS), 'file-'+str(transfer_id)): score += pass_file init_ttl = init_ttl_fail transfer_id = random.randint(1,100) init_seq_num = random.randint(1,100) filename = 'testfile2' # Move file on src router remote_api.copy_file_to(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[src]), ROUTERS), filename, filename) # Remove file on dst router (if it exists) remote_api.delete_file_from(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[dst]), ROUTERS), 'file-*') # Do File Transfer remote_api.run_cmd(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[src]), ROUTERS), 'chmod 777 '+filename) utils.run_cmd([controller_path, '-t', 'topology', '-f', ROUTER_ID_MAPPING[src], ROUTER_ID_MAPPING[dst], str(init_ttl), str(transfer_id), str(init_seq_num), filename, '-o', 'response.pkt']) sleep(8) # Checks File stats/meta and File Transfer failure expected_path = expected_path_fail if check_file_transfer(controller_path, init_ttl, transfer_id, init_seq_num, filename, expected_path, ROUTER_ID_MAPPING) and not (compare_files(cfg, filename, utils.get_router_ip(int(ROUTER_ID_MAPPING[dst]), ROUTERS), 'file-'+str(transfer_id))): score += pass_fail remote_api.cleanup(cfg) utils.cleanup() return score
def grade_update(controller_path, link_costs, link_update, updated_link_costs, cfg): cntrl_port = utils.random_port() remote_api.init_remote_assignment(cfg, cntrl_port) ROUTERS, ROUTER_ID_MAPPING = utils.gen_topology(link_costs, cntrl_port) utils.run_cmd([controller_path, '-t', 'topology', '-i', '1', '-o', 'response.pkt']) os.system('rm response.pkt*') #cleanup sleep(3) utils.run_cmd([controller_path, '-t', 'topology', '-u', ROUTER_ID_MAPPING[link_update[0]], ROUTER_ID_MAPPING[link_update[1]], str(link_update[2]), '-o', 'response.pkt']) os.system('rm response.pkt*') #cleanup sleep(3) success = False for src in ROUTERS: success = False utils.run_cmd([controller_path, '-t', 'topology', '-r', str(src.id), '-o', 'response.pkt']) try: with open('response.pkt', 'r') as f: header = struct.unpack('!4sBBH', f.read(8)) controller_ip, cntrl_code, resp_code, payload_len = utils.parse_response_header(header) payload = f.read(payload_len) os.system('hexdump -C response.pkt') if payload_len != len(ROUTERS)*8: break r_table = parse_routing_table(payload) #Build expected routing table expected_r_table = [] for dst in ROUTERS: expected_cost, nxt_hop = get_shortest_path(ROUTER_ID_MAPPING[str(src.id)], ROUTER_ID_MAPPING[str(dst.id)], updated_link_costs, ROUTER_ID_MAPPING) if expected_cost == 65535: nxt_hop = 65535 expected_r_table.append( [dst.id, nxt_hop, expected_cost] ) if cmp(sorted(r_table), sorted(expected_r_table)) == 0: success = True else: success = False break except: success = False break utils.delete_file('response.pkt') #cleanup remote_api.cleanup(cfg) utils.cleanup() return success
def grade_bonus(controller_path, link_costs, src, dst, init_ttls, file_list, expected_path, cfg): score = 0.0 cntrl_port = utils.random_port() remote_api.init_remote_assignment(cfg, cntrl_port) ROUTERS, ROUTER_ID_MAPPING = utils.gen_topology(link_costs, cntrl_port, scramble=True) transfer_ids = [] init_seq_nums = [] for index in range(len(file_list)): transfer_ids.append(random.randint(1,100)) init_seq_nums.append(random.randint(1,100)) # Move file on src router remote_api.copy_file_to(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[src[index]]), ROUTERS), file_list[index], file_list[index]) remote_api.run_cmd(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[src[index]]), ROUTERS), 'chmod 777 '+file_list[index]) # Remove file on dst router (if it exists) remote_api.delete_file_from(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING[dst[index]]), ROUTERS), 'file-*') # INIT utils.run_cmd([controller_path, '-t', 'topology', '-i', '1', '-o', 'response.pkt']) os.system('rm response.pkt*') #cleanup sleep(2) # Do File Transfer init_multi_file_transfer(controller_path, src, dst, init_ttls, transfer_ids, init_seq_nums, file_list, ROUTER_ID_MAPPING) sleep(8) os.system('rm multi_run_script') for index in range(len(file_list)): # Checks File stats/meta if check_file_transfer(controller_path, init_ttls[index], transfer_ids[index], init_seq_nums[index], file_list[index], expected_path[index], ROUTER_ID_MAPPING, multi=True): score += 10.0 # Check File itself if compare_files(cfg, file_list[index], utils.get_router_ip(int(ROUTER_ID_MAPPING[dst[index]]), ROUTERS), 'file-'+str(transfer_ids[index])): score += 10.0 remote_api.cleanup(cfg) utils.cleanup() if score == len(file_list)*20: return True else: return False
from utils import create_ssh_container, random_hostname, random_pwd, random_port parser = argparse.ArgumentParser(description='Docker Multi SSH') parser.add_argument( 'number', type=int, help='The number of the ssh-server containers you want to created') parser.add_argument('--mem', type=int, help='limit mem', default=128) args = parser.parse_args() count = args.number mem_limit = args.mem ssh_servers = [] tmp_ports = set() for i in range(0, count): port = random_port() while port in tmp_ports: port = random_port() tmp_ports.add(port) tmp_ports.add(port) ssh_servers.append({ 'hostname': random_hostname(), 'password': random_pwd(), 'ssh_port': port }) for server in ssh_servers: container = create_ssh_container(server['hostname'], server['password'], server['ssh_port'],
def crash(controller_path, cfg): score = 0.0 link_costs = [ ('1', '2', 1), ('2', '3', 1), ('3', '4', 1), ('4', '5', 1) ] cntrl_port = utils.random_port() remote_api.init_remote_assignment(cfg, cntrl_port) ROUTERS, ROUTER_ID_MAPPING = utils.gen_topology(link_costs, cntrl_port, scramble=True) utils.run_cmd([controller_path, '-t', 'topology', '-i', '1', '-o', 'response.pkt']) os.system('rm response.pkt*') #cleanup sleep(3) nonce = random.randint(100,1000) utils.run_cmd([controller_path, '-t', 'topology', '-a', ROUTER_ID_MAPPING['1'], '-o', 'response-author-'+str(nonce)+'.pkt']) before_author = False if os.path.isfile('response-author-'+str(nonce)+'.pkt'): before_author = True # Crash router utils.run_cmd([controller_path, '-t', 'topology', '-c', ROUTER_ID_MAPPING['1'], '-o', 'response.pkt']) os.system('rm response.pkt*') #cleanup nonce = random.randint(100,1000) utils.run_cmd([controller_path, '-t', 'topology', '-a', ROUTER_ID_MAPPING['1'], '-o', 'response-author-'+str(nonce)+'.pkt']) after_author = False if os.path.isfile('response-author-'+str(nonce)+'.pkt'): after_author = True if before_author and (not after_author): score += 5.0 sleep(4) utils.run_cmd([controller_path, '-t', 'topology', '-r', ROUTER_ID_MAPPING['3'], '-o', 'response.pkt']) before_cost = None try: with open('response.pkt', 'r') as f: header = struct.unpack('!4sBBH', f.read(8)) controller_ip, cntrl_code, resp_code, payload_len = utils.parse_response_header(header) payload = f.read(payload_len) os.system('hexdump -C response.pkt') if payload_len == len(ROUTERS)*8: r_table = parse_routing_table(payload) for entry in r_table: if entry[0] == int(ROUTER_ID_MAPPING['1']): before_cost = entry[2] except: pass sleep(2) utils.run_cmd([controller_path, '-t', 'topology', '-r', ROUTER_ID_MAPPING['3'], '-o', 'response.pkt']) after_cost = None try: with open('response.pkt', 'r') as f: header = struct.unpack('!4sBBH', f.read(8)) controller_ip, cntrl_code, resp_code, payload_len = utils.parse_response_header(header) payload = f.read(payload_len) os.system('hexdump -C response.pkt') if payload_len == len(ROUTERS)*8: r_table = parse_routing_table(payload) for entry in r_table: if entry[0] == int(ROUTER_ID_MAPPING['1']): after_cost = entry[2] except: pass if before_cost and after_cost and (after_cost > before_cost): score += 5.0 os.system('rm response-author*') #cleanup remote_api.cleanup(cfg) utils.cleanup() print score
def rupdates(controller_path, cfg): score = 0.0 link_costs = [ ('1', '2', 3) ] cntrl_port = utils.random_port() remote_api.init_remote_assignment(cfg, cntrl_port) ROUTERS, ROUTER_ID_MAPPING = utils.gen_topology(link_costs, cntrl_port) utils.run_cmd([controller_path, '-t', 'topology', '-i', '2', '-o' 'response.pkt']) os.system('rm response.pkt*') #cleanup if hasattr(sys, '_MEIPASS'): base_path = sys._MEIPASS else: base_path = os.path.abspath('.') packet_receiver_path = os.path.join(base_path, 'packet_receiver.py') # Neighbor remote_api.kill_process(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING['2']), ROUTERS), ROUTERS[1].control_port) remote_api.run_script(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING['2']), ROUTERS), str(ROUTERS[1].router_port)+' 4 '+remote_api.ASSIGNMENT_PATH, packet_receiver_path) # Non-Neighbor remote_api.kill_process(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING['3']), ROUTERS), ROUTERS[2].control_port) remote_api.run_script(cfg, utils.get_router_ip(int(ROUTER_ID_MAPPING['3']), ROUTERS), str(ROUTERS[2].router_port)+' 4 '+remote_api.ASSIGNMENT_PATH, packet_receiver_path) sleep(4) os.system('rm -f num_updates*') os.system('rm -f update-packet-*') # Check neighbor check = False remote_api.copy_file_from(cfg, ROUTERS[1].ip_addr, 'num_updates', 'num_updates_neighbor') if os.path.isfile('num_updates_neighbor'): check = True score += 2.5 # Check non-neighbor if check: os.system('rm -f num_updates_non-neighbor') remote_api.copy_file_from(cfg, ROUTERS[2].ip_addr, 'num_updates', 'num_updates_non-neighbor') if not os.path.isfile('num_updates_non-neighbor'): score += 2.5 with open('num_updates_neighbor', 'r') as f: num_updates = int(f.read()) if num_updates == 2 or num_updates == 3: score += 5.0 remote_api.copy_file_from(cfg, ROUTERS[1].ip_addr, os.path.join(remote_api.ASSIGNMENT_PATH, 'update-packet-'+str(num_updates-1)), './') with open('update-packet-'+str(num_updates-1), 'rb') as f: pkt = f.read() num_updates, src_router_port, src_router_ip = struct.unpack('!HH4s', pkt[:8]) updates = [] pkt = pkt[8:] if len(pkt) == num_updates*12: for index in range(num_updates): updates.append(struct.unpack('!4sHHHH', pkt[index*12:(index*12)+12])) # Build expected updates expected_updates = [] for router in ROUTERS: cost = 65535 if router.id == 1: cost = 0 if router.id == 2: cost = 3 update = (socket.inet_aton(router.ip_addr), router.router_port, 0, router.id, cost) expected_updates.append(update) if (num_updates == 5) and (src_router_port == ROUTERS[0].router_port) and (src_router_ip == socket.inet_aton(ROUTERS[0].ip_addr)) and (cmp(sorted(updates), sorted(expected_updates)) == 0): score += 5.0 os.system('rm -f num_updates*') os.system('rm -f update-packet-*') remote_api.cleanup(cfg) utils.cleanup() print score