[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.
This commit is contained in:
Abtin Keshavarzian
2022-02-08 22:37:17 -08:00
committed by GitHub
parent 4fb966e3eb
commit adf4e84caf
2 changed files with 28 additions and 49 deletions
+11 -1
View File
@@ -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;
+17 -48
View File
@@ -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<kTxtDataSize> string;
uint8_t txtDataBuffer[kTxtDataSize];
MutableData<kWithUint16Length> txtData;
Dns::TxtEntry txtEntries[2];
VerifyOrExit(mInitialized && mEnabled);
string.Append("%s=", kTxtRecordExtAddressKey);
string.AppendHexBytes(Get<Mac::Mac>().GetExtAddress().m8, sizeof(Mac::ExtAddress));
txtEntries[0].Init(kTxtRecordExtAddressKey, Get<Mac::Mac>().GetExtAddress().m8, sizeof(Mac::ExtAddress));
txtEntries[1].Init(kTxtRecordExtPanIdKey, Get<Mac::Mac>().GetExtendedPanId().m8, sizeof(Mac::ExtendedPanId));
*txtPtr++ = static_cast<uint8_t>(string.GetLength());
memcpy(txtPtr, string.AsCString(), string.GetLength());
txtPtr += string.GetLength();
string.Clear();
string.Append("%s=", kTxtRecordExtPanIdKey);
string.AppendHexBytes(Get<Mac::Mac>().GetExtendedPanId().m8, sizeof(Mac::ExtendedPanId));
*txtPtr++ = static_cast<uint8_t>(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<Mac::Mac>().GetExtAddress().ToString().AsCString(), kTxtRecordExtPanIdKey,
Get<Mac::Mac>().GetExtendedPanId().ToString().AsCString());
otPlatTrelRegisterService(&GetInstance(), mUdpPort, txtData, sizeof(txtData));
otPlatTrelRegisterService(&GetInstance(), mUdpPort, txtData.GetBytes(), static_cast<uint8_t>(txtData.GetLength()));
exit:
return;
@@ -246,27 +234,6 @@ exit:
return;
}
template <uint16_t kBufferSize>
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;
}