コード例 #1
0
    def ipv6_to_ptr(self, ipv6):
        uncompressed = IPv6().uncompress(ipv6)
        parts = reversed(uncompressed.split(':'))
        characters = []
        for part in parts:
            characters += reversed(part)

        return ".".join(characters) + ".ip6.arpa"
コード例 #2
0
    def data_line_from_model(self, model):
        locstring = ""
        if self.locations is not None and model.location_id is not None:
            for loc in self.locations:
                if loc.location_id == model.location_id:
                    locstring = "::" + loc.location
                    break

        # A record
        if model.type == "A":
            return "+" + model.host + ":" + model.val + \
                ":" + str(model.ttl) + locstring + "\n"
        # A+PTR record
        if model.type == "=":
            return "=" + model.host + ":" + model.val + \
                ":" + str(model.ttl) + locstring + "\n"
        # MX record
        elif model.type == "M":
            return "@" + model.host + "::" + model.val + ":" + \
                   str(model.distance) + ":" + str(model.ttl) + \
                   locstring + "\n"
        # PTR record
        elif model.type == "P":
            return "^" + model.host + ":" + model.val + \
                ":" + str(model.ttl) + locstring + "\n"
        # TXT record
        elif model.type == "T":
            return "'" + model.host + ":" + model.val.replace(":", r"\072") + \
                   ":" + str(model.ttl) + locstring + "\n"
        # SPF record (rfc 4408, deprecated)
        elif model.type == "F":
            rdata_len = self.dec_to_oct_tinydns(len(model.val))
            return ":" + model.host + ":99:" + rdata_len + \
                   model.val.replace(":", r"\072") + ":" + \
                   str(model.ttl) + locstring + "\n"
        # CNAME record
        elif model.type == "C":
            return "C" + model.host + ":" + model.val + \
                ":" + str(model.ttl) + locstring + "\n"
        # SOA record
        elif model.type == "S":
            soa = model.to_recordtype().to_dict()

            return "Z" + model.domain_id.domain + \
                ":" + soa['nameserver'] + \
                ":" + soa['email'] + \
                ":" + soa['serial'] + \
                ":" + soa['refresh'] + \
                ":" + soa['retry'] + \
                ":" + soa['expire'] + \
                ":" + soa['minimum'] + \
                ":" + str(soa['ttl']) + \
                "\n"
        # NS record
        elif model.type == "N":
            return "&" + model.host + \
                "::" + model.val + \
                ":" + str(model.ttl) + locstring + \
                "\n"

        # SRV record
        elif model.type == "V":
            encoded = self.encode_rdata(
                'cccq',
                [
                    model.distance,
                    model.weight,
                    model.port,
                    model.val.rstrip('.')
                ]
            )
            return ":" + model.host + \
                ":33:" + \
                encoded + \
                ":" + str(model.ttl) + locstring + \
                "\n"

        elif model.type == "3" or model.type == "6":
            # AAAA Record
            # FIXME - exception handling
            out = ""
            uncompressed = IPv6().uncompress(model.val)
            formatted_octal = self.ipv6_to_tinydns(uncompressed)

            out += ":" + model.host + ":28:" + formatted_octal + \
                ":" + str(model.ttl) + locstring + "\n"

            # if 6, then build corresponding PTR
            if model.type == "6":
                ptr = self.ipv6_to_ptr(uncompressed)
                out += "^" + ptr + ":" + model.host + ":" + str(model.ttl) + \
                    "\n"

            return out

        # CAA record
        elif model.type == "E":
            caa_split = model.val.split(":", 2)
            return ":" + model.host + \
                r":257:" + r"\%03o" % int(caa_split[0]) + \
                r"\%03o" % len(caa_split[1]) + \
                "".join(r"\%03o" % ord(c) for c in caa_split[1]) + \
                "".join(r"\%03o" % ord(c) for c in caa_split[2]) + \
                ":" + str(model.ttl) + \
                "\n"