Skip to content

Commit

Permalink
Remove the MDNS context handling, and fix the leak.
Browse files Browse the repository at this point in the history
  • Loading branch information
harimau-qirex committed Nov 18, 2021
1 parent c162ac6 commit 06a7272
Showing 1 changed file with 26 additions and 38 deletions.
64 changes: 26 additions & 38 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,40 +415,40 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin
uint8_t valueLen;
const void * valuePtr;

uint16_t recordCount = TXTRecordGetCount(txtLen, txtRecord);
for (uint16_t i = 0; i < recordCount; i++)
if (interfaceId != static_cast<uint32_t>(-1))
{
err = TXTRecordGetItemAtIndex(txtLen, txtRecord, i, kDnssdKeyMaxSize, key, &valueLen, &valuePtr);
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);
uint16_t recordCount = TXTRecordGetCount(txtLen, txtRecord);
for (uint16_t i = 0; i < recordCount; i++)
{
err = TXTRecordGetItemAtIndex(txtLen, txtRecord, i, kDnssdKeyMaxSize, key, &valueLen, &valuePtr);
VerifyOrReturnError(CheckForSuccess(sdCtx, __func__, err, true), CHIP_ERROR_INTERNAL);

strncpy(value, reinterpret_cast<const char *>(valuePtr), valueLen);
value[valueLen] = 0;
strncpy(value, reinterpret_cast<const char *>(valuePtr), valueLen);
value[valueLen] = 0;

sdCtx->textEntries.push_back(TextEntry{ strdup(key), reinterpret_cast<const uint8_t *>(strdup(value)), valueLen });
}
sdCtx->textEntries.push_back(TextEntry{ strdup(key), reinterpret_cast<const uint8_t *>(strdup(value)), valueLen });
}

DNSServiceProtocol protocol;
DNSServiceProtocol protocol;

#if INET_CONFIG_ENABLE_IPV4
if (addressType == chip::Inet::IPAddressType::kIPv4)
{
protocol = kDNSServiceProtocol_IPv4;
}
else if (addressType == chip::Inet::IPAddressType::kIPv6)
{
protocol = kDNSServiceProtocol_IPv6;
}
else
{
protocol = kDNSServiceProtocol_IPv4 | kDNSServiceProtocol_IPv6;
}
if (addressType == chip::Inet::IPAddressType::kIPv4)
{
protocol = kDNSServiceProtocol_IPv4;
}
else if (addressType == chip::Inet::IPAddressType::kIPv6)
{
protocol = kDNSServiceProtocol_IPv6;
}
else
{
protocol = kDNSServiceProtocol_IPv4 | kDNSServiceProtocol_IPv6;
}
#else
// without IPv4, IPv6 is the only option
protocol = kDNSServiceProtocol_IPv6;
// without IPv4, IPv6 is the only option
protocol = kDNSServiceProtocol_IPv6;
#endif

if (interfaceId != static_cast<uint32_t>(-1))
{
// -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);
Expand All @@ -460,18 +460,6 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin
}
else
{
// -1 is the local only interface. In this case, we can use localhost ::1
/*
// This fails to compile because sdRef is uninitialised. Initialising it to nullptr causes Add to fail.
CHIP_ERROR chip_err = MdnsContexts::GetInstance().Add(sdCtx, sdRef);
if (chip_err != CHIP_NO_ERROR)
{
// TODO: Above doesn't check the error return, but it does seem like we want to trigger the callback if this fails.
CheckForSuccess(sdCtx, __func__, kDNSServiceErr_Unknown, true);
return chip_err;
}
*/

sockaddr_in6 sockaddr;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin6_len = sizeof(sockaddr);
Expand All @@ -480,7 +468,7 @@ static CHIP_ERROR GetAddrInfo(void * context, DnssdResolveCallback callback, uin
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(sdRef, 0 /* flags */, interface, kDNSServiceErr_NoError, hostname, reinterpret_cast<struct sockaddr *>(&sockaddr),
OnGetAddrInfo(nullptr, 0 /* flags */, interface, kDNSServiceErr_NoError, hostname, reinterpret_cast<struct sockaddr *>(&sockaddr),
ttl, sdCtx);
return CHIP_NO_ERROR;
}
Expand Down

0 comments on commit 06a7272

Please sign in to comment.