Skip to content

Commit

Permalink
Change the ChipDnssdResolve API to be able to provide multiple IPs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Feb 16, 2022
1 parent 02d93e2 commit b683fec
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 23 deletions.
46 changes: 36 additions & 10 deletions src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace {
static DnssdCache<CHIP_CONFIG_MDNS_CACHE_SIZE> sDnssdCache;
#endif

static void HandleNodeResolve(void * context, DnssdService * result, CHIP_ERROR error)
static void HandleNodeResolve(void * context, DnssdService * result, const Span<Inet::IPAddress> & extraIPs, CHIP_ERROR error)
{
ResolverDelegateProxy * proxy = static_cast<ResolverDelegateProxy *>(context);

Expand All @@ -57,17 +57,27 @@ static void HandleNodeResolve(void * context, DnssdService * result, CHIP_ERROR
Platform::CopyString(nodeData.hostName, result->mHostName);
Platform::CopyString(nodeData.instanceName, result->mName);

size_t addressesFound = 0;
if (result->mAddress.HasValue())
{
nodeData.ipAddress[0] = result->mAddress.Value();
nodeData.interfaceId = result->mInterface;
nodeData.numIPs = 1;
nodeData.ipAddress[addressesFound] = result->mAddress.Value();
nodeData.interfaceId = result->mInterface;
++addressesFound;
}
else

for (auto & ip : extraIPs)
{
nodeData.numIPs = 0;
if (addressesFound == ArraySize(nodeData.ipAddress))
{
// Out of space.
break;
}
nodeData.ipAddress[addressesFound] = ip;
++addressesFound;
}

nodeData.numIPs = addressesFound;

nodeData.port = result->mPort;

for (size_t i = 0; i < result->mTextEntrySize; ++i)
Expand All @@ -81,7 +91,7 @@ static void HandleNodeResolve(void * context, DnssdService * result, CHIP_ERROR
proxy->Release();
}

static void HandleNodeIdResolve(void * context, DnssdService * result, CHIP_ERROR error)
static void HandleNodeIdResolve(void * context, DnssdService * result, const Span<Inet::IPAddress> & extraIPs, CHIP_ERROR error)
{
ResolverDelegateProxy * proxy = static_cast<ResolverDelegateProxy *>(context);
if (CHIP_NO_ERROR != error)
Expand Down Expand Up @@ -116,15 +126,31 @@ static void HandleNodeIdResolve(void * context, DnssdService * result, CHIP_ERRO
ResolvedNodeData nodeData;
Platform::CopyString(nodeData.mHostName, result->mHostName);
nodeData.mInterfaceId = result->mInterface;
nodeData.mAddress[0] = result->mAddress.ValueOr({});
nodeData.mPort = result->mPort;
nodeData.mNumIPs = 1;
nodeData.mPeerId = peerId;
// TODO: Use seconds?
const System::Clock::Timestamp currentTime = System::SystemClock().GetMonotonicTimestamp();

nodeData.mExpiryTime = currentTime + System::Clock::Seconds16(result->mTtlSeconds);

size_t addressesFound = 0;
if (result->mAddress.HasValue())
{
nodeData.mAddress[addressesFound] = result->mAddress.Value();
++addressesFound;
}
for (auto & ip : extraIPs)
{
if (addressesFound == ArraySize(nodeData.mAddress))
{
// Out of space.
break;
}
nodeData.mAddress[addressesFound] = ip;
++addressesFound;
}
nodeData.mNumIPs = addressesFound;

for (size_t i = 0; i < result->mTextEntrySize; ++i)
{
ByteSpan key(reinterpret_cast<const uint8_t *>(result->mTextEntries[i].mKey), strlen(result->mTextEntries[i].mKey));
Expand Down Expand Up @@ -155,7 +181,7 @@ static void HandleNodeBrowse(void * context, DnssdService * services, size_t ser
}
else
{
HandleNodeResolve(context, &services[i], error);
HandleNodeResolve(context, &services[i], Span<Inet::IPAddress>(), error);
}
}
proxy->Release();
Expand Down
5 changes: 4 additions & 1 deletion src/lib/dnssd/platform/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ struct DnssdService
*
* @param[in] context The context passed to ChipDnssdBrowse or ChipDnssdResolve.
* @param[in] result The mdns resolve result, can be nullptr if error happens.
* @param[in] extraIPs IP addresses, in addition to the one in "result", for
* the same hostname. Can be empty.
* @param[in] error The error code.
*
*/
using DnssdResolveCallback = void (*)(void * context, DnssdService * result, CHIP_ERROR error);
using DnssdResolveCallback = void (*)(void * context, DnssdService * result, const Span<Inet::IPAddress> & extraIPs,
CHIP_ERROR error);

/**
* The callback function for mDNS browse.
Expand Down
6 changes: 3 additions & 3 deletions src/platform/Darwin/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ bool CheckForSuccess(GenericContext * context, const char * name, DNSServiceErro
}
case ContextType::Resolve: {
ResolveContext * resolveContext = reinterpret_cast<ResolveContext *>(context);
resolveContext->callback(resolveContext->context, nullptr, CHIP_ERROR_INTERNAL);
resolveContext->callback(resolveContext->context, nullptr, Span<Inet::IPAddress>(), CHIP_ERROR_INTERNAL);
break;
}
case ContextType::GetAddrInfo: {
GetAddrInfoContext * resolveContext = reinterpret_cast<GetAddrInfoContext *>(context);
resolveContext->callback(resolveContext->context, nullptr, CHIP_ERROR_INTERNAL);
resolveContext->callback(resolveContext->context, nullptr, Span<Inet::IPAddress>(), CHIP_ERROR_INTERNAL);
break;
}
}
Expand Down Expand Up @@ -414,7 +414,7 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i
Platform::CopyString(service.mHostName, hostname);
service.mInterface = Inet::InterfaceId(sdCtx->interfaceId);

sdCtx->callback(sdCtx->context, &service, status);
sdCtx->callback(sdCtx->context, &service, Span<Inet::IPAddress>(), status);
MdnsContexts::GetInstance().Remove(sdCtx);
}

Expand Down
8 changes: 4 additions & 4 deletions src/platform/Linux/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,13 @@ void MdnsAvahi::HandleResolve(AvahiServiceResolver * resolver, AvahiIfIndex inte
if (resolver == nullptr)
{
ChipLogError(DeviceLayer, "Avahi resolve failed on retry");
context->mCallback(context->mContext, nullptr, CHIP_ERROR_INTERNAL);
context->mCallback(context->mContext, nullptr, Span<Inet::IPAddress>(), CHIP_ERROR_INTERNAL);
chip::Platform::Delete(context);
}
return;
}
ChipLogError(DeviceLayer, "Avahi resolve failed");
context->mCallback(context->mContext, nullptr, CHIP_ERROR_INTERNAL);
context->mCallback(context->mContext, nullptr, Span<Inet::IPAddress>(), CHIP_ERROR_INTERNAL);
break;
case AVAHI_RESOLVER_FOUND:
DnssdService result = {};
Expand Down Expand Up @@ -797,11 +797,11 @@ void MdnsAvahi::HandleResolve(AvahiServiceResolver * resolver, AvahiIfIndex inte

if (result_err == CHIP_NO_ERROR)
{
context->mCallback(context->mContext, &result, CHIP_NO_ERROR);
context->mCallback(context->mContext, &result, Span<Inet::IPAddress>(), CHIP_NO_ERROR);
}
else
{
context->mCallback(context->mContext, nullptr, result_err);
context->mCallback(context->mContext, nullptr, Span<Inet::IPAddress>(), result_err);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2283,7 +2283,8 @@ template <class ImplClass>
void GenericThreadStackManagerImpl_OpenThread<ImplClass>::DispatchResolve(intptr_t context)
{
auto * dnsResult = reinterpret_cast<DnsResult *>(context);
ThreadStackMgrImpl().mDnsResolveCallback(dnsResult->context, &(dnsResult->mMdnsService), dnsResult->error);
ThreadStackMgrImpl().mDnsResolveCallback(dnsResult->context, &(dnsResult->mMdnsService, Span<Inet::IPAddress>(),
dnsResult->error);
Platform::Delete<DnsResult>(dnsResult);
}

Expand Down
4 changes: 2 additions & 2 deletions src/platform/Tizen/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,12 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * data)
if (validIP)
{
mdnsService.mAddress.SetValue(ipStr);
rCtx->callback(rCtx->cbContext, &mdnsService, CHIP_NO_ERROR);
rCtx->callback(rCtx->cbContext, &mdnsService, chip::Span<chip::Inet::IPAddress>(), CHIP_NO_ERROR);
StopResolve(rCtx);
}
else
{
rCtx->callback(rCtx->cbContext, nullptr, CHIP_ERROR_INTERNAL);
rCtx->callback(rCtx->cbContext, nullptr, chip::Span<chip::Inet::IPAddress>(), CHIP_ERROR_INTERNAL);
RemoveContext(rCtx);
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/android/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ void HandleResolve(jstring instanceName, jstring serviceType, jstring address, j
const auto dispatch = [callbackHandle, contextHandle](CHIP_ERROR error, DnssdService * service = nullptr) {
DeviceLayer::StackLock lock;
DnssdResolveCallback callback = reinterpret_cast<DnssdResolveCallback>(callbackHandle);
callback(reinterpret_cast<void *>(contextHandle), service, error);
callback(reinterpret_cast<void *>(contextHandle), service, Span<Inet::IPAddress>(), error);
};

VerifyOrReturn(address != nullptr && port != 0, dispatch(CHIP_ERROR_UNKNOWN_RESOURCE_ID));
Expand Down
3 changes: 2 additions & 1 deletion src/platform/tests/TestDnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ using chip::Dnssd::DnssdService;
using chip::Dnssd::DnssdServiceProtocol;
using chip::Dnssd::TextEntry;

static void HandleResolve(void * context, DnssdService * result, CHIP_ERROR error)
static void HandleResolve(void * context, DnssdService * result, const chip::Span<chip::Inet::IPAddress> & extraIPs,
CHIP_ERROR error)
{
char addrBuf[100];
nlTestSuite * suite = static_cast<nlTestSuite *>(context);
Expand Down

0 comments on commit b683fec

Please sign in to comment.