Skip to content

Commit

Permalink
Deduplicate the detection of usable mdns interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
andy31415 committed Feb 18, 2021
1 parent 07e3e79 commit 9fecbf9
Showing 1 changed file with 26 additions and 46 deletions.
72 changes: 26 additions & 46 deletions src/lib/mdns/Advertiser_ImplMinimalMdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ void LogQuery(const QueryData & data)
ChipLogDetail(Discovery, "%s", logString.c_str());
}

/// Checks if the current interface is powered on
/// and not local loopback.
template <typename T>
bool IsCurrentInterfaceUsable(T & iterator)
{
if (!mIterator.IsUp() || !mIterator.SupportsMulticast())
{
return false; // not a usable interface
}
char name[chip::Inet::InterfaceIterator::kMaxIfNameLength];
if (mIterator.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to get interface name.");
return false;
}

if (strncmp(name, "lo", 2) == 0)
{
/// local loopback interface is not usable by MDNS
return false;
}
return true;
}

class AllInterfaces : public ListenIterator
{
private:
Expand Down Expand Up @@ -152,23 +176,7 @@ class AllInterfaces : public ListenIterator
return false; // nothing to try.
}

if (!mIterator.IsUp() || !mIterator.SupportsMulticast())
{
return true; // not a usable interface
}
char name[chip::Inet::InterfaceIterator::kMaxIfNameLength];
if (mIterator.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Interface iterator failed to get interface name.");
return true;
}

if (strncmp(name, "lo", 2) == 0)
{
ChipLogDetail(Discovery, "Skipping interface '%s' (assume local loopback)", name);
return true;
}
return false;
return !IsCurrentInterfaceUsable(mIterator);
}
};

Expand Down Expand Up @@ -584,34 +592,6 @@ FullQName AdvertiserMinMdns::GetCommisioningTextEntries(const CommissionAdvertis
}
}

bool CanAdvertiseOn(chip::Inet::InterfaceAddressIterator & current)
{
if (!current.IsUp())
{
return false;
}

if (!current.SupportsMulticast())
{
return false;
}

char name[chip::Inet::InterfaceIterator::kMaxIfNameLength];
if (current.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to get interface name - will not advertise on this interface");
return false;
}

if (strncmp(name, "lo", 2) == 0)
{
// Skip advertisement on what looks like a local loopback interface
return false;
}

return true;
}

bool AdvertiserMinMdns::ShouldAdvertiseOn(const chip::Inet::InterfaceId id, const chip::Inet::IPAddress & addr)
{
for (unsigned i = 0; i < mServer.GetEndpointCount(); i++)
Expand Down Expand Up @@ -650,7 +630,7 @@ void AdvertiserMinMdns::AdvertiseRecords()

for (; interfaceAddress.HasCurrent(); interfaceAddress.Next())
{
if (!CanAdvertiseOn(interfaceAddress))
if (!IsCurrentInterfaceUsable(interfaceAddress))
{
continue;
}
Expand Down

0 comments on commit 9fecbf9

Please sign in to comment.