Skip to content

Commit 35fb178

Browse files
authored
Revert "Fix the dnssd code that browses on both the local and srp domains" (#32691)
This reverts commit 88afa33.
1 parent 0f9542b commit 35fb178

File tree

3 files changed

+110
-136
lines changed

3 files changed

+110
-136
lines changed

src/platform/Darwin/DnssdContexts.cpp

+45-60
Original file line numberDiff line numberDiff line change
@@ -458,35 +458,27 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::
458458
std::shared_ptr<uint32_t> && consumerCounterToUse) :
459459
browseThatCausedResolve(browseCausingResolve)
460460
{
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;
461+
type = ContextType::Resolve;
462+
context = cbContext;
463+
callback = cb;
464+
protocol = GetProtocol(cbAddressType);
465+
instanceName = instanceNameToResolve;
466+
consumerCounter = std::move(consumerCounterToUse);
468467
}
469468

470469
ResolveContext::ResolveContext(CommissioningResolveDelegate * delegate, chip::Inet::IPAddressType cbAddressType,
471470
const char * instanceNameToResolve, std::shared_ptr<uint32_t> && consumerCounterToUse) :
472471
browseThatCausedResolve(nullptr)
473472
{
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;
473+
type = ContextType::Resolve;
474+
context = delegate;
475+
callback = nullptr;
476+
protocol = GetProtocol(cbAddressType);
477+
instanceName = instanceNameToResolve;
478+
consumerCounter = std::move(consumerCounterToUse);
481479
}
482480

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

491483
void ResolveContext::DispatchFailure(const char * errorStr, CHIP_ERROR err)
492484
{
@@ -534,7 +526,8 @@ void ResolveContext::DispatchSuccess()
534526

535527
for (auto interfaceIndex : priorityInterfaceIndices)
536528
{
537-
if (TryReportingResultsForInterfaceIndex(interfaceIndex))
529+
// Try finding interfaces for domains kLocalDot and kOpenThreadDot and delete them.
530+
if (TryReportingResultsForInterfaceIndex(static_cast<uint32_t>(interfaceIndex), std::string(kLocalDot)))
538531
{
539532
if (needDelete)
540533
{
@@ -543,7 +536,7 @@ void ResolveContext::DispatchSuccess()
543536
return;
544537
}
545538

546-
if (TryReportingResultsForInterfaceIndex(interfaceIndex))
539+
if (TryReportingResultsForInterfaceIndex(static_cast<uint32_t>(interfaceIndex), std::string(kOpenThreadDot)))
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.first, interface.first.second))
560552
{
561553
break;
562554
}
@@ -568,60 +560,52 @@ void ResolveContext::DispatchSuccess()
568560
}
569561
}
570562

571-
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex)
563+
bool ResolveContext::TryReportingResultsForInterfaceIndex(uint32_t interfaceIndex, std::string domainName)
572564
{
573565
if (interfaceIndex == 0)
574566
{
575567
// Not actually an interface we have.
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;
571+
std::pair<uint32_t, std::string> interfaceKey = std::make_pair(interfaceIndex, domainName);
572+
auto & interface = interfaces[interfaceKey];
573+
auto & ips = interface.addresses;
587574

588-
// Some interface may not have any ips, just ignore them.
589-
if (ips.size() == 0)
590-
{
591-
return false;
592-
}
575+
// Some interface may not have any ips, just ignore them.
576+
if (ips.size() == 0)
577+
{
578+
return false;
579+
}
593580

594-
ChipLogProgress(Discovery, "Mdns: Resolve success on interface %" PRIu32, interfaceIndex);
581+
ChipLogProgress(Discovery, "Mdns: Resolve success on interface %" PRIu32, interfaceIndex);
595582

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-
}
609-
610-
return true;
611-
}
583+
auto & service = interface.service;
584+
auto addresses = Span<Inet::IPAddress>(ips.data(), ips.size());
585+
if (nullptr == callback)
586+
{
587+
auto delegate = static_cast<CommissioningResolveDelegate *>(context);
588+
DiscoveredNodeData nodeData;
589+
service.ToDiscoveredNodeData(addresses, nodeData);
590+
delegate->OnNodeDiscovered(nodeData);
612591
}
613-
return false;
592+
else
593+
{
594+
callback(context, &service, addresses, CHIP_NO_ERROR);
595+
}
596+
597+
return true;
614598
}
615599

616-
CHIP_ERROR ResolveContext::OnNewAddress(const std::pair<uint32_t, std::string> & interfaceKey, const struct sockaddr * address)
600+
CHIP_ERROR ResolveContext::OnNewAddress(const std::pair<uint32_t, std::string> interfaceKey, const struct sockaddr * address)
617601
{
618602
// If we don't have any information about this interfaceId, just ignore the
619603
// address, since it won't be usable anyway without things like the port.
620604
// This can happen if "local" is set up as a search domain in the DNS setup
621605
// on the system, because the hostnames we are looking up all end in
622606
// ".local". In other words, we can get regular DNS results in here, not
623607
// just DNS-SD ones.
624-
auto interfaceId = interfaceKey.first;
608+
uint32_t interfaceId = interfaceKey.first;
625609

626610
if (interfaces.find(interfaceKey) == interfaces.end())
627611
{
@@ -736,7 +720,8 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname,
736720
}
737721

738722
std::pair<uint32_t, std::string> interfaceKey = std::make_pair(interfaceId, domainFromHostname);
739-
interfaces.insert(std::make_pair(std::move(interfaceKey), std::move(interface)));
723+
724+
interfaces.insert(std::make_pair(interfaceKey, std::move(interface)));
740725
}
741726

742727
bool ResolveContext::HasInterface()

src/platform/Darwin/DnssdImpl.cpp

+54-67
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ using namespace chip::Dnssd::Internal;
3232

3333
namespace {
3434

35-
constexpr char kLocalDot[] = "local.";
36-
37-
constexpr char kSrpDot[] = "default.service.arpa.";
38-
39-
// The extra time in milliseconds that we will wait for the resolution on the srp domain to complete.
40-
constexpr uint16_t kSrpTimeoutInMsec = 250;
35+
// The extra time in milliseconds that we will wait for the resolution on the open thread domain to complete.
36+
constexpr uint16_t kOpenThreadTimeoutInMsec = 250;
4137

4238
constexpr DNSServiceFlags kRegisterFlags = kDNSServiceFlagsNoAutoRename;
4339
constexpr DNSServiceFlags kBrowseFlags = kDNSServiceFlagsShareConnection;
@@ -148,57 +144,59 @@ std::string GetDomainFromHostName(const char * hostnameWithDomain)
148144
{
149145
std::string hostname = std::string(hostnameWithDomain);
150146

151-
// Find the first occurence of '.'
152-
size_t first_pos = hostname.find(".");
153-
154-
// if not found, return empty string
155-
VerifyOrReturnValue(first_pos != std::string::npos, std::string());
147+
// Find the last occurence of '.'
148+
size_t last_pos = hostname.find_last_of(".");
149+
if (last_pos != std::string::npos)
150+
{
151+
// Get a substring without last '.'
152+
std::string substring = hostname.substr(0, last_pos);
156153

157-
// Get a substring after the first occurence of '.' to the end of the string
158-
return hostname.substr(first_pos + 1, hostname.size());
154+
// Find the last occurence of '.' in the substring created above.
155+
size_t pos = substring.find_last_of(".");
156+
if (pos != std::string::npos)
157+
{
158+
// Return the domain name between the last 2 occurences of '.' including the trailing dot'.'.
159+
return std::string(hostname.substr(pos + 1, last_pos));
160+
}
161+
}
162+
return std::string();
159163
}
160164

165+
Global<MdnsContexts> MdnsContexts::sInstance;
166+
167+
namespace {
168+
161169
/**
162-
* @brief Callback that is called when the timeout for resolving on the kSrpDot domain has expired.
170+
* @brief Callback that is called when the timeout for resolving on the kOpenThreadDot domain has expired.
163171
*
164172
* @param[in] systemLayer The system layer.
165173
* @param[in] callbackContext The context passed to the timer callback.
166174
*/
167-
void SrpTimerExpiredCallback(System::Layer * systemLayer, void * callbackContext)
175+
void OpenThreadTimerExpiredCallback(System::Layer * systemLayer, void * callbackContext)
168176
{
169-
ChipLogProgress(Discovery, "Mdns: Timer expired for resolve to complete on the srp domain.");
177+
ChipLogProgress(Discovery, "Mdns: Timer expired for resolve to complete on the open thread domain.");
170178
auto sdCtx = static_cast<ResolveContext *>(callbackContext);
171179
VerifyOrDie(sdCtx != nullptr);
172-
sdCtx->Finalize();
173-
}
174180

175-
/**
176-
* @brief Starts a timer to wait for the resolution on the kSrpDot domain to happen.
177-
*
178-
* @param[in] timeoutSeconds The timeout in seconds.
179-
* @param[in] ResolveContext The resolve context.
180-
*/
181-
CHIP_ERROR StartSrpTimer(uint16_t timeoutInMSecs, ResolveContext * ctx)
182-
{
183-
VerifyOrReturnValue(ctx != nullptr, CHIP_ERROR_INCORRECT_STATE);
184-
return DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds16(timeoutInMSecs), SrpTimerExpiredCallback,
185-
reinterpret_cast<void *>(ctx));
181+
if (sdCtx->hasOpenThreadTimerStarted)
182+
{
183+
sdCtx->Finalize();
184+
}
186185
}
187186

188187
/**
189-
* @brief Cancels the timer that was started to wait for the resolution on the kSrpDot domain to happen.
188+
* @brief Starts a timer to wait for the resolution on the kOpenThreadDot domain to happen.
190189
*
190+
* @param[in] timeoutSeconds The timeout in seconds.
191191
* @param[in] ResolveContext The resolve context.
192192
*/
193-
void CancelSrpTimer(ResolveContext * ctx)
193+
void StartOpenThreadTimer(uint16_t timeoutInMSecs, ResolveContext * ctx)
194194
{
195-
DeviceLayer::SystemLayer().CancelTimer(SrpTimerExpiredCallback, reinterpret_cast<void *>(ctx));
195+
VerifyOrReturn(ctx != nullptr, ChipLogError(Discovery, "Can't schedule open thread timer since context is null"));
196+
DeviceLayer::SystemLayer().StartTimer(System::Clock::Milliseconds16(timeoutInMSecs), OpenThreadTimerExpiredCallback,
197+
reinterpret_cast<void *>(ctx));
196198
}
197199

198-
Global<MdnsContexts> MdnsContexts::sInstance;
199-
200-
namespace {
201-
202200
static void OnRegister(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType err, const char * name, const char * type,
203201
const char * domain, void * context)
204202
{
@@ -250,17 +248,17 @@ CHIP_ERROR Browse(BrowseHandler * sdCtx, uint32_t interfaceId, const char * type
250248
auto err = DNSServiceCreateConnection(&sdCtx->serviceRef);
251249
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
252250

253-
// We will browse on both the local domain and the srp domain.
251+
// We will browse on both the local domain and the open thread domain.
254252
ChipLogProgress(Discovery, "Browsing for: %s on domain %s", StringOrNullMarker(type), kLocalDot);
255253

256254
auto sdRefLocal = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
257255
err = DNSServiceBrowse(&sdRefLocal, kBrowseFlags, interfaceId, type, kLocalDot, OnBrowse, sdCtx);
258256
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
259257

260-
ChipLogProgress(Discovery, "Browsing for: %s on domain %s", StringOrNullMarker(type), kSrpDot);
258+
ChipLogProgress(Discovery, "Browsing for: %s on domain %s", StringOrNullMarker(type), kOpenThreadDot);
261259

262-
auto sdRefSrp = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
263-
err = DNSServiceBrowse(&sdRefSrp, kBrowseFlags, interfaceId, type, kSrpDot, OnBrowse, sdCtx);
260+
DNSServiceRef sdRefOpenThread = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
261+
err = DNSServiceBrowse(&sdRefOpenThread, kBrowseFlags, interfaceId, type, kOpenThreadDot, OnBrowse, sdCtx);
264262
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
265263

266264
return MdnsContexts::GetInstance().Add(sdCtx, sdCtx->serviceRef);
@@ -309,37 +307,25 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i
309307
{
310308
VerifyOrReturn(sdCtx->HasAddress(), sdCtx->Finalize(kDNSServiceErr_BadState));
311309

312-
if (domainName.compare(kSrpDot) == 0)
310+
if (domainName.compare(kOpenThreadDot) == 0)
313311
{
314-
ChipLogProgress(Discovery, "Mdns: Resolve completed on the srp domain.");
315-
316-
// Cancel the timer if one has been started
317-
if (sdCtx->hasSrpTimerStarted)
318-
{
319-
CancelSrpTimer(sdCtx);
320-
}
312+
ChipLogProgress(Discovery, "Mdns: Resolve completed on the open thread domain.");
321313
sdCtx->Finalize();
322314
}
323315
else if (domainName.compare(kLocalDot) == 0)
324316
{
325-
ChipLogProgress(Discovery,
326-
"Mdns: Resolve completed on the local domain. Starting a timer for the srp resolve to come back");
317+
ChipLogProgress(
318+
Discovery,
319+
"Mdns: Resolve completed on the local domain. Starting a timer for the open thread resolve to come back");
327320

328-
// Usually the resolution on the local domain is quicker than on the srp domain. We would like to give the
329-
// resolution on the srp domain around 250 millisecs more to give it a chance to resolve before finalizing
321+
// Usually the resolution on the local domain is quicker than on the open thread domain. We would like to give the
322+
// resolution on the open thread domain around 250 millisecs more to give it a chance to resolve before finalizing
330323
// the resolution.
331-
if (!sdCtx->hasSrpTimerStarted)
324+
if (!sdCtx->hasOpenThreadTimerStarted)
332325
{
333-
// Schedule a timer to allow the resolve on Srp domain to complete.
334-
CHIP_ERROR error = StartSrpTimer(kSrpTimeoutInMsec, sdCtx);
335-
336-
// If the timer fails to start, finalize the context and return.
337-
if (error != CHIP_NO_ERROR)
338-
{
339-
sdCtx->Finalize();
340-
return;
341-
}
342-
sdCtx->hasSrpTimerStarted = true;
326+
// Schedule a timer to allow the resolve on OpenThread domain to complete.
327+
StartOpenThreadTimer(kOpenThreadTimeoutInMsec, sdCtx);
328+
sdCtx->hasOpenThreadTimerStarted = true;
343329
}
344330
}
345331
}
@@ -381,7 +367,8 @@ static void OnResolve(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t inter
381367
if (!sdCtx->isResolveRequested)
382368
{
383369
GetAddrInfo(sdCtx);
384-
sdCtx->isResolveRequested = true;
370+
sdCtx->isResolveRequested = true;
371+
sdCtx->hasOpenThreadTimerStarted = false;
385372
}
386373
}
387374
}
@@ -395,13 +382,13 @@ static CHIP_ERROR Resolve(ResolveContext * sdCtx, uint32_t interfaceId, chip::In
395382
auto err = DNSServiceCreateConnection(&sdCtx->serviceRef);
396383
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
397384

398-
// Similar to browse, will try to resolve using both the local domain and the srp domain.
385+
// Similar to browse, will try to resolve using both the local domain and the open thread domain.
399386
auto sdRefLocal = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
400387
err = DNSServiceResolve(&sdRefLocal, kResolveFlags, interfaceId, name, type, kLocalDot, OnResolve, sdCtx);
401388
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
402389

403-
auto sdRefSrp = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
404-
err = DNSServiceResolve(&sdRefSrp, kResolveFlags, interfaceId, name, type, kSrpDot, OnResolve, sdCtx);
390+
auto sdRefOpenThread = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
391+
err = DNSServiceResolve(&sdRefOpenThread, kResolveFlags, interfaceId, name, type, kOpenThreadDot, OnResolve, sdCtx);
405392
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
406393

407394
auto retval = MdnsContexts::GetInstance().Add(sdCtx, sdCtx->serviceRef);

0 commit comments

Comments
 (0)