@@ -30,6 +30,12 @@ namespace {
30
30
31
31
constexpr uint8_t kDnssdKeyMaxSize = 32 ;
32
32
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
+ }
33
39
34
40
std::string GetHostNameWithoutDomain (const char * hostnameWithDomain)
35
41
{
@@ -246,7 +252,6 @@ void MdnsContexts::Delete(GenericContext * context)
246
252
{
247
253
DNSServiceRefDeallocate (context->serviceRef );
248
254
}
249
-
250
255
chip::Platform::Delete (context);
251
256
}
252
257
@@ -383,6 +388,7 @@ void BrowseContext::OnBrowseAdd(const char * name, const char * type, const char
383
388
ChipLogProgress (Discovery, " Mdns: %s name: %s, type: %s, domain: %s, interface: %" PRIu32, __func__, StringOrNullMarker (name),
384
389
StringOrNullMarker (type), StringOrNullMarker (domain), interfaceId);
385
390
391
+ VerifyOrReturn (IsLocalDomain (domain));
386
392
auto service = GetService (name, type, protocol, interfaceId);
387
393
services.push_back (service);
388
394
}
@@ -393,6 +399,7 @@ void BrowseContext::OnBrowseRemove(const char * name, const char * type, const c
393
399
StringOrNullMarker (type), StringOrNullMarker (domain), interfaceId);
394
400
395
401
VerifyOrReturn (name != nullptr );
402
+ VerifyOrReturn (IsLocalDomain (domain));
396
403
397
404
services.erase (std::remove_if (services.begin (), services.end (),
398
405
[name, type, interfaceId](const DnssdService & service) {
@@ -436,6 +443,8 @@ void BrowseWithDelegateContext::OnBrowseAdd(const char * name, const char * type
436
443
ChipLogProgress (Discovery, " Mdns: %s name: %s, type: %s, domain: %s, interface: %" PRIu32, __func__, StringOrNullMarker (name),
437
444
StringOrNullMarker (type), StringOrNullMarker (domain), interfaceId);
438
445
446
+ VerifyOrReturn (IsLocalDomain (domain));
447
+
439
448
auto delegate = static_cast <DnssdBrowseDelegate *>(context);
440
449
auto service = GetService (name, type, protocol, interfaceId);
441
450
delegate->OnBrowseAdd (service);
@@ -447,6 +456,7 @@ void BrowseWithDelegateContext::OnBrowseRemove(const char * name, const char * t
447
456
StringOrNullMarker (type), StringOrNullMarker (domain), interfaceId);
448
457
449
458
VerifyOrReturn (name != nullptr );
459
+ VerifyOrReturn (IsLocalDomain (domain));
450
460
451
461
auto delegate = static_cast <DnssdBrowseDelegate *>(context);
452
462
auto service = GetService (name, type, protocol, interfaceId);
@@ -458,35 +468,27 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::
458
468
std::shared_ptr<uint32_t > && consumerCounterToUse) :
459
469
browseThatCausedResolve (browseCausingResolve)
460
470
{
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);
468
477
}
469
478
470
479
ResolveContext::ResolveContext (CommissioningResolveDelegate * delegate, chip::Inet::IPAddressType cbAddressType,
471
480
const char * instanceNameToResolve, std::shared_ptr<uint32_t > && consumerCounterToUse) :
472
481
browseThatCausedResolve (nullptr )
473
482
{
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);
481
489
}
482
490
483
- ResolveContext::~ResolveContext ()
484
- {
485
- if (this ->hasSrpTimerStarted )
486
- {
487
- CancelSrpTimer (this );
488
- }
489
- }
491
+ ResolveContext::~ResolveContext () {}
490
492
491
493
void ResolveContext::DispatchFailure (const char * errorStr, CHIP_ERROR err)
492
494
{
@@ -534,16 +536,7 @@ void ResolveContext::DispatchSuccess()
534
536
535
537
for (auto interfaceIndex : priorityInterfaceIndices)
536
538
{
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)))
547
540
{
548
541
if (needDelete)
549
542
{
@@ -555,8 +548,7 @@ void ResolveContext::DispatchSuccess()
555
548
556
549
for (auto & interface : interfaces)
557
550
{
558
- auto interfaceId = interface.first .first ;
559
- if (TryReportingResultsForInterfaceIndex (interfaceId))
551
+ if (TryReportingResultsForInterfaceIndex (interface.first ))
560
552
{
561
553
break ;
562
554
}
@@ -576,54 +568,43 @@ bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceInde
576
568
return false ;
577
569
}
578
570
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 ;
593
573
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
+ }
595
579
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);
609
581
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);
612
590
}
613
- return false ;
591
+ else
592
+ {
593
+ callback (context, &service, addresses, CHIP_NO_ERROR);
594
+ }
595
+
596
+ return true ;
614
597
}
615
598
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)
617
600
{
618
601
// If we don't have any information about this interfaceId, just ignore the
619
602
// address, since it won't be usable anyway without things like the port.
620
603
// This can happen if "local" is set up as a search domain in the DNS setup
621
604
// on the system, because the hostnames we are looking up all end in
622
605
// ".local". In other words, we can get regular DNS results in here, not
623
606
// just DNS-SD ones.
624
- auto interfaceId = interfaceKey.first ;
625
-
626
- if (interfaces.find (interfaceKey) == interfaces.end ())
607
+ if (interfaces.find (interfaceId) == interfaces.end ())
627
608
{
628
609
return CHIP_NO_ERROR;
629
610
}
@@ -646,7 +627,7 @@ CHIP_ERROR ResolveContext::OnNewAddress(const std::pair<uint32_t, std::string> &
646
627
return CHIP_NO_ERROR;
647
628
}
648
629
649
- interfaces[interfaceKey ].addresses .push_back (ip);
630
+ interfaces[interfaceId ].addresses .push_back (ip);
650
631
651
632
return CHIP_NO_ERROR;
652
633
}
@@ -728,15 +709,7 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname,
728
709
// resolving.
729
710
interface.fullyQualifiedDomainName = hostnameWithDomain;
730
711
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)));
740
713
}
741
714
742
715
bool ResolveContext::HasInterface ()
0 commit comments