Skip to content

Commit

Permalink
Fix the code that gets the domain name returned from browse and pass …
Browse files Browse the repository at this point in the history
…it into the resolve
  • Loading branch information
nivi-apple committed Mar 29, 2024
1 parent bc4dd91 commit 28de37b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
3 changes: 3 additions & 0 deletions src/platform/Darwin/DnssdContexts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ void RegisterContext::DispatchSuccess()
}

BrowseContext * BrowseContext::sContextDispatchingSuccess = nullptr;
std::vector<DnssdService> * BrowseContext::sDispatchedServices = nullptr;

BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol)
{
Expand Down Expand Up @@ -367,7 +368,9 @@ void BrowseContext::DispatchPartialSuccess()
{
dnsServices.push_back(std::move(iter.first));
}
sDispatchedServices = &dnsServices;
callback(context, dnsServices.data(), dnsServices.size(), false, CHIP_NO_ERROR);
sDispatchedServices = nullptr;
sContextDispatchingSuccess = nullptr;
services.clear();
}
Expand Down
60 changes: 27 additions & 33 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ std::shared_ptr<uint32_t> GetCounterHolder(const char * name)
return std::make_shared<uint32_t>(0);
}

bool ServicesAreEqual(DnssdService service, DnssdService other)
{
return strcmp(service.mName, other.mName) == 0 && GetFullType(&service) == GetFullType(&other) &&
service.mInterface == other.mInterface;
}

bool IsSRPType(const char * domain)
{
return strcmp(kSRPDot, domain) == 0;
Expand Down Expand Up @@ -233,23 +227,26 @@ static void OnBrowse(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interf
sdCtx->OnBrowse(flags, name, type, domain, interfaceId);
}

CHIP_ERROR BrowseOnDomain(BrowseHandler * sdCtx, uint32_t interfaceId, const char * type, const char * domain)
{
auto sdRef = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection

auto err = DNSServiceBrowse(&sdRef, kBrowseFlags, interfaceId, type, domain, OnBrowse, sdCtx);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
return CHIP_NO_ERROR;
}

CHIP_ERROR Browse(BrowseHandler * sdCtx, uint32_t interfaceId, const char * type)
{
auto err = DNSServiceCreateConnection(&sdCtx->serviceRef);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));

// We will browse on both the local domain and the SRP domain.
ChipLogProgress(Discovery, "Browsing for: %s on local domain", StringOrNullMarker(type));

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

ChipLogProgress(Discovery, "Browsing for: %s on %s domain", StringOrNullMarker(type), kSRPDot);

auto sdRefSRP = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
err = DNSServiceBrowse(&sdRefSRP, kBrowseFlags, interfaceId, type, kSRPDot, OnBrowse, sdCtx);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
ReturnErrorOnFailure(BrowseOnDomain(sdCtx, interfaceId, type, kSRPDot));

return MdnsContexts::GetInstance().Add(sdCtx, sdCtx->serviceRef);
}
Expand Down Expand Up @@ -377,6 +374,15 @@ static void OnResolve(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t inter
}
}

static CHIP_ERROR ResolveWithContext(ResolveContext * sdCtx, uint32_t interfaceId, const char * type, const char * name, const char * domain, ResolveContextWithType * contextWithType)
{
auto sdRef = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection

auto err = DNSServiceResolve(&sdRef, kResolveFlags, interfaceId, name, type, domain, OnResolve, contextWithType);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
return CHIP_NO_ERROR;
}

static CHIP_ERROR Resolve(ResolveContext * sdCtx, uint32_t interfaceId, chip::Inet::IPAddressType addressType, const char * type,
const char * name, const char * domain)
{
Expand All @@ -390,25 +396,14 @@ static CHIP_ERROR Resolve(ResolveContext * sdCtx, uint32_t interfaceId, chip::In
// Otherwise we will try to resolve using both the local domain and the SRP domain.
if (domain != nullptr)
{
auto sdRef = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection

ResolveContextWithType * contextWithType =
IsSRPType(domain) ? &sdCtx->resolveContextWithSRPType : &sdCtx->resolveContextWithNonSRPType;
err = DNSServiceResolve(&sdRef, kResolveFlags, interfaceId, name, type, domain, OnResolve, contextWithType);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
ReturnErrorOnFailure(ResolveWithContext(sdCtx, interfaceId, type, name, domain, IsSRPType(domain) ? &sdCtx->resolveContextWithSRPType : &sdCtx->resolveContextWithNonSRPType));
sdCtx->shoulStartSRPTimerForResolve = false;
}
else
{
auto sdRefLocal = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
err = DNSServiceResolve(&sdRefLocal, kResolveFlags, interfaceId, name, type, kLocalDot, OnResolve,
&sdCtx->resolveContextWithNonSRPType);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
ReturnErrorOnFailure(ResolveWithContext(sdCtx, interfaceId, type, name, kLocalDot, &sdCtx->resolveContextWithNonSRPType));

auto sdRefSRP = sdCtx->serviceRef; // Mandatory copy because of kDNSServiceFlagsShareConnection
err = DNSServiceResolve(&sdRefSRP, kResolveFlags, interfaceId, name, type, kSRPDot, OnResolve,
&sdCtx->resolveContextWithSRPType);
VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err));
ReturnErrorOnFailure(ResolveWithContext(sdCtx, interfaceId, type, name, kSRPDot, &sdCtx->resolveContextWithNonSRPType));
}

auto retval = MdnsContexts::GetInstance().Add(sdCtx, sdCtx->serviceRef);
Expand Down Expand Up @@ -579,11 +574,10 @@ CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId inte

if (BrowseContext::sContextDispatchingSuccess != nullptr)
{
for (auto iter : BrowseContext::sContextDispatchingSuccess->services)
{
if (ServicesAreEqual(*service, iter.first))
{
domain = iter.second.c_str();
for (size_t i = 0; i < BrowseContext::sDispatchedServices->size(); ++i) {
if (service == &BrowseContext::sDispatchedServices->at(i)) {
domain = BrowseContext::sContextDispatchingSuccess->services[i].second.c_str();
break;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/platform/Darwin/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ struct BrowseContext : public BrowseHandler
// TODO: Consider fixing the higher-level APIs to make it possible to pass
// in multiple IPs for a successful browse result.
static BrowseContext * sContextDispatchingSuccess;
static std::vector<DnssdService> * sDispatchedServices;
};

struct BrowseWithDelegateContext : public BrowseHandler
Expand Down

0 comments on commit 28de37b

Please sign in to comment.