Skip to content

Commit

Permalink
[posix] decouple resolver.cpp from netif.cpp (openthread#10662)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
superwhd authored Sep 3, 2024
1 parent 2493658 commit 01cb5b0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
15 changes: 0 additions & 15 deletions src/posix/platform/netif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -2411,10 +2400,6 @@ void platformNetifProcess(const otSysMainloopContext *aContext)
}
#endif

#if OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
gResolver.Process(*aContext);
#endif

exit:
return;
}
Expand Down
22 changes: 22 additions & 0 deletions src/posix/platform/platform-posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/posix/platform/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/posix/platform/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit 01cb5b0

Please sign in to comment.