Skip to content

Commit 5db0414

Browse files
Merge branch 'master' into bugfix/rs911x_ble_tx_timer_issue
2 parents 0a2b2f7 + db74b20 commit 5db0414

File tree

3 files changed

+74
-231
lines changed

3 files changed

+74
-231
lines changed

src/platform/Darwin/DnssdContexts.cpp

+52-79
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ 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+
}
3339

3440
std::string GetHostNameWithoutDomain(const char * hostnameWithDomain)
3541
{
@@ -246,7 +252,6 @@ void MdnsContexts::Delete(GenericContext * context)
246252
{
247253
DNSServiceRefDeallocate(context->serviceRef);
248254
}
249-
250255
chip::Platform::Delete(context);
251256
}
252257

@@ -383,6 +388,7 @@ void BrowseContext::OnBrowseAdd(const char * name, const char * type, const char
383388
ChipLogProgress(Discovery, "Mdns: %s name: %s, type: %s, domain: %s, interface: %" PRIu32, __func__, StringOrNullMarker(name),
384389
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
385390

391+
VerifyOrReturn(IsLocalDomain(domain));
386392
auto service = GetService(name, type, protocol, interfaceId);
387393
services.push_back(service);
388394
}
@@ -393,6 +399,7 @@ void BrowseContext::OnBrowseRemove(const char * name, const char * type, const c
393399
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
394400

395401
VerifyOrReturn(name != nullptr);
402+
VerifyOrReturn(IsLocalDomain(domain));
396403

397404
services.erase(std::remove_if(services.begin(), services.end(),
398405
[name, type, interfaceId](const DnssdService & service) {
@@ -436,6 +443,8 @@ void BrowseWithDelegateContext::OnBrowseAdd(const char * name, const char * type
436443
ChipLogProgress(Discovery, "Mdns: %s name: %s, type: %s, domain: %s, interface: %" PRIu32, __func__, StringOrNullMarker(name),
437444
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
438445

446+
VerifyOrReturn(IsLocalDomain(domain));
447+
439448
auto delegate = static_cast<DnssdBrowseDelegate *>(context);
440449
auto service = GetService(name, type, protocol, interfaceId);
441450
delegate->OnBrowseAdd(service);
@@ -447,6 +456,7 @@ void BrowseWithDelegateContext::OnBrowseRemove(const char * name, const char * t
447456
StringOrNullMarker(type), StringOrNullMarker(domain), interfaceId);
448457

449458
VerifyOrReturn(name != nullptr);
459+
VerifyOrReturn(IsLocalDomain(domain));
450460

451461
auto delegate = static_cast<DnssdBrowseDelegate *>(context);
452462
auto service = GetService(name, type, protocol, interfaceId);
@@ -458,35 +468,27 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::
458468
std::shared_ptr<uint32_t> && consumerCounterToUse) :
459469
browseThatCausedResolve(browseCausingResolve)
460470
{
461-
type = ContextType::Resolve;
462-
context = cbContext;
463-
callback = cb;
464-
protocol = GetProtocol(cbAddressType);
465-
instanceName = instanceNameToResolve;
466-
consumerCounter = std::move(consumerCounterToUse);
467-
hasSrpTimerStarted = false;
471+
type = ContextType::Resolve;
472+
context = cbContext;
473+
callback = cb;
474+
protocol = GetProtocol(cbAddressType);
475+
instanceName = instanceNameToResolve;
476+
consumerCounter = std::move(consumerCounterToUse);
468477
}
469478

470479
ResolveContext::ResolveContext(CommissioningResolveDelegate * delegate, chip::Inet::IPAddressType cbAddressType,
471480
const char * instanceNameToResolve, std::shared_ptr<uint32_t> && consumerCounterToUse) :
472481
browseThatCausedResolve(nullptr)
473482
{
474-
type = ContextType::Resolve;
475-
context = delegate;
476-
callback = nullptr;
477-
protocol = GetProtocol(cbAddressType);
478-
instanceName = instanceNameToResolve;
479-
consumerCounter = std::move(consumerCounterToUse);
480-
hasSrpTimerStarted = false;
483+
type = ContextType::Resolve;
484+
context = delegate;
485+
callback = nullptr;
486+
protocol = GetProtocol(cbAddressType);
487+
instanceName = instanceNameToResolve;
488+
consumerCounter = std::move(consumerCounterToUse);
481489
}
482490

483-
ResolveContext::~ResolveContext()
484-
{
485-
if (this->hasSrpTimerStarted)
486-
{
487-
CancelSrpTimer(this);
488-
}
489-
}
491+
ResolveContext::~ResolveContext() {}
490492

491493
void ResolveContext::DispatchFailure(const char * errorStr, CHIP_ERROR err)
492494
{
@@ -534,16 +536,7 @@ void ResolveContext::DispatchSuccess()
534536

535537
for (auto interfaceIndex : priorityInterfaceIndices)
536538
{
537-
if (TryReportingResultsForInterfaceIndex(interfaceIndex))
538-
{
539-
if (needDelete)
540-
{
541-
MdnsContexts::GetInstance().Delete(this);
542-
}
543-
return;
544-
}
545-
546-
if (TryReportingResultsForInterfaceIndex(interfaceIndex))
539+
if (TryReportingResultsForInterfaceIndex(static_cast<uint32_t>(interfaceIndex)))
547540
{
548541
if (needDelete)
549542
{
@@ -555,8 +548,7 @@ void ResolveContext::DispatchSuccess()
555548

556549
for (auto & interface : interfaces)
557550
{
558-
auto interfaceId = interface.first.first;
559-
if (TryReportingResultsForInterfaceIndex(interfaceId))
551+
if (TryReportingResultsForInterfaceIndex(interface.first))
560552
{
561553
break;
562554
}
@@ -576,54 +568,43 @@ bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceInde
576568
return false;
577569
}
578570

579-
std::map<std::pair<uint32_t, std::string>, InterfaceInfo>::iterator iter = interfaces.begin();
580-
while (iter != interfaces.end())
581-
{
582-
std::pair<uint32_t, std::string> key = iter->first;
583-
if (key.first == interfaceIndex)
584-
{
585-
auto & interface = interfaces[key];
586-
auto & ips = interface.addresses;
587-
588-
// Some interface may not have any ips, just ignore them.
589-
if (ips.size() == 0)
590-
{
591-
return false;
592-
}
571+
auto & interface = interfaces[interfaceIndex];
572+
auto & ips = interface.addresses;
593573

594-
ChipLogProgress(Discovery, "Mdns: Resolve success on interface %" PRIu32, interfaceIndex);
574+
// Some interface may not have any ips, just ignore them.
575+
if (ips.size() == 0)
576+
{
577+
return false;
578+
}
595579

596-
auto & service = interface.service;
597-
auto addresses = Span<Inet::IPAddress>(ips.data(), ips.size());
598-
if (nullptr == callback)
599-
{
600-
auto delegate = static_cast<CommissioningResolveDelegate *>(context);
601-
DiscoveredNodeData nodeData;
602-
service.ToDiscoveredNodeData(addresses, nodeData);
603-
delegate->OnNodeDiscovered(nodeData);
604-
}
605-
else
606-
{
607-
callback(context, &service, addresses, CHIP_NO_ERROR);
608-
}
580+
ChipLogProgress(Discovery, "Mdns: Resolve success on interface %" PRIu32, interfaceIndex);
609581

610-
return true;
611-
}
582+
auto & service = interface.service;
583+
auto addresses = Span<Inet::IPAddress>(ips.data(), ips.size());
584+
if (nullptr == callback)
585+
{
586+
auto delegate = static_cast<CommissioningResolveDelegate *>(context);
587+
DiscoveredNodeData nodeData;
588+
service.ToDiscoveredNodeData(addresses, nodeData);
589+
delegate->OnNodeDiscovered(nodeData);
612590
}
613-
return false;
591+
else
592+
{
593+
callback(context, &service, addresses, CHIP_NO_ERROR);
594+
}
595+
596+
return true;
614597
}
615598

616-
CHIP_ERROR ResolveContext::OnNewAddress(const std::pair<uint32_t, std::string> & interfaceKey, const struct sockaddr * address)
599+
CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address)
617600
{
618601
// If we don't have any information about this interfaceId, just ignore the
619602
// address, since it won't be usable anyway without things like the port.
620603
// This can happen if "local" is set up as a search domain in the DNS setup
621604
// on the system, because the hostnames we are looking up all end in
622605
// ".local". In other words, we can get regular DNS results in here, not
623606
// just DNS-SD ones.
624-
auto interfaceId = interfaceKey.first;
625-
626-
if (interfaces.find(interfaceKey) == interfaces.end())
607+
if (interfaces.find(interfaceId) == interfaces.end())
627608
{
628609
return CHIP_NO_ERROR;
629610
}
@@ -646,7 +627,7 @@ CHIP_ERROR ResolveContext::OnNewAddress(const std::pair<uint32_t, std::string> &
646627
return CHIP_NO_ERROR;
647628
}
648629

649-
interfaces[interfaceKey].addresses.push_back(ip);
630+
interfaces[interfaceId].addresses.push_back(ip);
650631

651632
return CHIP_NO_ERROR;
652633
}
@@ -728,15 +709,7 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname,
728709
// resolving.
729710
interface.fullyQualifiedDomainName = hostnameWithDomain;
730711

731-
std::string domainFromHostname = GetDomainFromHostName(hostnameWithDomain);
732-
if (domainFromHostname.empty())
733-
{
734-
ChipLogError(Discovery, "Mdns: No domain set in hostname %s", hostnameWithDomain);
735-
return;
736-
}
737-
738-
std::pair<uint32_t, std::string> interfaceKey = std::make_pair(interfaceId, domainFromHostname);
739-
interfaces.insert(std::make_pair(std::move(interfaceKey), std::move(interface)));
712+
interfaces.insert(std::make_pair(interfaceId, std::move(interface)));
740713
}
741714

742715
bool ResolveContext::HasInterface()

0 commit comments

Comments
 (0)