def __init__(self, node_id, network, adapt_functions=None, **kwargs): """ :param node_id: any unique identifier :param network: a data structure holding every node """ Thread.__init__(self, **kwargs) # temp if adapt_functions is None: adapt_functions = [ AdaptationFunction(random.choice(("a", "b", "c")), random.choice(("a", "b", "c")), CV) for i in range(2) ] # --------------- communication related initialisation --------------- # # self.daemon = True self.id = node_id self.network = network self.wake_buffer = Queue() self.adapt_functions = adapt_functions self.routing_table = RoutingTable() for function in self.adapt_functions: self.routing_table.add_route(self.id, list(function._from), self.id, function, 0) # -------------------- counting sent and received -------------------- # self.conf_received = 0 self.conf_sent = 0
def cmdloop(self, routerID, routerDeadInterval, RouterPriority, hellointerval, inttransDelay): self.HelloInterval = hellointerval self.RouterID = routerID self.RouterDeadInterval = routerDeadInterval self.RouterPriority = RouterPriority self.IntTransDelay = inttransDelay self.listInterfaces = [] self.dataforUnicastDB = {} self.StartInterfacesList() self.LSDB = {} self.OpaqueID = 0 self.fakeRoutingTable = RoutingTable(self) self.realRoutingTable = ABRRoutingTable(self) self.thread = threading.Thread(target=self.multicastReceiver, args=()) self.thread.daemon = True self.thread.start() self.threadInc = threading.Thread(target=self.incrementLSATimer, args=()) self.threadInc.daemon = True self.threadInc.start() self.threadABR = False self.ABR = False self.threadUnicast = {} return cmd.Cmd.cmdloop(self)
def test_routing_table_add_valid(): table = RoutingTable() nodes = [i for i in range(10)] for node in nodes: H = random_stack(protocols, 20, 20) function = AdaptationFunction(H[-1], H[-1], CV) assert table.add_route(node, H, node, function, 1) row = table.get(node, H) assert row.cost == 1 and row.next_hop == node and row.function == function
def random_routing_table(n): table = RoutingTable() nodes = [i for i in range(n)] rd_prot = lambda: rd.choice(protocols) functions = [ AdaptationFunction(rd_prot(), rd_prot(), CV) for i in range(n) ] for i in range(3 * n): table.add_route(rd.choice(nodes), random_stack(protocols, 1, 10), rd.choice(nodes), rd.choice(functions), rd.randrange(100)) return table
def run(self): server = (self.ip, self.port) inputQueue = queue.Queue() outputQueue = queue.Queue() ##Creates the routingtable routingTable = RoutingTable(self.routingTableDir) #Starts the UDP server sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(server) print("Listening on " + self.ip + ":" + str(self.port)) ##Creates the Threads t = threading.Thread(target=inputThread, args=(inputQueue,sock,self.nodeID )) #t.start() t2 = threading.Thread(target=outputThread, args=(outputQueue,sock,routingTable )) t2.start() t3 = threading.Thread(target=logicalThread, args=(inputQueue,outputQueue,sock,self.blueGraphDir,self.nodeID )) t3.start() #Testing while True: test = int(input()) if test == 1: neighborList = [] testPack = obPackage(1,2,'e',0,"0.0.0.1",2,neighborList) ByteTestPack = testPack.serialize() inputQueue.put(ByteTestPack)
def read(self, lines): assert(isinstance(lines,list)) m = re.match("\s*getNodeId.*:[^0-9a-f]*([0-9a-f]+).*", lines[0]) if m: self.nodeId = m[1] lines = lines[1:] while len(lines) > 0: routingTable = RoutingTable() try: assert(isinstance(lines, list)) lines = routingTable.read(lines) assert(isinstance(lines, list)) self.routingTables.append(routingTable) except TableHeaderException as e: return lines except TableEntryException as e: return lines
def __init__(self, routingTableDir, dirGrafoAzul, timeout, ID): self.rand = random self.ip = '0.0.0.0' self.routingTable = RoutingTable(routingTableDir) self.colaEntrada = queue.Queue() self.colaSalida = queue.Queue() self.diccionariosACKs = { } # { SNRN_1:{1:'', 2:'', 3:'', 4:'', 5:''}, SNRN_2:{1:'', 2:'', 3:'', 4:'', 5:''}, SNRN_N:{1:'', 2:'', 3:'', 4:'', 5:''}} self.SNRN = self.rand.randrange(65536) self.secure_UDP = 0 self.port = 0000 self.nodeID = ID self.tablaNodosAzules = TablaNodosAzules(dirGrafoAzul) self.TIMEOUT = timeout self.semaphore = threading.Semaphore(0) self.timeStamp = time.time() self.blueNodesAsignedByMe = {}
class DHT(object): def __init__(self): # Session key self._key = os.urandom(20) # 20 random bytes == 160 bits self.krpc = KRPCServer(9001, "1.1.1") self.routing_table = RoutingTable() def ping_callback(self, message, connection): if "r" in message and "id" in message["r"]: message = message["r"] node_to_add = Node(connection, node_id=message["id"]) self.routing_table.add_or_update_node(node_to_add) def find_node_callback(self, message, connection): if "r" in message and "nodes" in message["r"]: for node in decode_nodes(message["r"]["nodes"]): found_node = Node(node[1], node_id=node[0]) self.krpc.send_query(build_ping_krpc_query(found_node.id), found_node, self.ping_callback)
def __init__(self, linkData, leafSwitchLids, spineSwitchLids): # the structure of the fabric self.linkData = linkData self.leafSwitchLids = set(leafSwitchLids) self.spineSwitchLids = set(spineSwitchLids) # generate routing table objects self.routingTables = {} for lid in leafSwitchLids + spineSwitchLids: self.routingTables[lid] = RoutingTable(linkData, lid)
def _generate_private_nodes(self, n): default_port = 5555 for i in range(0, n): self.users.append(User(f"private{i}", "127.0.0.1", default_port + i)) self.locks.append(threading.Lock()) self.tables.append(RoutingTable(self.users[i].node, self.bootstrap_node, self.bucket_limit, f"nodes{i}.txt", self.locks[i])) self.output_queues.append(deque()) self.server_threads.append( Server(self.users[i].node, self.tables[i], self.output_queues[i], self.k, self.connections_count)) self.server_threads[i].start() self.command_queues.append(deque()) self.client_threads.append( Client(self.users[i].node, self.tables[i], self.command_queues[i], self.k, self.alpha)) self.client_threads[i].start()
def main(self): self.login = "******" self.port = 4444 self.ip = "127.0.0.1" self.bucket_limit = 20 self.k = 10 self.connections_count = 10 self.user = User(self.login, self.ip, self.port) self.lock = threading.Lock() self.routing_table = RoutingTable(self.user.node, self.bootstrap_node, self.bucket_limit, "nodes.txt", self.lock) self.messages_output_queue = deque() self.server_thread = Server(self.user.node, self.routing_table, self.messages_output_queue, self.k, self.connections_count) self.server_thread.start() self.command_input_queue = deque() self.flask_thread = FlaskThread(self.command_input_queue, self.messages_output_queue) self.flask_thread.start() self.client_thread = Client(self.user.node, self.routing_table, self.command_input_queue, self.k, self.alpha) self.client_thread.start() while True: self.command_queues[0].append( f"{self.user.node.id} STORE priv2priv") time.sleep(1) self.command_queues[1].append( f"{self.users[self.private_nodes_count].node.id} STORE priv2pub1" ) time.sleep(1) self.command_queues[2].append( f"{self.users[self.private_nodes_count].node.id} STORE priv2pub2" ) time.sleep(1) self.command_queues[3].append( f"{self.users[self.private_nodes_count].node.id} STORE priv2pub3" ) time.sleep(1)
def run(self): inputQueue = queue.Queue() outputQueue = queue.Queue() # Create the routingTable routingTable = RoutingTable(self.routingTableDir) maxOrangeNodes = len(routingTable.table) # Creates the orange graph and the blueNodeTable table = blueNodeTable(self.blueGraphDir) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) self.ip = s.getsockname()[0] s.close() # Starts the UDP server sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((self.ip,self.port)) print("Listening Oranges on ip: %s port %d Im orange: %d" % (sock.getsockname()[0], self.port, self.nodeID)) # Starts the UDP Safe SecureUdpBlue = SecureUdp(100,1,True) #ventana de 100 con timeout de 1s print("Listening Blues on ip: %s port %d Im orange: %d" % (SecureUdpBlue.sock.getsockname()[0], SecureUdpBlue.sock.getsockname()[1], self.nodeID)) # Creates the Threads t = threading.Thread(target=inputThread, args=( inputQueue, outputQueue, sock, self.nodeID, self.debug, )) t.start() t2 = threading.Thread(target=outputThread, args=( outputQueue, sock, routingTable, self.debug, )) t2.start() t3 = threading.Thread(target=logicalThread, args=( inputQueue, outputQueue, sock, table, self.nodeID, maxOrangeNodes,self.debug,SecureUdpBlue)) t3.start() t4 = threading.Thread(target=inputThreadBlue, args=(SecureUdpBlue,inputQueue,)) t4.start()
class NodoNaranja: # Aqui se ponen los detalles para ajusta puerto y IP def __init__(self, routingTableDir, dirGrafoAzul, timeout, ID): self.rand = random self.ip = '0.0.0.0' self.routingTable = RoutingTable(routingTableDir) self.colaEntrada = queue.Queue() self.colaSalida = queue.Queue() self.diccionariosACKs = { } # { SNRN_1:{1:'', 2:'', 3:'', 4:'', 5:''}, SNRN_2:{1:'', 2:'', 3:'', 4:'', 5:''}, SNRN_N:{1:'', 2:'', 3:'', 4:'', 5:''}} self.SNRN = self.rand.randrange(65536) self.secure_UDP = 0 self.port = 0000 self.nodeID = ID self.tablaNodosAzules = TablaNodosAzules(dirGrafoAzul) self.TIMEOUT = timeout self.semaphore = threading.Semaphore(0) self.timeStamp = time.time() self.blueNodesAsignedByMe = {} def nextSNRN(self, SNRN): next = (SNRN + 1) % 65536 return next def run(self): for i in self.routingTable.table: if i.getNode() == self.nodeID: #print(i.print_data()) self.ip = i.getIp() self.port = i.getPort() self.secure_UDP = USL(self.ip, self.port, self.TIMEOUT) t6 = threading.Thread(target=self.secure_UDP.run) t6.start() # Hilos recibidor t = threading.Thread(target=self.HiloRecibidor) t.start() # print("hilo recibidor iniciado") # hilo timeouts t3 = threading.Thread(target=self.HiloTimeOuts) t3.start() # hilo enviador t4 = threading.Thread(target=self.HiloEnviador) t4.start() # print(("hilo enviador iniciado")) # hilo logico t5 = threading.Thread(target=self.HiloLogico) t5.start() # print("hilo logico iniciado") def clearAcks(self, acks, max): acks.clear() for i in range(max): if (i != self.nodeID): acks[i] = '' return acks def HiloTimeOuts(self): while True: #print("Time stamp: ", self.timeStamp - time.time() ) if time.time() - self.timeStamp > self.TIMEOUT: #print("TAMAÑO DE LA COLA A ENVIAR: ", len(self.cola_enviar)) if not self.colaSalida.empty(): self.semaphore.release() self.timeStamp = time.time() def HiloRecibidor(self): while True: #print("estoy a punto de recibir un paquete en el hilo recibidor") payload, client_address = self.secure_UDP.recibir( ) # recibe 1035 bytes y la (direcciónIP, puerto) del origen #print("recibà el paquete: ", payload) #payload, client_address = self.sock.recvfrom(1035) # recibe 1035 bytes y la (direcciónIP, puerto) del origen tipo = int.from_bytes(payload[:1], byteorder=('little')) if tipo == 0: # caso 1 narnja naranja targetNode = int.from_bytes(payload[9:10], byteorder=('little')) # destino #print("Es para: ", targetNode) #print("Yo soy: ", self.nodeID) if targetNode == self.nodeID: self.colaEntrada.put(payload) # If not then just put it to the outputQueue else: self.colaSalida.put(payload) ##narnaja azul elif tipo == 1: paquete = n_aPaq() paquete = paquete.unserialize(payload) paquete.ipAzul = client_address[ 0] # para poder responderle necesito la IP paquete.puertoAzul = client_address[1] # y el puerto # sin embargo ambas cosas vienen con el mensaje que me mandó. #paquete.imprimir() payload = paquete.serialize() self.colaEntrada.put(payload) def HiloEnviador(self): while True: self.semaphore.acquire() ##Takes a package from the queue. If the queue is empty it waits until a package arrives while not self.colaSalida.empty(): bytePacket = self.colaSalida.get() tipo = int.from_bytes(bytePacket[:1], byteorder='little') # si es para naranjas if tipo == 0: # Orange & Orange ##BYTE 9 has the orangetarget targetNode = int.from_bytes(bytePacket[9:10], byteorder='little') # Routing_table returns the address address = self.routingTable.retrieveAddress(targetNode) try: self.secure_UDP.send(bytePacket, address[0], address[1]) except OSError as err: print("Failed on addressing: ", err) # si es para azules elif tipo == 1: # si es azul bluepack = n_aPaq() bluepack = bluepack.unserialize(bytePacket) blueIp = bluepack.ipAzul bluePort = bluepack.puertoAzul self.secure_UDP.send(bytePacket, blueIp, bluePort) def HiloLogico(self): # print("This is a blue to orange pack, still needs the implementation") # paq = n_nPaq(1,0,0,0,'d',0,'0.0.0.0',0,0) # test=colaEntrada.get()#saca de cola # paq.unserialize(test);#ya puede usar paq para todo # print(paq.puertoAzul) puertoAzul = 8888 ipAzul = "0.0.0.0" nodoSolicitado = 350 snSolicitud = 0 prioridad = 500 acks = { } # diccionario para acks que utiliza el ID del nodo naranja para ver en si está ack'd o no. Esto se debe apendizar con el SNRN del paquete de solicitud como llave al self.diccionariosACKs ganeNodo = False acks_Write = {} acks_Write_Done = False MAX_NODOS_NARANJA = 4 procesando_solicitud_azul = False graphComplete = False workDone = False acks = self.clearAcks(acks, MAX_NODOS_NARANJA) #print(acks) while True: if not self.colaEntrada.empty(): packet = self.colaEntrada.get() #print("Paquete obtenido en hilo logico") tipo = int.from_bytes(packet[:1], byteorder=('little')) if tipo == 0: #print("el paquete es naranja-naranja") package = n_nPaq(0, self.nodeID, self.nodeID, self.nodeID, '', 0, "0.0.0.0", 5000, 0) package = package.unserialize(packet) #print(package.categoria, package.sn, package.origenNaranja, package.destinoNaranja, package.puertoAzul, # package.ipAzul, str(package.tipo), package.posGrafo, package.prioridad) if package.tipo == b'r': # Request de un pquete (solicitud) #print("Packet request from: ", package.origenNaranja, " pidiendo el numero: ", package.posGrafo, # " con la prioridad: ", package.prioridad) if package.posGrafo == nodoSolicitado: # yo pedi ese mismo nodo y por tanto hay conflicto if package.prioridad < prioridad: # yo gano el conflicto #print("Gané la batalla por el nodo ", nodoSolicitado, " (My ID: ", self.nodeID, # " Mi prioridad: ", # prioridad, ") (La ID del otro: ", package.origenNaranja, " La prioridad del otro: ", # package.prioridad, ")") negacion = n_nPaq(0, package.sn, self.nodeID, package.origenNaranja, 'd', package.posGrafo, package.ipAzul, package.puertoAzul, package.prioridad) negacion_bytes = negacion.serialize() self.colaSalida.put(negacion_bytes) elif package.prioridad > prioridad: # yo pierdo el conflicto #print("Perdà la batalla por el nodo ", nodoSolicitado, " (My ID: ", self.nodeID, " Mi prioridad: ", # prioridad, ") (La ID del otro: ", package.origenNaranja, " La prioridad del otro: ", package.prioridad, ")") accept = n_nPaq(0, package.sn, self.nodeID, package.origenNaranja, 'a', package.posGrafo, package.ipAzul, package.puertoAzul, package.prioridad) accept_bytes = accept.serialize() self.colaSalida.put(accept_bytes) else: # empatamos con prioridad #print("Empatamos la batalla por el nodo ", nodoSolicitado, " (My ID: ", self.nodeID," Mi prioridad: ", # prioridad, ") (La ID del otro: ", package.origenNaranja, " La prioridad del otro: ", package.prioridad, ")") if self.nodeID > package.origenNaranja: # lo resolvemos con ip y gano # print("Gané la batalla por el nodo ", nodoSolicitado, " (My ID: ", self.nodeID, " Mi prioridad: ", # prioridad, ") (La ID del otro: ", package.origenNaranja, " La prioridad del otro: ", package.prioridad, ")") negacion = n_nPaq( 0, package.sn, self.nodeID, package.origenNaranja, 'd', package.posGrafo, package.ipAzul, package.puertoAzul, package.prioridad) negacion_bytes = negacion.serialize() self.colaSalida.put(negacion_bytes) else: # perdà por ip # print("Perdà la batalla por el nodo ", nodoSolicitado, " (My ID: ", self.nodeID, " Mi prioridad: ", # prioridad, ") (La ID del otro: ", package.origenNaranja," La prioridad del otro: ", # package.prioridad, ")") accept = n_nPaq(0, package.sn, self.nodeID, package.origenNaranja, 'a', package.posGrafo, package.ipAzul, package.puertoAzul, package.prioridad) accept_bytes = accept.serialize() self.colaSalida.put(accept_bytes) else: # yo no pedà ese nodo #print("No hay batalla por el nodo ", nodoSolicitado, " (My ID: ", self.nodeID, " Mi prioridad: ", # prioridad, ") (La ID del otro: ", package.origenNaranja, " La prioridad del otro: ", package.prioridad, ")") self.tablaNodosAzules.marcarComoSolicitado( package.posGrafo) accept = n_nPaq(0, package.sn, self.nodeID, package.origenNaranja, 'a', package.posGrafo, package.ipAzul, package.puertoAzul, package.prioridad) accept_bytes = accept.serialize() self.colaSalida.put(accept_bytes) elif package.tipo == b'a': print("Recibi un accept por el nodo naranja ", package.origenNaranja, " Sobre mi pedido: ", package.posGrafo, " De parte del nodo azul: ", package.ipAzul, "Con SNRN: ", package.sn) if package.posGrafo == nodoSolicitado: # agrega el ack al mapa de acks. lo voy a explicar arriba. # print(self.diccionariosACKs) if package.sn in self.diccionariosACKs: self.diccionariosACKs[package.sn][ package. origenNaranja] = 'a' # del diccionario con llave = sn, en el lugar con llave origenNaranja, ponga el accept. #print("EL ORIGEN NARANJA ES: ", package.origenNaranja) #print(self.diccionariosACKs[package.sn][package.origenNaranja]) else: print( "Es un ack de un paquete anterior al actual" ) #print(self.diccionariosACKs) #if len(self.diccionariosACKs[package.sn]) == MAX_NODOS_NARANJA - 1: #esto hay que cambiarlo porque creo que el diccionario siepmre va a tener 5 elementos #acks_done = True #print("recibi todos los acks de la petición: ", nodoSolicitado) elif package.tipo == b'd': print("Recibi un decline por el nodo naranja ", package.origenNaranja, " Sobre mi pedido: ", nodoSolicitado, " De parte del nodo azul: ", ipAzul, " Con SNRN: ", package.sn) if package.posGrafo == nodoSolicitado: # si recibà un decliene significa que perdà la batalla por el nodo por lo que tengo que iniciar una nueva. self.diccionariosACKs.clear() acks = self.clearAcks(acks, MAX_NODOS_NARANJA) nodoSolicitado = self.tablaNodosAzules.getNodoDisponible( ) self.tablaNodosAzules.marcarComoSolicitado( nodoSolicitado) # print("Dado que perdà el nodo: ", package.posGrafo, " me veo en la obligación de cambiar por: ", nodoSolicitado) prioridad = self.rand.randrange(0, 4294967296) self.diccionariosACKs[self.SNRN] = acks # print(self.diccionariosACKs) # print("Nodos disponibles ", end='') self.tablaNodosAzules.printNodosDisponibles() snSolicitud = self.SNRN for i in self.routingTable.table: if not i.getNode() == self.nodeID: request = n_nPaq(0, self.SNRN, self.nodeID, i.getNode(), 'r', nodoSolicitado, ipAzul, puertoAzul, prioridad) request = request.serialize() self.colaSalida.put(request) self.SNRN = self.nextSNRN( self.SNRN ) # avanzo el SN para ponerle uno distinto al siguiente paquete de datos. # esto significa que perdà el paquete por lo que tengo que detener la espera de acks. elif package.tipo == b'w': print("Recibi un Write de parte del nodo naranja: ", package.origenNaranja, " Sobre el nodo: ", package.posGrafo) direccion = (package.ipAzul, package.puertoAzul) self.tablaNodosAzules.write(package.posGrafo, direccion) saved_packet = n_nPaq(0, self.SNRN, self.nodeID, package.origenNaranja, 's', package.posGrafo, package.ipAzul, package.puertoAzul, package.prioridad) self.SNRN = self.nextSNRN(self.SNRN) saved_packet = saved_packet.serialize() self.colaSalida.put(saved_packet) # write_ack = n_nPaq(0, sn, nodeID, package.origenNaranja, 's', posGrafo, ipAzul, puertoAzul, prioridad) # por definirse, mas los acks seguramente iran por secure UDP. elif package.tipo == b's': # Saved package print( "Recibi un Saved package de parte del nodo naranja: ", package.origenNaranja) if package.posGrafo == nodoSolicitado: acks_Write[package.origenNaranja] = 's' contador = 0 for i in acks_Write.keys(): contador += 1 if contador == MAX_NODOS_NARANJA - 1: acks_Write_Done = True elif tipo == 1: # el paquete es naranja-azul # cuerpo del naranja-azul if not procesando_solicitud_azul: # si no estoy procesando una solicitud azul entonces puedo proceder #print("Comunicación naranja-azul") bluePacket = n_aPaq() bluePacket = bluePacket.unserialize(packet) #print("Paquete de tipo: ", bluePacket.tipo) if bluePacket.tipo == 14: # es un paquete de solicitud. if len(self.tablaNodosAzules.nodosDisponibles ) != 0: ipAzul = bluePacket.ipAzul puertoAzul = bluePacket.puertoAzul #print("Es un paquete de solicitud azul con IP: ", str(ipAzul), " y puerto: ", puertoAzul) nodoSolicitado = self.tablaNodosAzules.getNodoDisponible( ) #nodoSolicitado = 4 #self.SNRN = 0 self.tablaNodosAzules.marcarComoSolicitado( nodoSolicitado) #print("Nodo solicitado: ", nodoSolicitado) prioridad = self.rand.randrange(0, 4294967296) self.diccionariosACKs[self.SNRN] = acks # print(self.diccionariosACKs) snSolicitud = self.SNRN for i in self.routingTable.table: if not i.getNode() == self.nodeID: request = n_nPaq( 0, self.SNRN, self.nodeID, i.getNode(), 'r', nodoSolicitado, i.getIp(), i.getPort(), prioridad) request = request.serialize() self.colaSalida.put(request) self.SNRN = self.nextSNRN( self.SNRN ) # avanzo el SN para ponerle uno distinto al siguiente paquete de datos. procesando_solicitud_azul = True else: print( "Solicitud rechazada: ya no hay más nodos azules disponibles en el grafo" ) else: # si ya estaba procesando una solicitud entonces devuelvo el paquete a la cola. #print("me llegó una solicitud mientras proceso otra, devolvà la solicitud a la cola.") self.colaEntrada.put(packet) # bluePacket.imprimir() # procesar una solicitud: # almacenar el ip y el puerto del nodo, ya # generar un paquete de request con el número de nodo del grafo que elegÃ. ya # hacer un broadcast con ese paquete utilizando la tabla de rutamiento, ya # generar el diccionario de acks para este nodo, ya # OBS: Tengo que asignar un SN a la petición y utilizar el mismo SN para diccionariosACKs ya # if procesando_solicitud_azul: # print("entré en procesando solicitud") countingACKs = 0 for i in range(MAX_NODOS_NARANJA): if not i == self.nodeID: if self.diccionariosACKs[snSolicitud][i] == 'a': countingACKs += 1 if countingACKs == MAX_NODOS_NARANJA - 1: ganeNodo = True # print(self.diccionariosACKs) #print(ganeNodo) if ganeNodo: #print("entré en gané nodo") vecinos_azules = [] for i in self.tablaNodosAzules.grafoNodosAzules[ nodoSolicitado]: vecinos_azules.append(i) # print(vecinos_azules) self.tablaNodosAzules.write(nodoSolicitado, (ipAzul, puertoAzul)) respuesta_azul = n_aPaq(1, self.SNRN, 15, nodoSolicitado, str(ipAzul), puertoAzul, vecinos_azules) self.SNRN = self.nextSNRN(self.SNRN) # respuesta_azul.imprimir() respuesta_azul = respuesta_azul.serialize() self.colaSalida.put(respuesta_azul) for i in self.routingTable.table: if not i.getNode() == self.nodeID: write_package = n_nPaq(0, self.SNRN, self.nodeID, i.getNode(), 'w', nodoSolicitado, str(ipAzul), puertoAzul, prioridad) # write_package.imprimir() write_package = write_package.serialize() self.colaSalida.put(write_package) self.blueNodesAsignedByMe[nodoSolicitado] = (str(ipAzul), puertoAzul) self.SNRN = self.nextSNRN(self.SNRN) ganeNodo = False acks = self.clearAcks(acks, MAX_NODOS_NARANJA) if acks_Write_Done: #print("entré en write acks done") procesando_solicitud_azul = False nodoSolicitado = -1 ipAzul = '0.0.0.0' puertoAzul = 0000 acks_Write.clear() acks_Write_Done = False if len(self.tablaNodosAzules.nodosDisponibles ) == 0 and not workDone and ( self.tablaNodosAzules.cantidadAsignados == self.tablaNodosAzules.cantidadNodos): if not procesando_solicitud_azul and not graphComplete and self.colaEntrada.empty( ) and self.colaSalida.empty(): for i in self.blueNodesAsignedByMe.keys(): respuesta_azul = n_aPaq( 1, self.SNRN, 16, i, self.blueNodesAsignedByMe[i][0], self.blueNodesAsignedByMe[i][1], self.tablaNodosAzules.getListaVecinos(i)) self.SNRN = self.nextSNRN(self.SNRN) # print(self.tablaNodosAzules.getListaVecinos(i)) respuesta_azul = respuesta_azul.serialize() self.colaSalida.put(respuesta_azul) graphComplete = True if graphComplete and self.colaEntrada.empty( ) and self.colaSalida.empty() and not workDone: for i in self.blueNodesAsignedByMe.keys(): paqGraphComplete = n_aPaq( 1, self.SNRN, 17, i, self.blueNodesAsignedByMe[i][0], self.blueNodesAsignedByMe[i][1], self.tablaNodosAzules.getListaVecinos(i)) self.SNRN = self.nextSNRN(self.SNRN) paqGraphComplete = paqGraphComplete.serialize() self.colaSalida.put(paqGraphComplete) workDone = True
class Node(Thread): """ Each node represents a router in the network """ last_received = 0 last_conf_received = 0 def __init__(self, node_id, network, adapt_functions=None, **kwargs): """ :param node_id: any unique identifier :param network: a data structure holding every node """ Thread.__init__(self, **kwargs) # temp if adapt_functions is None: adapt_functions = [ AdaptationFunction(random.choice(("a", "b", "c")), random.choice(("a", "b", "c")), CV) for i in range(2) ] # --------------- communication related initialisation --------------- # # self.daemon = True self.id = node_id self.network = network self.wake_buffer = Queue() self.adapt_functions = adapt_functions self.routing_table = RoutingTable() for function in self.adapt_functions: self.routing_table.add_route(self.id, list(function._from), self.id, function, 0) # -------------------- counting sent and received -------------------- # self.conf_received = 0 self.conf_sent = 0 @property def neighbors_id(self): "returns the id of this node's neighbors" return list(self.network.graph.neighbors(self.id)) # ------------------------- adaptation functions ------------------------- # @property def In(self): return list(set([x._from for x in self.adapt_functions])) @property def Out(self): return list(set([x._to for x in self.adapt_functions])) # ---------------------- send and receive messages ----------------------- # def send(self, receiver_id, message): "sends a specific message to a node" if isinstance(message, ConfigurationMessage): self.conf_sent += 1 self.network.links[self.id, receiver_id].put(message) self.network.threads[receiver_id].wake_buffer.put(self.id) def receive(self, sender_id, message): "called upon reception of a message sent by another node" # ----------------------------- routing ------------------------------ # if isinstance(message, Message): if message.dest == self.id: self.destination_reached(message) else: self.route(sender_id, message) # ------------------ configuring the routing table ------------------- # if isinstance(message, ConfigurationMessage): self.conf_received += 1 for function in self.adapt_functions: msg_copy = message.copy() if function.reverse.appliable(msg_copy): in_stack = function.reverse.apply_to_stack(msg_copy.stack) if len(in_stack) <= self.network.max_stack: cost = self.network.links[self.id, sender_id].cost( function) + msg_copy.cost added = self.routing_table.add_route( msg_copy.dest, in_stack, sender_id, function, cost) if added: for n_id in self.neighbors_id: self.send( n_id, ConfigurationMessage( msg_copy.dest, in_stack, cost)) Node.last_conf_received = time() Node.last_received = time() # ---------------------------- route messages ---------------------------- # def route(self, sender_id, message): "uses the routing table to route any received message" stack, dest = message.stack, message.dest if (dest, stack) in self.routing_table: row = self.routing_table.get(dest, stack) row.function.apply(message) if message.valid: self.send(row.next_hop, message) else: print("message {} not valid, I do not have this entry".format( message)) def destination_reached(self, message): """Any message passed through this function was destined to this router""" print("The message reached its destination:\n\t- {}\n" \ "\tArrived to {}".format(message, self.id)) # ---------------------------- initialisation ---------------------------- # def init(self): """ Sends messages to each neighbors to initialise the routing table """ for x in self.In: for n_id in self.neighbors_id: self.send(n_id, ConfigurationMessage(self.id, list(x), 0)) # ------------------------ wait for notifications ------------------------ # def wait_for_messages(self): # print(">> {} Finished sending".format(self.id)) while self.network.running: try: sender = self.wake_buffer.get(timeout=1e-2) # blocks link = self.network.links[sender, self.id] while not link.empty(): item = link.get_nowait() self.receive(sender, item) self.wake_buffer.task_done() except Empty: pass # ---------------------------- run the thread ---------------------------- # def run(self): # self.condition.acquire() self.init() self.wait_for_messages()
def __init__(self, routingclass): RoutingTable.__init__(self, routingclass) self.kernelEntries = {}
def test_routing_table_replace_route(): table = RoutingTable() function = AdaptationFunction("a", "a", CV) assert table.add_route(0, ["a"], 1, function, 100) for i in range(1, 101): assert table.add_route(0, ["a"], 1, function, 100 - i) # == True
def init_host(config_file): global host_ip global host_port global timeout global routing_table global watchDog global timer_dict global proxy_server_list fp = open(config_file) count = 1 for line in fp: if count == 1: para = line.strip().split() host_ip = socket.gethostbyname(socket.gethostname()) host_port = int(para[0]) timeout = float(para[1]) routing_table = RoutingTable(host_ip, host_port) else: routing_table.lock.acquire() para = line.strip().split() # splitting the ip and port t_para = para[0].split(':') # initiate the weight routing_table.set_neighbor(t_para[0], int(t_para[1]), float(para[1])) routing_table.set_edge_weight(host_ip, host_port, t_para[0], int(t_para[1]), float(para[1])) routing_table.set_edge_through(host_ip, host_port, t_para[0], int(t_para[1]), t_para[0], int(t_para[1])) routing_table.lock.release() count += 1 fp.close() routing_table.lock.acquire() routing_table.set_edge_weight(host_ip, host_port, host_ip, host_port, 0.0) routing_table.set_edge_through(host_ip, host_port, host_ip, host_port, host_ip, host_port) routing_table.lock.release() ''' This is just added for part three having a proxy list ''' for each in routing_table.get_all_neighbors(): proxy_server_list[each] = None watchDog = WatchDog(timeout, [], send_update) watchDog.start() for key in routing_table.neighbor.keys(): timer = WatchDog(3*timeout, [key], time_up) timer.start() timer_dict[key] = timer
def test_routing_table_add_invalid(): table = RoutingTable() function = AdaptationFunction("a", "a", CV) assert table.add_route(0, ["a"], 1, function, 12) for i in range(30): assert not table.add_route(0, ["a"], 1, function, rd.randint(12, 20))
def __init__(self): # Session key self._key = os.urandom(20) # 20 random bytes == 160 bits self.krpc = KRPCServer(9001, "1.1.1") self.routing_table = RoutingTable()
def init_host(config_file): global host_ip global host_port global timeout global routing_table global watchDog global timer_dict global proxy_server_list fp = open(config_file) count = 1 for line in fp: if count == 1: para = line.strip().split() host_ip = socket.gethostbyname(socket.gethostname()) host_port = int(para[0]) timeout = float(para[1]) routing_table = RoutingTable(host_ip, host_port) else: routing_table.lock.acquire() para = line.strip().split() # splitting the ip and port t_para = para[0].split(':') # initiate the weight routing_table.set_neighbor(t_para[0], int(t_para[1]), float(para[1])) routing_table.set_edge_weight(host_ip, host_port, t_para[0], int(t_para[1]), float(para[1])) routing_table.set_edge_through(host_ip, host_port, t_para[0], int(t_para[1]), t_para[0], int(t_para[1])) routing_table.lock.release() count += 1 fp.close() routing_table.lock.acquire() routing_table.set_edge_weight(host_ip, host_port, host_ip, host_port, 0.0) routing_table.set_edge_through(host_ip, host_port, host_ip, host_port, host_ip, host_port) routing_table.lock.release() ''' This is just added for part three having a proxy list ''' for each in routing_table.get_all_neighbors(): proxy_server_list[each] = None watchDog = WatchDog(timeout, [], send_update) watchDog.start() for key in routing_table.neighbor.keys(): timer = WatchDog(3 * timeout, [key], time_up) timer.start() timer_dict[key] = timer
def init(my_id): rt = RoutingTable(my_id) return rt
class cmdOSPF(cmd.Cmd): """Simple command processor for OSPF.""" def cmdloop(self, routerID, routerDeadInterval, RouterPriority, hellointerval, inttransDelay): self.HelloInterval = hellointerval self.RouterID = routerID self.RouterDeadInterval = routerDeadInterval self.RouterPriority = RouterPriority self.IntTransDelay = inttransDelay self.listInterfaces = [] self.dataforUnicastDB = {} self.StartInterfacesList() self.LSDB = {} self.OpaqueID = 0 self.fakeRoutingTable = RoutingTable(self) self.realRoutingTable = ABRRoutingTable(self) self.thread = threading.Thread(target=self.multicastReceiver, args=()) self.thread.daemon = True self.thread.start() self.threadInc = threading.Thread(target=self.incrementLSATimer, args=()) self.threadInc.daemon = True self.threadInc.start() self.threadABR = False self.ABR = False self.threadUnicast = {} return cmd.Cmd.cmdloop(self) def do_hello(self, line): """ Hello!!!""" print "hello!" def do_kernel_forwarding_table(self, line): """shows the kernel forwarding table""" bashCommand = "route -n" os.system(bashCommand) def do_ping(self, line): """ping [ip address]: Ping linux command. Only send 2 echo requests""" bashCommand = "ping -c 2 " + line os.system(bashCommand) def do_EOF(self, line): """to stop reading a file""" return True def do_bye(self, line): """to stop and exit the program""" print "Bye!" self.realRoutingTable.RemoveEntriesonKernelRT() return True def do_lsdb(self, line): """lsdb [area ip]: list all LSAs of the area""" self.LSDB[line][0].printLSDB() def do_graph(self, line): """graph [area ip]: shows the graph of the area""" self.LSDB[line][0].printGraph() def do_list(self, line): """list the interfaces of the equipment""" print utils.getAllInterfaces() def do_area_forwarding_table(self, line): """shows the area forwarding table based ony on the network and router LSAs for intra-area routing""" self.fakeRoutingTable.printAll() def do_forwarding_table(self, line): """shows the forwarding table of the OSPF""" self.realRoutingTable.printAll() def do_interface(self, line): """interface [name interface] [area] [cost]: to add a new interface on the program""" print "Add interface to OSPF" inter, area, cost = line.split() cost = int(cost) intadd = utils.getIPofInterface(inter) type = utils.getTypeofInterface(inter) netmask = utils.getNetMaskofInterface(inter) self.setInterface( interface(type, intadd, netmask, area, self.HelloInterval, self.RouterDeadInterval, self.IntTransDelay, self.RouterPriority, self.RouterID, self, cost), inter, self.RouterID, area) def multicastReceiver(self): MCAST_GROUP = '224.0.0.5' PROTO = 89 cast = mcast.mcast() mgroup = cast.create(MCAST_GROUP, PROTO) while True: data, addr = cast.recv(mgroup) interface_name = self.WhatInterfaceReceivedthePacket(addr[0]) packet = mcast.readPack(addr, data) interface_obj = self.getInterfaceByName(interface_name) if interface_name == 0 or interface_obj == None or packet == 0: continue interface_obj.packetReceived(packet, True) def unicastReceiver(self, interfaceaddr, type, timeout): PROTO = 89 bufferSize = 1500 s = socket(AF_INET, SOCK_RAW, PROTO) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind((interfaceaddr, PROTO)) if timeout: s.settimeout(20.0) try: while True: data, addr = s.recvfrom(bufferSize) packet = mcast.readPack(addr, data) if packet.getType() == type: if type == 2: self.dataforUnicastDB[interfaceaddr].receivePacket( packet) else: return packet except Exception: return 0 def getNextDBPacket(self, ip): return self.dataforUnicastDB[ip].returnpacket() def StartInterfacesList(self): for x in utils.getAllInterfaces(): self.listInterfaces.append({ 'interface-name': x, 'ip': utils.getIPofInterface(x), 'netmask': utils.getNetMaskofInterface(x), 'interface-object': None }) self.dataforUnicastDB[utils.getIPofInterface( x)] = DataStructurePacketsUnicast() def setInterface(self, object_interface, interface_name, rid, area): for x in self.listInterfaces: if x['interface-name'] == interface_name: x['interface-object'] = object_interface x['interface-area'] = area self.createLSA(area, rid, False) def createLSA(self, area, rid, sn): # create/Update RouterLSA linkdata = [] for x in self.listInterfaces: if x['interface-object'] != None and x['interface-object'].getArea( ) == area: typeLink = x['interface-object'].getTypeLink() if typeLink == 2: # transit network linkdata.append([ x['interface-object'].getDR(), x['interface-object'].getIPIntAddr(), typeLink, 0, x['interface-object'].getMetric() ]) else: if typeLink == 3: # stub networkIP = utils.getNetworkIP( x['interface-object'].getIPIntAddr(), x['interface-object'].getIPInterfaceMask()) linkdata.append([ networkIP, x['interface-object'].getIPInterfaceMask(), typeLink, 0, x['interface-object'].getMetric() ]) else: "Error creating LSA. Link Type not supported" if sn == False: rlsa = RouterLSA(None, 0, 0, 1, rid, rid, 0, 1, 0, 0, 0, False, len(linkdata), linkdata) else: rlsa = RouterLSA(None, 0, 0, 1, rid, rid, sn, 1, 0, 0, 0, False, len(linkdata), linkdata) aux = 1 if area in self.LSDB: if 'ABR' in self.LSDB: aux = aux + 1 if len(self.LSDB) > aux: rlsa.setBbit(True) if self.ABR == False: self.updateOurRouterLSAsTOABR(area) if self.threadABR == True: self.threadforAbrLsdbStart() else: if area != 'ABR': x = [LSDB(area, self), 0] else: x = [ABRLSDB(area, self), 0] aux = aux + 1 self.LSDB[area] = x if len(self.LSDB) > aux: rlsa.setBbit(True) if self.ABR == False: self.updateOurRouterLSAsTOABR(area) if self.threadABR == False: self.threadforAbrLsdbStart() self.LSDB[area][0].receiveLSA(rlsa) def updateOurRouterLSAsTOABR(self, area): self.ABR = True for x in self.LSDB: if x == 'ABR' or x == area: continue self.createLSA(x, self.RouterID, False) def threadforAbrLsdbStart(self): self.threadABR = True t = threading.Thread(target=self.createLSDBforABROverlay, args=[True]) t.daemon = True t.start() def newABRNeighbord(self): if self.ABR: self.createABRLSA() def createLSDBforABROverlay(self, createLSAs): if 'ABR' not in self.LSDB: x = [ABRLSDB('ABR', self), 0] self.LSDB['ABR'] = x if createLSAs: self.createABRLSA() self.createPrefixLSAs() self.createASBRLSAs() def createABRLSA(self): # create ABR LSA opaqueID = self.getOpaqueID() lsa = ABRLSA(None, 0, 2, opaqueID, self.RouterID, 0, 0, 0) # get ABR Neigbords and metric ABRNeighbords = [] for x in self.LSDB: if x == 'ABR': continue N = self.LSDB[x][0].getNeighbordABR(self.RouterID) for y in N: ABRNeighbords.append(y) # add them for x in ABRNeighbords: lsa.addLinkDataEntry(x) # add LSA to LSDB self.LSDB['ABR'][0].receiveLSA(lsa) def createPrefixLSAs(self): self.fakeRoutingTable.createPrefixLSAs() def createASBRLSAs(self): # Get ASBRs Routers for x in self.LSDB: if x == 'ABR': # special LSDB continue for y in self.LSDB[x][0].getAsbrRouterLSAS(self.RouterID): # create PrefixLSA for every NetworkLSA opaqueID = self.getOpaqueID() data = y.getPrefixAndCost() lsa = ASBRLSA(None, 0, 2, opaqueID, self.RouterID, 0, 0, 0, data[0], data[1]) self.LSDB['ABR'][0].receiveLSA(lsa) def getActiveAreas(self): out = [] for x in self.LSDB: if x == 'ABR': # special LSDB continue out.append(x) return out def getOpaqueID(self): self.OpaqueID += 1 return self.OpaqueID def incrementLSATimer(self): while True: for x in self.LSDB: if self.LSDB[x][1] == 1800: area = self.LSDB[x][0].getArea() if area != 'ABR': self.createLSA(area, self.RouterID, False) else: self.createABRLSA() self.LSDB[x][1] = 0 else: self.LSDB[x][1] = self.LSDB[x][1] + 1 sleep(1) def WhatInterfaceReceivedthePacket(self, sourceIP): for x in range(len(self.listInterfaces)): if utils.IPinNetwork(sourceIP, self.listInterfaces[x]['ip'], self.listInterfaces[x]['netmask']): return self.listInterfaces[x]['interface-name'] return 0 def getInterfaceByName(self, interface_name): for x in range(len(self.listInterfaces)): if self.listInterfaces[x]['interface-name'] == interface_name: return self.listInterfaces[x]['interface-object'] return 0 # error def receiveLSAtoLSDB(self, lsa, area): if area not in self.LSDB: if area == 'ABR': self.createLSDBforABROverlay(False) self.LSDB[area][0].receiveLSA(lsa) def getLSDB(self, areaid): return self.LSDB[areaid][0] def getInterfaceIPExcept(self, area): out = [] for x in self.listInterfaces: if x['interface-object'] != None and x['interface-area'] == area: out.append(x['interface-object'].getIPIntAddr()) return out def getAllInterface(self): out = [] for x in self.listInterfaces: if x['interface-object'] != None: out.append(x['interface-object'].getIPIntAddr()) return out def getRouterID(self): return self.RouterID def receivenewLSA(self, lsa): if 'ABR' in self.LSDB: self.receiveLSAtoLSDB(lsa, 'ABR') def setNewRoutes(self, routes, whatRT): if whatRT: self.realRoutingTable.receiveEntries(routes) else: self.fakeRoutingTable.receiveEntries(routes) def createPrefixLSA(self, prefix, cost, netmask): opaqueID = self.getOpaqueID() lsa = PrefixLSA(None, 0, 2, opaqueID, self.RouterID, 0, 0, 0, cost, netmask, prefix) if 'ABR' in self.LSDB: self.LSDB['ABR'][0].receiveLSA(lsa) def createSummaryLSAfromPrefixLSA(self, lsid, netmask, metric): newLSA = SummaryLSA(None, 0, 2, 3, lsid, self.getRouterID(), 0, 0, 0, netmask, metric) for x in self.LSDB: if x == 'ABR': continue self.LSDB[x][0].receiveLSA(newLSA) def getRoutingDatatointraareaRouting(self, destination): return self.fakeRoutingTable.getdataabout(destination) def getbestpathtoABR(self, abr): entries = [] for x in self.LSDB: if x == 'ABR': continue data = self.LSDB[x][0].getPathtoDestination(abr) if data is not False: entries.append(data) if len(entries) > 0: routes = [] entries = sorted(entries, key=itemgetter('cost')) leastcost = entries[0]['cost'] routes.append(entries[0]) for z in range(1, len(entries) - 1): if entries[z]['cost'] != leastcost: break routes.append(entries[z]) return routes else: return False def startThreadUnicast(self, interface): if interface in self.threadUnicast: return else: self.threadUnicast[interface] = threading.Thread( target=self.allwaysUnicast, args=[interface]) self.threadUnicast[interface].daemon = True self.threadUnicast[interface].start() def allwaysUnicast(self, interface): PROTO = 89 bufferSize = 1500 s = socket(AF_INET, SOCK_RAW, PROTO) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) while True: s.bind((interface, PROTO)) try: data, addr = s.recvfrom(bufferSize) packet = mcast.readPack(addr, data) interfaceName = utils.getInterfaceByIP(interface) interface_obj = self.getInterfaceByName(interfaceName) if interface == 0 or interface_obj == None or packet == 0: continue interface_obj.packetReceived(packet, False) except Exception: continue def AlertToCreateNetworkLSA(self, lsid, sn): interface = self.WhatInterfaceReceivedthePacket(lsid) interface_obj = self.getInterfaceByName(interface) if interface_obj.havetoNLSA(): interface_obj.createNLSA(sn, False) else: interface_obj.createNLSA(sn, True)