From 01cb5b08cf1dde2397de036b393d92c77c64cb80 Mon Sep 17 00:00:00 2001 From: Handa Wang <7058128+superwhd@users.noreply.github.com> Date: Wed, 4 Sep 2024 04:47:07 +0800 Subject: [PATCH] [posix] decouple `resolver.cpp` from `netif.cpp` (#10662) This commit introduces `platformResolver*` APIs so that `system.cpp` can treat resolver as an independent module. Reasons for this refactor: - Simplify the integration on Android platform. - The functionality of resolver is not related to the functionality of netif. --- src/posix/platform/netif.cpp | 15 --------------- src/posix/platform/platform-posix.h | 22 ++++++++++++++++++++++ src/posix/platform/resolver.cpp | 8 +++++++- src/posix/platform/system.cpp | 9 +++++++++ 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index edad3d708..edbe8c416 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -215,10 +215,6 @@ static otIp6Prefix sAddedExternalRoutes[kMaxExternalRoutesNum]; static constexpr uint32_t kNat64RoutePriority = 100; ///< Priority for route to NAT64 CIDR, 100 means a high priority. #endif -#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE -ot::Posix::Resolver gResolver; -#endif - #if defined(RTM_NEWMADDR) || defined(__NetBSD__) // on some BSDs (mac OS, FreeBSD), we get RTM_NEWMADDR/RTM_DELMADDR messages, so we don't need to monitor using MLD // on NetBSD, MLD monitoring simply doesn't work @@ -2285,9 +2281,6 @@ void platformNetifSetUp(void) #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE nat64Init(); #endif -#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE - gResolver.Init(); -#endif } void platformNetifTearDown(void) {} @@ -2345,10 +2338,6 @@ void platformNetifUpdateFdSet(otSysMainloopContext *aContext) FD_SET(sMLDMonitorFd, &aContext->mErrorFdSet); #endif -#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE - gResolver.UpdateFdSet(*aContext); -#endif - if (sTunFd > aContext->mMaxFd) { aContext->mMaxFd = sTunFd; @@ -2411,10 +2400,6 @@ void platformNetifProcess(const otSysMainloopContext *aContext) } #endif -#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE - gResolver.Process(*aContext); -#endif - exit: return; } diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 177106db7..d0c89987a 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -461,6 +461,28 @@ void platformSpinelManagerProcess(otInstance *aInstance, const otSysMainloopCont */ void platformSpinelManagerUpdateFdSet(otSysMainloopContext *aContext); +/** + * Initializes the resolver used by OpenThread. + * + */ +void platformResolverInit(void); + +/** + * Updates the file descriptor sets with file descriptors used by the resolver. + * + * @param[in] aContext A pointer to the mainloop context. + * + */ +void platformResolverUpdateFdSet(otSysMainloopContext *aContext); + +/** + * Performs the resolver processing. + * + * @param[in] aContext A pointer to the mainloop context. + * + */ +void platformResolverProcess(const otSysMainloopContext *aContext); + #ifdef __cplusplus } #endif diff --git a/src/posix/platform/resolver.cpp b/src/posix/platform/resolver.cpp index 642d77137..9aaa4ba3f 100644 --- a/src/posix/platform/resolver.cpp +++ b/src/posix/platform/resolver.cpp @@ -56,7 +56,7 @@ constexpr char kResolvConfFullPath[] = "/etc/resolv.conf"; constexpr char kNameserverItem[] = "nameserver"; } // namespace -extern ot::Posix::Resolver gResolver; +ot::Posix::Resolver gResolver; namespace ot { namespace Posix { @@ -293,6 +293,12 @@ void Resolver::Process(const otSysMainloopContext &aContext) } // namespace Posix } // namespace ot +void platformResolverProcess(const otSysMainloopContext *aContext) { gResolver.Process(*aContext); } + +void platformResolverUpdateFdSet(otSysMainloopContext *aContext) { gResolver.UpdateFdSet(*aContext); } + +void platformResolverInit(void) { gResolver.Init(); } + void otPlatDnsStartUpstreamQuery(otInstance *aInstance, otPlatDnsUpstreamQuery *aTxn, const otMessage *aQuery) { OT_UNUSED_VARIABLE(aInstance); diff --git a/src/posix/platform/system.cpp b/src/posix/platform/system.cpp index 5f3640a56..176a15fbb 100644 --- a/src/posix/platform/system.cpp +++ b/src/posix/platform/system.cpp @@ -156,6 +156,9 @@ void platformInitRcpMode(otPlatformConfig *aPlatformConfig) #if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE platformNetifInit(aPlatformConfig); #endif +#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE + platformResolverInit(); +#endif #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE #if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE @@ -408,6 +411,9 @@ void otSysMainloopUpdate(otInstance *aInstance, otSysMainloopContext *aMainloop) #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE platformTrelUpdateFdSet(aMainloop); #endif +#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE + platformResolverUpdateFdSet(aMainloop); +#endif if (otTaskletsArePending(aInstance)) { @@ -476,6 +482,9 @@ void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMa #if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE platformNetifProcess(aMainloop); #endif +#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE + platformResolverProcess(aMainloop); +#endif } bool IsSystemDryRun(void) { return gDryRun; }