Skip to content

Commit

Permalink
On Darwin, wait until GetAddrInfo returns all the results before call…
Browse files Browse the repository at this point in the history
…ing the resolve callback.
  • Loading branch information
bzbarsky-apple committed Feb 16, 2022
1 parent b683fec commit 711d868
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,35 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i

ChipLogDetail(DeviceLayer, "Mdns: %s hostname:%s", __func__, hostname);

chip::Inet::IPAddress ip;
CHIP_ERROR status = chip::Inet::IPAddress::GetIPAddressFromSockAddr(*address, ip);
if (status == CHIP_NO_ERROR)
{
sdCtx->addresses.push_back(ip);
}

if (flags & kDNSServiceFlagsMoreComing)
{
// Wait for that.
return;
}

DnssdService service = {};
service.mPort = sdCtx->port;
service.mTextEntries = sdCtx->textEntries.empty() ? nullptr : sdCtx->textEntries.data();
service.mTextEntrySize = sdCtx->textEntries.empty() ? 0 : sdCtx->textEntries.size();
chip::Inet::IPAddress ip;
CHIP_ERROR status = chip::Inet::IPAddress::GetIPAddressFromSockAddr(*address, ip);
service.mAddress.SetValue(ip);
// Use the first IP we got for the DnssdService.
if (sdCtx->addresses.size() != 0)
{
service.mAddress.SetValue(sdCtx->addresses.front());
sdCtx->addresses.erase(sdCtx->addresses.begin());
}
Platform::CopyString(service.mName, sdCtx->name);
Platform::CopyString(service.mHostName, hostname);
service.mInterface = Inet::InterfaceId(sdCtx->interfaceId);

sdCtx->callback(sdCtx->context, &service, Span<Inet::IPAddress>(), status);
// TODO: Does it really make sense to pass in the status from our last "get the IP address" operation?
sdCtx->callback(sdCtx->context, &service, Span<Inet::IPAddress>(sdCtx->addresses.data(), sdCtx->addresses.size()), status);
MdnsContexts::GetInstance().Remove(sdCtx);
}

Expand Down Expand Up @@ -512,6 +529,9 @@ static void OnResolve(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t inter

GetAddrInfo(sdCtx->context, sdCtx->callback, interfaceId, sdCtx->addressType, sdCtx->name, hostname, ntohs(port), txtLen,
txtRecord);

// TODO: If flags & kDNSServiceFlagsMoreComing should we keep waiting to see
// what else we resolve instead of calling Remove() here?
MdnsContexts::GetInstance().Remove(sdCtx);
}

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 @@ -91,6 +91,7 @@ struct ResolveContext : public GenericContext
struct GetAddrInfoContext : public GenericContext
{
DnssdResolveCallback callback;
std::vector<Inet::IPAddress> addresses;
std::vector<TextEntry> textEntries;
char name[Common::kInstanceNameMaxLength + 1];
uint32_t interfaceId;
Expand Down

0 comments on commit 711d868

Please sign in to comment.