Skip to content

Commit

Permalink
Stop spitting out binary TXT records as text. (#22172)
Browse files Browse the repository at this point in the history
Instead, actually look at record lengths, remove the lengths in favor
of comma-separating the records, replace various things that are not
printable with hex escape sequences.

The new output ends up looking something like:

    TXT:"SII=5000,SAI=2000,T=1"

Fixes #21867
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 16, 2023
1 parent fdbd92b commit be95555
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/platform/Darwin/DnssdContexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,40 @@ bool ResolveContext::HasAddress()
void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, const char * hostnameWithDomain, uint16_t port,
uint16_t txtLen, const unsigned char * txtRecord)
{
ChipLogDetail(Discovery, "Mdns : %s hostname:%s fullname:%s interface: %" PRIu32 " port: %u TXT:\"%.*s\"", __func__,
hostnameWithDomain, fullname, interfaceId, port, static_cast<int>(txtLen), txtRecord);
#if CHIP_DETAIL_LOGGING
std::string txtString;
auto txtRecordIter = txtRecord;
size_t remainingLen = txtLen;
while (remainingLen > 0)
{
size_t len = *txtRecordIter;
++txtRecordIter;
--remainingLen;
len = min(len, remainingLen);
chip::Span<const unsigned char> bytes(txtRecordIter, len);
if (txtString.size() > 0)
{
txtString.push_back(',');
}
for (auto & byte : bytes)
{
if ((std::isalnum(byte) || std::ispunct(byte)) && byte != '\\' && byte != ',')
{
txtString.push_back(static_cast<char>(byte));
}
else
{
char hex[5];
snprintf(hex, sizeof(hex), "\\x%02x", byte);
txtString.append(hex);
}
}
txtRecordIter += len;
remainingLen -= len;
}
#endif // CHIP_DETAIL_LOGGING
ChipLogDetail(Discovery, "Mdns : %s hostname:%s fullname:%s interface: %" PRIu32 " port: %u TXT:\"%s\"", __func__,
hostnameWithDomain, fullname, interfaceId, port, txtString.c_str());

InterfaceInfo interface;
interface.service.mPort = ntohs(port);
Expand Down

0 comments on commit be95555

Please sign in to comment.