feat(mdns): Use internal cache for browser update, and support subtype browsing - #1121
Draft
Siflorite wants to merge 1 commit into
Draft
feat(mdns): Use internal cache for browser update, and support subtype browsing#1121Siflorite wants to merge 1 commit into
Siflorite wants to merge 1 commit into
Conversation
zwx1995esp
reviewed
Aug 7, 2026
Siflorite
force-pushed
the
feat/mdns_browse_cache
branch
3 times, most recently
from
August 11, 2026 07:49
fa7c6c7 to
0f1fd81
Compare
Siflorite
force-pushed
the
feat/mdns_browse_cache
branch
from
August 11, 2026 08:16
0f1fd81 to
d5642b6
Compare
Contributor
Author
|
I have modified the commit a bit. Some names and annotations changed based on reviews of @zwx1995esp and some new types are added to support future resolvers. |
Siflorite
force-pushed
the
feat/mdns_browse_cache
branch
from
August 11, 2026 09:52
d5642b6 to
78163fd
Compare
Siflorite
force-pushed
the
feat/mdns_browse_cache
branch
from
August 11, 2026 11:39
78163fd to
5ebf488
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
I'm currently implementing OpenThread DNS-SD interfaces for ESP Thread libraries. Those interfaces require a PTR browser and continuous resolvers for PTR, SRV, and ADDR records. Current
mdnscomponent cannot provide full functions, thus a series of PRs will be proposed to implement necessary contents.As for browser, the existing browser in
mdnscomponent provides continuous browser throughmdns_browse_*, but it lacks these functions to meet OpenThread interface requirements:mdns_browse_new()andmdns_browse_delete()do not support browsers for subtypes.mdns_browse_notify_tonly returns amdns_result_t, wherettlis the minimum TTL of all records parsed from packet, not specific PTR TTL.This PR introduces
mdns_cacheto store cached results that match running browses (and future continuous resolvers). If cache is updated, matching browses will generate temporary results from cache and notify. When a browse is deleted, the matching cache entry will be removed.Cache structure
Cache is made up of two structures:
mdns_cache.cholds astatic mdns_cache_entry_t * s_cacheas the head of cache entry linked list. Cache entry is identified with hostname, netif, and IP protocol. Every cache entry holds a list of addresses binded to hostname and a list of services. Every service cache entry is identified with instance name, service name, and protocol. A service entry contains all content for PTR, SRV, and TXT record, as well as flags to mark the presence of each record and whether the cache is dirty.New routines
Browse registration routine is almost unchanged.
For notify events, the routine is as follow:
mdns_parse_packet()works the same untilif (type == MDNS_TYPE_PTR)mdns_priv_cache_update_*()to update cache and mark the matching cache dirty.mdns_priv_cache_process_dirty(), this function will callmdns_priv_browse_update_from_service_cache()for every dirty service cache.When a browse is removed, the routine is as follow:
The cache is updated in such a way:
For regular events (TTL>0):
ptr_presentand ttl.When a TTL=0 record is sent in, PTR will notify a goodbye event, then remove subtype from subtype list if subtype is not NULL, otherwise set ptr_present = false. SRV and TXT will mark present = false and clear record. A/AAAA will remove the address from address list of the cache entry and mark all its services as dirty. If all records under a service cache are absent, it will be removed; if an entry has no services, the entry will be removed.
All update functions return a
mdns_cache_update_result_t,MDNS_CACHE_ADDEDandMDNS_CACHE_REMOVEDrefer to whether a cache entry or a service cache is created or freed.MDNS_CACHE_UPDATEDandMDNS_CACHE_NO_CHANGErefer to whether cache content is changed.MDNS_CACHE_ERRORmeans error occurred while handling cache.Changed behaviors
These behaviors are different from previous versions and need careful consideration:
mdns_result_t *resultundermdns_browse_tis now removed, as the result of browse is only generated when notifies. As a result, thenextpointer of a result obtained from notifier will always be NULL.mdns_parse_packetassume that a packet only contains data of one service, thus only one browse will be notified. Now browses are notified based on dirty cache, therefore multiple browses can be notified, including batch TTL=0 events previously mentioned inmdns_notifier_t. Users should no longer traverse the results obtained from browses.ttlinmdns_result_tused to represent the minimum TTL of all records, which may cause ambiguity. Nowttlonly presents PTR TTL, it is recommended to use one-shot queries, or continuous resolvers which may be proposed in the future, to obtain TTL of SRV/TXT/A/AAAA records.char *subtypehas been appended tomdns_result_t, to distinguish subtype information for browse notifications. This may not cause ABI breaking change assubtypeis appended to the end and offsets of other members remain unchanged. But all binaries usingsizeof(mdns_result_t)need to be re-compiled.Related
Previously PR #1028 has proposed browse and query with subtypes, but it has not been pushed forward since March 2026. This PR uses some of the code in #1028 for subtype browse, the query part is not used. #1028 or a new PR should be pushed to implement one-shot query with subtypes, and support subtype for
mdns_lookup_selfhosted_service()andmdns_lookup_delegated_service().Testing
This PR has only been tested under mdns browse with one Thread BR and one Thread router, where browse and cache are created and removed in expected routine. A new PR should be made to implement unit tests for mdns_cache and mdns_browse.
Checklist
Before submitting a Pull Request, please ensure the following: