From adf4e84caf71b63f3026640fdd31055daf1e27e8 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 8 Feb 2022 22:37:17 -0800 Subject: [PATCH] [trel] change TXT data format to use binary value (#7401) This commit changes the TXT record data format to use binary value for extended address and extended PAN ID entries. --- src/core/net/dns_types.hpp | 12 +++++- src/core/radio/trel_interface.cpp | 65 ++++++++----------------------- 2 files changed, 28 insertions(+), 49 deletions(-) diff --git a/src/core/net/dns_types.hpp b/src/core/net/dns_types.hpp index 33b3f9d5e..4b2817ee6 100644 --- a/src/core/net/dns_types.hpp +++ b/src/core/net/dns_types.hpp @@ -1137,7 +1137,17 @@ public: * @param[in] aValueLength Number of bytes in @p aValue buffer. * */ - TxtEntry(const char *aKey, const uint8_t *aValue, uint8_t aValueLength) + TxtEntry(const char *aKey, const uint8_t *aValue, uint8_t aValueLength) { Init(aKey, aValue, aValueLength); } + + /** + * This method initializes a `TxtEntry` object. + * + * @param[in] aKey A pointer to the key string. + * @param[in] aValue A pointer to a buffer containing the value. + * @param[in] aValueLength Number of bytes in @p aValue buffer. + * + */ + void Init(const char *aKey, const uint8_t *aValue, uint8_t aValueLength) { mKey = aKey; mValue = aValue; diff --git a/src/core/radio/trel_interface.cpp b/src/core/radio/trel_interface.cpp index a2cf1a56e..3906bbea4 100644 --- a/src/core/radio/trel_interface.cpp +++ b/src/core/radio/trel_interface.cpp @@ -44,7 +44,6 @@ #include "common/logging.hpp" #include "common/string.hpp" #include "net/dns_types.hpp" -#include "utils/parse_cmdline.hpp" namespace ot { namespace Trel { @@ -132,41 +131,30 @@ void Interface::HandleRegisterServiceTask(Tasklet &aTasklet) void Interface::RegisterService(void) { - // TXT data consists of two entries: the length fields , the - // "key" string, "=" char, and hex representation of the MAC or - // Extended PAN ID values. + // TXT data consists of two entries: the length fields, the + // "key" string, "=" char, and binary representation of the MAC + // or Extended PAN ID values. static constexpr uint8_t kTxtDataSize = - sizeof(uint8_t) + sizeof(kTxtRecordExtAddressKey) - 1 + sizeof(char) + sizeof(Mac::ExtAddress) * 2 + - sizeof(uint8_t) + sizeof(kTxtRecordExtPanIdKey) - 1 + sizeof(char) + sizeof(Mac::ExtendedPanId) * 2; + /* ExtAddr */ sizeof(uint8_t) + sizeof(kTxtRecordExtAddressKey) - 1 + sizeof(char) + sizeof(Mac::ExtAddress) + + /* ExtPanId */ sizeof(uint8_t) + sizeof(kTxtRecordExtPanIdKey) - 1 + sizeof(char) + sizeof(Mac::ExtendedPanId); - uint8_t txtData[kTxtDataSize]; - uint8_t * txtPtr = &txtData[0]; - String string; + uint8_t txtDataBuffer[kTxtDataSize]; + MutableData txtData; + Dns::TxtEntry txtEntries[2]; VerifyOrExit(mInitialized && mEnabled); - string.Append("%s=", kTxtRecordExtAddressKey); - string.AppendHexBytes(Get().GetExtAddress().m8, sizeof(Mac::ExtAddress)); + txtEntries[0].Init(kTxtRecordExtAddressKey, Get().GetExtAddress().m8, sizeof(Mac::ExtAddress)); + txtEntries[1].Init(kTxtRecordExtPanIdKey, Get().GetExtendedPanId().m8, sizeof(Mac::ExtendedPanId)); - *txtPtr++ = static_cast(string.GetLength()); - memcpy(txtPtr, string.AsCString(), string.GetLength()); - txtPtr += string.GetLength(); - - string.Clear(); - string.Append("%s=", kTxtRecordExtPanIdKey); - string.AppendHexBytes(Get().GetExtendedPanId().m8, sizeof(Mac::ExtendedPanId)); - - *txtPtr++ = static_cast(string.GetLength()); - memcpy(txtPtr, string.AsCString(), string.GetLength()); - txtPtr += string.GetLength(); - - OT_ASSERT(txtPtr == OT_ARRAY_END(txtData)); + txtData.Init(txtDataBuffer, sizeof(txtDataBuffer)); + SuccessOrAssert(Dns::TxtEntry::AppendEntries(txtEntries, OT_ARRAY_LENGTH(txtEntries), txtData)); otLogInfoMac("Trel: Registering DNS-SD service: port:%u, txt:\"%s=%s, %s=%s\"", mUdpPort, kTxtRecordExtAddressKey, Get().GetExtAddress().ToString().AsCString(), kTxtRecordExtPanIdKey, Get().GetExtendedPanId().ToString().AsCString()); - otPlatTrelRegisterService(&GetInstance(), mUdpPort, txtData, sizeof(txtData)); + otPlatTrelRegisterService(&GetInstance(), mUdpPort, txtData.GetBytes(), static_cast(txtData.GetLength())); exit: return; @@ -246,27 +234,6 @@ exit: return; } -template -static Error ParseValueAsHexString(const Dns::TxtEntry &aTxtEntry, uint8_t (&aBuffer)[kBufferSize]) -{ - // Parse the value from `Dns::TxtEntry` as a hex string and - // populates the parsed bytes into `aBuffer`. It requires parsing of - // the hex string to result in exactly `kBufferSize` decoded bytes. - - Error error = kErrorParse; - char hexString[kBufferSize * 2 + 1]; - - VerifyOrExit(aTxtEntry.mValueLength < sizeof(hexString)); - - memcpy(hexString, aTxtEntry.mValue, aTxtEntry.mValueLength); - hexString[aTxtEntry.mValueLength] = '\0'; - - error = Utils::CmdLineParser::ParseAsHexString(hexString, aBuffer); - -exit: - return error; -} - Error Interface::ParsePeerInfoTxtData(const Peer::Info & aInfo, Mac::ExtAddress & aExtAddress, Mac::ExtendedPanId &aExtPanId) const @@ -286,13 +253,15 @@ Error Interface::ParsePeerInfoTxtData(const Peer::Info & aInfo, if (strcmp(entry.mKey, kTxtRecordExtAddressKey) == 0) { VerifyOrExit(!parsedExtAddress, error = kErrorParse); - SuccessOrExit(error = ParseValueAsHexString(entry, aExtAddress.m8)); + VerifyOrExit(entry.mValueLength == sizeof(Mac::ExtAddress), error = kErrorParse); + aExtAddress.Set(entry.mValue); parsedExtAddress = true; } else if (strcmp(entry.mKey, kTxtRecordExtPanIdKey) == 0) { VerifyOrExit(!parsedExtPanId, error = kErrorParse); - SuccessOrExit(error = ParseValueAsHexString(entry, aExtPanId.m8)); + VerifyOrExit(entry.mValueLength == sizeof(Mac::ExtendedPanId), error = kErrorParse); + memcpy(aExtPanId.m8, entry.mValue, sizeof(Mac::ExtendedPanId)); parsedExtPanId = true; }