コード例 #1
0
        def check_ip_availability(subnet_inst_, ip_):
            """
            Checks if an IP is available

            This function is a suicider: it will die if any of the tests fail. It will either raise
            nettools.core.errors.IPOffNetworkRangeException or rth.core.errors.IPAlreadyAttributed

            Args:
                subnet_inst_: The subnetwork instance
                ip_: The IP that has to be checked
            """

            # Checking that ip is effectively in range of the subnet
            if isinstance(ip_, str):
                ip_ = FourBytesLiteral().set_from_string_literal(ip_)

            mask = FourBytesLiteral().set_from_string_literal(
                Utils.mask_length_to_literal(subnet_inst_.mask_length))
            inst = IPv4Network().init_from_fbl(ip_, mask)

            if inst.address_type != 1:
                # means the address is either a network or a broadcast address
                raise IPOffNetworkRangeException(str(ip))

            # then we check that ip is not used by any of the current routers
            routers = subnet_inst_.routers
            for r in routers:
                if str(routers[r]) == str(ip_):
                    raise IPAlreadyAttributed(name, ip_,
                                              self.uid_to_name('router', r),
                                              str(router_name))
コード例 #2
0
    def __calculate_mask(self) -> None:
        """
        Calculates the mask from the instance var self.__mask

        If the mask is a literal mask (i.e. '255.255.255.0'), the try case is concerned.
        If instead, the user gave the mask length, we make sure to raise an AttributeError to switch to the
        except case to do proper testing.

        :raises:
            IncorrectMaskException: if the mask is wrongly formed (byte != 0 after byte < 255) or if the mask contains a
                byte that cannot be used in a mask.
        """

        try:
            # The mask is given by its literal
            temp = self.__mask.split('.')
            if len(temp) == 1:
                # If the mask is given by its length
                # Use AttributeError raise to switch to the except case
                raise AttributeError()

            length = 0

            for byte in range(4):
                concerned = int(temp[byte])
                # We check that the byte is in the awaited bytes list
                if concerned in Utils.mask_allowed_bytes:
                    # If mask contains a 0, we check that each next byte
                    # contains only a 0, else we raise an IncorrectMaskException
                    if concerned < 255:
                        for i in range(1, 4 - byte):
                            b = temp[byte + i]
                            if b != '0':
                                raise IncorrectMaskException(
                                    is_out_allowed=False,
                                    value=b,
                                    extra=byte + i)

                    length += Utils.switch_length(concerned, index=True)
                else:
                    raise IncorrectMaskException(is_out_allowed=True,
                                                 value=concerned)

            # Stock the length
            self.__mask_length = length
            self.__mask = FourBytesLiteral().set_from_string_literal(
                ".".join(temp))

        except AttributeError:
            # The mask is given by its length
            self.__mask_length = int(self.__mask)
            self.__mask = FourBytesLiteral().set_from_string_literal(
                Utils.mask_length_to_literal(self.__mask_length))

        finally:
            self.__addresses = 2**(32 - self.__mask_length) - 2
コード例 #3
0
    def __build_subnets(self) -> None:
        start_ip = self.network_range['start']

        for i in range(len(self.__subnets_sizes)):
            machines_bits = self.__submasks_machine_bits[i]
            mask_literal = FourBytesLiteral().set_eval(Utils.mask_length_to_literal(32 - machines_bits))

            result = IPv4Network().init_from_fbl(start_ip, mask_literal)
            self.__subnets.append(result)
            start_ip = Utils.ip_after(result.network_range['end'])