From 911153c7bc25d385141ef888a7ea0656fec4a90e Mon Sep 17 00:00:00 2001 From: Pradip De Date: Wed, 10 Jul 2024 13:06:33 -0700 Subject: [PATCH] Fix DNS-SD TCP advertisement to use a bitmap for Client and Server. When TCP is enabled, a node, currently, advertises as both a Client and Server. --- src/app/server/Dnssd.cpp | 11 +++++++++++ src/app/server/Dnssd.h | 6 ++++++ src/lib/dnssd/Advertiser.h | 17 ++++++++++++----- src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp | 6 +++--- src/lib/dnssd/Discovery_ImplPlatform.cpp | 3 ++- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/app/server/Dnssd.cpp b/src/app/server/Dnssd.cpp index 3cdcd35f9a7e98..bdd48accf9cb8d 100644 --- a/src/app/server/Dnssd.cpp +++ b/src/app/server/Dnssd.cpp @@ -170,6 +170,14 @@ void DnssdServer::AddICDKeyToAdvertisement(AdvertisingParams & advParams) } #endif +#if INET_CONFIG_ENABLE_TCP_ENDPOINT +template +void DnssdServer::AddTCPSupportToAdvertisement(AdvertisingParams & advertiseParameters) +{ + advertiseParameters.SetTCPSupportModes(chip::Dnssd::TCPModeAdvertise::kTCPClientServer); +} +#endif + void DnssdServer::GetPrimaryOrFallbackMACAddress(chip::MutableByteSpan mac) { if (ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR) @@ -213,6 +221,9 @@ CHIP_ERROR DnssdServer::AdvertiseOperational() AddICDKeyToAdvertisement(advertiseParameters); #endif +#if INET_CONFIG_ENABLE_TCP_ENDPOINT + AddTCPSupportToAdvertisement(advertiseParameters); +#endif auto & mdnsAdvertiser = chip::Dnssd::ServiceAdvertiser::Instance(); ChipLogProgress(Discovery, "Advertise operational node " ChipLogFormatX64 "-" ChipLogFormatX64, diff --git a/src/app/server/Dnssd.h b/src/app/server/Dnssd.h index 105318ca5a08be..e1bac22585ae28 100644 --- a/src/app/server/Dnssd.h +++ b/src/app/server/Dnssd.h @@ -96,6 +96,12 @@ class DLL_EXPORT DnssdServer : public ICDStateObserver void SetICDManager(ICDManager * manager) { mICDManager = manager; }; #endif + +#if INET_CONFIG_ENABLE_TCP_ENDPOINT + template + void AddTCPSupportToAdvertisement(AdvertisingParams & advParams); +#endif + /// Start operational advertising CHIP_ERROR AdvertiseOperational(); diff --git a/src/lib/dnssd/Advertiser.h b/src/lib/dnssd/Advertiser.h index 02888181f98181..491b569d4794a1 100644 --- a/src/lib/dnssd/Advertiser.h +++ b/src/lib/dnssd/Advertiser.h @@ -55,6 +55,14 @@ enum class ICDModeAdvertise : uint8_t kLIT, // The ICD is currently operating as a LIT. ICD=1 in DNS-SD key/value pairs. }; +enum class TCPModeAdvertise : uint8_t +{ + kNone = 0, // The device does not support TCP. + kTCPClient = 1 << 1, // The device supports the TCP client. + kTCPServer = 1 << 2, // The device supports the TCP server. + kTCPClientServer = (kTCPClient | kTCPServer), // The device supports both the TCP client and server. +}; + template class BaseAdvertisingParams { @@ -102,13 +110,12 @@ class BaseAdvertisingParams } const std::optional & GetLocalMRPConfig() const { return mLocalMRPConfig; } - // NOTE: The SetTcpSupported API is deprecated and not compliant with 1.3. T flag should not be set. - Derived & SetTcpSupported(std::optional tcpSupported) + Derived & SetTCPSupportModes(TCPModeAdvertise tcpSupportModes) { - mTcpSupported = tcpSupported; + mTcpSupportModes = tcpSupportModes; return *reinterpret_cast(this); } - std::optional GetTcpSupported() const { return mTcpSupported; } + TCPModeAdvertise GetTCPSupportModes() const { return mTcpSupportModes; } Derived & SetICDModeToAdvertise(ICDModeAdvertise operatingMode) { @@ -124,7 +131,7 @@ class BaseAdvertisingParams uint8_t mMacStorage[kMaxMacSize] = {}; size_t mMacLength = 0; std::optional mLocalMRPConfig; - std::optional mTcpSupported; + TCPModeAdvertise mTcpSupportModes = TCPModeAdvertise::kNone; ICDModeAdvertise mICDModeAdvertise = ICDModeAdvertise::kNone; }; diff --git a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp index 5ca002673cd4c9..ce46137c9a00e2 100644 --- a/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Advertiser_ImplMinimalMdns.cpp @@ -278,10 +278,10 @@ class AdvertiserMinMdns : public ServiceAdvertiser, } } - if (const auto & tcpSupported = params.GetTcpSupported(); tcpSupported.has_value()) + if (params.GetTCPSupportModes() != TCPModeAdvertise::kNone) { - size_t writtenCharactersNumber = - static_cast(snprintf(storage.tcpSupportedBuf, sizeof(storage.tcpSupportedBuf), "T=%d", *tcpSupported)); + size_t writtenCharactersNumber = static_cast(snprintf(storage.tcpSupportedBuf, sizeof(storage.tcpSupportedBuf), + "T=%d", static_cast(params.GetTCPSupportModes()))); VerifyOrReturnError((writtenCharactersNumber > 0) && (writtenCharactersNumber < sizeof(storage.tcpSupportedBuf)), CHIP_ERROR_INVALID_STRING_LENGTH); txtFields[numTxtFields++] = storage.tcpSupportedBuf; diff --git a/src/lib/dnssd/Discovery_ImplPlatform.cpp b/src/lib/dnssd/Discovery_ImplPlatform.cpp index beb733b1276a3f..8cdb9d5261afe1 100644 --- a/src/lib/dnssd/Discovery_ImplPlatform.cpp +++ b/src/lib/dnssd/Discovery_ImplPlatform.cpp @@ -240,7 +240,8 @@ CHIP_ERROR CopyTxtRecord(TxtFieldKey key, char * buffer, size_t bufferLen, const switch (key) { case TxtFieldKey::kTcpSupported: - return CopyTextRecordValue(buffer, bufferLen, params.GetTcpSupported()); + VerifyOrReturnError(params.GetTCPSupportModes() != TCPModeAdvertise::kNone, CHIP_ERROR_UNINITIALIZED); + return CopyTextRecordValue(buffer, bufferLen, static_cast(params.GetTCPSupportModes())); case TxtFieldKey::kSessionIdleInterval: #if CHIP_CONFIG_ENABLE_ICD_SERVER // A ICD operating as a LIT should not advertise its slow polling interval