From 0d3dcff526e16d47f8a005cb4da39188225fecce Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 2 Apr 2025 16:42:23 +0200 Subject: [PATCH 1/2] fix: correctly override question type flag for requests Currently even when setting the explicit question type flag, the implementation ignores it for subsequent queries. This commit ensures that all queries respect the explicit question type flag. --- src/zeroconf/_services/info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zeroconf/_services/info.py b/src/zeroconf/_services/info.py index 9cd8df16..fff9e125 100644 --- a/src/zeroconf/_services/info.py +++ b/src/zeroconf/_services/info.py @@ -859,7 +859,7 @@ async def async_request( if last <= now: return False if next_ <= now: - this_question_type = question_type or QU_QUESTION if first_request else QM_QUESTION + this_question_type = question_type or (QU_QUESTION if first_request else QM_QUESTION) out = self._generate_request_query(zc, now, this_question_type) first_request = False if out.questions: From 4d5dfa423eab68b61aba4df58663fb07cda28bc0 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Wed, 2 Apr 2025 16:43:59 +0200 Subject: [PATCH 2/2] chore(tests): add test for explicit question type flag Add unit test to validate that the explicit question type flag is set correctly in outgoing requests. --- tests/services/test_info.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/services/test_info.py b/tests/services/test_info.py index 3d4c5302..660b56d2 100644 --- a/tests/services/test_info.py +++ b/tests/services/test_info.py @@ -17,6 +17,7 @@ import zeroconf as r from zeroconf import DNSAddress, RecordUpdate, const +from zeroconf._protocol.outgoing import DNSOutgoing from zeroconf._services import info from zeroconf._services.info import ServiceInfo from zeroconf._utils.net import IPVersion @@ -1871,3 +1872,23 @@ async def test_address_resolver_ipv6(): aiozc.zeroconf.async_send(outgoing) assert await resolve_task assert resolver.ip_addresses_by_version(IPVersion.All) == [ip_address("fe80::52e:c2f2:bc5f:e9c6")] + + +@pytest.mark.asyncio +async def test_unicast_flag_if_requested() -> None: + """Verify we try four times even with the random delay.""" + type_ = "_typethatisnothere._tcp.local." + aiozc = AsyncZeroconf(interfaces=["127.0.0.1"]) + + def async_send(out: DNSOutgoing, addr: str | None = None, port: int = const._MDNS_PORT) -> None: + """Sends an outgoing packet.""" + for question in out.questions: + assert question.unicast + + # patch the zeroconf send + with patch.object(aiozc.zeroconf, "async_send", async_send): + await aiozc.async_get_service_info( + f"willnotbefound.{type_}", type_, question_type=r.DNSQuestionType.QU + ) + + await aiozc.async_close()