Esempio n. 1
0
    def visit_a_neighbor(self):
        #NeighborGroup return a neighbor to walk
        #self.neighbor_group.insert_trusted_neighbor(my_public_key=my_public_key,Graph=self.database.TrustGraph)
        neighbor_to_walk = self.neighbor_group.get_neighbor_to_walk()
        neighbor_to_walk_ADDR = neighbor_to_walk.get_public_address()
        #create new Message and specify its  parameter, make it a Introduction Request
        message_introduction_request = Message(
            neighbor_discovery=self,
            destination_address=neighbor_to_walk_ADDR,
            source_private_address=self.private_address,
            source_public_address=self.public_address)
        #encode the message to a introduction request, the binary string will be stored at attribute packet
        message_introduction_request.encode_introduction_request()
        #send the introduction request
        #self.transport.write(message_introduction_request.packet,neighbor_to_walk_ADDR)
        self.send_message(message_introduction_request.packet,
                          neighbor_to_walk_ADDR)
        logger.info("take step to: " + str(neighbor_to_walk_ADDR))

        if self.step_limit:
            self.step_count = self.step_count + 1
            if self.step_count > self.step_limit:
                print("already reach step_limit, stopping")
                self.listening_port.stopListening()
                time.sleep(10)
                self.reactor.stop()
 def test_decode_introduction_request(self):
     destination_address = ("8.8.8.8", 8)
     source_lan_address = ("192.168.1.200", 20000)
     source_wan_address = ("35.1.2.3", 20000)
     #use the walker to create a message
     message = Message(neighbor_discovery=neighbor_discovery,
                       destination_address=destination_address,
                       source_private_address=source_lan_address,
                       source_public_address=source_wan_address)
     message.encode_introduction_request()
     message_decode = Message(packet=message.packet)
     message_decode.decode_introduction_request()
     assert_equals(message.destination_address,
                   message_decode.destination_address)
     assert_equals(message.source_private_address,
                   message_decode.source_private_address)
     assert_equals(message.source_public_address,
                   message_decode.source_public_address)
 def test_create_introduction_request(self):
     #the following three address are fabricated
     #only for test use
     destination_address = ("8.8.8.8", 8)
     source_lan_address = ("192.168.1.200", 20000)
     source_wan_address = ("35.1.2.3", 20000)
     #use the walker to create a message
     message = Message(neighbor_discovery=neighbor_discovery,
                       source_private_address=source_lan_address,
                       source_public_address=source_wan_address,
                       destination_address=destination_address)
     message.encode_introduction_request()
     #now we create a message using in a KNOWN CORRECT WAY
     identifier = message.identifier
     data = [
         inet_aton(destination_address[0]),
         self._struct_H.pack(destination_address[1]),
         inet_aton(source_lan_address[0]),
         self._struct_H.pack(source_lan_address[1]),
         inet_aton(source_wan_address[0]),
         self._struct_H.pack(source_wan_address[1]),
         self._struct_B.pack(self._encode_advice_map[True]
                             | self._encode_connection_type_map[u"unknown"]
                             | self._encode_sync_map[False]),
         self._struct_H.pack(identifier)
     ]
     container = [self.prefix, chr(246)]
     #container.append(self.my_mid)
     my_public_key = self.my_public_key
     #container.extend((self._struct_H.pack(len(my_public_key)), my_public_key))
     container.append(self.my_mid)
     #now = int(time())
     now = self._struct_Q.pack(message.global_time)
     container.append(now)
     container.extend(data)
     #print container
     packet = "".join(container)
     packet_len = len(packet)
     signiture = neighbor_discovery.crypto.create_signature(
         self.my_key, packet)
     packet = packet + signiture
     assert_equals(message.packet[0:packet_len], packet[0:packet_len])