Skip to content

Commit 401fbf0

Browse files
nivi-applerestyled-commitsbzbarsky-apple
authored
Add support to browse and resolve on both local and SRP domain (project-chip#32779)
* Add domain names matching the DnssdServices stored in Browse Context This is needed to pass the domain returned from a call to Browse to the Resolve. * Restyled by clang-format * Add the domain names to the services vector * Restyled by clang-format * Add support to browse and resolve on both local and SRP domain * Restyled by clang-format * Fix the code that gets the domain name returned from browse and pass it into the resolve * Restyled by clang-format * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Address review comments * Updated the comment about shouldStartSRPTimerForResolve * Restyled by clang-format * Apply suggestions from code review Co-authored-by: Boris Zbarsky <bzbarsky@apple.com> * Addressed more review comments * Restyled by clang-format * Add a null check for domain when erasing the contents of services * Fix the check for domain name when erasing services and pass the correct ResolveContextWithType for the case where domain is not null during resolve * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Boris Zbarsky <bzbarsky@apple.com>
1 parent e39cef5 commit 401fbf0

File tree

3 files changed

+257
-52
lines changed

3 files changed

+257
-52
lines changed

src/platform/Darwin/DnssdContexts.cpp

+58-25
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@ namespace {
3030

3131
constexpr uint8_t kDnssdKeyMaxSize = 32;
3232
constexpr uint8_t kDnssdTxtRecordMaxEntries = 20;
33-
constexpr char kLocalDot[] = "local.";
34-
35-
bool IsLocalDomain(const char * domain)
36-
{
37-
return strcmp(kLocalDot, domain) == 0;
38-
}
3933

4034
std::string GetHostNameWithoutDomain(const char * hostnameWithDomain)
4135
{
@@ -341,7 +335,8 @@ void RegisterContext::DispatchSuccess()
341335
mHostNameRegistrar.Register();
342336
}
343337

344-
BrowseContext * BrowseContext::sContextDispatchingSuccess = nullptr;
338+
BrowseContext * BrowseContext::sContextDispatchingSuccess = nullptr;
339+
std::vector<DnssdService> * BrowseContext::sDispatchedServices = nullptr;
345340

346341
BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol)
347342
{
@@ -373,7 +368,10 @@ void BrowseContext::DispatchPartialSuccess()
373368
{
374369
dnsServices.push_back(std::move(iter.first));
375370
}
371+
372+
sDispatchedServices = &dnsServices;
376373
callback(context, dnsServices.data(), dnsServices.size(), false, CHIP_NO_ERROR);
374+
sDispatchedServices = nullptr;
377375
sContextDispatchingSuccess = nullptr;
378376
services.clear();
379377
}
@@ -393,7 +391,6 @@ void BrowseContext::OnBrowseAdd(const char * name, const char * type, const char
393391
ChipLogProgress(Discovery, "Mdns: %s name: %s, type: %s, domain: %s, interface: %" PRIu32, __func__, StringOrNullMarker(name),
394392
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
395393

396-
VerifyOrReturn(IsLocalDomain(domain));
397394
auto service = GetService(name, type, protocol, interfaceId);
398395
services.push_back(std::make_pair(std::move(service), std::string(domain)));
399396
}
@@ -404,13 +401,13 @@ void BrowseContext::OnBrowseRemove(const char * name, const char * type, const c
404401
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
405402

406403
VerifyOrReturn(name != nullptr);
407-
VerifyOrReturn(IsLocalDomain(domain));
404+
std::string domain_str(domain);
408405

409406
services.erase(std::remove_if(services.begin(), services.end(),
410-
[name, type, interfaceId, domain](const auto & service) {
407+
[name, type, interfaceId, &domain_str](const auto & service) {
411408
return strcmp(name, service.first.mName) == 0 && type == GetFullType(&service.first) &&
412409
service.first.mInterface == chip::Inet::InterfaceId(interfaceId) &&
413-
strcmp(domain, service.second.c_str()) == 0;
410+
service.second == domain_str;
414411
}),
415412
services.end());
416413
}
@@ -449,8 +446,6 @@ void BrowseWithDelegateContext::OnBrowseAdd(const char * name, const char * type
449446
ChipLogProgress(Discovery, "Mdns: %s name: %s, type: %s, domain: %s, interface: %" PRIu32, __func__, StringOrNullMarker(name),
450447
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
451448

452-
VerifyOrReturn(IsLocalDomain(domain));
453-
454449
auto delegate = static_cast<DnssdBrowseDelegate *>(context);
455450
auto service = GetService(name, type, protocol, interfaceId);
456451
delegate->OnBrowseAdd(service);
@@ -462,7 +457,6 @@ void BrowseWithDelegateContext::OnBrowseRemove(const char * name, const char * t
462457
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
463458

464459
VerifyOrReturn(name != nullptr);
465-
VerifyOrReturn(IsLocalDomain(domain));
466460

467461
auto delegate = static_cast<DnssdBrowseDelegate *>(context);
468462
auto service = GetService(name, type, protocol, interfaceId);
@@ -494,7 +488,13 @@ ResolveContext::ResolveContext(CommissioningResolveDelegate * delegate, chip::In
494488
consumerCounter = std::move(consumerCounterToUse);
495489
}
496490

497-
ResolveContext::~ResolveContext() {}
491+
ResolveContext::~ResolveContext()
492+
{
493+
if (isSRPTimerRunning)
494+
{
495+
CancelSRPTimer();
496+
}
497+
}
498498

499499
void ResolveContext::DispatchFailure(const char * errorStr, CHIP_ERROR err)
500500
{
@@ -554,7 +554,8 @@ void ResolveContext::DispatchSuccess()
554554

555555
for (auto & interface : interfaces)
556556
{
557-
if (TryReportingResultsForInterfaceIndex(interface.first))
557+
if (TryReportingResultsForInterfaceIndex(interface.first.interfaceId, interface.first.hostname,
558+
interface.first.isSRPResult))
558559
{
559560
break;
560561
}
@@ -566,16 +567,17 @@ void ResolveContext::DispatchSuccess()
566567
}
567568
}
568569

569-
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex)
570+
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex, const std::string & hostname, bool isSRPResult)
570571
{
571572
if (interfaceIndex == 0)
572573
{
573574
// Not actually an interface we have.
574575
return false;
575576
}
576577

577-
auto & interface = interfaces[interfaceIndex];
578-
auto & ips = interface.addresses;
578+
InterfaceKey interfaceKey = { interfaceIndex, hostname, isSRPResult };
579+
auto & interface = interfaces[interfaceKey];
580+
auto & ips = interface.addresses;
579581

580582
// Some interface may not have any ips, just ignore them.
581583
if (ips.size() == 0)
@@ -602,15 +604,45 @@ bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceInde
602604
return true;
603605
}
604606

605-
CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address)
607+
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex)
608+
{
609+
for (auto & interface : interfaces)
610+
{
611+
if (interface.first.interfaceId == interfaceIndex)
612+
{
613+
if (TryReportingResultsForInterfaceIndex(interface.first.interfaceId, interface.first.hostname,
614+
interface.first.isSRPResult))
615+
{
616+
return true;
617+
}
618+
}
619+
}
620+
return false;
621+
}
622+
623+
void ResolveContext::SRPTimerExpiredCallback(chip::System::Layer * systemLayer, void * callbackContext)
624+
{
625+
auto sdCtx = static_cast<ResolveContext *>(callbackContext);
626+
VerifyOrDie(sdCtx != nullptr);
627+
sdCtx->Finalize();
628+
}
629+
630+
void ResolveContext::CancelSRPTimer()
631+
{
632+
DeviceLayer::SystemLayer().CancelTimer(SRPTimerExpiredCallback, static_cast<void *>(this));
633+
}
634+
635+
CHIP_ERROR ResolveContext::OnNewAddress(const InterfaceKey & interfaceKey, const struct sockaddr * address)
606636
{
607637
// If we don't have any information about this interfaceId, just ignore the
608638
// address, since it won't be usable anyway without things like the port.
609639
// This can happen if "local" is set up as a search domain in the DNS setup
610640
// on the system, because the hostnames we are looking up all end in
611641
// ".local". In other words, we can get regular DNS results in here, not
612642
// just DNS-SD ones.
613-
if (interfaces.find(interfaceId) == interfaces.end())
643+
auto interfaceId = interfaceKey.interfaceId;
644+
645+
if (interfaces.find(interfaceKey) == interfaces.end())
614646
{
615647
return CHIP_NO_ERROR;
616648
}
@@ -633,7 +665,7 @@ CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct socka
633665
return CHIP_NO_ERROR;
634666
}
635667

636-
interfaces[interfaceId].addresses.push_back(ip);
668+
interfaces[interfaceKey].addresses.push_back(ip);
637669

638670
return CHIP_NO_ERROR;
639671
}
@@ -652,7 +684,7 @@ bool ResolveContext::HasAddress()
652684
}
653685

654686
void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, const char * hostnameWithDomain, uint16_t port,
655-
uint16_t txtLen, const unsigned char * txtRecord)
687+
uint16_t txtLen, const unsigned char * txtRecord, bool isFromSRPResolve)
656688
{
657689
#if CHIP_PROGRESS_LOGGING
658690
std::string txtString;
@@ -715,7 +747,8 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname,
715747
// resolving.
716748
interface.fullyQualifiedDomainName = hostnameWithDomain;
717749

718-
interfaces.insert(std::make_pair(interfaceId, std::move(interface)));
750+
InterfaceKey interfaceKey = { interfaceId, hostnameWithDomain, isFromSRPResolve };
751+
interfaces.insert(std::make_pair(std::move(interfaceKey), std::move(interface)));
719752
}
720753

721754
bool ResolveContext::HasInterface()
@@ -731,7 +764,7 @@ InterfaceInfo::InterfaceInfo()
731764

732765
InterfaceInfo::InterfaceInfo(InterfaceInfo && other) :
733766
service(std::move(other.service)), addresses(std::move(other.addresses)),
734-
fullyQualifiedDomainName(std::move(other.fullyQualifiedDomainName))
767+
fullyQualifiedDomainName(std::move(other.fullyQualifiedDomainName)), isDNSLookUpRequested(other.isDNSLookUpRequested)
735768
{
736769
// Make sure we're not trying to free any state from the other DnssdService,
737770
// since we took over ownership of its allocated bits.

0 commit comments

Comments
 (0)