Skip to content

Commit

Permalink
Make minmdns be move verbose in terms of packets sent and received. (#…
Browse files Browse the repository at this point in the history
…18566)

* Add some log points for minmdns parsing

* Log when operational/commission records were found

* Log IP addresses as well

* Update logging for resource data a bit to be less repeating

* Conditional compile on progress logging

* code clarity: add endif

* Restyle
  • Loading branch information
andy31415 authored May 19, 2022
1 parent d1b8ba9 commit 329389d
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/lib/dnssd/IncrementalResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <lib/dnssd/ServiceNaming.h>
#include <lib/dnssd/TxtFields.h>
#include <lib/dnssd/minimal_mdns/Logging.h>
#include <lib/dnssd/minimal_mdns/core/RecordWriter.h>
#include <lib/support/CHIPMemString.h>
#include <trace/trace.h>
Expand All @@ -26,6 +27,7 @@ namespace chip {
namespace Dnssd {

using namespace mdns::Minimal;
using namespace mdns::Minimal::Logging;

namespace {

Expand Down Expand Up @@ -187,6 +189,8 @@ CHIP_ERROR IncrementalResolver::InitializeParsing(mdns::Minimal::SerializedQName
return err;
}
}

LogFoundOperationalSrvRecord(mSpecificResolutionData.Get<OperationalNodeData>().peerId, mTargetHostName.Get());
break;
case ServiceNameType::kCommissioner:
case ServiceNameType::kCommissionable:
Expand All @@ -202,6 +206,8 @@ CHIP_ERROR IncrementalResolver::InitializeParsing(mdns::Minimal::SerializedQName

Platform::CopyString(mSpecificResolutionData.Get<CommissionNodeData>().instanceName, nameCopy.Value());
}

LogFoundCommissionSrvRecord(mSpecificResolutionData.Get<CommissionNodeData>().instanceName, mTargetHostName.Get());
break;
default:
return CHIP_ERROR_INVALID_ARGUMENT;
Expand Down Expand Up @@ -328,6 +334,9 @@ CHIP_ERROR IncrementalResolver::OnIpAddress(Inet::InterfaceId interface, const I
}

mCommonResolutionData.ipAddress[mCommonResolutionData.numIPs++] = addr;

LogFoundIPAddress(mTargetHostName.Get(), addr);

return CHIP_NO_ERROR;
}

Expand Down
11 changes: 11 additions & 0 deletions src/lib/dnssd/Resolver_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <lib/dnssd/MinimalMdnsServer.h>
#include <lib/dnssd/ResolverProxy.h>
#include <lib/dnssd/ServiceNaming.h>
#include <lib/dnssd/minimal_mdns/Logging.h>
#include <lib/dnssd/minimal_mdns/Parser.h>
#include <lib/dnssd/minimal_mdns/QueryBuilder.h>
#include <lib/dnssd/minimal_mdns/RecordData.h>
Expand Down Expand Up @@ -140,10 +141,16 @@ void PacketParser::OnResource(ResourceType type, const ResourceData & data)
{
return;
}
mdns::Minimal::Logging::LogReceivedResource(data);
ParseSRVResource(data);
break;
}
case RecordParsingState::kRecordParsing:
if (data.GetType() != QType::SRV)
{
// SRV packets logged during 'SrvInitialization' phase
mdns::Minimal::Logging::LogReceivedResource(data);
}
ParseResource(data);
break;
case RecordParsingState::kIdle:
Expand Down Expand Up @@ -471,6 +478,8 @@ CHIP_ERROR MinMdnsResolver::BuildQuery(QueryBuilder & builder, const ActiveResol
.SetType(QType::ANY) //
.SetAnswerViaUnicast(firstSend) //
;

mdns::Minimal::Logging::LogSendingQuery(query);
builder.AddQuery(query);

return CHIP_NO_ERROR;
Expand All @@ -493,6 +502,7 @@ CHIP_ERROR MinMdnsResolver::BuildQuery(QueryBuilder & builder, const ActiveResol
.SetAnswerViaUnicast(firstSend) //
;

mdns::Minimal::Logging::LogSendingQuery(query);
builder.AddQuery(query);

return CHIP_NO_ERROR;
Expand All @@ -510,6 +520,7 @@ CHIP_ERROR MinMdnsResolver::BuildQuery(QueryBuilder & builder, const ActiveResol
.SetAnswerViaUnicast(firstSend) //
;

mdns::Minimal::Logging::LogSendingQuery(query);
builder.AddQuery(query);

return CHIP_NO_ERROR;
Expand Down
16 changes: 16 additions & 0 deletions src/lib/dnssd/minimal_mdns/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ declare_args() {
# more UDP socket and corresponding code for the unicast query sending)
chip_use_ephemeral_port_for_mdns_unicast_query =
current_os == "mac" || current_os == "linux" || current_os == "android"

# Be very verbose regarding packet communication. Will spend extra RAM and
# Time to report mdns traffic in terms of sent and seen data.
chip_minmdns_high_verbosity =
current_os == "mac" || current_os == "linux" || current_os == "android"
}

config("config") {
Expand All @@ -37,10 +42,17 @@ config("config") {
} else {
defines += [ "CHIP_MINMDNS_USE_EPHEMERAL_UNICAST_PORT=0" ]
}

if (chip_minmdns_high_verbosity) {
defines += [ "CHIP_MINMDNS_HIGH_VERBOSITY=1" ]
} else {
defines += [ "CHIP_MINMDNS_HIGH_VERBOSITY=0" ]
}
}

static_library("minimal_mdns") {
sources = [
"Logging.h",
"Parser.cpp",
"Parser.h",
"Query.h",
Expand All @@ -55,6 +67,10 @@ static_library("minimal_mdns") {
"Server.h",
]

if (chip_minmdns_high_verbosity) {
sources += [ "Logging.cpp" ]
}

public_deps = [
"${chip_root}/src/inet",
"${chip_root}/src/lib/core",
Expand Down
147 changes: 147 additions & 0 deletions src/lib/dnssd/minimal_mdns/Logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <lib/dnssd/minimal_mdns/Logging.h>
#include <lib/support/StringBuilder.h>
#include <lib/support/logging/CHIPLogging.h>

namespace mdns {
namespace Minimal {
namespace Logging {

namespace {

#if CHIP_PROGRESS_LOGGING

const char * QueryTypeToString(mdns::Minimal::QType type)
{
// NOTE: not all values are handled, only things that matter
// and minmdns really cares about
switch (type)
{
case QType::A:
return "A";
case QType::PTR:
return "PTR";
case QType::TXT:
return "TXT";
case QType::AAAA:
return "AAAA";
case QType::SRV:
return "SRV";
case QType::ANY:
return "ANY";
default:
// Not reentrant, however our logging is in the chip thread so seems ok.
static char buff[16];
snprintf(buff, sizeof(buff), "(%d)", static_cast<int>(type));
return buff;
}
}

#endif // CHIP_PROGRESS_LOGGING

class QNameString
{
public:
QNameString(const mdns::Minimal::FullQName & name)
{
for (unsigned i = 0; i < name.nameCount; i++)
{
if (i != 0)
{
mBuffer.Add(".");
}
mBuffer.Add(name.names[i]);
}
}

QNameString(mdns::Minimal::SerializedQNameIterator name)
{
bool first = true;
while (name.Next())
{
if (first)
{
first = false;
}
else
{
mBuffer.Add(".");
}
mBuffer.Add(name.Value());
}
if (!name.IsValid())
{
mBuffer.Add("(!INVALID!)");
}
}

const char * c_str() const { return mBuffer.c_str(); }

bool Fit() const { return mBuffer.Fit(); }

private:
static constexpr size_t kMaxQNameLength = 128;
chip::StringBuilder<kMaxQNameLength> mBuffer;
};

} // namespace

void LogSendingQuery(const mdns::Minimal::Query & query)
{
QNameString name(query.GetName());

ChipLogProgress(Discovery, "MINMDNS: Sending query %s/%s for %s%s", QueryTypeToString(query.GetType()),
query.IsAnswerViaUnicast() ? "UNICAST" : "MULTICAST", name.c_str(), name.Fit() ? "" : "...");
}

void LogReceivedResource(const mdns::Minimal::ResourceData & data)
{
QNameString name(data.GetName());

ChipLogProgress(Discovery, "MINMDNS: received %s record for %s%s", QueryTypeToString(data.GetType()), name.c_str(),
name.Fit() ? "" : "...");
}

void LogFoundOperationalSrvRecord(const chip::PeerId & peerId, const mdns::Minimal::SerializedQNameIterator & targetHost)
{
QNameString host(targetHost);

ChipLogProgress(Discovery, "MINMDNS: Operational SRV for " ChipLogFormatX64 "-" ChipLogFormatX64 ": %s",
ChipLogValueX64(peerId.GetCompressedFabricId()), ChipLogValueX64(peerId.GetNodeId()), host.c_str());
}

void LogFoundCommissionSrvRecord(const char * instance, const mdns::Minimal::SerializedQNameIterator & targetHost)
{
QNameString host(targetHost);

ChipLogProgress(Discovery, "MINMDNS: Commission SRV for instance %s: %s", instance, host.c_str());
}

void LogFoundIPAddress(const mdns::Minimal::SerializedQNameIterator & targetHost, const chip::Inet::IPAddress & addr)
{
QNameString host(targetHost);
char ipBuff[chip::Inet::IPAddress::kMaxStringLength];

addr.ToString(ipBuff);

ChipLogProgress(Discovery, "MINMDNS: IP address %s found for %s%s", ipBuff, host.c_str(), host.Fit() ? "" : "...");
}

} // namespace Logging
} // namespace Minimal
} // namespace mdns
51 changes: 51 additions & 0 deletions src/lib/dnssd/minimal_mdns/Logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include <inet/IPAddress.h>
#include <lib/core/PeerId.h>
#include <lib/dnssd/minimal_mdns/Parser.h>
#include <lib/dnssd/minimal_mdns/Query.h>

namespace mdns {
namespace Minimal {

/// Provides potentially verbose logging for DNSSD operations when using
/// MinMdns.
namespace Logging {

#if CHIP_MINMDNS_HIGH_VERBOSITY

void LogSendingQuery(const mdns::Minimal::Query & query);
void LogReceivedResource(const mdns::Minimal::ResourceData & data);
void LogFoundOperationalSrvRecord(const chip::PeerId & peerId, const mdns::Minimal::SerializedQNameIterator & targetHost);
void LogFoundCommissionSrvRecord(const char * instance, const mdns::Minimal::SerializedQNameIterator & targetHost);
void LogFoundIPAddress(const mdns::Minimal::SerializedQNameIterator & targetHost, const chip::Inet::IPAddress & addr);

#else

inline void LogSendingQuery(const mdns::Minimal::Query & query) {}
inline void LogReceivedResource(const mdns::Minimal::ResourceData & data) {}
inline void LogFoundOperationalSrvRecord(const chip::PeerId & peerId, const mdns::Minimal::SerializedQNameIterator & targetHost) {}
inline void LogFoundCommissionSrvRecord(const char * instance, const mdns::Minimal::SerializedQNameIterator & targetHost) {}
inline void LogFoundIPAddress(const mdns::Minimal::SerializedQNameIterator & targetHost, const chip::Inet::IPAddress & addr) {}

#endif

} // namespace Logging
} // namespace Minimal
} // namespace mdns
2 changes: 2 additions & 0 deletions src/lib/dnssd/minimal_mdns/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class Query
return *this;
}

const FullQName & GetName() const { return mQName; }

/// Append the query to the specified buffer
///
/// @param hdr will be updated with a query count
Expand Down

0 comments on commit 329389d

Please sign in to comment.