Rewrite _lookup to work with the new twisted.names, and to use inlineCallbacks for...
authorQuentin Smith <quentin@mit.edu>
Mon, 16 Jun 2014 22:25:26 +0000 (18:25 -0400)
committerQuentin Smith <quentin@mit.edu>
Tue, 26 Nov 2019 00:40:05 +0000 (19:40 -0500)
debian/changelog
invirt-dns

index e782b06..ed218aa 100644 (file)
@@ -1,3 +1,9 @@
+invirt-dns (0.0.20) unstable; urgency=low
+
+  * Rewrite _lookup to use inlineCallbacks for readability, in the process
+    fixing a defer leak.
+
+ -- Quentin Smith <quentin@mit.edu>  Mon, 16 Jun 2014 18:25:13 -0400
 invirt-dns (0.0.19) precise; urgency=low
 
   * Add ".other" pseudo-subdomain to find IPs being used for purposes
index b540ff7..c9ce43e 100755 (executable)
@@ -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):
     """