Skip to content

Commit f46cdf3

Browse files
Fix Darwin dns-sd handling of results on interface index 0. (project-chip#32872)
project-chip#32513 introduced a bug, in that we would ignore all results associated with interface index 0. But that's perfectly valid for dns-sd results in general. What we want to ignore are cases where if_nametoindex returned 0, since those correspond to "no such interface name". The fix is to move "interfaceIndex == 0" check to the right spot. The rest of the changes are just there to improve logging. Specifically, log what's going on with our SRP resolve timer, and include the instance name being resolved in various logs so we can tell which logs have to do with which node being resolved.
1 parent 739923c commit f46cdf3

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

src/platform/Darwin/DnssdContexts.cpp

+36-16
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,26 @@ void ResolveContext::DispatchSuccess()
526526
// ChipDnssdResolveNoLongerNeeded don't find us and try to also remove us.
527527
bool needDelete = MdnsContexts::GetInstance().RemoveWithoutDeleting(this);
528528

529+
class AutoSelfDeleter
530+
{
531+
public:
532+
AutoSelfDeleter(bool needDelete, ResolveContext * self) : mNeedDelete(needDelete), mSelf(self) {}
533+
534+
~AutoSelfDeleter()
535+
{
536+
if (mNeedDelete)
537+
{
538+
MdnsContexts::GetInstance().Delete(mSelf);
539+
}
540+
}
541+
542+
private:
543+
bool mNeedDelete;
544+
ResolveContext * mSelf;
545+
};
546+
547+
AutoSelfDeleter selfDeleter(needDelete, this);
548+
529549
#if TARGET_OS_TV
530550
// On tvOS, prioritize results from en0, en1, ir0 in that order, if those
531551
// interfaces are present, since those will generally have more up-to-date
@@ -544,12 +564,15 @@ void ResolveContext::DispatchSuccess()
544564

545565
for (auto interfaceIndex : priorityInterfaceIndices)
546566
{
567+
if (interfaceIndex == 0)
568+
{
569+
// Not actually an interface we have, since if_nametoindex
570+
// returned 0.
571+
continue;
572+
}
573+
547574
if (TryReportingResultsForInterfaceIndex(static_cast<uint32_t>(interfaceIndex)))
548575
{
549-
if (needDelete)
550-
{
551-
MdnsContexts::GetInstance().Delete(this);
552-
}
553576
return;
554577
}
555578
}
@@ -559,24 +582,16 @@ void ResolveContext::DispatchSuccess()
559582
if (TryReportingResultsForInterfaceIndex(interface.first.interfaceId, interface.first.hostname,
560583
interface.first.isSRPResult))
561584
{
562-
break;
585+
return;
563586
}
564587
}
565588

566-
if (needDelete)
567-
{
568-
MdnsContexts::GetInstance().Delete(this);
569-
}
589+
ChipLogError(Discovery, "Successfully finalizing resolve for %s without finding any actual IP addresses.",
590+
instanceName.c_str());
570591
}
571592

572593
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex, const std::string & hostname, bool isSRPResult)
573594
{
574-
if (interfaceIndex == 0)
575-
{
576-
// Not actually an interface we have.
577-
return false;
578-
}
579-
580595
InterfaceKey interfaceKey = { interfaceIndex, hostname, isSRPResult };
581596
auto & interface = interfaces[interfaceKey];
582597
auto & ips = interface.addresses;
@@ -626,12 +641,16 @@ void ResolveContext::SRPTimerExpiredCallback(chip::System::Layer * systemLayer,
626641
{
627642
auto sdCtx = static_cast<ResolveContext *>(callbackContext);
628643
VerifyOrDie(sdCtx != nullptr);
644+
sdCtx->isSRPTimerRunning = false;
645+
646+
ChipLogProgress(Discovery, "SRP resolve timer for %s expired; completing resolve", sdCtx->instanceName.c_str());
629647
sdCtx->Finalize();
630648
}
631649

632650
void ResolveContext::CancelSRPTimer()
633651
{
634652
DeviceLayer::SystemLayer().CancelTimer(SRPTimerExpiredCallback, static_cast<void *>(this));
653+
ChipLogProgress(Discovery, "SRP resolve timer for %s cancelled; resolve timed out", instanceName.c_str());
635654
}
636655

637656
CHIP_ERROR ResolveContext::OnNewAddress(const InterfaceKey & interfaceKey, const struct sockaddr * address)
@@ -655,7 +674,8 @@ CHIP_ERROR ResolveContext::OnNewAddress(const InterfaceKey & interfaceKey, const
655674
#ifdef CHIP_PROGRESS_LOGGING
656675
char addrStr[INET6_ADDRSTRLEN];
657676
ip.ToString(addrStr, sizeof(addrStr));
658-
ChipLogProgress(Discovery, "Mdns: %s interface: %" PRIu32 " ip:%s", __func__, interfaceId, addrStr);
677+
ChipLogProgress(Discovery, "Mdns: %s instance: %s interface: %" PRIu32 " ip: %s", __func__, instanceName.c_str(), interfaceId,
678+
addrStr);
659679
#endif // CHIP_PROGRESS_LOGGING
660680

661681
if (ip.IsIPv6LinkLocal() && interfaceId == kDNSServiceInterfaceIndexLocalOnly)

src/platform/Darwin/DnssdImpl.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void LogOnFailure(const char * name, DNSServiceErrorType err)
7777
CHIP_ERROR StartSRPTimer(uint16_t timeoutInMSecs, ResolveContext * ctx)
7878
{
7979
VerifyOrReturnValue(ctx != nullptr, CHIP_ERROR_INCORRECT_STATE);
80+
ChipLogProgress(Discovery, "Starting timer to wait for possible SRP resolve results for %s", ctx->instanceName.c_str());
8081
return chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds16(timeoutInMSecs),
8182
ResolveContext::SRPTimerExpiredCallback, static_cast<void *>(ctx));
8283
}
@@ -280,6 +281,7 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i
280281
// we are done. Otherwise start the timer to give the resolve on SRP domain some extra time to complete.
281282
if (!sdCtx->shouldStartSRPTimerForResolve)
282283
{
284+
ChipLogDetail(Discovery, "No need to start SRP resolve timer for %s; completing resolve", sdCtx->instanceName.c_str());
283285
sdCtx->Finalize();
284286
}
285287
else
@@ -363,8 +365,8 @@ static CHIP_ERROR ResolveWithContext(ResolveContext * sdCtx, uint32_t interfaceI
363365
static CHIP_ERROR Resolve(ResolveContext * sdCtx, uint32_t interfaceId, chip::Inet::IPAddressType addressType, const char * type,
364366
const char * name, const char * domain)
365367
{
366-
ChipLogProgress(Discovery, "Resolve type=%s name=%s interface=%" PRIu32, StringOrNullMarker(type), StringOrNullMarker(name),
367-
interfaceId);
368+
ChipLogProgress(Discovery, "Resolve type=%s name=%s domain=%s interface=%" PRIu32, StringOrNullMarker(type),
369+
StringOrNullMarker(name), StringOrNullMarker(domain), interfaceId);
368370

369371
auto err = DNSServiceCreateConnection(&sdCtx->serviceRef);
370372
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));

0 commit comments

Comments
 (0)