self.ttl = ttl
try:
- self.type, self._format = self.formats[format]
+ self.write = self.formats[format]
except KeyError as e:
raise ValueError("Unsupported format for a zone: %s" % format)
if self.db.license:
f.write("License: %s\n" % self.db.license)
if self.db.description:
- f.write("%s" % self.db.description)
- f.write("Updated At: %s" % created_at.isoformat())
+ f.write("%s\n" % self.db.description)
+ f.write("Updated At: %s\n" % created_at.isoformat())
f.write(";##############################################################################\n")
f.write("$ORIGIN %s" % self.origin)
if self.ttl:
f.write("$TTL %s" % self.ttl)
- if self.format == "bogons":
- networks = self.db.list_bogons()
- else:
- networks = self.db.networks
-
- # Write all networks
- for network in networks:
- self.write(f, network)
-
- def write(self, f, network):
- rp = network.reverse_pointer(suffix="")
+ # Write all records
+ self.write(self, f)
- # If we don't have a reverse pointer, we will have to split the network
- # into its subnets and call ourselves again.
- if rp is None:
+ def _write_record(self, f, network, type, content):
+ """
+ Writes a single record to the output file
+ """
+ name = network.reverse_pointer(suffix="")
+ if name is None:
for subnet in network.subnets:
- self.write(f, subnet)
+ self._write_record(f, subnet, type, content)
return
- # Fetch the payload
- content = self._format(self, network)
+ f.write("%s IN %s %s\n" % (name, type, content))
- # Skip the network if there is no payload
- if content is None:
- return
+ # Bogons
- f.write("%s IN %s %s\n" % (rp, self.type, content))
+ def _write_bogons(self, f):
+ for network in self.db.list_bogons():
+ self._write_record(f, network, "A", "127.0.0.1")
- # DNSBL
+ # Origin
- def _format_dnsbl(self, network):
- return "127.0.0.2"
+ def _write_origin(self, f):
+ for network in self.db.networks:
+ rp = network.reverse_pointer(suffix="")
- # Origin
+ # If we don't have a reverse pointer, we will have to split the network
+ # into its subnets and call ourselves again.
+ if rp is None:
+ for subnet in network.subnets:
+ self._write_origin_network(f, subnet)
+
+ continue
+
+ self._write_origin_network(f, network)
- def _format_origin(self, network):
+ def _write_origin_network(self, f, network):
# Skip the network if it does not belong to an AS
if network.asn is None:
return
if asn is None:
return
- return "\"%s\"" % asn
+ self._write_record(f, network, "TXT", "\"%s\"" % asn)
formats = {
- "origin" : ("TXT", _format_asn),
- "bogons" : ("A", _format_dnsbl),
+ "bogons" : _write_bogons,
+ "origin" : _write_origin,
}