From 1156019f8ba8f9796d1f2ccb3e7100b2aad2b8ff Mon Sep 17 00:00:00 2001 From: C Freeman Date: Sat, 11 Dec 2021 02:59:55 -0500 Subject: [PATCH] Add retries on avahi resolve failure (#12923) --- src/platform/Linux/DnssdImpl.cpp | 26 ++++++++++++++++++++++++-- src/platform/Linux/DnssdImpl.h | 7 +++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/platform/Linux/DnssdImpl.cpp b/src/platform/Linux/DnssdImpl.cpp index 62f5713f0b369f..cfc0415b099dec 100644 --- a/src/platform/Linux/DnssdImpl.cpp +++ b/src/platform/Linux/DnssdImpl.cpp @@ -659,13 +659,20 @@ CHIP_ERROR MdnsAvahi::Resolve(const char * name, const char * type, DnssdService resolveContext->mInstance = this; resolveContext->mCallback = callback; resolveContext->mContext = context; + if (!interface.IsPresent()) { avahiInterface = AVAHI_IF_UNSPEC; } - resolver = avahi_service_resolver_new(mClient, avahiInterface, ToAvahiProtocol(transportType), name, - GetFullType(type, protocol).c_str(), nullptr, ToAvahiProtocol(addressType), + Platform::CopyString(resolveContext->mName, name); + resolveContext->mInterface = avahiInterface; + resolveContext->mTransport = ToAvahiProtocol(transportType); + resolveContext->mAddressType = ToAvahiProtocol(addressType); + resolveContext->mFullType = GetFullType(type, protocol); + + resolver = avahi_service_resolver_new(mClient, avahiInterface, resolveContext->mTransport, name, + resolveContext->mFullType.c_str(), nullptr, resolveContext->mAddressType, static_cast(0), HandleResolve, resolveContext); // Otherwise the resolver will be freed in the callback if (resolver == nullptr) @@ -688,6 +695,21 @@ void MdnsAvahi::HandleResolve(AvahiServiceResolver * resolver, AvahiIfIndex inte switch (event) { case AVAHI_RESOLVER_FAILURE: + if (context->mAttempts++ < 3) + { + ChipLogProgress(DeviceLayer, "Re-trying resolve"); + avahi_service_resolver_free(resolver); + resolver = avahi_service_resolver_new(context->mInstance->mClient, context->mInterface, context->mTransport, + context->mName, context->mFullType.c_str(), nullptr, context->mAddressType, + static_cast(0), HandleResolve, context); + if (resolver == nullptr) + { + ChipLogError(DeviceLayer, "Avahi resolve failed on retry"); + context->mCallback(context->mContext, nullptr, CHIP_ERROR_INTERNAL); + chip::Platform::Delete(context); + } + return; + } ChipLogError(DeviceLayer, "Avahi resolve failed"); context->mCallback(context->mContext, nullptr, CHIP_ERROR_INTERNAL); break; diff --git a/src/platform/Linux/DnssdImpl.h b/src/platform/Linux/DnssdImpl.h index 9e74475cf53d94..369ebfc569f602 100644 --- a/src/platform/Linux/DnssdImpl.h +++ b/src/platform/Linux/DnssdImpl.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -133,6 +134,12 @@ class MdnsAvahi MdnsAvahi * mInstance; DnssdResolveCallback mCallback; void * mContext; + char mName[Common::kInstanceNameMaxLength + 1]; + AvahiIfIndex mInterface; + AvahiProtocol mTransport; + AvahiProtocol mAddressType; + std::string mFullType; + uint8_t mAttempts = 0; }; MdnsAvahi() : mClient(nullptr), mGroup(nullptr) {}