Skip to content

Commit c7de925

Browse files
authored
Merge pull request #959 from david-cermak/fix/mdns_v1.10
[mdns]: v1.10: Fix to keep TXT/SRV in answers to queries
2 parents 9e0bcd4 + 0f6235f commit c7de925

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

components/mdns/mdns_send.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,20 @@ static bool create_answer_from_service(mdns_tx_packet_t *packet, mdns_service_t
257257
{
258258
mdns_host_item_t *host = get_host_item(service->hostname);
259259
bool is_delegated = (host != mdns_priv_get_self_host());
260-
if (question->type == MDNS_TYPE_PTR || question->type == MDNS_TYPE_ANY) {
261-
// According to RFC6763-section12.1, for DNS-SD, SRV, TXT and all address records
262-
// should be included in additional records.
260+
bool is_instance_question = !mdns_utils_str_null_or_empty(question->host);
261+
if ((question->type == MDNS_TYPE_ANY) && is_instance_question) {
262+
// Instance-scoped ANY queries expect the resolved data (SRV/TXT) to be
263+
// part of the answer section so that queriers can stop searching as soon
264+
// as they receive this packet (RFC6762 section 6, RFC6763 section 12.1).
265+
if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
266+
!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_TXT, service, NULL, send_flush, false) ||
267+
!mdns_priv_create_answer(&packet->additional, MDNS_TYPE_A, service, host, send_flush, false) ||
268+
!mdns_priv_create_answer(&packet->additional, MDNS_TYPE_AAAA, service, host, send_flush, false)) {
269+
return false;
270+
}
271+
} else if (question->type == MDNS_TYPE_PTR || (question->type == MDNS_TYPE_ANY && !is_instance_question)) {
272+
// For service discovery (PTR or wildcard ANY) RFC6763 section 12.1
273+
// requires SRV/TXT/address records to stay in the additional section.
263274
if (!mdns_priv_create_answer(&packet->answers, MDNS_TYPE_PTR, service, NULL, false, false) ||
264275
!mdns_priv_create_answer(&packet->additional, MDNS_TYPE_SRV, service, NULL, send_flush, false) ||
265276
!mdns_priv_create_answer(&packet->additional, MDNS_TYPE_TXT, service, NULL, send_flush, false) ||

components/mdns/tests/host_test/pytest_mdns.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ def test_ptr_additional_records_for_service(dig_app):
7676
dig_app.check_additional(resp, 'TXT', 'test_service._http._tcp.local', expected=True)
7777

7878

79+
def test_instance_any_answer_records(dig_app):
80+
"""Query ANY for the service instance and ensure SRV/TXT are in Answer (Q/A path)."""
81+
resp = dig_app.run_query('test_service._http._tcp.local', query_type='ANY')
82+
83+
# Answer section should contain SRV and TXT records for the instance
84+
srv_answers = dig_app.parse_section(resp, 'answer', 'SRV')
85+
txt_answers = dig_app.parse_section(resp, 'answer', 'TXT')
86+
assert any('test_service._http._tcp.local' in a for a in srv_answers)
87+
assert any('test_service._http._tcp.local' in a for a in txt_answers)
88+
89+
# We should not see a PTR for the generic service name in the Answer section
90+
ptr_answers = dig_app.parse_section(resp, 'answer', 'PTR')
91+
assert not any('_http._tcp.local' in a for a in ptr_answers)
92+
93+
7994
def test_remove_service(mdns_console, dig_app):
8095
mdns_console.send_input('mdns_service_remove _http _tcp')
8196
mdns_console.send_input('mdns_service_lookup _http _tcp')

0 commit comments

Comments
 (0)