Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Darwin mdns fix #11992

Merged
merged 13 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ void MdnsContexts::Delete(GenericContext * context)
}
}

DNSServiceRefDeallocate(context->serviceRef);
if (context->serviceRef != nullptr)
{
DNSServiceRefDeallocate(context->serviceRef);
}
chip::Platform::Delete(context);
}

Expand Down Expand Up @@ -293,7 +296,7 @@ CHIP_ERROR Register(uint32_t interfaceId, const char * type, const char * name,

sdCtx = chip::Platform::New<RegisterContext>(type, nullptr);
err = DNSServiceRegister(&sdRef, 0 /* flags */, interfaceId, name, type, kLocalDot, NULL, ntohs(port), recordLen,
recordBytesPtr, OnRegister, sdCtx);
recordBytesPtr, OnRegister, sdCtx);
TXTRecordDeallocate(recordRef);

VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err), CHIP_ERROR_INTERNAL);
Expand Down Expand Up @@ -460,13 +463,35 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin
protocol = kDNSServiceProtocol_IPv6;
#endif

err = DNSServiceGetAddrInfo(&sdRef, 0 /* flags */, interfaceId, protocol, hostname, OnGetAddrInfo, sdCtx);
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);
if (interfaceId != static_cast<uint32_t>(-1))
cecille marked this conversation as resolved.
Show resolved Hide resolved
{
// -1 is the local only interface. If we're not on that interface, we need to get the address for the given hostname.
err = DNSServiceGetAddrInfo(&sdRef, 0 /* flags */, interfaceId, protocol, hostname, OnGetAddrInfo, sdCtx);
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);

err = DNSServiceSetDispatchQueue(sdRef, chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue());
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);
err = DNSServiceSetDispatchQueue(sdRef, chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue());
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);

return MdnsContexts::GetInstance().Add(sdCtx, sdRef);
return MdnsContexts::GetInstance().Add(sdCtx, sdRef);
}
else
{
sockaddr_in6 sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin6_len = sizeof(sockaddr);
sockaddr.sin6_family = AF_INET6;
sockaddr.sin6_addr = in6addr_loopback;
sockaddr.sin6_port = htons((unsigned short) port);
uint32_t ttl = 120; // default TTL for records with hostnames is 120 seconds
uint32_t interface = 0; // Set interface to ANY (0) - network stack can decide how to route this.
OnGetAddrInfo(nullptr, 0 /* flags */, interface, kDNSServiceErr_NoError, hostname,
reinterpret_cast<struct sockaddr *>(&sockaddr), ttl, sdCtx);

// Don't leak memory.
cecille marked this conversation as resolved.
Show resolved Hide resolved
sdCtx->serviceRef = nullptr;
MdnsContexts::GetInstance().Delete(sdCtx);
return CHIP_NO_ERROR;
}
}

static void OnResolve(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceId, DNSServiceErrorType err,
Expand Down
3 changes: 2 additions & 1 deletion src/platform/Darwin/DnssdImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ class MdnsContexts
void SetHostname(const char * name) { mHostname = name; }
const char * GetHostname() { return mHostname.c_str(); }

void Delete(GenericContext * context);

private:
MdnsContexts(){};
static MdnsContexts sInstance;
std::string mHostname;

void Delete(GenericContext * context);
std::vector<GenericContext *> mContexts;
};

Expand Down