Skip to content

Commit

Permalink
[posix] support specifying DNS server for resolver (openthread#10663)
Browse files Browse the repository at this point in the history
This commit adds 2 system APIs for resolver for integration on Android
platform.
- `otSysUpstreamDnsServerSetResolvConfEnabled` is for
  enabling/disabling retrieving the DNS servers from `resolve.conf`.
- `otSysUpstreamDnsSetServerList` for specifying the DNS servers on
  the infra link.
  • Loading branch information
superwhd authored Sep 5, 2024
1 parent db63932 commit e63e9ce
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/posix/platform/include/openthread/openthread-system.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include <openthread/error.h>
#include <openthread/instance.h>
#include <openthread/ip6.h>
#include <openthread/platform/misc.h>

#include "lib/spinel/coprocessor_type.h"
Expand Down Expand Up @@ -311,6 +312,24 @@ bool otSysInfraIfIsRunning(void);
*/
void otSysCliInitUsingDaemon(otInstance *aInstance);

/**
* Sets whether to retrieve upstream DNS servers from "resolv.conf".
*
* @param[in] aEnabled TRUE if enable retrieving upstream DNS servers from "resolv.conf", FALSE otherwise.
*
*/
void otSysUpstreamDnsServerSetResolvConfEnabled(bool aEnabled);

/**
* Sets the upstream DNS server list.
*
* @param[in] aUpstreamDnsServers A pointer to the list of upstream DNS server addresses. Each address could be an IPv6
* address or an IPv4-mapped IPv6 address.
* @param[in] aNumServers The number of upstream DNS servers.
*
*/
void otSysUpstreamDnsSetServerList(const otIp6Address *aUpstreamDnsServers, int aNumServers);

#ifdef __cplusplus
} // end of extern "C"
#endif
Expand Down
30 changes: 30 additions & 0 deletions src/posix/platform/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include <openthread/logging.h>
#include <openthread/message.h>
#include <openthread/nat64.h>
#include <openthread/openthread-system.h>
#include <openthread/udp.h>
#include <openthread/platform/dns.h>
#include <openthread/platform/time.h>
Expand Down Expand Up @@ -85,6 +87,8 @@ void Resolver::LoadDnsServerListFromConf(void)
std::string line;
std::ifstream fp;

VerifyOrExit(mIsResolvConfEnabled);

mUpstreamDnsServerCount = 0;

fp.open(kResolvConfFullPath);
Expand All @@ -110,6 +114,8 @@ void Resolver::LoadDnsServerListFromConf(void)
}

mUpstreamDnsServerListFreshness = otPlatTimeGet();
exit:
return;
}

void Resolver::Query(otPlatDnsUpstreamQuery *aTxn, const otMessage *aQuery)
Expand Down Expand Up @@ -290,6 +296,23 @@ void Resolver::Process(const otSysMainloopContext &aContext)
}
}

void Resolver::SetUpstreamDnsServers(const otIp6Address *aUpstreamDnsServers, int aNumServers)
{
mUpstreamDnsServerCount = 0;

for (int i = 0; i < aNumServers && i < kMaxUpstreamServerCount; ++i)
{
otIp4Address ip4Address;

// TODO: support DNS servers with IPv6 addresses
if (otIp4FromIp4MappedIp6Address(&aUpstreamDnsServers[i], &ip4Address) == OT_ERROR_NONE)
{
mUpstreamDnsServerList[mUpstreamDnsServerCount] = ip4Address.mFields.m32;
mUpstreamDnsServerCount++;
}
}
}

} // namespace Posix
} // namespace ot

Expand All @@ -313,4 +336,11 @@ void otPlatDnsCancelUpstreamQuery(otInstance *aInstance, otPlatDnsUpstreamQuery
gResolver.Cancel(aTxn);
}

void otSysUpstreamDnsServerSetResolvConfEnabled(bool aEnabled) { gResolver.SetResolvConfEnabled(aEnabled); }

void otSysUpstreamDnsSetServerList(const otIp6Address *aUpstreamDnsServers, int aNumServers)
{
gResolver.SetUpstreamDnsServers(aUpstreamDnsServers, aNumServers);
}

#endif // OPENTHREAD_CONFIG_DNS_UPSTREAM_QUERY_ENABLE
19 changes: 19 additions & 0 deletions src/posix/platform/resolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ class Resolver : public Logger<Resolver>
*/
void Process(const otSysMainloopContext &aContext);

/**
* Sets whether to retrieve upstream DNS servers from "resolv.conf".
*
* @param[in] aEnabled TRUE if enable retrieving upstream DNS servers from "resolv.conf", FALSE otherwise.
*
*/
void SetResolvConfEnabled(bool aEnabled) { mIsResolvConfEnabled = aEnabled; }

/**
* Sets the upstream DNS servers.
*
* @param[in] aUpstreamDnsServers A pointer to the list of upstream DNS server addresses. Each address could be an
* IPv6 address or an IPv4-mapped IPv6 address.
* @param[in] aNumServers The number of upstream DNS servers.
*
*/
void SetUpstreamDnsServers(const otIp6Address *aUpstreamDnsServers, int aNumServers);

private:
static constexpr uint64_t kDnsServerListNullCacheTimeoutMs = 1 * 60 * 1000; // 1 minute
static constexpr uint64_t kDnsServerListCacheTimeoutMs = 10 * 60 * 1000; // 10 minutes
Expand All @@ -110,6 +128,7 @@ class Resolver : public Logger<Resolver>
void TryRefreshDnsServerList(void);
void LoadDnsServerListFromConf(void);

bool mIsResolvConfEnabled = true;
int mUpstreamDnsServerCount = 0;
in_addr_t mUpstreamDnsServerList[kMaxUpstreamServerCount];
uint64_t mUpstreamDnsServerListFreshness = 0;
Expand Down

0 comments on commit e63e9ce

Please sign in to comment.