Skip to content

Commit

Permalink
[mdns-avahi] utilize Avahi events for complete result handling
Browse files Browse the repository at this point in the history
This commit enhances `Mdns::Publisher` using Avahi to utilize the
events `AVAHI_BROWSER_ALL_FOR_NOW` and `CACHE_EXHAUSTED`, which
notify that all records have been processed and no further results
are expected in the near future.

This improvement prevents premature "resolved" callbacks after
receiving only the initial IPv6 address, thereby improving host and
service address resolution. The `Publisher` now waits for these
events to ensure it gathers all available addresses from the
cache and query responses before signaling resolved callback.
  • Loading branch information
abtink committed Feb 27, 2025
1 parent d1cb92e commit ba3a15b
Showing 1 changed file with 2 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/mdns/mdns_avahi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1367,13 +1367,11 @@ void PublisherAvahi::ServiceResolver::HandleResolveHostResult(AvahiRecordBrowser
OTBR_UNUSED_VARIABLE(aRecordBrowser);
OTBR_UNUSED_VARIABLE(aInterfaceIndex);
OTBR_UNUSED_VARIABLE(aProtocol);
OTBR_UNUSED_VARIABLE(aEvent);
OTBR_UNUSED_VARIABLE(aClazz);
OTBR_UNUSED_VARIABLE(aType);
OTBR_UNUSED_VARIABLE(aFlags);

Ip6Address address;
bool resolved = false;
int avahiError = AVAHI_OK;

otbrLog(aEvent != AVAHI_BROWSER_FAILURE ? OTBR_LOG_INFO : OTBR_LOG_WARNING, OTBR_LOG_TAG,
Expand All @@ -1400,10 +1398,9 @@ void PublisherAvahi::ServiceResolver::HandleResolveHostResult(AvahiRecordBrowser
{
mInstanceInfo.RemoveAddress(address);
}
resolved = true;

exit:
if (resolved)
if ((aEvent == AVAHI_BROWSER_CACHE_EXHAUSTED) || (aEvent == AVAHI_BROWSER_ALL_FOR_NOW))
{
// NOTE: This `HostSubscrption` object may be freed in `OnHostResolved`.
mPublisherAvahi->OnServiceResolved(mType, mInstanceInfo);
Expand Down Expand Up @@ -1498,13 +1495,11 @@ void PublisherAvahi::HostSubscription::HandleResolveResult(AvahiRecordBrowser
{
OTBR_UNUSED_VARIABLE(aRecordBrowser);
OTBR_UNUSED_VARIABLE(aProtocol);
OTBR_UNUSED_VARIABLE(aEvent);
OTBR_UNUSED_VARIABLE(aClazz);
OTBR_UNUSED_VARIABLE(aType);
OTBR_UNUSED_VARIABLE(aFlags);

Ip6Address address;
bool resolved = false;
int avahiError = AVAHI_OK;

otbrLog(aEvent != AVAHI_BROWSER_FAILURE ? OTBR_LOG_INFO : OTBR_LOG_WARNING, OTBR_LOG_TAG,
Expand Down Expand Up @@ -1536,10 +1531,9 @@ void PublisherAvahi::HostSubscription::HandleResolveResult(AvahiRecordBrowser
mHostInfo.mNetifIndex = static_cast<uint32_t>(aInterfaceIndex);
// TODO: Use a more proper TTL
mHostInfo.mTtl = kDefaultTtl;
resolved = true;

exit:
if (resolved)
if ((aEvent == AVAHI_BROWSER_CACHE_EXHAUSTED) || (aEvent == AVAHI_BROWSER_ALL_FOR_NOW))
{
// NOTE: This `HostSubscrption` object may be freed in `OnHostResolved`.
mPublisherAvahi->OnHostResolved(mHostName, mHostInfo);
Expand Down

0 comments on commit ba3a15b

Please sign in to comment.