Skip to content

Commit

Permalink
Filter received mdns records to chip only. (#7527)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecille authored and pull[bot] committed Jun 16, 2021
1 parent 22d165b commit 4186227
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions src/lib/mdns/Resolver_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,6 @@ void PacketDataReporter::OnOperationalSrvRecord(SerializedQNameIterator name, co
return;
}

// Before attempting to parse hex values for node/fabrid, validate
// that he response is indeed from a chip tcp service.
{
SerializedQNameIterator suffix = name;

constexpr const char * kExpectedSuffix[] = { "_chip", "_tcp", "local" };

if (suffix != FullQName(kExpectedSuffix))
{
#ifdef MINMDNS_RESOLVER_OVERLY_VERBOSE
ChipLogError(Discovery, "mDNS packet is not for a CHIP device");
#endif
mHasNodePort = false;
return;
}
}

if (ExtractIdFromInstanceName(name.Value(), &mNodeData.mPeerId) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to parse peer id from %s", name.Value());
Expand All @@ -171,11 +154,6 @@ void PacketDataReporter::OnOperationalSrvRecord(SerializedQNameIterator name, co

mNodeData.mPort = srv.GetPort();
mHasNodePort = true;

if (mHasIP)
{
mDelegate->OnNodeIdResolved(mNodeData);
}
}

void PacketDataReporter::OnCommissionableNodeSrvRecord(SerializedQNameIterator name, const SrvRecord & srv)
Expand All @@ -199,11 +177,6 @@ void PacketDataReporter::OnOperationalIPAddress(const chip::Inet::IPAddress & ad
// (if multi-admin decides to use unique ports for every ecosystem).
mNodeData.mAddress = addr;
mHasIP = true;

if (mHasNodePort)
{
mDelegate->OnNodeIdResolved(mNodeData);
}
}

void PacketDataReporter::OnCommissionableNodeIPAddress(const chip::Inet::IPAddress & addr)
Expand All @@ -215,6 +188,18 @@ void PacketDataReporter::OnCommissionableNodeIPAddress(const chip::Inet::IPAddre
mCommissionableNodeData.ipAddress[mCommissionableNodeData.numIPs++] = addr;
}

bool HasQNamePart(SerializedQNameIterator qname, QNamePart part)
{
while (qname.Next())
{
if (strcmp(qname.Value(), part) == 0)
{
return true;
}
}
return false;
}

void PacketDataReporter::OnResource(ResourceType type, const ResourceData & data)
{
if (!mValid)
Expand All @@ -238,11 +223,26 @@ void PacketDataReporter::OnResource(ResourceType type, const ResourceData & data
}
else if (mDiscoveryType == DiscoveryType::kOperational)
{
OnOperationalSrvRecord(data.GetName(), srv);
// Ensure this is our record.
if (HasQNamePart(data.GetName(), kOperationalServiceName))
{
OnOperationalSrvRecord(data.GetName(), srv);
}
else
{
mValid = false;
}
}
else if (mDiscoveryType == DiscoveryType::kCommissionableNode)
{
OnCommissionableNodeSrvRecord(data.GetName(), srv);
if (HasQNamePart(data.GetName(), kCommissionableServiceName))
{
OnCommissionableNodeSrvRecord(data.GetName(), srv);
}
else
{
mValid = false;
}
}
break;
}
Expand Down Expand Up @@ -304,6 +304,10 @@ void PacketDataReporter::OnComplete()
{
mDelegate->OnCommissionableNodeFound(mCommissionableNodeData);
}
else if (mDiscoveryType == DiscoveryType::kOperational && mHasIP && mHasNodePort)
{
mDelegate->OnNodeIdResolved(mNodeData);
}
}

class MinMdnsResolver : public Resolver, public MdnsPacketDelegate
Expand Down

0 comments on commit 4186227

Please sign in to comment.