Example #1
0
    def test_prefix_overlaps_prefixes(self):
        prefixes = [IPv6Network('2001:db8::/48'), IPv6Network('2001:db8:1:2::/64')]
        good_prefix = IPv6Network('2001:db8::/64')
        bad_prefix = IPv6Network('2001:db8:1::/64')

        self.assertTrue(prefix_overlaps_prefixes(good_prefix, prefixes))
        self.assertFalse(prefix_overlaps_prefixes(bad_prefix, prefixes))
Example #2
0
    def test_prefix_overlaps_prefixes(self):
        prefixes = [
            IPv6Network('2001:db8::/48'),
            IPv6Network('2001:db8:1:2::/64')
        ]
        good_prefix = IPv6Network('2001:db8::/64')
        bad_prefix = IPv6Network('2001:db8:1::/64')

        self.assertTrue(prefix_overlaps_prefixes(good_prefix, prefixes))
        self.assertFalse(prefix_overlaps_prefixes(bad_prefix, prefixes))
Example #3
0
    def handle_renew_rebind(self, bundle: TransactionBundle):
        """
        Handle a client renewing/rebinding addresses

        :param bundle: The request bundle
        """
        # Get the assignment
        assignment = self.get_assignment(bundle)

        # Client ID for logging
        client_id_option = bundle.request.get_option_of_type(ClientIdOption)

        # Collect unanswered options
        unanswered_iana_options = bundle.get_unanswered_iana_options()
        unanswered_iapd_options = bundle.get_unanswered_iapd_options()

        for option in unanswered_iapd_options:
            if assignment.prefix and prefix_overlaps_prefixes(assignment.prefix, option.get_prefixes()):
                # Overlap with our assigned prefix: take responsibility
                response_suboptions = []
                for suboption in option.get_options_of_type(IAPrefixOption):
                    if suboption.prefix == assignment.prefix:
                        # This is the correct option, renew it
                        logger.info("Renewing {} for {!r}".format(assignment.prefix, client_id_option.duid))
                        response_suboptions.append(IAPrefixOption(prefix=assignment.prefix,
                                                                  preferred_lifetime=self.prefix_preferred_lifetime,
                                                                  valid_lifetime=self.prefix_valid_lifetime))
                    else:
                        # This isn't right
                        logger.info("Withdrawing {} from {!r}".format(suboption.prefix, client_id_option.duid))
                        response_suboptions.append(IAPrefixOption(prefix=suboption.prefix,
                                                                  preferred_lifetime=0, valid_lifetime=0))

                response_option = IAPDOption(option.iaid, options=response_suboptions)
                bundle.response.options.append(response_option)
                bundle.mark_handled(option)

        for option in unanswered_iana_options:
            if any([address_in_prefixes(address, self.responsible_for_links) for address in option.get_addresses()]):
                # Overlap with our addresses: take responsibility
                response_suboptions = []
                for suboption in option.get_options_of_type(IAAddressOption):
                    if suboption.address == assignment.address:
                        # This is the correct option, renew it
                        logger.info("Renewing {} for {!r}".format(assignment.address, client_id_option.duid))
                        response_suboptions.append(IAAddressOption(address=assignment.address,
                                                                   preferred_lifetime=self.address_preferred_lifetime,
                                                                   valid_lifetime=self.address_valid_lifetime))
                    else:
                        # This isn't right
                        logger.info("Withdrawing {} from {!r}".format(suboption.address, client_id_option.duid))
                        response_suboptions.append(IAAddressOption(address=suboption.address,
                                                                   preferred_lifetime=0, valid_lifetime=0))

                response_option = IANAOption(option.iaid, options=response_suboptions)
                bundle.response.options.append(response_option)
                bundle.mark_handled(option)
Example #4
0
    def handle_renew_rebind(self, bundle: TransactionBundle):
        """
        Handle a client renewing/rebinding addresses

        :param bundle: The request bundle
        """
        # Get the assignment
        assignment = self.get_assignment(bundle)

        # Collect unanswered options
        unanswered_iana_options = bundle.get_unhandled_options(IANAOption)
        unanswered_iapd_options = bundle.get_unhandled_options(IAPDOption)

        for option in unanswered_iapd_options:
            if assignment.prefix and prefix_overlaps_prefixes(assignment.prefix, option.get_prefixes()):
                # Overlap with our assigned prefix: take responsibility
                response_suboptions = []
                for suboption in option.get_options_of_type(IAPrefixOption):
                    if suboption.prefix == assignment.prefix:
                        # This is the correct option, renew it
                        logger.log(DEBUG_HANDLING, "Renewing prefix {}".format(assignment.prefix))
                        response_suboptions.append(IAPrefixOption(prefix=assignment.prefix,
                                                                  preferred_lifetime=self.prefix_preferred_lifetime,
                                                                  valid_lifetime=self.prefix_valid_lifetime))
                    else:
                        # This isn't right
                        logger.log(DEBUG_HANDLING, "Withdrawing prefix {}".format(suboption.prefix))
                        response_suboptions.append(IAPrefixOption(prefix=suboption.prefix,
                                                                  preferred_lifetime=0, valid_lifetime=0))

                response_option = IAPDOption(option.iaid, options=response_suboptions)
                bundle.response.options.append(response_option)
                bundle.mark_handled(option)

        for option in unanswered_iana_options:
            response_suboptions = []
            for suboption in option.get_options_of_type(IAAddressOption):
                if suboption.address == assignment.address:
                    # This is the correct option, renew it
                    logger.log(DEBUG_HANDLING, "Renewing address {}".format(assignment.address))
                    response_suboptions.append(IAAddressOption(address=assignment.address,
                                                               preferred_lifetime=self.address_preferred_lifetime,
                                                               valid_lifetime=self.address_valid_lifetime))
                else:
                    # This isn't right
                    logger.log(DEBUG_HANDLING, "Withdrawing address {}".format(suboption.address))
                    response_suboptions.append(IAAddressOption(address=suboption.address,
                                                               preferred_lifetime=0, valid_lifetime=0))

            response_option = IANAOption(option.iaid, options=response_suboptions)
            bundle.response.options.append(response_option)
            bundle.mark_handled(option)
Example #5
0
    def handle_release_decline(self, bundle: TransactionBundle):
        """
        Handle a client releasing or declining resources. Doesn't really need to do anything because assignments are
        static. Just mark the right options as handled.

        :param bundle: The request bundle
        """
        # Get the assignment
        assignment = self.get_assignment(bundle)

        # Collect unanswered options
        unanswered_iana_options = bundle.get_unhandled_options(IANAOption)
        unanswered_iapd_options = bundle.get_unhandled_options(IAPDOption)

        for option in unanswered_iapd_options:
            if assignment.prefix and prefix_overlaps_prefixes(assignment.prefix, option.get_prefixes()):
                # Overlap with our assigned prefix: take responsibility
                bundle.mark_handled(option)

        for option in unanswered_iana_options:
            if assignment.address in option.get_addresses():
                bundle.mark_handled(option)
Example #6
0
    def handle_release_decline(self, bundle: TransactionBundle):
        """
        Handle a client releasing or declining resources. Doesn't really need to do anything because assignments are
        static. Just mark the right options as handled.

        :param bundle: The request bundle
        """
        # Get the assignment
        assignment = self.get_assignment(bundle)

        # Collect unanswered options
        unanswered_iana_options = bundle.get_unhandled_options(IANAOption)
        unanswered_iapd_options = bundle.get_unhandled_options(IAPDOption)

        for option in unanswered_iapd_options:
            if assignment.prefix and prefix_overlaps_prefixes(
                    assignment.prefix, option.get_prefixes()):
                # Overlap with our assigned prefix: take responsibility
                bundle.mark_handled(option)

        for option in unanswered_iana_options:
            if assignment.address in option.get_addresses():
                bundle.mark_handled(option)
Example #7
0
    def handle_renew_rebind(self, bundle: TransactionBundle):
        """
        Handle a client renewing/rebinding addresses

        :param bundle: The request bundle
        """
        # Get the assignment
        assignment = self.get_assignment(bundle)

        # Collect unanswered options
        unanswered_iana_options = bundle.get_unhandled_options(IANAOption)
        unanswered_iapd_options = bundle.get_unhandled_options(IAPDOption)

        for option in unanswered_iapd_options:
            if assignment.prefix and prefix_overlaps_prefixes(
                    assignment.prefix, option.get_prefixes()):
                # Overlap with our assigned prefix: take responsibility
                response_suboptions = []
                for suboption in option.get_options_of_type(IAPrefixOption):
                    if suboption.prefix == assignment.prefix:
                        # This is the correct option, renew it
                        logger.log(
                            DEBUG_HANDLING,
                            "Renewing prefix {}".format(assignment.prefix))
                        response_suboptions.append(
                            IAPrefixOption(
                                prefix=assignment.prefix,
                                preferred_lifetime=self.
                                prefix_preferred_lifetime,
                                valid_lifetime=self.prefix_valid_lifetime))
                    else:
                        # This isn't right
                        logger.log(
                            DEBUG_HANDLING,
                            "Withdrawing prefix {}".format(suboption.prefix))
                        response_suboptions.append(
                            IAPrefixOption(prefix=suboption.prefix,
                                           preferred_lifetime=0,
                                           valid_lifetime=0))

                response_option = IAPDOption(option.iaid,
                                             options=response_suboptions)
                bundle.response.options.append(response_option)
                bundle.mark_handled(option)

        for option in unanswered_iana_options:
            response_suboptions = []
            for suboption in option.get_options_of_type(IAAddressOption):
                if suboption.address == assignment.address:
                    # This is the correct option, renew it
                    logger.log(
                        DEBUG_HANDLING,
                        "Renewing address {}".format(assignment.address))
                    response_suboptions.append(
                        IAAddressOption(
                            address=assignment.address,
                            preferred_lifetime=self.address_preferred_lifetime,
                            valid_lifetime=self.address_valid_lifetime))
                else:
                    # This isn't right
                    logger.log(
                        DEBUG_HANDLING,
                        "Withdrawing address {}".format(suboption.address))
                    response_suboptions.append(
                        IAAddressOption(address=suboption.address,
                                        preferred_lifetime=0,
                                        valid_lifetime=0))

            response_option = IANAOption(option.iaid,
                                         options=response_suboptions)
            bundle.response.options.append(response_option)
            bundle.mark_handled(option)