From 3da26cfa879282c06fea97b7c98db371d0d018a7 Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Mon, 13 Sep 2021 23:10:52 +0800 Subject: [PATCH] Add mDNS shutdown (#9621) --- src/controller/CHIPDeviceController.cpp | 4 ++++ src/controller/tests/TestCommissionableNodeController.cpp | 1 + src/lib/mdns/Discovery_ImplPlatform.h | 1 + src/lib/mdns/MinimalMdnsServer.cpp | 5 +++++ src/lib/mdns/MinimalMdnsServer.h | 1 + src/lib/mdns/Resolver.h | 1 + src/lib/mdns/Resolver_ImplMinimalMdns.cpp | 6 ++++++ src/lib/mdns/Resolver_ImplNone.cpp | 1 + src/platform/fake/MdnsImpl.cpp | 5 +++++ 9 files changed, 25 insertions(+) diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 900262dffab078..1c497ece4e92a3 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -266,6 +266,10 @@ CHIP_ERROR DeviceController::Shutdown() // manager. app::InteractionModelEngine::GetInstance()->Shutdown(); +#if CHIP_DEVICE_CONFIG_ENABLE_MDNS + Mdns::Resolver::Instance().ShutdownResolver(); +#endif // CHIP_DEVICE_CONFIG_ENABLE_MDNS + // TODO(#6668): Some exchange has leak, shutting down ExchangeManager will cause a assert fail. // if (mExchangeMgr != nullptr) // { diff --git a/src/controller/tests/TestCommissionableNodeController.cpp b/src/controller/tests/TestCommissionableNodeController.cpp index 43aa5917d35fee..d0df594c6d2068 100644 --- a/src/controller/tests/TestCommissionableNodeController.cpp +++ b/src/controller/tests/TestCommissionableNodeController.cpp @@ -33,6 +33,7 @@ class MockResolver : public Resolver public: CHIP_ERROR SetResolverDelegate(ResolverDelegate *) override { return SetResolverDelegateStatus; } CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) override { return StartResolverStatus; } + void ShutdownResolver() override {} CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override { return ResolveNodeIdStatus; } CHIP_ERROR FindCommissioners(DiscoveryFilter filter = DiscoveryFilter()) override { return FindCommissionersStatus; } CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override { return CHIP_ERROR_NOT_IMPLEMENTED; } diff --git a/src/lib/mdns/Discovery_ImplPlatform.h b/src/lib/mdns/Discovery_ImplPlatform.h index c0bc89d1168831..0db90a695a1c62 100644 --- a/src/lib/mdns/Discovery_ImplPlatform.h +++ b/src/lib/mdns/Discovery_ImplPlatform.h @@ -43,6 +43,7 @@ class DiscoveryImplPlatform : public ServiceAdvertiser, public Resolver /// Starts the service resolver if not yet started CHIP_ERROR StartResolver(Inet::InetLayer * inetLayer, uint16_t port) override { return Init(); } + void ShutdownResolver() override { ChipMdnsShutdown(); } /// Advertises the CHIP node as an operational node CHIP_ERROR Advertise(const OperationalAdvertisingParameters & params) override; diff --git a/src/lib/mdns/MinimalMdnsServer.cpp b/src/lib/mdns/MinimalMdnsServer.cpp index 3044547126ede0..7bd268bb72eb8c 100644 --- a/src/lib/mdns/MinimalMdnsServer.cpp +++ b/src/lib/mdns/MinimalMdnsServer.cpp @@ -107,5 +107,10 @@ CHIP_ERROR GlobalMinimalMdnsServer::StartServer(chip::Inet::InetLayer * inetLaye return GlobalMinimalMdnsServer::Server().Listen(inetLayer, &allInterfaces, port); } +void GlobalMinimalMdnsServer::ShutdownServer() +{ + GlobalMinimalMdnsServer::Server().Shutdown(); +} + } // namespace Mdns } // namespace chip diff --git a/src/lib/mdns/MinimalMdnsServer.h b/src/lib/mdns/MinimalMdnsServer.h index e42342073fa5a4..e84cff5205a43e 100644 --- a/src/lib/mdns/MinimalMdnsServer.h +++ b/src/lib/mdns/MinimalMdnsServer.h @@ -90,6 +90,7 @@ class GlobalMinimalMdnsServer : public mdns::Minimal::ServerDelegate /// Calls Server().Listen() on all available interfaces CHIP_ERROR StartServer(chip::Inet::InetLayer * inetLayer, uint16_t port); + void ShutdownServer(); void SetQueryDelegate(MdnsPacketDelegate * delegate) { mQueryDelegate = delegate; } void SetResponseDelegate(MdnsPacketDelegate * delegate) { mResponseDelegate = delegate; } diff --git a/src/lib/mdns/Resolver.h b/src/lib/mdns/Resolver.h index 47faa4eaa7ace0..f8badf5ba10521 100644 --- a/src/lib/mdns/Resolver.h +++ b/src/lib/mdns/Resolver.h @@ -260,6 +260,7 @@ class Resolver /// /// Unsual name to allow base MDNS classes to implement both Advertiser and Resolver interfaces. virtual CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) = 0; + virtual void ShutdownResolver() = 0; /// Registers a resolver delegate if none has been registered before virtual CHIP_ERROR SetResolverDelegate(ResolverDelegate * delegate) = 0; diff --git a/src/lib/mdns/Resolver_ImplMinimalMdns.cpp b/src/lib/mdns/Resolver_ImplMinimalMdns.cpp index d73fb1cabb0fdf..599cb9269fe4e3 100644 --- a/src/lib/mdns/Resolver_ImplMinimalMdns.cpp +++ b/src/lib/mdns/Resolver_ImplMinimalMdns.cpp @@ -335,6 +335,7 @@ class MinMdnsResolver : public Resolver, public MdnsPacketDelegate ///// Resolver implementation CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) override; + void ShutdownResolver() override; CHIP_ERROR SetResolverDelegate(ResolverDelegate * delegate) override; CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override; CHIP_ERROR FindCommissionableNodes(DiscoveryFilter filter = DiscoveryFilter()) override; @@ -394,6 +395,11 @@ CHIP_ERROR MinMdnsResolver::StartResolver(chip::Inet::InetLayer * inetLayer, uin return GlobalMinimalMdnsServer::Instance().StartServer(inetLayer, port); } +void MinMdnsResolver::ShutdownResolver() +{ + GlobalMinimalMdnsServer::Instance().ShutdownServer(); +} + CHIP_ERROR MinMdnsResolver::SetResolverDelegate(ResolverDelegate * delegate) { mDelegate = delegate; diff --git a/src/lib/mdns/Resolver_ImplNone.cpp b/src/lib/mdns/Resolver_ImplNone.cpp index 8f1cb81b7298dd..bd0f097622327d 100644 --- a/src/lib/mdns/Resolver_ImplNone.cpp +++ b/src/lib/mdns/Resolver_ImplNone.cpp @@ -29,6 +29,7 @@ class NoneResolver : public Resolver CHIP_ERROR SetResolverDelegate(ResolverDelegate *) override { return CHIP_NO_ERROR; } CHIP_ERROR StartResolver(chip::Inet::InetLayer * inetLayer, uint16_t port) override { return CHIP_NO_ERROR; } + void ShutdownResolver() override {} CHIP_ERROR ResolveNodeId(const PeerId & peerId, Inet::IPAddressType type) override { diff --git a/src/platform/fake/MdnsImpl.cpp b/src/platform/fake/MdnsImpl.cpp index 30bb388066e5a3..9eb7379c799efb 100644 --- a/src/platform/fake/MdnsImpl.cpp +++ b/src/platform/fake/MdnsImpl.cpp @@ -95,6 +95,11 @@ CHIP_ERROR ChipMdnsInit(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCal return CHIP_NO_ERROR; } +CHIP_ERROR ChipMdnsShutdown() +{ + return CHIP_NO_ERROR; +} + CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) { return test::CheckExpected(test::CallType::kStart, service);