From 571f843781604564d25945b6c1a15b795209d073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Fri, 12 Mar 2021 21:18:23 +0100 Subject: [PATCH] [mdns] Fix Resolver initialization (#5347) ChipMdnsInit() call has recently been moved to ServiceAdvertiser::Start, hence the function is never called on the mDNS resolver side. Call the initialization function when necessary. --- src/lib/mdns/Discovery_ImplPlatform.cpp | 34 ++++++++++++------------- src/lib/mdns/Discovery_ImplPlatform.h | 2 ++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib/mdns/Discovery_ImplPlatform.cpp b/src/lib/mdns/Discovery_ImplPlatform.cpp index eb712430482f2f..1724591a1b03cb 100644 --- a/src/lib/mdns/Discovery_ImplPlatform.cpp +++ b/src/lib/mdns/Discovery_ImplPlatform.cpp @@ -57,24 +57,28 @@ constexpr uint64_t kUndefinedNodeId = 0; namespace chip { namespace Mdns { -DiscoveryImplPlatform::DiscoveryImplPlatform() -{ - mCommissionInstanceName = GetRandU64(); -} +DiscoveryImplPlatform DiscoveryImplPlatform::sManager; -CHIP_ERROR DiscoveryImplPlatform::Start(Inet::InetLayer * inetLayer, uint16_t port) +DiscoveryImplPlatform::DiscoveryImplPlatform() = default; + +CHIP_ERROR DiscoveryImplPlatform::Init() { - CHIP_ERROR error = CHIP_NO_ERROR; if (!mMdnsInitialized) { ReturnErrorOnFailure(ChipMdnsInit(HandleMdnsInit, HandleMdnsError, this)); - mMdnsInitialized = true; - } - else - { - error = ChipMdnsStopPublish(); + mCommissionInstanceName = GetRandU64(); + mMdnsInitialized = true; } + return CHIP_NO_ERROR; +} + +CHIP_ERROR DiscoveryImplPlatform::Start(Inet::InetLayer * inetLayer, uint16_t port) +{ + ReturnErrorOnFailure(Init()); + + CHIP_ERROR error = ChipMdnsStopPublish(); + if (error != CHIP_NO_ERROR) { ChipLogError(Discovery, "Failed to initialize platform mdns: %s", ErrorStr(error)); @@ -280,6 +284,8 @@ CHIP_ERROR DiscoveryImplPlatform::SetResolverDelegate(ResolverDelegate * delegat CHIP_ERROR DiscoveryImplPlatform::ResolveNodeId(uint64_t nodeId, uint64_t fabricId, Inet::IPAddressType type) { + ReturnErrorOnFailure(Init()); + MdnsService service; snprintf(service.mName, sizeof(service.mName), "%" PRIX64 "-%" PRIX64, nodeId, fabricId); @@ -357,12 +363,6 @@ void DiscoveryImplPlatform::HandleNodeIdResolve(void * context, MdnsService * re DiscoveryImplPlatform & DiscoveryImplPlatform::GetInstance() { - // TODO: Clean Mdns initialization order - // Previously sManager was a global object, but DiscoveryImplPlatform constructor calls - // platform-specific ChipMdnsInit() which for Linux initializes MdnsAvahi global object - // and that may lead to improper initialization, since the order in which global objects' - // constructors are called is undefined. - static DiscoveryImplPlatform sManager; return sManager; } diff --git a/src/lib/mdns/Discovery_ImplPlatform.h b/src/lib/mdns/Discovery_ImplPlatform.h index 1bc367f836b350..c05e227d5cb4ce 100644 --- a/src/lib/mdns/Discovery_ImplPlatform.h +++ b/src/lib/mdns/Discovery_ImplPlatform.h @@ -30,6 +30,8 @@ namespace Mdns { class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver { public: + CHIP_ERROR Init(); + CHIP_ERROR Start(Inet::InetLayer * inetLayer, uint16_t port) override; /// Advertises the CHIP node as an operational node