From be4ce643eca7db76fdcdb2d4ea299a0cfe7c5986 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 27 Feb 2026 16:54:57 -0800 Subject: [PATCH] [dns] make name compression per-instance and enforce mDNS check (#12538) This commit updates the DNS name compression setting to be a per-instance property rather than a global static variable. The `otDnsSetNameCompressionEnabled()` and `otDnsIsNameCompressionEnabled()` C APIs are updated to take an `otInstance` pointer. Internally, the `Instance` class now uses the `mDnsNameCompressionEnabled` member variable instead of the static `sDnsNameCompressionEnabled`. Additionally, this commit enforces that DNS name compression cannot be disabled when the OpenThread mDNS module is active. The `otDnsSetNameCompressionEnabled()` function now returns an `otError` and will return `OT_ERROR_NOT_CAPABLE` if a user attempts to disable compression while mDNS is enabled. When the mDNS module is enabled, it automatically sets name compression to true. The CLI command and related documentation are updated to reflect these changes. --- include/openthread/dns.h | 21 ++++++++++------ src/cli/README.md | 6 +++-- src/cli/cli_dns.cpp | 45 +++++++++------------------------- src/core/api/dns_api.cpp | 10 ++++++-- src/core/instance/instance.cpp | 26 +++++++++++++++++--- src/core/instance/instance.hpp | 28 +++++++++++++-------- src/core/net/dns_types.cpp | 2 +- src/core/net/mdns.cpp | 4 +++ 8 files changed, 83 insertions(+), 59 deletions(-) diff --git a/include/openthread/dns.h b/include/openthread/dns.h index 289cb1e19..526d3471f 100644 --- a/include/openthread/dns.h +++ b/include/openthread/dns.h @@ -39,6 +39,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { @@ -157,17 +158,23 @@ otError otDnsEncodeTxtData(const otDnsTxtEntry *aTxtEntries, /** * Enables/disables the "DNS name compression" mode. * - * By default DNS name compression is enabled. When disabled, DNS names are appended as full and never compressed. This - * is applicable to OpenThread's DNS and SRP client/server modules. + * By default, DNS name compression is enabled. When disabled, DNS names are appended in full and are never compressed. + * This applies to OpenThread's DNS and SRP client/server modules. + * + * DNS name compression cannot be disabled if the OpenThread mDNS module is enabled. Enabling the mDNS module will + * automatically enable name compression if it was previously disabled. Attempting to disable compression while the mDNS + * module is active will return `OT_ERROR_NOT_CAPABLE`. * * This is intended for testing only and available when `OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE` config is enabled. * - * Note that in the case `OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE` is used, this mode applies to all OpenThread - * instances (i.e., calling this function enables/disables the compression mode on all OpenThread instances). - * + * @param[in] aInstance A pointer to an OpenThread instance. * @param[in] aEnabled TRUE to enable the "DNS name compression" mode, FALSE to disable. + * + * @retval OT_ERROR_NONE The "DNS name compression" mode is updated. + * @retval OT_ERROR_NOT_CAPABLE The "DNS name compression" mode cannot be disabled since OpenThread mDNS module is + * enabled. */ -void otDnsSetNameCompressionEnabled(bool aEnabled); +otError otDnsSetNameCompressionEnabled(otInstance *aInstance, bool aEnabled); /** * Indicates whether the "DNS name compression" mode is enabled or not. @@ -176,7 +183,7 @@ void otDnsSetNameCompressionEnabled(bool aEnabled); * * @returns TRUE if the "DNS name compression" mode is enabled, FALSE otherwise. */ -bool otDnsIsNameCompressionEnabled(void); +bool otDnsIsNameCompressionEnabled(otInstance *aInstance); /** * @} diff --git a/src/cli/README.md b/src/cli/README.md index 2ee020f13..f4abcb549 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -1916,9 +1916,11 @@ Done Enable/Disable the "DNS name compression" mode. -By default DNS name compression is enabled. When disabled, DNS names are appended as full and never compressed. This is applicable to OpenThread's DNS and SRP client/server modules. +By default, DNS name compression is enabled. When disabled, DNS names are appended in full and are never compressed. This applies to OpenThread's DNS and SRP client/server modules. -This is intended for testing only and available under `REFERENCE_DEVICE` config. +DNS name compression cannot be disabled if the OpenThread mDNS module is enabled. Enabling the mDNS module will automatically enable name compression if it was previously disabled. Attempting to disable compression while the mDNS module is active will fail. + +This is intended for testing only and requires `OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE`. Get the current "DNS name compression" mode. diff --git a/src/cli/cli_dns.cpp b/src/cli/cli_dns.cpp index 4c87649ad..19de30c55 100644 --- a/src/cli/cli_dns.cpp +++ b/src/cli/cli_dns.cpp @@ -44,30 +44,12 @@ namespace Cli { template <> otError Dns::Process(Arg aArgs[]) { - otError error = OT_ERROR_NONE; - /** * @cli dns compression * @code * dns compression * Enabled * @endcode - * @cparam dns compression [@ca{enable|disable}] - * @par api_copy - * #otDnsIsNameCompressionEnabled - * @par - * By default DNS name compression is enabled. When disabled, - * DNS names are appended as full and never compressed. This - * is applicable to OpenThread's DNS and SRP client/server - * modules." - * `OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE` is required. - */ - if (aArgs[0].IsEmpty()) - { - OutputEnabledDisabledStatus(otDnsIsNameCompressionEnabled()); - } - /** - * @cli dns compression (enable,disable) * @code * dns compression enable * Enabled @@ -81,25 +63,20 @@ template <> otError Dns::Process(Arg aArgs[]) * @endcode * @cparam dns compression [@ca{enable|disable}] * @par - * Set the "DNS name compression" mode. + * Gets or sets the "DNS name compression" mode. * @par - * By default DNS name compression is enabled. When disabled, - * DNS names are appended as full and never compressed. This - * is applicable to OpenThread's DNS and SRP client/server - * modules." - * `OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE` is required. + * This is intended for testing only and requires `OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE`. + * @par + * By default, DNS name compression is enabled. When disabled, DNS names are appended in full and are never + * compressed. This applies to OpenThread's DNS and SRP client/server modules. + * @par + * DNS name compression cannot be disabled if the OpenThread mDNS module is enabled. Enabling the mDNS module will + * automatically enable name compression if it was previously disabled. Attempting to disable compression while the + * mDNS module is active will return an error. + * @sa otDnsIsNameCompressionEnabled * @sa otDnsSetNameCompressionEnabled */ - else - { - bool enable; - - SuccessOrExit(error = ParseEnableOrDisable(aArgs[0], enable)); - otDnsSetNameCompressionEnabled(enable); - } - -exit: - return error; + return ProcessEnableDisable(aArgs, otDnsIsNameCompressionEnabled, otDnsSetNameCompressionEnabled); } #endif // OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE diff --git a/src/core/api/dns_api.cpp b/src/core/api/dns_api.cpp index a2a37f935..71d6fc2eb 100644 --- a/src/core/api/dns_api.cpp +++ b/src/core/api/dns_api.cpp @@ -69,9 +69,15 @@ exit: } #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE -void otDnsSetNameCompressionEnabled(bool aEnabled) { Instance::SetDnsNameCompressionEnabled(aEnabled); } +otError otDnsSetNameCompressionEnabled(otInstance *aInstance, bool aEnabled) +{ + return AsCoreType(aInstance).SetDnsNameCompressionEnabled(aEnabled); +} -bool otDnsIsNameCompressionEnabled(void) { return Instance::IsDnsNameCompressionEnabled(); } +bool otDnsIsNameCompressionEnabled(otInstance *aInstance) +{ + return AsCoreType(aInstance).IsDnsNameCompressionEnabled(); +} #endif #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE diff --git a/src/core/instance/instance.cpp b/src/core/instance/instance.cpp index 7a87b9b79..7460364c5 100644 --- a/src/core/instance/instance.cpp +++ b/src/core/instance/instance.cpp @@ -66,9 +66,6 @@ static uint64_t gMultiInstanceRaw[MULTI_INSTANCE_SIZE]; OT_DEFINE_ALIGNED_VAR(sHeapRaw, sizeof(Utils::Heap), uint64_t); Utils::Heap *Instance::sHeap{nullptr}; #endif -#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE -bool Instance::sDnsNameCompressionEnabled = true; -#endif #endif #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE @@ -299,6 +296,9 @@ Instance::Instance(void) #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE , mNat64Translator(*this) #endif +#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE + , mDnsNameCompressionEnabled(true) +#endif #endif // OPENTHREAD_MTD || OPENTHREAD_FTD #if OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE , mLinkRaw(*this) @@ -514,6 +514,26 @@ exit: return error; } +#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE +Error Instance::SetDnsNameCompressionEnabled(bool aEnabled) +{ + Error error = kErrorNone; + +#if OPENTHREAD_CONFIG_MULTICAST_DNS_ENABLE + if (Get().IsEnabled()) + { + VerifyOrExit(aEnabled, error = kErrorNotCapable); + } +#endif + + mDnsNameCompressionEnabled = aEnabled; + ExitNow(); + +exit: + return error; +} +#endif + void Instance::GetBufferInfo(BufferInfo &aInfo) { aInfo.Clear(); diff --git a/src/core/instance/instance.hpp b/src/core/instance/instance.hpp index 2529b7f1c..00d35354e 100644 --- a/src/core/instance/instance.hpp +++ b/src/core/instance/instance.hpp @@ -371,23 +371,31 @@ public: #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE /** - * Enables/disables the "DNS name compressions" mode. + * Enables/disables the "DNS name compression" mode. * - * By default DNS name compression is enabled. When disabled, DNS names are appended as full and never compressed. - * This is applicable to OpenThread's DNS and SRP client/server modules. + * By default, DNS name compression is enabled. When disabled, DNS names are appended in full and are never + * compressed. This applies to OpenThread's DNS and SRP client/server modules. + * + * DNS name compression cannot be disabled if the OpenThread mDNS module is enabled. Enabling the mDNS module will + * automatically enable name compression if it was previously disabled. Attempting to disable compression while the + * mDNS module is active will return `kErrorNotCapable`. * * This is intended for testing only and available under a `REFERENCE_DEVICE` config. * * @param[in] aEnabled TRUE to enable the "DNS name compression" mode, FALSE to disable. + * + * @retval kErrorNone The "DNS name compression" mode is updated. + * @retval kErrorNotCapable The "DNS name compression" mode cannot be disabled since OpenThread mDNS module is + * enabled. */ - static void SetDnsNameCompressionEnabled(bool aEnabled) { sDnsNameCompressionEnabled = aEnabled; } + Error SetDnsNameCompressionEnabled(bool aEnabled); /** * Indicates whether the "DNS name compression" mode is enabled or not. * - * @returns TRUE if the "DNS name compressions" mode is enabled, FALSE otherwise. + * @returns TRUE if the "DNS name compression" mode is enabled, FALSE otherwise. */ - static bool IsDnsNameCompressionEnabled(void) { return sDnsNameCompressionEnabled; } + bool IsDnsNameCompressionEnabled(void) { return mDnsNameCompressionEnabled; } #endif /** @@ -450,10 +458,6 @@ private: static Utils::Heap *sHeap; #endif -#if (OPENTHREAD_MTD || OPENTHREAD_FTD) && OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - static bool sDnsNameCompressionEnabled; -#endif - //----------------------------------------------------------------------------------------------------------------- // Order of variables (their initialization in `Instance`) @@ -760,6 +764,10 @@ private: Nat64::Translator mNat64Translator; #endif +#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE + bool mDnsNameCompressionEnabled; +#endif + #endif // OPENTHREAD_MTD || OPENTHREAD_FTD #if OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE diff --git a/src/core/net/dns_types.cpp b/src/core/net/dns_types.cpp index 1d3a89aae..fa47fa207 100644 --- a/src/core/net/dns_types.cpp +++ b/src/core/net/dns_types.cpp @@ -285,7 +285,7 @@ Error Name::AppendPointerLabel(uint16_t aOffset, Message &aMessage) uint16_t value; #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE - if (!Instance::IsDnsNameCompressionEnabled()) + if (!aMessage.GetInstance().IsDnsNameCompressionEnabled()) { // If "DNS name compression" mode is disabled, instead of // appending the pointer label, read the name from the message diff --git a/src/core/net/mdns.cpp b/src/core/net/mdns.cpp index 02de62459..616a8a043 100644 --- a/src/core/net/mdns.cpp +++ b/src/core/net/mdns.cpp @@ -141,6 +141,10 @@ Error Core::SetEnabled(bool aEnable, uint32_t aInfraIfIndex, Requester aRequeste { LogInfo("%snabling on infra-if-index %lu", (aRequester == kRequesterAuto) ? "Auto-e" : "E", ToUlong(mInfraIfIndex)); + +#if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE + SuccessOrAssert(GetInstance().SetDnsNameCompressionEnabled(true)); +#endif } else {