def test_packet_setContent(self, type_p, content, id_p): p = Packet(type_p, content, id_packet=id_p) ip_addr = ipaddress.ip_network("10.0.0.0/24") r = Route(ip_addr, [], "5") p.content = r assert str(p.content) == str(r)
def handleTime(self, timeMillisecs): """TODO: handle current time""" if timeMillisecs - self.last_time > self.heartbeatTime: for k, v in self.table.iteritems(): # if k != v['nextHop']: packet = Packet(Packet.ROUTING, self.addr, k) packet.content = dumps(self.table) self.send(v["port"], packet) self.last_time = timeMillisecs
def handleRemoveLink(self, port): """TODO: handle removed link""" for k, v in self.table.iteritems(): if v["port"] == port: # keysToRemove.append(k) self.table[k] = { "cost": self.infinity, "nextHop": None, "port": None, } for k, v in self.table.iteritems(): # if k != v['nextHop']: packet = Packet(Packet.ROUTING, self.addr, k) packet.content = dumps(self.table) self.send(v["port"], packet)
def handleNewLink(self, port, endpoint, cost): """TODO: handle new link""" # should store the argument values in a data structure to use for routing. # If you want to send packets along this link, call self.send(port, packet) if endpoint not in self.table or cost < self.table[endpoint]["cost"]: self.table[endpoint] = { "cost": cost, "nextHop": endpoint, "port": port } for k, v in self.table.iteritems(): # if k != v['nextHop']: packet = Packet(Packet.ROUTING, self.addr, k) packet.content = dumps(self.table) self.send(v["port"], packet)
def handlePacket(self, port, packet): """TODO: process incoming packet""" # check whether the packet is a traceroute packet or a routing packet and # handle it appropriately if packet.isTraceroute(): if packet.dstAddr in self.table: self.send(self.table[packet.dstAddr]["port"], packet) elif packet.isRouting(): content = loads(packet.content) for k, v in content.iteritems(): if k != self.addr: if v["cost"] == self.infinity and v[ 'nextHop'] == packet.srcAddr: self.table[k]["cost"] = self.infinity self.table[k]['nextHop'] = None # packet.srcAddr self.table[k]['port'] = None for k, v in self.table.iteritems(): # if k != v['nextHop']: packet = Packet(Packet.ROUTING, self.addr, k) packet.content = dumps(self.table) self.send(v["port"], packet) if k not in self.table: self.table[k] = { "cost": v["cost"] + self.table[packet.srcAddr]["cost"], "nextHop": packet.srcAddr, "port": self.table[packet.srcAddr]["port"], } else: if self.table[packet.srcAddr]["cost"] + v[ "cost"] < self.table[k]["cost"]: self.table[k]["cost"] = self.table[ packet.srcAddr]["cost"] + v["cost"] self.table[k]["nextHop"] = packet.srcAddr self.table[k]["port"] = self.table[ packet.srcAddr]["port"]
bullet_count = 0 for c in packet_string: if(c == "|"): bullet_count += 1 if(bullet_count == 1 and c != " " and c != "|"): pkt_type += c if(bullet_count == 2 and c != " " and c != "|"): src_ip += c if(bullet_count == 3 and c != " " and c != "|"): dest_ip += c if(bullet_count == 4 and c != " " and c != "|"): content += c packet.pkt_type = pkt_type packet.src_ip = src_ip packet.dest_ip = dest_ip packet.content = content packet.datetime = datetime.now() packet_list.append(packet) log = "" udp_flag = True http_flag = True f = open("Output.txt", "w") for p in packet_list: if(p.pkt_type == "udp"): if udp_flag == True: p.server = "udp_1" udp_flag = False else: p.server = "udp_2" udp_flag = True print("Mensagem: " + p.src_ip + ": " + p.content + "\n")