Rewrite _lookup to work with the new twisted.names, and to use inlineCallbacks for...
[invirt/packages/invirt-dns.git] / invirt-dns
index 2b920bd..c9ce43e 100755 (executable)
@@ -154,13 +154,13 @@ class DatabaseAuthority(common.ResolverBase):
                 record = dns.Record_A(ip, ttl)
                 results.append(dns.RRHeader(name, dns.A, dns.IN,
                                             ttl, record, auth=True))
-            elif type == dns.SOA:
-                results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
-                                            ttl, self.soa, auth=True))
-            elif other and type == dns.TXT:
+            if other and type in (dns.TXT, dns.ALL_RECORDS):
                 record = dns.Record_TXT(action if action else '', ttl=ttl)
                 results.append(dns.RRHeader(name, dns.TXT, dns.IN,
                                             ttl, record, auth=True))
+            if type == dns.SOA:
+                results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
+                                            ttl, self.soa, auth=True))
 
         if len(results) == 0:
             authority = []
@@ -214,20 +214,31 @@ class DelegatingQuotingBindAuthority(authority.BindAuthority):
             L.append(split_line)
         return filter(None, L)
 
+    # See https://twistedmatrix.com/documents/13.1.0/api/twisted.internet.defer.html#inlineCallbacks
+    @defer.inlineCallbacks
     def _lookup(self, name, cls, type, timeout = None):
-        maybeDelegate = False
-        deferredResult = authority.BindAuthority._lookup(self, name, cls,
-                                                         type, timeout)
-        # If we didn't find an exact match for the name we were seeking,
-        # check if it's within a subdomain we're supposed to delegate to
-        # some other DNS server.
-        while (isinstance(deferredResult.result, failure.Failure)
-               and '.' in name):
-            maybeDelegate = True
-            name = name[name.find('.') + 1 :]
-            deferredResult = authority.BindAuthority._lookup(self, name, cls,
-                                                             dns.NS, timeout)
-        return deferredResult
+        try:
+            result = yield authority.BindAuthority._lookup(self, name, cls,
+                                                           type, timeout)
+            defer.returnValue(result)
+        except dns.AuthoritativeDomainError:
+            # If we didn't find an exact match for the name we were
+            # seeking, check if it's within a subdomain we're supposed
+            # to delegate to some other DNS server.
+            while '.' in name:
+                _, name = name.split('.', 1)
+                try:
+                    # BindAuthority puts the NS in the authority
+                    # section automatically for us, so just return
+                    # it. We override the type to NS.
+                    result = yield authority.BindAuthority._lookup(self, name, cls,
+                                                                   dns.NS, timeout)
+                    defer.returnValue(result)
+                except (dns.DomainError, dns.AuthoritativeDomainError):
+                    pass
+            # We didn't find a delegation, so return the original
+            # NXDOMAIN.
+            raise
 
 class TypeLenientResolverChain(resolve.ResolverChain):
     """