Example #1
0
 def add(self,
         node_id,
         ipv4=None,
         ipv6=None,
         port=None,
         is_static=False,
         domain0=None):
     """Add or update an neighbor node entry"""
     if node_id not in self.nodeinfo_list:
         self.nodeinfo_list[node_id] = NodeInfo(node_id=node_id,
                                                ipv4=ipv4,
                                                ipv6=ipv6,
                                                port=port,
                                                is_static=is_static,
                                                domain0=domain0)
         self.nodeinfo_list[node_id].key_manager = KeyExchangeManager(
             self.networking, self.domain_id, node_id)
         rand_time = random.uniform(
             1, KeyExchangeManager.KEY_EXCHANGE_INVOKE_MAX_BACKOFF)
         self.nodeinfo_list[node_id].key_manager.set_invoke_timer(rand_time)
         return True
     else:
         change_flag = self.nodeinfo_list[node_id].update(ipv4=ipv4,
                                                          ipv6=ipv6,
                                                          port=port,
                                                          domain0=domain0)
         return change_flag
Example #2
0
    def _process_message(self, domain_id, ipv4, ipv6, port, msg):
        """Process received message

        Args:
            domain_id (bytes): target domain_id
            ipv4 (str): IPv4 address of the sender node
            ipv6 (str): IPv6 address of the sender node
            port (int): Port number of the sender
            msg (dict): received message
        """
        if not self.check_admin_signature(domain_id, msg):
            self.logger.error("Illegal access to domain %s" % domain_id.hex())
            return

        source_node_id = msg[KeyType.source_node_id]
        if source_node_id in self.domains[domain_id]["neighbor"].nodeinfo_list:
            admin_msg_seq = msg[KeyType.message_seq]
            if self.domains[domain_id]["neighbor"].nodeinfo_list[
                    source_node_id].admin_sequence_number >= admin_msg_seq:
                return
            self.domains[domain_id]["neighbor"].nodeinfo_list[
                source_node_id].admin_sequence_number = admin_msg_seq

        if KeyType.domain_ping in msg and port is not None:
            self._receive_domain_ping(domain_id, port, msg)

        elif msg[KeyType.command] == BBcNetwork.REQUEST_KEY_EXCHANGE:
            if KeyType.ecdh in msg and KeyType.hint in msg and KeyType.nonce in msg and KeyType.random in msg:
                if source_node_id not in self.domains[domain_id][
                        'neighbor'].nodeinfo_list:
                    self.add_neighbor(domain_id, source_node_id, ipv4, ipv6,
                                      port)
                nodeinfo = self.domains[domain_id]['neighbor'].nodeinfo_list[
                    source_node_id]
                if nodeinfo.key_manager is None:
                    nodeinfo.key_manager = KeyExchangeManager(
                        self, domain_id, source_node_id)
                nodeinfo.key_manager.receive_exchange_request(
                    msg[KeyType.ecdh], msg[KeyType.nonce], msg[KeyType.random],
                    msg[KeyType.hint])

        elif msg[KeyType.command] == BBcNetwork.RESPONSE_KEY_EXCHANGE:
            if KeyType.ecdh in msg and KeyType.hint in msg and KeyType.nonce in msg and KeyType.random in msg:
                nodeinfo = self.domains[domain_id]['neighbor'].nodeinfo_list[
                    source_node_id]
                nodeinfo.key_manager.receive_exchange_response(
                    msg[KeyType.ecdh], msg[KeyType.random], msg[KeyType.hint])

        elif msg[KeyType.command] == BBcNetwork.CONFIRM_KEY_EXCHANGE:
            nodeinfo = self.domains[domain_id]['neighbor'].nodeinfo_list[
                source_node_id]
            nodeinfo.key_manager.receive_confirmation()

        elif msg[KeyType.command] == BBcNetwork.NOTIFY_LEAVE:
            if KeyType.source_node_id in msg:
                self.domains[domain_id]['topology'].notify_neighbor_update(
                    source_node_id, is_new=False)
                self.domains[domain_id]['neighbor'].remove(source_node_id)
Example #3
0
    def process_message(self, domain_id, ipv4, ipv6, port, msg):
        """
        (internal use) process received message
        :param domain_id: target domain_id of this message
        :param ipv4:      sender ipv4 address
        :param ipv6:      sender ipv6 address
        :param port:      sender address and port (None if TCP)
        :param msg:       the message body (already deserialized)
        :return:
        """
        if not self.check_admin_signature(domain_id, msg):
            self.logger.error("Illegal access to domain %s" % domain_id.hex())
            return

        source_node_id = msg[KeyType.source_node_id]
        if source_node_id in self.domains[domain_id]["neighbor"].nodeinfo_list:
            admin_msg_seq = msg[KeyType.message_seq]
            if self.domains[domain_id]["neighbor"].nodeinfo_list[
                    source_node_id].admin_sequence_number >= admin_msg_seq:
                return
            self.domains[domain_id]["neighbor"].nodeinfo_list[
                source_node_id].admin_sequence_number = admin_msg_seq

        if KeyType.domain_ping in msg and port is not None:
            self.receive_domain_ping(domain_id, ipv4, ipv6, port, msg)

        elif msg[KeyType.command] == BBcNetwork.REQUEST_KEY_EXCHANGE:
            if KeyType.ecdh in msg and KeyType.hint in msg and KeyType.nonce in msg and KeyType.random in msg:
                if source_node_id not in self.domains[domain_id][
                        'neighbor'].nodeinfo_list:
                    self.add_neighbor(domain_id, source_node_id, ipv4, ipv6,
                                      port)
                nodeinfo = self.domains[domain_id]['neighbor'].nodeinfo_list[
                    source_node_id]
                if nodeinfo.key_manager is None:
                    nodeinfo.key_manager = KeyExchangeManager(
                        self, domain_id, source_node_id)
                nodeinfo.key_manager.receive_exchange_request(
                    msg[KeyType.ecdh], msg[KeyType.nonce], msg[KeyType.random],
                    msg[KeyType.hint])

        elif msg[KeyType.command] == BBcNetwork.RESPONSE_KEY_EXCHANGE:
            if KeyType.ecdh in msg and KeyType.hint in msg and KeyType.nonce in msg and KeyType.random in msg:
                nodeinfo = self.domains[domain_id]['neighbor'].nodeinfo_list[
                    source_node_id]
                nodeinfo.key_manager.receive_exchange_response(
                    msg[KeyType.ecdh], msg[KeyType.random], msg[KeyType.hint])

        elif msg[KeyType.command] == BBcNetwork.CONFIRM_KEY_EXCHANGE:
            nodeinfo = self.domains[domain_id]['neighbor'].nodeinfo_list[
                source_node_id]
            nodeinfo.key_manager.receive_confirmation()

        elif msg[KeyType.command] == BBcNetwork.NOTIFY_LEAVE:
            if KeyType.source_node_id in msg:
                self.domains[domain_id]['topology'].notify_neighbor_update(
                    source_node_id, is_new=False)
                self.domains[domain_id]['neighbor'].remove(source_node_id)