]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add a response handler matching on specific QNAMEs
authorMichał Kępień <michal@isc.org>
Thu, 18 Sep 2025 08:13:13 +0000 (10:13 +0200)
committerŠtěpán Balážik <stepan@isc.org>
Wed, 29 Oct 2025 17:09:52 +0000 (18:09 +0100)
Add a new ResponseHandler subclass, QnameHandler, which enables
conveniently matching specific QNAMEs (without also matching their
subdomains like DomainHandler does).

bin/tests/system/isctest/asyncserver.py

index b74b90c1051154cc59db44841b9714803621d06d..e5277696cc3a5f170ff46b70601fa3b12b89dd7c 100644 (file)
@@ -554,6 +554,37 @@ class IgnoreAllQueries(ResponseHandler):
         yield ResponseDrop()
 
 
+class QnameHandler(ResponseHandler):
+    """
+    Base class used for deriving custom QNAME handlers.
+
+    The derived class must specify a list of `qnames` that it wants to handle.
+    Queries for exactly these QNAMEs will then be passed to the
+    `get_response()` method in the derived class.
+    """
+
+    @property
+    @abc.abstractmethod
+    def qnames(self) -> List[str]:
+        """
+        A list of QNAMEs handled by this class.
+        """
+        raise NotImplementedError
+
+    def __init__(self) -> None:
+        self._qnames: List[dns.name.Name] = [dns.name.from_text(d) for d in self.qnames]
+
+    def __str__(self) -> str:
+        return f"{self.__class__.__name__}(QNAMEs: {', '.join(self.qnames)})"
+
+    def match(self, qctx: QueryContext) -> bool:
+        """
+        Handle queries whose QNAME matches any of the QNAMEs handled by this
+        class.
+        """
+        return qctx.qname in self._qnames
+
+
 class DomainHandler(ResponseHandler):
     """
     Base class used for deriving custom domain handlers.