pvl/dns/zone.py
changeset 270 bafcda3a3c0d
parent 267 8c0c1b6e6aff
child 274 81af20dd142d
--- a/pvl/dns/zone.py	Mon Dec 16 18:37:08 2013 +0200
+++ b/pvl/dns/zone.py	Mon Dec 16 19:00:58 2013 +0200
@@ -7,6 +7,7 @@
 import codecs
 import datetime
 import logging
+import math
 import os.path
 
 log = logging.getLogger('pvl.dns.zone')
@@ -273,26 +274,26 @@
 
     @classmethod
     def A (cls, name, ip, **opts) :
-        return cls(name, 'A', [ip], **opts)
+        return cls(str(name), 'A', [str(ip)], **opts)
 
     @classmethod
     def CNAME (cls, name, host, **opts) :
-        return cls(name, 'CNAME', [host], **opts)
+        return cls(str(name), 'CNAME', [str(host)], **opts)
 
     @classmethod
     def TXT (cls, name, text, **opts) :
-        return cls(name, 'TXT',
+        return cls(str(name), 'TXT',
             [u'"{0}"'.format(text.replace('"', '\\"'))], 
             **opts
         )
 
     @classmethod
     def PTR (cls, name, ptr, **opts) :
-        return cls(name, 'PTR', [ptr], **opts)
+        return cls(str(name), 'PTR', [str(ptr)], **opts)
 
     @classmethod
     def MX (cls, name, priority, mx, **opts) :
-        return cls(name, 'MX', [priority, mx], **opts)
+        return cls(str(name), 'MX', [int(priority), str(mx)], **opts)
 
     def __init__ (self, name, type, data, origin=None, ttl=None, cls=None, line=None, comment=None) :
         self.name = name
@@ -537,7 +538,7 @@
     return '.'.join(tuple(reversed(parts)) + ( 'ip6', 'arpa'))
 
 def fqdn (*parts) :
-    fqdn = '.'.join(parts)
+    fqdn = '.'.join(str(part) for part in parts)
     
     # we may be given an fqdn in parts
     if not fqdn.endswith('.') :
@@ -545,4 +546,23 @@
     
     return fqdn
 
+def reverse_label (prefix, address) :
+    """
+        Determine the correct label for the given IP address within the reverse zone for the given prefix.
 
+        This includes all suffix octets (partially) covered by the prefix.
+    """
+
+    assert prefix.version == address.version
+    
+    hostbits = prefix.max_prefixlen - prefix.prefixlen
+
+    if prefix.version == 4 :
+        labelbytes = int(math.ceil(hostbits / 8.0))
+        labelraw = address.packed[-labelbytes:]
+
+        return '.'.join(reversed([str(ord(x)) for x in labelraw]))
+    else :
+        raise ValueError("unsupported address version: %s" % (prefix, ))
+
+