def _handle_packet(self, packet: Packet) -> Packet: if packet.packet_type == PacketType.REGISTERED_LOGIN_REQUEST: self.user = User.from_bytes_and_ip(packet.data, self.client_address[0]) return Packet(PacketType.LOGIN_ACKNOWLEDGE, "*****@*****.**") if packet.packet_type == PacketType.ADD_A_FILE_TO_SHARED_FILE_INDEX: index_server = IndexServer() file = File.from_bytes(packet.data) index_server.add(file, self.user) return None if (packet.packet_type == PacketType.UNSHARE_ALL_FILES_FROM_THE_SHARED_FILE_INDEX): index_server = IndexServer() removed = index_server.remove_all(self.user) return Packet( PacketType.UNSHARE_ALL_FILES_FROM_THE_SHARED_FILE_INDEX, str(removed)) if packet.packet_type == PacketType.SEARCH_QUERY_REQUEST: index_server = IndexServer() results = index_server.search(packet.data.decode("ascii")) for result in results: packet = Packet(PacketType.SEARCH_QUERY_RESULTS, str(result)) self.request.sendall(bytes(packet)) return Packet(PacketType.SEARCH_QUERY_RESULTS_END_NOTIFICATION, "") return Packet(PacketType.LOGOUT_OR_ERROR_MESSAGE_FROM_SERVER, "error")
def test_when_set_id_should_store_id(self): # Given p = Packet("receiver", " sender", []) # When p.set_id(11) # Then self.assertEqual(p.get_id(), 11)
def main(): # parameters Sim.scheduler.reset() Sim.set_debug("Node") # setup network net = Network("test2network.txt") for hostname, node in sorted(net.nodes.iteritems()): node.add_protocol(protocol="dvrouting", handler=RoutingApp(node)) n3 = net.get_node("n3") n3.add_protocol(protocol="delay", handler=DelayHandler()) p = Packet(destination_address=n3.get_address("n2"), ident=1, protocol="delay", length=1000) n2 = net.get_node("n2") Sim.scheduler.add(delay=1.2, event=p, handler=n2.send_packet) Sim.scheduler.add(delay=1.6, event=None, handler=n2.get_link("n3").down) Sim.scheduler.add(delay=1.6, event=None, handler=n3.get_link("n2").down) p = Packet(destination_address=n3.get_address("n2"), ident=2, protocol="delay", length=1000) Sim.scheduler.add(delay=6.6, event=p, handler=n2.send_packet) # run the simulation Sim.scheduler.run()
def test_when_add_events_should_store_new_events(self): # Given p = Packet("receiver", " sender", [1]) # When p.add_events([2, 3]) # Then self.assertEqual(p.get_events(), [1, 2, 3])
def scenario_three(): # (0, 1, 0.018000000000000002) # (0, 2, 0.026000000000000002) # (0, 3, 0.034) # (2.0, 4, 2.018) Sim.scheduler.reset() n1, n2 = setup_network('networks/two_nodes_3.txt') packet1 = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) packet2 = Packet(destination_address=n2.get_address('n1'), ident=2, protocol='delay', length=1000) packet3 = Packet(destination_address=n2.get_address('n1'), ident=3, protocol='delay', length=1000) packet4 = Packet(destination_address=n2.get_address('n1'), ident=4, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=packet1, handler=n1.send_packet) Sim.scheduler.add(delay=0, event=packet2, handler=n1.send_packet) Sim.scheduler.add(delay=0, event=packet3, handler=n1.send_packet) Sim.scheduler.add(delay=2, event=packet4, handler=n1.send_packet) Sim.scheduler.run()
def __init__(self, source_address=1, source_port=0, destination_address=1, destination_port=0, ident=0, ttl=100, protocol="TCP", body="", length=0, syn=False, ack=False, fin=False, sequence=0, ack_number=0, sent_time=0): Packet.__init__(self, source_address=source_address, source_port=source_port, destination_address=destination_address, destination_port=destination_port, ttl=ttl, ident=ident, protocol=protocol, body=body, length=length) self.sequence = sequence self.ack_number = ack_number self.sent_time = sent_time
def __init__(self, source_address=1, source_port=0, destination_address=1, destination_port=0, ident=0, ttl=100, protocol="TCP", body="", length=0, syn=False, ack=False, fin=False, sequence=0, ack_number=0, sent_time=0, ack_packet_length=0, drop=False): Packet.__init__(self, source_address=source_address, source_port=source_port, destination_address=destination_address, destination_port=destination_port, ttl=ttl, ident=ident, protocol=protocol, body=body, length=length) self.sequence = sequence self.ack_number = ack_number self.sent_time = sent_time # The size of the packet received self.ack_packet_length = ack_packet_length # Determine whether or not to drop the packet self.drop = drop
def main(): # parameters Sim.scheduler.reset() Sim.set_debug(True) # setup network net = Network('../networks/five-nodes.txt') # get nodes n1 = net.get_node('n1') n2 = net.get_node('n2') n3 = net.get_node('n3') n4 = net.get_node('n4') n5 = net.get_node('n5') # setup broadcast application b1 = BroadcastApp(n1) n1.add_protocol(protocol="broadcast", handler=b1) b2 = BroadcastApp(n2) n2.add_protocol(protocol="broadcast", handler=b2) b3 = BroadcastApp(n3) n3.add_protocol(protocol="broadcast", handler=b3) b4 = BroadcastApp(n4) n4.add_protocol(protocol="broadcast", handler=b4) b5 = BroadcastApp(n5) n5.add_protocol(protocol="broadcast", handler=b5) # send a broadcast packet from 1 with TTL 2, so everyone should get it p = Packet(source_address=n1.get_address('n2'), destination_address=0, ident=1, ttl=2, protocol='broadcast', length=100) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # send a broadcast packet from 1 with TTL 1, so just nodes 2 and 3 # should get it p = Packet(source_address=n1.get_address('n2'), destination_address=0, ident=2, ttl=1, protocol='broadcast', length=100) print("this is an address of n2", n1.get_address('n3')) Sim.scheduler.add(delay=1, event=p, handler=n1.send_packet) # send a broadcast packet from 3 with TTL 1, so just nodes 1, 4, and 5 # should get it p = Packet(source_address=n3.get_address('n1'), destination_address=0, ident=3, ttl=1, protocol='broadcast', length=100) Sim.scheduler.add(delay=2, event=p, handler=n3.send_packet) # run the simulation Sim.scheduler.run()
def main(): # parameters Sim.scheduler.reset() Sim.set_debug("Node") # setup network net = Network("test3network.txt") for hostname, node in sorted(net.nodes.iteritems()): node.add_protocol(protocol="dvrouting", handler=RoutingApp(node)) n7 = net.get_node("n7") n7.add_protocol(protocol="delay", handler=DelayHandler()) n10 = net.get_node("n10") n10.add_protocol(protocol="delay", handler=DelayHandler()) n15 = net.get_node("n15") n15.add_protocol(protocol="delay", handler=DelayHandler()) n8 = net.get_node("n8") n11 = net.get_node("n11") n12 = net.get_node("n12") p1 = Packet(destination_address=n7.get_address("n6"), ident=1, protocol="delay", length=1000) Sim.scheduler.add(delay=4.2, event=p1, handler=n12.send_packet) p3 = Packet(destination_address=n15.get_address("n14"), ident=2, protocol="delay", length=1000) Sim.scheduler.add(delay=4.3, event=p3, handler=n11.send_packet) p2 = Packet(destination_address=n10.get_address("n1"), ident=3, protocol="delay", length=1000) Sim.scheduler.add(delay=4.4, event=p2, handler=n8.send_packet) n2 = net.get_node("n2") Sim.scheduler.add(delay=4.8, event=None, handler=n2.get_link("n8").down) Sim.scheduler.add(delay=4.8, event=None, handler=n8.get_link("n2").down) p4 = Packet(destination_address=n10.get_address("n1"), ident=4, protocol="delay", length=1000) Sim.scheduler.add(delay=10.4, event=p4, handler=n8.send_packet) Sim.scheduler.add(delay=10.8, event=None, handler=n2.get_link("n8").up) Sim.scheduler.add(delay=10.8, event=None, handler=n8.get_link("n2").up) p5 = Packet(destination_address=n10.get_address("n1"), ident=5, protocol="delay", length=1000) Sim.scheduler.add(delay=16.4, event=p5, handler=n8.send_packet) # run the simulation Sim.scheduler.run()
def test_when_get_no_existed_event_count_should_return_zero(self): # Given p = Packet("receiver", " sender", []) # When event_a_log_count = p.get_event_count("A") event_b_log_count = p.get_event_count("B") # Then self.assertEqual(event_a_log_count, 0) self.assertEqual(event_b_log_count, 0)
def main(): # parameters Sim.scheduler.reset() # setup network #net = Network('./networks/five-nodes-line.txt') #net = Network('./networks/five-nodes-ring.txt') net = Network('./networks/fifteen-nodes.txt') # Setup broadcast protocol for all nodes in the network packet_count = 1 for k, n in net.nodes.iteritems(): b = BroadcastApp(n) ph = PacketHandler(k) n.add_protocol(protocol="broadcast", handler=b) n.add_protocol(protocol="transmit", handler=ph) n.init_routing() pbody = { "hostname": n.hostname, "dv": n.get_distance_vector() } p = Packet( destination_address=0, ident=packet_count, ttl=1, protocol='broadcast', body=pbody) Sim.scheduler.add(delay=0, event=p, handler=n.send_packet) packet_count = packet_count + 1 # Send a packet after everything has been setup n1 = net.get_node('n1') n4 = net.get_node('n4') n5 = net.get_node('n5') daddr = 5 p = Packet(destination_address=daddr, ident=2, protocol='transmit', length=1000) Sim.scheduler.add(delay=5, event=p, handler=n1.send_packet) if True: # Take a link down (between n5 and n1) Sim.scheduler.add(delay=10, event=None, handler=net.get_link(daddr-1).down) Sim.scheduler.add(delay=10, event=None, handler=net.get_link(daddr-1).down) # Send a packet p = Packet(destination_address=daddr, ident=3, protocol='transmit', length=1000) Sim.scheduler.add(delay=20, event=p, handler=n1.send_packet) # Bring the link back up Sim.scheduler.add(delay=30, event=None, handler=net.get_link(daddr-1).up) Sim.scheduler.add(delay=30, event=None, handler=net.get_link(daddr-1).up) # Send a packet p = Packet(destination_address=daddr, ident=2, protocol='transmit', length=1000) Sim.scheduler.add(delay=40, event=p, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def test_when_get_existed_event_count_should_return_log_count(self): # Given e1 = Event("LOGAA", 2, 1, 1, 1, 1) e2 = Event("LOGAB", 3, 1, 1, 1, 1) p = Packet("receiver", " sender", [e1, e2]) # When event_a_log_count = p.get_event_count("A") event_b_log_count = p.get_event_count("B") # Then self.assertEqual(event_a_log_count, 2) self.assertEqual(event_b_log_count, 3)
def test_when_init_should_store_fields(self): # Given p = Packet("receiver", " sender", []) # When rec = p.get_receiver() sen = p.get_sender() events = p.get_events() # Then self.assertEqual(rec, "receiver") self.assertEqual(sen, "sender") self.assertEqual(events, [])
def __init__(self,source_address=1,source_port=0, destination_address=0,destination_port=0, ident=0,ttl=1,protocol="dvrouting",body="",length=0, source_hostname="",dvs={}): Packet.__init__(self,source_address=source_address, source_port=source_port, destination_address=destination_address, destination_port=destination_port, ttl=ttl,ident=ident,protocol=protocol, body=body,length=length) self.source_hostname = source_hostname self.dvs = dvs
def __init__(self,source_address=1,source_port=0, destination_address=1,destination_port=0, ident=0,ttl=100,protocol="TCP",body="",length=0, syn=False,ack=False,fin=False,sequence=0, ack_number=0,sent_time=0): Packet.__init__(self,source_address=source_address, source_port=source_port, destination_address=destination_address, destination_port=destination_port, ttl=ttl,ident=ident,protocol=protocol, body=body,length=length) self.sequence = sequence self.ack_number = ack_number self.sent_time = sent_time
def main(): d = DelayHandler() for i in range(1, 4): Sim.scheduler.reset() net = Network('./threenodes' + str(i) + '.txt') n1 = net.get_node('n1') n2 = net.get_node('n2') n3 = net.get_node('n3') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n1.add_forwarding_entry(address=n3.get_address('n2'), link=n1.links[0]) n2.add_forwarding_entry(address=n3.get_address('n2'), link=n2.links[1]) net.nodes['n3'].add_protocol(protocol="delay", handler=d) n1_transmission_delay = 8000.0 / n1.links[0].bandwidth for j in range(0, 1001): protocol = None if (j > 995): protocol = 'delay' packet = Packet(destination_address=n3.get_address('n2'), ident=j, protocol=protocol, length=1000) delay = j * n1_transmission_delay Sim.scheduler.add(delay=delay, event=packet, handler=n1.send_packet) Sim.scheduler.run() print("\n")
def gossip(self): #make packet with dv in it. p = Packet( source_address=n1.get_address('n2'), destination_address=0, ident=0, ttl=1, protocol='dvrouting', body=dv) Sim.scheduler.add(delay=1, event=p, handler=self.node.send_packet)
def part2(config): Sim.scheduler.reset() # setup network net = Network(config) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n3 = net.get_node('n3') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n1.add_forwarding_entry(address=n3.get_address('n2'), link=n1.links[0]) n2.add_forwarding_entry(address=n3.get_address('n2'), link=n2.links[1]) # setup app d = DelayHandler() net.nodes['n2'].add_protocol(protocol="delay", handler=d) net.nodes['n3'].add_protocol(protocol='delay', handler=d) # send one packet for i in range(0,1000): Sim.scheduler.add(delay=i * 0.03125, event = Packet(destination_address=n3.get_address('n2'), ident=i, protocol='delay', length=1000), handler=n1.send_packet ) # p1 = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) # Sim.scheduler.add(delay=0, event=p1, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def receive_packet(self, packet): change = self.node.update_distance_vector(packet.body["hostname"], packet.body["dv"]) # If we haven't heard from a neighbor link in 40 seconds, drop the link and update the distance vector for host in self.node.distance_vectors: if host != self.node.hostname and Sim.scheduler.current_time() - self.node.get_distance_vector_time(host) > 6: change = True self.node.remove_distance_vector(host) break # If new values were detected and the forwarding entries were changed, broadcast new values delay = 2 if change: print("Change!", self.node.hostname, self.node.distance_vectors[self.node.hostname]["dv"]) delay = 1 # Setup new event to rebroadcast pbody = { "hostname": self.node.hostname, "dv": self.node.get_distance_vector() } p = Packet( destination_address=0, ident=0, ttl=1, protocol='broadcast', body=pbody) Sim.scheduler.add(delay=delay, event=p, handler=self.node.send_packet)
def main(): # parameters Sim.scheduler.reset() # setup network net = Network('threenodes2.txt') # setup routes node_a = net.get_node('A') node_b = net.get_node('B') node_c = net.get_node('C') node_a.add_forwarding_entry(address=node_b.get_address('A'), link=node_a.links[0]) node_b.add_forwarding_entry(address=node_c.get_address('B'), link=node_b.links[0]) # setup app d = DelayHandler() net.nodes['A'].add_protocol(protocol='delay', handler=d) net.nodes['B'].add_protocol(protocol='delay', handler=d) net.nodes['C'].add_protocol(protocol='delay', handler=d) # send one packet for i in range(1000): p = Packet(destination_address=node_c.get_address('B'), ident=i, protocol='delay', length=1000) Sim.scheduler.add(delay=i * .008, event=p, handler=node_a.send_packet) # run the simulation Sim.scheduler.run()
def run(nodes): Sim.scheduler.reset() apps = [] for node in nodes: b = BroadcastApp(node) node.add_protocol(protocol="dvrouting", handler=b) apps.append(b) for app in apps: app.broadcast() n1 = nodes[0] n3 = nodes[2] n4 = nodes[3] n5 = nodes[4] p = Packet( destination_address=5, ident=123897, ttl=5, protocol='print', length=100) printing_app = PrintingApp() n3.add_protocol(protocol='print', handler=printing_app) Sim.scheduler.add(delay=200, event=p, handler=n1.send_packet) # Sim.scheduler.add(delay=100, event=None, handler=n4.get_link('n1').up) Sim.scheduler.run()
def part1_2(): # parameters Sim.scheduler.reset() # setup network net = Network('../networks/lab1/2nodes-2.txt') # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup app d = DelayHandler() net.nodes['n2'].add_protocol(protocol="delay", handler=d) # send one packet p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def main(): # parameters Sim.scheduler.reset() # setup network net = Network('../networks/one-hop.txt') # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup app d = DelayHandler() net.nodes['n2'].add_protocol(protocol="delay", handler=d) # send one packet p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # take the link down Sim.scheduler.add(delay=1, event=None, handler=n1.get_link('n2').down) # send one packet (it won't go through) p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=1.1, event=p, handler=n1.send_packet) # bring the link up Sim.scheduler.add(delay=2, event=None, handler=n1.get_link('n2').up) # send one packet (and now it goes through) p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=2.1, event=p, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def _login(self): data = f'{generate_username()} nopass 6699 "bangbangnapster 0.1" 3' packet = Packet(PacketType.REGISTERED_LOGIN_REQUEST, data) self.sock.sendall(bytes(packet)) logged_in = read_packet(self.sock) if logged_in.packet_type != PacketType.LOGIN_ACKNOWLEDGE: print("failed to login, exiting") exit(1)
def _process(self, data: bytes): message = Packet.parse(data) if message.id in self.received: return self.received.add(message.id) if message.type is PacketType.MESSAGE or message.type is PacketType.ONLINE: self.got_messages.put(message) self.send(message) if message.type is PacketType.IP: cur_id, addr = message.data.split('/') self.ip_list.add(addr) self.send(message) if message.type is PacketType.GET_IP: self.send(message) for ip in self.ip_list: self.send( Packet(PacketType.IP, '{}/{}'.format(message.data, ip)))
def send(self, message: Packet): if message.id is None: message.id = random.randint(0, 2**60 - 1) if message.id in self._sent: return self._sent.add(message.id) self.sending_message_queue.put(message) self._sending.run()
def test_when_add_packet_should_assign_id_for_packet(self, decoder): # Given pm = PacketsManager() packet = Packet("dummy", " dummy", []) # When decoder.return_value.decode_packet.return_value = packet pm.add([]) # Then self.assertEqual(pm.get_latest_packet().get_id(), 1)
def test_when_add_packet_with_no_pushed_events_should_store_sender_only( self): # Given p = Packet("A", "B", []) # When self.cm.add_packet(p) # Then channels = [c.get_name() for c in self.cm.get_channels()] self.assertEqual(channels, ['A'])
def __init__(self, node): self.node = node self.dv = {} self.dv[self.node.get_address(self.node.hostname)] = 0 p = Packet( source_address=n1.get_address('n2'), destination_address=0, ident=0, ttl=1, protocol='dvrouting', body=dv) Sim.scheduler.add(delay=1, event=p, handler=self.gossip)
def scenario_two(): # (0, 1, 80.01) n1, n2 = setup_network('networks/two_nodes_2.txt') packet = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=packet, handler=n1.send_packet) Sim.scheduler.run()
def scenario_two(): transmission_delay = 0.008 n1, n2, n3 = setup_network('networks/three_nodes_2.txt') for i in range(1000): packet = Packet(destination_address=n3.get_address('n2'), ident=i+1, protocol='delay', length=1000) Sim.scheduler.add(delay=i*transmission_delay, event=packet, handler=n1.send_packet) Sim.scheduler.run()
def test_can_convert_a_packet_to_bytes(): packet_length = b"\x1d\x00" # 29 packet_type = b"\x02\x00" # 2 packet_data = b'foo badpass 6699 "nap v0.8" 3' packet = Packet(PacketType.REGISTERED_LOGIN_REQUEST, packet_data.decode("ascii")) result = bytes(packet) assert result == packet_length + packet_type + packet_data
def __init__(self,source_address=1,source_port=0, destination_address=1,destination_port=0, ident=0,ttl=100,protocol="TCP",body="",length=0, syn=False,ack=False,fin=False,sequence=0, ack_number=0,sent_time=0,ack_packet_length=0, drop=False): Packet.__init__(self,source_address=source_address, source_port=source_port, destination_address=destination_address, destination_port=destination_port, ttl=ttl,ident=ident,protocol=protocol, body=body,length=length) self.sequence = sequence self.ack_number = ack_number self.sent_time = sent_time # The size of the packet received self.ack_packet_length = ack_packet_length # Determine whether or not to drop the packet self.drop = drop