Example #1
0
    def get_GENERATE_directives(cls, dynamic_range):
        """Return the GENERATE directives for the forward zone of a network."""
        slash_16 = IPNetwork("%s/16" % IPAddress(dynamic_range.first))
        if dynamic_range.size > 256**2 or not ip_range_within_network(
                dynamic_range, slash_16):
            # We can't issue a sane set of $GENERATEs for any network
            # larger than a /16, or for one that spans two /16s, so we
            # don't try.
            return []

        generate_directives = set()
        subnets, prefix, rdns_suffix = get_details_for_ip_range(dynamic_range)
        for subnet in subnets:
            iterator = "%d-%d" % (
                (subnet.first & 0x000000FF),
                (subnet.last & 0x000000FF),
            )

            hostname = "%s-%d-$" % (
                prefix.replace(".", "-"),
                # Calculate what the third quad (i.e. 10.0.X.1) value should
                # be for this subnet.
                (subnet.first & 0x0000FF00) >> 8,
            )

            ip_address = "%s.%d.$" % (prefix, (subnet.first & 0x0000FF00) >> 8)
            generate_directives.add((iterator, hostname, ip_address))

        return sorted(generate_directives, key=lambda directive: directive[2])
Example #2
0
    def get_GENERATE_directives(cls, dynamic_range, domain, zone_info):
        """Return the GENERATE directives for the reverse zone of a network."""
        slash_16 = IPNetwork("%s/16" % IPAddress(dynamic_range.first))
        if dynamic_range.size > 256 ** 2 or not ip_range_within_network(
            dynamic_range, slash_16
        ):
            # We can't issue a sane set of $GENERATEs for any network
            # larger than a /16, or for one that spans two /16s, so we
            # don't try.
            return []

        generate_directives = set()
        # The largest subnet returned is a /24.
        subnets, prefix, rdns_suffix = get_details_for_ip_range(dynamic_range)
        for subnet in subnets:
            if IPAddress(subnet.first) in zone_info.subnetwork:
                iterator = "%d-%d" % (
                    (subnet.first & 0x000000FF),
                    (subnet.last & 0x000000FF),
                )
                hostname = "%s-%d-$" % (
                    prefix.replace(".", "-"),
                    (subnet.first & 0x0000FF00) >> 8,
                )
                # If we're at least a /24, then fully specify the name,
                # rather than trying to figure out how much of the name
                # is in the zone name.
                if zone_info.subnetwork.prefixlen <= 24:
                    rdns = "$.%d.%s" % (
                        (subnet.first & 0x0000FF00) >> 8,
                        rdns_suffix,
                    )
                else:
                    # Let the zone declaration provide the suffix.
                    # rather than trying to calculate it.
                    rdns = "$"
                generate_directives.add(
                    (iterator, rdns, "%s.%s." % (hostname, domain))
                )

        return sorted(generate_directives, key=lambda directive: directive[2])