diff --git a/src/lib/dnssd/IncrementalResolve.cpp b/src/lib/dnssd/IncrementalResolve.cpp index 2ac6ffd2f0484e..206d1a2f059cff 100644 --- a/src/lib/dnssd/IncrementalResolve.cpp +++ b/src/lib/dnssd/IncrementalResolve.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,7 @@ namespace chip { namespace Dnssd { using namespace mdns::Minimal; +using namespace mdns::Minimal::Logging; namespace { @@ -187,6 +189,8 @@ CHIP_ERROR IncrementalResolver::InitializeParsing(mdns::Minimal::SerializedQName return err; } } + + LogFoundOperationalSrvRecord(mSpecificResolutionData.Get().peerId, mTargetHostName.Get()); break; case ServiceNameType::kCommissioner: case ServiceNameType::kCommissionable: @@ -202,6 +206,8 @@ CHIP_ERROR IncrementalResolver::InitializeParsing(mdns::Minimal::SerializedQName Platform::CopyString(mSpecificResolutionData.Get().instanceName, nameCopy.Value()); } + + LogFoundCommissionSrvRecord(mSpecificResolutionData.Get().instanceName, mTargetHostName.Get()); break; default: return CHIP_ERROR_INVALID_ARGUMENT; @@ -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; } diff --git a/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp b/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp index 30500a6c800251..88afbebf7084e2 100644 --- a/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp +++ b/src/lib/dnssd/Resolver_ImplMinimalMdns.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -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: @@ -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; @@ -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; @@ -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; diff --git a/src/lib/dnssd/minimal_mdns/BUILD.gn b/src/lib/dnssd/minimal_mdns/BUILD.gn index 6e824c5be89ad8..0b4d68f3724e54 100644 --- a/src/lib/dnssd/minimal_mdns/BUILD.gn +++ b/src/lib/dnssd/minimal_mdns/BUILD.gn @@ -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") { @@ -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", @@ -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", diff --git a/src/lib/dnssd/minimal_mdns/Logging.cpp b/src/lib/dnssd/minimal_mdns/Logging.cpp new file mode 100644 index 00000000000000..355086c5b4b7a0 --- /dev/null +++ b/src/lib/dnssd/minimal_mdns/Logging.cpp @@ -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 +#include +#include + +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(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 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 diff --git a/src/lib/dnssd/minimal_mdns/Logging.h b/src/lib/dnssd/minimal_mdns/Logging.h new file mode 100644 index 00000000000000..de00df4e2a7dca --- /dev/null +++ b/src/lib/dnssd/minimal_mdns/Logging.h @@ -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 +#include +#include +#include + +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 diff --git a/src/lib/dnssd/minimal_mdns/Query.h b/src/lib/dnssd/minimal_mdns/Query.h index cfdd1b1120fe1e..beabf62a5eb7ee 100644 --- a/src/lib/dnssd/minimal_mdns/Query.h +++ b/src/lib/dnssd/minimal_mdns/Query.h @@ -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